diff --git a/website_sale_restrict_sepa_dd/__init__.py b/website_sale_restrict_sepa_dd/__init__.py index 7fbcfccd..4ece112b 100644 --- a/website_sale_restrict_sepa_dd/__init__.py +++ b/website_sale_restrict_sepa_dd/__init__.py @@ -2,3 +2,4 @@ # # SPDX-License-Identifier: AGPL-3.0-or-later from . import models +from . import controllers diff --git a/website_sale_restrict_sepa_dd/controllers/__init__.py b/website_sale_restrict_sepa_dd/controllers/__init__.py new file mode 100644 index 00000000..b71b9fca --- /dev/null +++ b/website_sale_restrict_sepa_dd/controllers/__init__.py @@ -0,0 +1,4 @@ +# SPDX-FileCopyrightText: 2024 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later +from . import main diff --git a/website_sale_restrict_sepa_dd/controllers/main.py b/website_sale_restrict_sepa_dd/controllers/main.py new file mode 100644 index 00000000..1fe784cc --- /dev/null +++ b/website_sale_restrict_sepa_dd/controllers/main.py @@ -0,0 +1,57 @@ +# SPDX-FileCopyrightText: 2024 Coop IT Easy SC +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +from odoo import http +from odoo.http import request + +from odoo.addons.website_sale.controllers.main import WebsiteSale + + +class WebsiteSaleRestrictSEPADD(WebsiteSale): + @http.route( + ["/shop/cart/update"], + type="http", + auth="public", + methods=["POST"], + website=True, + ) + def cart_update( + self, + product_id, + add_qty=1, + set_qty=0, + product_custom_attribute_values=None, + no_variant_attribute_values=None, + express=False, + **kwargs + ): + sale_order = request.website.sale_get_order(force_create=True) + if sale_order.state != "draft": + request.session["sale_order_id"] = None + sale_order = request.website.sale_get_order(force_create=True) + + warning = sale_order.check_product_compatibility(int(product_id)) + + request.session["website_sale_cart_warning"] = warning + + if warning: + return request.redirect("/shop/cart") + + return super().cart_update( + product_id=product_id, + add_qty=add_qty, + set_qty=set_qty, + product_custom_attribute_values=product_custom_attribute_values, + no_variant_attribute_values=no_variant_attribute_values, + express=express, + **kwargs + ) + + @http.route(["/shop/cart"], type="http", auth="public", website=True, sitemap=False) + def cart(self, access_token=None, revive="", **post): + warning = request.session["website_sale_cart_warning"] + del request.session["website_sale_cart_warning"] + response = super().cart(access_token=access_token, revive=revive, **post) + response.qcontext["website_sale_cart_warning"] = warning + return response diff --git a/website_sale_restrict_sepa_dd/models/sale_order.py b/website_sale_restrict_sepa_dd/models/sale_order.py index 35ae221f..ab7696e3 100644 --- a/website_sale_restrict_sepa_dd/models/sale_order.py +++ b/website_sale_restrict_sepa_dd/models/sale_order.py @@ -47,37 +47,45 @@ def _compute_only_sepa_dd_payment(self): order.order_line.mapped("product_id").mapped("only_sepa_dd_payment") ) - def _cart_update(self, product_id, line_id=None, add_qty=0, set_qty=0, **kwargs): - """Prevent incompatible product to be added to the cart.""" + def check_product_compatibility(self, product_id): self.ensure_one() product = self.env["product.product"].browse(product_id).exists() warning = "" + only_sepa_dd_product = self.order_line.mapped("product_id").filtered( + lambda p: p.only_sepa_dd_payment + ) + allow_sepa_dd_payment = all( + self.order_line.mapped("product_id").mapped("allow_sepa_dd_payment") + ) if product: - only_sepa_dd_product = self.order_line.mapped("product_id").filtered( - lambda p: p.only_sepa_dd_payment - ) - allow_sepa_dd_payment = all( - self.order_line.mapped("product_id").mapped("allow_sepa_dd_payment") - ) if only_sepa_dd_product and not product.allow_sepa_dd_payment: # This product cannot be added - add_qty, set_qty = None, 0 warning = _( f"Product {product.name} cannot be added to cart " "because product(s) : " f"{', '.join(p.name for p in only_sepa_dd_product)} " "must be payed by SEPA Direct Debit and product " f"{product.name} cannot be payed by such method." + "Please process this order first, or clear " + f"the order." ) elif not allow_sepa_dd_payment and product.only_sepa_dd_payment: - add_qty, set_qty = None, 0 warning = _( f"Product {product.name} cannot be added to cart " "because other products in the cart does not allow " "SEPA Direct Debit as payment method and " f"{product.name} must be payed with such a method." + "Please process this order first, or clear it." ) + return warning + def _cart_update(self, product_id, line_id=None, add_qty=0, set_qty=0, **kwargs): + """Prevent incompatible product to be added to the cart.""" + self.ensure_one() + warning = self.check_product_compatibility(product_id) + if warning: + add_qty = None + set_qty = 0 values = super()._cart_update( product_id=product_id, line_id=line_id, diff --git a/website_sale_restrict_sepa_dd/views/templates.xml b/website_sale_restrict_sepa_dd/views/templates.xml index 9f22585d..70f97017 100644 --- a/website_sale_restrict_sepa_dd/views/templates.xml +++ b/website_sale_restrict_sepa_dd/views/templates.xml @@ -1,20 +1,14 @@ -