Debian Codegraph @

addons/sale_loyalty/tests/test_program_multi_company.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo.fields import Command
4 from odoo.tests import tagged
5
6 from odoo.addons.sale_loyalty.tests.common import TestSaleCouponCommon
7
8
9 @tagged('post_install', '-at_install')
10 class TestSaleCouponMultiCompany(TestSaleCouponCommon):
11
12 def setUp(self):
13 super(TestSaleCouponMultiCompany, self).setUp()
14
15 self.company_a = self.env.company
16 self.company_b = self.env['res.company'].create(dict(name="TEST"))
17
18 self.immediate_promotion_program_c2 = self.env['loyalty.program'].create({
19 'name': 'Buy A + 1 B, 1 B are free',
20 'trigger': 'auto',
21 'program_type': 'promotion',
22 'applies_on': 'current',
23 'company_id': self.company_b.id,
24 'rule_ids': [(0, 0, {
25 'product_ids': self.product_A,
26 'reward_point_amount': 1,
27 'reward_point_mode': 'order',
28 })],
29 'reward_ids': [(0, 0, {
30 'reward_type': 'product',
31 'reward_product_id': self.product_B.id,
32 'reward_product_qty': 1,
33 'required_points': 1,
34 })],
35 })
36
37 def _get_applicable_programs(self, order):
38 return self.env['loyalty.program'].browse(p.id for p in order._get_applicable_program_points())
39
40 def test_applicable_programs(self):
41
42 order = self.empty_order
43 order.write({'order_line': [
44 (0, False, {
45 'product_id': self.product_A.id,
46 'name': '1 Product A',
47 'product_uom_qty': 1.0,
48 }),
49 (0, False, {
50 'product_id': self.product_B.id,
51 'name': '2 Product B',
52 'product_uom_qty': 1.0,
53 })
54 ]})
55 order._update_programs_and_rewards()
56
57 self.assertNotIn(self.immediate_promotion_program_c2, self._get_applicable_programs(order))
58 self.assertNotIn(self.immediate_promotion_program_c2, order._get_applied_programs())
59
60 order_b = self.env["sale.order"].create({
61 'company_id': self.company_b.id,
62 'partner_id': order.partner_id.id,
63 })
64 order_b.write({'order_line': [
65 (0, False, {
66 'product_id': self.product_A.id,
67 'name': '1 Product A',
68 'product_uom_qty': 1.0,
69 }),
70 (0, False, {
71 'product_id': self.product_B.id,
72 'name': '2 Product B',
73 'product_uom_qty': 1.0,
74 })
75 ]})
76 self.assertNotIn(self.immediate_promotion_program, self._get_applicable_programs(order_b))
77 order_b._update_programs_and_rewards()
78 self.assertIn(self.immediate_promotion_program_c2, order_b._get_applied_programs())
79 self.assertNotIn(self.immediate_promotion_program, order_b._get_applied_programs())
80
81 def test_applicable_programs_on_branch(self):
82 # create a branch
83 branch_a = self.env['res.company'].create(
84 {'name': 'Branch A', 'parent_id': self.company_a.id}
85 )
86
87 # create an order
88 order = self.env['sale.order'].create(
89 {'order_line': [
90 Command.create({
91 'product_id': self.product_A.id,
92 'name': '1 Product A',
93 'product_uom_qty': 1.0,
94 }),
95 Command.create({
96 'product_id': self.product_B.id,
97 'name': '2 Product B',
98 'product_uom_qty': 1.0,
99 })
100 ],
101 'company_id': branch_a.id,
102 'partner_id': self.partner.id
103 }
104 )
105
106 order._update_programs_and_rewards()
107 self.assertIn(self.immediate_promotion_program, order._get_applied_programs())
108
109 def test_applicable_programs_confirm_on_branch(self):
110 # create a branch
111 self.env['loyalty.program'].search([]).write({'active': False})
112 branch_a = self.env['res.company'].create(
113 {'name': 'Branch A', 'parent_id': self.company_a.id}
114 )
115
116 LoyaltyProgram = self.env['loyalty.program']
117 LoyaltyProgram.create(LoyaltyProgram._get_template_values()['loyalty'])
118
119 self.sale_user.write({'company_ids': [Command.set((branch_a + self.company_a).ids)]})
120
121 # create an order
122 order = self.empty_order
123 order.update(
124 {
125 'order_line': [
126 Command.create({
127 'product_id': self.product_A.id,
128 }),
129 ],
130 'company_id': branch_a.id,
131 'partner_id': self.partner.id,
132 'user_id': self.sale_user.id
133 }
134 )
135
136 order.with_user(self.sale_user).with_company(branch_a.id).sudo(False).action_confirm()
137 self.assertEqual(order.state, 'sale')