Debian Codegraph @

addons/stock_landed_costs/tests/test_stock_landed_costs.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.addons.stock_landed_costs.tests.common import TestStockLandedCostsCommon
6 from odoo.exceptions import ValidationError
7 from odoo.tests import tagged
8 from odoo import fields
9
10
11 @tagged('post_install', '-at_install')
12 @skip('Temporary to fast merge new valuation')
13 class TestStockLandedCosts(TestStockLandedCostsCommon):
14
15 def test_stock_landed_costs(self):
16 # In order to test the landed costs feature of stock,
17 # I create a landed cost, confirm it and check its account move created
18
19 # I create 2 products with different volume and gross weight and configure
20 # them for real_time valuation and fifo costing method
21 product_landed_cost_1 = self.env['product.product'].create({
22 'name': "LC product 1",
23 'weight': 10,
24 'volume': 1,
25 'categ_id': self.stock_account_product_categ.id,
26 'is_storable': True,
27 })
28
29 product_landed_cost_2 = self.env['product.product'].create({
30 'name': "LC product 2",
31 'weight': 20,
32 'volume': 1.5,
33 'categ_id': self.stock_account_product_categ.id,
34 'is_storable': True,
35 })
36
37 self.assertEqual(product_landed_cost_1.value_svl, 0)
38 self.assertEqual(product_landed_cost_1.quantity_svl, 0)
39 self.assertEqual(product_landed_cost_2.value_svl, 0)
40 self.assertEqual(product_landed_cost_2.quantity_svl, 0)
41
42 picking_default_vals = self.env['stock.picking'].default_get(list(self.env['stock.picking'].fields_get()))
43
44 # I create 2 picking moving those products
45 vals = dict(picking_default_vals, **{
46 'name': 'LC_pick_1',
47 'picking_type_id': self.warehouse.out_type_id.id,
48 'move_ids': [(0, 0, {
49 'product_id': product_landed_cost_1.id,
50 'product_uom_qty': 15,
51 'product_uom': self.ref('uom.product_uom_unit'),
52 'location_id': self.warehouse.lot_stock_id.id,
53 'location_dest_id': self.ref('stock.stock_location_customers'),
54 })],
55 })
56 picking_landed_cost_1 = self.env['stock.picking'].new(vals)
57 picking_landed_cost_1._onchange_picking_type()
58 vals = picking_landed_cost_1._convert_to_write(picking_landed_cost_1._cache)
59 picking_landed_cost_1 = self.env['stock.picking'].create(vals)
60 move_1_id = picking_landed_cost_1.move_ids.id
61
62 # Confirm and assign picking
63 picking_landed_cost_1.picking_type_id.create_backorder = 'never'
64 self.env.company.anglo_saxon_accounting = True
65 picking_landed_cost_1.action_confirm()
66 picking_landed_cost_1.action_assign()
67 picking_landed_cost_1.move_ids.quantity = 5
68 picking_landed_cost_1.button_validate()
69
70 vals = dict(picking_default_vals, **{
71 'name': 'LC_pick_2',
72 'picking_type_id': self.warehouse.out_type_id.id,
73 'move_ids': [(0, 0, {
74 'product_id': product_landed_cost_2.id,
75 'product_uom_qty': 10,
76 'product_uom': self.ref('uom.product_uom_unit'),
77 'location_id': self.warehouse.lot_stock_id.id,
78 'location_dest_id': self.ref('stock.stock_location_customers'),
79 })],
80 })
81 picking_landed_cost_2 = self.env['stock.picking'].new(vals)
82 picking_landed_cost_2._onchange_picking_type()
83 vals = picking_landed_cost_2._convert_to_write(picking_landed_cost_2._cache)
84 picking_landed_cost_2 = self.env['stock.picking'].create(vals)
85 move_2_id = picking_landed_cost_2.move_ids.id
86
87 # Confirm and assign picking
88 picking_landed_cost_2.action_confirm()
89 picking_landed_cost_2.action_assign()
90 picking_landed_cost_2.move_ids.quantity = 10
91 picking_landed_cost_2.button_validate()
92
93 self.assertEqual(product_landed_cost_1.value_svl, 0)
94 self.assertEqual(product_landed_cost_1.quantity_svl, -5)
95 self.assertEqual(product_landed_cost_2.value_svl, 0)
96 self.assertEqual(product_landed_cost_2.quantity_svl, -10)
97
98 # I create a landed cost for those 2 pickings
99 default_vals = self.env['stock.landed.cost'].default_get(list(self.env['stock.landed.cost'].fields_get()))
100 virtual_home_staging = self.env['product.product'].create({
101 'name': 'Virtual Home Staging',
102 'categ_id': self.stock_account_product_categ.id,
103 })
104 default_vals.update({
105 'picking_ids': [picking_landed_cost_1.id, picking_landed_cost_2.id],
106 'account_journal_id': self.expenses_journal,
107 'cost_lines': [
108 (0, 0, {'product_id': virtual_home_staging.id}),
109 (0, 0, {'product_id': virtual_home_staging.id}),
110 (0, 0, {'product_id': virtual_home_staging.id}),
111 (0, 0, {'product_id': virtual_home_staging.id})],
112 'valuation_adjustment_lines': [],
113 })
114 cost_lines_values = {
115 'name': ['equal split', 'split by quantity', 'split by weight', 'split by volume'],
116 'split_method': ['equal', 'by_quantity', 'by_weight', 'by_volume'],
117 'price_unit': [10, 150, 250, 20],
118 }
119 stock_landed_cost_1 = self.env['stock.landed.cost'].new(default_vals)
120 for index, cost_line in enumerate(stock_landed_cost_1.cost_lines):
121 cost_line.onchange_product_id()
122 cost_line.name = cost_lines_values['name'][index]
123 cost_line.split_method = cost_lines_values['split_method'][index]
124 cost_line.price_unit = cost_lines_values['price_unit'][index]
125 vals = stock_landed_cost_1._convert_to_write(stock_landed_cost_1._cache)
126 stock_landed_cost_1 = self.env['stock.landed.cost'].create(vals)
127
128 # I compute the landed cost using Compute button
129 stock_landed_cost_1.compute_landed_cost()
130
131 # I check the valuation adjustment lines
132 for valuation in stock_landed_cost_1.valuation_adjustment_lines:
133 if valuation.cost_line_id.name == 'equal split':
134 self.assertEqual(valuation.additional_landed_cost, 5)
135 elif valuation.cost_line_id.name == 'split by quantity' and valuation.move_id.id == move_1_id:
136 self.assertEqual(valuation.additional_landed_cost, 50)
137 elif valuation.cost_line_id.name == 'split by quantity' and valuation.move_id.id == move_2_id:
138 self.assertEqual(valuation.additional_landed_cost, 100)
139 elif valuation.cost_line_id.name == 'split by weight' and valuation.move_id.id == move_1_id:
140 self.assertEqual(valuation.additional_landed_cost, 50)
141 elif valuation.cost_line_id.name == 'split by weight' and valuation.move_id.id == move_2_id:
142 self.assertEqual(valuation.additional_landed_cost, 200)
143 elif valuation.cost_line_id.name == 'split by volume' and valuation.move_id.id == move_1_id:
144 self.assertEqual(valuation.additional_landed_cost, 5)
145 elif valuation.cost_line_id.name == 'split by volume' and valuation.move_id.id == move_2_id:
146 self.assertEqual(valuation.additional_landed_cost, 15)
147 else:
148 raise ValidationError('unrecognized valuation adjustment line')
149
150 # I confirm the landed cost
151 stock_landed_cost_1.button_validate()
152
153 # I check that the landed cost is now "Closed" and that it has an accounting entry
154 self.assertEqual(stock_landed_cost_1.state, "done")
155 self.assertTrue(stock_landed_cost_1.account_move_id)
156 self.assertEqual(len(stock_landed_cost_1.account_move_id.line_ids), 48)
157
158 lc_value = sum(stock_landed_cost_1.account_move_id.line_ids.filtered(lambda aml: aml.account_id.name.startswith('Expenses')).mapped('debit'))
159 product_value = abs(product_landed_cost_1.value_svl) + abs(product_landed_cost_2.value_svl)
160 self.assertEqual(lc_value, product_value)
161
162 self.assertEqual(len(picking_landed_cost_1.move_ids.stock_valuation_layer_ids), 5)
163 self.assertEqual(len(picking_landed_cost_2.move_ids.stock_valuation_layer_ids), 5)
164
165 def test_aml_account_selection(self):
166 """
167 Process a PO with a landed cost, then create and post the bill. The
168 account of the landed cost AML should be:
169 - Expense if the categ valuation is manual
170 - Stock IN if the categ valuation is real time
171 """
172 self.landed_cost.landed_cost_ok = True
173
174 for valuation in ['periodic', 'real_time']:
175 self.landed_cost.categ_id.property_valuation = valuation
176 account_name = 'stock_input' if valuation == 'real_time' else 'expense'
177 account = self.landed_cost.product_tmpl_id.get_product_accounts()[account_name]
178 po = self.env['purchase.order'].create({
179 'partner_id': self.partner_a.id,
180 'currency_id': self.company_data['currency'].id,
181 'order_line': [
182 (0, 0, {
183 'name': self.product_a.name,
184 'product_id': self.product_a.id,
185 'product_qty': 1.0,
186 'product_uom_id': self.product_a.uom_id.id,
187 'price_unit': 100.0,
188 'tax_ids': False,
189 }),
190 (0, 0, {
191 'name': self.landed_cost.name,
192 'product_id': self.landed_cost.id,
193 'product_qty': 1.0,
194 'price_unit': 100.0,
195 }),
196 ],
197 })
198 po.button_confirm()
199
200 receipt = po.picking_ids
201 receipt.move_ids.quantity = 1
202 receipt.button_validate()
203 po.order_line[1].qty_received = 1
204
205 po.action_create_invoice()
206 bill = po.invoice_ids
207 bill.invoice_date = fields.Date.today()
208
209 self.env['account.move.line'].create({
210 'move_id': bill.id,
211 'display_type': 'line_section',
212 'name': 'Great Section',
213 })
214
215 bill._post()
216
217 landed_cost_aml = bill.invoice_line_ids.filtered(lambda l: l.product_id == self.landed_cost)
218 self.assertEqual(bill.state, 'posted', 'Incorrect value with valuation %s' % valuation)
219 self.assertEqual(landed_cost_aml.account_id, account, 'Incorrect value with valuation %s' % valuation)
220
221 def test_landed_cost_in_move_line(self):
222 """
223 Tests that a move line created through the catalog gives the right landed cost
224 """
225 self.landed_cost.landed_cost_ok = True
226 account_move = self.env['account.move'].create({
227 'move_type': 'in_invoice',
228 'partner_id': self.partner_a.id
229 })
230 account_move._update_order_line_info(
231 product_id=self.landed_cost.id,
232 quantity=1
233 )
234 self.assertTrue(account_move.invoice_line_ids.is_landed_costs_line, "The landed cost should appear in the move line.")
235 account_move._update_order_line_info(
236 product_id=self.product.id,
237 quantity=1
238 )
239 move_line_no_landed = account_move.line_ids.filtered(lambda line: line.product_id == self.product)
240 self.assertFalse(move_line_no_landed.is_landed_costs_line, "The landed cost should not be set to True.")