Skip to content

Commit

Permalink
[13.0][IMP] stock_analytic - add analytic tags on stock move
Browse files Browse the repository at this point in the history
  • Loading branch information
baimont committed Nov 11, 2020
1 parent 2f47ad5 commit 01bf08a
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 22 deletions.
20 changes: 10 additions & 10 deletions stock_analytic/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stock_analytic/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "
Expand Down
18 changes: 11 additions & 7 deletions stock_analytic/models/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion stock_analytic/models/stock_scrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 24 additions & 1 deletion stock_analytic/tests/test_stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -140,21 +152,31 @@ 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(
self.location,
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)
Expand All @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions stock_analytic/tests/test_stock_scrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)
1 change: 1 addition & 0 deletions stock_analytic/views/stock_move_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
position="after"
>
<field name="analytic_account_id" />
<field name="analytic_tag_ids" widget="many2many_tags" />
</xpath>
</field>
</record>
Expand Down
1 change: 1 addition & 0 deletions stock_analytic/views/stock_scrap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<field name="arch" type="xml">
<xpath expr="//group/group[1]" position="inside">
<field name="analytic_account_id" />
<field name="analytic_tag_ids" widget="many2many_tags" />
</xpath>
</field>
</record>
Expand Down

0 comments on commit 01bf08a

Please sign in to comment.