Files
tryton/carrier.py
T

120 lines
4.6 KiB
Python
Raw Normal View History

2014-12-02 12:55:11 +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:50:26 +01:00
from decimal import Decimal
2012-09-24 22:17:49 +02:00
from trytond.model import ModelSQL, ModelView, fields
2012-09-13 12:41:15 +02:00
from trytond.pyson import Eval, Bool, Id
2012-09-24 22:17:49 +02:00
from trytond.pool import Pool, PoolMeta
2012-02-07 19:50:26 +01:00
from trytond.transaction import Transaction
2012-09-24 22:17:49 +02:00
__all__ = ['Carrier', 'WeightPriceList']
2012-02-07 19:50:26 +01:00
2012-09-24 22:17:49 +02:00
class Carrier:
2016-03-11 21:01:53 +01:00
__metaclass__ = PoolMeta
2012-09-24 22:17:49 +02:00
__name__ = 'carrier'
2012-02-07 19:50:26 +01:00
weight_uom = fields.Many2One('product.uom', 'Weight Uom',
domain=[('category', '=', Id('product', 'uom_cat_weight'))],
states={
'invisible': Eval('carrier_cost_method') != 'weight',
'required': Eval('carrier_cost_method') == 'weight',
'readonly': Bool(Eval('weight_price_list', [])),
},
2018-02-26 13:08:29 +01:00
depends=['carrier_cost_method', 'weight_price_list'],
help="The unit of weight criteria of the price list.")
2014-01-26 14:29:08 +01:00
weight_uom_digits = fields.Function(fields.Integer('Weight Uom Digits'),
'on_change_with_weight_uom_digits')
2012-02-07 19:50:26 +01:00
weight_currency = fields.Many2One('currency.currency', 'Currency',
states={
'invisible': Eval('carrier_cost_method') != 'weight',
'required': Eval('carrier_cost_method') == 'weight',
'readonly': Bool(Eval('weight_price_list', [])),
},
2018-02-26 13:08:29 +01:00
depends=['carrier_cost_method', 'weight_price_list'],
help="The currency of the price.")
2012-02-07 19:50:26 +01:00
weight_currency_digits = fields.Function(fields.Integer(
2014-01-26 14:29:08 +01:00
'Weight Currency Digits'),
2012-09-24 22:17:49 +02:00
'on_change_with_weight_currency_digits')
2012-02-07 19:50:26 +01:00
weight_price_list = fields.One2Many('carrier.weight_price_list', 'carrier',
'Price List',
states={
'invisible': Eval('carrier_cost_method') != 'weight',
'readonly': ~(Eval('weight_uom', 0) & Eval('weight_currency', 0)),
},
2018-02-26 13:08:29 +01:00
depends=['carrier_cost_method', 'weight_uom', 'weight_currency'],
help="Add price to the carrier service.")
2012-02-07 19:50:26 +01:00
2012-09-24 22:17:49 +02:00
@classmethod
def __setup__(cls):
super(Carrier, cls).__setup__()
2012-02-07 19:50:26 +01:00
selection = ('weight', 'Weight')
2012-09-24 22:17:49 +02:00
if selection not in cls.carrier_cost_method.selection:
cls.carrier_cost_method.selection.append(selection)
2012-02-07 19:50:26 +01:00
2012-09-24 22:17:49 +02:00
@staticmethod
def default_weight_uom_digits():
2012-02-07 19:50:26 +01:00
return 2
2012-09-24 22:17:49 +02:00
@staticmethod
def default_weight_currency_digits():
Company = Pool().get('company.company')
2012-02-07 19:50:26 +01:00
company = Transaction().context.get('company')
if company:
2012-09-24 22:17:49 +02:00
return Company(company).currency.digits
2012-02-07 19:50:26 +01:00
return 2
2014-01-26 14:29:08 +01:00
@fields.depends('weight_uom')
2012-09-24 22:17:49 +02:00
def on_change_with_weight_uom_digits(self, name=None):
if self.weight_uom:
return self.weight_uom.digits
2012-02-07 19:50:26 +01:00
return 2
2014-01-26 14:29:08 +01:00
@fields.depends('weight_currency')
2012-09-24 22:17:49 +02:00
def on_change_with_weight_currency_digits(self, name=None):
if self.weight_currency:
return self.weight_currency.digits
2012-02-07 19:50:26 +01:00
return 2
2012-09-24 22:17:49 +02:00
def compute_weight_price(self, weight):
2012-02-07 19:50:26 +01:00
"Compute price based on weight"
2012-09-24 22:17:49 +02:00
for line in reversed(self.weight_price_list):
2012-02-07 19:50:26 +01:00
if line.weight < weight:
return line.price
return Decimal(0)
2012-09-24 22:17:49 +02:00
def get_sale_price(self):
price, currency_id = super(Carrier, self).get_sale_price()
if self.carrier_cost_method == 'weight':
2012-02-07 19:50:26 +01:00
weight_price = Decimal(0)
for weight in Transaction().context.get('weights', []):
2012-09-24 22:17:49 +02:00
weight_price += self.compute_weight_price(weight)
return weight_price, self.weight_currency.id
2012-02-07 19:50:26 +01:00
return price, currency_id
2012-09-24 22:17:49 +02:00
def get_purchase_price(self):
price, currency_id = super(Carrier, self).get_purchase_price()
if self.carrier_cost_method == 'weight':
2012-02-07 19:50:26 +01:00
weight_price = Decimal(0)
for weight in Transaction().context.get('weights', []):
2012-09-24 22:17:49 +02:00
weight_price += self.compute_weight_price(weight)
return weight_price, self.weight_currency.id
2012-02-07 19:50:26 +01:00
return price, currency_id
class WeightPriceList(ModelSQL, ModelView):
'Carrier Weight Price List'
2012-09-24 22:17:49 +02:00
__name__ = 'carrier.weight_price_list'
2018-02-26 13:08:29 +01:00
carrier = fields.Many2One('carrier', 'Carrier', required=True, select=True,
help="The carrier that the price list belongs to.")
2012-02-07 19:50:26 +01:00
weight = fields.Float('Weight',
2018-02-26 13:08:29 +01:00
digits=(16, Eval('_parent_carrier.weight_uom_digits', 2)),
help="The upper limit for the price.")
2012-02-07 19:50:26 +01:00
price = fields.Numeric('Price',
2018-02-26 13:08:29 +01:00
digits=(16, Eval('_parent_carrier.weight_currency_digits', 2)),
help="The price of the carrier service.")
2012-02-07 19:50:26 +01:00
2012-09-24 22:17:49 +02:00
@classmethod
def __setup__(cls):
super(WeightPriceList, cls).__setup__()
cls._order.insert(0, ('weight', 'ASC'))