This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
party.py
183 lines (152 loc) · 6.15 KB
/
party.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond import backend
from trytond.i18n import gettext
from trytond.model import ModelSQL, ValueMixin, fields
from trytond.modules.party.exceptions import EraseError
from trytond.pool import Pool, PoolMeta
from trytond.tools.multivalue import migrate_property
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')
supplier_location = supplier_location
customer_location = customer_location
@classmethod
def __register__(cls, module_name):
exist = backend.TableHandler.table_exist(cls._table)
super().__register__(module_name)
if not exist:
cls._migrate_property([], [], [])
@classmethod
def _migrate_property(cls, field_names, value_names, fields):
field_names.extend(['supplier_location', 'customer_location'])
value_names.extend(['supplier_location', 'customer_location'])
migrate_property(
'party.party', field_names, cls, value_names,
parent='party', fields=fields)
@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)
location = fields.Many2One(
'stock.location', "Location", required=True, ondelete='CASCADE',
domain=[
('type', '=', 'warehouse'),
])
class Address(metaclass=PoolMeta):
__name__ = 'party.address'
delivery = fields.Boolean(
'Delivery',
help="Check to send deliveries to the address.")
warehouses = fields.One2Many(
'stock.location', 'address', "Warehouses", readonly=True)
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)
class Replace(metaclass=PoolMeta):
__name__ = 'party.replace'
@classmethod
def fields_to_replace(cls):
return super().fields_to_replace() + [
('stock.shipment.in', 'supplier'),
('stock.shipment.in.return', 'supplier'),
('stock.shipment.out', 'customer'),
('stock.shipment.out.return', 'customer'),
]
class Erase(metaclass=PoolMeta):
__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')
super().check_erase_company(party, company)
for Shipment, field in [
(ShipmentIn, 'supplier'),
(ShipmentInReturn, 'supplier'),
(ShipmentOut, 'customer'),
(ShipmentOutReturn, 'customer'),
]:
shipments = Shipment.search([
(field, '=', party.id),
('company', '=', company.id),
('state', 'not in', ['done', 'cancelled']),
])
if shipments:
raise EraseError(
gettext('stock.msg_erase_party_shipment',
party=party.rec_name,
company=company.rec_name))