Debian Codegraph @

addons/sale_purchase/tests/test_sale_purchase.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import Command
4 from odoo.exceptions import UserError, AccessError
5 from odoo.tests import tagged
6 from odoo.addons.sale_purchase.tests.common import TestCommonSalePurchaseNoChart
7
8
9 @tagged('-at_install', 'post_install')
10 class TestSalePurchase(TestCommonSalePurchaseNoChart):
11
12 @classmethod
13 def setUpClass(cls):
14 super().setUpClass()
15
16 cls.company_data_2 = cls.setup_other_company()
17
18 # create a generic Sale Order with 2 classical products and a purchase service
19 SaleOrder = cls.env['sale.order']
20 cls.analytic_plan = cls.env['account.analytic.plan'].create({'name': 'Plan Test'})
21 cls.test_analytic_account_1, cls.test_analytic_account_2 = cls.env['account.analytic.account'].create([
22 {
23 'name': 'analytic_account_test_1',
24 'plan_id': cls.analytic_plan.id,
25 }, {
26 'name': 'analytic_account_test_2',
27 'plan_id': cls.analytic_plan.id,
28 },
29 ])
30 cls.sale_order_1 = SaleOrder.create({
31 'partner_id': cls.partner_a.id,
32 'partner_invoice_id': cls.partner_a.id,
33 'partner_shipping_id': cls.partner_a.id,
34 })
35 cls.sol1_service_deliver = cls.env['sale.order.line'].create({
36 'product_id': cls.company_data['product_service_delivery'].id,
37 'product_uom_qty': 1,
38 'order_id': cls.sale_order_1.id,
39 'tax_ids': False,
40 })
41 cls.sol1_product_order = cls.env['sale.order.line'].create({
42 'product_id': cls.company_data['product_order_no'].id,
43 'product_uom_qty': 2,
44 'order_id': cls.sale_order_1.id,
45 'tax_ids': False,
46 })
47 cls.sol1_service_purchase_1 = cls.env['sale.order.line'].create({
48 'product_id': cls.service_purchase_1.id,
49 'product_uom_qty': 4,
50 'order_id': cls.sale_order_1.id,
51 'tax_ids': False,
52 })
53
54 cls.sale_order_2 = SaleOrder.create({
55 'partner_id': cls.partner_a.id,
56 'partner_invoice_id': cls.partner_a.id,
57 'partner_shipping_id': cls.partner_a.id,
58 })
59 cls.sol2_product_deliver = cls.env['sale.order.line'].create({
60 'product_id': cls.company_data['product_delivery_no'].id,
61 'product_uom_qty': 5,
62 'order_id': cls.sale_order_2.id,
63 'tax_ids': False,
64 })
65 cls.sol2_service_order = cls.env['sale.order.line'].create({
66 'product_id': cls.company_data['product_service_order'].id,
67 'product_uom_qty': 6,
68 'order_id': cls.sale_order_2.id,
69 'tax_ids': False,
70 })
71 cls.sol2_service_purchase_2 = cls.env['sale.order.line'].create({
72 'product_id': cls.service_purchase_2.id,
73 'product_uom_qty': 7,
74 'order_id': cls.sale_order_2.id,
75 'tax_ids': False,
76 })
77
78 def test_sale_create_purchase(self):
79 """ Confirming 2 sales orders with a service that should create two PO, then cancelling the PO should shedule 1 next activity per SO """
80 self.sale_order_1.action_confirm()
81 self.sale_order_2.action_confirm()
82
83 purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.service_purchase_1.seller_ids.partner_id.id), ('state', '=', 'draft')])
84 purchase_lines_so1 = self.env['purchase.order.line'].search([('sale_line_id', 'in', self.sale_order_1.order_line.ids)])
85 purchase_line1 = purchase_lines_so1[0]
86
87 purchase_lines_so2 = self.env['purchase.order.line'].search([('sale_line_id', 'in', self.sale_order_2.order_line.ids)])
88 purchase_line2 = purchase_lines_so2[0]
89
90 self.assertEqual(len(purchase_order), 2, "Two PO should have been created, from the 2 Sales orders")
91 self.assertEqual(len(purchase_order.order_line), 2, "The purchase order should have 2 lines")
92 self.assertIn(self.sale_order_1.name, purchase_order[1].origin, "The PO should have SO 1 in its source documents")
93 self.assertIn(self.sale_order_2.name, purchase_order[0].origin, "The PO should have SO 2 in its source documents")
94 self.assertEqual(len(purchase_lines_so1), 1, "Only one SO line from SO 1 should have create a PO line")
95 self.assertEqual(len(purchase_lines_so2), 1, "Only one SO line from SO 2 should have create a PO line")
96 self.assertEqual(len(purchase_order.activity_ids), 0, "No activity should be scheduled on the PO")
97 self.assertEqual(set(purchase_order.mapped('state')), {'draft'}, "The created PO should be in draft state.")
98
99 self.assertNotEqual(purchase_line1.product_id, purchase_line2.product_id, "The 2 PO line should have different products")
100 self.assertEqual(purchase_line1.product_id, self.sol1_service_purchase_1.product_id, "The create PO line must have the same product as its mother SO line")
101 self.assertEqual(purchase_line2.product_id, self.sol2_service_purchase_2.product_id, "The create PO line must have the same product as its mother SO line")
102
103 self.assertEqual(purchase_line1.price_unit, self.service_purchase_1.seller_ids.price, "Unit price should be taken from the vendor line")
104 self.assertEqual(purchase_line2.price_unit, self.service_purchase_2.seller_ids.price, "Unit price should be taken from the vendor line")
105 self.assertEqual(purchase_line1.discount, self.service_purchase_1.seller_ids.discount, "Discount should be taken from the vendor line")
106
107 purchase_order.button_cancel()
108
109 self.assertEqual(len(self.sale_order_1.activity_ids), 1, "One activity should be scheduled on the SO 1 since the PO has been cancelled")
110 self.assertEqual(self.sale_order_1.user_id, self.sale_order_1.activity_ids[0].user_id, "The activity should be assigned to the SO responsible")
111
112 self.assertEqual(len(self.sale_order_2.activity_ids), 1, "One activity should be scheduled on the SO 2 since the PO has been cancelled")
113 self.assertEqual(self.sale_order_2.user_id, self.sale_order_2.activity_ids[0].user_id, "The activity should be assigned to the SO responsible")
114
115 def test_uom_conversion(self):
116 """ Test generated PO use the right UoM according to product configuration """
117 self.service_purchase_2.seller_ids.product_uom_id = self.env.ref('uom.product_uom_unit')
118 self.sale_order_2.action_confirm()
119 purchase_line = self.env['purchase.order.line'].search([('sale_line_id', '=', self.sol2_service_purchase_2.id)]) # only one line
120
121 self.assertTrue(purchase_line, "The SO line should generate a PO line")
122 self.assertEqual(purchase_line.product_uom_id, self.service_purchase_2.seller_ids.product_uom_id, "The UoM on the purchase line should be the one from the product configuration")
123 self.assertNotEqual(purchase_line.product_uom_id, self.sol2_service_purchase_2.product_uom_id, "As the product configuration, the UoM on the SO line should still be different from the one on the PO line")
124 self.assertEqual(purchase_line.product_qty, self.sol2_service_purchase_2.product_uom_qty * 12, "The quantity from the SO should be converted with th UoM factor on the PO line")
125
126 def test_no_supplier(self):
127 """ Test confirming SO with product with no supplier raise Error """
128 # delete the suppliers
129 self.service_purchase_1.seller_ids.unlink()
130 # confirm the SO should raise UserError
131 with self.assertRaises(UserError):
132 self.sale_order_1.action_confirm()
133
134 def test_reconfirm_sale_order(self):
135 """ Confirm SO, cancel it, then re-confirm it should not regenerate a purchase line """
136 self.sale_order_1.action_confirm()
137
138 purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.service_purchase_1.seller_ids.partner_id.id), ('state', '=', 'draft')])
139 purchase_lines = self.env['purchase.order.line'].search([('sale_line_id', 'in', self.sale_order_1.order_line.ids)])
140 purchase_line = purchase_lines[0]
141
142 self.assertEqual(len(purchase_lines), 1, "Only one purchase line should be created on SO confirmation")
143 self.assertEqual(len(purchase_order), 1, "One purchase order should have been created on SO confirmation")
144 self.assertEqual(len(purchase_order.order_line), 1, "Only one line on PO, after SO confirmation")
145 self.assertEqual(purchase_order, purchase_lines.order_id, "The generated purchase line should be in the generated purchase order")
146 self.assertEqual(purchase_order.state, 'draft', "Generated purchase should be in draft state")
147 self.assertEqual(purchase_line.price_unit, self.service_purchase_1.seller_ids.price, "Purchase line price is the one from the supplier")
148 self.assertEqual(purchase_line.product_qty, self.sol1_service_purchase_1.product_uom_qty, "Quantity on SO line is not the same on the purchase line (same UoM)")
149
150 self.sale_order_1._action_cancel()
151
152 self.assertEqual(len(purchase_order.activity_ids), 1, "One activity should be scheduled on the PO since a SO has been cancelled")
153
154 purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.service_purchase_1.seller_ids.partner_id.id), ('state', '=', 'draft')])
155 purchase_lines = self.env['purchase.order.line'].search([('sale_line_id', 'in', self.sale_order_1.order_line.ids)])
156 purchase_line = purchase_lines[0]
157
158 self.assertEqual(len(purchase_lines), 1, "Always one purchase line even after SO cancellation")
159 self.assertTrue(purchase_order, "Always one purchase order even after SO cancellation")
160 self.assertEqual(len(purchase_order.order_line), 1, "Still one line on PO, even after SO cancellation")
161 self.assertEqual(purchase_order, purchase_lines.order_id, "The generated purchase line should still be in the generated purchase order")
162 self.assertEqual(purchase_order.state, 'draft', "Generated purchase should still be in draft state")
163 self.assertEqual(purchase_line.price_unit, self.service_purchase_1.seller_ids.price, "Purchase line price is still the one from the supplier")
164 self.assertEqual(purchase_line.product_qty, self.sol1_service_purchase_1.product_uom_qty, "Quantity on SO line should still be the same on the purchase line (same UoM)")
165
166 self.sale_order_1.action_draft()
167 self.sale_order_1.action_confirm()
168
169 purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.service_purchase_1.seller_ids.partner_id.id), ('state', '=', 'draft')])
170 purchase_lines = self.env['purchase.order.line'].search([('sale_line_id', 'in', self.sale_order_1.order_line.ids)])
171 purchase_line = purchase_lines[0]
172
173 self.assertEqual(len(purchase_lines), 1, "Still only one purchase line should be created even after SO reconfirmation")
174 self.assertEqual(len(purchase_order), 1, "Still one purchase order should be after SO reconfirmation")
175 self.assertEqual(len(purchase_order.order_line), 1, "Only one line on PO, even after SO reconfirmation")
176 self.assertEqual(purchase_order, purchase_lines.order_id, "The generated purchase line should be in the generated purchase order")
177 self.assertEqual(purchase_order.state, 'draft', "Generated purchase should be in draft state")
178 self.assertEqual(purchase_line.price_unit, self.service_purchase_1.seller_ids.price, "Purchase line price is the one from the supplier")
179 self.assertEqual(purchase_line.product_qty, self.sol1_service_purchase_1.product_uom_qty, "Quantity on SO line is not the same on the purchase line (same UoM)")
180
181 def test_update_ordered_sale_quantity(self):
182 """ Test the purchase order behovior when changing the ordered quantity on the sale order line.
183 Increase of qty on the SO
184 - If PO is draft ['draft', 'sent', 'to approve'] : increase the quantity on the PO
185 - If PO is confirmed ['purchase', 'cancel'] : create a new PO
186
187 Decrease of qty on the SO
188 - If PO is draft ['draft', 'sent', 'to approve'] : next activity on the PO
189 - If PO is confirmed ['purchase', 'cancel'] : next activity on the PO
190 """
191 self.sale_order_1.action_confirm()
192
193 purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.service_purchase_1.seller_ids.partner_id.id), ('state', '=', 'draft')])
194 purchase_lines = self.env['purchase.order.line'].search([('sale_line_id', 'in', self.sale_order_1.order_line.ids)])
195 purchase_line = purchase_lines[0]
196
197 self.assertEqual(purchase_order.state, 'draft', "The created purchase should be in draft state")
198 self.assertFalse(purchase_order.activity_ids, "There is no activities on the PO")
199 self.assertEqual(purchase_line.product_qty, self.sol1_service_purchase_1.product_uom_qty, "Quantity on SO line is not the same on the purchase line (same UoM)")
200
201 # increase the ordered quantity on sale line
202 self.sol1_service_purchase_1.write({'product_uom_qty': self.sol1_service_purchase_1.product_uom_qty + 12}) # product_uom_qty = 16
203 self.assertEqual(purchase_line.product_qty, self.sol1_service_purchase_1.product_uom_qty, "The quantity of draft PO line should be increased as the one from the sale line changed")
204
205 sale_line_old_quantity = self.sol1_service_purchase_1.product_uom_qty
206
207 # decrease the ordered quantity on sale line
208 self.sol1_service_purchase_1.write({'product_uom_qty': self.sol1_service_purchase_1.product_uom_qty - 3}) # product_uom_qty = 13
209 self.assertEqual(len(purchase_order.activity_ids), 1, "One activity should have been created on the PO")
210 self.assertEqual(purchase_order.activity_ids.user_id, purchase_order.user_id, "Activity assigned to PO responsible")
211 self.assertEqual(purchase_order.activity_ids.state, 'today', "Activity is for today, as it is urgent")
212
213 # confirm the PO
214 purchase_order.button_confirm()
215
216 # decrease the ordered quantity on sale line
217 self.sol1_service_purchase_1.write({'product_uom_qty': self.sol1_service_purchase_1.product_uom_qty - 5}) # product_uom_qty = 8
218
219 self.env.invalidate_all() # Note: creating a second activity will not refresh the cache
220
221 self.assertEqual(purchase_line.product_qty, sale_line_old_quantity, "The quantity on the PO line should not have changed.")
222 self.assertEqual(len(purchase_order.activity_ids), 2, "a second activity should have been created on the PO")
223 self.assertEqual(purchase_order.activity_ids.mapped('user_id'), purchase_order.user_id, "Activities assigned to PO responsible")
224 self.assertEqual(purchase_order.activity_ids.mapped('state'), ['today', 'today'], "Activities are for today, as it is urgent")
225
226 # increase the ordered quantity on sale line
227 delta = 8
228 self.sol1_service_purchase_1.write({'product_uom_qty': self.sol1_service_purchase_1.product_uom_qty + delta}) # product_uom_qty = 16
229
230 self.assertEqual(purchase_line.product_qty, sale_line_old_quantity, "The quantity on the PO line should not have changed.")
231 self.assertEqual(len(purchase_order.activity_ids), 2, "Always 2 activity on confirmed the PO")
232
233 purchase_order2 = self.env['purchase.order'].search([('partner_id', '=', self.service_purchase_1.seller_ids.partner_id.id), ('state', '=', 'draft')])
234 purchase_lines = self.env['purchase.order.line'].search([('sale_line_id', 'in', self.sale_order_1.order_line.ids)])
235 purchase_lines2 = purchase_lines.filtered(lambda pol: pol.order_id == purchase_order2)
236 purchase_line2 = purchase_lines2[0]
237
238 self.assertTrue(purchase_order2, "A second PO is created by increasing sale quantity when first PO is confirmed")
239 self.assertEqual(purchase_order2.state, 'draft', "The second PO is in draft state")
240 self.assertNotEqual(purchase_order, purchase_order2, "The 2 PO are different")
241 self.assertEqual(len(purchase_lines), 2, "The same Sale Line has created 2 purchase lines")
242 self.assertEqual(len(purchase_order2.order_line), 1, "The 2nd PO has only one line")
243 self.assertEqual(purchase_line2.sale_line_id, self.sol1_service_purchase_1, "The 2nd PO line came from the SO line sol1_service_purchase_1")
244 self.assertEqual(purchase_line2.product_qty, delta, "The quantity of the new PO line is the quantity added on the Sale Line, after first PO confirmation")
245
246 def test_pol_description(self):
247 """
248 test cases when product names are different from how the vendor refers to, which is allowed
249 """
250 service = self.env['product.product'].create({
251 'name': 'Super Product',
252 'type': 'service',
253 'service_to_purchase': True,
254 'seller_ids': [(0, 0, {
255 'partner_id': self.partner_vendor_service.id,
256 'min_qty': 1,
257 'price': 10,
258 'product_code': 'C01',
259 'product_name': 'Name01',
260 'sequence': 1,
261 })]
262 })
263
264 so = self.env['sale.order'].create({
265 'partner_id': self.partner_a.id,
266 'order_line': [
267 (0, 0, {
268 'name': service.name,
269 'product_id': service.id,
270 'product_uom_qty': 1,
271 })
272 ],
273 })
274 so.action_confirm()
275
276 po = self.env['purchase.order'].search([('partner_id', '=', self.partner_vendor_service.id)], order='id desc', limit=1)
277 self.assertEqual(po.order_line.name, "[C01] Name01")
278
279 def test_pol_custom_attribute(self):
280 """
281 test that custom atributes are passed from the SO the PO for service products
282 """
283 # Setup service product variants
284 product_attribute = self.env['product.attribute'].create({
285 'name': 'product attribute',
286 'display_type': 'radio',
287 'create_variant': 'always'
288 })
289
290 product_attribute_value = self.env['product.attribute.value'].create({
291 'name': 'single product attribute value',
292 'is_custom': True,
293 'attribute_id': product_attribute.id
294 })
295
296 product_attribute_line = self.env['product.template.attribute.line'].create({
297 'attribute_id': product_attribute.id,
298 'product_tmpl_id': self.service_purchase_1.product_tmpl_id.id,
299 'value_ids': [Command.link(product_attribute_value.id)]
300 })
301
302 custom_value = "test"
303
304 # create and confirm SO
305 sale_order = self.env['sale.order'].create({
306 'partner_id': self.partner_a.id,
307 'order_line': [
308 Command.create({
309 'name': self.service_purchase_1.name,
310 'product_id': self.service_purchase_1.id,
311 'product_uom_qty': 1,
312 'product_custom_attribute_value_ids': [
313 Command.create({
314 'custom_product_template_attribute_value_id': product_attribute_line.product_template_value_ids.id,
315 'custom_value': custom_value,
316 })
317 ],
318 })
319 ],
320 })
321 sale_order.action_confirm()
322 pol = sale_order._get_purchase_orders().order_line
323 self.assertEqual(pol.name, f"{self.service_purchase_1.display_name}\n{product_attribute.name}: {product_attribute_value.name}: {custom_value}")
324
325 def test_service_to_purchase_multi_company(self):
326 """Test the service to purchase in a multi-company environment
327
328 The `product.template.service_to_purchase` is a company_dependent field, whose
329 value depends on the company are in, which is not necessarily the order company
330
331 Granted that:
332 - The current company is company_1
333 - The product is configured as a service to be purchased on company_1
334 - The product is NOT configured as a service to be purchased on company_2
335 - We process an order on company_2, while being logged in company_1
336
337 The order must be processed without generating a PO, respecting the product
338 setting for this order's company. We also check that the opposite case holds
339 true as well (i.e. PO is generated when confirming with a company that isn't
340 configured for it, but the SO's company is)
341 """
342 company_1 = self.env.company
343 company_2 = self.company_data_2['company']
344 self.env.user.company_ids += company_2
345 self.assertTrue(self.service_purchase_1.service_to_purchase)
346 self.assertFalse(self.service_purchase_1.with_company(company_2).service_to_purchase)
347 order = self.env['sale.order'].create({
348 'partner_id': self.partner_a.id,
349 'company_id': company_2.id,
350 'order_line': [
351 Command.create({
352 'product_id': self.service_purchase_1.id,
353 'product_uom_qty': 1,
354 })
355 ]
356 })
357 # FIXME: there is some sort of multi-company misconfiguration with the permissions that require a sudo here
358 # for this test to run. Issue doesn't occur when running test locally => probably some other module is messing
359 # with the permissions and/or there's an issue with the subsidiary setup
360 order.sudo().with_company(company_1).action_confirm()
361 self.assertFalse(order.purchase_order_count)
362
363 order2 = self.env['sale.order'].create({
364 'partner_id': self.partner_a.id,
365 'company_id': company_1.id,
366 'order_line': [
367 Command.create({
368 'product_id': self.service_purchase_1.id,
369 'product_uom_qty': 1,
370 })
371 ]
372 })
373
374 # FIXME: same sudo issue as above
375 order2.sudo().with_company(company_2).action_confirm()
376 self.assertTrue(order2.purchase_order_count)
377
378 def test_service_to_purchase_branch_tax_propagation(self):
379 """
380 Ensure that SO/PO of a branch can use root company's taxes
381 """
382 branch = self.env['res.company'].create({
383 'name': "Branch Company",
384 'parent_id': self.env.company.id,
385 })
386 self.env.user.company_id = branch
387 service_product = self.env['product.product'].create({
388 'name': "Branch Out-sourced Service",
389 'standard_price': 200.0,
390 'type': 'service',
391 'invoice_policy': 'delivery',
392 'taxes_id': self.company_data['default_tax_sale'],
393 'supplier_taxes_id': self.company_data['default_tax_purchase'],
394 'service_to_purchase': True,
395 'seller_ids': [Command.create({
396 'partner_id': self.partner_b.id,
397 'min_qty': 1,
398 'price': 100,
399 })],
400 })
401 so = self.env['sale.order'].create({
402 'partner_id': self.partner_a.id,
403 'order_line': [Command.create({
404 'product_id': service_product.id,
405 })],
406 })
407 self.assertEqual(so.order_line.tax_ids, self.company_data['default_tax_sale'])
408 so.action_confirm()
409 self.assertEqual(so.order_line.purchase_line_ids.tax_ids, self.company_data['default_tax_purchase'])