Skip to content

Commit

Permalink
Merge PR #146 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Aug 31, 2023
2 parents 0c52630 + 9cea208 commit a0ea4d4
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions sale_promotion_discount_in_field/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models
from . import wizard
1 change: 1 addition & 0 deletions sale_promotion_discount_in_field/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"version": "14.0.1.0.1",
"summary": "Put promotion discount into discount field",
"author": "Ooops, Cetmix, Odoo Community Association (OCA)",
"maintainers": ["CetmixGitDrone", "Volodiay622"],
"license": "AGPL-3",
"category": "Sales Management",
"website": "https://github.com/OCA/sale-promotion",
Expand Down
1 change: 1 addition & 0 deletions sale_promotion_discount_in_field/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"This module should be renamed sale_coupon_discount_in_field in future migrations for naming consistency."
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,56 @@ def test_program_reward_discount_line_on_next_order_no_code_needed(self):
1,
"You should get a coupon for you next order that will offer 10% discount",
)

def _apply_coupon(self, order, coupon_code):
return self.env["sale.coupon.apply.code"].apply_coupon(order, coupon_code)

def test_program_reward_discount_line_with_applicability(self):
coupon_code = "test_coupon"
# Create a program with specific conditions
program = self.env["coupon.program"].create(
{
"name": "10% reduction program",
"promo_code_usage": "no_code_needed",
"reward_type": "discount_line",
"program_type": "promotion_program",
"discount_type": "percentage",
"discount_percentage": 10.0,
"active": True,
"discount_apply_on": "on_order",
"promo_code": coupon_code,
"promo_applicability": "on_current_order",
"maximum_use_number": 1,
}
)

# Test when error_status is empty (coupon is correct and exists).
error_status = program._check_promo_code(self.order, coupon_code)
self.assertFalse(error_status)

self.assertEqual(
self._apply_coupon(self.order, "wrong_coupon"),
{"not_found": "This coupon is invalid (wrong_coupon)."},
msg="coupon must not be found",
)

# Apply the program
result = self._apply_coupon(self.order, coupon_code)
self.assertEqual(
self.order.code_promo_program_id,
program,
msg="program must be added to order",
)
self.assertEqual(
self.order.amount_total,
2475.0,
"The order total with programs should be 2475.00",
)
self.assertEqual(result, {}, msg="Error must not occur")

# Try to use the applied coupon
error_status = program._check_promo_code(self.order, coupon_code)
result = self._apply_coupon(self.order, coupon_code)
self.assertEqual(
result, error_status, msg="The promo code is already applied on this order"
)
1 change: 1 addition & 0 deletions sale_promotion_discount_in_field/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import sale_coupon_apply_code
25 changes: 25 additions & 0 deletions sale_promotion_discount_in_field/wizard/sale_coupon_apply_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from odoo import models
from odoo.osv import expression


class SaleCouponApplyCode(models.TransientModel):
_inherit = "sale.coupon.apply.code"

def apply_coupon(self, order, coupon_code):
# OVERRIDE to apply the program to the order without reward lines
program_domain = order._get_coupon_program_domain()
additional_conditions = [
("promo_code", "=", coupon_code),
("reward_type", "=", "discount_line"),
("promo_applicability", "!=", "on_next_order"),
]
program_domain = expression.AND([program_domain, additional_conditions])
program = self.env["coupon.program"].search(program_domain)
if program:
error_status = program._check_promo_code(order, coupon_code)
if not error_status:
order._create_reward_line(program)
order.code_promo_program_id = program
else:
return super().apply_coupon(order, coupon_code)
return error_status

0 comments on commit a0ea4d4

Please sign in to comment.