diff --git a/rma/models/stock_rule.py b/rma/models/stock_rule.py index 244e8419..58a007bb 100644 --- a/rma/models/stock_rule.py +++ b/rma/models/stock_rule.py @@ -41,5 +41,7 @@ class StockRule(models.Model): res["partner_id"] = line.delivery_address_id.id elif line.rma_id.partner_id: res["partner_id"] = line.rma_id.partner_id.id - res["price_unit"] = line._get_price_unit() + res["price_unit"] = line.with_context( + product_required=product_id + )._get_price_unit() return res diff --git a/rma_sale/models/procurement.py b/rma_sale/models/procurement.py index bc9a29ee..e57ad4fa 100644 --- a/rma_sale/models/procurement.py +++ b/rma_sale/models/procurement.py @@ -33,5 +33,7 @@ class StockRule(models.Model): line = self.env["rma.order.line"].browse([line]) if line.reference_move_id: return res - res["price_unit"] = line._get_price_unit() + res["price_unit"] = line.with_context( + product_required=product_id + )._get_price_unit() return res diff --git a/rma_sale_mrp/README.rst b/rma_sale_mrp/README.rst new file mode 100644 index 00000000..43d5b4bf --- /dev/null +++ b/rma_sale_mrp/README.rst @@ -0,0 +1,59 @@ +============================= +RMA Manufacturing Integration +============================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:0a029de8ab7176f3af957dcf8796bec39ee8fba332aea42ef559cf108dd14d50 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-ForgeFlow%2Fstock--rma-lightgray.png?logo=github + :target: https://github.com/ForgeFlow/stock-rma/tree/14.0/rma_mrp + :alt: ForgeFlow/stock-rma + +|badge1| |badge2| |badge3| + + +**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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ForgeFlow + +Contributors +~~~~~~~~~~~~ + +* Christopher Ormaza + +Maintainers +~~~~~~~~~~~ + +This module is part of the `ForgeFlow/stock-rma `_ project on GitHub. + +You are welcome to contribute. diff --git a/rma_sale_mrp/__init__.py b/rma_sale_mrp/__init__.py new file mode 100644 index 00000000..1cdbcf8b --- /dev/null +++ b/rma_sale_mrp/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) +from . import models diff --git a/rma_sale_mrp/__manifest__.py b/rma_sale_mrp/__manifest__.py new file mode 100644 index 00000000..f5b989ba --- /dev/null +++ b/rma_sale_mrp/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +{ + "name": "RMA Manufacturing Integration", + "version": "14.0.1.1.0", + "license": "LGPL-3", + "category": "RMA", + "summary": "Integrates RMA with Kit Manufacturing/Sale Process", + "author": "ForgeFlow", + "website": "https://github.com/ForgeFlow/stock-rma", + "depends": ["rma_sale", "sale_mrp"], + "data": [], + "installable": True, +} diff --git a/rma_sale_mrp/models/__init__.py b/rma_sale_mrp/models/__init__.py new file mode 100644 index 00000000..6ed3aa95 --- /dev/null +++ b/rma_sale_mrp/models/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) +from . import rma_order_line diff --git a/rma_sale_mrp/models/rma_order_line.py b/rma_sale_mrp/models/rma_order_line.py new file mode 100644 index 00000000..ed2c8463 --- /dev/null +++ b/rma_sale_mrp/models/rma_order_line.py @@ -0,0 +1,41 @@ +# Copyright 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from odoo import models +from odoo.tools import float_is_zero + + +class RmaOrderLine(models.Model): + + _inherit = "rma.order.line" + + def _get_price_unit(self): + self.ensure_one() + price_unit = 0 + if ( + self.env.context.get("product_required") + and self.env.context.get("product_required") != self.product_id + and self.sale_line_id + and self.sale_line_id.move_ids.filtered( + lambda x: x.state == "done" + and x.product_id == self.env.context.get("product_required") + ) + ): + done_moves = self.sale_line_id.move_ids.filtered( + lambda x: x.state == "done" + and x.location_dest_id.usage != "internal" + and x.location_id.usage == "internal" + and x.product_id == self.env.context.get("product_required") + ) + layers_value = sum(done_moves.mapped("stock_valuation_layer_ids.value")) + layers_quantity = sum( + done_moves.mapped("stock_valuation_layer_ids.quantity") + ) + pd = self.env["decimal.precision"].precision_get("Product Price") + if not float_is_zero(layers_quantity, precision_digits=pd): + price_unit = layers_value / layers_quantity + else: + price_unit = super(RmaOrderLine, self)._get_price_unit() + else: + price_unit = super(RmaOrderLine, self)._get_price_unit() + return price_unit diff --git a/rma_sale_mrp/readme/CONTRIBUTORS.rst b/rma_sale_mrp/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..b647a292 --- /dev/null +++ b/rma_sale_mrp/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Christopher Ormaza diff --git a/rma_sale_mrp/readme/DESCRIPTION.rst b/rma_sale_mrp/readme/DESCRIPTION.rst new file mode 100644 index 00000000..e69de29b diff --git a/rma_sale_mrp/static/description/index.html b/rma_sale_mrp/static/description/index.html new file mode 100644 index 00000000..e71e10e9 --- /dev/null +++ b/rma_sale_mrp/static/description/index.html @@ -0,0 +1,415 @@ + + + + + + +RMA Manufacturing Integration + + + +
+

RMA Manufacturing Integration

+ + +

Beta License: LGPL-3 ForgeFlow/stock-rma

+

Table of contents

+ +
+

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 to smash it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • ForgeFlow
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is part of the ForgeFlow/stock-rma project on GitHub.

+

You are welcome to contribute.

+
+
+
+ + diff --git a/rma_sale_mrp/tests/__init__.py b/rma_sale_mrp/tests/__init__.py new file mode 100644 index 00000000..65b2926d --- /dev/null +++ b/rma_sale_mrp/tests/__init__.py @@ -0,0 +1,3 @@ +# Copyright 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) +from . import test_rma_mrp diff --git a/rma_sale_mrp/tests/test_rma_mrp.py b/rma_sale_mrp/tests/test_rma_mrp.py new file mode 100644 index 00000000..47696e85 --- /dev/null +++ b/rma_sale_mrp/tests/test_rma_mrp.py @@ -0,0 +1,256 @@ +# Copyright 2023 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from odoo.tests import Form, TransactionCase + + +class TestRmaMrp(TransactionCase): + def setUp(self): + super().setUp() + self.product_model = self.env["product.product"] + self.template_model = self.env["product.template"] + self.product_ctg_model = self.env["product.category"] + self.journal_model = self.env["account.journal"] + self.rma_line = self.env["rma.order.line"] + self.rma_make_picking = self.env["rma_make_picking.wizard"] + self.rma_op_obj = self.env["rma.operation"] + # Get required Model data + self.product_uom = self.env.ref("uom.product_uom_unit") + self.company = self.env.ref("base.main_company") + self.stock_picking_type_out = self.env.ref("stock.picking_type_out") + self.stock_picking_type_in = self.env.ref("stock.picking_type_in") + self.stock_location_id = self.env.ref("stock.stock_location_stock") + self.stock_location_customer_id = self.env.ref("stock.stock_location_customers") + self.stock_location_supplier_id = self.env.ref("stock.stock_location_suppliers") + self.rma_route_cust = self.env.ref("rma.route_rma_customer") + self.customer_view = self.env.ref("rma_sale.view_rma_line_form") + + self.stock_journal = self.env["account.journal"].create( + {"name": "Stock journal", "type": "general", "code": "STK00"} + ) + # Create product category + self.product_ctg = self._create_product_category() + + # Create partners + self.supplier = self.env["res.partner"].create({"name": "Test supplier"}) + self.customer = self.env["res.partner"].create({"name": "Test customer"}) + + # Create a Product with real cost + standard_price = 10.0 + list_price = 20.0 + self.kit_product = self._create_product(standard_price, False, list_price) + self.component_product_1 = self._create_product( + standard_price, False, list_price + ) + self.component_product_2 = self._create_product( + standard_price, False, list_price + ) + + # Create BoM for Kit A + bom_product_form = Form(self.env["mrp.bom"]) + bom_product_form.product_id = self.kit_product + bom_product_form.product_tmpl_id = self.kit_product.product_tmpl_id + bom_product_form.product_qty = 1.0 + bom_product_form.type = "phantom" + with bom_product_form.bom_line_ids.new() as bom_line: + bom_line.product_id = self.component_product_1 + bom_line.product_qty = 1.0 + with bom_product_form.bom_line_ids.new() as bom_line: + bom_line.product_id = self.component_product_2 + bom_line.product_qty = 1.0 + self.bom_kit = bom_product_form.save() + + # RMA configuration + + self.operation_1 = self.rma_op_obj.create( + { + "code": "TEST", + "name": "Refund and receive", + "type": "customer", + "receipt_policy": "ordered", + "refund_policy": "ordered", + "in_route_id": self.rma_route_cust.id, + "out_route_id": self.rma_route_cust.id, + } + ) + + def _create_product_category(self): + product_ctg = self.product_ctg_model.create( + { + "name": "test_product_ctg", + "property_valuation": "real_time", + "property_cost_method": "fifo", + "property_stock_journal": self.stock_journal.id, + } + ) + return product_ctg + + def _create_product(self, standard_price, template, list_price): + """Create a Product variant.""" + if not template: + template = self.template_model.create( + { + "name": "test_product", + "categ_id": self.product_ctg.id, + "type": "product", + "standard_price": standard_price, + "valuation": "real_time", + "invoice_policy": "delivery", + } + ) + return template.product_variant_ids[0] + product = self.product_model.create( + {"product_tmpl_id": template.id, "list_price": list_price} + ) + return product + + def _create_receipt(self, product, qty, price_unit=10.0): + return self.env["stock.picking"].create( + { + "name": self.stock_picking_type_in.sequence_id._next(), + "partner_id": self.supplier.id, + "picking_type_id": self.stock_picking_type_in.id, + "location_id": self.stock_location_supplier_id.id, + "location_dest_id": self.stock_location_id.id, + "move_lines": [ + ( + 0, + 0, + { + "name": product.name, + "product_id": product.id, + "product_uom": product.uom_id.id, + "product_uom_qty": qty, + "price_unit": price_unit, + "location_id": self.stock_location_supplier_id.id, + "location_dest_id": self.stock_location_id.id, + "procure_method": "make_to_stock", + }, + ) + ], + } + ) + + def _do_picking(self, picking, qty): + """Do picking with only one move on the given date.""" + picking.action_confirm() + picking.action_assign() + picking.move_lines.quantity_done = qty + res = picking.button_validate() + if isinstance(res, dict) and res: + backorder_wiz_id = res["res_id"] + backorder_wiz = self.env["stock.backorder.confirmation"].browse( + [backorder_wiz_id] + ) + backorder_wiz.process() + return True + + def _make_sale_order(self, product, quantity, price_unit=10.0): + so = Form(self.env["sale.order"]) + so.partner_id = self.customer + with so.order_line.new() as sale_line: + sale_line.product_id = product + sale_line.product_uom_qty = quantity + sale_line.price_unit = price_unit or product.list_price + so = so.save() + so.action_confirm() + return so + + def _receive_rma(self, rma_line_ids): + wizard = self.rma_make_picking.with_context( + { + "active_ids": rma_line_ids.ids, + "active_model": "rma.order.line", + "picking_type": "incoming", + "active_id": 1, + } + ).create({}) + wizard._create_picking() + pickings = rma_line_ids._get_in_pickings() + pickings.action_assign() + for picking in pickings: + for mv in picking.move_lines: + mv.quantity_done = mv.product_uom_qty + # In case of two step pickings, ship in two steps: + while pickings.filtered(lambda p: p.state == "assigned"): + pickings._action_done() + return pickings + + def _create_rma_receipt(self, so_line, price_unit=10.0): + rma_line = Form( + self.rma_line.with_context(customer=1), + view=self.customer_view.id, + ) + rma_line.partner_id = so_line.order_id.partner_id + rma_line.sale_line_id = so_line + rma_line.price_unit = price_unit + rma_line.operation_id = self.operation_1 + rma_line = rma_line.save() + rma_line.action_rma_to_approve() + picking = self._receive_rma(rma_line) + return picking + + def test_01_kit_return_with_diff_prices(self): + receipt_01 = self._create_receipt(self.component_product_1, 10, 10.0) + self._do_picking(receipt_01, 10.0) + receipt_02 = self._create_receipt(self.component_product_2, 10, 10.0) + self._do_picking(receipt_02, 10.0) + + order_01 = self._make_sale_order(self.kit_product, 10, 30.0) + self._do_picking(order_01.picking_ids, 10.0) + + receipt_03 = self._create_receipt(self.component_product_1, 10, 15.0) + self._do_picking(receipt_03, 10.0) + receipt_04 = self._create_receipt(self.component_product_2, 10, 15.0) + self._do_picking(receipt_04, 10.0) + + order_02 = self._make_sale_order(self.kit_product, 10, 30.0) + self._do_picking(order_02.picking_ids, 10.0) + + rma_picking_01 = self._create_rma_receipt( + order_01.order_line, order_01.order_line.price_unit + ) + + component_1_sm = rma_picking_01.move_lines.filtered( + lambda x: x.product_id == self.component_product_1 + ) + component_2_sm = rma_picking_01.move_lines.filtered( + lambda x: x.product_id == self.component_product_2 + ) + + self.assertTrue(bool(component_1_sm)) + self.assertTrue(bool(component_2_sm)) + + self.component_product_1.standard_price = 20.0 + self.component_product_2.standard_price = 20.0 + + self.assertEqual( + 100.0, sum(component_1_sm.mapped("stock_valuation_layer_ids.value")) + ) + self.assertEqual( + 100.0, sum(component_2_sm.mapped("stock_valuation_layer_ids.value")) + ) + + rma_picking_02 = self._create_rma_receipt( + order_02.order_line, order_02.order_line.price_unit + ) + + component_1_sm = rma_picking_02.move_lines.filtered( + lambda x: x.product_id == self.component_product_1 + ) + component_2_sm = rma_picking_02.move_lines.filtered( + lambda x: x.product_id == self.component_product_2 + ) + + self.assertTrue(bool(component_1_sm)) + self.assertTrue(bool(component_2_sm)) + + self.component_product_1.standard_price = 25.0 + self.component_product_2.standard_price = 25.0 + + self.assertEqual( + 150.0, sum(component_1_sm.mapped("stock_valuation_layer_ids.value")) + ) + self.assertEqual( + 150.0, sum(component_2_sm.mapped("stock_valuation_layer_ids.value")) + ) diff --git a/setup/rma_sale_mrp/odoo/addons/rma_sale_mrp b/setup/rma_sale_mrp/odoo/addons/rma_sale_mrp new file mode 120000 index 00000000..aa7efac9 --- /dev/null +++ b/setup/rma_sale_mrp/odoo/addons/rma_sale_mrp @@ -0,0 +1 @@ +../../../../rma_sale_mrp \ No newline at end of file diff --git a/setup/rma_sale_mrp/setup.py b/setup/rma_sale_mrp/setup.py new file mode 100644 index 00000000..28c57bb6 --- /dev/null +++ b/setup/rma_sale_mrp/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)