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 16, 2024
1 parent 14a5941 commit fba9cc7
Show file tree
Hide file tree
Showing 15 changed files with 214 additions and 277 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 <akim.juillerat@camptocamp.com>
* Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
* ACSONE SA/NV
* Dung Tran <dungtd@trobz.com>

Other credits
Expand Down
4 changes: 2 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,8 @@
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": ["sale_stock", "stock_orderpoint_manual_procurement"],
"depends": ["base_partition", "product_route_mto", "stock"],
"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.

141 changes: 141 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,141 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


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

is_missing_default_orderpoint_for_mto = fields.Boolean(
compute="_compute_is_missing_default_orderpoint_for_mto",
)

@api.depends("is_mto", "orderpoint_ids", "type")
def _compute_is_missing_default_orderpoint_for_mto(self):
default_company = self.env["res.company"]._get_main_company()
for product in self:
company = product.company_id or default_company
len_wh = len(
self.env["stock.warehouse"].search([("company_id", "=", company.id)])
)
len_wh_orderpoint = len(
product.orderpoint_ids.partition("warehouse_id").keys()
)
product.is_missing_default_orderpoint_for_mto = (
product.is_mto
and product.type == "product"
and len_wh != len_wh_orderpoint
)

def _create_default_orderpoint_for_mto(self):
default_company = self.env["res.company"]._get_main_company()
for company, products in self.partition("company_id").items():
company = company or default_company
warehouses = self.env["stock.warehouse"].search(
[("company_id", "=", company.id)]
)
for product in products:
if not product.is_missing_default_orderpoint_for_mto:
continue
for warehouse in warehouses:
product._get_mto_orderpoint(warehouse)

def _get_mto_orderpoint(self, warehouse):
self.ensure_one()
orderpoint = (
self.env["stock.warehouse.orderpoint"]
.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)
self.env["stock.warehouse.orderpoint"].create(vals)
return orderpoint

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.
"""
self.filtered(
"is_missing_default_orderpoint_for_mto"
)._create_default_orderpoint_for_mto()

@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(
[("archive_orderpoints_mto_removal", "=", True)]
)
if warehouses:
locations = warehouses._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.

4 changes: 3 additions & 1 deletion stock_orderpoint_mto_as_mts/models/stock_warehouse.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# 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"

archive_orderpoints_mto_removal = fields.Boolean(default=False)

def _get_locations_for_mto_orderpoints(self):
return self.mapped("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 <akim.juillerat@camptocamp.com>
* Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
* ACSONE SA/NV
* Dung Tran <dungtd@trobz.com>

0 comments on commit fba9cc7

Please sign in to comment.