Skip to content
Snippets Groups Projects
Commit 7823155d authored by Romain Derie's avatar Romain Derie
Browse files

[FIX] website_sale_stock: use correct qty to show warning/error


Without this commit, if you had:
- 3 Qty for product P in WH1
- 10 Qty for product P in WH2
- no warehouse set on website
- set show stock and prevent sales if not enough stock enabled

And then:
- Add 3 P in your cart
- In cart, add more P, it would let you do it, while it shouldn't

You would then be stuck on payment step as it would tell you only 3 are
available.

This commit improves 6b05fb2b by ensuring the warehouse_id used is the
correct one (the one that will be used on the SO (required field)).
That commit was fixing a case that was not supposed to happen[1], it is that
case that should have been fixed (which is fixed by previous commit of this PR)

[1]: it would fix the 'issue' when you add a product which has no stock
     available for the warehouse that will be used when creating the SO, by
     letting you through the warning, but you would then be stuck on the
     payment step as the qty check would be correctly done.
     The fix should have been to display the correct qty and prevent the add to
     cart in that case.

closes odoo/odoo#77308

Signed-off-by: default avatarRomain Derie (rde) <rde@odoo.com>
Co-authored-by: default avatarRomain Derie <rde@odoo.com>
Co-authored-by: default avatarAdrien Widart <awt@odoo.com>
parent e1fb4f69
No related branches found
No related tags found
No related merge requests found
......@@ -17,11 +17,8 @@ class SaleOrder(models.Model):
for line in self.order_line:
if line.product_id.type == 'product' and line.product_id.inventory_availability in ['always', 'threshold']:
cart_qty = sum(self.order_line.filtered(lambda p: p.product_id.id == line.product_id.id).mapped('product_uom_qty'))
# The quantity should be computed based on the warehouse of the website, not the
# warehouse of the SO.
website = self.env['website'].get_current_website()
if cart_qty > line.product_id.with_context(warehouse=website.warehouse_id.id).virtual_available and (line_id == line.id):
qty = line.product_id.with_context(warehouse=website.warehouse_id.id).virtual_available - cart_qty
if cart_qty > line.product_id.with_context(warehouse=self.warehouse_id.id).virtual_available and (line_id == line.id):
qty = line.product_id.with_context(warehouse=self.warehouse_id.id).virtual_available - cart_qty
new_val = super(SaleOrder, self)._cart_update(line.product_id.id, line.id, qty, 0, **kwargs)
values.update(new_val)
......
......@@ -78,3 +78,24 @@ class TestWebsiteSaleStockProductWarehouse(TestSaleProductAttributeValueSetup):
# Check available quantity of product is according to warehouse
self.assertEqual(combination_info['virtual_available'], qty_b, "%s units of Product B should be available in warehouse %s" % (qty_b, wh))
def test_02_update_cart_with_multi_warehouses(self):
""" When the user updates his cart and increases a product quantity, if
this quantity is not available in the SO's warehouse, a warning should
be returned and the quantity updated to its maximum. """
so = self.env['sale.order'].create({
'partner_id': self.env.user.partner_id.id,
'order_line': [(0, 0, {
'name': self.product_A.name,
'product_id': self.product_A.id,
'product_uom_qty': 5,
'product_uom': self.product_A.uom_id.id,
'price_unit': self.product_A.list_price,
})]
})
with MockRequest(self.env, website=self.website, sale_order_id=so.id):
values = so._cart_update(product_id=self.product_A.id, line_id=so.order_line.id, set_qty=20)
self.assertTrue(values.get('warning', False))
self.assertEqual(values.get('quantity'), 10)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment