diff options
| -rwxr-xr-x | indoteknik_custom/__manifest__.py | 1 | ||||
| -rwxr-xr-x | indoteknik_custom/models/__init__.py | 3 | ||||
| -rw-r--r-- | indoteknik_custom/models/report_logbook_sj.py | 51 | ||||
| -rw-r--r-- | indoteknik_custom/models/sj_tele.py | 59 | ||||
| -rwxr-xr-x | indoteknik_custom/security/ir.model.access.csv | 3 | ||||
| -rw-r--r-- | indoteknik_custom/views/sj_tele.xml | 15 |
6 files changed, 87 insertions, 45 deletions
diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index 31685005..d852d2e1 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -177,6 +177,7 @@ 'views/tukar_guling_po.xml', # 'views/refund_sale_order.xml', 'views/update_date_planned_po_wizard_view.xml', + 'views/sj_tele.xml' ], 'demo': [], 'css': [], diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 3a9f9312..c0aa7085 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -156,4 +156,5 @@ from . import refund_sale_order # from . import patch from . import tukar_guling from . import tukar_guling_po -from . import update_date_planned_po_wizard
\ No newline at end of file +from . import update_date_planned_po_wizard +from . import sj_tele
\ No newline at end of file diff --git a/indoteknik_custom/models/report_logbook_sj.py b/indoteknik_custom/models/report_logbook_sj.py index 85ab755d..6915ad9b 100644 --- a/indoteknik_custom/models/report_logbook_sj.py +++ b/indoteknik_custom/models/report_logbook_sj.py @@ -39,6 +39,10 @@ class ReportLogbookSJ(models.Model): count_line = fields.Char(string='Count Line', compute='_compute_count_line') + def write(self, vals): + self.action_send_to_telegram() + res = super(ReportLogbookSJ, self).write(vals) + @api.depends('report_logbook_sj_line') def _compute_count_line(self): for rec in self: @@ -67,49 +71,10 @@ class ReportLogbookSJ(models.Model): 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): + user_logistic = self.env.ref('indoteknik_custom.group_role_logistic') + if self.user != user_logistic: + return entries = [] pickings = self.report_logbook_sj_line.mapped('picking_id') for p in pickings: @@ -130,7 +95,7 @@ class ReportLogbookSJ(models.Model): seen.add(key) unique_entries.append((name, pid)) - header = "berikut merupakan nomor picking yang belum ada di Logbook SJ report:\n" + header = f"Saya {self.env.user.name} sudah mengisi 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 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 diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index 3a320510..0d2789b3 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -195,4 +195,5 @@ access_tukar_guling_mapping_koli_all_users,tukar.guling.mapping.koli.all.users,m access_purchase_order_update_date_wizard,access.purchase.order.update.date.wizard,model_purchase_order_update_date_wizard,base.group_user,1,1,1,1 access_sync_promise_date_wizard,access.sync.promise.date.wizard,model_sync_promise_date_wizard,base.group_user,1,1,1,1 access_sync_promise_date_wizard_line,access.sync.promise.date.wizard.line,model_sync_promise_date_wizard_line,base.group_user,1,1,1,1 -access_change_date_planned_wizard,access.change.date.planned.wizard,model_change_date_planned_wizard,,1,1,1,1
\ No newline at end of file +access_change_date_planned_wizard,access.change.date.planned.wizard,model_change_date_planned_wizard,,1,1,1,1 +access_sj_tele,access.sj.tele,model_sj_tele,base.group_system,1,1,1,1
\ No newline at end of file diff --git a/indoteknik_custom/views/sj_tele.xml b/indoteknik_custom/views/sj_tele.xml new file mode 100644 index 00000000..cefcc968 --- /dev/null +++ b/indoteknik_custom/views/sj_tele.xml @@ -0,0 +1,15 @@ +<odoo> + <data noupdate="1"> + <record id="woi" model="ir.cron"> + <field name="name">SJ TELE</field> + <field name="model_id" ref="model_sj_tele"/> + <field name="state">code</field> + <field name="code">model.woi()</field> + <field name="interval_number">1</field> + <field name="interval_type">days</field> + <field name="numbercall">-1</field> + <field name="user_id" ref="base.user_root"/> + <field name="active">False</field> + </record> + </data> +</odoo>
\ No newline at end of file |
