summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/product_sla.py
blob: 2e663d30875256580815da427966874ea5c2e018 (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
from odoo import models, api, fields
from odoo.exceptions import AccessError, UserError, ValidationError
from datetime import timedelta, date
import logging
import math
_logger = logging.getLogger(__name__)


class ProductSla(models.Model):
    _name = 'product.sla'
    _description = 'Product Sla'
    _rec_name = 'product_variant_id'

    product_variant_id = fields.Many2one('product.product',string='Product')
    avg_leadtime = fields.Char(string='AVG Leadtime', readonly=True)
    leadtime = fields.Char(string='Leadtime', readonly=True)
    sla = fields.Char(string='SLA', readonly=True)
    version = fields.Integer(string="Version", compute="_compute_version")

    def _compute_version(self):
        for sla in self:
            sla.version = sla.product_variant_id.sla_version

    def generate_product_variant_id_sla(self, limit=5000):
    # Filter produk non-Altama
        products = self.env['product.product'].search([
            ('x_manufacture', 'not in', [10, 122, 89]),
            ('location_id', '=', 57),
            ('stock_move_ids', '!=', False),
        ], order='sla_version asc', limit=limit)

        i = 1
        for product in products:
            _logger.info(f'Product SLA: {i}/{len(products)}')
            i += 1
            product.sla_version += 1

            product_sla = self.search([('product_variant_id', '=', product.id)], limit=1)
            if not product_sla:
                product_sla = self.env['product.sla'].create({
                    'product_variant_id': product.id,
                })
 
            product_sla.generate_product_sla()

    def generate_product_sla(self):
        self.avg_leadtime = '-'
        self.sla = '-'

        product = self.product_variant_id

        qty_available = 0
        qty_available = product.qty_onhand_bandengan
        
        
        if qty_available > 0:
            self.sla = '1 Hari'

        query = [
            ('product_id', '=', product.id),
            ('picking_id', '!=', False),
            ('picking_id.location_id', '=', 57),    
            ('picking_id.state', 'not in', ['cancel'])
        ]
        picking = self.env['stock.move.line'].search(query)
        leadtimes=[]     
        for stock in picking:
            date_delivered = stock.picking_id.driver_departure_date
            date_so_confirmed = stock.picking_id.sale_id.date_order
            if date_delivered and date_so_confirmed:
                leadtime = date_delivered - date_so_confirmed
                leadtime_in_days = leadtime.days
                leadtimes.append(leadtime_in_days)
        
        if len(leadtimes) > 0:
            avg_leadtime = sum(leadtimes) / len(leadtimes)
            rounded_leadtime = math.ceil(avg_leadtime)
            self.avg_leadtime = rounded_leadtime
            if rounded_leadtime >= 1 and rounded_leadtime <= 5:
                self.sla = '3-7 Hari'
            elif rounded_leadtime >= 6 and rounded_leadtime <= 10:
                self.sla = '4-12 Hari'
            elif rounded_leadtime >= 11:
                self.sla = 'Indent'