diff options
| author | Miqdad <ahmadmiqdad27@gmail.com> | 2025-09-04 18:38:12 +0700 |
|---|---|---|
| committer | Miqdad <ahmadmiqdad27@gmail.com> | 2025-09-04 18:38:12 +0700 |
| commit | 666232252b4f5c5626bb5f276f645d4989495fa1 (patch) | |
| tree | 9955566b0bfdf2315ed086caed827fe6cda32654 | |
| parent | dd2bc67bbdaa771adf6bbedc01ba23a98ea03574 (diff) | |
<Miqdad> Add SO
| -rw-r--r-- | indoteknik_custom/models/sj_tele.py | 60 |
1 files changed, 27 insertions, 33 deletions
diff --git a/indoteknik_custom/models/sj_tele.py b/indoteknik_custom/models/sj_tele.py index c359d6d8..50e47e78 100644 --- a/indoteknik_custom/models/sj_tele.py +++ b/indoteknik_custom/models/sj_tele.py @@ -11,49 +11,43 @@ class SjTele(models.Model): picking_id = fields.Many2one('stock.picking', string='Picking') + sale_id = fields.Many2one('sale.order', string='Sales Order') picking_name = fields.Char(string='Picking Name') create_date = fields.Datetime(string='Create Date') - @api.model - def woi(self): + def woi(self): bot_mqdd = '8203414501:AAHy_XwiUAVrgRM2EJzW7sZx9npRLITZpb8' chat_id_mqdd = '-4885333032' - apiURL = f'https://api.telegram.org/bot{bot_mqdd}/sendMessage' - - self.env.cr.execute(""" - SELECT - COALESCE(sp.id, st.picking_id) AS pid, - COALESCE(sp.name, st.picking_name) AS pname - FROM sj_tele st - LEFT JOIN stock_picking sp - ON sp.id = st.picking_id - LEFT JOIN report_logbook_sj_line rlsl - ON (rlsl.picking_id = COALESCE(sp.id, st.picking_id)) - OR (rlsl.name IS NOT NULL AND rlsl.name = COALESCE(sp.name, st.picking_name)) - WHERE rlsl.id IS NULL - AND COALESCE(sp.name, st.picking_name) IS NOT NULL - ORDER BY st.create_date ASC - LIMIT 20 - """) - rows = self.env.cr.fetchall() - - if not rows: - _logger.info("SJ Tele: tidak ada data untuk dikirim (staging kosong atau semua sudah di logbook).") - text = "Selamat anda menamatkan Logbook SJ Report" + api_base = f'https://api.telegram.org/bot{bot_mqdd}' + + data = self.search([], order='create_date asc', limit=15) + + if not data: + text = "Berikut merupakan nomor BU/OUT yang belum ada di Logbook SJ report:\n✅ tidak ada data (semua sudah tercatat)." try: - resp = requests.post(apiURL, json={'chat_id': chat_id_mqdd, 'text': text}, timeout=15) + r = requests.post(api_base + "/sendMessage", + json={'chat_id': chat_id_mqdd, 'text': text}, + timeout=20) + r.raise_for_status() except Exception as e: - logging.getLogger(__name__).exception("Gagal kirim Telegram: %s", e) + _logger.exception("Gagal kirim Telegram (no data): %s", e) return True + lines = [] + for rec in data: + name = rec.picking_name or (rec.picking_id.name if rec.picking_id else '') + pid = rec.picking_id.id if rec.picking_id else '-' + so = rec.sale_id.name if rec.sale_id else '-' + if name: + lines.append(f"{name} - {so} ({pid})") + header = "Berikut merupakan nomor BU/OUT yang belum ada di Logbook SJ report:\n" - body = "\n".join(f"{name} ({pid})" for pid, name in rows if name) - text = header + body + text = header + "\n".join(lines) + try: - resp = requests.post(apiURL, json={'chat_id': chat_id_mqdd, 'text': text}, timeout=15) - except requests.HTTPError: - _logger.error("Telegram response: %s", resp.text) - raise + r = requests.post(api_base + "/sendMessage", + json={'chat_id': chat_id_mqdd, 'text': text}) + r.raise_for_status() except Exception as e: - logging.getLogger(__name__).exception("Gagal kirim Telegram: %s", e) + _logger.exception("Gagal kirim Telegram: %s", e) return True
\ No newline at end of file |
