Debian Codegraph @

addons/sale_mrp/tests/test_sale_mrp_anglo_saxon_valuation.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from unittest import skip
4
5 from odoo.fields import Command
6 from odoo.tests import Form, tagged
7
8 from odoo.addons.sale.tests.common import TestSaleCommon
9 from odoo.addons.stock_account.tests.test_anglo_saxon_valuation_reconciliation_common import ValuationReconciliationTestCommon
10
11
12 @tagged('post_install', '-at_install')
13 @skip('Temporary to fast merge new valuation')
14 class TestSaleMRPAngloSaxonValuation(TestSaleCommon, ValuationReconciliationTestCommon):
15
16 @classmethod
17 def setUpClass(cls):
18 super().setUpClass()
19
20 cls.env.user.company_id.anglo_saxon_accounting = True
21
22 @classmethod
23 def _create_product(cls, **create_vals):
24 if create_vals.get('is_storable'):
25 create_vals['categ_id'] = cls.stock_account_product_categ.id
26 return super()._create_product(**create_vals)
27
28 def test_sale_mrp_kit_bom_cogs(self):
29 """Check invoice COGS aml after selling and delivering a product
30 with Kit BoM having another product with Kit BoM as component"""
31 # ----------------------------------------------
32 # BoM of Kit A:
33 # - BoM Type: Kit
34 # - Quantity: 3
35 # - Components:
36 # * 2 x Kit B
37 # * 1 x Component A (Cost: $3, Storable)
38 #
39 # BoM of Kit B:
40 # - BoM Type: Kit
41 # - Quantity: 10
42 # - Components:
43 # * 2 x Component B (Cost: $4, Storable)
44 # * 3 x Component BB (Cost: $5, Consumable)
45 # ----------------------------------------------
46
47 self.component_a = self._create_product(name='Component A', is_storable=True, standard_price=3.00)
48 self.component_b = self._create_product(name='Component B', is_storable=True, standard_price=4.00)
49 self.component_bb = self._create_product(name='Component BB', is_storable=False, standard_price=5.00)
50 self.kit_a = self._create_product(name='Kit A', is_storable=True, standard_price=0.00)
51 self.kit_b = self._create_product(name='Kit B', is_storable=False, standard_price=0.00)
52 pack_2 = self.env['uom.uom'].create({'name': 'Pack of 2', 'relative_factor': 2, 'relative_uom_id': self.kit_a.uom_id.id})
53
54 self.kit_a.write({
55 'property_account_expense_id': self.company_data['default_account_expense'].id,
56 'property_account_income_id': self.company_data['default_account_revenue'].id,
57 })
58
59 # Create BoM for Kit A
60 bom_product_form = Form(self.env['mrp.bom'])
61 bom_product_form.product_tmpl_id = self.kit_a.product_tmpl_id
62 bom_product_form.product_qty = 3.0
63 bom_product_form.type = 'phantom'
64 with bom_product_form.bom_line_ids.new() as bom_line:
65 bom_line.product_id = self.kit_b
66 bom_line.product_qty = 2.0
67 with bom_product_form.bom_line_ids.new() as bom_line:
68 bom_line.product_id = self.component_a
69 bom_line.product_qty = 1.0
70 self.bom_a = bom_product_form.save()
71
72 # Create BoM for Kit B
73 bom_product_form = Form(self.env['mrp.bom'])
74 bom_product_form.product_tmpl_id = self.kit_b.product_tmpl_id
75 bom_product_form.product_qty = 10.0
76 bom_product_form.type = 'phantom'
77 with bom_product_form.bom_line_ids.new() as bom_line:
78 bom_line.product_id = self.component_b
79 bom_line.product_qty = 2.0
80 with bom_product_form.bom_line_ids.new() as bom_line:
81 bom_line.product_id = self.component_bb
82 bom_line.product_qty = 3.0
83 self.bom_b = bom_product_form.save()
84
85 so = self.env['sale.order'].create({
86 'partner_id': self.partner_a.id,
87 'order_line': [
88 (0, 0, {
89 'name': self.kit_a.name,
90 'product_id': self.kit_a.id,
91 'product_uom_qty': 0.5,
92 'product_uom_id': pack_2.id,
93 'price_unit': 1,
94 'tax_ids': False,
95 })],
96 })
97 so.action_confirm()
98 so.picking_ids.move_ids.write({'quantity': 1, 'picked': True})
99 so.picking_ids.button_validate()
100
101 invoice = so.with_context(default_journal_id=self.company_data['default_journal_sale'].id)._create_invoices()
102 invoice.action_post()
103
104 # Check the resulting accounting entries
105 amls = invoice.line_ids
106 self.assertEqual(len(amls), 4)
107 stock_out_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_stock_out'])
108 self.assertEqual(stock_out_aml.debit, 0)
109 self.assertAlmostEqual(stock_out_aml.credit, 1.53, msg="Should not include the value of consumable component")
110 cogs_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_expense'])
111 self.assertAlmostEqual(cogs_aml.debit, 1.53, msg="Should not include the value of consumable component")
112 self.assertEqual(cogs_aml.credit, 0)
113
114 def test_sale_mrp_anglo_saxon_variant(self):
115 """Test the price unit of kit with variants"""
116 # Check that the correct bom are selected when computing price_unit for COGS
117
118 self.env.company.currency_id = self.env.ref('base.USD')
119
120 # Create variant attributes
121 self.prod_att_1 = self.env['product.attribute'].create({'name': 'Color'})
122 self.prod_attr1_v1 = self.env['product.attribute.value'].create({'name': 'red', 'attribute_id': self.prod_att_1.id, 'sequence': 1})
123 self.prod_attr1_v2 = self.env['product.attribute.value'].create({'name': 'blue', 'attribute_id': self.prod_att_1.id, 'sequence': 2})
124
125 # Create Product template with variants
126 self.product_template = self.env['product.template'].create({
127 'name': 'Product Template',
128 'is_storable': True,
129 'uom_id': self.uom_unit.id,
130 'invoice_policy': 'delivery',
131 'categ_id': self.stock_account_product_categ.id,
132 'attribute_line_ids': [(0, 0, {
133 'attribute_id': self.prod_att_1.id,
134 'value_ids': [(6, 0, [self.prod_attr1_v1.id, self.prod_attr1_v2.id])]
135 })]
136 })
137
138 # Get product variant
139 self.pt_attr1_v1 = self.product_template.attribute_line_ids[0].product_template_value_ids[0]
140 self.pt_attr1_v2 = self.product_template.attribute_line_ids[0].product_template_value_ids[1]
141 self.variant_1 = self.product_template._get_variant_for_combination(self.pt_attr1_v1)
142 self.variant_2 = self.product_template._get_variant_for_combination(self.pt_attr1_v2)
143
144 def create_simple_bom_for_product(product, name, price):
145 component = self.env['product.product'].create({
146 'name': 'Component ' + name,
147 'is_storable': True,
148 'uom_id': self.uom_unit.id,
149 'categ_id': self.stock_account_product_categ.id,
150 'standard_price': price
151 })
152 self.env['stock.quant'].sudo().create({
153 'product_id': component.id,
154 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
155 'quantity': 10.0,
156 })
157 bom = self.env['mrp.bom'].create({
158 'product_tmpl_id': self.product_template.id,
159 'product_id': product.id,
160 'product_qty': 1.0,
161 'type': 'phantom'
162 })
163 self.env['mrp.bom.line'].create({
164 'product_id': component.id,
165 'product_qty': 1.0,
166 'bom_id': bom.id
167 })
168
169 create_simple_bom_for_product(self.variant_1, "V1", 20)
170 create_simple_bom_for_product(self.variant_2, "V2", 10)
171
172 def create_post_sale_order(product):
173 so_vals = {
174 'partner_id': self.partner_a.id,
175 'partner_invoice_id': self.partner_a.id,
176 'partner_shipping_id': self.partner_a.id,
177 'order_line': [(0, 0, {
178 'name': product.name,
179 'product_id': product.id,
180 'product_uom_qty': 2,
181 'price_unit': product.list_price
182 })],
183 'company_id': self.company_data['company'].id,
184 }
185 so = self.env['sale.order'].create(so_vals)
186 # Validate the SO
187 so.action_confirm()
188 # Deliver the three finished products
189 pick = so.picking_ids
190 # To check the products on the picking
191 pick.button_validate()
192 # Create the invoice
193 so._create_invoices()
194 invoice = so.invoice_ids
195 invoice.action_post()
196 return invoice
197
198 # Create a SO for variant 1
199 self.invoice_1 = create_post_sale_order(self.variant_1)
200 self.invoice_2 = create_post_sale_order(self.variant_2)
201
202 def check_cogs_entry_values(invoice, expected_value):
203 aml = invoice.line_ids
204 aml_expense = aml.filtered(lambda l: l.display_type == 'cogs' and l.debit > 0)
205 aml_output = aml.filtered(lambda l: l.display_type == 'cogs' and l.credit > 0)
206 self.assertEqual(aml_expense.debit, expected_value, "Cost of Good Sold entry missing or mismatching for variant")
207 self.assertEqual(aml_output.credit, expected_value, "Cost of Good Sold entry missing or mismatching for variant")
208
209 # Check that the cost of Good Sold entries for variant 1 are equal to 2 * 20 = 40
210 check_cogs_entry_values(self.invoice_1, 40)
211 # Check that the cost of Good Sold entries for variant 2 are equal to 2 * 10 = 20
212 check_cogs_entry_values(self.invoice_2, 20)
213
214 def test_anglo_saxo_return_and_credit_note(self):
215 """
216 When posting a credit note for a returned kit, the value of the anglo-saxo lines
217 should be based on the returned component's value
218 """
219 self.stock_account_product_categ.property_cost_method = 'fifo'
220
221 kit = self._create_product(name='Simple Kit', is_storable=True, standard_price=0)
222 component = self._create_product(name='Compo A', is_storable=True, standard_price=0)
223 kit.property_account_expense_id = self.company_data['default_account_expense']
224
225 self.env['mrp.bom'].create({
226 'product_tmpl_id': kit.product_tmpl_id.id,
227 'product_qty': 1.0,
228 'type': 'phantom',
229 'bom_line_ids': [(0, 0, {'product_id': component.id, 'product_qty': 1.0})]
230 })
231
232 # Receive 3 components: one @10, one @20 and one @60
233 in_moves = self.env['stock.move'].create([{
234 'product_id': component.id,
235 'location_id': self.env.ref('stock.stock_location_suppliers').id,
236 'location_dest_id': self.company_data['default_warehouse'].lot_stock_id.id,
237 'product_uom': component.uom_id.id,
238 'product_uom_qty': 1,
239 'price_unit': p,
240 } for p in [10, 20, 60]])
241 in_moves._action_confirm()
242 in_moves.write({'quantity': 1, 'picked': True})
243 in_moves._action_done()
244
245 # Sell 3 kits
246 so = self.env['sale.order'].create({
247 'partner_id': self.env['res.partner'].create({'name': 'Test Partner'}).id,
248 'order_line': [
249 (0, 0, {
250 'name': kit.name,
251 'product_id': kit.id,
252 'product_uom_qty': 3.0,
253 'price_unit': 100,
254 'tax_ids': False,
255 })],
256 })
257 so.action_confirm()
258
259 # Deliver the components: 1@10, then 1@20 and then 1@60
260 pickings = []
261 picking = so.picking_ids
262 while picking:
263 pickings.append(picking)
264 picking.move_ids.write({'quantity': 1, 'picked': True})
265 action = picking.button_validate()
266 if isinstance(action, dict):
267 Form.from_action(self.env, action).save().process()
268 picking = picking.backorder_ids
269
270 invoice = so._create_invoices()
271 invoice.action_post()
272
273 # Receive one @100
274 in_moves = self.env['stock.move'].create({
275 'product_id': component.id,
276 'location_id': self.env.ref('stock.stock_location_suppliers').id,
277 'location_dest_id': self.company_data['default_warehouse'].lot_stock_id.id,
278 'product_uom': component.uom_id.id,
279 'product_uom_qty': 1,
280 'price_unit': 100,
281 })
282 in_moves._action_confirm()
283 in_moves.write({'quantity': 1, 'picked': True})
284 in_moves._action_done()
285
286 # Return the second picking (i.e. one component @20)
287 ctx = {'active_id': pickings[1].id, 'active_model': 'stock.picking'}
288 return_wizard = Form(self.env['stock.return.picking'].with_context(ctx)).save()
289 return_wizard.product_return_moves.quantity = 1
290 return_picking = return_wizard._create_return()
291 return_picking.move_ids.write({'quantity': 1, 'picked': True})
292 return_picking.button_validate()
293
294 # Add a credit note for the returned kit
295 ctx = {'active_model': 'account.move', 'active_ids': invoice.ids}
296 refund_wizard = self.env['account.move.reversal'].with_context(ctx).create({
297 'journal_id': invoice.journal_id.id,
298 })
299 action = refund_wizard.refund_moves()
300 reverse_invoice = self.env['account.move'].browse(action['res_id'])
301 with Form(reverse_invoice) as reverse_invoice_form:
302 with reverse_invoice_form.invoice_line_ids.edit(0) as line:
303 line.quantity = 1
304 reverse_invoice.action_post()
305
306 amls = reverse_invoice.line_ids
307 stock_out_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_stock_out'])
308 self.assertEqual(stock_out_aml.debit, 20, 'Should be to the value of the returned component')
309 self.assertEqual(stock_out_aml.credit, 0)
310 cogs_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_expense'])
311 self.assertEqual(cogs_aml.debit, 0)
312 self.assertEqual(cogs_aml.credit, 20, 'Should be to the value of the returned component')
313
314 def test_anglo_saxo_return_and_create_invoice(self):
315 """
316 When creating an invoice for a returned kit, the value of the anglo-saxo lines
317 should be based on the returned component's value
318 """
319 self.stock_account_product_categ.property_cost_method = 'fifo'
320
321 kit = self._create_product(name='Simple Kit', is_storable=True, standard_price=0)
322 component = self._create_product(name='Compo A', is_storable=True, standard_price=0)
323 (kit + component).invoice_policy = 'delivery'
324 kit.property_account_expense_id = self.company_data['default_account_expense']
325
326 self.env['mrp.bom'].create({
327 'product_tmpl_id': kit.product_tmpl_id.id,
328 'product_qty': 1.0,
329 'type': 'phantom',
330 'bom_line_ids': [(0, 0, {'product_id': component.id, 'product_qty': 1.0})]
331 })
332
333 # Receive 3 components: one @10, one @20 and one @60
334 in_moves = self.env['stock.move'].create([{
335 'product_id': component.id,
336 'location_id': self.env.ref('stock.stock_location_suppliers').id,
337 'location_dest_id': self.company_data['default_warehouse'].lot_stock_id.id,
338 'product_uom': component.uom_id.id,
339 'product_uom_qty': 1,
340 'price_unit': p,
341 } for p in [10, 20, 60]])
342 in_moves._action_confirm()
343 in_moves.write({'quantity': 1, 'picked': True})
344 in_moves._action_done()
345
346 # Sell 3 kits
347 so = self.env['sale.order'].create({
348 'partner_id': self.env['res.partner'].create({'name': 'Test Partner'}).id,
349 'order_line': [
350 (0, 0, {
351 'name': kit.name,
352 'product_id': kit.id,
353 'product_uom_qty': 3.0,
354 'price_unit': 100,
355 'tax_ids': False,
356 })],
357 })
358 so.action_confirm()
359
360 # Deliver the components: 1@10, then 1@20 and then 1@60
361 pickings = []
362 picking = so.picking_ids
363 while picking:
364 pickings.append(picking)
365 picking.move_ids.write({'quantity': 1, 'picked': True})
366 action = picking.button_validate()
367 if isinstance(action, dict):
368 Form.from_action(self.env, action).save().process()
369 picking = picking.backorder_ids
370
371 invoice = so._create_invoices()
372 invoice.action_post()
373
374 # Receive one @100
375 in_moves = self.env['stock.move'].create({
376 'product_id': component.id,
377 'location_id': self.env.ref('stock.stock_location_suppliers').id,
378 'location_dest_id': self.company_data['default_warehouse'].lot_stock_id.id,
379 'product_uom': component.uom_id.id,
380 'product_uom_qty': 1,
381 'price_unit': 100,
382 })
383 in_moves._action_confirm()
384 in_moves.write({'quantity': 1, 'picked': True})
385 in_moves._action_done()
386
387 # Return the second picking (i.e. one component @20)
388 ctx = {'active_id': pickings[1].id, 'active_model': 'stock.picking'}
389 return_wizard = Form(self.env['stock.return.picking'].with_context(ctx)).save()
390 return_wizard.product_return_moves.quantity = 1
391 return_picking = return_wizard._create_return()
392 return_picking.move_ids.write({'quantity': 1, 'picked': True})
393 return_picking.button_validate()
394
395 # Create a new invoice for the returned kit
396 ctx = {'active_model': 'sale.order', 'active_ids': so.ids}
397 create_invoice_wizard = self.env['sale.advance.payment.inv'].with_context(ctx).create(
398 {'advance_payment_method': 'delivered'})
399 create_invoice_wizard.create_invoices()
400 reverse_invoice = so.invoice_ids[-1]
401 with Form(reverse_invoice) as reverse_invoice_form:
402 with reverse_invoice_form.invoice_line_ids.edit(0) as line:
403 line.quantity = 1
404 reverse_invoice.action_post()
405
406 amls = reverse_invoice.line_ids
407 stock_out_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_stock_out'])
408 self.assertEqual(stock_out_aml.debit, 20, 'Should be to the value of the returned component')
409 self.assertEqual(stock_out_aml.credit, 0)
410 cogs_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_expense'])
411 self.assertEqual(cogs_aml.debit, 0)
412 self.assertEqual(cogs_aml.credit, 20, 'Should be to the value of the returned component')
413
414 def test_kit_avco_fully_owned_and_delivered_invoice_post_delivery(self):
415 self.stock_account_product_categ.property_cost_method = 'average'
416
417 compo01 = self._create_product(name='Compo 01', is_storable=True, standard_price=10)
418 compo02 = self._create_product(name='Compo 02', is_storable=True, standard_price=20)
419 kit = self._create_product(name='Kit', is_storable=True, standard_price=0)
420
421 (compo01 + compo02 + kit).invoice_policy = 'delivery'
422
423 self.env['stock.quant']._update_available_quantity(compo01, self.company_data['default_warehouse'].lot_stock_id, 1, owner_id=self.partner_b)
424 self.env['stock.quant']._update_available_quantity(compo02, self.company_data['default_warehouse'].lot_stock_id, 1, owner_id=self.partner_b)
425
426 self.env['mrp.bom'].create({
427 'product_id': kit.id,
428 'product_tmpl_id': kit.product_tmpl_id.id,
429 'product_uom_id': kit.uom_id.id,
430 'product_qty': 1.0,
431 'type': 'phantom',
432 'bom_line_ids': [
433 (0, 0, {'product_id': compo01.id, 'product_qty': 1.0}),
434 (0, 0, {'product_id': compo02.id, 'product_qty': 1.0}),
435 ],
436 })
437
438 so = self.env['sale.order'].create({
439 'partner_id': self.partner_a.id,
440 'order_line': [
441 (0, 0, {
442 'name': kit.name,
443 'product_id': kit.id,
444 'product_uom_qty': 1.0,
445 'price_unit': 5,
446 'tax_ids': False,
447 })],
448 })
449 so.action_confirm()
450 so.picking_ids.move_ids.write({'quantity': 1, 'picked': True})
451 so.picking_ids.button_validate()
452
453 invoice = so._create_invoices()
454 invoice.action_post()
455
456 # COGS should not exist because the products are owned by an external partner
457 amls = invoice.line_ids
458 self.assertRecordValues(amls, [
459 # pylint: disable=bad-whitespace
460 {'account_id': self.company_data['default_account_revenue'].id, 'debit': 0, 'credit': 5},
461 {'account_id': self.company_data['default_account_receivable'].id, 'debit': 5, 'credit': 0},
462 ])
463
464 def test_kit_avco_partially_owned_and_delivered_invoice_post_delivery(self):
465 self.stock_account_product_categ.property_cost_method = 'average'
466
467 compo01 = self._create_product(name='Compo 01', is_storable=True, standard_price=10)
468 compo02 = self._create_product(name='Compo 02', is_storable=True, standard_price=20)
469 kit = self._create_product(name='Kit', is_storable=True, standard_price=0)
470
471 (compo01 + compo02 + kit).invoice_policy = 'delivery'
472
473 self.env['stock.quant']._update_available_quantity(compo01, self.company_data['default_warehouse'].lot_stock_id, 1, owner_id=self.partner_b)
474 self.env['stock.quant']._update_available_quantity(compo01, self.company_data['default_warehouse'].lot_stock_id, 1)
475 self.env['stock.quant']._update_available_quantity(compo02, self.company_data['default_warehouse'].lot_stock_id, 1, owner_id=self.partner_b)
476 self.env['stock.quant']._update_available_quantity(compo02, self.company_data['default_warehouse'].lot_stock_id, 1)
477
478 self.env['mrp.bom'].create({
479 'product_id': kit.id,
480 'product_tmpl_id': kit.product_tmpl_id.id,
481 'product_uom_id': kit.uom_id.id,
482 'product_qty': 1.0,
483 'type': 'phantom',
484 'bom_line_ids': [
485 (0, 0, {'product_id': compo01.id, 'product_qty': 1.0}),
486 (0, 0, {'product_id': compo02.id, 'product_qty': 1.0}),
487 ],
488 })
489
490 so = self.env['sale.order'].create({
491 'partner_id': self.partner_a.id,
492 'order_line': [
493 (0, 0, {
494 'name': kit.name,
495 'product_id': kit.id,
496 'product_uom_qty': 2.0,
497 'price_unit': 5,
498 'tax_ids': False,
499 })],
500 })
501 so.action_confirm()
502 so.picking_ids.move_line_ids.quantity = 1
503 so.picking_ids.move_ids.picked = True
504 so.picking_ids.button_validate()
505
506 invoice = so._create_invoices()
507 invoice.action_post()
508
509 # COGS should not exist because the products are owned by an external partner
510 amls = invoice.line_ids
511 self.assertRecordValues(amls, [
512 # pylint: disable=bad-whitespace
513 {'account_id': self.company_data['default_account_revenue'].id, 'debit': 0, 'credit': 10},
514 {'account_id': self.company_data['default_account_receivable'].id, 'debit': 10, 'credit': 0},
515 {'account_id': self.company_data['default_account_stock_out'].id, 'debit': 0, 'credit': 30},
516 {'account_id': self.company_data['default_account_expense'].id, 'debit': 30, 'credit': 0},
517 ])
518
519 def test_anglo_saxo_kit_subkits(self):
520 """Check invoice COGS aml after selling and delivering a product
521 with Kit BoM producing 2 times the product and having
522 2 products with Kit BoM as components"""
523
524 # ----------------------------------------------
525 # BoM of Main kit:
526 # - BoM Type: Kit
527 # - Quantity: 4
528 # - Components:
529 # * 1 x Subkit A
530 # * 1 x Subkit B
531 #
532 # BoM of Subkit A:
533 # - BoM Type: Kit
534 # - Quantity: 1
535 # - Components:
536 # * 2 x Component A (Cost: $10, Storable)
537 #
538 # BoM of Subkit B:
539 # - BoM Type: Kit
540 # - Quantity: 1
541 # - Components:
542 # * 2 x Component B (Cost: $6, Storable)
543 # ----------------------------------------------
544
545 component_a = self._create_product(name='Component A', is_storable=True, standard_price=10.00)
546 component_b = self._create_product(name='Component B', is_storable=True, standard_price=6.00)
547 subkit_a = self._create_product(name='Subkit A', is_storable=True, standard_price=0.00)
548 subkit_b = self._create_product(name='Subkit B', is_storable=True, standard_price=0.00)
549 main_kit = self._create_product(name='Main kit', is_storable=True, standard_price=0.00)
550
551 main_kit.write({
552 'property_account_expense_id': self.company_data['default_account_expense'].id,
553 'property_account_income_id': self.company_data['default_account_revenue'].id,
554 })
555
556 # Create BoM for Main kit
557 self.env['mrp.bom'].create({
558 'product_id': main_kit.id,
559 'product_tmpl_id': main_kit.product_tmpl_id.id,
560 'product_qty': 4.0,
561 'type': 'phantom',
562 'bom_line_ids': [
563 (0, 0, {'product_id': subkit_a.id, 'product_qty': 1.0}),
564 (0, 0, {'product_id': subkit_b.id, 'product_qty': 1.0}),
565 ],
566 })
567 # Create BoM for Subkit A
568 self.env['mrp.bom'].create({
569 'product_id': subkit_a.id,
570 'product_tmpl_id': subkit_a.product_tmpl_id.id,
571 'product_qty': 1.0,
572 'type': 'phantom',
573 'bom_line_ids': [
574 (0, 0, {'product_id': component_a.id, 'product_qty': 2.0}),
575 ],
576 })
577 # Create BoM for Subkit B
578 self.env['mrp.bom'].create({
579 'product_id': subkit_b.id,
580 'product_tmpl_id': subkit_b.product_tmpl_id.id,
581 'product_qty': 1.0,
582 'type': 'phantom',
583 'bom_line_ids': [
584 (0, 0, {'product_id': component_b.id, 'product_qty': 2.0}),
585 ],
586 })
587
588 so = self.env['sale.order'].create({
589 'partner_id': self.partner_a.id,
590 'order_line': [
591 (0, 0, {
592 'name': main_kit.name,
593 'product_id': main_kit.id,
594 'product_uom_qty': 1.0,
595 'price_unit': 1,
596 'tax_ids': False,
597 })],
598 })
599 so.action_confirm()
600 for move in so.picking_ids.move_ids:
601 move.quantity = move.product_uom_qty
602 so.picking_ids.button_validate()
603
604 invoice = so.with_context(default_journal_id=self.company_data['default_journal_sale'].id)._create_invoices()
605 invoice.action_post()
606
607 # Check the resulting accounting entries
608 amls = invoice.line_ids
609 self.assertEqual(len(amls), 4)
610 stock_out_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_stock_out'])
611 self.assertEqual(stock_out_aml.debit, 0)
612 self.assertAlmostEqual(stock_out_aml.credit, 8.00, msg="Should include include the components from all subkits, with the price adapted for 1 Main kit")
613 cogs_aml = amls.filtered(lambda aml: aml.account_id == self.company_data['default_account_expense'])
614 self.assertAlmostEqual(cogs_aml.debit, 8.00, msg="Should include include the components from all subkits, with the price adapted for 1 Main kit")
615 self.assertEqual(cogs_aml.credit, 0)
616
617 def test_sell_kit_invoice_before_delivery(self):
618 """ When a kit product is invoiced prior to delivery, we want to make sure to reconcile all
619 the AMLs from its explosion together, else we risk re-reconciliation attempts (which will
620 block certain actions from being performed altogether).
621 """
622 self.stock_account_product_categ.property_cost_method = 'average'
623
624 compo01 = self._create_product(name="Compo 01", is_storable=True, standard_price=10)
625 compo02 = self._create_product(name="Compo 02", is_storable=True, standard_price=20)
626 kit = self._create_product(name="Kit", is_storable=True, standard_price=30)
627 (compo01 + compo02 + kit).write({'invoice_policy': 'order'})
628 warehouse = self.company_data['default_warehouse']
629 self.env['stock.quant']._update_available_quantity(compo01, warehouse.lot_stock_id, 1.0)
630 self.env['stock.quant']._update_available_quantity(compo02, warehouse.lot_stock_id, 2.0)
631 self.env['mrp.bom'].create({
632 'type': 'phantom',
633 'product_id': kit.id,
634 'product_tmpl_id': kit.product_tmpl_id.id,
635 'product_qty': 1,
636 'bom_line_ids': [
637 Command.create({'product_id': compo01.id, 'product_qty': 1}),
638 Command.create({'product_id': compo02.id, 'product_qty': 1}),
639 ],
640 })
641 sale_order = self.env['sale.order'].create({
642 'partner_id': self.partner_a.id,
643 'order_line': [
644 Command.create({
645 'product_id': kit.id,
646 'product_uom_qty': 1,
647 'price_unit': 10,
648 }),
649 Command.create({
650 'product_id': compo02.id,
651 'product_uom_qty': 1,
652 'price_unit': 5,
653 }),
654 ],
655 })
656 sale_order.action_confirm()
657 invoice = sale_order.with_context(default_journal_id=self.company_data['default_journal_sale'].id)._create_invoices()
658 invoice.action_post()
659 delivery = sale_order.picking_ids
660 # would fail due to attempted re-reconciliation prior to this commit
661 delivery.button_validate()
662 stock_output_amls = self.env['account.move.line'].search([('account_id', '=', self.company_data['default_account_stock_out'].id)], order='id asc')
663 self.assertRecordValues(stock_output_amls,
664 [
665 {'product_id': kit.id, 'reconciled': True, 'debit': 0.0, 'credit': 30.0},
666 {'product_id': compo02.id, 'reconciled': True, 'debit': 0.0, 'credit': 20.0},
667 {'product_id': compo01.id, 'reconciled': True, 'debit': 10.0, 'credit': 0.0},
668 {'product_id': compo02.id, 'reconciled': True, 'debit': 20.0, 'credit': 0.0},
669 {'product_id': compo02.id, 'reconciled': True, 'debit': 20.0, 'credit': 0.0},
670 ]
671 )