File: test_delivery_cost.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 (563 lines) | stat: -rw-r--r-- 22,982 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from freezegun import freeze_time

from odoo import Command
from odoo.tests import Form, tagged
from odoo.tools import float_compare

from odoo.addons.delivery.tests.common import DeliveryCommon
from odoo.addons.sale.tests.common import SaleCommon


@tagged('post_install', '-at_install')
class TestDeliveryCost(DeliveryCommon, SaleCommon):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()

        cls._enable_uom()

        # the tests hereunder assume all the prices in USD
        cls.env.company.country_id = cls.env.ref('base.us').id

        cls.product.weight = 1.0
        cls.product_delivery_normal = cls._prepare_carrier_product(
            name='Normal Delivery Charges',
            list_price=10.0,
        )
        cls.normal_delivery = cls._prepare_carrier(
            product=cls.product_delivery_normal,
            name='Normal Delivery Charges',
            delivery_type='fixed',
            fixed_price=10.0,
        )
        cls.partner_4 = cls.env['res.partner'].create({
            'name': 'Another Customer',
            'child_ids': [
                Command.create({
                    'name': "Another Customer's Address",
                })
            ]
        })
        cls.partner_address_13 = cls.partner_4.child_ids
        cls.product_uom_hour = cls.env.ref('uom.product_uom_hour')

    def test_00_delivery_cost(self):
        # In order to test Carrier Cost
        # Create sales order with Normal Delivery Charges

        self.sale_normal_delivery_charges = self.env['sale.order'].create({
            'partner_id': self.partner.id,
            'partner_invoice_id': self.partner.id,
            'partner_shipping_id': self.partner.id,
            'order_line': [
                Command.create({
                    'product_id': self.product.id,
                    'price_unit': 750.00,
                })
            ],
        })
        # I add delivery cost in Sales order

        self.a_sale = self.env['account.account'].create({
            'code': 'X2020',
            'name': 'Product Sales - (test)',
            'account_type': 'income',
            'tag_ids': [Command.set(self.env.ref('account.account_tag_operating').ids)]
        })

        self.product_consultant = self.env['product.product'].create({
            'sale_ok': True,
            'list_price': 75.0,
            'standard_price': 30.0,
            'uom_id': self.product_uom_hour.id,
            'uom_po_id': self.product_uom_hour.id,
            'name': 'Service',
            'type': 'service'
        })

        # I add delivery cost in Sales order
        delivery_wizard = Form(self.env['choose.delivery.carrier'].with_context({
            'default_order_id': self.sale_normal_delivery_charges.id,
            'default_carrier_id': self.normal_delivery.id
        }))
        choose_delivery_carrier = delivery_wizard.save()
        choose_delivery_carrier.button_confirm()

        # I check sales order after added delivery cost

        line = self.sale_normal_delivery_charges.order_line.filtered_domain([
            ('product_id', '=', self.normal_delivery.product_id.id)])
        self.assertEqual(len(line), 1, "Delivery cost is not Added")

        zin = str(delivery_wizard.display_price) + " " + str(delivery_wizard.delivery_price) + ' ' + line.company_id.country_id.code + line.company_id.name
        self.assertEqual(float_compare(line.price_subtotal, 10.0, precision_digits=2), 0,
            "Delivery cost does not correspond to 10.0. %s %s" % (line.price_subtotal, zin))

        # I confirm the sales order

        self.sale_normal_delivery_charges.action_confirm()

        # Create one more sales order with Free Delivery Charges
        self.delivery_sale_order_cost = self.env['sale.order'].create({
            'partner_id': self.partner_4.id,
            'partner_invoice_id': self.partner_address_13.id,
            'partner_shipping_id': self.partner_address_13.id,
            'order_line': [
                Command.create({
                    'product_id': self.product_consultant.id,
                    'product_uom_qty': 24,
                    'product_uom': self.product_uom_hour.id,
                    'price_unit': 75.00,
                }),
                Command.create({
                    'product_id': self.product.id,
                    'product_uom_qty': 30,
                    'price_unit': 38.25,
                })
            ],
        })

        # I add free delivery cost in Sales order
        delivery_wizard = Form(self.env['choose.delivery.carrier'].with_context({
            'default_order_id': self.delivery_sale_order_cost.id,
            'default_carrier_id': self.free_delivery.id
        }))
        choose_delivery_carrier = delivery_wizard.save()
        choose_delivery_carrier.button_confirm()

        # I check sales order after adding delivery cost
        line = self.delivery_sale_order_cost.order_line.filtered_domain([
            ('product_id', '=', self.free_delivery.product_id.id)])

        self.assertEqual(len(line), 1, "Delivery cost is not Added")
        self.assertEqual(float_compare(line.price_subtotal, 0, precision_digits=2), 0,
            "Delivery cost is not correspond.")

        # I set default delivery policy
        self.env['res.config.settings'].create({}).execute()

    def test_01_delivery_cost_from_pricelist(self):
        """ This test aims to validate the use of a pricelist to compute the delivery cost in the case the associated
            product of the shipping method is defined in the pricelist """

        # Create pricelist with a custom price for the standard shipping method
        my_pricelist = self.env['product.pricelist'].create({
            'name': 'shipping_cost_change',
            'item_ids': [Command.create({
                'compute_price': 'fixed',
                'fixed_price': 5,
                'applied_on': '0_product_variant',
                'product_id': self.normal_delivery.product_id.id,
            })],
        })

        # Create sales order with Normal Delivery Charges
        sale_pricelist_based_delivery_charges = self.env['sale.order'].create({
            'partner_id': self.partner.id,
            'pricelist_id': my_pricelist.id,
            'order_line': [Command.create({
                'product_id': self.product.id,
                'product_uom_qty': 1,
                'price_unit': 750.00,
            })],
        })

        # Add of delivery cost in Sales order
        delivery_wizard = Form(self.env['choose.delivery.carrier'].with_context({
            'default_order_id': sale_pricelist_based_delivery_charges.id,
            'default_carrier_id': self.normal_delivery.id
        }))
        self.assertEqual(delivery_wizard.delivery_price, 5.0, "Delivery cost does not correspond to 5.0 in wizard")
        delivery_wizard.save().button_confirm()

        line = sale_pricelist_based_delivery_charges.order_line.filtered_domain([
            ('product_id', '=', self.normal_delivery.product_id.id)])
        self.assertEqual(len(line), 1, "Delivery cost hasn't been added to SO")
        self.assertEqual(line.price_subtotal, 5.0, "Delivery cost does not correspond to 5.0")

    def test_02_delivery_cost_from_different_currency(self):
        """ This test aims to validate the use of a pricelist using a different currency to compute the delivery cost in
            the case the associated product of the shipping method is defined in the pricelist """

        # Create pricelist with a custom price for the standard shipping method
        my_pricelist = self.env['product.pricelist'].create({
            'name': 'shipping_cost_change',
            'item_ids': [Command.create({
                'compute_price': 'fixed',
                'fixed_price': 5,
                'applied_on': '0_product_variant',
                'product_id': self.normal_delivery.product_id.id,
            })],
            'currency_id': self.env.ref('base.EUR').id,
        })

        # Create sales order with Normal Delivery Charges
        sale_pricelist_based_delivery_charges = self.env['sale.order'].create({
            'partner_id': self.partner.id,
            'pricelist_id': my_pricelist.id,
            'order_line': [Command.create({
                'product_id': self.product.id,
                'product_uom_qty': 1,
                'price_unit': 750.00,
            })],
        })

        # Add of delivery cost in Sales order
        delivery_wizard = Form(self.env['choose.delivery.carrier'].with_context({
            'default_order_id': sale_pricelist_based_delivery_charges.id,
            'default_carrier_id': self.normal_delivery.id
        }))
        self.assertEqual(delivery_wizard.delivery_price, 5.0, "Delivery cost does not correspond to 5.0 in wizard")
        delivery_wizard.save().button_confirm()

        line = sale_pricelist_based_delivery_charges.order_line.filtered_domain([
            ('product_id', '=', self.normal_delivery.product_id.id)])
        self.assertEqual(len(line), 1, "Delivery cost hasn't been added to SO")
        self.assertEqual(line.price_subtotal, 5.0, "Delivery cost does not correspond to 5.0")

    def test_01_taxes_on_delivery_cost(self):
        # Creating taxes and fiscal position

        self.env.ref('base.group_user').write({'implied_ids': [(4, self.env.ref('product.group_product_pricelist').id)]})

        tax_price_include, tax_price_exclude = self.env['account.tax'].create([{
            'name': '10% inc',
            'type_tax_use': 'sale',
            'amount_type': 'percent',
            'amount': 10,
            'price_include_override': 'tax_included',
            'include_base_amount': True,
        }, {
            'name': '15% exc',
            'type_tax_use': 'sale',
            'amount_type': 'percent',
            'amount': 15,
        }])

        fiscal_position = self.env['account.fiscal.position'].create({
            'name': 'fiscal_pos_a',
            'tax_ids': [
                (0, None, {
                    'tax_src_id': tax_price_include.id,
                    'tax_dest_id': tax_price_exclude.id,
                }),
            ],
        })

        # Setting tax on delivery product
        self.normal_delivery.product_id.taxes_id = tax_price_include

        # Create sales order
        # Required to see `pricelist_id` in the view
        self.env.user.groups_id += self.env.ref('product.group_product_pricelist')
        order_form = Form(self.env['sale.order'].with_context(tracking_disable=True))
        order_form.partner_id = self.partner
        order_form.fiscal_position_id = fiscal_position

        # Try adding delivery product as a normal product
        with order_form.order_line.new() as line:
            line.product_id = self.normal_delivery.product_id
            line.product_uom_qty = 1.0
        sale_order = order_form.save()

        self.assertRecordValues(sale_order.order_line, [{'price_subtotal': 9.09, 'price_total': 10.45}])

        # Now trying to add the delivery line using the delivery wizard, the results should be the same as before
        delivery_wizard = Form(self.env['choose.delivery.carrier'].with_context(default_order_id=sale_order.id,
                          default_carrier_id=self.normal_delivery.id))
        choose_delivery_carrier = delivery_wizard.save()
        choose_delivery_carrier.button_confirm()

        line = sale_order.order_line.filtered_domain([
            ('product_id', '=', self.normal_delivery.product_id.id),
            ('is_delivery', '=', True),
        ])

        self.assertRecordValues(line, [{'price_subtotal': 9.09, 'price_total': 10.45}])

    def test_estimated_weight(self):
        """
        Test that negative qty SO lines are not included in the estimated weight calculation
        of delivery carriers (since it's used when calculating their rates).
        """
        sale_order = self.env['sale.order'].create({
            'partner_id': self.partner.id,
            'order_line': [
                Command.create({
                    'product_id': self.product.id,
                    'product_uom_qty': 1,
                }),
                Command.create({
                    'product_id': self.product.id,
                    'product_uom_qty': -1,
                }),
            ],
        })
        shipping_weight = sale_order._get_estimated_weight()
        self.assertEqual(shipping_weight, self.product.weight, "Only positive quantity products' weights should be included in estimated weight")

    def test_fixed_price_margins(self):
        """
         margins should be ignored for fixed price carriers
        """
        sale_order = self.env['sale.order'].create({
            'partner_id': self.partner.id,
            'name': 'SO - fixed del',
            'order_line': [
                (0, 0, {
                    'product_id': self.product.id,
                    'product_uom_qty': 1,
                }),
            ]
        })
        self.normal_delivery.fixed_margin = 100
        self.normal_delivery.margin = 4.2
        delivery_wizard = Form(self.env['choose.delivery.carrier'].with_context(default_order_id=sale_order.id,
                          default_carrier_id=self.normal_delivery.id))
        choose_delivery_carrier = delivery_wizard.save()
        choose_delivery_carrier.button_confirm()

        line = sale_order.order_line.filtered('is_delivery')
        self.assertEqual(line.price_unit, self.normal_delivery.fixed_price)

    def test_price_with_weight_volume_variable(self):
        """ Test that the price is correctly computed when the variable is weight*volume. """
        qty = 3
        list_price = 2
        volume = 2.5
        weight = 1.5
        sale_order = self.env['sale.order'].create({
            'partner_id': self.partner_4.id,
            'order_line': [
                (0, 0, {
                    'product_id': self.env['product.product'].create({
                        'name': 'wv',
                        'weight': weight,
                        'volume': volume,
                    }).id,
                    'product_uom_qty': qty,
                }),
            ],
        })
        delivery = self.env['delivery.carrier'].create({
            'name': 'Delivery Charges',
            'delivery_type': 'base_on_rule',
            'product_id': self.product_delivery_normal.id,
            'price_rule_ids': [(0, 0, {
                'variable': 'price',
                'operator': '>=',
                'max_value': 0,
                'list_price': list_price,
                'variable_factor': 'wv',
            })]
        })
        self.assertEqual(
            delivery._get_price_available(sale_order),
            qty * list_price * weight * volume,
            "The shipping price is not correctly computed with variable weight*volume.",
        )

    def test_delivery_product_taxes_on_branch(self):
        """ Check taxes populated on delivery line on branch company.
            Taxes from the branch company should be taken with a fallback on parent company.
        """
        company = self.env.company
        branch = self.env['res.company'].create({
            'name': 'Branch',
            'country_id': company.country_id.id,
            'parent_id': company.id,
        })
        # create taxes for the parent company and its branch
        tax_groups = self.env['account.tax.group'].create([{
            'name': 'Tax Group A',
            'company_id': company.id,
        }, {
            'name': 'Tax Group B',
            'company_id': branch.id,
        }])
        tax_a = self.env['account.tax'].create({
            'name': 'Tax A',
            'type_tax_use': 'sale',
            'amount_type': 'percent',
            'amount': 10,
            'tax_group_id': tax_groups[0].id,
            'company_id': company.id,
        })
        tax_b = self.env['account.tax'].create({
            'name': 'Tax B',
            'type_tax_use': 'sale',
            'amount_type': 'percent',
            'amount': 20,
            'tax_group_id': tax_groups[1].id,
            'company_id': branch.id,
        })
        # create delivery product with taxes from both branch and parent company
        delivery_product = self.env['product.product'].create({
            'name': 'Delivery Product',
            'taxes_id': [Command.set((tax_a + tax_b).ids)],
        })
        # create delivery
        delivery = self.env['delivery.carrier'].create({
            'name': 'Delivery Charges',
            'delivery_type': 'fixed',
            'product_id': delivery_product.id,
            'company_id': branch.id,
        })
        # create a SO from Branch
        sale_order = self.env['sale.order'].create({
            'partner_id': self.partner_4.id,
            'company_id': branch.id,
            'order_line': [Command.create({
                'product_id': self.product.id,
                'product_uom_qty': 1,
            })],
        })
        # add delivery
        wizard = self.env['choose.delivery.carrier'].create({
            'order_id': sale_order.id,
            'carrier_id': delivery.id,
            'company_id': branch.id,
        })
        wizard.button_confirm()
        delivery_line = sale_order.order_line.filtered(lambda l: l.is_delivery)

        # delivery line should have taxes from the branch company
        self.assertRecordValues(delivery_line, [{'product_id': delivery_product.id, 'tax_id': tax_b.ids}])

        # update delivery product by setting only the tax from parent company
        delivery_product.write({'taxes_id': [Command.set((tax_a).ids)]})
        # update delivery
        wizard = self.env['choose.delivery.carrier'].create({
            'order_id': sale_order.id,
            'carrier_id': delivery.id,
            'company_id': branch.id,
        })
        wizard.button_confirm()
        delivery_line = sale_order.order_line.filtered(lambda l: l.is_delivery)

        # delivery line should have taxes from the parent company as there is no tax from the branch company
        self.assertRecordValues(delivery_line, [{'product_id': delivery_product.id, 'tax_id': tax_a.ids}])

    def test_update_weight_in_shipping_when_change_quantity(self):
        product_test = self.env['product.product'].create({
            'name': 'Test product',
            'weight': 1,
        })
        sale_order = self.env['sale.order'].create({
            'partner_id': self.partner.id,
            'order_line': [
                Command.create({
                    'product_id': product_test.id,
                    'product_uom_qty': 10,
                    'product_uom': self.uom_unit.id,
                }),
            ],
        })
        delivery = self.env['delivery.carrier'].create({
            'name': 'Delivery Charges',
            'delivery_type': 'base_on_rule',
            'product_id': product_test.id,
            'price_rule_ids': [
                Command.create({
                    'variable': 'weight',
                    'operator': '<=',
                    'max_value': 30,
                    'list_base_price': 5,
                    'variable_factor': 'weight',
                }),
                Command.create({
                    'variable': 'weight',
                    'operator': '>=',
                    'max_value': 60,
                    'list_base_price': 10,
                    'variable_factor': 'weight',
                })
            ]
        })

        del_form = sale_order.action_open_delivery_wizard()
        choose_delivery_carrier = self.env[del_form['res_model']].with_context(del_form['context']).create({
            'carrier_id': delivery.id,
            'order_id': sale_order.id
        })
        choose_delivery_carrier.button_confirm()
        self.assertEqual(choose_delivery_carrier.total_weight, 10)
        sale_order.order_line.write({
            'product_uom_qty': 100,
        })
        updated_del_form = sale_order.action_open_delivery_wizard()
        self.assertEqual(updated_del_form['context']['default_total_weight'], 100)

    def test_base_on_rule_currency_is_converted(self):
        """
        For based on rules delivery method without a company, check that the price
        is converted from the main's company's currency to the current company's on SOs
        """

        # Create a company that uses a different currency
        currency_bells = self.env['res.currency'].create({
            'name': 'Bell',
            'symbol': 'C',
        })

        nook_inc = self.env['res.company'].create({
            'name': 'Nook inc.',
            'currency_id': currency_bells.id,
        })

        with freeze_time('2000-01-01'):  # Make sure the rate is in the past
            self.env['res.currency.rate'].with_company(nook_inc).create({
                'currency_id': currency_bells.id,
                'company_rate': 0.5,
                'inverse_company_rate': 2,
            })

        # Company less shipping method
        product_delivery_rule = self.env['product.product'].with_company(nook_inc).create({
            'name': 'rule delivery charges',
            'type': 'service',
            'list_price': 10.0,
            'categ_id': self.env.ref('delivery.product_category_deliveries').id,
        })

        delivery = self.env['delivery.carrier'].with_company(nook_inc).create({
            'name': 'Rule Delivery',
            'delivery_type': 'base_on_rule',
            'product_id': product_delivery_rule.id,
            'price_rule_ids': [(0, 0, {
                'variable': 'price',
                'operator': '>=',
                'max_value': 0,
                'variable_factor': 'weight',
                'list_base_price': 15,
            })]
        })

        # Create sale using the shipping method
        so = self.env['sale.order'].with_company(nook_inc).create({
            'partner_id': self.partner_4.id,
            'partner_invoice_id': self.partner_4.id,
            'partner_shipping_id': self.partner_4.id,
            'order_line': [(0, 0, {
                'name': 'PC Assamble + 2GB RAM',
                'product_id': self.product.id,
                'product_uom_qty': 1,
                'product_uom': self.uom_unit.id,
                'price_unit': 750.00,
            })],
        })

        delivery_wizard = Form(self.env['choose.delivery.carrier'].with_company(nook_inc).with_context({
            'default_order_id': so.id,
            'default_carrier_id': delivery.id,
        }))
        choose_delivery_carrier = delivery_wizard.save()
        choose_delivery_carrier.button_confirm()

        # check delivery price was properly converted
        delivery_sol = so.order_line[-1]
        self.assertEqual(delivery_sol.product_id, delivery.product_id)
        self.assertEqual(delivery_sol.price_subtotal, 7.5)