From 7754caf8eb7517b05ec36862d6cefdbe4cbebe1c Mon Sep 17 00:00:00 2001
From: SilvioC2C
Date: Thu, 18 Apr 2024 12:53:06 +0200
Subject: [PATCH] fixup! address PR reviews, add migration script, tests
overhaul
---
.../README.rst | 8 +-
.../__manifest__.py | 5 +-
.../migrations/14.0.1.1.0/post-mig.py | 25 +
.../migrations/14.0.1.1.0/pre-mig.py | 25 +
.../models/res_company.py | 6 -
.../models/res_config_settings.py | 4 -
.../models/stock_warehouse.py | 9 -
.../models/stock_warehouse_orderpoint.py | 63 +--
.../static/description/index.html | 8 +-
.../tests/common.py | 53 +-
.../tests/test_calendar_orderpoint.py | 455 +++++++++++++++---
.../views/res_config_settings.xml | 14 +-
.../views/stock_warehouse.xml | 8 +-
13 files changed, 543 insertions(+), 140 deletions(-)
create mode 100644 stock_warehouse_calendar_orderpoint/migrations/14.0.1.1.0/post-mig.py
create mode 100644 stock_warehouse_calendar_orderpoint/migrations/14.0.1.1.0/pre-mig.py
diff --git a/stock_warehouse_calendar_orderpoint/README.rst b/stock_warehouse_calendar_orderpoint/README.rst
index 6b4411ddcd83..7b4caf8a01a5 100644
--- a/stock_warehouse_calendar_orderpoint/README.rst
+++ b/stock_warehouse_calendar_orderpoint/README.rst
@@ -7,7 +7,7 @@ Stock Warehouse Calendar (reordering rules)
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !! source digest: sha256:37eef2072a4e67946542d61be1ad33d6f944a858e6c39d0e8c257435edb9941d
+ !! source digest: sha256:98565ba67ef39c3d2057bca2b8fd948f4989c37e962ea67d54ab940af6e3495c
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
@@ -17,7 +17,7 @@ Stock Warehouse Calendar (reordering rules)
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-camptocamp%2Fstock--logistics--warehouse-lightgray.png?logo=github
- :target: https://github.com/camptocamp/stock-logistics-warehouse/tree/14.0-add-stock_warehouse_calendar_purchase/stock_warehouse_calendar_orderpoint
+ :target: https://github.com/camptocamp/stock-logistics-warehouse/tree/14.0-add-stock_warehouse_calendar_orderpoint/stock_warehouse_calendar_orderpoint
:alt: camptocamp/stock-logistics-warehouse
|badge1| |badge2| |badge3|
@@ -64,7 +64,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues `_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
-`feedback `_.
+`feedback `_.
Do not contact contributors directly about support or help with technical issues.
@@ -92,6 +92,6 @@ Current maintainer:
|maintainer-sebalix|
-This module is part of the `camptocamp/stock-logistics-warehouse `_ project on GitHub.
+This module is part of the `camptocamp/stock-logistics-warehouse `_ project on GitHub.
You are welcome to contribute.
diff --git a/stock_warehouse_calendar_orderpoint/__manifest__.py b/stock_warehouse_calendar_orderpoint/__manifest__.py
index c7ca7e6e5459..e80062e04e62 100644
--- a/stock_warehouse_calendar_orderpoint/__manifest__.py
+++ b/stock_warehouse_calendar_orderpoint/__manifest__.py
@@ -3,7 +3,7 @@
{
"name": "Stock Warehouse Calendar (reordering rules)",
"summary": "Adds a calendar to the Warehouse for reordering rules",
- "version": "14.0.1.0.0",
+ "version": "14.0.1.1.0",
"license": "LGPL-3",
"website": "https://github.com/OCA/stock-logistics-warehouse",
"author": "Camptocamp, " "Odoo Community Association (OCA)",
@@ -14,9 +14,6 @@
"views/stock_warehouse.xml",
"views/stock_warehouse_orderpoint.xml",
],
- "demo": [
- "demo/resource_calendar.xml",
- ],
"installable": True,
"development_status": "Production/Stable",
"maintainers": ["sebalix"],
diff --git a/stock_warehouse_calendar_orderpoint/migrations/14.0.1.1.0/post-mig.py b/stock_warehouse_calendar_orderpoint/migrations/14.0.1.1.0/post-mig.py
new file mode 100644
index 000000000000..18bc0c11f016
--- /dev/null
+++ b/stock_warehouse_calendar_orderpoint/migrations/14.0.1.1.0/post-mig.py
@@ -0,0 +1,25 @@
+# Copyright 2024 Camptocamp SA
+# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl)
+
+from odoo.tools.sql import column_exists
+
+
+def migrate(cr, version):
+ if not version:
+ return
+ if column_exists(cr, "stock_warehouse", "orderpoint_on_workday_tmp"):
+ # Fill ``orderpoint_on_workday_policy`` with a default value where needed, then
+ # drop the temporary column
+ cr.execute(
+ """
+ UPDATE stock_warehouse
+ SET orderpoint_on_workday_policy = 'skip_to_first_workday'
+ WHERE orderpoint_on_workday_tmp
+ """
+ )
+ cr.execute(
+ """
+ ALTER TABLE stock_warehouse
+ DROP COLUMN orderpoint_on_workday_tmp
+ """
+ )
diff --git a/stock_warehouse_calendar_orderpoint/migrations/14.0.1.1.0/pre-mig.py b/stock_warehouse_calendar_orderpoint/migrations/14.0.1.1.0/pre-mig.py
new file mode 100644
index 000000000000..1efbe4f29209
--- /dev/null
+++ b/stock_warehouse_calendar_orderpoint/migrations/14.0.1.1.0/pre-mig.py
@@ -0,0 +1,25 @@
+# Copyright 2024 Camptocamp SA
+# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl)
+
+from odoo.tools.sql import column_exists
+
+
+def migrate(cr, version):
+ if not version:
+ return
+ if column_exists(cr, "stock_warehouse", "orderpoint_on_workday"):
+ # Store ``orderpoint_on_workday`` in a temporary column to be used in the post-mig
+ # script to set default values for ``orderpoint_on_workday_policy``
+ cr.execute(
+ """
+ ALTER TABLE stock_warehouse
+ ADD COLUMN IF NOT EXISTS orderpoint_on_workday_tmp BOOLEAN DEFAULT false
+ """
+ )
+ cr.execute(
+ """
+ UPDATE stock_warehouse
+ SET orderpoint_on_workday_tmp = true
+ WHERE orderpoint_on_workday
+ """
+ )
diff --git a/stock_warehouse_calendar_orderpoint/models/res_company.py b/stock_warehouse_calendar_orderpoint/models/res_company.py
index afed9524796e..80a9f4040eda 100644
--- a/stock_warehouse_calendar_orderpoint/models/res_company.py
+++ b/stock_warehouse_calendar_orderpoint/models/res_company.py
@@ -14,12 +14,6 @@ class ResCompany(models.Model):
"This value will be used as default value for new warehouses linked to this"
" company",
)
- orderpoint_on_workday = fields.Boolean(
- string="Schedule the lead date on workday only",
- help="Postpone the lead date to the first available workday\n"
- "This value will be used as default value for new warehouses linked to this"
- " company",
- )
orderpoint_on_workday_policy = fields.Selection(
[
("skip_to_first_workday", "Skip to first workday"),
diff --git a/stock_warehouse_calendar_orderpoint/models/res_config_settings.py b/stock_warehouse_calendar_orderpoint/models/res_config_settings.py
index 6a6fc716e4ec..7a3e39957b6e 100644
--- a/stock_warehouse_calendar_orderpoint/models/res_config_settings.py
+++ b/stock_warehouse_calendar_orderpoint/models/res_config_settings.py
@@ -11,10 +11,6 @@ class ResConfigSettings(models.TransientModel):
related="company_id.orderpoint_calendar_id",
readonly=False,
)
- orderpoint_on_workday = fields.Boolean(
- related="company_id.orderpoint_on_workday",
- readonly=False,
- )
orderpoint_on_workday_policy = fields.Selection(
related="company_id.orderpoint_on_workday_policy",
readonly=False,
diff --git a/stock_warehouse_calendar_orderpoint/models/stock_warehouse.py b/stock_warehouse_calendar_orderpoint/models/stock_warehouse.py
index 23e9f03b2e32..c956b76a3216 100644
--- a/stock_warehouse_calendar_orderpoint/models/stock_warehouse.py
+++ b/stock_warehouse_calendar_orderpoint/models/stock_warehouse.py
@@ -13,12 +13,6 @@ class StockWarehouse(models.Model):
default=lambda o: o._default_orderpoint_calendar_id(),
help="Calendar used to compute the lead date of reordering rules",
)
- orderpoint_on_workday = fields.Boolean(
- string="Reordering on Workday",
- default=lambda o: o._default_orderpoint_on_workday(),
- help="Postpone the lead date to the first available workday based on the"
- " Working Hours calendar",
- )
orderpoint_on_workday_policy = fields.Selection(
[
("skip_to_first_workday", "Skip to first workday"),
@@ -40,8 +34,5 @@ class StockWarehouse(models.Model):
def _default_orderpoint_calendar_id(self):
return self.env.company.orderpoint_calendar_id
- def _default_orderpoint_on_workday(self):
- return self.env.company.orderpoint_on_workday
-
def _default_orderpoint_on_workday_policy(self):
return self.env.company.orderpoint_on_workday_policy
diff --git a/stock_warehouse_calendar_orderpoint/models/stock_warehouse_orderpoint.py b/stock_warehouse_calendar_orderpoint/models/stock_warehouse_orderpoint.py
index 9d85a8fc8523..92f3c6dc3ae7 100644
--- a/stock_warehouse_calendar_orderpoint/models/stock_warehouse_orderpoint.py
+++ b/stock_warehouse_calendar_orderpoint/models/stock_warehouse_orderpoint.py
@@ -14,47 +14,54 @@ class StockWarehouseOrderpoint(models.Model):
"rule_ids",
"product_id.seller_ids",
"product_id.seller_ids.delay",
+ "warehouse_id.calendar_id",
"warehouse_id.orderpoint_calendar_id",
"warehouse_id.orderpoint_on_workday_policy",
)
def _compute_lead_days(self):
super()._compute_lead_days()
- # Override to use the orderpoint calendar to compute the 'lead_days_date'
+ # Override to use the WH/OP calendars to compute ``lead_days_date``
for orderpoint in self.with_context(bypass_delay_description=True):
wh = orderpoint.warehouse_id
if not orderpoint.product_id or not orderpoint.location_id:
orderpoint.lead_days_date = False
continue
- # Get the next planned date to execute this orderpoint
- start_date = orderpoint._get_next_reordering_date()
+ # Get the reordering date from the OP calendar
+ reordering_date = orderpoint._get_next_reordering_date()
# Get the lead days for this orderpoint
lead_days = orderpoint._get_lead_days()
- # Get calendar, workday policy from warehouse
+ # Get the WH calendar
calendar = wh.calendar_id
- policy = wh.orderpoint_on_workday and wh.orderpoint_on_workday_policy
- if calendar and policy == "skip_to_first_workday":
- # Consume all the lead days, then move up to the first workday
- # according to the calendar
- lead_days_date = calendar.plan_hours(
- 0, start_date + relativedelta(days=lead_days), compute_leaves=True
- )
- elif calendar and policy == "skip_all_non_workdays":
- # Postpone to the next available workday if needed, consuming lead days
- # only on workdays
- lead_days_date = calendar.plan_hours(0, start_date, compute_leaves=True)
- if lead_days_date.date() != start_date.date():
- # We've consumed a lead day if the lead date is not the start date
- lead_days -= 1
- while lead_days > 0:
- # Always get the next working day according to the calendar, and
- # decrease the lead days at each iteration
- lead_days_date = calendar.plan_hours(
- 0, lead_days_date + relativedelta(days=1), compute_leaves=True
+ if calendar and lead_days:
+ if wh.orderpoint_on_workday_policy == "skip_all_non_workdays":
+ # Get the first workday for the WH calendar after consuming the
+ # ``lead_days`` as workdays (for the WH calendar itself) starting
+ # from the day after the reordering date itself
+ lead_days_date = calendar.plan_days(
+ lead_days,
+ reordering_date + relativedelta(days=1),
+ compute_leaves=True,
+ )
+ else:
+ # Get the first workday for the WH calendar after consuming the
+ # ``lead_days`` as solar days
+ # (This is the behavior for policy ``skip_to_first_workday``, but
+ # also a fallback in case the policy is not defined)
+ lead_days_date = calendar.plan_days(
+ 1,
+ reordering_date + relativedelta(days=lead_days),
+ compute_leaves=True,
)
- lead_days -= 1
+ elif calendar:
+ # Get the first workday for the WH calendar
+ lead_days_date = calendar.plan_days(
+ 1, reordering_date, compute_leaves=True
+ )
+ elif lead_days:
+ # No WH calendar defined => consume ``lead_days`` as solar days
+ lead_days_date = reordering_date + relativedelta(days=lead_days)
else:
- # Simply postpone according to delays
- lead_days_date = start_date + relativedelta(days=lead_days)
+ lead_days_date = reordering_date
orderpoint.lead_days_date = lead_days_date
def _get_lead_days(self):
@@ -66,13 +73,13 @@ def _get_next_reordering_date(self):
self.ensure_one()
now = fields.Datetime.now()
calendar = self.warehouse_id.orderpoint_calendar_id
- # TODO: should we take into account days off or the reordering calendar with
+ # TODO: should we take into account days off of the reordering calendar with
# 'compute_leaves=True' here?
return calendar and calendar.plan_hours(0, now) or now
@api.depends("rule_ids", "product_id.seller_ids", "product_id.seller_ids.delay")
def _compute_json_popover(self):
- # Overridden to sent the OP ID to 'stock.rule._get_lead_days()'
+ # Overridden to send the OP ID to 'stock.rule._get_lead_days()'
# method through the context, so we can display the reordering date
# on the popover forecast widget
for orderpoint in self:
diff --git a/stock_warehouse_calendar_orderpoint/static/description/index.html b/stock_warehouse_calendar_orderpoint/static/description/index.html
index c1e7f764ac58..bff665bbcb05 100644
--- a/stock_warehouse_calendar_orderpoint/static/description/index.html
+++ b/stock_warehouse_calendar_orderpoint/static/description/index.html
@@ -366,9 +366,9 @@
Stock Warehouse Calendar (reordering rules)
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-!! source digest: sha256:37eef2072a4e67946542d61be1ad33d6f944a858e6c39d0e8c257435edb9941d
+!! source digest: sha256:98565ba67ef39c3d2057bca2b8fd948f4989c37e962ea67d54ab940af6e3495c
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
-
+
This module adds an Reordering Calendar to the Warehouse. This calendar
can then used by reordering rules to compute the forecasted date when goods
will be received.
Bugs are tracked on GitHub Issues.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
-feedback.