[MIG] stock_account_quantity_history_location: Migration to 13.0

This commit is contained in:
Víctor Martínez
2021-05-03 16:39:35 +02:00
parent 0e9dfeb3cb
commit c7cd78a631
5 changed files with 110 additions and 50 deletions

View File

@@ -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,
}

View File

@@ -1 +1,5 @@
* Jordi Ballester Alomar <jordi.ballester@eficent.com>
* `Tecnativa <https://www.tecnativa.com>`_:
* Víctor Martínez

View File

@@ -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)

View File

@@ -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

View File

@@ -3,13 +3,16 @@
<record id="view_stock_account_quantity_history_location" model="ir.ui.view">
<field name="name">Valuation Report</field>
<field name="model">stock.quantity.history</field>
<field name="inherit_id" ref="stock_account.view_stock_quantity_history" />
<field name="inherit_id" ref="stock.view_stock_quantity_history" />
<field
name="groups_id"
eval="[(4, ref('stock.group_stock_multi_locations'))]"
/>
<field name="arch" type="xml">
<field name="compute_at_date" position="before">
<field name="location_id" />
<field name="inventory_datetime" position="before">
<field
name="include_child_locations"
attrs="{'invisible': [('location_id', '=', False)]}"
name="location_id"
invisible="context.get('active_model') != 'stock.valuation.layer'"
/>
</field>
</field>