summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/vendor_sla.py
blob: b052e6cb47d66006eb1a1c9f0266b65e115b3343 (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
from odoo import models, fields, api
import logging
import math
_logger = logging.getLogger(__name__)

class VendorSLA(models.Model):
    _name = 'vendor.sla'
    _description = 'Vendor SLA'
    _rec_name = 'id_vendor'
    
    id_vendor = fields.Many2one('res.partner', string='Name', domain="[('industry_id', '=', 46)]")
    duration = fields.Integer(string='Duration', description='SLA Duration')
    unit = fields.Selection(
        [('jam', 'Jam'),('hari', 'Hari')],
        string="SLA Time"
    )
    duration_unit = fields.Char(string="Duration (Unit)", compute="_compute_duration_unit")

        # pertama, lakukan group by vendor pada modul purchase.order
        # kedua, pada setiap Purchase order pada group by vendor tersebut, lakukan penghitungan penjumlahan setiap nilai datetime field date_planed dikurangi date_approve purchase order
        # dibagi jumlah data dari setiap Purchase order pada group by vendor tersebut. hasilnya lalu di gunakan untuk mengset nilai duration
    def generate_vendor_id_sla(self):
        # Step 1: Group stock pickings by vendor based on operation type
        stock_picking_env = self.env['stock.picking']
        stock_moves = stock_picking_env.read_group(
            domain=[
                ('state', '=', 'done'),
                ('location_id', '=', 4),  # Partner Locations/Vendors
                ('location_dest_id', '=', 57)  # BU/Stock
            ],
            fields=['partner_id', 'date_done', 'scheduled_date'],
            groupby=['partner_id'],
            lazy=False
        )

        for group in stock_moves:
            partner_id = group['partner_id'][0]
            total_duration = 0
            count = 0

            # Step 2: Calculate the average duration for each vendor
            pos_for_vendor = stock_picking_env.search([
                ('partner_id', '=', partner_id),
                ('state', '=', 'done'),
            ])

            for po in pos_for_vendor:
                if po.date_done and po.purchase_id.date_approve:
                    date_of_transfer = fields.Datetime.to_datetime(po.date_done)
                    po_confirmation_date = fields.Datetime.to_datetime(po.purchase_id.date_approve)
                    if date_of_transfer < po_confirmation_date: continue
                    
                    days_difference = (date_of_transfer - po_confirmation_date).days
                    if days_difference > 14:
                        continue
                    duration = (date_of_transfer - po_confirmation_date).total_seconds() / 3600  # Convert to hours
                    total_duration += duration
                    count += 1

            if count > 0:
                average_duration = total_duration / count

                # Step 3: Update the duration field in the corresponding res.partner record
                vendor_sla = self.search([('id_vendor', '=', partner_id)], limit=1)

                # Konversi jam ke hari jika diperlukan
                if average_duration >= 24:
                    days = average_duration / 24
                    if days - int(days) > 0.5:  # Jika sisa lebih dari 0,5, bulatkan ke atas
                        days = int(days) + 1
                    else:  # Jika sisa 0,5 atau kurang, bulatkan ke bawah
                        days = int(days)
                    duration_to_save = days
                    unit_to_save = 'hari'
                else:
                    duration_to_save = round(average_duration)
                    unit_to_save = 'jam'

                # Update atau create vendor SLA record
                if vendor_sla:
                    vendor_sla.write({
                        'duration': duration_to_save,
                        'unit': unit_to_save
                    })
                else:
                    self.create({
                        'id_vendor': partner_id,
                        'duration': duration_to_save,
                        'unit': unit_to_save
                    })
        _logger.info(f'Proses SLA untuk Vendor selesai dilakukan')

    @api.depends('duration', 'unit')
    def _compute_duration_unit(self):
        for record in self:
            if record.duration and record.unit:
                record.duration_unit = f"{record.duration} {record.unit}"
            else:
                record.duration_unit = "-"