summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/sj_tele.py
blob: c359d6d876340daffb4faf76e3bf3ae5e03b5f6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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