diff options
| author | Miqdad <ahmadmiqdad27@gmail.com> | 2025-09-03 18:05:00 +0700 |
|---|---|---|
| committer | Miqdad <ahmadmiqdad27@gmail.com> | 2025-09-03 18:05:00 +0700 |
| commit | d956020b6b588e5216f1238b82f87b6fb35b2afc (patch) | |
| tree | a0c1f64ed17abddf685f6525d4ced5a3d5d5da16 /indoteknik_custom/models | |
| parent | a870c651045725bb77d621bdcbe6b610d0b52b49 (diff) | |
<Miqdad> send bu out to tele
Diffstat (limited to 'indoteknik_custom/models')
| -rw-r--r-- | indoteknik_custom/models/report_logbook_sj.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/indoteknik_custom/models/report_logbook_sj.py b/indoteknik_custom/models/report_logbook_sj.py index 17119c12..85ab755d 100644 --- a/indoteknik_custom/models/report_logbook_sj.py +++ b/indoteknik_custom/models/report_logbook_sj.py @@ -2,6 +2,11 @@ from odoo import models, fields, api from odoo.exceptions import UserError from pytz import timezone from datetime import datetime +import requests +import json +import logging + +_logger = logging.getLogger(__name__) class ReportLogbookSJ(models.Model): _name = 'report.logbook.sj' @@ -60,6 +65,84 @@ class ReportLogbookSJ(models.Model): self.state = 'terima_semua' else: raise UserError('Hanya Accounting yang bisa Approve') + + + @api.model + def cron_daily_logbook_gap_to_telegram(self): + bot_mqdd = '8203414501:AAHy_XwiUAVrgRM2EJzW7sZx9npRLITZpb8' + chat_id_mqdd = '-4885333032' + apiURL = f'https://api.telegram.org/bot{bot_mqdd}/sendMessage' + + # ambil dari 1 Februari 2024 (UTC) + dt_from = datetime(2024, 2, 1, 0, 0, 0) + + self.env.cr.execute(""" + SELECT sp.id, sp.name + FROM stock_picking sp + LEFT JOIN report_logbook_sj_line rlsl + ON rlsl.picking_id = sp.id + OR (rlsl.name IS NOT NULL AND rlsl.name = sp.name) + WHERE sp.picking_type_id = 29 + AND sp.state = 'done' + AND sp.create_date >= %s + AND rlsl.id IS NULL + ORDER BY sp.create_date ASC limit 20 + """, (dt_from,)) + rows = self.env.cr.fetchall() + + if not rows: + return True + + header = "berikut merupakan nomor picking yang belum ada di Logbook SJ report:\n" + body = "\n".join(f"{name} ({pid})" for pid, name in rows if name) + text = header + body + + # kirim satu pesan (tanpa pemotongan) + try: + resp = requests.post(apiURL, json={'chat_id': chat_id_mqdd, 'text': text}, timeout=15) + resp.raise_for_status() + except requests.HTTPError: + _logger.error("Telegram response: %s", resp.text) # <— LIHAT DESKRIPSINYA DI LOG + raise + except Exception as e: + # log saja; biar cron tidak crash + logging.getLogger(__name__).exception("Gagal kirim Telegram: %s", e) + return True + + def action_send_to_telegram(self): + entries = [] + pickings = self.report_logbook_sj_line.mapped('picking_id') + for p in pickings: + if p: + entries.append((p.name, p.id)) + + fallback_names = [l.name for l in self.report_logbook_sj_line if not l.picking_id and l.name] + if fallback_names: + picks = self.env['stock.picking'].search([('name', 'in', list(set(fallback_names)))]) + name2id = {p.name: p.id for p in picks} + for n in fallback_names: + entries.append((n, name2id.get(n))) + + seen, unique_entries = set(), [] + for name, pid in entries: + key = pid or name + if key and key not in seen: + seen.add(key) + unique_entries.append((name, pid)) + + header = "berikut merupakan nomor picking yang belum ada di Logbook SJ report:\n" + body = "\n".join(f"{name} ({pid or '-'})" for name, pid in unique_entries) if unique_entries else "- (tidak ada)" + text = header + body + + bot_mqdd = '8203414501:AAHy_XwiUAVrgRM2EJzW7sZx9npRLITZpb8' + chat_id_mqdd = '-4885333032' + apiURL = f'https://api.telegram.org/bot{bot_mqdd}/sendMessage' + try: + hehe = requests.post(apiURL, json={'chat_id': chat_id_mqdd, 'text': text}) + _logger.info(hehe) + except Exception as e: + print(e) + class ReportLogbookSJLine(models.Model): _name = 'report.logbook.sj.line' |
