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 30fee3cb78
commit 46002a114c
1 changed files with 35 additions and 37 deletions

72
sale.py
View File

@ -1,6 +1,6 @@
#This file is part sale_margin module for Tryton.
#The COPYRIGHT file at the top level of this repository contains
#the full copyright notices and license terms.
# This file is part sale_margin module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from decimal import Decimal
from math import fabs
from trytond.model import fields
@ -12,6 +12,7 @@ __all__ = ['Sale', 'SaleLine']
__metaclass__ = PoolMeta
DIGITS = config_.getint('product', 'price_decimal', default=4)
_ZERO = Decimal(0)
class Sale:
@ -19,8 +20,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)),
@ -41,7 +42,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):
@ -50,7 +51,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'))
@ -95,7 +96,7 @@ class SaleLine:
@staticmethod
def default_cost_price():
return Decimal(0)
return _ZERO
@fields.depends('product')
def on_change_product(self):
@ -112,29 +113,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'])
@ -144,15 +141,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:
@ -162,8 +159,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)