forked from OCA/purchase-workflow
/
test_purchase_request_to_rfq.py
419 lines (403 loc) · 17 KB
/
test_purchase_request_to_rfq.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Copyright 2018-2019 ForgeFlow, S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0)
from odoo import SUPERUSER_ID
from odoo.tests import common
class TestPurchaseRequestToRfq(common.TransactionCase):
def setUp(self):
super(TestPurchaseRequestToRfq, self).setUp()
self.purchase_request_obj = self.env["purchase.request"]
self.purchase_request_line_obj = self.env["purchase.request.line"]
self.wiz = self.env["purchase.request.line.make.purchase.order"]
self.purchase_order = self.env["purchase.order"]
vendor = self.env["res.partner"].create({"name": "Partner #2"})
self.service_product = self.env["product.product"].create(
{"name": "Product Service Test", "type": "service"}
)
self.env["product.supplierinfo"].create(
{
"partner_id": vendor.id,
"product_tmpl_id": self.service_product.product_tmpl_id.id,
}
)
self.product_product = self.env["product.product"].create(
{
"name": "Product Product Test",
"type": "product",
"description_purchase": "Test Description",
}
)
def test_wizard_default_get(self):
# Check that correct create items by purchase.request
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
"line_ids": [
[
0,
0,
{
"product_id": self.env.ref("product.product_product_13").id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 2.0,
},
]
],
}
purchase_request1 = self.purchase_request_obj.create(vals)
purchase_request1.button_approved()
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
"line_ids": [
[
0,
0,
{
"product_id": self.env.ref("product.product_product_13").id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 2.0,
},
]
],
}
purchase_request2 = self.purchase_request_obj.create(vals)
purchase_request2.button_approved()
vals = {"supplier_id": self.env.ref("base.res_partner_1").id}
wiz_id = self.wiz.with_context(
active_model="purchase.request.line",
active_ids=[
purchase_request1.mapped("line_ids").id,
purchase_request2.mapped("line_ids").id,
],
).create(vals)
self.assertEqual(
(purchase_request1 | purchase_request2).mapped("line_ids"),
wiz_id.item_ids.mapped("line_id"),
"Should have same purchase request lines",
)
def test_purchase_request_to_purchase_rfq(self):
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
}
purchase_request = self.purchase_request_obj.create(vals)
vals = {
"request_id": purchase_request.id,
"product_id": self.env.ref("product.product_product_13").id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 5.0,
}
purchase_request_line = self.purchase_request_line_obj.create(vals)
purchase_request.button_to_approve()
purchase_request.button_approved()
vals = {"supplier_id": self.env.ref("base.res_partner_12").id}
wiz_id = self.wiz.with_context(
active_model="purchase.request.line",
active_ids=[purchase_request_line.id],
active_id=purchase_request_line.id,
).create(vals)
wiz_id.make_purchase_order()
self.assertTrue(
len(purchase_request_line.purchase_lines), "Should have a purchase line"
)
self.assertEqual(
purchase_request_line.purchase_lines.product_id.id,
purchase_request_line.product_id.id,
"Should have same product",
)
self.assertEqual(
purchase_request_line.purchase_lines.state,
purchase_request_line.purchase_state,
"Should have same state",
)
def test_bug_is_editable_multiple_lines(self):
# Check that reading multiple lines is still possible
# https://github.com/OCA/purchase-workflow/pull/291
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
}
purchase_request = self.purchase_request_obj.create(vals)
vals = {
"request_id": purchase_request.id,
"product_id": self.env.ref("product.product_product_13").id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 5.0,
}
purchase_request_line = self.purchase_request_line_obj.create(vals)
request_lines = purchase_request_line + purchase_request_line.copy()
request_lines.mapped("is_editable")
# Test also for onchanges on non created lines
self.purchase_request_line_obj.new({}).is_editable
def test_purchase_request_to_purchase_rfq_minimum_order_qty(self):
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
}
purchase_request = self.purchase_request_obj.create(vals)
vals = {
"request_id": purchase_request.id,
"product_id": self.env.ref("product.product_product_8").id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 1.0,
}
purchase_request_line = self.purchase_request_line_obj.create(vals)
vals = {"supplier_id": self.env.ref("base.res_partner_1").id}
purchase_request.button_approved()
wiz_id = self.wiz.with_context(
active_model="purchase.request.line",
active_ids=[purchase_request_line.id],
active_id=purchase_request_line.id,
).create(vals)
wiz_id.make_purchase_order()
self.assertTrue(
len(purchase_request_line.purchase_lines), "Should have a purchase line"
)
self.assertEqual(
purchase_request_line.purchase_lines.product_id.id,
purchase_request_line.product_id.id,
"Should have same product",
)
self.assertEqual(
purchase_request_line.purchase_lines.state,
purchase_request_line.purchase_state,
"Should have same state",
)
self.assertEqual(
purchase_request_line.purchase_lines.product_qty,
5,
"The PO line should have the minimum order quantity.",
)
self.assertEqual(
purchase_request_line,
purchase_request_line.purchase_lines.purchase_request_lines,
"The PO should cross-reference to the purchase request.",
)
def test_purchase_request_to_purchase_rfq_multiple_po(self):
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
}
purchase_request1 = self.purchase_request_obj.create(vals)
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
}
purchase_request2 = self.purchase_request_obj.create(vals)
vals = {
"request_id": purchase_request1.id,
"product_id": self.env.ref("product.product_product_6").id,
"product_uom_id": self.env.ref("uom.product_uom_dozen").id,
"product_qty": 1.0,
}
purchase_request_line1 = self.purchase_request_line_obj.create(vals)
vals = {
"request_id": purchase_request2.id,
"product_id": self.env.ref("product.product_product_6").id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 1.0,
}
purchase_request_line2 = self.purchase_request_line_obj.create(vals)
vals = {
"request_id": purchase_request2.id,
"product_id": self.product_product.id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 1.0,
}
purchase_request_line3 = self.purchase_request_line_obj.create(vals)
vals = {"supplier_id": self.env.ref("base.res_partner_1").id}
purchase_request1.button_approved()
purchase_request2.button_approved()
wiz_id = self.wiz.with_context(
active_model="purchase.request.line",
active_ids=[
purchase_request_line1.id,
purchase_request_line2.id,
purchase_request_line3.id,
],
).create(vals)
for item in wiz_id.item_ids:
if item.line_id.id == purchase_request_line2.id:
item.keep_description = True
if item.line_id.id == purchase_request_line3.id:
item.onchange_product_id()
wiz_id.make_purchase_order()
self.assertEqual(
purchase_request_line1.rfq_qty, 1.0, "Quantity in RFQ should be 1"
)
self.assertEqual(
purchase_request_line2.rfq_qty, 1.0, "Quantity in RFQ should be 1"
)
purchase_request_line1.purchase_lines.order_id.button_confirm()
purchase_request_line1._compute_purchased_qty()
self.assertEqual(
purchase_request_line1.purchased_qty, 1.0, "Purchased quantity should be 1"
)
def test_purchase_request_to_purchase_rfq_multiple_PO_purchaseUoM(self):
product = self.env.ref("product.product_product_6")
product.uom_po_id = self.env.ref("uom.product_uom_dozen")
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
}
purchase_request1 = self.purchase_request_obj.create(vals)
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
}
purchase_request2 = self.purchase_request_obj.create(vals)
vals = {
"request_id": purchase_request1.id,
"product_id": product.id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 12.0,
}
purchase_request_line1 = self.purchase_request_line_obj.create(vals)
vals = {
"request_id": purchase_request2.id,
"product_id": product.id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 12.0,
}
purchase_request_line2 = self.purchase_request_line_obj.create(vals)
vals = {
"request_id": purchase_request2.id,
"product_id": product.id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 1.0,
}
purchase_request_line3 = self.purchase_request_line_obj.create(vals)
vals = {"supplier_id": self.env.ref("base.res_partner_1").id}
purchase_request1.button_approved()
purchase_request2.button_approved()
wiz_id = self.wiz.with_context(
active_model="purchase.request.line",
active_ids=[
purchase_request_line1.id,
purchase_request_line2.id,
purchase_request_line3.id,
],
).create(vals)
for item in wiz_id.item_ids:
if item.line_id.id == purchase_request_line2.id:
# PRL will be splitted into another POL to keep description
item.keep_description = True
wiz_id.make_purchase_order()
po_line = purchase_request_line1.purchase_lines[0]
self.assertEqual(po_line.product_qty, 1.09, "Quantity should be 1.09")
self.assertEqual(
po_line.product_uom,
self.env.ref("uom.product_uom_dozen"),
"The purchase UoM should be Dozen(s).",
)
def test_purchase_request_to_rfq_minimum_order_qty_existing_po(self):
# Define Supplier
supplier = self.env.ref("base.res_partner_1")
# Create Product Widget min_qty = 5
product = self.env["product.product"].create(
{
"name": "Widget",
"type": "product",
"seller_ids": [
(0, 0, {"partner_id": supplier.id, "delay": 10, "min_qty": 5})
],
}
)
# Create Purchase Order with qty = 3 through Purchase Request
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
}
purchase_request1 = self.purchase_request_obj.create(vals)
vals = {
"request_id": purchase_request1.id,
"product_id": product.id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 3.0,
}
purchase_request_line1 = self.purchase_request_line_obj.create(vals)
purchase_request1.button_approved()
vals = {"supplier_id": supplier.id}
wiz_id = self.wiz.with_context(
active_model="purchase.request.line", active_ids=[purchase_request_line1.id]
).create(vals)
wiz_id.make_purchase_order()
# The planned date is taken from the request, not from the supplier
self.assertEqual(
purchase_request_line1.date_required.day,
purchase_request_line1.purchase_lines.date_planned.day,
)
po = purchase_request_line1.purchase_lines[0].order_id
# Create Purchase Request
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
}
purchase_request2 = self.purchase_request_obj.create(vals)
# Create Purchase Request Line qty = 3
vals = {
"request_id": purchase_request2.id,
"product_id": product.id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 3.0,
}
purchase_request_line2 = self.purchase_request_line_obj.create(vals)
# Validate
purchase_request2.button_approved()
# Create RFQ to Previous PO
vals = {"supplier_id": supplier.id, "purchase_order_id": po.id}
wiz_id = self.wiz.with_context(
active_model="purchase.request.line", active_ids=[purchase_request_line2.id]
).create(vals)
wiz_id.make_purchase_order()
# Check Purchase qty should be 6
po_line = purchase_request_line2.purchase_lines[0]
# Add unit price in PO Line
po_line.write({"price_unit": 10})
self.assertEqual(po_line.product_qty, 6.0, "Quantity should be 6")
# auto change state to done
po_line.order_id.button_confirm()
picking = po_line.order_id.picking_ids[0]
picking.move_line_ids[0].write({"qty_done": 6.0})
picking.button_validate()
def _setup_analytic_distribution(self):
analytic_plan = self.env["account.analytic.plan"].create(
{"name": "Plan Test", "company_id": False}
)
analytic_account = self.env["account.analytic.account"].create(
{"name": "default", "plan_id": analytic_plan.id}
)
return {str(analytic_account.id): 100}
def test_purchase_request_to_purchase_order_analytic_data_propagation(self):
analytic_distribution = self._setup_analytic_distribution()
vals = {
"picking_type_id": self.env.ref("stock.picking_type_in").id,
"requested_by": SUPERUSER_ID,
"line_ids": [
(
0,
0,
{
"product_id": self.env.ref("product.product_product_10").id,
"product_uom_id": self.env.ref("uom.product_uom_unit").id,
"product_qty": 5.0,
"analytic_distribution": analytic_distribution,
},
)
],
}
purchase_request = self.purchase_request_obj.create(vals)
purchase_request.button_to_approve()
purchase_request.button_approved()
supplier = self.env.ref("base.res_partner_12")
vals = {
"supplier_id": supplier.id,
}
wiz_id = self.wiz.with_context(
active_model="purchase.request.line",
active_ids=[purchase_request["line_ids"][0].id],
active_id=purchase_request["line_ids"][0].id,
).create(vals)
wiz_id.make_purchase_order()
po_line = purchase_request["line_ids"][0].purchase_lines[0]
self.assertEqual(po_line.analytic_distribution, analytic_distribution)