2016-04-13 17:17:34 +02:00
|
|
|
# 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.
|
2012-07-24 11:56:39 +02:00
|
|
|
from decimal import Decimal
|
2015-05-21 11:07:02 +02:00
|
|
|
from math import fabs
|
2012-10-16 17:10:24 +02:00
|
|
|
from trytond.model import fields
|
2012-07-24 11:56:39 +02:00
|
|
|
from trytond.pyson import Eval
|
2012-10-16 17:10:24 +02:00
|
|
|
from trytond.pool import Pool, PoolMeta
|
2016-04-13 17:17:34 +02:00
|
|
|
from trytond.modules.product import price_digits
|
2021-08-23 18:04:15 +02:00
|
|
|
from trytond.modules.currency.fields import Monetary
|
2012-07-24 11:56:39 +02:00
|
|
|
|
2012-10-16 17:10:24 +02:00
|
|
|
__all__ = ['Sale', 'SaleLine']
|
|
|
|
|
2019-08-06 14:01:11 +02:00
|
|
|
_ZERO = Decimal(0)
|
2014-01-17 13:45:34 +01:00
|
|
|
|
2021-08-23 18:04:15 +02:00
|
|
|
|
2018-08-18 09:57:24 +02:00
|
|
|
class Sale(metaclass=PoolMeta):
|
2012-10-16 17:10:24 +02:00
|
|
|
__name__ = 'sale.sale'
|
2021-08-23 18:04:15 +02:00
|
|
|
margin = fields.Function(Monetary('Margin',
|
|
|
|
currency='currency', digits='currency',
|
|
|
|
help=('It gives profitability by calculating the difference '
|
|
|
|
'between the Unit Price and Cost Price.')),
|
2014-11-12 12:35:46 +01:00
|
|
|
'get_margin')
|
2021-08-23 18:04:15 +02:00
|
|
|
margin_cache = Monetary('Margin Cache',
|
|
|
|
currency='currency', digits='currency', readonly=True)
|
2014-11-12 12:35:46 +01:00
|
|
|
margin_percent = fields.Function(fields.Numeric('Margin (%)',
|
2021-08-23 18:04:15 +02:00
|
|
|
digits=(16, 4)), 'get_margin_percent')
|
2014-11-12 12:35:46 +01:00
|
|
|
margin_percent_cache = fields.Numeric('Margin (%) Cache',
|
|
|
|
digits=(16, 4), readonly=True)
|
2012-07-24 11:56:39 +02:00
|
|
|
|
2017-03-23 23:21:22 +01:00
|
|
|
@classmethod
|
|
|
|
def copy(cls, sales, default=None):
|
|
|
|
if default is None:
|
|
|
|
default = {}
|
|
|
|
default = default.copy()
|
|
|
|
default['margin_cache'] = None
|
|
|
|
default['margin_percent_cache'] = None
|
|
|
|
return super(Sale, cls).copy(sales, default=default)
|
|
|
|
|
2012-10-16 17:10:24 +02:00
|
|
|
def get_margin(self, name):
|
2012-07-24 11:56:39 +02:00
|
|
|
'''
|
|
|
|
Return the margin of each sales
|
|
|
|
'''
|
2012-10-16 17:10:24 +02:00
|
|
|
Currency = Pool().get('currency.currency')
|
|
|
|
if (self.state in self._states_cached
|
|
|
|
and self.margin_cache is not None):
|
|
|
|
return self.margin_cache
|
|
|
|
margin = sum((l.margin for l in self.lines if l.type == 'line'),
|
2019-08-06 14:01:11 +02:00
|
|
|
_ZERO)
|
2012-10-16 17:10:24 +02:00
|
|
|
return Currency.round(self.currency, margin)
|
2012-07-24 11:56:39 +02:00
|
|
|
|
2014-11-12 12:35:46 +01:00
|
|
|
def get_margin_percent(self, name):
|
2017-03-23 23:21:22 +01:00
|
|
|
Configuration = Pool().get('sale.configuration')
|
|
|
|
|
|
|
|
config = Configuration(1)
|
2017-03-24 07:56:24 +01:00
|
|
|
sale_margin_method = config.sale_margin_method or 'cost_price'
|
2017-03-23 23:21:22 +01:00
|
|
|
|
2014-11-12 12:35:46 +01:00
|
|
|
if (self.state in self._states_cached
|
|
|
|
and self.margin_percent_cache is not None):
|
|
|
|
return self.margin_percent_cache
|
|
|
|
|
2017-03-23 23:21:22 +01:00
|
|
|
price = sum(
|
|
|
|
Decimal(str(fabs(l.quantity))) * (
|
2019-08-06 14:01:11 +02:00
|
|
|
getattr(l, sale_margin_method) or _ZERO)
|
2014-11-12 12:35:46 +01:00
|
|
|
for l in self.lines if l.type == 'line')
|
2017-03-23 23:21:22 +01:00
|
|
|
if price:
|
|
|
|
return (self.margin / price).quantize(Decimal('0.0001'))
|
2015-04-16 10:50:12 +02:00
|
|
|
else:
|
|
|
|
return Decimal('1.0')
|
2014-11-12 12:35:46 +01:00
|
|
|
|
2012-10-16 17:10:24 +02:00
|
|
|
@classmethod
|
|
|
|
def store_cache(cls, sales):
|
2014-11-12 12:35:46 +01:00
|
|
|
super(Sale, cls).store_cache(sales)
|
2012-10-16 17:10:24 +02:00
|
|
|
for sale in sales:
|
|
|
|
cls.write([sale], {
|
2012-07-24 11:56:39 +02:00
|
|
|
'margin_cache': sale.margin,
|
2014-11-12 12:35:46 +01:00
|
|
|
'margin_percent_cache': sale.margin_percent,
|
2012-07-24 11:56:39 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-18 09:57:24 +02:00
|
|
|
class SaleLine(metaclass=PoolMeta):
|
2012-10-16 17:10:24 +02:00
|
|
|
__name__ = 'sale.line'
|
2016-04-13 17:17:34 +02:00
|
|
|
cost_price = fields.Numeric('Cost Price', digits=price_digits,
|
2012-07-24 11:56:39 +02:00
|
|
|
states={
|
|
|
|
'invisible': Eval('type') != 'line',
|
2016-07-01 11:39:38 +02:00
|
|
|
'readonly': ~Eval('sale_state').in_(['draft', 'quotation']),
|
2016-07-01 14:00:15 +02:00
|
|
|
},
|
|
|
|
depends=['type', 'sale_state'])
|
2021-08-23 18:04:15 +02:00
|
|
|
margin = fields.Function(Monetary('Margin',
|
|
|
|
currency='currency', digits='currency',
|
|
|
|
states={
|
|
|
|
'invisible': ~Eval('type').in_(['line', 'subtotal']),
|
|
|
|
'readonly': ~Eval('_parent_sale'),
|
|
|
|
},
|
|
|
|
depends=['type', 'amount']), 'on_change_with_margin')
|
2014-11-12 12:35:46 +01:00
|
|
|
margin_percent = fields.Function(fields.Numeric('Margin (%)',
|
2021-08-23 18:04:15 +02:00
|
|
|
digits=(16, 4), states={
|
|
|
|
'invisible': ~Eval('type').in_(['line', 'subtotal']),
|
|
|
|
}, depends=['type']), 'on_change_with_margin_percent')
|
2014-10-29 13:42:30 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __setup__(cls):
|
|
|
|
super(SaleLine, cls).__setup__()
|
|
|
|
if hasattr(cls, 'gross_unit_price'):
|
2021-10-01 13:59:59 +02:00
|
|
|
cls.margin.on_change_with.add('gross_unit_price')
|
2021-10-01 15:01:48 +02:00
|
|
|
cls.margin_percent.on_change_with.add('gross_unit_price')
|
2012-07-24 11:56:39 +02:00
|
|
|
|
2013-10-04 10:57:14 +02:00
|
|
|
@staticmethod
|
|
|
|
def default_cost_price():
|
2019-08-06 14:01:11 +02:00
|
|
|
return _ZERO
|
2013-10-04 10:57:14 +02:00
|
|
|
|
2015-09-04 00:37:17 +02:00
|
|
|
@fields.depends('product')
|
2012-10-16 17:10:24 +02:00
|
|
|
def on_change_product(self):
|
2015-09-04 00:37:17 +02:00
|
|
|
super(SaleLine, self).on_change_product()
|
2014-07-02 09:09:49 +02:00
|
|
|
if self.product:
|
2015-09-04 00:37:17 +02:00
|
|
|
cost_price = self.product.cost_price
|
|
|
|
self.cost_price = cost_price.quantize(
|
2015-07-29 17:13:33 +02:00
|
|
|
Decimal(1) / 10 ** self.__class__.cost_price.digits[1])
|
2012-07-24 11:56:39 +02:00
|
|
|
|
2014-10-29 13:42:30 +01:00
|
|
|
@fields.depends('type', 'quantity', 'cost_price', '_parent_sale.currency',
|
2018-08-18 09:56:56 +02:00
|
|
|
'_parent_sale.lines', methods=['on_change_with_amount'])
|
2014-10-29 13:42:30 +01:00
|
|
|
def on_change_with_margin(self, name=None):
|
2012-07-24 11:56:39 +02:00
|
|
|
'''
|
|
|
|
Return the margin of each sale lines
|
|
|
|
'''
|
2012-10-16 17:10:24 +02:00
|
|
|
Currency = Pool().get('currency.currency')
|
2019-08-06 14:01:11 +02:00
|
|
|
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
|
2014-11-12 12:35:46 +01:00
|
|
|
|
2017-03-23 23:21:22 +01:00
|
|
|
@fields.depends('type', 'quantity', 'cost_price', 'unit_price',
|
2018-08-18 09:56:56 +02:00
|
|
|
'_parent_sale.currency', '_parent_sale.lines',
|
|
|
|
methods=['on_change_with_margin'])
|
2014-11-12 12:35:46 +01:00
|
|
|
def on_change_with_margin_percent(self, name=None):
|
2017-03-23 23:21:22 +01:00
|
|
|
Configuration = Pool().get('sale.configuration')
|
|
|
|
|
|
|
|
config = Configuration(1)
|
2017-03-24 07:56:24 +01:00
|
|
|
sale_margin_method = config.sale_margin_method or 'cost_price'
|
2017-03-23 23:21:22 +01:00
|
|
|
|
2014-11-12 12:35:46 +01:00
|
|
|
if self.type not in ('line', 'subtotal'):
|
|
|
|
return
|
|
|
|
self.margin = self.on_change_with_margin()
|
|
|
|
if not self.margin:
|
|
|
|
return
|
2019-08-06 14:01:11 +02:00
|
|
|
price = _ZERO
|
2014-11-12 12:35:46 +01:00
|
|
|
if self.type == 'line':
|
2015-04-16 10:50:12 +02:00
|
|
|
if not self.quantity:
|
2014-11-12 12:35:46 +01:00
|
|
|
return
|
2017-03-23 23:21:22 +01:00
|
|
|
if sale_margin_method == 'cost_price' and not self.cost_price:
|
2015-04-16 10:50:12 +02:00
|
|
|
return Decimal('1.0')
|
2017-03-23 23:21:22 +01:00
|
|
|
if sale_margin_method == 'unit_price' and not self.unit_price:
|
|
|
|
return Decimal('1.0')
|
|
|
|
if sale_margin_method == 'unit_price':
|
2017-03-24 07:56:24 +01:00
|
|
|
price = self.get_margin_unit_price()
|
2017-03-23 23:21:22 +01:00
|
|
|
else:
|
2017-03-24 07:56:24 +01:00
|
|
|
price = self.get_margin_cost_price()
|
2017-03-23 23:21:22 +01:00
|
|
|
return (self.margin / price).quantize(Decimal('0.0001'))
|
2014-11-12 12:35:46 +01:00
|
|
|
else:
|
|
|
|
for line2 in self.sale.lines:
|
|
|
|
if self == line2:
|
2017-03-23 23:21:22 +01:00
|
|
|
if not price:
|
2015-04-16 10:50:12 +02:00
|
|
|
return Decimal('1.0')
|
|
|
|
else:
|
2018-02-01 17:23:58 +01:00
|
|
|
return (
|
|
|
|
self.margin / price).quantize(Decimal('0.0001'))
|
2014-11-12 12:35:46 +01:00
|
|
|
if line2.type == 'line':
|
2017-03-23 23:21:22 +01:00
|
|
|
if sale_margin_method == 'unit_price':
|
2017-03-24 07:56:24 +01:00
|
|
|
price += line2.get_margin_unit_price()
|
2017-03-23 23:21:22 +01:00
|
|
|
else:
|
2017-03-24 07:56:24 +01:00
|
|
|
price += line2.get_margin_cost_price()
|
2014-11-12 12:35:46 +01:00
|
|
|
elif line2.type == 'subtotal':
|
2019-08-06 14:01:11 +02:00
|
|
|
price = _ZERO
|
|
|
|
return price
|
2015-05-21 11:07:02 +02:00
|
|
|
|
2017-03-24 07:56:24 +01:00
|
|
|
def get_margin_cost_price(self):
|
2015-05-21 11:07:02 +02:00
|
|
|
return Decimal(str(fabs(self.quantity))) * (self.cost_price or
|
2019-08-06 14:01:11 +02:00
|
|
|
_ZERO)
|
2017-03-23 23:21:22 +01:00
|
|
|
|
2017-03-24 07:56:24 +01:00
|
|
|
def get_margin_unit_price(self):
|
2017-03-23 23:21:22 +01:00
|
|
|
return Decimal(str(fabs(self.quantity))) * (self.unit_price or
|
2019-08-06 14:01:11 +02:00
|
|
|
_ZERO)
|