From 6e9e91d29a522f7af935771a08e2fd3782874e7d Mon Sep 17 00:00:00 2001 From: Mikel Arregi Date: Fri, 9 Jul 2021 13:51:29 +0200 Subject: [PATCH] [ADD] mrp_supplier_price_generate_structure --- .../README.rst | 40 +++++++ .../__init__.py | 1 + .../__manifest__.py | 19 ++++ .../models/__init__.py | 2 + .../models/mrp_production_product_line.py | 22 ++++ .../models/stock_rule.py | 14 +++ .../tests/__init__.py | 1 + ...t_mrp_supplier_price_generate_structure.py | 106 ++++++++++++++++++ 8 files changed, 205 insertions(+) create mode 100644 mrp_supplier_price_generate_structure/README.rst create mode 100644 mrp_supplier_price_generate_structure/__init__.py create mode 100644 mrp_supplier_price_generate_structure/__manifest__.py create mode 100644 mrp_supplier_price_generate_structure/models/__init__.py create mode 100644 mrp_supplier_price_generate_structure/models/mrp_production_product_line.py create mode 100644 mrp_supplier_price_generate_structure/models/stock_rule.py create mode 100644 mrp_supplier_price_generate_structure/tests/__init__.py create mode 100644 mrp_supplier_price_generate_structure/tests/test_mrp_supplier_price_generate_structure.py diff --git a/mrp_supplier_price_generate_structure/README.rst b/mrp_supplier_price_generate_structure/README.rst new file mode 100644 index 000000000..2bce60e83 --- /dev/null +++ b/mrp_supplier_price_generate_structure/README.rst @@ -0,0 +1,40 @@ +================================================================ +Glue module mrp_supplier_price mrp_production_generate_structure +================================================================ + +.. |badge1| 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 + +|badge1| + +Extend mrp_production_generate_structure features using data provide by mrp_supplier_price module + +**Table of contents** + +.. contents:: + :local: + +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 +~~~~~~~ + +* AvanzOSC + +Contributors +~~~~~~~~~~~~ + +* Mikel Arregi +* Ana Juaristi diff --git a/mrp_supplier_price_generate_structure/__init__.py b/mrp_supplier_price_generate_structure/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/mrp_supplier_price_generate_structure/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mrp_supplier_price_generate_structure/__manifest__.py b/mrp_supplier_price_generate_structure/__manifest__.py new file mode 100644 index 000000000..321c46e18 --- /dev/null +++ b/mrp_supplier_price_generate_structure/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2021 Mikel Arregi Etxaniz - AvanzOSC +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html +{ + "name": "Glue module mrp_supplier_price mrp_production_generate_structure", + "version": "12.0.1.0.0", + "license": "AGPL-3", + "depends": [ + "mrp_supplier_price", "mrp_production_generate_structure", + ], + "author": "AvanzOSC", + "website": "http://www.avanzosc.es", + "category": "Tools", + "data": [ + + ], + 'demo': [], + 'installable': True, + 'auto_install': False, +} diff --git a/mrp_supplier_price_generate_structure/models/__init__.py b/mrp_supplier_price_generate_structure/models/__init__.py new file mode 100644 index 000000000..1c6534b72 --- /dev/null +++ b/mrp_supplier_price_generate_structure/models/__init__.py @@ -0,0 +1,2 @@ +from . import stock_rule +from . import mrp_production_product_line diff --git a/mrp_supplier_price_generate_structure/models/mrp_production_product_line.py b/mrp_supplier_price_generate_structure/models/mrp_production_product_line.py new file mode 100644 index 000000000..cd8025b5d --- /dev/null +++ b/mrp_supplier_price_generate_structure/models/mrp_production_product_line.py @@ -0,0 +1,22 @@ +# Copyright 2021 Mikel Arregi Etxaniz - AvanzOSC +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from odoo import models + + +class MrpProductionProductLine(models.Model): + _inherit = 'mrp.production.product.line' + + def _get_po_values(self, rule): + res = super()._get_po_values(rule) + res.update({'partner_id': self.supplier_id.id}) + return res + + def create_automatic_purchase_order(self, origin_manufacture_order, level): + if self.supplier_id not in self.product_id.seller_ids.mapped( + 'name'): + self.env['product.supplierinfo'].create({ + 'name': self.supplier_id.id, + 'product_tmpl_id': self.product_id.product_tmpl_id.id, + }) + return super().create_automatic_purchase_order( + origin_manufacture_order, level) diff --git a/mrp_supplier_price_generate_structure/models/stock_rule.py b/mrp_supplier_price_generate_structure/models/stock_rule.py new file mode 100644 index 000000000..2619d2011 --- /dev/null +++ b/mrp_supplier_price_generate_structure/models/stock_rule.py @@ -0,0 +1,14 @@ +# Copyright 2021 Mikel Arregi Etxaniz - AvanzOSC +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from odoo import api, models + + +class StockRule(models.Model): + _inherit = "stock.rule" + + @api.multi + def _make_po_select_supplier(self, values, suppliers): + partner_id = values.get('partner_id') + if partner_id: + return suppliers.filtered(lambda x: x.name.id == partner_id)[0] + return super()._make_po_select_supplier(values, suppliers) diff --git a/mrp_supplier_price_generate_structure/tests/__init__.py b/mrp_supplier_price_generate_structure/tests/__init__.py new file mode 100644 index 000000000..9625e9bf3 --- /dev/null +++ b/mrp_supplier_price_generate_structure/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mrp_supplier_price_generate_structure diff --git a/mrp_supplier_price_generate_structure/tests/test_mrp_supplier_price_generate_structure.py b/mrp_supplier_price_generate_structure/tests/test_mrp_supplier_price_generate_structure.py new file mode 100644 index 000000000..3cb74edc1 --- /dev/null +++ b/mrp_supplier_price_generate_structure/tests/test_mrp_supplier_price_generate_structure.py @@ -0,0 +1,106 @@ +# Copyright 2021 Mikel Arregi - AvanzOSC +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html + +from odoo.tests import common +from odoo.exceptions import UserError + + +@common.at_install(False) +@common.post_install(True) +class MrpSupplierPriceGenerateStructureTest(common.SavepointCase): + + @classmethod + def setUpClass(cls): + super(MrpSupplierPriceGenerateStructureTest, cls).setUpClass() + cls.mrp_production_model = cls.env['mrp.production'] + cls.bom_model = cls.env['mrp.bom'] + cls.product_model = cls.env['product.product'] + cls.buy_route = cls.env.ref("purchase_stock.route_warehouse0_buy") + cls.supplier = cls.env['res.partner'].create({ + 'name': 'Supplier Test', + 'supplier': True, + }) + cls.supplier2 = cls.env['res.partner'].create({ + 'name': 'Supplier Test2', + 'supplier': True, + }) + cls.supplier3 = cls.env['res.partner'].create({ + 'name': 'Supplier Test3', + 'supplier': True, + }) + bom_product = cls.product_model.create({ + 'name': 'BoM product', + }) + cls.component1 = cls.product_model.create({ + 'name': 'Component1', + 'standard_price': 10.0, + }) + cls.component2 = cls.product_model.create({ + 'name': 'Component2', + 'standard_price': 15.0, + 'route_ids': [(4, cls.buy_route.id)], + }) + cls.env['product.supplierinfo'].create({ + 'name': cls.supplier.id, + 'product_tmpl_id': cls.component2.product_tmpl_id.id, + 'min_qty': 1.0, + 'price': 10.0, + }) + cls.env['product.supplierinfo'].create({ + 'name': cls.supplier.id, + 'product_tmpl_id': cls.component2.product_tmpl_id.id, + 'min_qty': 10.0, + 'price': 8.0, + }) + cls.env['product.supplierinfo'].create({ + 'name': cls.supplier2.id, + 'product_tmpl_id': cls.component2.product_tmpl_id.id, + 'min_qty': 10.0, + 'price': 8.0, + }) + vals = { + 'product_tmpl_id': bom_product.product_tmpl_id.id, + 'product_id': bom_product.id, + 'bom_line_ids': + [(0, 0, {'product_id': cls.component1.id, + 'product_qty': 2.0}), + (0, 0, {'product_id': cls.component2.id, + 'product_qty': 12.0}), + (0, 0, {'product_id': cls.component2.id, + 'product_qty': 8.0}), + (0, 0, {'product_id': cls.component2.id, + 'product_qty': 4.0}), + ], + } + cls.mrp_bom = cls.bom_model.create(vals) + cls.production = cls.mrp_production_model.create({ + 'product_id': bom_product.id, + 'product_uom_id': bom_product.uom_id.id, + 'bom_id': cls.mrp_bom.id, + }) + + def test_mrp_production(self): + self.production.action_compute() + count = 0 + for line in self.production.product_line_ids: + if line.product_id.id == self.component2.id and count==0: + line.supplier_id = self.supplier2.id + line.button_create_purchase_manufacturing_order() + self.assertTrue( + line.purchase_order_line_id.order_id.partner_id == + self.supplier2) + count += 1 + elif line.product_id.id == self.component2.id and count==1: + line.supplier_id = self.supplier3.id + line.button_create_purchase_manufacturing_order() + self.assertTrue( + line.purchase_order_line_id.order_id.partner_id == + self.supplier3) + count += 1 + elif line.product_id.id == self.component2.id: + line.button_create_purchase_manufacturing_order() + self.assertTrue( + line.purchase_order_line_id.order_id.partner_id == + self.supplier) + self.assertTrue(self.supplier3 in self.component2.seller_ids.mapped( + 'name'))