Skip to content
Snippets Groups Projects
Commit 72a17baf authored by Walid HANNICHE (waha)'s avatar Walid HANNICHE (waha)
Browse files

[FIX] sale_purchase_stock: correct PO deadline date


steps to reproduce it:
1. Create a storable product, add a Purchase vendor with
 start date = 20/6/2022 and delay = 56
2. Create another storable product, add the same Purchase vendor with
 start date = 11/01/2023 and delay = 21
3. For both products, add Routes "MTO".
4. Create a SO with those 2 products, and confirm it
5. As you can see, the price of the first product is correctly set
but not the second product

BUG:
This is due of "Order Deadline" (date_order) being set to an old date,
which is before the second product date_order.

FIX:
set max PO date as today

opw-3167094

closes odoo/odoo#124448

Related: odoo/enterprise#44391
Signed-off-by: default avatarWilliam Henrotin (whe) <whe@odoo.com>
parent 18978814
No related branches found
No related tags found
No related merge requests found
...@@ -259,16 +259,17 @@ class StockRule(models.Model): ...@@ -259,16 +259,17 @@ class StockRule(models.Model):
res['orderpoint_id'] = orderpoint_id.id res['orderpoint_id'] = orderpoint_id.id
return res return res
def _get_po_date(self, company_id, values):
purchase_date = min([fields.Datetime.from_string(value['date_planned']) - relativedelta(days=int(value['supplier'].delay)) for value in values])
return purchase_date - relativedelta(days=company_id.po_lead)
def _prepare_purchase_order(self, company_id, origins, values): def _prepare_purchase_order(self, company_id, origins, values):
""" Create a purchase order for procuremets that share the same domain """ Create a purchase order for procuremets that share the same domain
returned by _make_po_get_domain. returned by _make_po_get_domain.
params values: values of procurements params values: values of procurements
params origins: procuremets origins to write on the PO params origins: procuremets origins to write on the PO
""" """
purchase_date = min([fields.Datetime.from_string(value['date_planned']) - relativedelta(days=int(value['supplier'].delay)) for value in values]) purchase_date = max(self._get_po_date(company_id, values), fields.Datetime.now())
purchase_date = (purchase_date - relativedelta(days=company_id.po_lead))
# Since the procurements are grouped if they share the same domain for # Since the procurements are grouped if they share the same domain for
# PO but the PO does not exist. In this case it will create the PO from # PO but the PO does not exist. In this case it will create the PO from
......
...@@ -361,5 +361,5 @@ class TestPurchaseLeadTime(PurchaseTestCommon): ...@@ -361,5 +361,5 @@ class TestPurchaseLeadTime(PurchaseTestCommon):
purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.partner_1.id)]) purchase_order = self.env['purchase.order'].search([('partner_id', '=', self.partner_1.id)])
today = fields.Datetime.start_of(fields.Datetime.now(), 'day') today = fields.Datetime.start_of(fields.Datetime.now(), 'day')
self.assertEqual(purchase_order.date_order, today) self.assertEqual(fields.Datetime.start_of(purchase_order.date_order, 'day'), today)
self.assertEqual(fields.Datetime.start_of(purchase_order.date_planned, 'day'), today + timedelta(days=7)) self.assertEqual(fields.Datetime.start_of(purchase_order.date_planned, 'day'), today + timedelta(days=7))
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details. # Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import test_access_rights from . import test_access_rights
from . import test_lead_time
# -*- 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 """
seller = self.env['product.supplierinfo'].create({
'name': self.vendor.id,
'min_qty': 1,
'price': 1,
'delay': 7,
})
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)])
start_of_day = lambda x: fields.Datetime.start_of(x, 'day')
today = start_of_day(fields.Datetime.now())
self.assertEqual(start_of_day(po.date_order), today)
self.assertEqual(start_of_day(po.date_planned), today + timedelta(days=7))
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