-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIG] website_sale_coupon_selection_wizard: Migration to version 16.0
TT44379
- Loading branch information
1 parent
eb6b09c
commit 9f50369
Showing
20 changed files
with
683 additions
and
246 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
setup/website_sale_coupon_selection_wizard/odoo/addons/website_sale_coupon_selection_wizard
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...website_sale_loyalty_suggestion_wizard/odoo/addons/website_sale_loyalty_suggestion_wizard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../website_sale_loyalty_suggestion_wizard |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,27 @@ | ||
# Copyright 2021 Tecnativa - David Vidal | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
{ | ||
"name": "Coupons Selection for eCommerce", | ||
"summary": "Allows to apply and configure promotions directly from the website", | ||
"version": "15.0.1.0.0", | ||
"name": "Website Sale Loyalty Suggestion Wizard", | ||
"summary": "Suggests promotions and allows you to configure and apply these " | ||
"promotions directly from the website", | ||
"version": "16.0.1.0.0", | ||
"development_status": "Beta", | ||
"category": "eCommerce", | ||
"website": "https://github.com/OCA/sale-promotion", | ||
"author": "Tecnativa, Odoo Community Association (OCA)", | ||
"maintainers": ["chienandalu"], | ||
"license": "AGPL-3", | ||
"depends": [ | ||
"sale_coupon_selection_wizard", | ||
"sale_coupon_order_suggestion", | ||
"website_sale_coupon_page", | ||
"sale_loyalty", | ||
"sale_loyalty_order_suggestion", | ||
"website_sale_loyalty_page", | ||
], | ||
"data": ["templates/promotion_templates.xml"], | ||
"data": ["templates/promotion_templates.xml", "templates/wizard_templates.xml"], | ||
"assets": { | ||
"web.assets_frontend": [ | ||
"/website_sale_coupon_selection_wizard/static/src/scss/" | ||
"website_sale_coupon_selection.scss", | ||
"/sale_coupon_selection_wizard/static/src/js/" | ||
"coupon_selection_wizard_mixin.js", | ||
"/website_sale_coupon_selection_wizard/static/src/js/" | ||
"website_sale_coupon_selection_wizard.js", | ||
"/website_sale_loyalty_suggestion_wizard/static/src/scss/" | ||
"website_sale_loyalty_suggestion_wizard.scss", | ||
"/website_sale_loyalty_suggestion_wizard/static/src/js/*", | ||
] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from . import coupon_page | ||
from . import promotion_page | ||
from . import main | ||
from . import promotion_wizard |
61 changes: 32 additions & 29 deletions
61
website_sale_loyalty_suggestion_wizard/controllers/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,66 @@ | ||
# Copyright 2021 Tecnativa - David Vidal | ||
# Copyright 2024 Tecnativa - Pilar Vargas | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
from odoo.http import request, route | ||
|
||
from odoo.addons.website_sale.controllers.main import WebsiteSale | ||
|
||
|
||
class WebsiteSaleCouponWizard(WebsiteSale): | ||
class WebsiteSaleLoyaltySuggestionWizard(WebsiteSale): | ||
def _get_sale_loyalty_reward_wizard(self, order, program): | ||
wizard = ( | ||
request.env["sale.loyalty.reward.wizard"] | ||
.with_context(active_id=order.id) | ||
.sudo() | ||
.create({"selected_reward_id": program.reward_ids[:1].id}) | ||
) | ||
return wizard | ||
|
||
@route( | ||
["/promotions/<int:program_id>/apply"], type="http", auth="public", website=True | ||
) | ||
def promotion_program_apply(self, program_id, **kwargs): | ||
program = request.env["coupon.program"].sudo().browse(program_id).exists() | ||
program = request.env["loyalty.program"].sudo().browse(program_id).exists() | ||
request.session.pop("wizard_id", None) | ||
if not program or not program.active or not program.is_published: | ||
return | ||
return request.redirect("/shop/cart") | ||
# Prevent to apply a promotion to a processed order | ||
order = request.website.sale_get_order() | ||
if order and order.state != "draft": | ||
request.session["sale_order_id"] = None | ||
order = request.website.sale_get_order() | ||
# We won't apply it twice | ||
if program in (order.no_code_promo_program_ids | order.code_promo_program_id): | ||
return | ||
# If the promotion is directly applicable (promotion code), just apply without | ||
# further ado. | ||
if program.promo_code_usage == "code_needed" and ( | ||
program not in order.sudo()._available_multi_criteria_multi_gift_programs() | ||
): | ||
return self.pricelist(program.promo_code) | ||
if program in order._get_reward_programs(): | ||
return request.redirect("/shop/cart") | ||
# Let's inject some context into the view | ||
request.session["promotion_id"] = program.id | ||
request.session["order_id"] = order.id | ||
return request.redirect("/shop/cart") | ||
|
||
@route() | ||
def pricelist(self, promo, **post): | ||
"""When applying a configurable promotion code, we'll offer the customer | ||
to configure it.""" | ||
if promo: | ||
order = request.website.sale_get_order() | ||
program = ( | ||
request.env["coupon.program"] | ||
.sudo() | ||
.search([("promo_code", "=", promo)]) | ||
) | ||
if program in order.sudo()._available_multi_criteria_multi_gift_programs(): | ||
request.session["promotion_id"] = program.id | ||
return request.redirect("/shop/cart") | ||
return super().pricelist(promo) | ||
|
||
@route() | ||
def cart(self, **post): | ||
error = request.session.get("error_promo_code") | ||
response = super().cart(**post) | ||
promotion = request.session.get("promotion_id") | ||
order = request.session.get("sale_order_id") | ||
if promotion: | ||
response.qcontext["promotion_id"] = ( | ||
request.env["coupon.program"].sudo().browse(promotion) | ||
program_id = request.env["loyalty.program"].sudo().browse(promotion) | ||
order_id = request.env["sale.order"].browse(order) | ||
wizard_id = self._get_sale_loyalty_reward_wizard(order_id, program_id) | ||
mandatory_program_options = ( | ||
response.qcontext.get("mandatory_program_options") | ||
or wizard_id.loyalty_rule_line_ids | ||
) | ||
response.qcontext["promotion_id"] = program_id | ||
response.qcontext["order_id"] = order_id | ||
response.qcontext["mandatory_program_options"] = mandatory_program_options | ||
if error: | ||
request.session["error_promo_code"] = error | ||
return response | ||
|
||
@route(["/promotions/dismiss"], type="http", auth="public", website=True) | ||
def promotion_in_cart_dismiss(self, **kw): | ||
request.session.pop("promotion_id", None) | ||
request.session.pop("error_promo_code", None) | ||
request.session.pop("wizard_id", None) | ||
return request.redirect("/shop/cart") | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 61 additions & 24 deletions
85
website_sale_loyalty_suggestion_wizard/controllers/promotion_wizard.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,79 @@ | ||
# Copyright 2021 Tecnativa - David Vidal | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
from odoo import _ | ||
from odoo.exceptions import ValidationError | ||
from odoo.http import request, route | ||
|
||
from odoo.addons.sale_coupon_selection_wizard.controllers.main import ( | ||
CouponSelectionWizardController, | ||
) | ||
from odoo.addons.website_sale_loyalty.controllers.main import WebsiteSale | ||
|
||
|
||
class CouponSelectionWizardController(CouponSelectionWizardController): | ||
class WebsiteSaleLoyaltySuggestionWizardController(WebsiteSale): | ||
def _process_promotion_lines(self, wizard_id, promotion_lines): | ||
for product, qty in promotion_lines.items(): | ||
line = wizard_id.loyalty_rule_line_ids.filtered( | ||
lambda x: x.product_id.id == int(product) | ||
) | ||
if len(promotion_lines) == 1: | ||
qty = line.units_required - line.units_included | ||
if not qty: | ||
continue | ||
line.units_to_include = qty | ||
|
||
def _process_reward_line_options(self, wizard_id, reward_line_options): | ||
reward_id = wizard_id.selected_reward_id | ||
if reward_id.reward_type == "product": | ||
reward_products = reward_id.reward_product_ids | ||
if len(reward_products) == 1: | ||
wizard_id.selected_product_id = reward_products.id | ||
else: | ||
wizard_id.selected_product_id = ( | ||
int(reward_line_options.get("selected_product_ids", False)[0]) | ||
or wizard_id.selected_product_id.id | ||
) | ||
|
||
@route( | ||
"/website_sale_loyalty_suggestion_wizard/get_defaults", | ||
type="json", | ||
auth="public", | ||
methods=["POST"], | ||
) | ||
def get_default_products(self): | ||
program_id = ( | ||
request.env["loyalty.program"] | ||
.sudo() | ||
.browse(request.session.get("promotion_id")) | ||
) | ||
order_id = request.env["sale.order"].browse( | ||
request.session.get("sale_order_id") | ||
) | ||
wiz = self._get_sale_loyalty_reward_wizard(order_id, program_id) | ||
return wiz.selected_product_id.ids | ||
|
||
@route( | ||
"/website_sale_coupon_selection_wizard/apply", | ||
"/website_sale_loyalty_suggestion_wizard/apply", | ||
type="json", | ||
auth="public", | ||
methods=["POST"], | ||
) | ||
def apply_promotion_public( | ||
self, program_id, sale_order_id, promotion_lines, reward_line_options, **kw | ||
self, program_id, promotion_lines, reward_line_options, **kw | ||
): | ||
"""Frontend controller that wraps common methods and handles errors properly""" | ||
error, sale_form, program = self._try_to_apply_promotion( | ||
program_id, sale_order_id, promotion_lines, reward_line_options, **kw | ||
order_id = request.env["sale.order"].browse( | ||
request.session.get("sale_order_id") | ||
) | ||
if error: | ||
request.session["error_promo_code"] = error | ||
program_id = request.env["loyalty.program"].sudo().browse(program_id) | ||
wiz = self._get_sale_loyalty_reward_wizard(order_id, program_id) | ||
reward_id = reward_line_options.get("reward_id", False) | ||
wiz.selected_reward_id = int(reward_id) or ( | ||
program_id.reward_ids.id if len(program_id.reward_ids) == 1 else False | ||
) | ||
if wiz.selected_reward_id: | ||
self._process_promotion_lines(wiz, promotion_lines) | ||
self._process_reward_line_options(wiz, reward_line_options) | ||
try: | ||
wiz.action_apply() | ||
except ValidationError as e: | ||
request.session["error_promo_code"] = str(e) | ||
return | ||
# Once checked write the new lines and force the code if the promo has one | ||
order = sale_form.save() | ||
promo_applied = self._apply_promotion(order, program, reward_line_options) | ||
if not promo_applied: | ||
request.session["error_promo_code"] = _( | ||
"This promotion can't be applied to this order" | ||
) | ||
request.session.pop("promotion_id", None) | ||
request.session.pop("error_promo_code", None) | ||
|
||
@route(website=True) | ||
def configure_promotion(self, program_id, **kw): | ||
if not self._get_order(kw.get("sale_order_id")): | ||
kw["sale_order_id"] = request.website.sale_get_order(force_create=True) | ||
return super().configure_promotion(program_id, **kw) |
Oops, something went wrong.