Debian Codegraph @

addons/website_sale_loyalty/tests/test_website_sale_loyalty_delivery.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 HttpCase, tagged
5 from odoo.exceptions import ValidationError
6
7 from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT
8 from odoo.addons.website_sale.tests.common import MockRequest, WebsiteSaleCommon
9 from odoo.addons.payment import utils as payment_utils
10 from odoo.addons.website_sale_loyalty.controllers.cart import Cart
11 from odoo.addons.website_sale_loyalty.controllers.delivery import WebsiteSaleLoyaltyDelivery
12
13
14 @tagged('post_install', '-at_install')
15 class TestWebsiteSaleDelivery(HttpCase, WebsiteSaleCommon):
16
17 @classmethod
18 def setUpClass(cls):
19 super().setUpClass()
20 cls.Controller = WebsiteSaleLoyaltyDelivery()
21
22 # Disable mail logic
23 cls.env = cls.env['base'].with_context(**DISABLED_MAIL_CONTEXT).env
24 # Disable existing pricelists
25 cls.env['product.pricelist'].with_context(active_test=False).search([]).unlink()
26 # Disable existing reward programs
27 cls.env['loyalty.program'].search([]).active = False
28 # Remove taxes completely during the following tests.
29 cls.env.companies.account_sale_tax_id = False
30
31 cls.user_admin = cls.env.ref('base.user_admin')
32 cls.partner_admin = cls.user_admin.partner_id
33 cls.partner_admin.write(cls.dummy_partner_address_values)
34
35 cls.website2 = cls.env['website'].create({'name': 'website 2'})
36
37 cls.product_plumbus = cls.env['product.product'].create({
38 'name': "Plumbus",
39 'list_price': 100.0,
40 'type': 'consu',
41 'website_published': True,
42 })
43
44 cls.product_gift_card = cls.env['product.product'].create({
45 'name': 'TEST - Gift Card',
46 'list_price': 50,
47 'type': 'service',
48 'is_published': True,
49 })
50
51 gift_card_program = cls.env['loyalty.program'].create({
52 'name': 'Gift Cards',
53 'program_type': 'gift_card',
54 'applies_on': 'future',
55 'trigger': 'auto',
56 'rule_ids': [Command.create({
57 'reward_point_amount': 1,
58 'reward_point_mode': 'money',
59 'reward_point_split': True,
60 'product_ids': cls.product_gift_card.ids,
61 })],
62 'reward_ids': [Command.create({
63 'reward_type': 'discount',
64 'discount_mode': 'per_point',
65 'discount': 1,
66 'discount_applicability': 'order',
67 'required_points': 1,
68 'description': 'PAY WITH GIFT CARD',
69 })],
70 })
71
72 # Create a gift card to be used
73 cls.gift_card = cls.env['loyalty.card'].create({
74 'program_id': gift_card_program.id,
75 'points': 50000,
76 'code': '123456',
77 })
78
79 # Create a 50% discount on order code
80 cls.promo_discount_code = cls.env['loyalty.program'].create({
81 'name': "50% discount code",
82 'program_type': 'promo_code',
83 'trigger': 'with_code',
84 'applies_on': 'current',
85 'rule_ids': [Command.create({
86 'code': "test-50pc",
87 })],
88 'reward_ids': [Command.create({
89 'reward_type': 'discount',
90 'discount': 50.0,
91 'discount_mode': 'percent',
92 'discount_applicability': 'order',
93 })],
94 })
95
96 ewallet_program = cls.env['loyalty.program'].create({
97 'name': "eWallet",
98 'program_type': 'ewallet',
99 'applies_on': 'future',
100 'trigger': 'auto',
101 'reward_ids': [Command.create({
102 'description': "Pay with eWallet",
103 'reward_type': 'discount',
104 'discount_mode': 'per_point',
105 'discount': 1,
106 })],
107 })
108
109 cls.ewallet = cls.env['loyalty.card'].create({
110 'program_id': ewallet_program.id,
111 'partner_id': cls.partner_admin.id,
112 'points': 1000000,
113 })
114
115 delivery_product1, delivery_product2 = cls.env['product.product'].create([{
116 'name': "Delivery 1",
117 'invoice_policy': 'order',
118 'type': 'service',
119 }, {
120 'name': "Delivery 2",
121 'invoice_policy': 'order',
122 'type': 'service',
123 }])
124
125 cls.normal_delivery, cls.normal_delivery2 = cls.env['delivery.carrier'].create([{
126 'name': 'delivery1',
127 'fixed_price': 5,
128 'delivery_type': 'fixed',
129 'website_published': True,
130 'product_id': delivery_product1.id,
131 }, {
132 'name': 'delivery2',
133 'fixed_price': 10,
134 'delivery_type': 'fixed',
135 'website_published': True,
136 'product_id': delivery_product2.id,
137 }])
138
139 def create_program_with_code(self, code, website_id):
140 return self.env['loyalty.program'].create({
141 'name': "Discount delivery",
142 'program_type': 'promo_code',
143 'website_id': website_id.id,
144 'rule_ids': [Command.create({
145 'code': code,
146 'minimum_amount': 0,
147 })],
148 })
149
150 def test_shop_sale_gift_card_keep_delivery(self):
151 # Get admin user and set his preferred shipping method to normal delivery
152 # This test also tests that we can indeed pay delivery fees with gift cards/ewallet
153 self.partner_admin.property_delivery_carrier_id = self.normal_delivery
154 self.start_tour("/", 'shop_sale_loyalty_delivery', login='admin')
155
156 def test_shipping_discount(self):
157 """
158 Check display of shipping discount promotion on checkout,
159 combined with another reward (eWallet).
160 """
161 self.env['loyalty.program'].create({
162 'name': "Buy 3, get up to $6 discount on shipping!",
163 'program_type': 'promotion',
164 'applies_on': 'current',
165 'trigger': 'auto',
166 'rule_ids': [Command.create({
167 'minimum_qty': 3.0,
168 })],
169 'reward_ids': [Command.create({
170 'reward_type': 'shipping',
171 'discount_max_amount': 6.0,
172 })],
173 })
174 self.start_tour("/", 'check_shipping_discount', login="admin")
175
176 def test_update_shipping_after_discount(self):
177 """
178 Verify that after applying a discount code, any `free_over` shipping gets recalculated.
179 """
180 self.free_delivery.action_archive()
181 self.normal_delivery.write({'free_over': True, 'amount': 75.0})
182 self.start_tour("/shop", 'update_shipping_after_discount', login=self.user_admin.login)
183
184 def test_express_checkout_shipping_discount(self):
185 """
186 Check display of shipping discount promotion in express checkout form by ensuring is present
187 in the values returned to the form.
188 """
189 # Create a discount code
190 program = self.env['loyalty.program'].sudo().create({
191 'name': 'Free Shipping',
192 'program_type': 'promo_code',
193 'rule_ids': [
194 Command.create({
195 'code': "FREE",
196 'minimum_amount': 0,
197 })
198 ],
199 'reward_ids': [
200 Command.create({
201 'reward_type': 'shipping',
202 'discount_max_amount': 6.0,
203 })
204 ]
205 })
206
207 # Apply discount
208 self.cart._try_apply_code("FREE")
209 self.cart._apply_program_reward(program.reward_ids, program.coupon_ids)
210
211 with MockRequest(self.env, sale_order_id=self.cart.id, website=self.website):
212 result = self.Controller.shop_set_delivery_method(self.normal_delivery2.id)
213 self.assertEqual(result['delivery_discount_minor_amount'], -600)
214
215 def test_express_checkout_does_not_count_delivery_discount_in_payment_values(self):
216 """Test that the amount to pay does not include the free delivery amount in express
217 checkout."""
218 program = self.env['loyalty.program'].sudo().create({
219 'name': 'Discount delivery',
220 'program_type': 'promo_code',
221 'rule_ids': [Command.create({
222 'code': "FREE",
223 'minimum_amount': 0,
224 })],
225 'reward_ids': [Command.create({
226 'reward_type': 'shipping',
227 'discount_max_amount': 2.0,
228 })]
229 })
230 amount_without_delivery = payment_utils.to_minor_currency_units(
231 self.cart.amount_total, self.cart.currency_id
232 )
233 self.cart.set_delivery_line(self.normal_delivery, self.normal_delivery.fixed_price)
234 self.cart._try_apply_code("FREE")
235 self.cart._apply_program_reward(program.reward_ids, program.coupon_ids)
236 with MockRequest(self.env, sale_order_id=self.cart.id, website=self.website):
237 payment_values = Cart()._get_express_shop_payment_values(self.cart)
238
239 self.assertEqual(payment_values['minor_amount'], amount_without_delivery)
240
241 def test_prevent_unarchive_when_conflicting_active_program_exists_on_same_website(self):
242 """Unarchiving a program should fail if another active program already has the same
243 rule code on the same website."""
244 program = self.create_program_with_code("FREE", self.website)
245 program.action_archive()
246 self.create_program_with_code("FREE", self.website)
247
248 with self.assertRaises(ValidationError):
249 program.action_unarchive()
250
251 def test_unarchive_when_conflicting_active_program_exists_on_different_website(self):
252 """Unarchiving a program should succeed when another active program already has the
253 same rule code on a different website."""
254 program = self.create_program_with_code("FREE", self.website)
255 program.action_archive()
256
257 self.create_program_with_code("FREE", self.website2)
258 program.action_unarchive()
259
260 def test_prevent_unarchive_when_batch_contains_duplicate_codes_on_same_website(self):
261 """Unarchiving multiple programs at once should fail if they share the same rule code
262 on the same website."""
263 program1 = self.create_program_with_code("FREE", self.website)
264 program1.action_archive()
265 program2 = self.create_program_with_code("FREE", self.website)
266 program2.action_archive()
267
268 with self.assertRaises(ValidationError):
269 (program1 + program2).action_unarchive()