forked from OCA/delivery-carrier
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_package_fee.py
652 lines (613 loc) · 23.8 KB
/
test_package_fee.py
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# Copyright 2020 Camptocamp
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.tests.common import Form, TransactionCase
from odoo.tools import mute_logger
class TestPackageFee(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.product1 = cls.env["product.product"].create(
{"name": "Product 1", "type": "product", "lst_price": 1.0}
)
cls.product2 = cls.env["product.product"].create(
{"name": "Product 2", "type": "product", "lst_price": 1.0}
)
cls.partner = cls.env["res.partner"].create({"name": "Partner"})
cls.carrier_product = cls.env["product.product"].create(
{"name": "Shipping", "type": "service"}
)
cls.fee1 = cls.env["product.product"].create(
{"name": "LSVA Fee", "type": "service", "lst_price": 3.0}
)
cls.fee2 = cls.env["product.product"].create(
{"name": "Service Fee", "type": "service", "lst_price": 4.0}
)
cls.carrier = cls.env["delivery.carrier"].create(
{
"name": "Delivery",
"fixed_price": 10.0,
"product_id": cls.carrier_product.id,
"package_fee_ids": [
(0, 0, {"product_id": cls.fee1.id}),
(0, 0, {"product_id": cls.fee2.id}),
],
}
)
cls.wh = cls.env.ref("stock.warehouse0")
cls.sale = cls._create_sale()
cls._add_sale_carrier(cls.sale, cls.carrier)
cls._update_qty_in_location(
cls.wh.lot_stock_id,
cls.sale.order_line[0].product_id,
cls.sale.order_line[0].product_uom_qty,
)
cls._update_qty_in_location(
cls.wh.lot_stock_id,
cls.sale.order_line[1].product_id,
cls.sale.order_line[1].product_uom_qty,
)
cls.sale.action_confirm()
cls.pack1 = cls.env["stock.quant.package"].create({})
cls.pack2 = cls.env["stock.quant.package"].create({})
cls.sptype1 = cls.env["stock.package.type"].create({"name": "SPType 1"})
cls.sptype2 = cls.env["stock.package.type"].create({"name": "SPType 2"})
@classmethod
def _update_qty_in_location(cls, location, product, quantity):
quants = cls.env["stock.quant"]._gather(product, location, strict=True)
# this method adds the quantity to the current quantity, so get the diff
quantity -= sum(quants.mapped("quantity"))
cls.env["stock.quant"]._update_available_quantity(product, location, quantity)
@classmethod
def _create_sale(cls):
sale_form = Form(cls.env["sale.order"])
sale_form.partner_id = cls.partner
with mute_logger("odoo.tests.common.onchange"):
with sale_form.order_line.new() as line:
line.product_id = cls.product1
line.product_uom_qty = 10.0
with sale_form.order_line.new() as line:
line.product_id = cls.product2
line.product_uom_qty = 10.0
return sale_form.save()
@classmethod
def _add_sale_carrier(cls, sale, carrier):
delivery_wizard = Form(
cls.env["choose.delivery.carrier"].with_context(
default_order_id=sale.id, default_carrier_id=carrier.id
)
)
choose_delivery_carrier = delivery_wizard.save()
choose_delivery_carrier.button_confirm()
def test_package_fee_simple(self):
"""All stock moves processed at once"""
picking = self.sale.picking_ids
self.assertEqual(picking.state, "assigned")
picking.move_line_ids[0].result_package_id = self.pack1
picking.move_line_ids[0].qty_done = 10.0
picking.move_line_ids[1].result_package_id = self.pack2
picking.move_line_ids[1].qty_done = 10.0
picking._action_done()
self.assertEqual(picking.state, "done")
self.assertRecordValues(
self.sale.order_line,
[
{
"product_id": self.product1.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 1",
},
{
"product_id": self.product2.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 2",
},
{
"product_id": self.carrier_product.id,
"product_uom_qty": 1.0,
"price_unit": 10.0,
"name": "Delivery",
},
# additional package fee lines from here
{
"product_id": self.fee1.id,
# one unit per package
"product_uom_qty": 2.0,
"price_unit": 3.0,
"name": "LSVA Fee ({})".format(picking.name),
},
{
"product_id": self.fee2.id,
# one unit per package
"product_uom_qty": 2.0,
"price_unit": 4.0,
"name": "Service Fee ({})".format(picking.name),
},
],
)
def test_package_fee_simple_with_fiscal_position_tax(self):
"""All stock moves processed at once"""
# Creating taxes and fiscal position
tax_price_include = self.env["account.tax"].create(
{
"name": "10% inc",
"type_tax_use": "sale",
"amount_type": "percent",
"amount": 10,
"price_include": True,
"include_base_amount": True,
}
)
tax_price_include2 = self.env["account.tax"].create(
{
"name": "15% inc",
"type_tax_use": "sale",
"amount_type": "percent",
"amount": 15,
"price_include": True,
"include_base_amount": True,
}
)
tax_price_exclude = self.env["account.tax"].create(
{
"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 in fiscal position on fee2 product
self.fee1.taxes_id = tax_price_include2
self.fee2.taxes_id = tax_price_include
self.sale.fiscal_position_id = fiscal_position
picking = self.sale.picking_ids
self.assertEqual(picking.state, "assigned")
picking.move_line_ids[0].result_package_id = self.pack1
picking.move_line_ids[0].qty_done = 10.0
picking.move_line_ids[1].result_package_id = self.pack2
picking.move_line_ids[1].qty_done = 10.0
picking._action_done()
self.assertEqual(picking.state, "done")
so_line_fee1 = self.sale.order_line.filtered(
lambda l: l.product_id == self.fee1
)
so_line_fee2 = self.sale.order_line.filtered(
lambda l: l.product_id == self.fee2
)
self.assertNotEqual(so_line_fee1.tax_id[0], tax_price_exclude)
# only fee2 has tax tax_price_exclude due to defined fiscal position
self.assertEqual(so_line_fee2.tax_id[0], tax_price_exclude)
def test_package_fee_backorder(self):
"""Stock moves valided in 2 times using a backorder"""
picking = self.sale.picking_ids
self.assertEqual(picking.state, "assigned")
picking.move_line_ids[0].result_package_id = self.pack1
picking.move_line_ids[0].qty_done = 10.0
picking._action_done()
self.assertEqual(picking.state, "done")
backorder = picking.backorder_ids
self.assertRecordValues(
self.sale.order_line,
[
{
"product_id": self.product1.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 1",
},
{
"product_id": self.product2.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 2",
},
{
"product_id": self.carrier_product.id,
"product_uom_qty": 1.0,
"price_unit": 10.0,
"name": "Delivery",
},
# additional package fee lines from here
{
"product_id": self.fee1.id,
# one unit per package
"product_uom_qty": 1.0,
"price_unit": 3.0,
"name": "LSVA Fee ({})".format(picking.name),
},
{
"product_id": self.fee2.id,
# one unit per package
"product_uom_qty": 1.0,
"price_unit": 4.0,
"name": "Service Fee ({})".format(picking.name),
},
],
)
backorder.move_line_ids[0].result_package_id = self.pack2
backorder.move_line_ids[0].qty_done = 10.0
backorder._action_done()
self.assertEqual(backorder.state, "done")
self.assertRecordValues(
self.sale.order_line,
[
{
"product_id": self.product1.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 1",
},
{
"product_id": self.product2.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 2",
},
{
"product_id": self.carrier_product.id,
"product_uom_qty": 1.0,
"price_unit": 10.0,
"name": "Delivery",
},
# these lines have been added by the first picking
{
"product_id": self.fee1.id,
# one unit per package
"product_uom_qty": 1.0,
"price_unit": 3.0,
"name": "LSVA Fee ({})".format(picking.name),
},
{
"product_id": self.fee2.id,
# one unit per package
"product_uom_qty": 1.0,
"price_unit": 4.0,
"name": "Service Fee ({})".format(picking.name),
},
# new lines added for the backorder
{
"product_id": self.fee1.id,
# one unit per package
"product_uom_qty": 1.0,
"price_unit": 3.0,
"name": "LSVA Fee ({})".format(backorder.name),
},
{
"product_id": self.fee2.id,
# one unit per package
"product_uom_qty": 1.0,
"price_unit": 4.0,
"name": "Service Fee ({})".format(backorder.name),
},
],
)
def test_package_fee_pricelist(self):
"""Price set on a pricelist is used"""
# set pricelist prices for package fees
pricelist = self.sale.pricelist_id
fee1_price = 13.5
fee2_price = 17.2
self.env["product.pricelist.item"].create(
{
"pricelist_id": pricelist.id,
"product_id": self.fee1.id,
"applied_on": "0_product_variant",
"fixed_price": fee1_price,
}
)
self.env["product.pricelist.item"].create(
{
"pricelist_id": pricelist.id,
"product_id": self.fee2.id,
"applied_on": "0_product_variant",
"fixed_price": fee2_price,
}
)
picking = self.sale.picking_ids
self.assertEqual(picking.state, "assigned")
picking.move_line_ids[0].result_package_id = self.pack1
picking.move_line_ids[0].qty_done = 10.0
picking.move_line_ids[1].result_package_id = self.pack2
picking.move_line_ids[1].qty_done = 10.0
picking._action_done()
self.assertEqual(picking.state, "done")
self.assertRecordValues(
self.sale.order_line,
[
{
"product_id": self.product1.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 1",
},
{
"product_id": self.product2.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 2",
},
{
"product_id": self.carrier_product.id,
"product_uom_qty": 1.0,
"price_unit": 10.0,
"name": "Delivery",
},
# additional package fee lines from here
{
"product_id": self.fee1.id,
# one unit per package
"product_uom_qty": 2.0,
"price_unit": fee1_price,
"name": "LSVA Fee ({})".format(picking.name),
},
{
"product_id": self.fee2.id,
# one unit per package
"product_uom_qty": 2.0,
"price_unit": fee2_price,
"name": "Service Fee ({})".format(picking.name),
},
],
)
def test_package_no_package(self):
"""No packages, no fees"""
picking = self.sale.picking_ids
self.assertEqual(picking.state, "assigned")
picking.move_line_ids[0].qty_done = 10.0
picking.move_line_ids[1].qty_done = 10.0
picking.with_context(set_default_package=False)._action_done()
self.assertEqual(picking.state, "done")
self.assertRecordValues(
self.sale.order_line,
[
{
"product_id": self.product1.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 1",
},
{
"product_id": self.product2.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 2",
},
{
"product_id": self.carrier_product.id,
"product_uom_qty": 1.0,
"price_unit": 10.0,
"name": "Delivery",
},
],
)
def test_package_no_price(self):
"""No price on product, no fees"""
picking = self.sale.picking_ids
self.fee1.lst_price = 0.0
self.assertEqual(picking.state, "assigned")
picking.move_line_ids[0].result_package_id = self.pack1
picking.move_line_ids[0].qty_done = 10.0
picking.move_line_ids[1].result_package_id = self.pack2
picking.move_line_ids[1].qty_done = 10.0
picking._action_done()
self.assertEqual(picking.state, "done")
self.assertRecordValues(
self.sale.order_line,
[
{
"product_id": self.product1.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 1",
},
{
"product_id": self.product2.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 2",
},
{
"product_id": self.carrier_product.id,
"product_uom_qty": 1.0,
"price_unit": 10.0,
"name": "Delivery",
},
# only the Service Fees are added because there is
# no price on the LSVA (could be free for a customer
# based on a pricelist for instance)
{
"product_id": self.fee2.id,
# one unit per package
"product_uom_qty": 2.0,
"price_unit": 4.0,
"name": "Service Fee ({})".format(picking.name),
},
],
)
def test_package_with_type_no_fee_line(self):
"""Assign all fee lines to a package type, no fee line added to SO"""
self.carrier.package_fee_ids.write({"package_type_id": self.sptype1.id})
picking = self.sale.picking_ids
self.assertEqual(picking.state, "assigned")
picking.move_line_ids[0].result_package_id = self.pack1
picking.move_line_ids[0].qty_done = 10.0
picking.move_line_ids[1].result_package_id = self.pack2
picking.move_line_ids[1].qty_done = 10.0
picking._action_done()
self.assertEqual(picking.state, "done")
self.assertRecordValues(
self.sale.order_line,
[
{
"product_id": self.product1.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 1",
},
{
"product_id": self.product2.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 2",
},
{
"product_id": self.carrier_product.id,
"product_uom_qty": 1.0,
"price_unit": 10.0,
"name": "Delivery",
},
],
)
def test_package_with_type_one_fee_line(self):
"""Assign all fee lines to a package type, but one fee line added to SO"""
self.carrier.package_fee_ids[0].package_type_id = self.sptype1
self.carrier.package_fee_ids[1].package_type_id = self.sptype2
self.pack1.package_type_id = self.sptype1
picking = self.sale.picking_ids
self.assertEqual(picking.state, "assigned")
picking.move_line_ids[0].result_package_id = self.pack1
picking.move_line_ids[0].qty_done = 10.0
picking.move_line_ids[1].result_package_id = self.pack2
picking.move_line_ids[1].qty_done = 10.0
picking._action_done()
self.assertEqual(picking.state, "done")
self.assertRecordValues(
self.sale.order_line,
[
{
"product_id": self.product1.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 1",
},
{
"product_id": self.product2.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 2",
},
{
"product_id": self.carrier_product.id,
"product_uom_qty": 1.0,
"price_unit": 10.0,
"name": "Delivery",
},
# additional package fee lines from here
{
"product_id": self.fee1.id,
"product_uom_qty": 1.0,
"price_unit": 3.0,
"name": "LSVA Fee ({})".format(picking.name),
},
],
)
def test_package_with_type_one_fee_line_qty(self):
"""Assign all fee lines to a package type, but one fee line added to SO, qty=2"""
self.carrier.package_fee_ids[0].package_type_id = self.sptype1
self.carrier.package_fee_ids[1].package_type_id = self.sptype2
self.pack1.package_type_id = self.sptype1
self.pack2.package_type_id = self.sptype1
picking = self.sale.picking_ids
self.assertEqual(picking.state, "assigned")
picking.move_line_ids[0].result_package_id = self.pack1
picking.move_line_ids[0].qty_done = 10.0
picking.move_line_ids[1].result_package_id = self.pack2
picking.move_line_ids[1].qty_done = 10.0
picking._action_done()
self.assertEqual(picking.state, "done")
self.assertRecordValues(
self.sale.order_line,
[
{
"product_id": self.product1.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 1",
},
{
"product_id": self.product2.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 2",
},
{
"product_id": self.carrier_product.id,
"product_uom_qty": 1.0,
"price_unit": 10.0,
"name": "Delivery",
},
# additional package fee lines from here
{
"product_id": self.fee1.id,
"product_uom_qty": 2.0,
"price_unit": 3.0,
"name": "LSVA Fee ({})".format(picking.name),
},
],
)
def test_package_with_type_two_fee_line_qty(self):
"""Assign all fee lines to a package type, 2 fee lines added to SO"""
self.carrier.package_fee_ids[0].package_type_id = self.sptype1
self.carrier.package_fee_ids[1].package_type_id = self.sptype2
self.pack1.package_type_id = self.sptype1
self.pack2.package_type_id = self.sptype2
picking = self.sale.picking_ids
self.assertEqual(picking.state, "assigned")
picking.move_line_ids[0].result_package_id = self.pack1
picking.move_line_ids[0].qty_done = 10.0
picking.move_line_ids[1].result_package_id = self.pack2
picking.move_line_ids[1].qty_done = 10.0
picking._action_done()
self.assertEqual(picking.state, "done")
self.assertRecordValues(
self.sale.order_line,
[
{
"product_id": self.product1.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 1",
},
{
"product_id": self.product2.id,
"product_uom_qty": 10.0,
"price_unit": 1.0,
"name": "Product 2",
},
{
"product_id": self.carrier_product.id,
"product_uom_qty": 1.0,
"price_unit": 10.0,
"name": "Delivery",
},
# additional package fee lines from here
{
"product_id": self.fee1.id,
"product_uom_qty": 1.0,
"price_unit": 3.0,
"name": "LSVA Fee ({})".format(picking.name),
},
{
"product_id": self.fee2.id,
"product_uom_qty": 1.0,
"price_unit": 4.0,
"name": "Service Fee ({})".format(picking.name),
},
],
)