trytond-sale_margin /

#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 trytond.model import fields
from trytond.pyson import Eval
from trytond.transaction import Transaction
from trytond.pool import Pool, PoolMeta

__all__ = ['Sale', 'SaleLine']
__metaclass__ = PoolMeta

class Sale:
    __name__ = ''

    margin = fields.Function(fields.Numeric('Margin',
            digits=(16, Eval('currency_digits', 2),),
            help='It gives profitability by calculating the difference '
             'between the Unit Price and Cost Price.'), 
    margin_cache = fields.Numeric('Margin Cache',
        digits=(16, Eval('currency_digits', 2)),

    def get_margin(self, name):
        Return the margin of each sales
        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'),
        return Currency.round(self.currency, margin)

    def store_cache(cls, sales):
        for sale in sales:
            cls.write([sale], {
                    'untaxed_amount_cache': sale.untaxed_amount,
                    'tax_amount_cache': sale.tax_amount,
                    'total_amount_cache': sale.total_amount,
                    'margin_cache': sale.margin,

class SaleLine:
    'Sale Line'
    __name__ = 'sale.line'

    cost_price = fields.Numeric('Cost Price', digits=(16, 4),
            'invisible': Eval('type') != 'line',
            'required': Eval('type') == 'line',
            }, depends=['type'])
    margin = fields.Function(fields.Numeric('Margin',
            digits=(16, Eval('_parent_sale', {}).get('currency_digits', 2)),
                'invisible': ~Eval('type').in_(['line', 'subtotal']),
                'readonly': ~Eval('_parent_sale'),
                }, on_change_with=['type', 'quantity', 'cost_price', 
                'unit_price', 'unit', '_parent_sale.currency'],
            depends=['type']), 'get_margin')

    def on_change_product(self):
        if not self.product:
            return {}
        res = super(SaleLine, self).on_change_product()
        res['cost_price'] = self.product.cost_price
        return res

    def on_change_with_margin(self):
        cost = Decimal(str(self.quantity or '0.0')) * \
                    (self.cost_price or Decimal('0.0'))
        amount = Decimal(str(self.quantity or '0.0')) * \
                    (self.unit_price or Decimal('0.0'))
        res = Decimal(amount-cost)
        return res

    def get_margin(self, name):
        Return the margin of each sale lines
        Currency = Pool().get('currency.currency')
        res = {}
        if self.type == 'line':
            cost = Decimal(str(self.quantity)) * (self.cost_price or Decimal('0.0')) 
            amount = Decimal(str(self.quantity)) * (self.unit_price)
            return Currency.round(, amount - cost)
            return Decimal('0.0')