From 6ff646f15051979357d3f0d6f93e6db9ebbd1159 Mon Sep 17 00:00:00 2001 From: "Andrea Grazioso (agr-odoo)" Date: Tue, 26 May 2020 13:13:17 +0000 Subject: [PATCH] [FIX] website_sale_stock: check cart before payment Have a Storable Product configured to 'Show inventory below a threshold and prevent sales if not enough stock' (availability settings). Have a quantity available of such product, i.e 100 From the shop create 2 sessions with 2 different users A and B. With both follow the steps in parallel: - put 100 of product in cart - go to checkout up to payment screen Press 'Pay Now' with A (and confirm the order in backend if auto confirm is not enabled so available quantity is updated) Press 'Pay Now' with B 200 of product are requested because all cart checks for availability are done before payment but not on the final step. Adding a final step of check for availability opw-2261598 closes odoo/odoo#52954 X-original-commit: 4d3f2fbdcda68d4f0e0bb3bb34d51e71d963621b Signed-off-by: Nicolas Lempereur (nle) Signed-off-by: Nicolas Martinelli (nim) --- .../controllers/__init__.py | 1 + addons/website_sale_stock/controllers/main.py | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 addons/website_sale_stock/controllers/main.py diff --git a/addons/website_sale_stock/controllers/__init__.py b/addons/website_sale_stock/controllers/__init__.py index b6ee6a5108f..2343d8d0202 100644 --- a/addons/website_sale_stock/controllers/__init__.py +++ b/addons/website_sale_stock/controllers/__init__.py @@ -2,3 +2,4 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from . import variant +from . import main diff --git a/addons/website_sale_stock/controllers/main.py b/addons/website_sale_stock/controllers/main.py new file mode 100644 index 00000000000..43f70fa4e9f --- /dev/null +++ b/addons/website_sale_stock/controllers/main.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo.addons.website_sale.controllers.main import WebsiteSale + +from odoo import http,_ +from odoo.http import request +from odoo.exceptions import ValidationError + + +class WebsiteSaleStock(WebsiteSale): + @http.route() + def payment_transaction(self, **kwargs): + """ Payment transaction override to double check cart quantities before + placing the order + """ + order = request.website.sale_get_order() + values = [] + for line in order.order_line: + if line.product_id.type == 'product' and line.product_id.inventory_availability in ['always', 'threshold']: + cart_qty = sum(order.order_line.filtered(lambda p: p.product_id.id == line.product_id.id).mapped('product_uom_qty')) + avl_qty = line.product_id.with_context(warehouse=order.warehouse_id.id).virtual_available + if cart_qty > avl_qty: + values.append(_('You ask for %s products but only %s is available') % (cart_qty, avl_qty if avl_qty > 0 else 0)) + if values: + raise ValidationError('. '.join(values) + '.') + return super(WebsiteSaleStock, self).payment_transaction(**kwargs)