Correct on_change_with methods in which nothing was being returned

#038505
This commit is contained in:
Carlos G?lvez 2019-08-06 14:01:11 +02:00
parent 51f016b5cb
commit f506f65294
1 changed files with 32 additions and 34 deletions

66
sale.py
View File

@ -10,6 +10,7 @@ from trytond.modules.product import price_digits
__all__ = ['Sale', 'SaleLine']
_ZERO = Decimal(0)
class Sale:
__metaclass__ = PoolMeta
@ -17,8 +18,8 @@ class Sale:
margin = fields.Function(fields.Numeric('Margin',
digits=(16, Eval('currency_digits', 2),),
depends=['currency_digits'],
help='It gives profitability by calculating the difference '
'between the Unit Price and Cost Price.'),
help=('It gives profitability by calculating the difference '
'between the Unit Price and Cost Price.')),
'get_margin')
margin_cache = fields.Numeric('Margin Cache',
digits=(16, Eval('currency_digits', 2)),
@ -39,7 +40,7 @@ class Sale:
and self.margin_cache is not None):
return self.margin_cache
margin = sum((l.margin for l in self.lines if l.type == 'line'),
Decimal(0))
_ZERO)
return Currency.round(self.currency, margin)
def get_margin_percent(self, name):
@ -48,7 +49,7 @@ class Sale:
return self.margin_percent_cache
cost = sum(
Decimal(str(fabs(l.quantity))) * (l.cost_price or Decimal(0))
Decimal(str(fabs(l.quantity))) * (l.cost_price or _ZERO)
for l in self.lines if l.type == 'line')
if cost:
return (self.margin / cost).quantize(Decimal('0.0001'))
@ -94,7 +95,7 @@ class SaleLine:
@staticmethod
def default_cost_price():
return Decimal(0)
return _ZERO
@fields.depends('product')
def on_change_product(self):
@ -111,29 +112,25 @@ class SaleLine:
Return the margin of each sale lines
'''
Currency = Pool().get('currency.currency')
if not self.sale or not self.sale.currency:
return Decimal(0)
currency = self.sale.currency
if self.type == 'line':
if self.quantity and self.cost_price:
cost = Decimal(str(self.quantity)) * (self.cost_price)
else:
cost = Decimal(0)
self.amount = self.on_change_with_amount()
return Currency.round(currency, self.amount - cost)
elif self.type == 'subtotal':
cost = Decimal(0)
for line2 in self.sale.lines:
if self == line2:
return cost
if line2.type == 'line':
cost2 = Decimal(str(line2.quantity)) * (line2.cost_price or
Decimal(0))
cost += Currency.round(currency, line2.amount - cost2)
elif line2.type == 'subtotal':
cost = Decimal(0)
else:
return Decimal(0)
cost = _ZERO
if self.sale and self.sale.currency:
currency = self.sale.currency
if self.type == 'line':
if self.quantity and self.cost_price:
cost = Decimal(str(self.quantity)) * (self.cost_price)
self.amount = self.on_change_with_amount()
return Currency.round(currency, self.amount - cost)
elif self.type == 'subtotal':
for line2 in self.sale.lines:
if self == line2:
return cost
if line2.type == 'line':
cost_price = line2.cost_price or _ZERO
cost2 = Decimal(str(line2.quantity)) * cost_price
cost += Currency.round(currency, line2.amount - cost2)
elif line2.type == 'subtotal':
cost = _ZERO
return cost
@fields.depends('type', 'quantity', 'cost_price', '_parent_sale.currency',
'_parent_sale.lines', methods=['margin'])
@ -143,15 +140,15 @@ class SaleLine:
self.margin = self.on_change_with_margin()
if not self.margin:
return
cost = _ZERO
if self.type == 'line':
if not self.quantity:
return
if not self.cost_price:
return Decimal('1.0')
cost = self.get_cost_price()
return (self.margin / cost).quantize(Decimal('0.0001'))
cost_price = self.get_cost_price()
cost = (self.margin / cost_price).quantize(Decimal('0.0001'))
else:
cost = Decimal(0)
for line2 in self.sale.lines:
if self == line2:
if not cost:
@ -161,8 +158,9 @@ class SaleLine:
if line2.type == 'line':
cost += line2.get_cost_price()
elif line2.type == 'subtotal':
cost = Decimal(0)
cost = _ZERO
return cost
def get_cost_price(self):
return Decimal(str(fabs(self.quantity))) * (self.cost_price or
Decimal(0))
return Decimal(str(fabs(self.quantity))) * (self.cost_price or _ZERO)