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 | |
| parent | a870c651045725bb77d621bdcbe6b610d0b52b49 (diff) | |
<Miqdad> send bu out to tele
| -rw-r--r-- | indoteknik_custom/models/report_logbook_sj.py | 83 | ||||
| -rw-r--r-- | indoteknik_custom/views/report_logbook_sj.xml | 91 |
2 files changed, 133 insertions, 41 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' diff --git a/indoteknik_custom/views/report_logbook_sj.xml b/indoteknik_custom/views/report_logbook_sj.xml index 94f6c2ab..6458af40 100644 --- a/indoteknik_custom/views/report_logbook_sj.xml +++ b/indoteknik_custom/views/report_logbook_sj.xml @@ -12,9 +12,10 @@ <field name="date_approve"/> <field name="approve_by_finance"/> <field name="state"/> + </tree> </field> - </record> + </record> <record id="report_logbook_sj_line_tree" model="ir.ui.view"> <field name="name">report.logbook.sj.line.tree</field> @@ -42,50 +43,48 @@ <field name="arch" type="xml"> <form> <header> - <button name="approve" - string="Validate" - type="object" - /> + <button name="approve" string="Validate" type="object" /> + <button name="action_send_to_telegram" string="Kirim ke Telegram" type="object" /> </header> <sheet string="Report logbook SJ"> - <div class="oe_button_box" name="button_box"/> + <div class="oe_button_box" name="button_box"/> + <group> <group> - <group> - <field name="name" readonly="1"/> - <field name="date" readonly="1"/> - <field name="date_approve" readonly="1"/> - </group> - <group> - <field name="approve_by_finance" readonly="1"/> - <field name="state" readonly="1"/> - <field name="created_by" readonly="1"/> - <field name="approve_by" readonly="1"/> - <field name="count_line" readonly="1"/> - </group> + <field name="name" readonly="1"/> + <field name="date" readonly="1"/> + <field name="date_approve" readonly="1"/> </group> - <notebook> - <page string="Line"> - <field name="report_logbook_sj_line"/> - </page> - </notebook> - </sheet> - <div class="oe_chatter"> - <field name="message_follower_ids" widget="mail_followers"/> - <field name="message_ids" widget="mail_thread"/> - </div> + <group> + <field name="approve_by_finance" readonly="1"/> + <field name="state" readonly="1"/> + <field name="created_by" readonly="1"/> + <field name="approve_by" readonly="1"/> + <field name="count_line" readonly="1"/> + </group> + </group> + <notebook> + <page string="Line"> + <field name="report_logbook_sj_line"/> + </page> + </notebook> + </sheet> + <div class="oe_chatter"> + <field name="message_follower_ids" widget="mail_followers"/> + <field name="message_ids" widget="mail_thread"/> + </div> </form> </field> </record> <record id="report_logbook_sj_view_search" model="ir.ui.view"> - <field name="name">report.logbook.sj.search.view</field> <!-- Made the name more descriptive --> - <field name="model">report.logbook.sj</field> - <field name="arch" type="xml"> - <search string="Search Report"> - <field name="sj_number"/> - </search> - </field> - </record> + <field name="name">report.logbook.sj.search.view</field> <!-- Made the name more descriptive --> + <field name="model">report.logbook.sj</field> + <field name="arch" type="xml"> + <search string="Search Report"> + <field name="sj_number"/> + </search> + </field> + </record> <record id="report_logbook_sj_action" model="ir.actions.act_window"> <field name="name">Report Logbook SJ</field> @@ -94,9 +93,19 @@ <field name="view_mode">tree,form</field> </record> - <menuitem id="menu_report_logbook_sj" - name="Report Logbook SJ" - action="report_logbook_sj_action" - parent="account.menu_finance_reports" - sequence="200"/> + <menuitem id="menu_report_logbook_sj" name="Report Logbook SJ" action="report_logbook_sj_action" parent="account.menu_finance_reports" sequence="200"/> + + <data noupdate="1"> + <record id="cron_daily_logbook_gap_to_telegram" model="ir.cron"> + <field name="name">Daily Logbook SJ Gap → Telegram</field> + <field name="model_id" ref="model_report_logbook_sj"/> + <field name="state">code</field> + <field name="code">model.cron_daily_logbook_gap_to_telegram()</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 |
