File: test_average_price.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (292 lines) | stat: -rw-r--r-- 12,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.addons.stock_account.tests.test_anglo_saxon_valuation_reconciliation_common import ValuationReconciliationTestCommon
from odoo.tests import tagged

import time


@tagged('-at_install', 'post_install')
class TestAveragePrice(ValuationReconciliationTestCommon):

    def test_00_average_price(self):
        """ Testcase for average price computation"""

        res_partner_3 = self.env['res.partner'].create({
            'name': 'Gemini Partner',
        })

        # Set a product as using average price.
        product_cable_management_box = self.env['product.product'].create({
            'default_code': 'AVG',
            'name': 'Average Ice Cream',
            'is_storable': True,
            'categ_id': self.stock_account_product_categ.id,
            'list_price': 100.0,
            'standard_price': 60.0,
            'uom_id': self.env.ref('uom.product_uom_kgm').id,
            'uom_po_id': self.env.ref('uom.product_uom_kgm').id,
            'supplier_taxes_id': [],
            'description': 'FIFO Ice Cream',
        })
        product_cable_management_box.categ_id.property_cost_method = 'average'

        # I create a draft Purchase Order for first incoming shipment for 10 pieces at 60€
        purchase_order_1 = self.env['purchase.order'].create({
            'partner_id': res_partner_3.id,
            'order_line': [(0, 0, {
                'name': 'Average Ice Cream',
                'product_id': product_cable_management_box.id,
                'product_qty': 10.0,
                'product_uom': self.env.ref('uom.product_uom_kgm').id,
                'price_unit': 60.0,
                'date_planned': time.strftime('%Y-%m-%d'),
            })]
        })

        # Confirm the first purchase order
        purchase_order_1.button_confirm()

        # Check the "Approved" status of purchase order 1
        self.assertEqual(purchase_order_1.state, 'purchase', "Wrong state of purchase order!")

        # Process the reception of purchase order 1
        picking = purchase_order_1.picking_ids[0]
        picking.button_validate()

        # Check the average_price of the product (average icecream).
        self.assertEqual(product_cable_management_box.qty_available, 10.0, 'Wrong quantity in stock after first reception')
        self.assertEqual(product_cable_management_box.standard_price, 60.0, 'Standard price should be the price of the first reception!')

        # I create a draft Purchase Order for second incoming shipment for 30 pieces at 80€
        purchase_order_2 = self.env['purchase.order'].create({
            'partner_id': res_partner_3.id,
            'order_line': [(0, 0, {
                'name': product_cable_management_box.name,
                'product_id': product_cable_management_box.id,
                'product_qty': 30.0,
                'product_uom': self.env.ref('uom.product_uom_kgm').id,
                'price_unit': 80.0,
                'date_planned': time.strftime('%Y-%m-%d'),
            })]
        })

        # Confirm the second purchase order
        purchase_order_2.button_confirm()
        # Process the reception of purchase order 2
        picking = purchase_order_2.picking_ids[0]
        picking.button_validate()

        # Check the standard price
        self.assertEqual(product_cable_management_box.standard_price, 75.0, 'After second reception, we should have an average price of 75.0 on the product')

        # Create picking to send some goods
        outgoing_shipment = self.env['stock.picking'].create({
            'picking_type_id': self.company_data['default_warehouse'].out_type_id.id,
            'location_id': self.company_data['default_warehouse'].lot_stock_id.id,
            'location_dest_id': self.env.ref('stock.stock_location_customers').id,
            'move_ids': [(0, 0, {
                'name': 'outgoing_shipment_avg_move',
                'product_id': product_cable_management_box.id,
                'product_uom_qty': 20.0,
                'product_uom': self.env.ref('uom.product_uom_kgm').id,
                'location_id':  self.company_data['default_warehouse'].lot_stock_id.id,
                'location_dest_id': self.env.ref('stock.stock_location_customers').id})]
            })

        # Assign this outgoing shipment and process the delivery
        outgoing_shipment.action_assign()
        outgoing_shipment.button_validate()

        # Check the average price (60 * 10 + 30 * 80) / 40 = 75.0€ did not change
        self.assertEqual(product_cable_management_box.standard_price, 75.0, 'Average price should not have changed with outgoing picking!')
        self.assertEqual(product_cable_management_box.qty_available, 20.0, 'Pieces were not picked correctly as the quantity on hand is wrong')

        # Make a new purchase order with 500 g Average Ice Cream at a price of 0.2€/g
        purchase_order_3 = self.env['purchase.order'].create({
            'partner_id': res_partner_3.id,
            'order_line': [(0, 0, {
                'name': product_cable_management_box.name,
                'product_id': product_cable_management_box.id,
                'product_qty': 500.0,
                'product_uom': self.ref('uom.product_uom_gram'),
                'price_unit': 0.2,
                'date_planned': time.strftime('%Y-%m-%d'),
            })]
        })

        # Confirm the first purchase order
        purchase_order_3.button_confirm()
        # Process the reception of purchase order 3 in grams

        picking = purchase_order_3.picking_ids[0]
        picking.button_validate()

        # Check price is (75.0 * 20 + 200*0.5) / 20.5 = 78.04878€
        self.assertEqual(product_cable_management_box.qty_available, 20.5, 'Reception of purchase order in grams leads to wrong quantity in stock')
        self.assertEqual(round(product_cable_management_box.standard_price, 2), 78.05,
            'Standard price as average price of third reception with other UoM incorrect! Got %s instead of 78.05' % (round(product_cable_management_box.standard_price, 2)))

    def test_inventory_user_svl_access(self):
        """ Test to check if Inventory/User is able to validate a
        transfer when the product has been invoiced already """

        avco_product = self.env['product.product'].create({
            'name': 'Average Ice Cream',
            'is_storable': True,
            'categ_id': self.stock_account_product_categ.id,
            'purchase_method': 'purchase',
        })
        avco_product.categ_id.property_cost_method = 'average'

        purchase_order = self.env['purchase.order'].create({
            'partner_id': self.partner_a.id,
            'order_line': [(0, 0, {
                'product_id': avco_product.id,
                'product_qty': 1.0,
            })]
        })

        purchase_order.button_confirm()
        purchase_order.action_create_invoice()
        bill = purchase_order.invoice_ids[0]

        bill.invoice_date = time.strftime('%Y-%m-%d')
        bill.invoice_line_ids[0].quantity = 1.0
        bill.action_post()

        self.assertEqual(purchase_order.order_line[0].qty_invoiced, 1.0, 'QTY invoiced should have been set to 1 on the purchase order line')

        picking = purchase_order.picking_ids[0]
        picking.move_ids.picked = True
        # clear cash to ensure access rights verification
        self.env.invalidate_all()
        picking.with_user(self.res_users_stock_user).button_validate()

        self.assertEqual(picking.state, 'done', 'Transfer should be in the DONE state')

    def test_bill_before_reciept(self):
        """ Check unit price of recieved product that has been invoiced already """

        avco_product = self.env['product.product'].create({
            'name': 'Average Ice Cream',
            'is_storable': True,
            'categ_id': self.stock_account_product_categ.id,
            'purchase_method': 'purchase',
        })
        avco_product.categ_id.property_cost_method = 'average'

        purchase_order = self.env['purchase.order'].create({
            'partner_id': self.partner_a.id,
            'order_line': [(0, 0, {
                'product_id': avco_product.id,
                'product_qty': 1.0,
            })]
        })

        purchase_order.button_confirm()
        purchase_order.action_create_invoice()
        bill = purchase_order.invoice_ids[0]

        bill.invoice_date = time.strftime('%Y-%m-%d')
        bill.invoice_line_ids[0].price_unit = 100.0
        bill.button_cancel()

        purchase_order.action_create_invoice()
        bill = purchase_order.invoice_ids[1]

        bill.invoice_date = time.strftime('%Y-%m-%d')
        bill.invoice_line_ids[0].price_unit = 300.0
        bill.invoice_line_ids[0].quantity = 1.0
        bill.action_post()

        picking = purchase_order.picking_ids[0]
        picking.button_validate()

        self.assertEqual(avco_product.avg_cost, 300)

    def test_svl_avco_with_discount(self):
        """
            Ensure the stock valuation is correct when
            the purchase order has a discount and the
            product was invoiced before being received
        """

        avco_product = self.env['product.product'].create({
            'name': 'Average Ice Cream',
            'type': 'consu',
            'categ_id': self.stock_account_product_categ.id,
            'purchase_method': 'purchase',
        })
        avco_product.categ_id.property_cost_method = 'average'

        purchase_order = self.env['purchase.order'].create({
            'partner_id': self.partner_a.id,
            'order_line': [(0, 0, {
                'product_id': avco_product.id,
                'product_qty': 10.0,
                'price_unit': 10.0,
                'discount': 10.0,
            })]
        })

        purchase_order.button_confirm()
        purchase_order.action_create_invoice()
        bill = purchase_order.invoice_ids[0]

        bill.invoice_date = time.strftime('%Y-%m-%d')
        bill.action_post()

        picking = purchase_order.picking_ids[0]
        picking.button_validate()

        self.assertEqual(avco_product.avg_cost, 9)
        self.assertEqual(avco_product.value_svl, 90)

    def test_no_compensatory_svl_from_asymmetrical_rounding(self):
        """ Ensure that a purchase order for a high quantity of some product using avg costing does
        not calculate the price unit asymmetrically for the order(line) and the invoice AML.
        """
        self.stock_account_product_categ.property_cost_method = 'average'
        avco_product = self.env['product.product'].create({
            'name': 'test_rounding_in_valuation product',
            'is_storable': True,
            'categ_id': self.stock_account_product_categ.id,
            'purchase_method': 'purchase',
            'standard_price': 2.0,
        })

        incl_tax = self.env['account.tax'].create({
            'name': 'test_rounding_in_valuation tax',
            'type_tax_use': 'purchase',
            'amount_type': 'percent',
            'amount': 10,
            'price_include_override': 'tax_included',
            'invoice_repartition_line_ids': [
                (0, 0, {'repartition_type': 'base'}),
                (0, 0, {
                    'repartition_type': 'tax',
                    'factor_percent': 100,
                    'account_id': self.env['account.account'].search([('name', '=', 'Tax Paid')], limit=1).id,
                }),
            ],
            'include_base_amount': False,
        })

        po = self.env['purchase.order'].create({
            'partner_id': self.partner_a.id,
            'order_line': [(0, 0, {
                'product_id': avco_product.id,
                'product_qty': 999,
                'taxes_id': [(6, 0, [incl_tax.id])],
            })],
        })
        po.button_confirm()

        po.picking_ids.move_ids.quantity = 999
        po.picking_ids.button_validate()
        po.action_create_invoice()
        po.invoice_ids[0].invoice_date = time.strftime('%Y-%m-%d')
        po.invoice_ids[0].action_post()

        self.assertFalse(po.picking_ids.move_ids.stock_valuation_layer_ids.stock_valuation_layer_ids)