Skip to content

Commit

Permalink
fixup! address PR reviews, add migration script, tests overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
SilvioC2C committed Apr 18, 2024
1 parent 7979341 commit ab301a6
Show file tree
Hide file tree
Showing 14 changed files with 543 additions and 157 deletions.
8 changes: 4 additions & 4 deletions stock_warehouse_calendar_orderpoint/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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|
Expand Down Expand Up @@ -64,7 +64,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/camptocamp/stock-logistics-warehouse/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 <https://github.com/camptocamp/stock-logistics-warehouse/issues/new?body=module:%20stock_warehouse_calendar_orderpoint%0Aversion:%2014.0-add-stock_warehouse_calendar_purchase%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/camptocamp/stock-logistics-warehouse/issues/new?body=module:%20stock_warehouse_calendar_orderpoint%0Aversion:%2014.0-add-stock_warehouse_calendar_orderpoint%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand Down Expand Up @@ -92,6 +92,6 @@ Current maintainer:

|maintainer-sebalix|

This module is part of the `camptocamp/stock-logistics-warehouse <https://github.com/camptocamp/stock-logistics-warehouse/tree/14.0-add-stock_warehouse_calendar_purchase/stock_warehouse_calendar_orderpoint>`_ project on GitHub.
This module is part of the `camptocamp/stock-logistics-warehouse <https://github.com/camptocamp/stock-logistics-warehouse/tree/14.0-add-stock_warehouse_calendar_orderpoint/stock_warehouse_calendar_orderpoint>`_ project on GitHub.

You are welcome to contribute.
5 changes: 1 addition & 4 deletions stock_warehouse_calendar_orderpoint/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand All @@ -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"],
Expand Down
17 changes: 0 additions & 17 deletions stock_warehouse_calendar_orderpoint/demo/resource_calendar.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
"""
)
Original file line number Diff line number Diff line change
@@ -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
"""
)
6 changes: 0 additions & 6 deletions stock_warehouse_calendar_orderpoint/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 0 additions & 9 deletions stock_warehouse_calendar_orderpoint/models/stock_warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 28 in stock_warehouse_calendar_orderpoint/models/stock_warehouse_orderpoint.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_calendar_orderpoint/models/stock_warehouse_orderpoint.py#L27-L28

Added lines #L27 - L28 were not covered by tests
# 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):
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ <h1 class="title">Stock Warehouse Calendar (reordering rules)</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:37eef2072a4e67946542d61be1ad33d6f944a858e6c39d0e8c257435edb9941d
!! source digest: sha256:98565ba67ef39c3d2057bca2b8fd948f4989c37e962ea67d54ab940af6e3495c
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/camptocamp/stock-logistics-warehouse/tree/14.0-add-stock_warehouse_calendar_purchase/stock_warehouse_calendar_orderpoint"><img alt="camptocamp/stock-logistics-warehouse" src="https://img.shields.io/badge/github-camptocamp%2Fstock--logistics--warehouse-lightgray.png?logo=github" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/camptocamp/stock-logistics-warehouse/tree/14.0-add-stock_warehouse_calendar_orderpoint/stock_warehouse_calendar_orderpoint"><img alt="camptocamp/stock-logistics-warehouse" src="https://img.shields.io/badge/github-camptocamp%2Fstock--logistics--warehouse-lightgray.png?logo=github" /></a></p>
<p>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.</p>
Expand Down Expand Up @@ -415,7 +415,7 @@ <h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/camptocamp/stock-logistics-warehouse/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/camptocamp/stock-logistics-warehouse/issues/new?body=module:%20stock_warehouse_calendar_orderpoint%0Aversion:%2014.0-add-stock_warehouse_calendar_purchase%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/camptocamp/stock-logistics-warehouse/issues/new?body=module:%20stock_warehouse_calendar_orderpoint%0Aversion:%2014.0-add-stock_warehouse_calendar_orderpoint%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -436,7 +436,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
<p>Current maintainer:</p>
<p><a class="reference external image-reference" href="https://github.com/sebalix"><img alt="sebalix" src="https://github.com/sebalix.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/camptocamp/stock-logistics-warehouse/tree/14.0-add-stock_warehouse_calendar_purchase/stock_warehouse_calendar_orderpoint">camptocamp/stock-logistics-warehouse</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/camptocamp/stock-logistics-warehouse/tree/14.0-add-stock_warehouse_calendar_orderpoint/stock_warehouse_calendar_orderpoint">camptocamp/stock-logistics-warehouse</a> project on GitHub.</p>
<p>You are welcome to contribute.</p>
</div>
</div>
Expand Down
53 changes: 45 additions & 8 deletions stock_warehouse_calendar_orderpoint/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,46 @@ def setUpClass(cls):
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.product = cls.env.ref("product.product_delivery_02").copy()
cls.wh = cls.env.ref("stock.warehouse0")
cls.wh.write(
days_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
cls.working_hours_calendar = cls.env["resource.calendar"].create(
{
"calendar_id": cls.env.ref("resource.resource_calendar_std").id,
"orderpoint_calendar_id": cls.env.ref(
"stock_warehouse_calendar_orderpoint.resource_calendar_orderpoint_demo"
).id,
"orderpoint_on_workday": True,
"orderpoint_on_workday_policy": "skip_to_first_workday",
"name": "Working Hours (Mon-Fri, 8-12 + 13-17)",
"tz": "UTC",
"attendance_ids": [
(
0,
0,
{
"name": days_names[day],
"dayofweek": str(day),
"hour_from": hour,
"hour_to": hour + 4,
"day_period": "morning" if hour < 12 else "afternoon",
},
)
for day in (0, 1, 2, 3, 4)
for hour in (8, 13)
],
}
)
cls.reordering_calendar = cls.env["resource.calendar"].create(
{
"name": "Reordering Hours (Wed 8-12 + 13-17, Sat 13-17)",
"tz": "UTC",
"attendance_ids": [
(
0,
0,
{
"name": days_names[day],
"dayofweek": str(day),
"hour_from": hour,
"hour_to": hour + 4,
"day_period": "morning" if hour < 12 else "afternoon",
},
)
for day, hour in ((2, 8), (2, 13), (5, 13))
],
}
)
cls.orderpoint = cls.env["stock.warehouse.orderpoint"].create(
Expand All @@ -31,4 +63,9 @@ def setUpClass(cls):
"product_uom": cls.env.ref("uom.product_uom_unit"),
}
)
cls.orderpoint.rule_ids.write({"action": "pull", "delay": 2})
# We want only 1 reordering rule of type "pull" to avoid inconsistent behaviors
cls.reordering_rule = cls.orderpoint.rule_ids[0]
cls.reordering_rule.action = "pull"
other_rules = cls.orderpoint.rule_ids - cls.reordering_rule
if other_rules:
other_rules.unlink()

Check warning on line 71 in stock_warehouse_calendar_orderpoint/tests/common.py

View check run for this annotation

Codecov / codecov/patch

stock_warehouse_calendar_orderpoint/tests/common.py#L71

Added line #L71 was not covered by tests
Loading

0 comments on commit ab301a6

Please sign in to comment.