2014-12-02 12:55:08 +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.
|
2016-05-10 12:06:24 +02:00
|
|
|
from trytond.cache import Cache
|
2021-12-11 17:58:42 +01:00
|
|
|
from trytond.model import (
|
|
|
|
|
DeactivableMixin, MatchMixin, ModelSQL, ModelView, fields,
|
|
|
|
|
sequence_ordered)
|
|
|
|
|
from trytond.pool import Pool
|
2022-12-30 13:56:10 +01:00
|
|
|
from trytond.transaction import inactive_records
|
2011-08-07 11:22:51 +02:00
|
|
|
|
|
|
|
|
|
2020-04-11 15:03:15 +02:00
|
|
|
class Carrier(DeactivableMixin, ModelSQL, ModelView):
|
2011-08-07 11:22:51 +02:00
|
|
|
'Carrier'
|
2012-09-24 22:17:47 +02:00
|
|
|
__name__ = 'carrier'
|
2011-08-07 11:22:51 +02:00
|
|
|
party = fields.Many2One('party.party', 'Party', required=True,
|
2018-02-26 13:12:07 +01:00
|
|
|
ondelete='CASCADE', help="The party which represents the carrier.")
|
2011-08-07 11:22:51 +02:00
|
|
|
carrier_product = fields.Many2One('product.product', 'Carrier Product',
|
|
|
|
|
required=True, domain=[
|
|
|
|
|
('type', '=', 'service'),
|
2014-03-31 19:06:14 +02:00
|
|
|
('template.type', '=', 'service'),
|
2018-02-26 13:12:07 +01:00
|
|
|
], help="The product to invoice the carrier service.")
|
2011-08-07 11:22:51 +02:00
|
|
|
carrier_cost_method = fields.Selection([
|
|
|
|
|
('product', 'Product Price'),
|
|
|
|
|
], 'Carrier Cost Method', required=True,
|
2019-09-30 09:39:04 +02:00
|
|
|
help='Method to compute carrier cost.')
|
2019-12-05 22:29:59 +01:00
|
|
|
selections = fields.One2Many('carrier.selection', 'carrier', "Selections")
|
2011-08-07 11:22:51 +02:00
|
|
|
|
2012-09-24 22:17:47 +02:00
|
|
|
@staticmethod
|
|
|
|
|
def default_carrier_cost_method():
|
2011-08-07 11:22:51 +02:00
|
|
|
return 'product'
|
|
|
|
|
|
2012-09-24 22:17:47 +02:00
|
|
|
def get_rec_name(self, name):
|
|
|
|
|
return '%s - %s' % (self.party.rec_name, self.carrier_product.rec_name)
|
2012-01-02 18:52:27 +01:00
|
|
|
|
2019-07-15 10:39:55 +02:00
|
|
|
@classmethod
|
|
|
|
|
def search_rec_name(cls, name, clause):
|
|
|
|
|
if clause[1].startswith('!') or clause[1].startswith('not '):
|
|
|
|
|
bool_op = 'AND'
|
|
|
|
|
else:
|
|
|
|
|
bool_op = 'OR'
|
|
|
|
|
return [bool_op,
|
|
|
|
|
('party.rec_name',) + tuple(clause[1:]),
|
|
|
|
|
('carrier_product.rec_name',) + tuple(clause[1:]),
|
|
|
|
|
]
|
|
|
|
|
|
2012-09-24 22:17:47 +02:00
|
|
|
def get_sale_price(self):
|
2011-08-07 11:22:51 +02:00
|
|
|
'Compute carrier sale price with currency'
|
2021-08-27 08:48:34 +02:00
|
|
|
pool = Pool()
|
|
|
|
|
Company = pool.get('company.company')
|
2024-06-26 13:37:01 +02:00
|
|
|
if (self.carrier_cost_method == 'product'
|
|
|
|
|
and self.carrier_product._context.get('company') is not None):
|
2021-08-27 08:48:34 +02:00
|
|
|
list_price = self.carrier_product.list_price_used
|
|
|
|
|
if list_price is not None:
|
|
|
|
|
company = Company(self.carrier_product._context['company'])
|
|
|
|
|
return list_price, company.currency.id
|
2021-09-13 23:29:56 +02:00
|
|
|
return None, None
|
2011-08-07 11:22:51 +02:00
|
|
|
|
2012-09-24 22:17:47 +02:00
|
|
|
def get_purchase_price(self):
|
2011-08-07 11:22:51 +02:00
|
|
|
'Compute carrier purchase price with currency'
|
2021-08-27 08:48:34 +02:00
|
|
|
pool = Pool()
|
|
|
|
|
Company = pool.get('company.company')
|
2024-06-26 13:37:01 +02:00
|
|
|
if (self.carrier_cost_method == 'product'
|
|
|
|
|
and self.carrier_product._context.get('company') is not None):
|
2021-08-27 08:48:34 +02:00
|
|
|
cost_price = self.carrier_product.cost_price
|
|
|
|
|
if cost_price is not None:
|
|
|
|
|
company = Company(self.carrier_product._context['company'])
|
|
|
|
|
return cost_price, company.currency.id
|
2021-09-13 23:29:56 +02:00
|
|
|
return None, None
|
2016-05-10 12:06:24 +02:00
|
|
|
|
2016-07-08 10:50:58 +02:00
|
|
|
@classmethod
|
|
|
|
|
def create(cls, *args, **kwargs):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
CarrierSelection = pool.get('carrier.selection')
|
|
|
|
|
carriers = super(Carrier, cls).create(*args, **kwargs)
|
|
|
|
|
CarrierSelection._get_carriers_cache.clear()
|
|
|
|
|
return carriers
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def delete(cls, *args, **kwargs):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
CarrierSelection = pool.get('carrier.selection')
|
|
|
|
|
super(Carrier, cls).delete(*args, **kwargs)
|
|
|
|
|
CarrierSelection._get_carriers_cache.clear()
|
|
|
|
|
|
2016-05-10 12:06:24 +02:00
|
|
|
|
2020-03-01 12:33:52 +01:00
|
|
|
class Selection(
|
2018-03-26 14:10:04 +02:00
|
|
|
DeactivableMixin, sequence_ordered(), MatchMixin, ModelSQL, ModelView):
|
2016-05-10 12:06:24 +02:00
|
|
|
'Carrier Selection'
|
|
|
|
|
__name__ = 'carrier.selection'
|
2021-01-18 23:32:21 +01:00
|
|
|
_get_carriers_cache = Cache(
|
|
|
|
|
'carrier.selection.get_carriers', context=False)
|
2016-05-10 12:06:24 +02:00
|
|
|
|
|
|
|
|
from_country = fields.Many2One('country.country', 'From Country',
|
2018-02-26 13:12:07 +01:00
|
|
|
ondelete='RESTRICT',
|
|
|
|
|
help="Apply only when shipping from this country.\n"
|
|
|
|
|
"Leave empty for any countries.")
|
2016-05-10 12:06:24 +02:00
|
|
|
to_country = fields.Many2One('country.country', 'To Country',
|
2018-02-26 13:12:07 +01:00
|
|
|
ondelete='RESTRICT',
|
|
|
|
|
help="Apply only when shipping to this country.\n"
|
|
|
|
|
"Leave empty for any countries.")
|
2016-05-10 12:06:24 +02:00
|
|
|
carrier = fields.Many2One('carrier', 'Carrier', required=True,
|
2018-02-26 13:12:07 +01:00
|
|
|
ondelete='CASCADE', help="The selected carrier.")
|
2016-05-10 12:06:24 +02:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def create(cls, *args, **kwargs):
|
2020-03-01 12:33:52 +01:00
|
|
|
selections = super().create(*args, **kwargs)
|
2016-05-10 12:06:24 +02:00
|
|
|
cls._get_carriers_cache.clear()
|
|
|
|
|
return selections
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def write(cls, *args, **kwargs):
|
2020-03-01 12:33:52 +01:00
|
|
|
super().write(*args, **kwargs)
|
2016-05-10 12:06:24 +02:00
|
|
|
cls._get_carriers_cache.clear()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def delete(cls, *args, **kwargs):
|
2020-03-01 12:33:52 +01:00
|
|
|
super().delete(*args, **kwargs)
|
2016-05-10 12:06:24 +02:00
|
|
|
cls._get_carriers_cache.clear()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_carriers(cls, pattern):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
Carrier = pool.get('carrier')
|
|
|
|
|
|
2018-06-26 19:11:46 +02:00
|
|
|
key = tuple(sorted(pattern.items()))
|
2016-05-10 12:06:24 +02:00
|
|
|
carriers = cls._get_carriers_cache.get(key)
|
|
|
|
|
if carriers is not None:
|
|
|
|
|
return Carrier.browse(carriers)
|
|
|
|
|
|
|
|
|
|
carriers = []
|
2016-07-08 10:50:58 +02:00
|
|
|
selections = cls.search([])
|
|
|
|
|
if not selections:
|
2022-12-30 13:56:10 +01:00
|
|
|
with inactive_records():
|
2016-07-08 10:50:58 +02:00
|
|
|
carriers = Carrier.search([])
|
|
|
|
|
else:
|
|
|
|
|
for selection in selections:
|
2021-07-07 00:46:17 +02:00
|
|
|
if (selection.match(pattern)
|
|
|
|
|
and selection.carrier not in carriers):
|
2016-07-08 10:50:58 +02:00
|
|
|
carriers.append(selection.carrier)
|
2016-05-10 12:06:24 +02:00
|
|
|
|
2018-06-26 19:11:46 +02:00
|
|
|
cls._get_carriers_cache.set(key, list(map(int, carriers)))
|
2016-05-10 12:06:24 +02:00
|
|
|
return carriers
|