diff --git a/mrp_multi_level_estimate/README.rst b/mrp_multi_level_estimate/README.rst new file mode 100644 index 000000000..2406e8d02 --- /dev/null +++ b/mrp_multi_level_estimate/README.rst @@ -0,0 +1,90 @@ +======================== +MRP Multi Level Estimate +======================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fmanufacture-lightgray.png?logo=github + :target: https://github.com/OCA/manufacture/tree/12.0/mrp_multi_level_estimate + :alt: OCA/manufacture +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/manufacture-12-0/manufacture-12-0-mrp_multi_level_estimate + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/129/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Integration for MRP Multi Level and `Stock Demand Estimates `_ system. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +You can edit how to consolidate your estimates as demand at product MRP area +level using the field *Group Days of Estimates*. This number represents the +days to group your estimates as demand for the MRP, e.g: if set to 7, you will +have your estimates (regardless of the date range used) grouped in weekly +demand. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Eficent + +Contributors +~~~~~~~~~~~~ + +* Lois Rilo + +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. + +.. |maintainer-lreficent| image:: https://github.com/lreficent.png?size=40px + :target: https://github.com/lreficent + :alt: lreficent + +Current `maintainer `__: + +|maintainer-lreficent| + +This module is part of the `OCA/manufacture `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mrp_multi_level_estimate/__init__.py b/mrp_multi_level_estimate/__init__.py new file mode 100644 index 000000000..aee8895e7 --- /dev/null +++ b/mrp_multi_level_estimate/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizards diff --git a/mrp_multi_level_estimate/__manifest__.py b/mrp_multi_level_estimate/__manifest__.py new file mode 100644 index 000000000..a57ff4203 --- /dev/null +++ b/mrp_multi_level_estimate/__manifest__.py @@ -0,0 +1,25 @@ +# Copyright 2019 Eficent Business and IT Consulting Services S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/Agpl.html). + +{ + "name": "MRP Multi Level Estimate", + "version": "12.0.1.0.0", + "development_status": "Beta", + "license": "AGPL-3", + "author": "Eficent, " + "Odoo Community Association (OCA)", + "maintainers": ["lreficent"], + "summary": "Allows to consider demand estimates using MRP multi level.", + "website": "https://github.com/OCA/manufacture", + "category": "Manufacturing", + "depends": [ + "mrp_multi_level", + "stock_demand_estimate", + ], + "data": [ + "views/product_mrp_area_views.xml", + ], + "installable": True, + "application": False, + "auto_install": True, +} diff --git a/mrp_multi_level_estimate/models/__init__.py b/mrp_multi_level_estimate/models/__init__.py new file mode 100644 index 000000000..3bf74fdf9 --- /dev/null +++ b/mrp_multi_level_estimate/models/__init__.py @@ -0,0 +1 @@ +from . import product_mrp_area diff --git a/mrp_multi_level_estimate/models/product_mrp_area.py b/mrp_multi_level_estimate/models/product_mrp_area.py new file mode 100644 index 000000000..f76b8d0d8 --- /dev/null +++ b/mrp_multi_level_estimate/models/product_mrp_area.py @@ -0,0 +1,22 @@ +# Copyright 2019 Eficent Business and IT Consulting Services S.L. +# - Lois Rilo Antelo +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class ProductMRPArea(models.Model): + _inherit = "product.mrp.area" + + group_estimate_days = fields.Integer( + string="Group Days of Estimates", + default=1, + help="The days to group your estimates as demand for the MRP." + "It can be different from the lenght of the date ranges you " + "use in the estimates.", + ) + + _sql_constraints = [ + ("group_estimate_days_check", "CHECK( group_estimate_days >= 0 )", + "Group Days of Estimates must be greater than or equal to zero."), + ] diff --git a/mrp_multi_level_estimate/readme/CONFIGURE.rst b/mrp_multi_level_estimate/readme/CONFIGURE.rst new file mode 100644 index 000000000..1b720c2e6 --- /dev/null +++ b/mrp_multi_level_estimate/readme/CONFIGURE.rst @@ -0,0 +1,5 @@ +You can edit how to consolidate your estimates as demand at product MRP area +level using the field *Group Days of Estimates*. This number represents the +days to group your estimates as demand for the MRP, e.g: if set to 7, you will +have your estimates (regardless of the date range used) grouped in weekly +demand. diff --git a/mrp_multi_level_estimate/readme/CONTRIBUTORS.rst b/mrp_multi_level_estimate/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..4b574636c --- /dev/null +++ b/mrp_multi_level_estimate/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Lois Rilo diff --git a/mrp_multi_level_estimate/readme/DESCRIPTION.rst b/mrp_multi_level_estimate/readme/DESCRIPTION.rst new file mode 100644 index 000000000..e7f56f84a --- /dev/null +++ b/mrp_multi_level_estimate/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Integration for MRP Multi Level and `Stock Demand Estimates `_ system. diff --git a/mrp_multi_level_estimate/static/description/icon.png b/mrp_multi_level_estimate/static/description/icon.png new file mode 100644 index 000000000..b1cef6c4c Binary files /dev/null and b/mrp_multi_level_estimate/static/description/icon.png differ diff --git a/mrp_multi_level_estimate/static/description/index.html b/mrp_multi_level_estimate/static/description/index.html new file mode 100644 index 000000000..186f38560 --- /dev/null +++ b/mrp_multi_level_estimate/static/description/index.html @@ -0,0 +1,430 @@ + + + + + + +MRP Multi Level Estimate + + + +
+

MRP Multi Level Estimate

+ + +

Beta License: AGPL-3 OCA/manufacture Translate me on Weblate Try me on Runbot

+

Integration for MRP Multi Level and Stock Demand Estimates system.

+

Table of contents

+ +
+

Configuration

+

You can edit how to consolidate your estimates as demand at product MRP area +level using the field Group Days of Estimates. This number represents the +days to group your estimates as demand for the MRP, e.g: if set to 7, you will +have your estimates (regardless of the date range used) grouped in weekly +demand.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Eficent
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

Current maintainer:

+

lreficent

+

This module is part of the OCA/manufacture project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/mrp_multi_level_estimate/tests/__init__.py b/mrp_multi_level_estimate/tests/__init__.py new file mode 100644 index 000000000..f26efbd60 --- /dev/null +++ b/mrp_multi_level_estimate/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mrp_multi_level_estimate diff --git a/mrp_multi_level_estimate/tests/test_mrp_multi_level_estimate.py b/mrp_multi_level_estimate/tests/test_mrp_multi_level_estimate.py new file mode 100644 index 000000000..66161a18e --- /dev/null +++ b/mrp_multi_level_estimate/tests/test_mrp_multi_level_estimate.py @@ -0,0 +1,128 @@ +# Copyright 2018-19 Eficent Business and IT Consulting Services S.L. +# (http://www.eficent.com) +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from datetime import datetime, timedelta + +from dateutil.rrule import WEEKLY +from odoo.addons.mrp_multi_level.tests.common import TestMrpMultiLevelCommon + + +class TestMrpMultiLevelEstimate(TestMrpMultiLevelCommon): + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.estimate_obj = cls.env["stock.demand.estimate"] + + # Create new clean area: + cls.estimate_loc = cls.loc_obj.create({ + "name": "Test location for estimates", + "usage": "internal", + "location_id": cls.wh.view_location_id.id, + }) + cls.estimate_area = cls.mrp_area_obj.create({ + "name": "Test", + "warehouse_id": cls.wh.id, + "location_id": cls.estimate_loc.id, + }) + cls.product_mrp_area_obj.create({ + 'product_id': cls.prod_test.id, + 'mrp_area_id': cls.estimate_area.id, + 'mrp_nbr_days': 7, + }) + + # Create Date Ranges: + cls.dr_type = cls.env["date.range.type"].create({ + "name": "Weeks", + "company_id": False, + "allow_overlap": False, + }) + today = datetime.today().replace(hour=0) + generator = cls.env["date.range.generator"].create({ + "date_start": today - timedelta(days=3), + "name_prefix": "W-", + "type_id": cls.dr_type.id, + "duration_count": 1, + "unit_of_time": WEEKLY, + "count": 3}) + generator.action_apply() + + # Create Demand Estimates: + ranges = cls.env["date.range"].search( + [("type_id", "=", cls.dr_type.id)]) + qty = 140.0 + for dr in ranges: + qty += 70.0 + cls._create_demand_estimate( + cls.prod_test, cls.stock_location, dr, qty) + cls._create_demand_estimate( + cls.prod_test, cls.estimate_loc, dr, qty) + + cls.mrp_multi_level_wiz.create({}).run_mrp_multi_level() + + @classmethod + def _create_demand_estimate(cls, product, location, date_range, qty): + cls.estimate_obj.create({ + "product_id": product.id, + "location_id": location.id, + "product_uom": product.uom_id.id, + "product_uom_qty": qty, + "date_range_id": date_range.id, + }) + + def test_01_demand_estimates(self): + """Tests demand estimates integration.""" + estimates = self.estimate_obj.search([ + ("product_id", "=", self.prod_test.id), + ("location_id", "=", self.stock_location.id)]) + self.assertEqual(len(estimates), 3) + moves = self.mrp_move_obj.search([ + ("product_id", "=", self.prod_test.id), + ("mrp_area_id", "=", self.mrp_area.id), + ]) + # 3 weeks - 3 days in the past = 18 days of valid estimates: + moves_from_estimates = moves.filtered(lambda m: m.mrp_type == "d") + self.assertEqual(len(moves_from_estimates), 18) + quantities = moves_from_estimates.mapped("mrp_qty") + self.assertIn(-30.0, quantities) # 210 a week => 30.0 dayly: + self.assertIn(-40.0, quantities) # 280 a week => 40.0 dayly: + self.assertIn(-50.0, quantities) # 350 a week => 50.0 dayly: + plans = self.planned_order_obj.search([ + ("product_id", "=", self.prod_test.id), + ("mrp_area_id", "=", self.mrp_area.id), + ]) + action = list(set(plans.mapped("mrp_action"))) + self.assertEqual(len(action), 1) + self.assertEqual(action[0], "buy") + self.assertEqual(len(plans), 18) + inventories = self.mrp_inventory_obj.search([ + ("mrp_area_id", "=", self.estimate_area.id)]) + self.assertEqual(len(inventories), 18) + + def test_02_group_demand_estimates(self): + """Test demand grouping functionality, `nbr_days`.""" + estimates = self.estimate_obj.search([ + ("product_id", "=", self.prod_test.id), + ("location_id", "=", self.estimate_loc.id)]) + self.assertEqual(len(estimates), 3) + moves = self.mrp_move_obj.search([ + ("product_id", "=", self.prod_test.id), + ("mrp_area_id", "=", self.estimate_area.id), + ]) + supply_plans = self.planned_order_obj.search([ + ("product_id", "=", self.prod_test.id), + ("mrp_area_id", "=", self.estimate_area.id), + ]) + # 3 weeks - 3 days in the past = 18 days of valid estimates: + moves_from_estimates = moves.filtered(lambda m: m.mrp_type == "d") + self.assertEqual(len(moves_from_estimates), 18) + # 18 days of demand / 7 nbr_days = 2.57 => 3 supply moves expected. + self.assertEqual(len(supply_plans), 3) + quantities = supply_plans.mapped("mrp_qty") + week_1_expected = sum(moves_from_estimates[0:7].mapped("mrp_qty")) + self.assertIn(abs(week_1_expected), quantities) + week_2_expected = sum(moves_from_estimates[7:14].mapped("mrp_qty")) + self.assertIn(abs(week_2_expected), quantities) + week_3_expected = sum(moves_from_estimates[14:].mapped("mrp_qty")) + self.assertIn(abs(week_3_expected), quantities) diff --git a/mrp_multi_level_estimate/views/product_mrp_area_views.xml b/mrp_multi_level_estimate/views/product_mrp_area_views.xml new file mode 100644 index 000000000..cf1f382b1 --- /dev/null +++ b/mrp_multi_level_estimate/views/product_mrp_area_views.xml @@ -0,0 +1,16 @@ + + + + + product.mrp.area.form - estimates + product.mrp.area + form + + + + + + + + + diff --git a/mrp_multi_level_estimate/wizards/__init__.py b/mrp_multi_level_estimate/wizards/__init__.py new file mode 100644 index 000000000..869fb19c7 --- /dev/null +++ b/mrp_multi_level_estimate/wizards/__init__.py @@ -0,0 +1 @@ +from . import mrp_multi_level diff --git a/mrp_multi_level_estimate/wizards/mrp_multi_level.py b/mrp_multi_level_estimate/wizards/mrp_multi_level.py new file mode 100644 index 000000000..70c47285e --- /dev/null +++ b/mrp_multi_level_estimate/wizards/mrp_multi_level.py @@ -0,0 +1,75 @@ +# Copyright 2019 Eficent Business and IT Consulting Services S.L. +# - Lois Rilo +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +import logging +from datetime import timedelta +from odoo import api, fields, models +from odoo.tools.float_utils import float_round +logger = logging.getLogger(__name__) + + +class MultiLevelMrp(models.TransientModel): + _inherit = "mrp.multi.level" + + @api.model + def _prepare_mrp_move_data_from_estimate( + self, estimate, product_mrp_area, date): + mrp_type = "d" + origin = "fc" + daily_qty = float_round( + estimate.daily_qty, + precision_rounding=product_mrp_area.product_id.uom_id.rounding, + rounding_method="HALF-UP") + return { + "mrp_area_id": product_mrp_area.mrp_area_id.id, + "product_id": product_mrp_area.product_id.id, + "product_mrp_area_id": product_mrp_area.id, + "production_id": None, + "purchase_order_id": None, + "purchase_line_id": None, + "stock_move_id": None, + "mrp_qty": -daily_qty * product_mrp_area.group_estimate_days, + "current_qty": -daily_qty, + "mrp_date": date, + "current_date": date, + "mrp_type": mrp_type, + "mrp_origin": origin, + "mrp_order_number": None, + "parent_product_id": None, + "name": "Forecast", + "state": "confirmed", + } + + @api.model + def _estimates_domain(self, product_mrp_area): + locations = product_mrp_area.mrp_area_id._get_locations() + return [ + ("product_id", "=", product_mrp_area.product_id.id), + ("location_id", "in", locations.ids), + ("date_range_id.date_end", ">=", fields.Date.today()), + ] + + @api.model + def _init_mrp_move_from_forecast(self, product_mrp_area): + res = super(MultiLevelMrp, self)._init_mrp_move_from_forecast( + product_mrp_area) + if not product_mrp_area.group_estimate_days: + return False + today = fields.Date.today() + domain = self._estimates_domain(product_mrp_area) + estimates = self.env["stock.demand.estimate"].search(domain) + for rec in estimates: + start = rec.date_range_id.date_start + if start < today: + start = today + mrp_date = fields.Date.from_string(start) + date_end = fields.Date.from_string(rec.date_range_id.date_end) + delta = timedelta(days=product_mrp_area.group_estimate_days) + while mrp_date <= date_end: + mrp_move_data = \ + self._prepare_mrp_move_data_from_estimate( + rec, product_mrp_area, mrp_date) + self.env["mrp.move"].create(mrp_move_data) + mrp_date += delta + return res