from odoo import fields, models, api, _ from odoo.exceptions import AccessError, UserError, ValidationError from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT class PurchaseOrderLine(models.Model): _inherit = 'purchase.order.line' # Override method from addons/purchase/models/purchase.py @api.onchange('product_qty', 'product_uom') def _onchange_quantity(self): res = super(PurchaseOrderLine, self)._onchange_quantity() # Custom script purchase_pricelist = self.env['purchase.pricelist'].search([ ('product_id', '=', self.product_id.id), ('vendor_id', '=', self.partner_id.id) ], limit=1) price_unit = purchase_pricelist.product_price if not price_unit: product_supplierinfo = self.env['product.supplierinfo'].search([ ('product_tmpl_id', '=', self.product_id.product_tmpl_id.id), ('name', '=', self.partner_id.id) ], limit=1) price_unit = product_supplierinfo.price self.price_unit = price_unit return res # def compute_item_margin(self): # for line in self: # if not line.product_id or line.price_unit <= 0 or line.product_uom_qty <= 0 or line.product_id.type == 'service' or not line.order_id.sale_order_id.id: # line.item_margin = 0 # line.item_percent_margin = 0 # continue # sale_order_line = self.env['sale.order.line'].search( # [('product_id', '=', line.product_id.id), # ('order_id', '=', line.order_id.sale_order_id.id)], limit=1, order='price_reduce_taxexcl') # # est_purchase_price = sale_order_line.purchase_price # real_purchase_price = line.price_unit # real_tax = 0 # real_tax_amount = 0 # count_real_tax = 0 # for tax in line.taxes_id: # count_real_tax += 1 # real_tax = tax # real_tax_amount += tax.amount # if sale_order_line.purchase_tax_id.amount != real_tax_amount or count_real_tax > 1: # raise UserError("Beda tax amount dengan Sales") # # # untaxed sales price # sales_price = sale_order_line.price_reduce_taxexcl * sale_order_line.product_uom_qty # # must add expedition price in sales_price, expedition payed by customer? # # sales_price -= round((line.order_id.sale_order_id.delivery_amount / line.order_id.sale_order_id.count_line_product), 2) # # untaxed purchase price # purchase_price = line.price_subtotal # # must add expedition price in purchase_price as expenses # purchase_price += round((line.order_id.delivery_amount / line.order_id.count_line_product), 2) # margin_per_item = sales_price - purchase_price # line.item_margin = margin_per_item # if sales_price > 0: # line.item_percent_margin = round((margin_per_item / sales_price), 2) * 100