This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
purchase.py
350 lines (312 loc) · 14.5 KB
/
purchase.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# 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.model import fields
from trytond.modules.currency.fields import Monetary
from trytond.modules.product import price_digits, round_price
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval, If
class Line(metaclass=PoolMeta):
__name__ = 'purchase.line'
secondary_quantity = fields.Function(fields.Float(
"Secondary Quantity", digits='secondary_unit',
states={
'invisible': ((Eval('type') != 'line')
| ~Eval('secondary_unit')),
'readonly': Eval('purchase_state') != 'draft',
}),
'on_change_with_secondary_quantity', setter='set_secondary')
secondary_unit = fields.Many2One(
'product.uom', "Secondary Unit", ondelete='RESTRICT',
domain=[
If(Eval('purchase_state') == 'draft',
('category', '=', Eval('product_secondary_uom_category')),
()),
],
states={
'invisible': ((Eval('type') != 'line')
| (~Eval('secondary_uom_factor')
& ~Eval('secondary_uom_rate'))),
'readonly': Eval('purchase_state') != 'draft',
})
secondary_unit_price = fields.Function(Monetary(
"Secondary Unit Price", currency='currency', digits=price_digits,
states={
'invisible': ((Eval('type') != 'line')
| ~Eval('secondary_unit')),
'readonly': Eval('purchase_state') != 'draft',
}),
'on_change_with_secondary_unit_price', setter='set_secondary')
secondary_uom_factor = fields.Float("Secondary UOM Factor")
secondary_uom_rate = fields.Float("Secondary UOM Rate")
product_secondary_uom_category = fields.Function(
fields.Many2One(
'product.uom.category', "Product Secondary UOM Category"),
'on_change_with_product_secondary_uom_category')
@fields.depends('product', 'product_supplier')
def _secondary_record(self):
if (self.product_supplier
and self.product_supplier.purchase_secondary_uom):
return self.product_supplier
elif self.product and self.product.purchase_secondary_uom:
return self.product
@fields.depends('quantity', 'unit', 'secondary_unit',
'secondary_uom_factor', 'secondary_uom_rate')
def on_change_with_secondary_quantity(self, name=None):
pool = Pool()
Uom = pool.get('product.uom')
if (self.quantity and self.unit and self.secondary_unit
and (self.secondary_uom_factor or self.secondary_uom_rate)):
return Uom.compute_qty(
self.unit, self.quantity,
self.secondary_unit, round=True,
factor=self.secondary_uom_factor, rate=self.secondary_uom_rate)
else:
return None
@fields.depends('secondary_quantity', 'secondary_unit', 'unit',
'secondary_uom_factor', 'secondary_uom_rate',
methods=['on_change_quantity', 'on_change_with_amount'])
def on_change_secondary_quantity(self):
pool = Pool()
Uom = pool.get('product.uom')
if (self.secondary_quantity and self.secondary_unit and self.unit
and (self.secondary_uom_factor or self.secondary_uom_rate)):
self.quantity = Uom.compute_qty(
self.secondary_unit, self.secondary_quantity,
self.unit, round=True,
factor=self.secondary_uom_rate, rate=self.secondary_uom_factor)
self.on_change_quantity()
self.amount = self.on_change_with_amount()
@fields.depends('unit_price', 'unit', 'secondary_unit',
'secondary_uom_factor', 'secondary_uom_rate')
def on_change_with_secondary_unit_price(self, name=None):
pool = Pool()
Uom = pool.get('product.uom')
if (self.unit_price is not None and self.unit and self.secondary_unit
and (self.secondary_uom_factor or self.secondary_uom_rate)):
unit_price = Uom.compute_price(
self.unit, self.unit_price, self.secondary_unit,
factor=self.secondary_uom_factor, rate=self.secondary_uom_rate)
return round_price(unit_price)
else:
return None
@fields.depends('secondary_unit_price', 'secondary_unit', 'unit',
'secondary_uom_factor', 'secondary_uom_rate',
methods=['on_change_with_amount'])
def on_change_secondary_unit_price(self, name=None):
pool = Pool()
Uom = pool.get('product.uom')
if (self.secondary_unit_price is not None
and self.secondary_unit and self.unit
and (self.secondary_uom_factor or self.secondary_uom_rate)):
self.unit_price = Uom.compute_price(
self.secondary_unit, self.secondary_unit_price, self.unit,
factor=self.secondary_uom_rate, rate=self.secondary_uom_factor)
self.unit_price = round_price(self.unit_price)
self.amount = self.on_change_with_amount()
@fields.depends(methods=[
'on_change_secondary_quantity', 'on_change_secondary_unit_price'])
def on_change_secondary_unit(self):
self.on_change_secondary_quantity()
self.on_change_secondary_unit_price()
@fields.depends(methods=['_secondary_record'])
def on_change_with_product_secondary_uom_category(self, name=None):
secondary_record = self._secondary_record()
if secondary_record:
return secondary_record.purchase_secondary_uom.category.id
@classmethod
def set_secondary(cls, lines, name, value):
pass
@fields.depends('secondary_unit',
methods=['on_change_with_secondary_quantity', '_secondary_record'])
def on_change_product(self):
super().on_change_product()
secondary_record = self._secondary_record()
if secondary_record and self.secondary_unit:
if (self.secondary_unit.category
!= secondary_record.purchase_secondary_uom.category):
self.secondary_unit = None
if secondary_record:
self.secondary_uom_factor = (
secondary_record.purchase_secondary_uom_normal_factor)
self.secondary_uom_rate = (
secondary_record.purchase_secondary_uom_normal_rate)
else:
self.secondary_unit = None
self.secondary_uom_factor = None
self.secondary_uom_rate = None
self.secondary_quantity = self.on_change_with_secondary_quantity()
def get_invoice_line(self):
pool = Pool()
InvoiceLine = pool.get('account.invoice.line')
lines = super().get_invoice_line()
if hasattr(InvoiceLine, 'secondary_unit'):
for line in lines:
if line.type != 'line':
continue
if line.unit == self.unit:
line.secondary_unit = self.secondary_unit
return lines
def get_move(self, move_type):
move = super().get_move(move_type)
if move and hasattr(move.__class__, 'secondary_unit'):
if move.uom == self.unit:
move.secondary_unit = self.secondary_unit
return move
class RequisitionLine(metaclass=PoolMeta):
__name__ = 'purchase.requisition.line'
product_secondary_uom_category = fields.Function(
fields.Many2One(
'product.uom.category', "Product Secondary UOM Category"),
'on_change_with_product_secondary_uom_category')
@classmethod
def _unit_categories(cls):
return super()._unit_categories() + ['product_secondary_uom_category']
@fields.depends('product')
def on_change_with_product_secondary_uom_category(self, name=None):
if self.product and self.product.purchase_secondary_uom:
return self.product.purchase_secondary_uom.category.id
@property
def request_unit(self):
unit = super().request_unit
product = self.product
if (product
and self.unit
and product.purchase_secondary_uom
and (self.unit.category
== product.purchase_secondary_uom.category)):
unit = product.purchase_uom
return unit
@property
def request_quantity(self):
pool = Pool()
Uom = pool.get('product.uom')
quantity = super().request_quantity
product = self.product
request_unit = self.request_unit
if (product
and self.unit
and request_unit
and product.purchase_secondary_uom
and (self.unit.category
== product.purchase_secondary_uom.category)
and request_unit.category == product.purchase_uom.category):
quantity = Uom.compute_qty(
self.unit, self.quantity, request_unit, round=True,
factor=product.purchase_secondary_uom_normal_rate,
rate=product.purchase_secondary_uom_normal_factor)
return quantity
@property
def request_unit_price(self):
pool = Pool()
Uom = pool.get('product.uom')
unit_price = super().request_unit_price
product = self.product
if (product
and self.unit
and self.unit_price
and product.purchase_secondary_uom
and (self.unit.category
== product.purchase_secondary_uom.category)):
unit_price = round_price(
Uom.compute_price(
self.unit, self.unit_price, product.purchase_uom,
factor=product.purchase_secondary_uom_normal_rate,
rate=product.purchase_secondary_uom_normal_factor))
return unit_price
class BlanketAgreementLine(metaclass=PoolMeta):
__name__ = 'purchase.blanket_agreement.line'
product_secondary_uom_category = fields.Function(
fields.Many2One(
'product.uom.category', "Product Secondary UOM Category"),
'on_change_with_product_secondary_uom_category')
@classmethod
def _unit_categories(cls):
return super()._unit_categories() + ['product_secondary_uom_category']
def is_same_uom_category(self, purchase_line):
return super().is_same_uom_category(purchase_line) or (
purchase_line.secondary_unit and (
self.unit.category == purchase_line.secondary_unit.category))
def remainig_quantity_for_purchase(self, line, round=True):
pool = Pool()
Uom = pool.get('product.uom')
remaining_quantity = super().remainig_quantity_for_purchase(
line, round=round)
if (self.remaining_quantity is not None
and line.secondary_unit
and self.unit.category == line.secondary_unit.category):
remaining_quantity = Uom.compute_qty(
self.unit, self.remaining_quantity, line.unit,
factor=line.secondary_uom_rate,
rate=line.secondary_uom_factor,
round=round)
return remaining_quantity
@fields.depends('product')
def on_change_with_product_secondary_uom_category(self, name=None):
if self.product and self.product.purchase_secondary_uom:
return self.product.purchase_secondary_uom.category.id
def _set_purchase_line_quantity(self, purchase_line):
super()._set_purchase_line_quantity(purchase_line)
secondary_uom = self.product.purchase_secondary_uom
if (secondary_uom
and self.unit.category == secondary_uom.category):
purchase_line.unit = self.product.purchase_uom
purchase_line.secondary_quantity = self.remaining_quantity or 0
purchase_line.secondary_unit = self.unit
purchase_line.secondary_unit_price = self.unit_price
purchase_line.on_change_secondary_quantity()
class LineBlanketAgreement(metaclass=PoolMeta):
__name__ = 'purchase.line'
@fields.depends(
'blanket_agreement_line', '_parent_blanket_agreement_line.unit',
'_parent_blanket_agreement_line.unit_price', 'unit', 'secondary_unit')
def compute_unit_price(self):
pool = Pool()
Uom = pool.get('product.uom')
unit_price = super().compute_unit_price()
line = self.blanket_agreement_line
if (line
and self.unit
and self.secondary_unit
and line.unit
and self.secondary_unit.category == line.unit.category):
secondary_unit_price = Uom.compute_price(
line.unit, line.unit_price, self.secondary_unit)
unit_price = Uom.compute_price(
self.secondary_unit, secondary_unit_price, self.unit,
factor=self.secondary_uom_rate, rate=self.secondary_uom_factor)
unit_price = round_price(unit_price)
return unit_price
@fields.depends(
'blanket_agreement_line',
'_parent_blanket_agreement_line.unit',
'_parent_blanket_agreement_line.remaining_quantity',
'secondary_quantity', 'secondary_unit',
methods=['on_change_secondary_quantity'])
def on_change_blanket_agreement_line(self):
pool = Pool()
Uom = pool.get('product.uom')
super().on_change_blanket_agreement_line()
if self.blanket_agreement_line:
line = self.blanket_agreement_line
if (self.secondary_unit and line.unit
and self.secondary_unit.category == line.unit.category):
if line.remaining_quantity is not None:
remaining_quantity = Uom.compute_qty(
line.unit, line.remaining_quantity,
self.secondary_unit)
if (self.secondary_quantity is None
or self.secondary_quantity > remaining_quantity):
self.secondary_quantity = remaining_quantity
self.on_change_secondary_quantity()
def quantity_for_blanket_agreement(self, line, round=True):
pool = Pool()
Uom = pool.get('product.uom')
quantity = super().quantity_for_blanket_agreement(line, round=round)
if (self.secondary_unit
and self.secondary_unit.category == line.unit.category):
quantity = Uom.compute_qty(
self.unit, (self.actual_quantity or self.quantity), line.unit,
factor=self.secondary_uom_factor,
rate=self.secondary_uom_rate,
round=round)
return quantity