Files
tryton/carrier.py
T

52 lines
2.0 KiB
Python
Raw Normal View History

2014-12-02 12:55:09 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2012-02-07 19:38:56 +01:00
from decimal import Decimal
2012-09-24 22:17:48 +02:00
from trytond.model import fields
2012-02-07 19:38:56 +01:00
from trytond.transaction import Transaction
2012-09-24 22:17:48 +02:00
from trytond.pool import Pool, PoolMeta
2012-02-07 19:38:56 +01:00
from trytond.pyson import Eval
2012-09-24 22:17:48 +02:00
__all__ = ['Carrier']
__metaclass__ = PoolMeta
2012-02-07 19:38:56 +01:00
2012-09-24 22:17:48 +02:00
class Carrier:
__name__ = 'carrier'
2012-02-07 19:38:56 +01:00
percentage = fields.Numeric('Percentage', digits=(16, 8),
states={
'invisible': Eval('carrier_cost_method') != 'percentage',
},
depends=['carrier_cost_method'])
2012-09-24 22:17:48 +02:00
@classmethod
def __setup__(cls):
super(Carrier, cls).__setup__()
2012-02-07 19:38:56 +01:00
selection = ('percentage', 'Percentage')
2012-09-24 22:17:48 +02:00
if selection not in cls.carrier_cost_method.selection:
cls.carrier_cost_method.selection.append(selection)
2012-02-07 19:38:56 +01:00
2012-09-24 22:17:48 +02:00
def compute_percentage(self, amount, currency_id):
2012-02-07 19:38:56 +01:00
"Compute price based on a percentage of amount"
2012-09-24 22:17:48 +02:00
Currency = Pool().get('currency.currency')
2012-02-07 19:38:56 +01:00
2012-09-24 22:17:48 +02:00
price = amount * self.percentage / Decimal(100)
2012-02-07 19:38:56 +01:00
if not currency_id:
return price, currency_id
2012-09-24 22:17:48 +02:00
currency = Currency(currency_id)
return currency.round(price), currency_id
2012-02-07 19:38:56 +01:00
2012-09-24 22:17:48 +02:00
def get_sale_price(self):
price, currency_id = super(Carrier, self).get_sale_price()
if self.carrier_cost_method == 'percentage':
2012-02-07 19:38:56 +01:00
amount = Transaction().context.get('amount', Decimal(0))
currency_id = Transaction().context.get('currency', currency_id)
2012-09-24 22:17:48 +02:00
return self.compute_percentage(amount, currency_id)
2012-02-07 19:38:56 +01:00
return price, currency_id
2012-09-24 22:17:48 +02:00
def get_purchase_price(self):
price, currency_id = super(Carrier, self).get_purchase_price()
if self.carrier_cost_method == 'percentage':
2012-02-07 19:38:56 +01:00
amount = Transaction().context.get('amount', Decimal(0))
currency_id = Transaction().context.get('currency', currency_id)
2012-09-24 22:17:48 +02:00
return self.compute_percentage(amount, currency_id)
2012-02-07 19:38:56 +01:00
return price, currency_id