diff --git a/stock_analytic/README.rst b/stock_analytic/README.rst index 4a90cfbc77..918a416410 100644 --- a/stock_analytic/README.rst +++ b/stock_analytic/README.rst @@ -6,36 +6,36 @@ Stock Analytic ============== -Adds an analytic account in stock move to be able to get analytic information -when generating the journal items. +Adds an analytic account and analytic tags in stock move to be able to get +analytic information when generating the journal items. Usage ===== -To Assign an Analytic Account to a Stock Move ---------------------------------------------- +To Assign an Analytic Account and Analytic Tags to a Stock Move +--------------------------------------------------------------- You need to: #. Create manually or open draft picking -#. Add move lines and fill **analytic account** field +#. Add move lines and fill **analytic account** and **analytic tags** fields -Assigned Journal Items created from Stock Move with Analytic Account --------------------------------------------------------------------- +Assigned Journal Items created from Stock Move with Analytic Account and Analytic Tags +-------------------------------------------------------------------------------------- If stock move automatically create journal entry, the journal entry will contain journal items with following rule: #. Journal item with account equal to product's valuation account will not be - assigned to any analytic account + assigned to any analytic account, neither analytic tags #. Journal item with account different to product's valuation account will be assigned to an analytic account according to the stock move's analytic - account + account. The same logic applies to analytic tags. .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot - :target: https://runbot.odoo-community.org/runbot/87/12.0 + :target: https://runbot.odoo-community.org/runbot/87/13.0 Bug Tracker diff --git a/stock_analytic/__manifest__.py b/stock_analytic/__manifest__.py index 87bbaa00c5..ef2b77bb47 100644 --- a/stock_analytic/__manifest__.py +++ b/stock_analytic/__manifest__.py @@ -6,7 +6,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { "name": "Stock Analytic", - "summary": "Adds an analytic account in stock move", + "summary": "Adds an analytic account and analytic tags in stock move", "version": "13.0.1.0.0", "author": "Julius Network Solutions, " "ClearCorp, OpenSynergy Indonesia, " diff --git a/stock_analytic/models/stock.py b/stock_analytic/models/stock.py index 0838037028..d2e4a9803d 100644 --- a/stock_analytic/models/stock.py +++ b/stock_analytic/models/stock.py @@ -14,6 +14,7 @@ class StockMove(models.Model): analytic_account_id = fields.Many2one( string="Analytic Account", comodel_name="account.analytic.account", ) + analytic_tag_ids = fields.Many2many("account.analytic.tag", string="Analytic Tags") def _prepare_account_move_line( self, qty, cost, credit_account_id, debit_account_id, description @@ -22,16 +23,19 @@ def _prepare_account_move_line( res = super(StockMove, self)._prepare_account_move_line( qty, cost, credit_account_id, debit_account_id, description ) - # Add analytic account in debit line - if not self.analytic_account_id or not res: - return res - - for num in range(0, 2): + for line in res: if ( - res[num][2]["account_id"] + line[2]["account_id"] != self.product_id.categ_id.property_stock_valuation_account_id.id ): - res[num][2].update({"analytic_account_id": self.analytic_account_id.id}) + # Add analytic account in debit line + if self.analytic_account_id: + line[2].update({"analytic_account_id": self.analytic_account_id.id}) + # Add analytic tags in debit line + if self.analytic_tag_ids: + line[2].update( + {"analytic_tag_ids": [(6, 0, self.analytic_tag_ids.ids)]} + ) return res @api.model diff --git a/stock_analytic/models/stock_scrap.py b/stock_analytic/models/stock_scrap.py index d20565975a..2767ec5add 100644 --- a/stock_analytic/models/stock_scrap.py +++ b/stock_analytic/models/stock_scrap.py @@ -9,8 +9,14 @@ class StockScrap(models.Model): analytic_account_id = fields.Many2one( string="Analytic Account", comodel_name="account.analytic.account" ) + analytic_tag_ids = fields.Many2many("account.analytic.tag", string="Analytic Tags") def _prepare_move_values(self): res = super()._prepare_move_values() - res.update({"analytic_account_id": self.analytic_account_id.id}) + res.update( + { + "analytic_account_id": self.analytic_account_id.id, + "analytic_tag_ids": [(6, 0, self.analytic_tag_ids.ids)], + } + ) return res diff --git a/stock_analytic/tests/test_stock_picking.py b/stock_analytic/tests/test_stock_picking.py index 7fe2e2c11c..536023a747 100644 --- a/stock_analytic/tests/test_stock_picking.py +++ b/stock_analytic/tests/test_stock_picking.py @@ -46,6 +46,12 @@ def setUp(self): self.stock_journal = self.env["account.journal"].create( {"name": "Stock Journal", "code": "STJTEST", "type": "general"} ) + self.analytic_tag_1 = self.env["account.analytic.tag"].create( + {"name": "analytic tag test 1"} + ) + self.analytic_tag_2 = self.env["account.analytic.tag"].create( + {"name": "analytic tag test 2"} + ) self.analytic_account = self.env.ref("analytic.analytic_agrolait") self.warehouse = self.env.ref("stock.warehouse0") self.location = self.warehouse.lot_stock_id @@ -65,7 +71,12 @@ def setUp(self): self.product.update({"categ_id": self.product_categ.id}) def _create_picking( - self, location_id, location_dest_id, picking_type_id, analytic_account_id=False + self, + location_id, + location_dest_id, + picking_type_id, + analytic_account_id=False, + analytic_tag_ids=False, ): picking_data = { "picking_type_id": picking_type_id.id, @@ -90,6 +101,7 @@ def _create_picking( "analytic_account_id": ( analytic_account_id.id if analytic_account_id else False ), + "analytic_tag_ids": [(6, 0, analytic_tag_ids if analytic_tag_ids else [])], } self.env["stock.move"].create(move_data) @@ -140,14 +152,23 @@ def _check_analytic_account_no_error(self, picking): self.assertEqual( acc_line.analytic_account_id.id, move.analytic_account_id.id ) + self.assertEqual( + acc_line.analytic_tag_ids.ids, move.analytic_tag_ids.ids + ) def _check_no_analytic_account(self, picking): criteria2 = [ ("move_id.ref", "=", picking.name), ("analytic_account_id", "!=", False), ] + criteria3 = [ + ("move_id.ref", "=", picking.name), + ("analytic_tag_ids", "!=", []), + ] line_count = self.env["account.move.line"].search_count(criteria2) self.assertEqual(line_count, 0) + line_count = self.env["account.move.line"].search_count(criteria3) + self.assertEqual(line_count, 0) def test_outgoing_picking_with_analytic(self): picking = self._create_picking( @@ -155,6 +176,7 @@ def test_outgoing_picking_with_analytic(self): self.dest_location, self.outgoing_picking_type, self.analytic_account, + [self.analytic_tag_1.id | self.analytic_tag_2.id], ) self.__update_qty_on_hand_product(self.product, 1) self._confirm_picking_no_error(picking) @@ -180,6 +202,7 @@ def test_incoming_picking_with_analytic(self): self.dest_location, self.incoming_picking_type, self.analytic_account, + [self.analytic_tag_1.id | self.analytic_tag_2.id], ) self._confirm_picking_no_error(picking) self._picking_done_no_error(picking) diff --git a/stock_analytic/tests/test_stock_scrap.py b/stock_analytic/tests/test_stock_scrap.py index 99a05a5887..69f03960a7 100644 --- a/stock_analytic/tests/test_stock_scrap.py +++ b/stock_analytic/tests/test_stock_scrap.py @@ -11,6 +11,12 @@ def setUp(self): self.warehouse = self.env.ref("stock.warehouse0") self.location = self.warehouse.lot_stock_id self.analytic_account = self.env.ref("analytic.analytic_agrolait") + self.analytic_tag_1 = self.env["account.analytic.tag"].create( + {"name": "analytic tag test 1"} + ) + self.analytic_tag_2 = self.env["account.analytic.tag"].create( + {"name": "analytic tag test 2"} + ) def __update_qty_on_hand_product(self, product, new_qty): qty_wizard = self.env["stock.change.product.qty"].create( @@ -22,7 +28,7 @@ def __update_qty_on_hand_product(self, product, new_qty): ) qty_wizard.change_product_qty() - def _create_scrap(self, analytic_account_id=False): + def _create_scrap(self, analytic_account_id=False, analytic_tag_ids=False): scrap_data = { "product_id": self.product.id, "scrap_qty": 1.00, @@ -31,6 +37,7 @@ def _create_scrap(self, analytic_account_id=False): "analytic_account_id": analytic_account_id and analytic_account_id.id or False, + "analytic_tag_ids": [(6, 0, analytic_tag_ids if analytic_tag_ids else [])], } return self.env["stock.scrap"].create(scrap_data) @@ -49,6 +56,9 @@ def _check_analytic_account_no_error(self, scrap): self.assertEqual( acc_line.analytic_account_id.id, scrap.analytic_account_id.id ) + self.assertEqual( + acc_line.analytic_tag_ids.ids, scrap.analytic_tag_ids.ids + ) def test_scrap_without_analytic(self): self.__update_qty_on_hand_product(self.product, 1) @@ -57,6 +67,8 @@ def test_scrap_without_analytic(self): def test_scrap_with_analytic(self): self.__update_qty_on_hand_product(self.product, 1) - scrap = self._create_scrap(self.analytic_account) + scrap = self._create_scrap( + self.analytic_account, [self.analytic_tag_1.id | self.analytic_tag_2.id] + ) self._validate_scrap_no_error(scrap) self._check_analytic_account_no_error(scrap) diff --git a/stock_analytic/views/stock_move_views.xml b/stock_analytic/views/stock_move_views.xml index 6820140f50..e545cb81f0 100644 --- a/stock_analytic/views/stock_move_views.xml +++ b/stock_analytic/views/stock_move_views.xml @@ -62,6 +62,7 @@ position="after" > + diff --git a/stock_analytic/views/stock_scrap.xml b/stock_analytic/views/stock_scrap.xml index 36e905f959..13b3162f8f 100644 --- a/stock_analytic/views/stock_scrap.xml +++ b/stock_analytic/views/stock_scrap.xml @@ -6,6 +6,7 @@ +