From 643564ac2eb00eafc09df85a508f5b1bc9b6fef3 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Thu, 22 Aug 2024 12:00:27 +0700 Subject: cr automatic purchase --- indoteknik_custom/models/automatic_purchase.py | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/indoteknik_custom/models/automatic_purchase.py b/indoteknik_custom/models/automatic_purchase.py index f5b1baf9..3561fc0f 100644 --- a/indoteknik_custom/models/automatic_purchase.py +++ b/indoteknik_custom/models/automatic_purchase.py @@ -545,6 +545,37 @@ class AutomaticPurchaseLine(models.Model): def _calculate_subtotal(self): for line in self: line.subtotal = line.qty_purchase * line.last_price + + @api.onchange('product_id') + def _onchange_product_id(self): + for line in self: + purchase_price = self.env['purchase.pricelist'].search([ + ('product_id', '=', line.product_id.id), + ('vendor_id', '=', line.partner_id.id) + ], order='count_trx_po desc, count_trx_po_vendor desc', limit=1) + vendor_id = purchase_price.vendor_id.id + price, taxes = self._get_valid_purchase_price(purchase_price) + line.last_price = price + line.taxes_id = taxes + line.brand_id = line.product_id.product_tmpl_id.x_manufacture.id + line.partner_id = vendor_id + + def _get_valid_purchase_price(self, purchase_price): + price = 0 + taxes = '' + 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 + + 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 + + return price, taxes class AutomaticPurchaseMatch(models.Model): -- cgit v1.2.3