diff --git a/mrp_account_analytic/README.rst b/mrp_account_analytic/README.rst new file mode 100644 index 000000000..0d63177d7 --- /dev/null +++ b/mrp_account_analytic/README.rst @@ -0,0 +1 @@ +To generate diff --git a/mrp_account_analytic/__init__.py b/mrp_account_analytic/__init__.py new file mode 100644 index 000000000..bb83730e9 --- /dev/null +++ b/mrp_account_analytic/__init__.py @@ -0,0 +1,4 @@ +# Copyright (C) 2021 Open Source Integrators +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/mrp_account_analytic/__manifest__.py b/mrp_account_analytic/__manifest__.py new file mode 100644 index 000000000..1562ad695 --- /dev/null +++ b/mrp_account_analytic/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright (C) 2021 Open Source Integrators +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Manufacturing Analytic Items", + "summary": "Consuming raw materials and operations generated Analytic Items", + "version": "14.0.1.0.0", + "category": "Manufacturing", + "author": "Open Source Integrators, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/manufacture", + "license": "AGPL-3", + "depends": ["mrp_analytic"], + "data": [ + "views/account_analytic_line_view.xml", + ], + "installable": True, + "maintainers": ["dreispt"], + "development_status": "Beta", +} diff --git a/mrp_account_analytic/models/__init__.py b/mrp_account_analytic/models/__init__.py new file mode 100644 index 000000000..853b1a022 --- /dev/null +++ b/mrp_account_analytic/models/__init__.py @@ -0,0 +1,3 @@ +from . import account_analytic_line +from . import mrp_workorder +from . import stock_move diff --git a/mrp_account_analytic/models/account_analytic_line.py b/mrp_account_analytic/models/account_analytic_line.py new file mode 100644 index 000000000..dc2c90b4f --- /dev/null +++ b/mrp_account_analytic/models/account_analytic_line.py @@ -0,0 +1,21 @@ +# Copyright (C) 2020 Open Source Integrators +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class AccountAnalyticLine(models.Model): + _inherit = "account.analytic.line" + + manufacturing_order_id = fields.Many2one( + "mrp.production", + string="Related Manufacturing Order", + ) + stock_move_id = fields.Many2one( + "stock.move", + string="Related Stock Move", + ) + workorder_id = fields.Many2one( + "mrp.workorder", + string="Work Order", + ) diff --git a/mrp_account_analytic/models/mrp_workorder.py b/mrp_account_analytic/models/mrp_workorder.py new file mode 100644 index 000000000..ce3cbcedf --- /dev/null +++ b/mrp_account_analytic/models/mrp_workorder.py @@ -0,0 +1,46 @@ +# Copyright (C) 2021 Open Source Integrators +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from odoo import api, fields, models + + +class MrpWorkcenterProductivity(models.Model): + _inherit = "mrp.workcenter.productivity" + + def _prepare_mrp_workorder_analytic_item(self): + """ + Prepare additional values for Analytic Items created. + For compatibility with analytic_activity_cost + """ + self.ensure_one() + return { + "name": "{} / {}".format(self.production_id.name, self.workorder_id.name), + "account_id": self.production_id.analytic_account_id.id, + "date": fields.Date.today(), + "company_id": self.company_id.id, + "manufacturing_order_id": self.production_id.id, + "workorder_id": self.workorder_id.id, + "unit_amount": self.duration / 60, # convert minutes to hours + "amount": -self.duration / 60 * self.workcenter_id.costs_hour, + } + + def generate_mrp_work_analytic_line(self): + AnalyticLine = self.env["account.analytic.line"].sudo() + for timelog in self: + line_vals = timelog._prepare_mrp_workorder_analytic_item() + analytic_line = AnalyticLine.create(line_vals) + analytic_line.on_change_unit_amount() + + @api.model + def create(self, vals): + timelog = super().create(vals) + if vals.get("date_end"): + timelog.generate_mrp_work_analytic_line() + return timelog + + def write(self, vals): + if vals.get("date_end"): + self.generate_mrp_work_analytic_line() + res = super().write(vals) + return res diff --git a/mrp_account_analytic/models/stock_move.py b/mrp_account_analytic/models/stock_move.py new file mode 100644 index 000000000..8aff5912a --- /dev/null +++ b/mrp_account_analytic/models/stock_move.py @@ -0,0 +1,82 @@ +# Copyright (C) 2021 Open Source Integrators +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +from odoo import api, models + + +class StockMove(models.Model): + _inherit = "stock.move" + + def _prepare_mrp_raw_material_analytic_line(self): + """ + Prepare additional values for Analytic Items created. + """ + self.ensure_one() + move = self + mrp_order = move.raw_material_production_id + return { + "date": move.date, + "name": "{} / {}".format(mrp_order.name, move.product_id.display_name), + "ref": mrp_order.name, + "account_id": mrp_order.analytic_account_id.id, + "manufacturing_order_id": mrp_order.id, + "company_id": mrp_order.company_id.id, + "stock_move_id": move.id, + "product_id": move.product_id.id, + "unit_amount": move.quantity_done, + } + + def generate_mrp_raw_analytic_line(self): + """ + Generate Analytic Lines. + One Analytic Item for each Stock Move line. + If the Stock Move is updated, the existing Analytic Item is updated. + """ + AnalyticLine = self.env["account.analytic.line"].sudo() + existing_items = AnalyticLine.search([("stock_move_id", "in", self.ids)]) + for move in self.filtered("raw_material_production_id.analytic_account_id"): + line_vals = move._prepare_mrp_raw_material_analytic_line() + if move in existing_items.mapped("stock_move_id"): + analytic_line = existing_items.filtered( + lambda x: x.stock_move_id == move + ) + analytic_line.write(line_vals) + analytic_line.on_change_unit_amount() + elif line_vals.get("unit_amount"): + analytic_line = AnalyticLine.create(line_vals) + analytic_line.on_change_unit_amount() + + def write(self, vals): + """ When material is consumed, generate Analytic Items """ + res = super().write(vals) + if vals.get("quantity_done"): + self.generate_mrp_raw_analytic_line() + return res + + @api.model + def create(self, vals): + qty_done = vals.get("quantity_done") + res = super().create(vals) + if qty_done: + res.generate_mrp_raw_analytic_line() + return res + + +class StockMoveLine(models.Model): + _inherit = "stock.move.line" + + def write(self, vals): + qty_done = vals.get("qty_done") + res = super().write(vals) + if qty_done: + self.mapped("move_id").generate_mrp_raw_analytic_line() + return res + + @api.model + def create(self, vals): + qty_done = vals.get("qty_done") + res = super().create(vals) + if qty_done: + res.mapped("move_id").generate_mrp_raw_analytic_line() + return res diff --git a/mrp_account_analytic/readme/CONTRIBUTORS.rst b/mrp_account_analytic/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..837de373e --- /dev/null +++ b/mrp_account_analytic/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Daniel Reis diff --git a/mrp_account_analytic/readme/DESCRIPTION.rst b/mrp_account_analytic/readme/DESCRIPTION.rst new file mode 100644 index 000000000..ec216c918 --- /dev/null +++ b/mrp_account_analytic/readme/DESCRIPTION.rst @@ -0,0 +1,4 @@ +Generates Analytic Items during manufacturing operations. + +When raw materials are consumed or work center time is recorded, +Analytic Items are created, capturing the corresponding quantity and cost. diff --git a/mrp_account_analytic/readme/USAGE.rst b/mrp_account_analytic/readme/USAGE.rst new file mode 100644 index 000000000..0f5e2348c --- /dev/null +++ b/mrp_account_analytic/readme/USAGE.rst @@ -0,0 +1,14 @@ +To use: + +* On the *Manufacturing Order*, *Miscellaneous* tab, set the Analytic Account to use. + This may correspond to a Project. + +On Manufacturing Orders, Analytic Items are automatically generated when: + + * Raw materials are consumed, or + * Time is spent on Operations. + +To analyze costs: + +* Go to *Manufacturing > Reports > Analytic Items*. + The cost data is available there and can be used for analysis. diff --git a/mrp_account_analytic/tests/__init__.py b/mrp_account_analytic/tests/__init__.py new file mode 100644 index 000000000..2b58ade96 --- /dev/null +++ b/mrp_account_analytic/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mrp_analytic diff --git a/mrp_account_analytic/tests/test_mrp_analytic.py b/mrp_account_analytic/tests/test_mrp_analytic.py new file mode 100644 index 000000000..3f3969b64 --- /dev/null +++ b/mrp_account_analytic/tests/test_mrp_analytic.py @@ -0,0 +1,116 @@ +from odoo.tests import Form, common + + +class TestMRP(common.TransactionCase): + """ + Create a Manufacturing Order, with Raw Materials and Operations. + Consuming raw materials generates or updates Analytic Items. + Working on Operations generates or updates Analytic Items. + """ + + def setUp(self): + super().setUp() + # Analytic Account + self.analytic_1 = self.env["account.analytic.account"].create({"name": "Job 1"}) + # Work Center + self.mrp_workcenter_1 = self.env["mrp.workcenter"].create( + { + "name": "Assembly Line", + "costs_hour": 40, + } + ) + # Products + self.product_lemonade = self.env["product.product"].create( + { + "name": "Lemonade", + "type": "product", + "standard_price": 20, + } + ) + self.product_lemon = self.env["product.product"].create( + { + "name": "Lemon", + "type": "product", + "standard_price": 1, + } + ) + # BOM + self.mrp_bom_lemonade = self.env["mrp.bom"].create( + { + "product_tmpl_id": self.product_lemonade.product_tmpl_id.id, + "operation_ids": [ + ( + 0, + 0, + { + "workcenter_id": self.mrp_workcenter_1.id, + "name": "Squeeze Lemons", + "time_cycle": 15, + }, + ), + ], + } + ) + self.mrp_bom_lemonade.write( + { + "bom_line_ids": [ + ( + 0, + 0, + { + "product_id": self.product_lemon.id, + "product_qty": 4, + }, + ) + ] + } + ) + # MO + mo_create_form = Form(self.env["mrp.production"]) + mo_create_form.product_id = self.product_lemonade + mo_create_form.bom_id = self.mrp_bom_lemonade + mo_create_form.product_qty = 1 + self.mo_lemonade = mo_create_form.save() + self.mo_lemonade.analytic_account_id = self.analytic_1 + self.mo_lemonade.action_confirm() + + def test_100_one_step_produce(self): + # Form edit the MO and Save + mo_form = Form(self.mo_lemonade) + mo_form.qty_producing = 1 + mo_lemonade = mo_form.save() + # Set 15 minutes to work time and "Mark As Done" + mo_lemonade.workorder_ids.duration = 15 + mo_lemonade.button_mark_done() + + analytic_items = self.env["account.analytic.line"].search( + [("manufacturing_order_id", "=", mo_lemonade.id)] + ) + # Expected (4 * 1.00) + (0.25 * 40.00) => 14.00 + analytic_qty = sum(analytic_items.mapped("unit_amount")) + self.assertEqual(analytic_qty, 4.25, "Expected Analytic Items total quantity") + analytic_amount = sum(analytic_items.mapped("amount")) + self.assertEqual( + analytic_amount, -14.00, "Expected Analytic Items total amount" + ) + + def test_110_two_step_produce(self): + # Consume some raw material + self.mo_lemonade.move_raw_ids.write({"quantity_done": 1}) + self.mo_lemonade.move_raw_ids.write({"quantity_done": 2}) + self.mo_lemonade.move_raw_ids.write({"quantity_done": 4}) + # Work on operations up to 15 minutes + self.mo_lemonade.workorder_ids.write({"duration": 5}) + self.mo_lemonade.workorder_ids.write({"duration": 10}) + self.mo_lemonade.workorder_ids.write({"duration": 15}) + + analytic_items = self.env["account.analytic.line"].search( + [("manufacturing_order_id", "=", self.mo_lemonade.id)] + ) + # Expected (4 * 1.00) + (0.25 * 40.00) => 14.00 + analytic_qty = sum(analytic_items.mapped("unit_amount")) + self.assertEqual(analytic_qty, 4.25, "Expected Analytic Items total quantity") + analytic_amount = sum(analytic_items.mapped("amount")) + self.assertEqual( + analytic_amount, -14.00, "Expected Analytic Items total amount" + ) diff --git a/mrp_account_analytic/views/account_analytic_line_view.xml b/mrp_account_analytic/views/account_analytic_line_view.xml new file mode 100644 index 000000000..7fde329a8 --- /dev/null +++ b/mrp_account_analytic/views/account_analytic_line_view.xml @@ -0,0 +1,70 @@ + + + + + account.analytic.line.custom.tree + account.analytic.line + + + + + + + + + + + + + + account.analytic.line.custom.form + account.analytic.line + + + + + + + + + + + + + account.analytic.line.custom.filter + account.analytic.line + + + + + + + + + + + Analytic Items + account.analytic.line + tree,kanban,form,graph,pivot + + + {'search_default_filter_is_related_to_mo': True} + + + + diff --git a/setup/mrp_account_analytic/odoo/addons/mrp_account_analytic b/setup/mrp_account_analytic/odoo/addons/mrp_account_analytic new file mode 120000 index 000000000..d39786be8 --- /dev/null +++ b/setup/mrp_account_analytic/odoo/addons/mrp_account_analytic @@ -0,0 +1 @@ +../../../../mrp_account_analytic \ No newline at end of file diff --git a/setup/mrp_account_analytic/setup.py b/setup/mrp_account_analytic/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/mrp_account_analytic/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)