diff options
| author | trisusilo48 <tri.susilo@altama.co.id> | 2025-04-30 16:43:38 +0700 |
|---|---|---|
| committer | trisusilo48 <tri.susilo@altama.co.id> | 2025-04-30 16:43:38 +0700 |
| commit | 7897614cee8a347dfdd933df72db95859cb1a558 (patch) | |
| tree | d491f296af49fcbfb5f2c4395232bc8eb8912ef9 | |
| parent | c77d250353dbed0ba1ec5cd8abd940ba95a011fc (diff) | |
fixing handle 15 pm, weekend, and holidays
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product.py | 7 | ||||
| -rwxr-xr-x | indoteknik_custom/models/product_template.py | 3 | ||||
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 71 |
3 files changed, 54 insertions, 27 deletions
diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 9566ed05..ecd37cda 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -49,10 +49,9 @@ class Product(controller.Controller): ]) jakarta = pytz.timezone("Asia/Jakarta") start_date = datetime.now(jakarta) - additional_days_after_3pm = request.env['sale.order'].handling_order_after_3pm(start_date) - start_date += timedelta(days=additional_days_after_3pm) additional_days = request.env['sale.order'].get_days_until_next_business_day(start_date) + include_instant = True if(len(products) == len(product_ids)): @@ -67,8 +66,8 @@ class Product(controller.Controller): return self.response({ 'include_instant': include_instant, 'sla_duration': 1, - 'sla_additional_days': additional_days, - 'sla_total' : 1 + int(additional_days), + 'sla_additional_days': int(additional_days), + 'sla_total' : int(additional_days), 'sla_unit': 'Hari' }) diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index e6a01a04..56ae3087 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -341,6 +341,9 @@ class ProductTemplate(models.Model): 'search_key':[item_code], } response = requests.post(url, headers=headers, json=json_data) + if response.status_code != 200: + return 0 + datas = json.loads(response.text)['data'] qty = 0 for data in datas: diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 25c1b3a8..795bfa0b 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -565,29 +565,58 @@ class SaleOrder(models.Model): def get_days_until_next_business_day(self, start_date=None, *args, **kwargs): jakarta = pytz.timezone("Asia/Jakarta") - current = datetime.now(jakarta) - + now = datetime.now(jakarta) + + if start_date is None: + start_date = now + + if start_date.tzinfo is None: + start_date = jakarta.localize(start_date) + + holiday = self.env['hr.public.holiday'] + batas_waktu = datetime.strptime("15:00", "%H:%M").time() + current_day = start_date.date() offset = 0 + + # Step 1: Lewat jam 15 → Tambah 1 hari + if start_date.time() > batas_waktu: + offset += 1 + + # Step 2: Hitung hari libur selama offset itu + i = 0 + total_days = 0 + while i < offset: + current_day += timedelta(days=1) + total_days += 1 + is_weekend = current_day.weekday() >= 5 + is_holiday = holiday.search([("start_date", "=", current_day)]) + if not is_weekend and not is_holiday: + i += 1 # hanya hitung hari kerja + + # Step 3: Tambah 1 hari masa persiapan gudang i = 0 + while i < 1: + current_day += timedelta(days=1) + total_days += 1 + is_weekend = current_day.weekday() >= 5 + is_holiday = holiday.search([("start_date", "=", current_day)]) + if not is_weekend and not is_holiday: + i += 1 - current_day = start_date.date() - holiday = self.env['hr.public.holiday'] - + # Step 4: Kalau current_day ternyata weekend/libur, cari hari kerja berikutnya while True: - # # Tambah satu hari, cek apakah hari kerja - current_day += timedelta(days=i) - i = 1 - - # Lewati weekend is_weekend = current_day.weekday() >= 5 is_holiday = holiday.search([("start_date", "=", current_day)]) if is_weekend or is_holiday: - offset += 1 - continue - - break + current_day += timedelta(days=1) + total_days += 1 + else: + break + + final_offset = (current_day - start_date.date()).days + return final_offset + - return offset def calculate_sla_by_vendor(self, products): product_ids = products.mapped('product_id.id') # Kumpulkan semua ID produk @@ -637,11 +666,9 @@ class SaleOrder(models.Model): max_slatime = 1 # Default SLA jika tidak ada slatime = self.calculate_sla_by_vendor(rec.order_line) max_slatime = max(max_slatime, slatime['slatime']) - - days_after_3pm = self.handling_order_after_3pm(current_date) - date_after_3pm = current_date + timedelta(days=days_after_3pm) - sum_days = max_slatime + self.get_days_until_next_business_day(date_after_3pm) + sum_days = max_slatime + self.get_days_until_next_business_day(current_date) + sum_days -= 1 if not rec.estimated_arrival_days: rec.estimated_arrival_days = sum_days @@ -665,11 +692,9 @@ class SaleOrder(models.Model): max_slatime = 1 # Default SLA jika tidak ada slatime = self.calculate_sla_by_vendor(rec.order_line) max_slatime = max(max_slatime, slatime['slatime']) - - days_after_3pm = self.handling_order_after_3pm(current_date) - date_after_3pm = current_date + timedelta(days=days_after_3pm) - sum_days = max_slatime + self.get_days_until_next_business_day(date_after_3pm) + sum_days = max_slatime + self.get_days_until_next_business_day(current_date) + sum_days -= 1 eta_minimum = current_date + timedelta(days=sum_days) if expected_date < eta_minimum.date(): |
