Skip to Content
Menu
This question has been flagged
1824 Views

i create a custom filed (x_discount) in Purchase Order Line  and i want to apply discount on price_unit field which is already available in Purchase Order Line . Please guide me what code i use for this operation? 

now i am using this code for apply discount in Purchase order line but this code apply Discount as % on Purchase Order Line but i want to apply Discount as amount. What  should i change in this code to achieve my goal please guide

class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"

@api.depends('discount')
def _compute_amount(self):
for line in self:
price_unit = False
# This is always executed for allowing other modules to use this
# with different conditions than discount != 0
price = line._get_discounted_price_unit()
if price != line.price_unit:
# Only change value if it's different
price_unit = line.price_unit
line.price_unit = price
super(PurchaseOrderLine, line)._compute_amount()
if price_unit:
line.price_unit = price_unit

discount = fields.Float(
string='Discount (%)', digits=dp.get_precision('Discount'),
)

_sql_constraints = [
('discount_limit', 'CHECK (discount <= 100.0)',
'Discount must be lower than 100%.'),
]

def _get_discounted_price_unit(self):
"""Inheritable method for getting the unit price after applying
discount(s).

:rtype: float
:return: Unit price after discount(s).
"""
self.ensure_one()
if self.discount:
return self.price_unit * (1 - self.discount / 100)
return self.price_unit

@api.multi
def _get_stock_move_price_unit(self):
"""Get correct price with discount replacing current price_unit
value before calling super and restoring it later for assuring
maximum inheritability.
"""
price_unit = False
price = self._get_discounted_price_unit()
if price != self.price_unit:
# Only change value if it's different
price_unit = self.price_unit
self.price_unit = price
price = super(PurchaseOrderLine, self)._get_stock_move_price_unit()
if price_unit:
self.price_unit = price_unit
return price 


Avatar
Discard