Files
tryton/modules/stock/party.py
T

166 lines
5.5 KiB
Python
Raw Normal View History

2016-10-02 15:21:29 +02: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-12-29 14:20:29 +01:00
from trytond.i18n import gettext
2022-09-10 18:34:29 +02:00
from trytond.model import ModelSQL, ValueMixin, fields
2018-12-29 14:20:29 +01:00
from trytond.modules.party.exceptions import EraseError
2021-12-11 17:58:44 +01:00
from trytond.pool import Pool, PoolMeta
2022-09-10 18:34:29 +02:00
from trytond.transaction import Transaction
supplier_location = fields.Many2One(
'stock.location', "Supplier Location", domain=[('type', '=', 'supplier')],
help="The default source location for stock received from the party.")
customer_location = fields.Many2One(
'stock.location', "Customer Location", domain=[('type', '=', 'customer')],
help="The default destination location for stock sent to the party.")
class Party(metaclass=PoolMeta):
__name__ = 'party.party'
supplier_location = fields.MultiValue(supplier_location)
customer_location = fields.MultiValue(customer_location)
locations = fields.One2Many(
'party.party.location', 'party', "Locations")
delivered_to_warehouses = fields.Many2Many(
'party.party-delivered_to-stock.location', 'party', 'location',
"Delivered to Warehouses",
domain=[
('type', '=', 'warehouse'),
],
filter=[
('allow_pickup', '=', True),
])
@classmethod
def multivalue_model(cls, field):
pool = Pool()
if field in {'supplier_location', 'customer_location'}:
return pool.get('party.party.location')
return super().multivalue_model(field)
@classmethod
def default_supplier_location(cls, **pattern):
return cls.multivalue_model(
'supplier_location').default_supplier_location()
@classmethod
def default_customer_location(cls, **pattern):
return cls.multivalue_model(
'customer_location').default_customer_location()
def address_get(self, type=None):
pool = Pool()
Location = pool.get('stock.location')
context = Transaction().context
address = super().address_get(type=type)
if (type == 'delivery'
and context.get('warehouse')):
warehouse = Location(context['warehouse'])
if warehouse in self.delivered_to_warehouses:
address = warehouse.address
return address
class PartyLocation(ModelSQL, ValueMixin):
"Party Location"
__name__ = 'party.party.location'
party = fields.Many2One('party.party', "Party", ondelete='CASCADE')
2022-09-10 18:34:29 +02:00
supplier_location = supplier_location
customer_location = customer_location
@classmethod
def default_supplier_location(cls):
pool = Pool()
ModelData = pool.get('ir.model.data')
try:
return ModelData.get_id('stock', 'location_supplier')
except KeyError:
return None
@classmethod
def default_customer_location(cls):
pool = Pool()
ModelData = pool.get('ir.model.data')
try:
return ModelData.get_id('stock', 'location_customer')
except KeyError:
return None
class PartyDeliveredToWarehouse(ModelSQL):
"Party Delivered to Warehouse"
__name__ = 'party.party-delivered_to-stock.location'
party = fields.Many2One(
'party.party', "Party", required=True, ondelete='CASCADE')
2022-09-10 18:34:29 +02:00
location = fields.Many2One(
'stock.location', "Location", required=True, ondelete='CASCADE',
domain=[
('type', '=', 'warehouse'),
])
2018-12-29 14:20:29 +01:00
2016-10-02 15:21:29 +02:00
2021-02-13 22:10:53 +01:00
class Address(metaclass=PoolMeta):
__name__ = 'party.address'
delivery = fields.Boolean(
'Delivery',
help="Check to send deliveries to the address.")
2022-09-10 18:34:29 +02:00
warehouses = fields.One2Many(
'stock.location', 'address', "Warehouses", readonly=True)
2021-02-13 22:10:53 +01:00
class ContactMechanism(metaclass=PoolMeta):
__name__ = 'party.contact_mechanism'
delivery = fields.Boolean(
'Delivery',
help="Check to use for delivery.")
@classmethod
def usages(cls, _fields=None):
if _fields is None:
_fields = []
_fields.append('delivery')
return super().usages(_fields=_fields)
2020-03-01 12:33:54 +01:00
class Replace(metaclass=PoolMeta):
2016-10-02 15:21:29 +02:00
__name__ = 'party.replace'
@classmethod
def fields_to_replace(cls):
2020-03-01 12:33:54 +01:00
return super().fields_to_replace() + [
2016-10-02 15:21:29 +02:00
('stock.shipment.in', 'supplier'),
('stock.shipment.in.return', 'supplier'),
('stock.shipment.out', 'customer'),
('stock.shipment.out.return', 'customer'),
]
2018-03-24 00:10:37 +01:00
2020-03-01 12:33:54 +01:00
class Erase(metaclass=PoolMeta):
2018-03-24 00:10:37 +01:00
__name__ = 'party.erase'
def check_erase_company(self, party, company):
pool = Pool()
ShipmentIn = pool.get('stock.shipment.in')
ShipmentInReturn = pool.get('stock.shipment.in.return')
ShipmentOut = pool.get('stock.shipment.out')
ShipmentOutReturn = pool.get('stock.shipment.out.return')
2020-03-01 12:33:54 +01:00
super().check_erase_company(party, company)
2018-03-24 00:10:37 +01:00
for Shipment, field in [
(ShipmentIn, 'supplier'),
(ShipmentInReturn, 'supplier'),
(ShipmentOut, 'customer'),
(ShipmentOutReturn, 'customer'),
]:
shipments = Shipment.search([
(field, '=', party.id),
2019-12-14 10:47:54 +01:00
('company', '=', company.id),
('state', 'not in', ['done', 'cancelled']),
2018-03-24 00:10:37 +01:00
])
if shipments:
2018-12-29 14:20:29 +01:00
raise EraseError(
gettext('stock.msg_erase_party_shipment',
party=party.rec_name,
company=company.rec_name))