Skip to content

Commit

Permalink
TA#70572 [14.0][FIX] stock_orderpoint_scheduled_date: get lead days f… (
Browse files Browse the repository at this point in the history
  • Loading branch information
majouda authored Nov 28, 2024
1 parent c21a2a1 commit 38fecff
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 27 deletions.
4 changes: 2 additions & 2 deletions stock_orderpoint_scheduled_date/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Stock Orderpoint Scheduled Date",
"summary": "Force Scheduled Date in Stock Orderpoint",
"version": "14.0.1.1.0",
"version": "14.0.1.2.0",
"category": "stock",
"website": "https://bit.ly/numigi-com",
"author": "Numigi",
Expand All @@ -14,6 +14,6 @@
"data": [
"security/ir.model.access.csv",
"views/stock_orderpoint_views.xml",
"wizard/stock_warehouse_orderpoint_schedule_date.xml"
"wizard/stock_warehouse_orderpoint_schedule_date.xml",
],
}
1 change: 1 addition & 0 deletions stock_orderpoint_scheduled_date/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import stock_warehouse_orderpoint
from . import stock_rule
28 changes: 28 additions & 0 deletions stock_orderpoint_scheduled_date/models/stock_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# © 2024 - today Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models, fields, _


class StockRule(models.Model):
_inherit = "stock.rule"

def _get_lead_days(self, product):
"""Add the delay from the manually entered scheduled date in the replenishment
order to the cumulative delay and cumulative description.
"""
delay, delay_description = super()._get_lead_days(product)
scheduled_date = self.env.context.get("scheduled_date")
bypass_delay_description = self.env.context.get(
"bypass_delay_description", False
)
if scheduled_date:
# Convert the scheduled date from string to UTC datetime
manual_delay = (scheduled_date - fields.Datetime.now()).days
delay += manual_delay
if not bypass_delay_description:
delay_description += (
"<tr><td>%s</td><td class='text-right'>+ %d %s</td></tr>"
% (_("Manual Lead Time"), manual_delay, _("day(s)"))
)
return delay, delay_description
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,85 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, models, fields, _
from odoo.exceptions import ValidationError


class stockWarehouseOrderpoint(models.Model):
_inherit = "stock.warehouse.orderpoint"

scheduled_date = fields.Date("Scheduled Date")
previous_scheduled_date = fields.Date(
"Previous Scheduled Date", default=fields.Date.today()
scheduled_date = fields.Datetime(
"Scheduled Date", help="The date when this replenishment should be scheduled."
)

@api.depends('rule_ids', 'product_id.seller_ids',
'product_id.seller_ids.delay', 'scheduled_date')
@api.depends(
"rule_ids",
"product_id.seller_ids",
"product_id.seller_ids.delay",
"scheduled_date",
)
def _compute_lead_days(self):
for orderpoint in self.with_context(bypass_delay_description=True):
if orderpoint.scheduled_date:
orderpoint.lead_days_date = orderpoint.scheduled_date
elif orderpoint.previous_scheduled_date:
orderpoint.lead_days_date = orderpoint.previous_scheduled_date
else:
super(stockWarehouseOrderpoint, orderpoint)._compute_lead_days()

def open_set_schedule_date_wizard(self):
for orderpoint in self:
# Pass scheduled_date via context for lead time computation
super(
stockWarehouseOrderpoint,
orderpoint.with_context(scheduled_date=orderpoint.scheduled_date),
)._compute_lead_days()

@api.depends(
"rule_ids",
"product_id.seller_ids",
"product_id.seller_ids.delay",
"scheduled_date",
)
def _compute_json_popover(self):
for orderpoint in self:
# Pass scheduled_date and delay description context
super(
stockWarehouseOrderpoint,
orderpoint.with_context(
scheduled_date=orderpoint.scheduled_date,
bypass_delay_description=False,
),
)._compute_json_popover()

@api.model
def action_open_set_schedule_date_wizard(self):
"""Open a wizard to set the scheduled date."""
wizard = self.env['stock.warehouse.orderpoint.schedule.date'].create({})
wizard.orderpoint_ids = self
action = wizard.get_formview_action()
action['target'] = 'new'
action['name'] = _('Set Schedule Date')
return action

def action_replenish(self):
result = super(stockWarehouseOrderpoint, self).action_replenish()
self.write(
{"previous_scheduled_date": self.scheduled_date, "scheduled_date": False}
)
return result
@api.model
def action_open_orderpoints(self):
"""Override to pass scheduled_date in the context."""
return super(
stockWarehouseOrderpoint,
self.with_context(scheduled_date=self.scheduled_date),
).action_open_orderpoints()

def _procure_orderpoint_confirm(
self, use_new_cursor=False, company_id=None, raise_user_error=True
):
"""Ensure orderpoints with different scheduled dates aren't processed together."""
scheduled_dates = set(self.mapped("scheduled_date"))
if len(scheduled_dates) > 1:
raise ValidationError(
_("Please select lines with the same scheduled date.")
)
return super(
stockWarehouseOrderpoint,
self.with_context(scheduled_date=self[0].scheduled_date),
)._procure_orderpoint_confirm(use_new_cursor, company_id, raise_user_error)

def _prepare_procurement_values(self, date=False, group=False):
"""Prepare procurement values with consideration for the scheduled date."""
values = super()._prepare_procurement_values(date=date, group=group)
if self.scheduled_date:
values["date_planned"] = self.scheduled_date
values["date_deadline"] = self.scheduled_date
# Reset scheduled_date after processing
self.scheduled_date = False
return values
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
<field name="inherit_id" ref="stock.view_warehouse_orderpoint_tree_editable"/>
<field name="arch" type="xml">
<field name="product_uom_name" position="after">
<field name="scheduled_date"/>
<field name="previous_scheduled_date" invisible="1"/>
<field name="scheduled_date" widget="Date"/>
</field>
</field>
</record>
Expand All @@ -17,8 +16,7 @@
<field name="inherit_id" ref="stock.view_warehouse_orderpoint_form"/>
<field name="arch" type="xml">
<field name="qty_multiple" position="after">
<field name="scheduled_date"/>
<field name="previous_scheduled_date" invisible="1"/>
<field name="scheduled_date" widget="Date"/>
</field>
</field>
</record>
Expand All @@ -32,7 +30,7 @@
<field name="type">ir.actions.server</field>
<field name="state">code</field>
<field name="code">
action = records.open_set_schedule_date_wizard()
action = records.action_open_set_schedule_date_wizard()
</field>
</record>

Expand Down

0 comments on commit 38fecff

Please sign in to comment.