Skip to content

Commit

Permalink
[IMP] stock_orderpoint_mto_as_mts
Browse files Browse the repository at this point in the history
  • Loading branch information
tuantrantg committed Apr 25, 2024
1 parent 14a5941 commit 65bb160
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 278 deletions.
3 changes: 2 additions & 1 deletion stock_orderpoint_mto_as_mts/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Sale Stock Mto As Mts Orderpoint
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:9d20edbec95507caa98c58ff6804d63697bb6169bf0b50f59bffba1a8b383b0b
!! source digest: sha256:c8b1ce07ee2789445838ad44716c9e14a30bc2f72ed5b4cf970336217b7fdf4e
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
Expand Down Expand Up @@ -81,6 +81,7 @@ Contributors

* Akim Juillerat <[email protected]>
* Jacques-Etienne Baudoux (BCIM) <[email protected]>
* ACSONE SA/NV
* Dung Tran <[email protected]>

Other credits
Expand Down
9 changes: 7 additions & 2 deletions stock_orderpoint_mto_as_mts/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": ["sale_stock", "stock_orderpoint_manual_procurement"],
"depends": [
"base_partition",
"product_route_mto",
"stock",
"stock_orderpoint_default_location",
],
"data": [
"data/stock_data.xml",
"views/stock_warehouse_views.xml",
],
}
8 changes: 0 additions & 8 deletions stock_orderpoint_mto_as_mts/data/stock_data.xml

This file was deleted.

4 changes: 1 addition & 3 deletions stock_orderpoint_mto_as_mts/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
from . import product
from . import sale_order
from . import stock_move
from . import product_product
from . import stock_warehouse
45 changes: 0 additions & 45 deletions stock_orderpoint_mto_as_mts/models/product.py

This file was deleted.

123 changes: 123 additions & 0 deletions stock_orderpoint_mto_as_mts/models/product_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, models


class ProductProduct(models.Model):
_inherit = "product.product"

def _get_warehouse_missing_orderpoint_for_mto(self):
res = {}
wh_obj = self.env["stock.warehouse"]
for product in self:
wh_domain = [("mto_as_mts", "=", True)]
if product.company_id:
wh_domain.append(("company_id", "=", product.company_id.id))
wh = wh_obj.search(wh_domain)
wh_orderpoint = product.orderpoint_ids.partition("warehouse_id").keys()
wh_wo_orderpoint = wh.filtered(lambda r: r not in wh_orderpoint)
if wh_wo_orderpoint:
res[product] = wh_wo_orderpoint
return res

def _create_default_orderpoint_for_mto(self, warehouses):
self.ensure_one()
orderpoints = self.env["stock.warehouse.orderpoint"]
orderpoint_obj = self.env["stock.warehouse.orderpoint"]
for warehouse in warehouses:
orderpoint = orderpoint_obj.with_context(active_test=False).search(
[
("product_id", "=", self.id),
(
"location_id",
"=",
warehouse._get_locations_for_mto_orderpoints().id,
),
],
limit=1,
)
if orderpoint and not orderpoint.active:
orderpoint.write(
{"active": True, "product_min_qty": 0.0, "product_max_qty": 0.0}
)
elif not orderpoint:
vals = self._prepare_missing_orderpoint_vals(warehouse)
orderpoint = orderpoint_obj.create(vals)
orderpoints |= orderpoint
return orderpoints

def _prepare_missing_orderpoint_vals(self, warehouse):
self.ensure_one()
return {
"warehouse_id": warehouse.id,
"product_id": self.id,
"company_id": warehouse.company_id.id,
"product_min_qty": 0,
"product_max_qty": 0,
"location_id": warehouse._get_locations_for_mto_orderpoints().id,
"product_uom": self.uom_id.id,
}

def _ensure_default_orderpoint_for_mto(self):
"""Ensure that a default orderpoint is created for the MTO products.
that have no orderpoint yet.
"""
dict_wh = self._get_warehouse_missing_orderpoint_for_mto()
for product in dict_wh:
product._create_default_orderpoint_for_mto(dict_wh[product])

@api.model_create_multi
def create(self, vals_list):
products = super().create(vals_list)
products.sudo()._ensure_default_orderpoint_for_mto()
return products

def write(self, vals):
# Archive orderpoints when MTO route is removed
if "route_ids" not in vals:
res = super().write(vals)
self.sudo()._ensure_default_orderpoint_for_mto()
return res
mto_products = self._filter_mto_products()
res = super().write(vals)
not_mto_products = self._filter_mto_products(mto=False)
# products to update are the intersection of both recordsets
products_to_update = mto_products & not_mto_products
if products_to_update:
products_to_update._archive_orderpoints_on_mto_removal()
return res

def _filter_mto_products(self, mto=True):
if mto:
func = lambda p: p.is_mto # noqa
else:
func = lambda p: not p.is_mto # noqa
return self.filtered(func)

def _get_orderpoints_to_archive_domain(self):
domain = []
warehouses = self.env["stock.warehouse"].search(
[("mto_as_mts", "=", True), ("archive_orderpoints_mto_removal", "=", True)]
)
if warehouses:
locations = self.env["stock.location"]
for warehouse in warehouses:
locations |= warehouse._get_locations_for_mto_orderpoints()
domain.extend(
[
("product_id", "in", self.ids),
("product_min_qty", "=", 0.0),
("product_max_qty", "=", 0.0),
("location_id", "in", locations.ids),
]
)
return domain

def _archive_orderpoints_on_mto_removal(self):
domain = self._get_orderpoints_to_archive_domain()
if domain:
ops = self.env["stock.warehouse.orderpoint"].search(domain)
if ops:
ops.write({"active": False})
86 changes: 0 additions & 86 deletions stock_orderpoint_mto_as_mts/models/sale_order.py

This file was deleted.

18 changes: 0 additions & 18 deletions stock_orderpoint_mto_as_mts/models/stock_move.py

This file was deleted.

8 changes: 6 additions & 2 deletions stock_orderpoint_mto_as_mts/models/stock_warehouse.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Copyright 2020 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import models
from odoo import fields, models


class StockWarehouse(models.Model):

_inherit = "stock.warehouse"

mto_as_mts = fields.Boolean(default=False)
archive_orderpoints_mto_removal = fields.Boolean(default=False)

def _get_locations_for_mto_orderpoints(self):
return self.mapped("lot_stock_id")
self.ensure_one()
return self.default_orderpoint_location_id or self.lot_stock_id
1 change: 1 addition & 0 deletions stock_orderpoint_mto_as_mts/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Akim Juillerat <[email protected]>
* Jacques-Etienne Baudoux (BCIM) <[email protected]>
* ACSONE SA/NV
* Dung Tran <[email protected]>
Loading

0 comments on commit 65bb160

Please sign in to comment.