Skip to content

Commit

Permalink
[MIG] website_sale_coupon_selection_wizard: Migration to version 16.0
Browse files Browse the repository at this point in the history
TT44379
  • Loading branch information
pilarvargas-tecnativa committed Mar 13, 2024
1 parent d11817a commit 401026a
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 175 deletions.
28 changes: 15 additions & 13 deletions website_sale_coupon_selection_wizard/README.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
===============================
Coupons Selection for eCommerce
Loyalty Selection for eCommerce
===============================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:94c44400c8e0661ca641d2c4de75be1950d9d1107237aa4189128911a9d1b019
!! source digest: sha256:d3a541663fd3180e254392c60810609dea963dabe291610a5d3e57c7df4f2507
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand All @@ -17,19 +17,20 @@ Coupons Selection for eCommerce
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsale--promotion-lightgray.png?logo=github
:target: https://github.com/OCA/sale-promotion/tree/15.0/website_sale_coupon_selection_wizard
:target: https://github.com/OCA/sale-promotion/tree/16.0/website_sale_coupon_selection_wizard
:alt: OCA/sale-promotion
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/sale-promotion-15-0/sale-promotion-15-0-website_sale_coupon_selection_wizard
:target: https://translation.odoo-community.org/projects/sale-promotion-16-0/sale-promotion-16-0-website_sale_coupon_selection_wizard
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/sale-promotion&target_branch=15.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/sale-promotion&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows to apply multi-gift mult-criteria promotion with a fancy wizard on
the eCommerce.
This module makes suggestions for promotions whose rules include a product added to the
cart. It also allows you to configure and apply these promotions with an elegant eCommerce
wizard.

**Table of contents**

Expand All @@ -49,22 +50,22 @@ Option 1:

Option 2:

#. Add a product in the promotion criteria.
#. A cart hint will show up.
#. Add a product in the promotion rules.
#. A cart suggestion will appear.
#. Add the promotion and configure the options.

Option 3:

#. Add a promotion code.
#. If the promotion needs to be configured (i.e.: select the present)
#. Select a suggested promotion in the shopping cart from a list of "Suggested promotions".
#. Add the promotion and configure the options.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/sale-promotion/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/sale-promotion/issues/new?body=module:%20website_sale_coupon_selection_wizard%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/sale-promotion/issues/new?body=module:%20website_sale_coupon_selection_wizard%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand All @@ -82,6 +83,7 @@ Contributors
* `Tecnativa <https://www.tecnativa.com>`_:

* David Vidal
* Pilar Vargas

Maintainers
~~~~~~~~~~~
Expand All @@ -104,6 +106,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-chienandalu|

This module is part of the `OCA/sale-promotion <https://github.com/OCA/sale-promotion/tree/15.0/website_sale_coupon_selection_wizard>`_ project on GitHub.
This module is part of the `OCA/sale-promotion <https://github.com/OCA/sale-promotion/tree/16.0/website_sale_coupon_selection_wizard>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
20 changes: 9 additions & 11 deletions website_sale_coupon_selection_wizard/__manifest__.py
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": "Loyalty Selection for eCommerce",
"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_coupon_selection_wizard/static/src/js/*",
]
},
}
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
51 changes: 24 additions & 27 deletions website_sale_coupon_selection_wizard/controllers/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
# 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):
def _get_sale_loyalty_reward_wizard(self, order, program):
wizard = (

Check warning on line 11 in website_sale_coupon_selection_wizard/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/main.py#L11

Added line #L11 was not covered by tests
request.env["sale.loyalty.reward.wizard"]
.with_context(active_id=order.id)
.create({"selected_reward_id": program.reward_ids[:1].id})
)
return wizard.id

Check warning on line 16 in website_sale_coupon_selection_wizard/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/main.py#L16

Added line #L16 was not covered by tests

@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()

Check warning on line 22 in website_sale_coupon_selection_wizard/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/main.py#L22

Added line #L22 was not covered by tests
if not program or not program.active or not program.is_published:
return

Check warning on line 24 in website_sale_coupon_selection_wizard/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/main.py#L24

Added line #L24 was not covered by tests
# Prevent to apply a promotion to a processed order
Expand All @@ -19,42 +28,30 @@ def promotion_program_apply(self, program_id, **kwargs):
request.session["sale_order_id"] = None
order = request.website.sale_get_order()

Check warning on line 29 in website_sale_coupon_selection_wizard/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/main.py#L28-L29

Added lines #L28 - L29 were not covered by tests
# We won't apply it twice
if program in (order.no_code_promo_program_ids | order.code_promo_program_id):
if program in order._get_reward_programs():
return

Check warning on line 32 in website_sale_coupon_selection_wizard/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/main.py#L32

Added line #L32 was not covered by tests
# 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)
# 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")

Check warning on line 36 in website_sale_coupon_selection_wizard/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/main.py#L34-L36

Added lines #L34 - L36 were not covered by tests

@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")

Check warning on line 43 in website_sale_coupon_selection_wizard/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/main.py#L40-L43

Added lines #L40 - L43 were not covered by tests
if promotion:
response.qcontext["promotion_id"] = (
request.env["coupon.program"].sudo().browse(promotion)
)
program_id = request.env["loyalty.program"].browse(promotion)
order_id = request.env["sale.order"].browse(order)
wizard_id = self._get_sale_loyalty_reward_wizard(order_id, program_id)
wiz = request.env["sale.loyalty.reward.wizard"].browse(wizard_id)
mandatory_program_options = wiz.loyalty_rule_line_ids
response.qcontext["promotion_id"] = program_id
response.qcontext["order_id"] = order_id
response.qcontext["mandatory_program_options"] = mandatory_program_options

Check warning on line 52 in website_sale_coupon_selection_wizard/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/main.py#L45-L52

Added lines #L45 - L52 were not covered by tests
if error:
request.session["error_promo_code"] = error
return response

Check warning on line 55 in website_sale_coupon_selection_wizard/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/main.py#L54-L55

Added lines #L54 - L55 were not covered by tests

@route(["/promotions/dismiss"], type="http", auth="public", website=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo.http import request, route

from odoo.addons.website_sale_coupon_page.controllers.main import WebsiteSale
from odoo.addons.website_sale_loyalty_page.controllers.main import WebsiteSale


class CouponPage(WebsiteSale):
Expand All @@ -16,13 +16,8 @@ def promotion(self, **post):
promo_values = response.qcontext.get("promos", [])
for promo_dict in promo_values:
promo_dict["applicable"] = False
promo = request.env["coupon.program"].sudo().browse(promo_dict["id"])
if (
promo in (order.no_code_promo_program_ids | order.code_promo_program_id)
) or (
promo
not in order.sudo()._available_multi_criteria_multi_gift_programs()
):
promo = request.env["loyalty.program"].sudo().browse(promo_dict["id"])
if promo not in order.sudo()._get_available_programs():
continue
promo_dict["applicable"] = True

Check warning on line 22 in website_sale_coupon_selection_wizard/controllers/promotion_page.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/promotion_page.py#L22

Added line #L22 was not covered by tests
return response
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# 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 CouponSelectionWizardController(WebsiteSale):
@route(
"/website_sale_coupon_selection_wizard/apply",
type="json",
Expand All @@ -19,24 +17,37 @@ def apply_promotion_public(
self, program_id, sale_order_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 = request.env["sale.order"].browse(sale_order_id)
program = request.env["loyalty.program"].browse(program_id)
wizard_id = self._get_sale_loyalty_reward_wizard(order, program)
wiz = request.env["sale.loyalty.reward.wizard"].browse(wizard_id)
reward_id = reward_line_options.get("reward", False)
wiz.selected_reward_id = int(reward_id) or (

Check warning on line 25 in website_sale_coupon_selection_wizard/controllers/promotion_wizard.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/promotion_wizard.py#L20-L25

Added lines #L20 - L25 were not covered by tests
program.reward_ids.id if len(program.reward_ids) == 1 else False
)
if error:
request.session["error_promo_code"] = error
if wiz.selected_reward_id:
reward = wiz.selected_reward_id

Check warning on line 29 in website_sale_coupon_selection_wizard/controllers/promotion_wizard.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/promotion_wizard.py#L29

Added line #L29 was not covered by tests
for product, qty in promotion_lines.items():
line = wiz.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

Check warning on line 35 in website_sale_coupon_selection_wizard/controllers/promotion_wizard.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/promotion_wizard.py#L35

Added line #L35 was not covered by tests
if not qty:
continue
line.units_to_include = qty

Check warning on line 38 in website_sale_coupon_selection_wizard/controllers/promotion_wizard.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/promotion_wizard.py#L37-L38

Added lines #L37 - L38 were not covered by tests
if reward.reward_type == "product":
reward_products = reward.reward_product_ids

Check warning on line 40 in website_sale_coupon_selection_wizard/controllers/promotion_wizard.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/promotion_wizard.py#L40

Added line #L40 was not covered by tests
if len(reward_products) == 1:
wiz.selected_product_id = reward_products.id

Check warning on line 42 in website_sale_coupon_selection_wizard/controllers/promotion_wizard.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/promotion_wizard.py#L42

Added line #L42 was not covered by tests
else:
wiz.selected_product_id = int(

Check warning on line 44 in website_sale_coupon_selection_wizard/controllers/promotion_wizard.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/promotion_wizard.py#L44

Added line #L44 was not covered by tests
reward_line_options.get("reward_product")
)
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)

Check warning on line 53 in website_sale_coupon_selection_wizard/controllers/promotion_wizard.py

View check run for this annotation

Codecov / codecov/patch

website_sale_coupon_selection_wizard/controllers/promotion_wizard.py#L47-L53

Added lines #L47 - L53 were not covered by tests

@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)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* `Tecnativa <https://www.tecnativa.com>`_:

* David Vidal
* Pilar Vargas
5 changes: 3 additions & 2 deletions website_sale_coupon_selection_wizard/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
This module allows to apply multi-gift mult-criteria promotion with a fancy wizard on
the eCommerce.
This module makes suggestions for promotions whose rules include a product added to the
cart. It also allows you to configure and apply these promotions with an elegant eCommerce
wizard.
8 changes: 4 additions & 4 deletions website_sale_coupon_selection_wizard/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Option 1:

Option 2:

#. Add a product in the promotion criteria.
#. A cart hint will show up.
#. Add a product in the promotion rules.
#. A cart suggestion will appear.
#. Add the promotion and configure the options.

Option 3:

#. Add a promotion code.
#. If the promotion needs to be configured (i.e.: select the present)
#. Select a suggested promotion in the shopping cart from a list of "Suggested promotions".
#. Add the promotion and configure the options.
Loading

0 comments on commit 401026a

Please sign in to comment.