From 5e4dbe26161a032d0cd40d57b2a3ba2644d603b2 Mon Sep 17 00:00:00 2001 From: "Touati Djamel (otd)" Date: Wed, 12 Jul 2023 15:32:14 +0000 Subject: [PATCH] [FIX] stock_account: remove AAL when changing qty of the component to 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steps to reproduce the bug: - Create a storable product “P1” with BoM: - Component: "C1", cost: $100 - Create a Manufacturing Order to produce 1 unit of "P1": - Set any analytic account - Confirm and validate the MO - An analytic account line is created for $100 of C1. - Change the quantity of the component to 0. - Try to save Problem: A traceback is triggered, because the `amount` and `unit_amount` variables are used without assignment. Solution: Assign “0” value to both variable in the beginning of the function, therefore the analytic account line will be deleted. opw-3257240 closes odoo/odoo#128495 X-original-commit: 513e3dea2c838bb6f805c567069ea39406c6634e Signed-off-by: William Henrotin (whe) Signed-off-by: Djamel Touati (otd) --- .../tests/test_analytic_account.py | 50 +++++++++++++++++++ addons/stock_account/models/stock_move.py | 5 ++ 2 files changed, 55 insertions(+) diff --git a/addons/mrp_account/tests/test_analytic_account.py b/addons/mrp_account/tests/test_analytic_account.py index 8c9488eacb9..2af76f710e4 100644 --- a/addons/mrp_account/tests/test_analytic_account.py +++ b/addons/mrp_account/tests/test_analytic_account.py @@ -249,3 +249,53 @@ class TestAnalyticAccount(TestMrpAnalyticAccount): self.assertEqual(len(analytic_account_no_company.line_ids), 1) mo_no_company.workorder_ids.unlink() self.assertEqual(len(analytic_account_no_company.line_ids), 0) + + def test_update_components_qty_to_0(self): + """ Test that the analytic lines are deleted when the quantity of the component is set to 0. + Create a Mo with analytic account and a component, confirm and validate it, + set the quantity of the component to 0, the analytic lines should be deleted. + """ + component = self.env['product.product'].create({ + 'name': 'Component', + 'type': 'product', + 'standard_price': 100, + }) + product = self.env['product.product'].create({ + 'name': 'Product', + 'type': 'product', + }) + bom = self.env['mrp.bom'].create({ + 'product_tmpl_id': product.product_tmpl_id.id, + 'product_qty': 1, + 'product_uom_id': product.uom_id.id, + 'type': 'normal', + 'bom_line_ids': [(0, 0, { + 'product_id': component.id, + 'product_qty': 1, + 'product_uom_id': component.uom_id.id, + })], + }) + analytic_account = self.env['account.analytic.account'].create({ + 'name': "Test Account", + 'plan_id': self.analytic_plan.id, + }) + mo_form = Form(self.env['mrp.production']) + mo_form.product_id = product + mo_form.bom_id = bom + mo_form.product_qty = 1.0 + mo_form.analytic_distribution = {str(analytic_account.id): 100.0} + mo = mo_form.save() + mo.action_confirm() + self.assertEqual(mo.state, 'confirmed') + + + mo_form = Form(mo) + mo_form.qty_producing = 1 + mo = mo_form.save() + self.assertEqual(mo.state, 'to_close') + mo.button_mark_done() + self.assertEqual(mo.state, 'done') + self.assertEqual(analytic_account.debit, 100) + mo.move_raw_ids[0].quantity_done = 0 + self.assertEqual(analytic_account.debit, 0) + self.assertFalse(analytic_account.line_ids) diff --git a/addons/stock_account/models/stock_move.py b/addons/stock_account/models/stock_move.py index ba89c8ba24c..fae10e7c704 100644 --- a/addons/stock_account/models/stock_move.py +++ b/addons/stock_account/models/stock_move.py @@ -412,6 +412,7 @@ class StockMove(models.Model): if self.state in ['cancel', 'draft']: return False + amount, unit_amount = 0, 0 if self.state != 'done': unit_amount = self.product_uom._compute_quantity( self.quantity_done, self.product_id.uom_id) @@ -429,6 +430,10 @@ class StockMove(models.Model): amount = sum(self.stock_valuation_layer_ids.mapped('value')) unit_amount = - sum(self.stock_valuation_layer_ids.mapped('quantity')) + if self.analytic_account_line_ids and amount == 0 and unit_amount == 0: + self.analytic_account_line_ids.unlink() + return False + return self.env['account.analytic.account']._perform_analytic_distribution( self._get_analytic_distribution(), amount, unit_amount, self.analytic_account_line_ids, self)