summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorit-fixcomart <it@fixcomart.co.id>2025-02-03 14:45:18 +0700
committerit-fixcomart <it@fixcomart.co.id>2025-02-03 14:45:18 +0700
commitd0647f4b4a0df94c7b51852823df37eeb5b89e3e (patch)
tree537a44b2d48009fb1049f0ce816ec6ab784dc9a8
parent74796f3390ad90e2ae981351cd0491aca6dccc9c (diff)
<iman> add generate vendor sla
-rw-r--r--indoteknik_custom/models/vendor_sla.py75
1 files changed, 72 insertions, 3 deletions
diff --git a/indoteknik_custom/models/vendor_sla.py b/indoteknik_custom/models/vendor_sla.py
index 9af86a14..67b6ffc3 100644
--- a/indoteknik_custom/models/vendor_sla.py
+++ b/indoteknik_custom/models/vendor_sla.py
@@ -1,4 +1,7 @@
from odoo import models, fields, api
+import logging
+import math
+_logger = logging.getLogger(__name__)
class VendorSLA(models.Model):
_name = 'vendor.sla'
@@ -12,15 +15,81 @@ class VendorSLA(models.Model):
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 purchase orders by vendor (partner_id)
+ po_env = self.env['purchase.order']
+ pos = po_env.read_group(
+ domain=[('state', '=', 'done')],
+ fields=['partner_id', 'date_planned', 'date_approve'],
+ groupby=['partner_id'],
+ lazy=False
+ )
+
+ for group in pos:
+ partner_id = group['partner_id'][0]
+ total_duration = 0
+ count = 0
+
+ # Step 2: Calculate the average duration for each vendor
+ pos_for_vendor = po_env.search([
+ ('partner_id', '=', partner_id),
+ ('state', '=', 'done'),
+ ('date_planned', '>=', '2023-01-01')
+ ])
+
+ for po in pos_for_vendor:
+ if po.date_planned and po.date_approve:
+ date_planned = fields.Datetime.to_datetime(po.date_planned)
+ date_approve = fields.Datetime.to_datetime(po.date_approve)
+ if date_planned < date_approve: continue
+ duration = (date_planned - date_approve).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 = ""
+ record.duration_unit = "-"
\ No newline at end of file