summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/report_logbook_sj.py
blob: e67ea72492662dd0f7352d1f1fb10bad5b00870c (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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'
    _description = "Logbook SJ"
    _inherit = ['mail.thread']
    _rec_name = 'name'

    name = fields.Char(string='Name', default='Logbook SJ')
    date = fields.Datetime(string='Date Created')
    date_approve = fields.Datetime(string='Date Approve', tracking=3)
    approve_by_finance = fields.Boolean(string='Approve By Finance', tracking=3)
    approve_by = fields.Many2one(comodel_name='res.users', string='Approve By', tracking=3)
    created_by = fields.Many2one(comodel_name='res.users', string='Created By', tracking=3)
    report_logbook_sj_line = fields.One2many(
        comodel_name='report.logbook.sj.line',
        inverse_name='report_logbook_sj_id',
        string='Logbook SJ Line'
    )
    state = fields.Selection(
        [('belum_terima', 'Belum Terima'),
         ('terima_sebagian', 'Terima Sebagian'),
         ('terima_semua', 'Sudah di terima semua'),
        ],
        default='terima_semua',
        string='Status',
        tracking=True,
    )
    
    sj_number = fields.Char(string='Picking', related='report_logbook_sj_line.name')

    count_line = fields.Char(string='Count Line', compute='_compute_count_line')

    @api.depends('report_logbook_sj_line')
    def _compute_count_line(self):
        for rec in self:
            rec.count_line = len(rec.report_logbook_sj_line)

    @api.model
    def create(self, vals):
        vals['name'] = self.env['ir.sequence'].next_by_code('report.logbook.sj') or '0'
        # if self.env.user.has_group('indoteknik_custom.group_role_logistic'):
        #     self.action_send_to_telegram()
        result = super(ReportLogbookSJ, self).create(vals)
        return result

    def approve(self):
        current_time = datetime.utcnow()
        if self.env.user.is_accounting:
            self.approve_by_finance = True
            self.date_approve = current_time
            self.approve_by = self.env.user.id
            if any(line.not_exist for line in self.report_logbook_sj_line):
                if all(line.not_exist for line in self.report_logbook_sj_line):
                    self.state = 'belum_terima'
                else:
                    self.state = 'terima_sebagian'
            else:
                self.state = 'terima_semua'
        else:
            raise UserError('Hanya Accounting yang bisa Approve')
    

    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 = f"{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

        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'

    name = fields.Char(string='SJ Number')
    driver_id = fields.Many2one(comodel_name='res.users', string='Driver')
    departure_date = fields.Char(string='Departure Date')
    arrival_date = fields.Char(string='Arrival Date')
    carrier_id = fields.Many2one('delivery.carrier', string='Shipping Method')
    tracking_no = fields.Char(string='Tracking No')
    logbook_sj_id = fields.Many2one('report.logbook.sj', string='Logbook SJ')  # Corrected model name
    partner_id = fields.Many2one('res.partner', string='Customer')
    picking_id = fields.Many2one('stock.picking', string='Picking')
    sale_id = fields.Many2one('sale.order', string='Sale Order')
    report_logbook_sj_id = fields.Many2one('report.logbook.sj', string='Logbook SJ')
    not_exist = fields.Boolean(string='Not Exist')
    note = fields.Char(string='Note')