This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stock.py
318 lines (275 loc) · 10.5 KB
/
stock.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# 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 functools import wraps
from trytond.model import ModelView, Workflow, fields
from trytond.modules.product import round_price
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.transaction import Transaction
class Location(metaclass=PoolMeta):
__name__ = 'stock.location'
consignment_party = fields.Many2One(
'party.party', "Consignment Party",
states={
'invisible': ~Eval('type').in_(['supplier', 'storage']),
},
help="The party invoiced when consignment stock is used.")
@classmethod
def __setup__(cls):
super().__setup__()
cls.lost_found_location.states['invisible'] &= (
~Eval('type').in_(['supplier', 'customer']))
@classmethod
def _parent_domain(cls):
domain = super(Location, cls)._parent_domain()
domain['supplier'].append('storage')
domain['storage'].append('customer')
return domain
@property
def lost_found_used(self):
lost_found = super().lost_found_used
if not lost_found and not self.warehouse and self.type == 'storage':
location = self.parent
while location:
if location.type in {'supplier', 'storage'}:
lost_found = location.lost_found_location
break
location = location.parent
return lost_found
class LocationLeadTime(metaclass=PoolMeta):
__name__ = 'stock.location.lead_time'
@classmethod
def __setup__(cls):
super(LocationLeadTime, cls).__setup__()
cls.warehouse_to.domain = ['OR',
cls.warehouse_to.domain,
('type', '=', 'storage'),
]
def set_origin_consignment(func):
@wraps(func)
def wrapper(cls, moves):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
to_save = []
move2line = {}
for move in moves:
if not move.consignment_invoice_lines:
lines = move.get_invoice_lines_consignment()
if lines:
to_save.extend(lines)
move2line[move] = lines[0]
if to_save:
InvoiceLine.save(to_save)
for move, line in move2line.items():
if not move.origin:
move.origin = line
if move.unit_price is None:
move.unit_price = line.unit_price
move.currency = line.currency
cls.save(list(move2line.keys()))
return func(cls, moves)
return wrapper
def unset_origin_consignment(func):
@wraps(func)
def wrapper(cls, moves):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
lines, to_save = [], []
for move in moves:
for invoice_line in move.consignment_invoice_lines:
lines.append(invoice_line)
if move.origin == move:
move.origin = None
to_save.append(move)
if lines:
InvoiceLine.delete(lines)
cls.save(to_save)
return func(cls, moves)
return wrapper
class Move(metaclass=PoolMeta):
__name__ = 'stock.move'
consignment_invoice_lines = fields.One2Many(
'account.invoice.line', 'origin', "Consignment Invoice Lines",
readonly=True,
states={
'invisible': ~Eval('consignment_invoice_lines'),
})
@fields.depends('state', 'from_location', 'to_location')
def on_change_with_unit_price_required(self, name=None):
required = super().on_change_with_unit_price_required(name)
if (required
and self.state in {'staging', 'draft'}
and self.from_location
and self.to_location
and ((
self.from_location.type == 'supplier'
and self.to_location.type in {
'storage', 'production', 'customer'})
or (self.from_location.type in {
'storage', 'production', 'supplier'}
and self.to_location.type == 'customer'))
and self.from_location.consignment_party):
required = False
return required
@classmethod
def _get_origin(cls):
return super(Move, cls)._get_origin() + ['account.invoice.line']
@fields.depends('from_location')
def on_change_with_assignation_required(self, name=None):
required = super(Move, self).on_change_with_assignation_required(
name=name)
if self.from_location:
if (self.from_location.type == 'supplier'
and self.from_location.warehouse):
required = True
return required
def get_invoice_lines_consignment(self):
lines = []
if (self.from_location.type == 'supplier'
and self.to_location.type in {
'storage', 'production', 'customer'}
and self.from_location.consignment_party):
lines.append(self._get_supplier_invoice_line_consignment())
if (self.from_location.type in {'storage', 'production', 'supplier'}
and self.to_location.type == 'customer'
and self.from_location.consignment_party):
lines.append(self._get_customer_invoice_line_consignment())
return lines
def _get_supplier_invoice_line_consignment(self):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
Product = pool.get('product.product')
ProductSupplier = pool.get('purchase.product_supplier')
with Transaction().set_context(
supplier=self.from_location.consignment_party.id):
pattern = ProductSupplier.get_pattern()
for product_supplier in self.product.product_suppliers_used(**pattern):
currency = product_supplier.currency
break
else:
currency = self.company.currency
line = InvoiceLine()
line.invoice_type = 'in'
line.type = 'line'
line.company = self.company
line.party = self.from_location.consignment_party
line.currency = currency
line.product = self.product
line.quantity = self.quantity
line.unit = self.uom
line.stock_moves = [self]
line.origin = self
line.on_change_product()
with Transaction().set_context(
currency=line.currency.id,
supplier=line.party.id,
uom=line.unit,
taxes=[t.id for t in line.taxes]):
line.unit_price = Product.get_purchase_price(
[line.product], line.quantity)[line.product.id]
if line.unit_price is not None:
line.unit_price = round_price(line.unit_price)
return line
def _get_customer_invoice_line_consignment(self):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
Product = pool.get('product.product')
line = InvoiceLine()
line.invoice_type = 'out'
line.type = 'line'
line.company = self.company
line.party = self.from_location.consignment_party
line.currency = self.company.currency
line.product = self.product
line.quantity = self.quantity
line.unit = self.uom
line.stock_moves = [self]
line.origin = self
line.on_change_product()
with Transaction().set_context(
currency=line.currency.id,
customer=line.party.id,
uom=line.unit,
taxes=[t.id for t in line.taxes]):
line.unit_price = Product.get_sale_price(
[line.product], line.quantity)[line.product.id]
if line.unit_price is not None:
line.unit_price = round_price(line.unit_price)
return line
@classmethod
@ModelView.button
@Workflow.transition('draft')
@unset_origin_consignment
def draft(cls, moves):
super(Move, cls).draft(moves)
@classmethod
@ModelView.button
@Workflow.transition('assigned')
@set_origin_consignment
def assign(cls, moves):
super(Move, cls).assign(moves)
@classmethod
@ModelView.button
@Workflow.transition('done')
@set_origin_consignment
def do(cls, moves):
super(Move, cls).do(moves)
@classmethod
@ModelView.button
@Workflow.transition('cancelled')
@unset_origin_consignment
def cancel(cls, moves):
super(Move, cls).cancel(moves)
@classmethod
def copy(cls, moves, default=None):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
moves = super(Move, cls).copy(moves, default=default)
if not Transaction().context.get('_stock_move_split'):
to_save = []
for move in moves:
if isinstance(move.origin, InvoiceLine):
move.origin = None
to_save.append(move)
if to_save:
cls.save(to_save)
return moves
class ShipmentInternal(metaclass=PoolMeta):
__name__ = 'stock.shipment.internal'
@classmethod
def __setup__(cls):
super(ShipmentInternal, cls).__setup__()
cls.from_location.domain = ['OR',
cls.from_location.domain,
('type', '=', 'supplier'),
]
cls.to_location.domain = ['OR',
cls.to_location.domain,
('type', 'in', ['supplier', 'customer']),
]
@fields.depends('to_location')
def on_change_with_planned_start_date(self, pattern=None):
if pattern is None:
pattern = {}
if self.to_location and not self.to_location.warehouse:
pattern.setdefault('location_to', self.to_location.id)
return super(ShipmentInternal, self).on_change_with_planned_start_date(
pattern=pattern)
class Inventory(metaclass=PoolMeta):
__name__ = 'stock.inventory'
@classmethod
def __setup__(cls):
super(Inventory, cls).__setup__()
cls.location.domain = ['OR',
cls.location.domain,
('type', '=', 'supplier'),
]
class OrderPoint(metaclass=PoolMeta):
__name__ = 'stock.order_point'
@classmethod
def __setup__(cls):
super(OrderPoint, cls).__setup__()
cls.provisioning_location.domain = ['OR',
cls.provisioning_location.domain,
('type', '=', 'supplier'),
]