Purpose ======= Currently we are updating the coupon rewards on the fly when creating/modifying/deleting a sales order line. To avoid multiple searches on res_partners and product to know whether the customer/product is valid on the sale coupon program, we store the applicable customers and products on the program according to the domain defined on it. This brings 2 issues: - The customers/products stored on the program are not updated (Was the original spec) - The checks are numerous and maybe overkill for the given results Specification ============= - Don't recompute the reward lines on SO modifications. Add a button to compute the line by hand and make an automatic compute on the SO validation or on the page shop/payment. - When recomputing the reward lines, make a search on the res_partners and products instead. Now that the action is not triggered so often we can afford to make a search. - Don't use the fields rule_partners_domain and rule_products_domain. This should be removed in master. - The field is_public_included is useless now. And we decided to make the coupon available for all the partners if they fit the domain on the program.
97 lines
3.7 KiB
Python
97 lines
3.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from openerp.addons.sale_coupon.tests.common import TestSaleCouponCommon
|
|
from odoo.exceptions import UserError
|
|
|
|
|
|
class TestProgramWithCodeOperations(TestSaleCouponCommon):
|
|
# Test the basic operation (apply_coupon) on an coupon program on which we should
|
|
# apply the reward when the code is correct or remove the reward automatically when the reward is
|
|
# not valid anymore.
|
|
|
|
def test_program_basic_operation_coupon_code(self):
|
|
# Test case: Generate a coupon for my customer, and add a reward then remove it automatically
|
|
|
|
self.code_promotion_program.reward_type = 'discount'
|
|
|
|
self.env['sale.coupon.generate'].with_context(active_id=self.code_promotion_program.id).create({
|
|
'generation_type': 'nbr_customer',
|
|
'partners_domain': "[('id', 'in', [%s])]" % (self.steve.id),
|
|
}).generate_coupon()
|
|
|
|
# Test the valid code on a wrong sales order
|
|
wrong_partner_order = self.env['sale.order'].create({
|
|
'partner_id': self.env.ref('base.res_partner_1').id,
|
|
})
|
|
with self.assertRaises(UserError):
|
|
self.env['sale.coupon.apply.code'].with_context(active_id=wrong_partner_order.id).create({
|
|
'coupon_code': self.code_promotion_program.coupon_ids.code
|
|
}).process_coupon()
|
|
|
|
# Test now on a valid sales order
|
|
order = self.empty_order
|
|
order.write({'order_line': [
|
|
(0, False, {
|
|
'product_id': self.product_A.id,
|
|
'name': '1 Product A',
|
|
'product_uom': self.uom_unit.id,
|
|
'product_uom_qty': 1.0,
|
|
})
|
|
]})
|
|
self.env['sale.coupon.apply.code'].with_context(active_id=order.id).create({
|
|
'coupon_code': self.code_promotion_program.coupon_ids.code
|
|
}).process_coupon()
|
|
order.recompute_coupon_lines()
|
|
self.assertEqual(len(order.order_line.ids), 2)
|
|
|
|
# Remove the product A from the sale order
|
|
order.write({'order_line': [(2, order.order_line[0].id, False)]})
|
|
order.recompute_coupon_lines()
|
|
self.assertEqual(len(order.order_line.ids), 0)
|
|
|
|
def test_on_next_order_reward_promo_program(self):
|
|
# Test case: Apply a program that generates a coupon, which is used afterward
|
|
|
|
self.immediate_promotion_program.write({
|
|
'promo_applicability': 'on_next_order',
|
|
'promo_code_usage': 'code_needed',
|
|
'promo_code': 'bleurk',
|
|
})
|
|
|
|
order = self.empty_order
|
|
order.write({'order_line': [
|
|
(0, False, {
|
|
'product_id': self.product_A.id,
|
|
'name': '1 Product A',
|
|
'product_uom': self.uom_unit.id,
|
|
'product_uom_qty': 1.0,
|
|
})
|
|
]})
|
|
|
|
self.env['sale.coupon.apply.code'].with_context(active_id=order.id).create({
|
|
'coupon_code': 'bleurk'
|
|
}).process_coupon()
|
|
# Ensure that the coupon is correctly generated
|
|
order.recompute_coupon_lines()
|
|
self.assertEqual(len(self.immediate_promotion_program.coupon_ids.ids), 1)
|
|
|
|
generated_coupon = self.immediate_promotion_program.coupon_ids[0]
|
|
order = self.env['sale.order'].create({
|
|
'partner_id': self.steve.id
|
|
})
|
|
order.write({'order_line': [
|
|
(0, False, {
|
|
'product_id': self.product_B.id,
|
|
'name': '1 Product B',
|
|
'product_uom': self.uom_unit.id,
|
|
'product_uom_qty': 1.0,
|
|
})
|
|
]})
|
|
self.env['sale.coupon.apply.code'].with_context(active_id=order.id).create({
|
|
'coupon_code': generated_coupon.code
|
|
})
|
|
# Ensure that the generated coupon is valid
|
|
order.recompute_coupon_lines()
|
|
self.assertEqual(len(order.order_line.ids), 2)
|