From a9cbd2a2ae2e21f1ba14379aecd908c44497b8ab Mon Sep 17 00:00:00 2001
From: William Henrotin <whe@odoo.com>
Date: Thu, 31 Aug 2023 11:18:25 +0000
Subject: [PATCH] [FIX] {sale_}purchase{_stock}: select sellers depending on
 date

Commit 72a17bafba6e7 force the PO `date_order` to be >= at ` today() to
ensure the select_seller method take the right seller price. This
introduces an error in the MPS module that may need to create PO in the
past.

Instead, this commit patch the call to `_select_seller` to take at least
`today()`

closes odoo/odoo#134130

Opw: 3167094
X-original-commit: b06b0a77bcac6c5289e957e5133d6a213d68948d
Signed-off-by: Arnold Moyaux (arm) <arm@odoo.com>
Signed-off-by: William Henrotin (whe) <whe@odoo.com>
---
 addons/purchase/models/purchase.py            |  2 +-
 addons/purchase_stock/models/stock_rule.py    |  2 +-
 .../tests/test_lead_time.py                   | 63 +++++++++++++++++++
 3 files changed, 65 insertions(+), 2 deletions(-)
 create mode 100644 addons/sale_purchase_stock/tests/test_lead_time.py

diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py
index 4965db73158e..2237a985de3e 100644
--- a/addons/purchase/models/purchase.py
+++ b/addons/purchase/models/purchase.py
@@ -1413,7 +1413,7 @@ class PurchaseOrderLine(models.Model):
         seller = product_id.with_company(company_id)._select_seller(
             partner_id=partner,
             quantity=uom_po_qty,
-            date=po.date_order and po.date_order.date(),
+            date=max(po.date_order and po.date_order.date(), fields.Date.today()),
             uom_id=product_id.uom_po_id)
 
         product_taxes = product_id.supplier_taxes_id.filtered(lambda x: x.company_id.id == company_id.id)
diff --git a/addons/purchase_stock/models/stock_rule.py b/addons/purchase_stock/models/stock_rule.py
index 6fb77d51d7d0..07990b6b2edd 100644
--- a/addons/purchase_stock/models/stock_rule.py
+++ b/addons/purchase_stock/models/stock_rule.py
@@ -62,7 +62,7 @@ class StockRule(models.Model):
                 supplier = procurement.product_id.with_company(procurement.company_id.id)._select_seller(
                     partner_id=procurement.values.get("supplierinfo_name"),
                     quantity=procurement.product_qty,
-                    date=procurement_date_planned.date(),
+                    date=max(procurement_date_planned.date(), fields.Date.today()),
                     uom_id=procurement.product_uom)
 
             # Fall back on a supplier for which no price may be defined. Not ideal, but better than
diff --git a/addons/sale_purchase_stock/tests/test_lead_time.py b/addons/sale_purchase_stock/tests/test_lead_time.py
new file mode 100644
index 000000000000..c29bf843fee5
--- /dev/null
+++ b/addons/sale_purchase_stock/tests/test_lead_time.py
@@ -0,0 +1,63 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from datetime import timedelta
+
+from odoo import fields
+from odoo.tests import tagged
+from odoo.addons.sale_purchase.tests.common import TestCommonSalePurchaseNoChart
+
+
+@tagged('post_install', '-at_install')
+class TestLeadTime(TestCommonSalePurchaseNoChart):
+
+    @classmethod
+    def setUpClass(cls):
+        super(TestLeadTime, cls).setUpClass()
+
+        cls.buy_route = cls.env.ref('purchase_stock.route_warehouse0_buy')
+        cls.mto_route = cls.env.ref('stock.route_warehouse0_mto')
+        cls.mto_route.active = True
+        cls.vendor = cls.env['res.partner'].create({'name': 'The Emperor'})
+        cls.user_salesperson = cls.env['res.users'].with_context(no_reset_password=True).create({
+            'name': 'Le Grand Horus',
+            'login': 'grand.horus',
+            'email': 'grand.horus@chansonbelge.dz',
+        })
+
+
+    def test_supplier_lead_time(self):
+        """ Basic stock configuration and a supplier with a minimum qty and a lead time """
+
+        self.env.user.company_id.po_lead = 7
+        seller = self.env['product.supplierinfo'].create({
+            'name': self.vendor.id,
+            'min_qty': 1,
+            'price': 10,
+            'date_start': fields.Date.today() - timedelta(days=1),
+        })
+
+        product = self.env['product.product'].create({
+            'name': 'corpse starch',
+            'type': 'product',
+            'seller_ids': [(6, 0, seller.ids)],
+            'route_ids': [(6, 0, (self.mto_route + self.buy_route).ids)],
+        })
+
+        so = self.env['sale.order'].with_user(self.user_salesperson).create({
+            'partner_id': self.partner_a.id,
+            'user_id': self.user_salesperson.id,
+        })
+        self.env['sale.order.line'].create({
+            'name': product.name,
+            'product_id': product.id,
+            'product_uom_qty': 1,
+            'product_uom': product.uom_id.id,
+            'price_unit': product.list_price,
+            'tax_id': False,
+            'order_id': so.id,
+        })
+        so.action_confirm()
+
+        po = self.env['purchase.order'].search([('partner_id', '=', self.vendor.id)])
+        self.assertEqual(po.order_line.price_unit, seller.price)
-- 
GitLab