diff options
| author | HafidBuroiroh <hafidburoiroh09@gmail.com> | 2026-03-13 14:09:09 +0700 |
|---|---|---|
| committer | HafidBuroiroh <hafidburoiroh09@gmail.com> | 2026-03-13 14:09:09 +0700 |
| commit | 8c5101926d38bf7d24a1a418910a017531ee7c49 (patch) | |
| tree | 94e7155cd47f2dff50cc39406d22717fb480dab5 | |
| parent | b29d8538eb4b2f42ff60e896f9a1647e962d8ffc (diff) | |
<hafid> fix vendor purchase pricelist
| -rw-r--r-- | indoteknik_custom/models/sourcing_job_order.py | 70 |
1 files changed, 67 insertions, 3 deletions
diff --git a/indoteknik_custom/models/sourcing_job_order.py b/indoteknik_custom/models/sourcing_job_order.py index a93368dc..b35f33a7 100644 --- a/indoteknik_custom/models/sourcing_job_order.py +++ b/indoteknik_custom/models/sourcing_job_order.py @@ -1,6 +1,6 @@ from odoo import models, fields, api, _ from odoo.exceptions import UserError -from datetime import date, datetime +from datetime import date, datetime, timedelta import requests import logging import pytz @@ -842,7 +842,6 @@ class SourcingJobOrderLine(models.Model): 'taxes_product_id': rec.tax_id.id if rec.tax_id else False, 'brand_id': product.x_manufacture.id if product.x_manufacture else False, 'human_last_update': jakarta_tz, - 'is_winner': True, } if not purchase_price: @@ -860,9 +859,10 @@ class SourcingJobOrderLine(models.Model): "product_uom_qty": rec.quantity or 1, "price_unit": rec.price or 0, "name": rec.product_name_md, + "vendor_id": rec.vendor_id.id, }) - so_line_new.product_id_change() + so_line_new._onchange_vendor_id_custom() vals = SaleOrderLine._convert_to_write(so_line_new._cache) SaleOrderLine.create(vals) @@ -1019,6 +1019,70 @@ class SourcingJobOrderLine(models.Model): rec.now_price = pricelist.include_price or 0.0 rec.last_updated_price = pricelist.write_date or 0.0 rec.tax_id = pricelist.taxes_product_id.id or pricelist.taxes_system_id.id or False + + @api.onchange('vendor_id') + def _onchange_vendor_id_custom(self): + self._update_purchase_info() + + def _update_purchase_info(self): + if not self.product_id or self.product_id.type == 'service': + return + + if self.product_id.categ_id.id == 34: + self.price = self.product_id.standard_price + self.tax_id = False + self.now_price = 0 + self.last_updated_price = False + elif self.product_id.x_manufacture.override_vendor_id: + price, taxes, vendor_id, last_updated = self._get_purchase_price_by_vendor(self.product_id, self.vendor_id) + self.price = price + self.now_price = price + self.tax_id = taxes + self.last_updated_price = last_updated + + def _get_purchase_price_by_vendor(self, product_id, vendor_id): + purchase_price = self.env['purchase.pricelist'].search( + [('product_id', '=', product_id.id), + ('vendor_id', '=', vendor_id.id), + ], + limit=1) + + return self._get_valid_purchase_price(purchase_price) + + def _get_valid_purchase_price(self, purchase_price): + current_time = datetime.now() + delta_time = current_time - timedelta(days=365) + default_timestamp = datetime(1970, 1, 1, 0, 0, 0) + # delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S') + + price = 0 + taxes = 24 + vendor_id = False + human_last_update = purchase_price.human_last_update or datetime.min + system_last_update = purchase_price.system_last_update or datetime.min + + # if purchase_price.taxes_product_id.type_tax_use == 'purchase': + price = purchase_price.product_price + taxes = purchase_price.taxes_product_id.id or 24 + vendor_id = purchase_price.vendor_id.id + last_updated = human_last_update + if delta_time > human_last_update: + price = 0 + taxes = 24 + vendor_id = False + + if system_last_update > human_last_update: + # if purchase_price.taxes_system_id.type_tax_use == 'purchase': + price = purchase_price.system_price + taxes = purchase_price.taxes_system_id.id or 24 + vendor_id = purchase_price.vendor_id.id + last_updated = system_last_update + if delta_time > system_last_update: + price = 0 + taxes = 24 + vendor_id = False + + return price, taxes, vendor_id, last_updated @api.onchange('attachment_type') def _onchange_attachment_type(self): |
