Debian Codegraph @

addons/stock_landed_costs/tests/test_stockvaluationlayer.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 """ Implementation of "INVENTORY VALUATION TESTS (With valuation layers)" spreadsheet. """
4
5 from unittest import skip
6
7 from odoo import fields
8 from odoo.tests import Form, tagged
9 from odoo.addons.stock_landed_costs.tests.common import TestStockLandedCostsCommon
10 from freezegun import freeze_time
11 import time
12
13
14 @skip('Temporary to fast merge new valuation')
15 class TestStockValuationLCCommon(TestStockLandedCostsCommon):
16
17 @classmethod
18 def setUpClass(cls):
19 super().setUpClass()
20
21 cls.product1 = cls.env['product.product'].create({
22 'name': 'product1',
23 'is_storable': True,
24 'categ_id': cls.stock_account_product_categ.id,
25 })
26 cls.productlc1 = cls.env['product.product'].create({
27 'name': 'product1',
28 'type': 'service',
29 'categ_id': cls.stock_account_product_categ.id,
30 'landed_cost_ok': True,
31 })
32
33 def setUp(self):
34 super().setUp()
35 self.days = 0
36
37 def _get_stock_input_move_lines(self):
38 return self.env['account.move.line'].search([
39 ('account_id', '=', self.company_data['default_account_stock_in'].id),
40 ], order='id')
41
42 def _get_stock_output_move_lines(self):
43 return self.env['account.move.line'].search([
44 ('account_id', '=', self.company_data['default_account_stock_out'].id),
45 ], order='id')
46
47 def _get_stock_valuation_move_lines(self):
48 return self.env['account.move.line'].search([
49 ('account_id', '=', self.company_data['default_account_stock_valuation'].id),
50 ], order='id')
51
52 def _get_payable_move_lines(self):
53 return self.env['account.move.line'].search([
54 ('account_id', '=', self.company_data['default_account_payable'].id),
55 ], order='id')
56
57 def _get_expense_move_lines(self):
58 return self.env['account.move.line'].search([
59 ('account_id', '=', self.company_data['default_account_expense'].id),
60 ], order='id')
61
62 def _make_lc(self, move, amount):
63 picking = move.picking_id
64 lc = Form(self.env['stock.landed.cost'])
65 lc.account_journal_id = self.stock_journal
66 lc.picking_ids.add(move.picking_id)
67 with lc.cost_lines.new() as cost_line:
68 cost_line.product_id = self.productlc1
69 cost_line.price_unit = amount
70 lc = lc.save()
71 lc.compute_landed_cost()
72 lc.button_validate()
73 return lc
74
75 def _make_in_move(self, product, quantity, unit_cost=None, create_picking=False, product_uom=False):
76 """ Helper to create and validate a receipt move.
77 """
78 unit_cost = unit_cost or product.standard_price
79 in_move = self.env['stock.move'].create({
80 'product_id': product.id,
81 'location_id': self.env.ref('stock.stock_location_suppliers').id,
82 'location_dest_id': self.company_data['default_warehouse'].lot_stock_id.id,
83 'product_uom': product_uom.id if product_uom else self.env.ref('uom.product_uom_unit').id,
84 'product_uom_qty': quantity,
85 'price_unit': unit_cost,
86 'picking_type_id': self.company_data['default_warehouse'].in_type_id.id,
87 })
88
89 if create_picking:
90 picking = self.env['stock.picking'].create({
91 'picking_type_id': in_move.picking_type_id.id,
92 'location_id': in_move.location_id.id,
93 'location_dest_id': in_move.location_dest_id.id,
94 })
95 in_move.write({'picking_id': picking.id})
96
97 in_move._action_confirm()
98 in_move._action_assign()
99 in_move.move_line_ids.quantity = quantity
100 in_move.picked = True
101 in_move._action_done()
102
103 self.days += 1
104 return in_move.with_context(svl=True)
105
106 def _make_out_move(self, product, quantity, force_assign=None, create_picking=False):
107 """ Helper to create and validate a delivery move.
108 """
109 out_move = self.env['stock.move'].create({
110 'product_id': product.id,
111 'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
112 'location_dest_id': self.env.ref('stock.stock_location_customers').id,
113 'product_uom': self.env.ref('uom.product_uom_unit').id,
114 'product_uom_qty': quantity,
115 'picking_type_id': self.company_data['default_warehouse'].out_type_id.id,
116 })
117
118 if create_picking:
119 picking = self.env['stock.picking'].create({
120 'picking_type_id': out_move.picking_type_id.id,
121 'location_id': out_move.location_id.id,
122 'location_dest_id': out_move.location_dest_id.id,
123 })
124 out_move.write({'picking_id': picking.id})
125
126 out_move._action_confirm()
127 out_move._action_assign()
128 if force_assign:
129 self.env['stock.move.line'].create({
130 'move_id': out_move.id,
131 'product_id': out_move.product_id.id,
132 'product_uom_id': out_move.product_uom.id,
133 'location_id': out_move.location_id.id,
134 'location_dest_id': out_move.location_dest_id.id,
135 })
136 out_move.move_line_ids.quantity = quantity
137 out_move.picked = True
138 out_move._action_done()
139
140 self.days += 1
141 return out_move.with_context(svl=True)
142
143
144 @tagged('-at_install', 'post_install')
145 class TestStockValuationLCFIFO(TestStockValuationLCCommon):
146 @classmethod
147 def setUpClass(cls):
148 super().setUpClass()
149 cls.product1.product_tmpl_id.categ_id.property_cost_method = 'fifo'
150 cls.product1.product_tmpl_id.categ_id.property_valuation = 'real_time'
151
152 def test_normal_1(self):
153 move1 = self._make_in_move(self.product1, 10, unit_cost=10, create_picking=True)
154 move2 = self._make_in_move(self.product1, 10, unit_cost=20)
155 lc = self._make_lc(move1, 100)
156 move3 = self._make_out_move(self.product1, 1)
157
158 self.assertEqual(self.product1.value_svl, 380)
159 self.assertEqual(self.product1.quantity_svl, 19)
160 self.assertEqual(self.product1.standard_price, 20)
161
162 def test_negative_1(self):
163 self.product1.standard_price = 10
164 move1 = self._make_out_move(self.product1, 2, force_assign=True)
165 move2 = self._make_in_move(self.product1, 10, unit_cost=15, create_picking=True)
166 lc = self._make_lc(move2, 100)
167
168 self.assertEqual(self.product1.value_svl, 200)
169 self.assertEqual(self.product1.quantity_svl, 8)
170
171 def test_alreadyout_1(self):
172 move1 = self._make_in_move(self.product1, 10, unit_cost=10, create_picking=True)
173 move2 = self._make_out_move(self.product1, 10)
174 lc = self._make_lc(move1, 100)
175
176 self.assertEqual(self.product1.value_svl, 0)
177 self.assertEqual(self.product1.quantity_svl, 0)
178
179 def test_alreadyout_2(self):
180 move1 = self._make_in_move(self.product1, 10, unit_cost=10, create_picking=True)
181 move2 = self._make_in_move(self.product1, 10, unit_cost=20)
182 move2 = self._make_out_move(self.product1, 1)
183 lc = self._make_lc(move1, 100)
184
185 self.assertEqual(self.product1.value_svl, 380)
186 self.assertEqual(self.product1.quantity_svl, 19)
187
188 def test_alreadyout_3(self):
189 move1 = self._make_in_move(self.product1, 10, unit_cost=10, create_picking=True)
190 move2 = self._make_out_move(self.product1, 10)
191 move1.move_line_ids.quantity = 15
192 lc = self._make_lc(move1, 60)
193
194 self.assertEqual(self.product1.value_svl, 70)
195 self.assertEqual(self.product1.quantity_svl, 5)
196
197 def test_fifo_to_standard_1(self):
198 move1 = self._make_in_move(self.product1, 10, unit_cost=10)
199 move2 = self._make_in_move(self.product1, 10, unit_cost=15)
200 move3 = self._make_out_move(self.product1, 5)
201 lc = self._make_lc(move1, 100)
202 self.product1.product_tmpl_id.categ_id.property_cost_method = 'standard'
203 out_svl = self.product1.stock_valuation_layer_ids.sorted()[-2]
204 in_svl = self.product1.stock_valuation_layer_ids.sorted()[-1]
205
206 self.assertEqual(out_svl.value, -250)
207 # 15 * 16.66
208 self.assertAlmostEqual(in_svl.value, 250.0, 2)
209
210 def test_rounding_1(self):
211 """3@100, out 1, out 1, out 1"""
212 move1 = self._make_in_move(self.product1, 3, unit_cost=20, create_picking=True)
213 lc = self._make_lc(move1, 40)
214 move2 = self._make_out_move(self.product1, 1)
215 move3 = self._make_out_move(self.product1, 1)
216 move4 = self._make_out_move(self.product1, 1)
217
218 self.assertEqual(self.product1.stock_valuation_layer_ids.mapped('value'), [60.0, 40.0, -33.33, -33.34, -33.33])
219 self.assertEqual(self.product1.value_svl, 0)
220 self.assertEqual(self.product1.quantity_svl, 0)
221
222 def test_rounding_2(self):
223 """3@98, out 1, out 1, out 1"""
224 move1 = self._make_in_move(self.product1, 3, unit_cost=20, create_picking=True)
225 lc = self._make_lc(move1, 38)
226 move2 = self._make_out_move(self.product1, 1)
227 move3 = self._make_out_move(self.product1, 1)
228 move4 = self._make_out_move(self.product1, 1)
229
230 self.assertEqual(move2.stock_valuation_layer_ids.value, -32.67)
231 self.assertEqual(move3.stock_valuation_layer_ids.value, -32.67)
232 self.assertAlmostEqual(move4.stock_valuation_layer_ids.value, -32.66, delta=0.01) # self.env.company.currency_id.round(-32.66) -> -32.660000000000004
233 self.assertEqual(self.product1.value_svl, 0)
234 self.assertEqual(self.product1.quantity_svl, 0)
235
236 def test_rounding_3(self):
237 """[email protected], out 1, out 1, out 1"""
238 move1 = self._make_in_move(self.product1, 3, unit_cost=1, create_picking=True)
239 lc = self._make_lc(move1, 1.85)
240 move2 = self._make_out_move(self.product1, 1)
241 move3 = self._make_out_move(self.product1, 1)
242 move4 = self._make_out_move(self.product1, 1)
243
244 self.assertEqual(self.product1.stock_valuation_layer_ids.mapped('value'), [3.0, 1.85, -1.62, -1.62, -1.61])
245 self.assertEqual(self.product1.value_svl, 0)
246 self.assertEqual(self.product1.quantity_svl, 0)
247
248 def test_in_and_out_1(self):
249 move1 = self._make_in_move(self.product1, 10, unit_cost=100, create_picking=True)
250 self.assertEqual(move1.stock_valuation_layer_ids[0].remaining_value, 1000)
251 lc1 = self._make_lc(move1, 100)
252 self.assertEqual(move1.stock_valuation_layer_ids[0].remaining_value, 1100)
253 lc2 = self._make_lc(move1, 50)
254 self.assertEqual(move1.stock_valuation_layer_ids[0].remaining_value, 1150)
255 self.assertEqual(self.product1.value_svl, 1150)
256 self.assertEqual(self.product1.quantity_svl, 10)
257 move2 = self._make_out_move(self.product1, 1)
258 self.assertEqual(move2.stock_valuation_layer_ids.value, -115)
259
260 def test_landed_cost_different_uom(self):
261 """
262 Check that the SVL is correctly updated with the landed cost divided by the quantity in the product UOM.
263 """
264 uom_gram = self.env.ref('uom.product_uom_gram')
265 uom_kgm = self.env.ref('uom.product_uom_kgm')
266 # the product uom is in gram but the transfer is in kg
267 self.product1.uom_id = uom_gram
268 move1 = self._make_in_move(self.product1, 1, unit_cost=10, create_picking=True, product_uom=uom_kgm)
269 self.assertEqual(move1.stock_valuation_layer_ids[0].remaining_value, 10000)
270 self.assertEqual(move1.stock_valuation_layer_ids[0].remaining_qty, 1000)
271 self._make_lc(move1, 250)
272 self.assertEqual(move1.stock_valuation_layer_ids[0].remaining_value, 10250)
273
274
275 @tagged('-at_install', 'post_install')
276 class TestStockValuationLCAVCO(TestStockValuationLCCommon):
277 @classmethod
278 def setUpClass(cls):
279 super().setUpClass()
280 cls.product1.product_tmpl_id.categ_id.property_cost_method = 'average'
281 cls.product1.product_tmpl_id.categ_id.property_valuation = 'real_time'
282
283 def test_normal_1(self):
284 move1 = self._make_in_move(self.product1, 10, unit_cost=10, create_picking=True)
285 move2 = self._make_in_move(self.product1, 10, unit_cost=20)
286 lc = self._make_lc(move1, 100)
287 move3 = self._make_out_move(self.product1, 1)
288
289 self.assertEqual(self.product1.value_svl, 380)
290
291 def test_negative_1(self):
292 self.product1.standard_price = 10
293 move1 = self._make_out_move(self.product1, 2, force_assign=True)
294 move2 = self._make_in_move(self.product1, 10, unit_cost=15, create_picking=True)
295 lc = self._make_lc(move2, 100)
296
297 self.assertEqual(self.product1.value_svl, 200)
298 self.assertEqual(self.product1.quantity_svl, 8)
299
300 def test_alreadyout_1(self):
301 move1 = self._make_in_move(self.product1, 10, unit_cost=10, create_picking=True)
302 move2 = self._make_out_move(self.product1, 10)
303 lc = self._make_lc(move1, 100)
304
305 self.assertEqual(len(self.product1.stock_valuation_layer_ids), 2)
306 self.assertEqual(self.product1.value_svl, 0)
307 self.assertEqual(self.product1.quantity_svl, 0)
308
309 def test_alreadyout_2(self):
310 move1 = self._make_in_move(self.product1, 10, unit_cost=10, create_picking=True)
311 move2 = self._make_in_move(self.product1, 10, unit_cost=20)
312 move2 = self._make_out_move(self.product1, 1)
313 lc = self._make_lc(move1, 100)
314
315 self.assertEqual(self.product1.value_svl, 375)
316 self.assertEqual(self.product1.quantity_svl, 19)
317
318 def test_lc_generated_from_bill_multi_comapnies(self):
319 """
320 In a multi-company environment:
321 Confirm PO, receive products, post bill and generate LC
322 """
323 company = self.env.company
324 self.env.user.company_id = self.env['res.company'].create({
325 'name': 'Another Company',
326 })
327
328 po_form = Form(self.env['purchase.order'])
329 po_form.company_id = company
330 po_form.partner_id = self.partner_a
331 with po_form.order_line.new() as po_line:
332 po_line.product_id = self.product1
333 po_line.product_qty = 1
334 po_line.price_unit = 10
335 po_line.tax_ids.clear()
336 po = po_form.save()
337 po.button_confirm()
338
339 receipt = po.picking_ids
340 receipt.move_line_ids.quantity = 1
341 receipt.button_validate()
342
343 bill_form = Form.from_action(self.env, po.action_create_invoice())
344 bill_form.invoice_date = bill_form.date
345 with bill_form.invoice_line_ids.new() as inv_line:
346 inv_line.product_id = self.productlc1
347 inv_line.price_unit = 5
348 inv_line.is_landed_costs_line = True
349 bill = bill_form.save()
350 bill.action_post()
351
352 lc_form = Form.from_action(self.env, bill.button_create_landed_costs())
353 lc_form.picking_ids.add(receipt)
354 lc = lc_form.save()
355 lc.button_validate()
356
357 product = self.product1.with_company(company)
358 self.assertEqual(product.value_svl, 15)
359 self.assertEqual(product.quantity_svl, 1)
360 self.assertEqual(product.standard_price, 15)
361
362 @tagged('-at_install', 'post_install')
363 class TestStockValuationLCFIFOVB(TestStockValuationLCCommon):
364 @classmethod
365 def setUpClass(cls):
366 super(TestStockValuationLCFIFOVB, cls).setUpClass()
367 cls.vendor1 = cls.env['res.partner'].create({'name': 'vendor1'})
368 cls.vendor1.property_account_payable_id = cls.company_data['default_account_payable']
369 cls.vendor2 = cls.env['res.partner'].create({'name': 'vendor2'})
370 cls.vendor2.property_account_payable_id = cls.company_data['default_account_payable']
371 cls.product1.product_tmpl_id.categ_id.property_cost_method = 'fifo'
372 cls.product1.product_tmpl_id.categ_id.property_valuation = 'real_time'
373
374 def test_vendor_bill_flow_anglo_saxon_1(self):
375 """In anglo saxon accounting, receive 10@10 and invoice. Then invoice 1@50 as a landed costs
376 and create a linked landed costs record.
377 """
378 self.env.company.anglo_saxon_accounting = True
379
380 # Create an RFQ for self.product1, 10@10
381 rfq = Form(self.env['purchase.order'])
382 rfq.partner_id = self.vendor1
383
384 with rfq.order_line.new() as po_line:
385 po_line.product_id = self.product1
386 po_line.product_qty = 10
387 po_line.price_unit = 10
388 po_line.tax_ids.clear()
389
390 rfq = rfq.save()
391 rfq.button_confirm()
392
393 # Process the receipt
394 receipt = rfq.picking_ids
395 receipt.button_validate()
396 self.assertEqual(rfq.order_line.qty_received, 10)
397
398 input_aml = self._get_stock_input_move_lines()[-1]
399 self.assertEqual(input_aml.debit, 0)
400 self.assertEqual(input_aml.credit, 100)
401 valuation_aml = self._get_stock_valuation_move_lines()[-1]
402 self.assertEqual(valuation_aml.debit, 100)
403 self.assertEqual(valuation_aml.credit, 0)
404
405 # Create a vendor bill for the RFQ
406 action = rfq.action_create_invoice()
407 vb = self.env['account.move'].browse(action['res_id'])
408 vb.invoice_date = vb.date
409 vb.action_post()
410
411 input_aml = self._get_stock_input_move_lines()[-1]
412 self.assertEqual(input_aml.debit, 100)
413 self.assertEqual(input_aml.credit, 0)
414 payable_aml = self._get_payable_move_lines()[-1]
415 self.assertEqual(payable_aml.debit, 0)
416 self.assertEqual(payable_aml.credit, 100)
417
418 # Create a vendor bill for a landed cost product, post it and validate a landed cost
419 # linked to this vendor bill. LC; 1@50
420 lcvb = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
421 lcvb.invoice_date = lcvb.date
422 lcvb.partner_id = self.vendor2
423 with lcvb.invoice_line_ids.new() as inv_line:
424 inv_line.product_id = self.productlc1
425 inv_line.price_unit = 50
426 inv_line.is_landed_costs_line = True
427 with lcvb.invoice_line_ids.edit(0) as inv_line:
428 inv_line.tax_ids.clear()
429 lcvb = lcvb.save()
430 lcvb.action_post()
431
432 input_aml = self._get_stock_input_move_lines()[-1]
433 self.assertEqual(input_aml.debit, 50)
434 self.assertEqual(input_aml.credit, 0)
435 payable_aml = self._get_payable_move_lines()[-1]
436 self.assertEqual(payable_aml.debit, 0)
437 self.assertEqual(payable_aml.credit, 50)
438
439 lc = Form.from_action(self.env, lcvb.button_create_landed_costs())
440 lc.picking_ids.add(receipt)
441 lc = lc.save()
442 lc.button_validate()
443
444 self.assertEqual(lc.cost_lines.price_unit, 50)
445 self.assertEqual(lc.cost_lines.product_id, self.productlc1)
446
447 input_aml = self._get_stock_input_move_lines()[-1]
448 self.assertEqual(input_aml.debit, 0)
449 self.assertEqual(input_aml.credit, 50)
450 valuation_aml = self._get_stock_valuation_move_lines()[-1]
451 self.assertEqual(valuation_aml.debit, 50)
452 self.assertEqual(valuation_aml.credit, 0)
453
454 # Check reconciliation of input aml of lc
455 lc_input_aml = lc.account_move_id.line_ids.filtered(lambda aml: aml.account_id == self.company_data['default_account_stock_in'])
456 self.assertTrue(len(lc_input_aml.full_reconcile_id), 1)
457
458 self.assertEqual(self.product1.quantity_svl, 10)
459 self.assertEqual(self.product1.value_svl, 150)
460
461 def test_vendor_bill_flow_anglo_saxon_2(self):
462 """In anglo saxon accounting, receive 10@10 and invoice with the addition of 1@50 as a
463 landed costs and create a linked landed costs record.
464 """
465 self.env.company.anglo_saxon_accounting = True
466
467 # Create an RFQ for self.product1, 10@10
468 rfq = Form(self.env['purchase.order'])
469 rfq.partner_id = self.vendor1
470
471 with rfq.order_line.new() as po_line:
472 po_line.product_id = self.product1
473 po_line.product_qty = 10
474 po_line.price_unit = 10
475 po_line.tax_ids.clear()
476
477 rfq = rfq.save()
478 rfq.button_confirm()
479
480 # Process the receipt
481 receipt = rfq.picking_ids
482 receipt.button_validate()
483 self.assertEqual(rfq.order_line.qty_received, 10)
484
485 input_aml = self._get_stock_input_move_lines()[-1]
486 self.assertEqual(input_aml.debit, 0)
487 self.assertEqual(input_aml.credit, 100)
488 valuation_aml = self._get_stock_valuation_move_lines()[-1]
489 self.assertEqual(valuation_aml.debit, 100)
490 self.assertEqual(valuation_aml.credit, 0)
491
492 # Create a vendor bill for the RFQ and add to it the landed cost
493 vb = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
494 vb.partner_id = self.vendor1
495 vb.invoice_date = vb.date
496 self.productlc1.landed_cost_ok = True
497 with vb.invoice_line_ids.new() as inv_line:
498 inv_line.product_id = self.productlc1
499 inv_line.price_unit = 50
500 inv_line.is_landed_costs_line = True
501 vb = vb.save()
502 vb.action_post()
503
504 lc = Form.from_action(self.env, vb.button_create_landed_costs())
505 lc.picking_ids.add(receipt)
506 lc = lc.save()
507 lc.button_validate()
508
509 # Check reconciliation of input aml of lc
510 lc_input_aml = lc.account_move_id.line_ids.filtered(lambda aml: aml.account_id == self.company_data['default_account_stock_in'])
511 self.assertTrue(len(lc_input_aml.full_reconcile_id), 1)
512
513 def test_vendor_bill_flow_continental_1(self):
514 """In continental accounting, receive 10@10 and invoice. Then invoice 1@50 as a landed costs
515 and create a linked landed costs record.
516 """
517 self.env.company.anglo_saxon_accounting = False
518
519 # Create an RFQ for self.product1, 10@10
520 rfq = Form(self.env['purchase.order'])
521 rfq.partner_id = self.vendor1
522
523 with rfq.order_line.new() as po_line:
524 po_line.product_id = self.product1
525 po_line.product_qty = 10
526 po_line.price_unit = 10
527 po_line.tax_ids.clear()
528
529 rfq = rfq.save()
530 rfq.button_confirm()
531
532 # Process the receipt
533 receipt = rfq.picking_ids
534 receipt.button_validate()
535 self.assertEqual(rfq.order_line.qty_received, 10)
536
537 input_aml = self._get_stock_input_move_lines()[-1]
538 self.assertEqual(input_aml.debit, 0)
539 self.assertEqual(input_aml.credit, 100)
540 valuation_aml = self._get_stock_valuation_move_lines()[-1]
541 self.assertEqual(valuation_aml.debit, 100)
542 self.assertEqual(valuation_aml.credit, 0)
543
544 # Create a vebdor bill for the RFQ
545 action = rfq.action_create_invoice()
546 vb = self.env['account.move'].browse(action['res_id'])
547 vb.invoice_date = vb.date
548 vb.action_post()
549
550 expense_aml = self._get_expense_move_lines()[-1]
551 self.assertEqual(expense_aml.debit, 100)
552 self.assertEqual(expense_aml.credit, 0)
553
554 payable_aml = self._get_payable_move_lines()[-1]
555 self.assertEqual(payable_aml.debit, 0)
556 self.assertEqual(payable_aml.credit, 100)
557
558 # Create a vendor bill for a landed cost product, post it and validate a landed cost
559 # linked to this vendor bill. LC; 1@50
560 lcvb = Form(self.env['account.move'].with_context(default_move_type='in_invoice'))
561 lcvb.partner_id = self.vendor2
562 lcvb.invoice_date = lcvb.date
563 with lcvb.invoice_line_ids.new() as inv_line:
564 inv_line.product_id = self.productlc1
565 inv_line.price_unit = 50
566 inv_line.is_landed_costs_line = True
567 with lcvb.invoice_line_ids.edit(0) as inv_line:
568 inv_line.tax_ids.clear()
569 lcvb = lcvb.save()
570 lcvb.action_post()
571
572 expense_aml = self._get_expense_move_lines()[-1]
573 self.assertEqual(expense_aml.debit, 50)
574 self.assertEqual(expense_aml.credit, 0)
575 payable_aml = self._get_payable_move_lines()[-1]
576 self.assertEqual(payable_aml.debit, 0)
577 self.assertEqual(payable_aml.credit, 50)
578
579 lc = Form.from_action(self.env, lcvb.button_create_landed_costs())
580 lc.picking_ids.add(receipt)
581 lc = lc.save()
582 lc.button_validate()
583
584 self.assertEqual(lc.cost_lines.price_unit, 50)
585 self.assertEqual(lc.cost_lines.product_id, self.productlc1)
586
587 input_aml = self._get_stock_input_move_lines()[-1]
588 self.assertEqual(input_aml.debit, 0)
589 self.assertEqual(input_aml.credit, 50)
590 valuation_aml = self._get_stock_valuation_move_lines()[-1]
591 self.assertEqual(valuation_aml.debit, 50)
592 self.assertEqual(valuation_aml.credit, 0)
593
594 self.assertEqual(self.product1.quantity_svl, 10)
595 self.assertEqual(self.product1.value_svl, 150)
596
597 def test_create_landed_cost_from_bill_multi_currencies(self):
598 # create a vendor bill in EUR where base currency in USD
599 company = self.env.user.company_id
600 currency_grp = self.env.ref('base.group_multi_currency')
601 self.env.user.write({'group_ids': [(4, currency_grp.id)]})
602 usd_currency = self.env.ref('base.USD')
603 eur_currency = self.env.ref('base.EUR')
604 eur_currency.active = True
605
606 company.currency_id = usd_currency
607
608 invoice_date = '2023-01-01'
609 accounting_date = '2024-01-31'
610
611 self.cr.execute("UPDATE res_company SET currency_id = %s WHERE id = %s", (usd_currency.id, company.id))
612 self.env['res.currency.rate'].search([]).unlink()
613 self.env['res.currency.rate'].create({
614 'name': invoice_date,
615 'rate': 1.0,
616 'currency_id': usd_currency.id,
617 'company_id': company.id,
618 })
619
620 self.env['res.currency.rate'].create({
621 'name': invoice_date,
622 'rate': 0.5,
623 'currency_id': eur_currency.id,
624 'company_id': company.id,
625 })
626
627 self.env['res.currency.rate'].create({
628 'name': accounting_date,
629 'rate': 0.25,
630 'currency_id': eur_currency.id,
631 'company_id': company.id,
632 })
633
634 po_form = Form(self.env['purchase.order'])
635 po_form.partner_id = self.vendor1
636 with po_form.order_line.new() as po_line:
637 po_line.product_id = self.product1
638 po_line.product_qty = 1
639 po_line.price_unit = 10
640 po = po_form.save()
641 po.button_confirm()
642
643 receipt = po.picking_ids
644 receipt.move_line_ids.quantity = 1
645 receipt.button_validate()
646
647 action = po.action_create_invoice()
648 bill = self.env['account.move'].browse(action['res_id'])
649 bill_form = Form(bill)
650 bill_form.invoice_date = invoice_date
651 bill_form.date = accounting_date
652 bill_form.currency_id = eur_currency
653
654 with bill_form.invoice_line_ids.new() as inv_line:
655 inv_line.product_id = self.productlc1
656 inv_line.price_unit = 5
657 inv_line.currency_id = eur_currency
658
659 bill = bill_form.save()
660 bill.action_post()
661
662 lc_form = Form.from_action(self.env, bill.button_create_landed_costs())
663 lc_form.picking_ids.add(receipt)
664 lc = lc_form.save()
665 lc.button_validate()
666
667 self.assertEqual(lc.cost_lines.price_unit, 10)
668
669
670 @tagged('-at_install', 'post_install')
671 class TestAccountInvoicingWithCOA(TestStockValuationLCCommon):
672 def setUp(self):
673 self.usd = self.env.ref('base.USD')
674 self.eur = self.env.ref('base.EUR')
675 self.env.company.currency_id = self.usd
676 self.env['res.currency.rate'].search([]).unlink()
677
678 def create_rate(self, inv_rate):
679 return self.env['res.currency.rate'].create({
680 'name': time.strftime('%Y-%m-%d'),
681 'inverse_company_rate': inv_rate,
682 'currency_id': self.eur.id,
683 'company_id': self.env.company.id,
684 })
685
686 def _bill(self, po, qty=None, price=None):
687 action = po.action_create_invoice()
688 bill = self.env["account.move"].browse(action["res_id"])
689 bill.invoice_date = fields.Date.today()
690 if qty is not None:
691 bill.invoice_line_ids.quantity = qty
692 if price is not None:
693 bill.invoice_line_ids.price_unit = price
694 bill.action_post()
695 return bill
696
697 def _return(self, picking, qty):
698 wizard_form = Form(self.env['stock.return.picking'].with_context(active_ids=picking.ids, active_id=picking.id, active_model='stock.picking'))
699 wizard = wizard_form.save()
700 wizard.product_return_moves.quantity = qty
701 return_picking = wizard._create_return()
702 return_picking.move_ids.quantity = qty
703 return_picking.button_validate()
704 return return_picking
705
706 def _purchase_receipt(self, product, qty, price, curr):
707 po_form = Form(self.env['purchase.order'])
708 po_form.partner_id = self.env['res.partner'].browse(self.supplier_id)
709 po_form.currency_id = curr
710 with po_form.order_line.new() as po_line:
711 po_line.product_id = product
712 po_line.product_qty = qty
713 po_line.price_unit = price
714 po = po_form.save()
715 po.button_confirm()
716
717 receipt = po.picking_ids
718 receipt.move_ids.quantity = qty
719 receipt.button_validate()
720
721 return po, receipt
722
723 def test_fifo_return_twice_and_bill_with_landed_cost_and_multi_currency(self):
724 """This check ensure that the landed cost does not prevent '_generate_price_difference_vals' to compute
725 the correct 'quantity already out' when handling a Return of a Return of a Receipt.
726 An inccorect value of 'quantity already out' would generate COGS lines in the vendor bill.
727 """
728 self.product1.categ_id.property_cost_method = 'fifo'
729 self.product1.categ_id.property_valuation = 'real_time'
730 self.eur.active = True
731
732 with freeze_time('2025-01-01'):
733 self.create_rate(1.0)
734 po1, _ = self._purchase_receipt(self.product1, 5, 10, self.eur)
735 self._bill(po1)
736
737 with freeze_time('2025-01-02'):
738 self.create_rate(1.5)
739 po2, receipt02 = self._purchase_receipt(self.product1, 10, 10, self.eur)
740 self._make_lc(receipt02.move_ids, 10)
741 receipt_return = self._return(receipt02, 10)
742 self._return(receipt_return, 10)
743
744 with freeze_time('2025-01-03'):
745 self.create_rate(2.0)
746 bill2 = self._bill(po2)
747
748 in_acc_id = self.company_data['default_account_stock_in'].id
749 tax_acc_id = self.company_data['default_account_tax_purchase'].id
750 payable_acc_id = self.company_data['default_account_payable'].id
751 self.assertRecordValues(bill2.line_ids, [
752 {'account_id': in_acc_id, 'balance': 200.0, 'amount_currency': 100},
753 {'account_id': tax_acc_id, 'balance': 30.0, 'amount_currency': 15},
754 {'account_id': payable_acc_id, 'balance': -230.0, 'amount_currency': -115},
755 ])