In purchase.order.line i have added some fields and their respective inverse method to change the corresponding fields
class PurchaseOrderInherit(models.Model):
_inherit = "purchase.order.line"
mrp = fields.Float("MRP")
margin = fields.Float("Mark Down%", compute="_compute_margin", inverse="_inverse_margin", digits='Product Price', store=True,
help="(MRP Excl. - Cost Excl.) / MRP Excl.")
margin_up = fields.Float("Mark Up %", compute="_compute_margin", inverse="_inverse_margin_up", digits='Product Price', store=True,
help="(MRP Excl. - Cost Excl.) / Cost Excl.")
margin_status = fields.Char("Change in Margin%", compute="_compute_margin_status", store=True)
margin_prev = fields.Float("Margin Prev %", store=True)
disc_amount = fields.Float("Disc. Amount", compute="_compute_disc_amount", inverse="_inverse_disc_amount",
help="Discount Amount for Total Quantity", store="True", digits='Product Price')
@api.depends("mrp", "product_qty", "price_subtotal", "product_id")
def _compute_margin(self):
for rec in self:
if rec.mrp and rec.price_subtotal:
base_amount = self._get_tax_amount(rec.product_id, rec.mrp) * rec.product_qty
rec.margin = (base_amount - rec.price_subtotal) / base_amount * 100 if base_amount else 0
rec.margin_up = (base_amount - rec.price_subtotal) / rec.price_subtotal * 100 if rec.price_subtotal else 0
else:
rec.margin = False
rec.margin_up = False
@api.onchange('margin_up')
def _inverse_margin_up(self):
for rec in self:
if rec.product_id and rec.margin_up not in (False, 0):
if rec.price_subtotal:
base_amount = rec.price_subtotal * (1 + rec.margin_up / 100)
rec.mrp = round(rec.product_qty and self._get_mrp_from_base_amount(rec.product_id, base_amount) / rec.product_qty)
@api.onchange('margin')
def _inverse_margin(self):
for rec in self:
base_amount = self._get_tax_amount(rec.product_id, rec.mrp)
if base_amount != 0 and rec.discount != 100:
rec.price_unit = (base_amount * (1 - rec.margin / 100)) / (1 - rec.discount / 100)
def _get_mrp_from_base_amount(self, product, base_amount):
taxes = product.taxes_id.filtered(
lambda x: x.company_id.id == self.env.company.id
or x.company_id.id == self.env.company.parent_id.id
)
tax_amount = sum(taxes.mapped('amount'))
mrp = base_amount * (1 + tax_amount / 100)
return mrp
def _get_tax_amount(self, product, price):
taxes_id = product.taxes_id.filtered(
lambda x: x.company_id.id == self.env.company.id
or x.company_id.id == self.env.company.parent_id.id
)
if taxes_id:
amount = taxes_id.compute_all(
price, product=product, partner=self.env["res.partner"]
)
amount = amount["total_excluded"]
else:
amount = price
return amount
# update margin status
@api.depends("mrp", "price_unit")
def _compute_margin_status(self):
for rec in self:
margin_prev = False
if rec.product_id.id:
margin_prev = (
self.env["purchase.order.line"]
.search(
[
("product_id", "=", rec.product_id.id),
("partner_id", "=", rec.partner_id.id),
("order_id", "!=", rec.order_id._origin.id),
("company_id", '=', self.env.company.id),
],
order="create_date desc",
limit=1,
)
.margin
)
rec.margin_prev = margin_prev
if margin_prev == False:
rec.margin_status = "NA"
elif rec.margin > margin_prev:
rec.margin_status = str(round(rec.margin - margin_prev, 1)) + " ⇑"
elif rec.margin == margin_prev:
rec.margin_status = str(round(rec.margin - margin_prev, 1)) + " ⇔"
elif rec.margin rec.margin_status = str(round(rec.margin - margin_prev, 1)) + " ⇓"
@api.depends('discount')
def _compute_disc_amount(self):
for rec in self:
if rec.discount:
rec.disc_amount = rec.price_unit * rec.product_qty * (rec.discount / 100)
else:
rec.disc_amount = 0.0
@api.onchange('disc_amount')
def _inverse_disc_amount(self):
for rec in self:
if rec.price_unit and rec.product_qty:
rec.discount = (rec.disc_amount / (rec.price_unit * rec.product_qty)) * 100
I have added all the inverse function as api.onchange() but it triggers whenever the field updates it results some unwanted values is there anything i can do
if anyone knows help me