|
| 1 | +# SPDX-FileCopyrightText: 2024 Coop IT Easy SC |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + |
| 5 | +from odoo import http |
| 6 | +from odoo.http import request |
| 7 | + |
| 8 | +from odoo.addons.website_sale.controllers.main import WebsiteSale |
| 9 | + |
| 10 | +SEPA_DEBIT_SUB_DATA = "sepa_debit_subscription_data" |
| 11 | + |
| 12 | + |
| 13 | +class SubscriptionForm(http.Controller): |
| 14 | + @http.route( |
| 15 | + [ |
| 16 | + "/sepa-debit-subscription", |
| 17 | + "/sepa-debit-subscription/product/<int:pid>", |
| 18 | + ], |
| 19 | + auth="public", |
| 20 | + website=True, |
| 21 | + sitemap=False, |
| 22 | + ) |
| 23 | + def sepa_debit_subscriptions(self, pid=None): |
| 24 | + """Show list of available subscription""" |
| 25 | + if SEPA_DEBIT_SUB_DATA in request.session: |
| 26 | + del request.session[SEPA_DEBIT_SUB_DATA] |
| 27 | + products = request.env["product.product"].search( |
| 28 | + [("is_contract", "=", True), ("website_published", "=", True)] |
| 29 | + ) |
| 30 | + product = request.env["product.product"].browse(pid).exists() |
| 31 | + if product and product in products: |
| 32 | + request.session[SEPA_DEBIT_SUB_DATA] = { |
| 33 | + "product_id": product.id, |
| 34 | + } |
| 35 | + return request.redirect("/web/login?redirect=/shop/checkout") |
| 36 | + values = { |
| 37 | + "products": products, |
| 38 | + } |
| 39 | + return request.render( |
| 40 | + "website_sale_subscription_form.sepa_debit_subscriptions", values |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +class WebsiteSaleSubscription(WebsiteSale): |
| 45 | + @http.route( |
| 46 | + [ |
| 47 | + "/shop", |
| 48 | + "/shop/page/<int:page>", |
| 49 | + '/shop/category/<model("product.public.category"):category>', |
| 50 | + '/shop/category/<model("product.public.category"):category>/page/<int:page>', |
| 51 | + ], |
| 52 | + type="http", |
| 53 | + auth="public", |
| 54 | + website=True, |
| 55 | + sitemap=WebsiteSale.sitemap_shop, |
| 56 | + ) |
| 57 | + def shop( |
| 58 | + self, |
| 59 | + page=0, |
| 60 | + category=None, |
| 61 | + search="", |
| 62 | + min_price=0.0, |
| 63 | + max_price=0.0, |
| 64 | + ppg=False, |
| 65 | + **post |
| 66 | + ): |
| 67 | + if SEPA_DEBIT_SUB_DATA in request.session: |
| 68 | + del request.session[SEPA_DEBIT_SUB_DATA] |
| 69 | + return super().shop( |
| 70 | + page=page, |
| 71 | + category=category, |
| 72 | + search=search, |
| 73 | + min_price=min_price, |
| 74 | + max_price=max_price, |
| 75 | + ppg=ppg, |
| 76 | + **post, |
| 77 | + ) |
| 78 | + |
| 79 | + @http.route( |
| 80 | + ["/shop/checkout"], type="http", auth="public", website=True, sitemap=False |
| 81 | + ) |
| 82 | + def checkout(self, **post): |
| 83 | + result = super().checkout(**post) |
| 84 | + if "sepa_debit_subscription_data" in request.session: |
| 85 | + if result.template == "website_sale.checkout": |
| 86 | + result.qcontext["only_services"] = False |
| 87 | + return result |
| 88 | + |
| 89 | + @http.route( |
| 90 | + "/shop/payment", type="http", auth="public", website=True, sitemap=False |
| 91 | + ) |
| 92 | + def shop_payment(self, **post): |
| 93 | + if request.httprequest.method == "POST": |
| 94 | + # TODO: record IBAN and SEPA mandate |
| 95 | + order = request.website.sale_get_order() |
| 96 | + # FIXME: save order before ? or use dedicated existing |
| 97 | + # route ? |
| 98 | + order.with_context(send_email=True).action_confirm() |
| 99 | + return request.redirect(order.get_portal_url()) |
| 100 | + return super().shop_payment(**post) |
0 commit comments