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 8b1199772b
commit f7afadf644
1 changed files with 28 additions and 30 deletions

58
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):
__name__ = 'sale.sale'
@ -47,7 +48,7 @@ class Sale(metaclass=PoolMeta):
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):
@ -62,7 +63,7 @@ class Sale(metaclass=PoolMeta):
price = sum(
Decimal(str(fabs(l.quantity))) * (
getattr(l, sale_margin_method) or Decimal(0))
getattr(l, sale_margin_method) or _ZERO)
for l in self.lines if l.type == 'line')
if price:
return (self.margin / price).quantize(Decimal('0.0001'))
@ -109,7 +110,7 @@ class SaleLine(metaclass=PoolMeta):
@staticmethod
def default_cost_price():
return Decimal(0)
return _ZERO
@fields.depends('product')
def on_change_product(self):
@ -126,29 +127,25 @@ class SaleLine(metaclass=PoolMeta):
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', 'unit_price',
'_parent_sale.currency', '_parent_sale.lines',
@ -164,6 +161,7 @@ class SaleLine(metaclass=PoolMeta):
self.margin = self.on_change_with_margin()
if not self.margin:
return
price = _ZERO
if self.type == 'line':
if not self.quantity:
return
@ -177,7 +175,6 @@ class SaleLine(metaclass=PoolMeta):
price = self.get_margin_cost_price()
return (self.margin / price).quantize(Decimal('0.0001'))
else:
price = Decimal(0)
for line2 in self.sale.lines:
if self == line2:
if not price:
@ -191,12 +188,13 @@ class SaleLine(metaclass=PoolMeta):
else:
price += line2.get_margin_cost_price()
elif line2.type == 'subtotal':
price = Decimal(0)
price = _ZERO
return price
def get_margin_cost_price(self):
return Decimal(str(fabs(self.quantity))) * (self.cost_price or
Decimal(0))
_ZERO)
def get_margin_unit_price(self):
return Decimal(str(fabs(self.quantity))) * (self.unit_price or
Decimal(0))
_ZERO)