From c7cd78a6314f193a709eb7cbab0a86ab1c24538f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Mon, 3 May 2021 16:39:35 +0200 Subject: [PATCH] [MIG] stock_account_quantity_history_location: Migration to 13.0 --- .../__manifest__.py | 9 +- .../readme/CONSTRIBUTORS.rst | 4 + .../tests/test_valuation_location.py | 100 +++++++++++++----- .../wizards/stock_quantity_history.py | 34 +++--- .../wizards/stock_quantity_history.xml | 13 ++- 5 files changed, 110 insertions(+), 50 deletions(-) diff --git a/stock_account_quantity_history_location/__manifest__.py b/stock_account_quantity_history_location/__manifest__.py index 6637473..f207572 100644 --- a/stock_account_quantity_history_location/__manifest__.py +++ b/stock_account_quantity_history_location/__manifest__.py @@ -1,5 +1,6 @@ # Copyright 2019 Eficent Business and IT Consulting Services, S.L. # Copyright 2019 Aleph Objects, Inc. +# Copyright 2021 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { @@ -7,11 +8,11 @@ "summary": """ Glue module between Stock Account and Stock Quantity History Location modules""", - "version": "12.0.1.1.0", + "version": "13.0.1.0.0", "license": "AGPL-3", - "author": "Eficent," "Odoo Community Association (OCA)", + "author": "Eficent, Odoo Community Association (OCA)", "website": "https://github.com/OCA/stock-logistics-reporting", - "depends": ["stock_account", "stock_quantity_history_location",], - "data": ["wizards/stock_quantity_history.xml",], + "depends": ["stock_account", "stock_quantity_history_location"], + "data": ["wizards/stock_quantity_history.xml"], "auto_install": True, } diff --git a/stock_account_quantity_history_location/readme/CONSTRIBUTORS.rst b/stock_account_quantity_history_location/readme/CONSTRIBUTORS.rst index 6860aff..36bb64b 100644 --- a/stock_account_quantity_history_location/readme/CONSTRIBUTORS.rst +++ b/stock_account_quantity_history_location/readme/CONSTRIBUTORS.rst @@ -1 +1,5 @@ * Jordi Ballester Alomar + +* `Tecnativa `_: + + * Víctor Martínez diff --git a/stock_account_quantity_history_location/tests/test_valuation_location.py b/stock_account_quantity_history_location/tests/test_valuation_location.py index 34c591c..1d0eb1b 100644 --- a/stock_account_quantity_history_location/tests/test_valuation_location.py +++ b/stock_account_quantity_history_location/tests/test_valuation_location.py @@ -1,5 +1,8 @@ # Copyright 2020 Tecnativa - David Vidal +# Copyright 2021 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from datetime import datetime + from odoo.addons.stock_quantity_history_location.tests import ( test_stock_quantity_history_location, ) @@ -11,48 +14,87 @@ class TestValuationLocation( @classmethod def setUpClass(cls): super().setUpClass() - cls.env["product.price.history"].create( - {"datetime": "2019-08-12", "product_id": cls.product.id, "cost": 35.01,} + cls.sqh_model = cls.env["stock.quantity.history"] + cls.sqh_model_svl = cls.env["stock.quantity.history"].with_context( + active_model="stock.valuation.layer" ) - cls.env["product.price.history"].create( - {"datetime": "2020-01-01", "product_id": cls.product.id, "cost": 33.33,} - ) - cls.product.categ_id.property_cost_method = "standard" - cls.test_stock_loc2 = cls.env["stock.location"].create( - { - "usage": "internal", - "name": "Test Stock Location 2", - "company_id": cls.main_company.id, - } - ) - # Create a move for the past - move = cls.env["stock.move"].create( + + def _create_stock_move(self, location_dest_id, qty): + move = self.env["stock.move"].create( { "name": "Stock move in", - "location_id": cls.supplier_location.id, - "location_dest_id": cls.test_stock_loc2.id, - "product_id": cls.product.id, - "product_uom": cls.product.uom_id.id, - "product_uom_qty": 50.0, + "location_id": self.supplier_location.id, + "location_dest_id": location_dest_id.id, + "product_id": self.product.id, + "product_uom": self.product.uom_id.id, + "product_uom_qty": qty, } ) move._action_confirm() move._action_assign() move_line = move.move_line_ids[0] - move_line.qty_done = 50.0 + move_line.qty_done = qty move._action_done() - move.date = "2020-01-01" + return move def test_stock_location_valuation(self): - wizard = self.env["stock.quantity.history"].create( + wizard = self.sqh_model.create( { - "location_id": self.test_stock_loc2.id, + "location_id": self.test_stock_loc.id, "include_child_locations": True, - "compute_at_date": 1, - "date": "2020-01-01", + "inventory_datetime": datetime.today(), } ) - action = wizard.with_context(company_owned=True, valuation=True).open_table() - self.assertAlmostEqual( - self.product.with_context(action["context"]).stock_value, 1666.5 + action = wizard.with_context(company_owned=True).open_at_date() + qty_svl_prev = self.product.with_context(action["context"]).quantity_svl + move = self._create_stock_move(location_dest_id=self.test_stock_loc, qty=60) + wizard["inventory_datetime"] = move.stock_valuation_layer_ids.create_date + action = wizard.with_context(company_owned=True).open_at_date() + product = self.product.with_context(action["context"]) + self.assertEqual(product.quantity_svl - qty_svl_prev, 60) + + def test_stock_location_valuation_without_location_id(self): + wizard = self.sqh_model.create( + {"include_child_locations": True, "inventory_datetime": datetime.today()} ) + action = wizard.with_context(company_owned=True, valuation=True).open_at_date() + qty_svl_prev = self.product.with_context(action["context"]).quantity_svl + self._create_stock_move(location_dest_id=self.test_stock_loc, qty=50) + move = self._create_stock_move( + location_dest_id=self.child_test_stock_loc, qty=10 + ) + wizard["inventory_datetime"] = move.stock_valuation_layer_ids.create_date + action = wizard.with_context(company_owned=True).open_at_date() + product = self.product.with_context(action["context"]) + self.assertEqual(product.quantity_svl - qty_svl_prev, 60) + + def test_stock_location_valuation_with_svl(self): + wizard = self.sqh_model_svl.create( + { + "location_id": self.test_stock_loc.id, + "include_child_locations": True, + "inventory_datetime": datetime.today(), + } + ) + action = wizard.with_context(company_owned=True).open_at_date() + records_prev = self.env[action["res_model"]].search(action["domain"]) + move = self._create_stock_move(location_dest_id=self.test_stock_loc, qty=100) + wizard["inventory_datetime"] = move.stock_valuation_layer_ids.create_date + action = wizard.with_context(company_owned=True).open_at_date() + records = self.env[action["res_model"]].search(action["domain"]) - records_prev + self.assertEqual(sum(records.mapped("quantity")), 100) + + def test_stock_location_valuation_with_svl_without_location_id(self): + wizard = self.sqh_model_svl.create( + {"include_child_locations": True, "inventory_datetime": datetime.today()} + ) + action = wizard.with_context(company_owned=True).open_at_date() + records_prev = self.env[action["res_model"]].search(action["domain"]) + self._create_stock_move(location_dest_id=self.test_stock_loc, qty=100) + move = self._create_stock_move( + location_dest_id=self.child_test_stock_loc, qty=100 + ) + wizard["inventory_datetime"] = move.stock_valuation_layer_ids.create_date + action = wizard.with_context(company_owned=True).open_at_date() + records = self.env[action["res_model"]].search(action["domain"]) - records_prev + self.assertEqual(sum(records.mapped("quantity")), 200) diff --git a/stock_account_quantity_history_location/wizards/stock_quantity_history.py b/stock_account_quantity_history_location/wizards/stock_quantity_history.py index 690ee21..8d8220b 100644 --- a/stock_account_quantity_history_location/wizards/stock_quantity_history.py +++ b/stock_account_quantity_history_location/wizards/stock_quantity_history.py @@ -1,26 +1,18 @@ # Copyright 2019 Eficent Business and IT Consulting Services, S.L. # Copyright 2019 Aleph Objects, Inc. +# Copyright 2021 Tecnativa - Víctor Martínez # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). import ast from odoo import models +from odoo.osv import expression class StockQuantityHistory(models.TransientModel): _inherit = "stock.quantity.history" - def open_table(self): - """Stock valuation goes by a different path than stock report, so - we ensure the correct context as well""" - action = super().open_table() - if not self.env.context.get("valuation"): - return action - # Show 0 quantities on Inventory Valuation to display Account Valuation - # anomalies, such as, non 0 stock_value on cost_method FIFO - if not self.location_id: - domain = ast.literal_eval(action["domain"]) - domain.pop(domain.index(("qty_available", "!=", 0))) - action["domain"] = domain + def open_at_date(self): + action = super().open_at_date() ctx = action["context"] if isinstance(ctx, str): ctx = ast.literal_eval(ctx) @@ -36,4 +28,22 @@ class StockQuantityHistory(models.TransientModel): self.location_id.complete_name, ) action["context"] = ctx + if self.env.context.get("active_model") == "stock.valuation.layer": + action["domain"] = expression.AND( + [ + action["domain"], + [("stock_move_id.location_dest_id", "=", self.location_id.id)], + ] + ) + else: + # Show 0 quantities on Inventory Valuation to display Account Valuation + # anomalies, such as, non 0 stock_value on cost_method FIFO + if self.env.context.get("active_model") == "stock.valuation.layer": + action["domain"] = expression.AND( + [action["domain"], [("quantity", "!=", 0)]] + ) + else: + action["domain"] = expression.AND( + [action["domain"], [("qty_available", "!=", 0)]] + ) return action diff --git a/stock_account_quantity_history_location/wizards/stock_quantity_history.xml b/stock_account_quantity_history_location/wizards/stock_quantity_history.xml index 9f64613..255ad9f 100644 --- a/stock_account_quantity_history_location/wizards/stock_quantity_history.xml +++ b/stock_account_quantity_history_location/wizards/stock_quantity_history.xml @@ -3,13 +3,16 @@ Valuation Report stock.quantity.history - + + - - +