Skip to content

Commit

Permalink
fixup!
Browse files Browse the repository at this point in the history
  • Loading branch information
yostashiro authored and AungKoKoLin1997 committed Jan 21, 2025
1 parent f1277c7 commit d4a7f4d
Show file tree
Hide file tree
Showing 31 changed files with 828 additions and 287 deletions.
161 changes: 161 additions & 0 deletions sale_lead_time/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
==============
Sale Lead Time
==============

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:79be59475657d45c9881f4693df4e99894b904631029a20ab08f12cbfa64d303
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github
:target: https://github.com/OCA/sale-workflow/tree/16.0/sale_lead_time
:alt: OCA/sale-workflow
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/sale-workflow-16-0/sale-workflow-16-0-sale_lead_time
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/sale-workflow&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module enhances the sales order process by adding a delivery_lead_time that is
determined based on the most closely matching lead time profile.
This time is then incorporated into the customer_lead of each sale order line,
optimizing delivery scheduling based on specific lead time configurations.

**Table of contents**

.. contents::
:local:

Configuration
=============

To configure this module, you must first properly set up your lead time profiles:

1. Go to *Inventory > Configuration > Settings*. Find the section Delivery Lead Time
Settings, and update the factors of warehouse, country, state and partner according
to your specific requirements. Setting 0.0 means that matches of the corresponding
field will not be counted in score calculation of the lead time profile.
2. Navigate to *Inventory > Configuration > Lead Time Profiles*, and create records
according to the reality of delivery logistics.

Example of how most matched lead time profile is determined:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When there are lead time profile records like this, and you have an order to deliver
from Main WH to Cust A:

+---------+-------+---------+-----------+------------------+
| Country | State | Partner | Warehouse | Lead Time (Days) |
+=========+=======+=========+===========+==================+
| Japan | Tokyo | Cust A | | 3.0 |
+---------+-------+---------+-----------+------------------+
| Japan | Tokyo | | Main WH | 5.0 |
+---------+-------+---------+-----------+------------------+
| Japan | Tokyo | | 2nd WH | 7.0 |
+---------+-------+---------+-----------+------------------+

If the factors are configured to be like this:

- Country: 1.0
- State: 1.0
- Partner: 1.0
- Warehouse: 2.0

The scores of each profile will be calculated as follows:

+---------+-------+---------+-----------+------------------+-------------------------------+
| Country | State | Partner | Warehouse | Lead Time (Days) | Score Calculation |
+=========+=======+=========+===========+==================+===============================+
| Japan | Tokyo | Cust A | | 3.0 | 1.0 + 1.0 + 1.0 + 0.0 = 3.0 |
+---------+-------+---------+-----------+------------------+-------------------------------+
| Japan | Tokyo | | Main WH | 5.0 | 1.0 + 1.0 + 0.0 + 2.0 = 4.0 |
+---------+-------+---------+-----------+------------------+-------------------------------+
| Japan | Tokyo | | 2nd WH | 7.0 | N/A for warehouse mismatch |
+---------+-------+---------+-----------+------------------+-------------------------------+

As a result, 5.0 days of the Delivery Lead Time will be proposed for the sales order.

In a tie-breaking situation, the lead time profile with the lowest lead time will be chosen.

Usage
=====

The system proposes the **Delivery Lead Time** in sales orders (Other Info tab) based on
the most closely matching lead time profile, determined by the warehouse and delivery
address.

The **Delivery Lead Time** can be manually updated as necessary.

The **Delivery Lead Time** is incorporated into the lead time of each sales order line
by adding days to the lead time proposed based on the standard logic, which governs
delivery scheduling.

Example:
~~~~~~~~

Assume the **Sales Security Lead Time** is set to 2.0 days.

In vanilla Odoo, for a sales order with the following details:

- **Order Date**: 2025-01-19 18:00
- **Order Line Lead Time**: 5.0 days

The delivery order dates would be:

- **Scheduled Date**: 2025-01-22 18:00
- **Deadline**: 2025-01-24 18:00:00

If this module is installed and the **Delivery Lead Time** proposed is 3.0 days, the
values will be updated as follows:

- **Order Line Lead Time**: 8.0 days
- **Deadline (Delivery)**: 2025-01-27 18:00

The **Scheduled Date** will remain unchanged.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/sale-workflow/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/OCA/sale-workflow/issues/new?body=module:%20sale_lead_time%0Aversion:%2016.0%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.

Credits
=======

Authors
~~~~~~~

* Quartile

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/sale-workflow <https://github.com/OCA/sale-workflow/tree/16.0/sale_lead_time>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Copyright 2025 Quartile
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
"name": "Sale Lead Time Profile",
"name": "Sale Lead Time",
"version": "16.0.1.0.0",
"author": "Quartile, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/sale-workflow",
"license": "AGPL-3",
"depends": ["sale_stock"],
"data": [
"security/lead_time_profile_security.xml",
"security/ir.model.access.csv",
"views/lead_time_profile_views.xml",
"views/res_config_settings_view.xml",
"views/sale_order_views.xml",
],
"installable": True,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from . import lead_time_profile
from . import res_company
from . import res_config_settings
from . import sale_order
from . import sale_order_line
90 changes: 90 additions & 0 deletions sale_lead_time/models/lead_time_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Copyright 2025 Quartile
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models


class LeadTimeProfile(models.Model):
_name = "lead.time.profile"
_description = "Lead Time Profile"
_order = "country_id, state_id, partner_id, warehouse_id"

warehouse_id = fields.Many2one(
"stock.warehouse", help="Matched against the warehouse of the sales order."
)
partner_id = fields.Many2one(
"res.partner", help="Matched against the delivery address of the sales order."
)
state_id = fields.Many2one(
"res.country.state",
domain="[('country_id', '=?', country_id)]",
help="Matched against the state of the delivery address of the sales order.",
)
country_id = fields.Many2one(
"res.country",
help="Matched against the country of the delivery address of the sales order.",
)
company_id = fields.Many2one(
"res.company", required=True, default=lambda self: self.env.company
)
lead_time = fields.Float(string="Lead Time (Days)", required=True)

@api.onchange("partner_id")
def _onchange_partner_id(self):
for rec in self:
if rec.partner_id:
rec.state_id = rec.partner_id.state_id

Check warning on line 36 in sale_lead_time/models/lead_time_profile.py

View check run for this annotation

Codecov / codecov/patch

sale_lead_time/models/lead_time_profile.py#L36

Added line #L36 was not covered by tests

@api.onchange("state_id")
def _onchange_state_id(self):
for rec in self:
if rec.partner_id.state_id != rec.state_id:
rec.partner_id = False
rec.country_id = rec.state_id.country_id

Check warning on line 43 in sale_lead_time/models/lead_time_profile.py

View check run for this annotation

Codecov / codecov/patch

sale_lead_time/models/lead_time_profile.py#L42-L43

Added lines #L42 - L43 were not covered by tests

@api.onchange("country_id")
def _onchange_country_id(self):
for rec in self:
if rec.state_id.country_id != rec.country_id:
rec.state_id = False

Check warning on line 49 in sale_lead_time/models/lead_time_profile.py

View check run for this annotation

Codecov / codecov/patch

sale_lead_time/models/lead_time_profile.py#L49

Added line #L49 was not covered by tests
if rec.partner_id.country_id != rec.country_id:
rec.partner_id = False

Check warning on line 51 in sale_lead_time/models/lead_time_profile.py

View check run for this annotation

Codecov / codecov/patch

sale_lead_time/models/lead_time_profile.py#L51

Added line #L51 was not covered by tests

def _get_score(self, **kwargs):
"""Return a matching score for this lead time profile.
The method scores each relevant match (warehouse/country/state/partner)
based on factors defined in the company. For example, if the partner matches,
the score is increased by the factor_partner. If any mismatch is found, it
immediately returns -1.
:param kwargs: Dictionary containing 'warehouse' and 'partner'.
:return: A float representing the total match score if no mismatch is found,
or -1 if any mismatch is found.
"""
self.ensure_one()
score = 0
partner = kwargs.get("partner")
warehouse = kwargs.get("warehouse")
company = self.company_id
if self.partner_id:
if partner == self.partner_id:
score += company.factor_partner
else:
return -1

Check warning on line 74 in sale_lead_time/models/lead_time_profile.py

View check run for this annotation

Codecov / codecov/patch

sale_lead_time/models/lead_time_profile.py#L74

Added line #L74 was not covered by tests
if self.state_id:
if partner.state_id == self.state_id:
score += company.factor_state
else:
return -1

Check warning on line 79 in sale_lead_time/models/lead_time_profile.py

View check run for this annotation

Codecov / codecov/patch

sale_lead_time/models/lead_time_profile.py#L79

Added line #L79 was not covered by tests
if self.country_id:
if partner.country_id == self.country_id:
score += company.factor_country
else:
return -1

Check warning on line 84 in sale_lead_time/models/lead_time_profile.py

View check run for this annotation

Codecov / codecov/patch

sale_lead_time/models/lead_time_profile.py#L84

Added line #L84 was not covered by tests
if self.warehouse_id:
if warehouse == self.warehouse_id:
score += company.factor_warehouse
else:
return -1

Check warning on line 89 in sale_lead_time/models/lead_time_profile.py

View check run for this annotation

Codecov / codecov/patch

sale_lead_time/models/lead_time_profile.py#L89

Added line #L89 was not covered by tests
return score
13 changes: 13 additions & 0 deletions sale_lead_time/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2025 Quartile
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

factor_warehouse = fields.Float(default=1.0)
factor_country = fields.Float(default=1.0)
factor_state = fields.Float(default=1.0)
factor_partner = fields.Float(default=1.0)
24 changes: 24 additions & 0 deletions sale_lead_time/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2025 Quartile
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

factor_warehouse = fields.Float(
related="company_id.factor_warehouse", readonly=False
)
factor_country = fields.Float(related="company_id.factor_country", readonly=False)
factor_state = fields.Float(related="company_id.factor_state", readonly=False)
factor_partner = fields.Float(related="company_id.factor_partner", readonly=False)

def open_lead_time_profile_list(self):
self.ensure_one()
return {

Check warning on line 19 in sale_lead_time/models/res_config_settings.py

View check run for this annotation

Codecov / codecov/patch

sale_lead_time/models/res_config_settings.py#L18-L19

Added lines #L18 - L19 were not covered by tests
"type": "ir.actions.act_window",
"name": "Lead Time Profiles",
"res_model": "lead.time.profile",
"view_mode": "tree",
}
77 changes: 77 additions & 0 deletions sale_lead_time/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright 2025 Quartile
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models
from odoo.osv import expression


class SaleOrder(models.Model):
_inherit = "sale.order"

delivery_lead_time = fields.Float(
compute="_compute_delivery_lead_time",
store=True,
readonly=False,
help="Number of days expected for delivery from the warehouse to the delivery address.",
)

def _get_partner_address_domain(self):
self.ensure_one()
partner = self.partner_shipping_id
partner_domain = [("partner_id", "=", partner.id)]
state_domain = [
("partner_id", "=", False),
("state_id", "=", partner.state_id.id),
]
country_domain = [
("partner_id", "=", False),
("state_id", "=", False),
("country_id", "=", partner.country_id.id),
]
no_address_domain = [
("partner_id", "=", False),
("state_id", "=", False),
("country_id", "=", False),
]
return expression.OR(
[partner_domain, state_domain, country_domain, no_address_domain]
)

def _get_warehouse_domain(self):
self.ensure_one()
return [
"|",
("warehouse_id", "=", self.warehouse_id.id),
("warehouse_id", "=", False),
]

@api.depends("partner_shipping_id", "warehouse_id")
def _compute_delivery_lead_time(self):
for rec in self:
rec.delivery_lead_time = 0.0
if not rec.partner_shipping_id:
continue
partner_address_domain = rec._get_partner_address_domain()
warehouse_domain = rec._get_warehouse_domain()
domain = expression.AND([partner_address_domain, warehouse_domain])
profiles = self.env["lead.time.profile"].search(domain)
if not profiles:
continue
profile_scores = {
profile: profile._get_score(
**{
"partner": rec.partner_shipping_id,
"warehouse": rec.warehouse_id,
}
)
for profile in profiles
}
# In case of a tie, pick the profile with the lowest lead time.
best_profile = max(
profiles,
key=lambda profile: (profile_scores[profile], -profile.lead_time),
default=None,
)
rec.delivery_lead_time = (
best_profile.lead_time if profile_scores[best_profile] >= 0.0 else 0.0
)
File renamed without changes.
Loading

0 comments on commit d4a7f4d

Please sign in to comment.