summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/product_sla.py
diff options
context:
space:
mode:
Diffstat (limited to 'indoteknik_custom/models/product_sla.py')
-rw-r--r--indoteknik_custom/models/product_sla.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/indoteknik_custom/models/product_sla.py b/indoteknik_custom/models/product_sla.py
new file mode 100644
index 00000000..00df2fa2
--- /dev/null
+++ b/indoteknik_custom/models/product_sla.py
@@ -0,0 +1,64 @@
+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'
+ 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)
+
+ def generate_product_variant_id_sla(self):
+ # Filter produk non-Altama
+ products = self.env['stock.move.line'].search([
+ # ('product_id', '=', self.product_variant_id.id),
+ ('product_id.x_manufacture', 'not in', [10,122,89]),
+ ('picking_id.driver_departure_date', '!=', False)
+ ])
+
+ for product in products:
+ sla = self.env['product.sla'].create({
+ 'product_variant_id': product.product_id.id,
+ })
+
+ sla.generate_product_sla()
+
+ def generate_product_sla(self):
+ for sla in self:
+ query = [
+ ('product_id', '=', sla.product_variant_id.id),
+ ('picking_id', '!=', False),
+ ('picking_id.state', '=', 'done'),
+ ]
+ picking = self.env['stock.move.line'].search(query)
+ leadtimes=[]
+ for stock in picking:
+ if not stock.picking_id.driver_departure_date:
+ continue
+ 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(float(avg_leadtime))
+ sla.avg_leadtime = rounded_leadtime
+
+ if picking.picking_id.location_id == 57 and picking.picking_id.qty_available > 10:
+ sla.sla = '1 Hari'
+ elif sla.avg_leadtime and float(sla.avg_leadtime) >= 1 and float(sla.avg_leadtime) <= 5:
+ sla.sla = '3-6 Hari'
+ elif sla.avg_leadtime and float(sla.avg_leadtime) >= 6 and float(sla.avg_leadtime) <= 10:
+ sla.sla = '4-12 Hari'
+ elif sla.avg_leadtime and float(sla.avg_leadtime) >= 11:
+ sla.sla = 'Indent'
+ else:
+ sla.sla = '-' \ No newline at end of file