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 05d6fc57c2
commit 662bd7da5c
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'] __all__ = ['Sale', 'SaleLine']
_ZERO = Decimal(0)
class Sale: class Sale:
__metaclass__ = PoolMeta __metaclass__ = PoolMeta
@ -48,7 +49,7 @@ class Sale:
and self.margin_cache is not None): and self.margin_cache is not None):
return self.margin_cache return self.margin_cache
margin = sum((l.margin for l in self.lines if l.type == 'line'), margin = sum((l.margin for l in self.lines if l.type == 'line'),
Decimal(0)) _ZERO)
return Currency.round(self.currency, margin) return Currency.round(self.currency, margin)
def get_margin_percent(self, name): def get_margin_percent(self, name):
@ -63,7 +64,7 @@ class Sale:
price = sum( price = sum(
Decimal(str(fabs(l.quantity))) * ( 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') for l in self.lines if l.type == 'line')
if price: if price:
return (self.margin / price).quantize(Decimal('0.0001')) return (self.margin / price).quantize(Decimal('0.0001'))
@ -111,7 +112,7 @@ class SaleLine:
@staticmethod @staticmethod
def default_cost_price(): def default_cost_price():
return Decimal(0) return _ZERO
@fields.depends('product') @fields.depends('product')
def on_change_product(self): def on_change_product(self):
@ -128,29 +129,25 @@ class SaleLine:
Return the margin of each sale lines Return the margin of each sale lines
''' '''
Currency = Pool().get('currency.currency') Currency = Pool().get('currency.currency')
if not self.sale or not self.sale.currency: cost = _ZERO
return Decimal(0) if self.sale and self.sale.currency:
currency = self.sale.currency currency = self.sale.currency
if self.type == 'line': if self.type == 'line':
if self.quantity and self.cost_price: if self.quantity and self.cost_price:
cost = Decimal(str(self.quantity)) * (self.cost_price) cost = Decimal(str(self.quantity)) * (self.cost_price)
else: self.amount = self.on_change_with_amount()
cost = Decimal(0) return Currency.round(currency, self.amount - cost)
self.amount = self.on_change_with_amount() elif self.type == 'subtotal':
return Currency.round(currency, self.amount - cost) for line2 in self.sale.lines:
elif self.type == 'subtotal': if self == line2:
cost = Decimal(0) return cost
for line2 in self.sale.lines: if line2.type == 'line':
if self == line2: cost_price = line2.cost_price or _ZERO
return cost cost2 = Decimal(str(line2.quantity)) * cost_price
if line2.type == 'line': cost += Currency.round(currency, line2.amount - cost2)
cost2 = Decimal(str(line2.quantity)) * (line2.cost_price or elif line2.type == 'subtotal':
Decimal(0)) cost = _ZERO
cost += Currency.round(currency, line2.amount - cost2) return cost
elif line2.type == 'subtotal':
cost = Decimal(0)
else:
return Decimal(0)
@fields.depends('type', 'quantity', 'cost_price', 'unit_price', @fields.depends('type', 'quantity', 'cost_price', 'unit_price',
'_parent_sale.currency', '_parent_sale.lines', methods=['margin']) '_parent_sale.currency', '_parent_sale.lines', methods=['margin'])
@ -165,6 +162,7 @@ class SaleLine:
self.margin = self.on_change_with_margin() self.margin = self.on_change_with_margin()
if not self.margin: if not self.margin:
return return
price = _ZERO
if self.type == 'line': if self.type == 'line':
if not self.quantity: if not self.quantity:
return return
@ -178,7 +176,6 @@ class SaleLine:
price = self.get_margin_cost_price() price = self.get_margin_cost_price()
return (self.margin / price).quantize(Decimal('0.0001')) return (self.margin / price).quantize(Decimal('0.0001'))
else: else:
price = Decimal(0)
for line2 in self.sale.lines: for line2 in self.sale.lines:
if self == line2: if self == line2:
if not price: if not price:
@ -192,12 +189,13 @@ class SaleLine:
else: else:
price += line2.get_margin_cost_price() price += line2.get_margin_cost_price()
elif line2.type == 'subtotal': elif line2.type == 'subtotal':
price = Decimal(0) price = _ZERO
return price
def get_margin_cost_price(self): def get_margin_cost_price(self):
return Decimal(str(fabs(self.quantity))) * (self.cost_price or return Decimal(str(fabs(self.quantity))) * (self.cost_price or
Decimal(0)) _ZERO)
def get_margin_unit_price(self): def get_margin_unit_price(self):
return Decimal(str(fabs(self.quantity))) * (self.unit_price or return Decimal(str(fabs(self.quantity))) * (self.unit_price or
Decimal(0)) _ZERO)