trytond-sale_margin/sale.py

110 lines
3.9 KiB
Python
Raw Normal View History

2012-07-24 11:56:39 +02:00
#This file is part sale_margin module for Tryton.
2014-08-30 15:14:54 +02:00
#The COPYRIGHT file at the top level of this repository contains
2012-07-24 11:56:39 +02:00
#the full copyright notices and license terms.
from decimal import Decimal
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
2014-11-05 17:17:48 +01:00
from trytond.config import config
DIGITS = int(config.get('digits', 'unit_price_digits', 4))
2012-07-24 11:56:39 +02:00
2012-10-16 17:10:24 +02:00
__all__ = ['Sale', 'SaleLine']
__metaclass__ = PoolMeta
2014-01-17 13:45:34 +01:00
2012-10-16 17:10:24 +02:00
class Sale:
__name__ = 'sale.sale'
2012-07-24 11:56:39 +02:00
margin = fields.Function(fields.Numeric('Margin',
digits=(16, Eval('currency_digits', 2),),
2014-08-30 15:14:54 +02:00
depends=['currency_digits'],
2012-07-24 11:56:39 +02:00
help='It gives profitability by calculating the difference '
2014-08-30 15:14:54 +02:00
'between the Unit Price and Cost Price.'),
2012-07-24 11:56:39 +02:00
'get_margin')
margin_cache = fields.Numeric('Margin Cache',
digits=(16, Eval('currency_digits', 2)),
readonly=True,
depends=['currency_digits'])
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'),
Decimal(0))
return Currency.round(self.currency, margin)
2012-07-24 11:56:39 +02:00
2012-10-16 17:10:24 +02:00
@classmethod
def store_cache(cls, sales):
for sale in sales:
cls.write([sale], {
2012-07-24 11:56:39 +02:00
'untaxed_amount_cache': sale.untaxed_amount,
'tax_amount_cache': sale.tax_amount,
'total_amount_cache': sale.total_amount,
'margin_cache': sale.margin,
})
2012-10-16 17:10:24 +02:00
class SaleLine:
__name__ = 'sale.line'
cost_price = fields.Numeric('Cost Price', digits=(16, DIGITS),
2012-07-24 11:56:39 +02:00
states={
'invisible': Eval('type') != 'line',
}, depends=['type'])
margin = fields.Function(fields.Numeric('Margin',
digits=(16, Eval('_parent_sale', {}).get('currency_digits', 2)),
states={
'invisible': ~Eval('type').in_(['line', 'subtotal']),
'readonly': ~Eval('_parent_sale'),
},
depends=['type', 'amount']),
'on_change_with_margin')
@classmethod
def __setup__(cls):
super(SaleLine, cls).__setup__()
if hasattr(cls, 'gross_unit_price'):
cls.on_change_with_margin.depends.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():
return Decimal('0.0')
2012-10-16 17:10:24 +02:00
def on_change_product(self):
res = super(SaleLine, self).on_change_product()
if self.product:
res['cost_price'] = self.product.cost_price
2012-07-24 11:56:39 +02:00
return res
@fields.depends('type', 'quantity', 'cost_price', '_parent_sale.currency',
'_parent_sale.lines', methods=['amount'])
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')
2014-11-05 17:17:48 +01:00
if not self.sale or not self.sale.currency:
return Decimal('0.0')
2014-08-30 15:16:39 +02:00
currency = self.sale.currency
2012-10-16 17:10:24 +02:00
if self.type == 'line':
2014-08-30 15:14:54 +02:00
cost = Decimal(str(self.quantity)) * (self.cost_price or
Decimal('0.0'))
self.amount = self.on_change_with_amount()
2014-08-30 15:16:39 +02:00
return Currency.round(currency, self.amount - cost)
elif self.type == 'subtotal':
cost = Decimal('0.0')
for line2 in self.sale.lines:
if line2.type == 'line':
cost2 = Decimal(str(line2.quantity)) * (line2.cost_price or
Decimal('0.0'))
cost += Currency.round(currency, line2.amount - cost2)
elif line2.type == 'subtotal':
if self == line2:
return cost
cost = Decimal('0.0')
2012-10-16 17:10:24 +02:00
else:
return Decimal('0.0')