diff options
Diffstat (limited to 'indoteknik_custom/models/sj_tele.py')
| -rw-r--r-- | indoteknik_custom/models/sj_tele.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/indoteknik_custom/models/sj_tele.py b/indoteknik_custom/models/sj_tele.py new file mode 100644 index 00000000..c359d6d8 --- /dev/null +++ b/indoteknik_custom/models/sj_tele.py @@ -0,0 +1,59 @@ +from odoo import models, fields, api +import requests +import json +import logging + +_logger = logging.getLogger(__name__) + +class SjTele(models.Model): + _name = 'sj.tele' + _description = 'sj.tele' + + + picking_id = fields.Many2one('stock.picking', string='Picking') + picking_name = fields.Char(string='Picking Name') + create_date = fields.Datetime(string='Create Date') + + @api.model + 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" + try: + resp = requests.post(apiURL, json={'chat_id': chat_id_mqdd, 'text': text}, timeout=15) + except Exception as e: + logging.getLogger(__name__).exception("Gagal kirim Telegram: %s", e) + return True + + 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 + 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 + except Exception as e: + logging.getLogger(__name__).exception("Gagal kirim Telegram: %s", e) + return True
\ No newline at end of file |
