Debian Codegraph @

addons/mrp_subcontracting_landed_costs/tests/test_subcontracting_landed_costs.py

raw symbols SCIP
1 from unittest import skip
2
3 from odoo.exceptions import ValidationError
4 from odoo.tests import Form, tagged
5
6 from odoo.addons.mrp_subcontracting.tests.common import TestMrpSubcontractingCommon
7
8
9 @tagged('post_install', '-at_install')
10 @skip('Temporary to fast merge new valuation')
11 class TestSubcontractingLandedCosts(TestMrpSubcontractingCommon):
12
13 def test_subcontracting_landed_cost_receipts_flow(self):
14 """
15 This test verifies that landed costs can be applied to subcontracting receipts
16 rather than being added directly to the manufacturing order.
17 """
18 self.product_category.property_cost_method = 'fifo'
19 self.product_category.property_valuation = 'real_time'
20 po = self.env['purchase.order'].create({
21 'partner_id': self.subcontractor_partner1.id,
22 'order_line': [(0, 0, {
23 'name': self.finished.name,
24 'product_id': self.finished.id,
25 'product_uom_qty': 10,
26 'product_uom_id': self.finished.uom_id.id,
27 'price_unit': 10,
28 })],
29 })
30 po.button_confirm()
31
32 mo = self.env['mrp.production'].search([('bom_id', '=', self.bom.id)])
33 self.assertTrue(mo)
34
35 action = po.action_view_picking()
36 in_picking = self.env[action['res_model']].browse(action['res_id'])
37 in_picking.move_ids.quantity = 10
38 in_picking.move_ids.picked = True
39 in_picking.button_validate()
40
41 # create a landed cost for the incoming picking
42 default_vals = self.env['stock.landed.cost'].default_get(list(self.env['stock.landed.cost'].fields_get()))
43 freight_charges = self.env['product.product'].create({
44 'name': 'Freight Charges',
45 'categ_id': self.product_category.id,
46 })
47 default_vals.update({
48 'picking_ids': [in_picking.id],
49 'cost_lines': [(0, 0, {
50 'product_id': freight_charges.id,
51 'name': 'equal split',
52 'split_method': 'equal',
53 'price_unit': 99,
54 })],
55 })
56 stock_landed_cost = self.env['stock.landed.cost'].create(default_vals)
57
58 # compute the landed cost using compute button
59 stock_landed_cost.compute_landed_cost()
60
61 # check the valuation adjustment lines
62 for valuation in stock_landed_cost.valuation_adjustment_lines:
63 if valuation.cost_line_id.name == 'equal split':
64 self.assertEqual(valuation.former_cost, 100)
65 self.assertEqual(valuation.additional_landed_cost, 99, 'Additional Landed Cost should be 99 instead of %s' % (valuation.additional_landed_cost))
66 self.assertEqual(valuation.final_cost, 199)
67 else:
68 raise ValidationError('unrecognized valuation adjustment line')
69
70 # confirm the landed cost
71 stock_landed_cost.button_validate()
72 self.assertEqual(stock_landed_cost.state, "done")
73
74 self.assertEqual(len(in_picking.move_ids.stock_valuation_layer_ids), 1)
75 self.assertEqual(in_picking.move_ids.stock_valuation_layer_ids.value, 99)
76
77 new_po = self.env['purchase.order'].create({
78 'partner_id': self.subcontractor_partner1.id,
79 'order_line': [(0, 0, {
80 'name': self.finished.name,
81 'product_id': self.finished.id,
82 'product_uom_qty': 10,
83 'product_uom_id': self.finished.uom_id.id,
84 'price_unit': 10,
85 })],
86 })
87
88 # The following checks ensure that the landed cost is distributed uniformly between standard product and subcontracting product
89 product = self.env['product.product'].create({
90 'name': 'Product',
91 'is_storable': True,
92 'categ_id': self.product_category.id,
93 })
94 with Form(new_po) as po_form:
95 with po_form.order_line.new() as new_line:
96 new_line.product_id = product
97 new_line.product_qty = 10
98 new_line.price_unit = 20
99 new_po.button_confirm()
100
101 new_mo = self.env['mrp.production'].search([('bom_id', '=', self.bom.id)])
102 self.assertTrue(new_mo)
103
104 action = new_po.action_view_picking()
105 in_picking = self.env[action['res_model']].browse(action['res_id'])
106 in_picking.move_ids.quantity = 10
107 in_picking.move_ids.picked = True
108 in_picking.button_validate()
109
110 # create a landed cost for the incoming picking
111 default_vals = self.env['stock.landed.cost'].default_get(list(self.env['stock.landed.cost'].fields_get()))
112 default_vals.update({
113 'picking_ids': [in_picking.id],
114 'cost_lines': [(0, 0, {
115 'product_id': freight_charges.id,
116 'name': 'equal split',
117 'split_method': 'equal',
118 'price_unit': 99,
119 })],
120 })
121 stock_landed_cost = self.env['stock.landed.cost'].create(default_vals)
122
123 # compute the landed cost using compute button
124 stock_landed_cost.compute_landed_cost()
125
126 # check the valuation adjustment lines
127 self.assertEqual(len(stock_landed_cost.valuation_adjustment_lines), 2)
128 for valuation in stock_landed_cost.valuation_adjustment_lines:
129 if valuation.cost_line_id.name == 'equal split':
130 self.assertEqual(valuation.additional_landed_cost, 49.5, 'Additional Landed Cost should be 49.5 instead of %s' % (valuation.additional_landed_cost))
131 else:
132 raise ValidationError('unrecognized valuation adjustment line')
133
134 # confirm the landed cost
135 stock_landed_cost.button_validate()
136 self.assertEqual(stock_landed_cost.state, "done")