Files
tryton/carrier.py
T

132 lines
4.7 KiB
Python
Raw Normal View History

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.
2018-03-26 14:10:04 +02:00
from trytond.model import (
ModelView, ModelSQL, DeactivableMixin, MatchMixin, fields,
sequence_ordered)
2011-08-07 11:22:51 +02:00
from trytond.transaction import Transaction
2011-10-18 11:02:37 +02:00
from trytond.pool import Pool
2016-05-10 12:06:24 +02:00
from trytond.cache import Cache
2011-08-07 11:22:51 +02:00
2016-05-10 12:06:24 +02:00
__all__ = ['Carrier', 'CarrierSelection']
2012-09-24 22:17:47 +02:00
2011-08-07 11:22:51 +02:00
class Carrier(ModelSQL, ModelView):
'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.')
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'
2012-09-24 22:17:47 +02:00
User = Pool().get('res.user')
if self.carrier_cost_method == 'product':
2019-11-16 12:45:28 +01:00
user = User(Transaction().user)
2012-09-24 22:17:47 +02:00
return self.carrier_product.list_price, user.company.currency.id
2012-03-27 10:43:28 +02:00
return 0, 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'
2012-09-24 22:17:47 +02:00
User = Pool().get('res.user')
if self.carrier_cost_method == 'product':
user = User(Transaction().user)
return self.carrier_product.cost_price, user.company.currency.id
2012-03-27 10:43:28 +02:00
return 0, 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
2018-03-26 14:10:04 +02:00
class CarrierSelection(
DeactivableMixin, sequence_ordered(), MatchMixin, ModelSQL, ModelView):
2016-05-10 12:06:24 +02:00
'Carrier Selection'
__name__ = 'carrier.selection'
_get_carriers_cache = Cache('carrier.selection.get_carriers')
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):
selections = super(CarrierSelection, cls).create(*args, **kwargs)
cls._get_carriers_cache.clear()
return selections
@classmethod
def write(cls, *args, **kwargs):
super(CarrierSelection, cls).write(*args, **kwargs)
cls._get_carriers_cache.clear()
@classmethod
def delete(cls, *args, **kwargs):
super(CarrierSelection, cls).delete(*args, **kwargs)
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:
with Transaction().set_context(active_test=False):
carriers = Carrier.search([])
else:
for selection in selections:
if selection.match(pattern):
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