Files
tryton/modules/carrier_weight/carrier.py
T

115 lines
4.4 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
2021-08-21 09:09:55 +02:00
from trytond.modules.currency.fields import Monetary
2021-12-11 17:58:42 +01:00
from trytond.pool import PoolMeta
from trytond.pyson import Bool, Eval, Id
from trytond.transaction import Transaction
2021-08-21 09:09:55 +02:00
2012-02-07 19:50:26 +01:00
2018-06-26 19:11:46 +02:00
class Carrier(metaclass=PoolMeta):
2012-09-24 22:17:49 +02:00
__name__ = 'carrier'
2023-08-20 12:17:27 +02:00
weight_uom = fields.Many2One(
'product.uom', "Weight UoM",
2012-02-07 19:50:26 +01:00
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', [])),
},
2023-08-20 12:17:27 +02:00
help="The Unit of Measure of weight criteria for the price list.")
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
help="The currency of the price.")
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)),
},
2023-01-30 17:37:12 +01:00
help="Add price per weight to the carrier service.\n"
"The first line for which the weight is greater is used.\n"
"The line with weight of 0 is used as default price.")
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
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
2023-01-30 17:37:12 +01:00
else:
if not line.weight and not weight:
return line.price
2012-02-07 19:50:26 +01:00
return Decimal(0)
2023-01-30 17:37:12 +01:00
def _get_weight_price(self):
weights = Transaction().context.get('weights', [])
if weights:
weight_price = sum(
self.compute_weight_price(w) for w in weights)
else:
weight_price = self.compute_weight_price(0)
return weight_price, self.weight_currency.id
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':
2023-01-30 17:37:12 +01:00
price, currency_id = self._get_weight_price()
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':
2023-01-30 17:37:12 +01:00
price, currency_id = self._get_weight_price()
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'
carrier = fields.Many2One(
'carrier', "Carrier", required=True,
2018-02-26 13:08:29 +01:00
help="The carrier that the price list belongs to.")
weight = fields.Float(
"Weight", digits='weight_uom',
2023-01-30 17:37:12 +01:00
domain=[
('weight', '>=', 0),
],
depends={'carrier'},
help="The lower limit for the price.")
2021-08-21 09:09:55 +02:00
price = Monetary(
"Price", currency='currency', digits='currency',
2018-02-26 13:08:29 +01:00
help="The price of the carrier service.")
2012-02-07 19:50:26 +01:00
2021-08-21 09:09:55 +02:00
currency = fields.Function(fields.Many2One(
'currency.currency', "Currency"),
'on_change_with_currency')
weight_uom = fields.Function(fields.Many2One(
'product.uom', "Weight UoM",
help="The Unit of Measure of the weight."),
'on_change_with_weight_uom')
2021-08-21 09:09:55 +02:00
2012-09-24 22:17:49 +02:00
@classmethod
def __setup__(cls):
super(WeightPriceList, cls).__setup__()
cls._order.insert(0, ('weight', 'ASC'))
2021-08-21 09:09:55 +02:00
@fields.depends('carrier', '_parent_carrier.weight_currency')
def on_change_with_currency(self, name=None):
2023-03-26 09:49:00 +02:00
return self.carrier.weight_currency if self.carrier else None
@fields.depends('carrier', '_parent_carrier.weight_uom')
def on_change_with_weight_uom(self, name=None):
return self.carrier.weight_uom if self.carrier else None