[IMP] stock_account_valuation_report: black, isort, prettier

This commit is contained in:
AaronHForgeFlow
2021-05-06 14:45:42 +02:00
committed by Bernat Puig Font
parent 6a232ed715
commit 00b7ebda20
4 changed files with 116 additions and 86 deletions

View File

@@ -12,8 +12,6 @@
"category": "Warehouse Management", "category": "Warehouse Management",
"depends": ["stock_account"], "depends": ["stock_account"],
"license": "AGPL-3", "license": "AGPL-3",
"data": [ "data": ["views/product_product_views.xml"],
"views/product_product_views.xml", "installable": True,
],
'installable': True,
} }

View File

@@ -3,30 +3,30 @@
# Copyright 2018 Aleph Objects, Inc. # Copyright 2018 Aleph Objects, Inc.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import fields, models, _ from odoo import _, fields, models
class ProductProduct(models.Model): class ProductProduct(models.Model):
_inherit = 'product.product' _inherit = "product.product"
stock_value = fields.Float( stock_value = fields.Float("Inventory Value", compute="_compute_inventory_value")
'Inventory Value', compute='_compute_inventory_value') account_value = fields.Float("Accounting Value", compute="_compute_inventory_value")
account_value = fields.Float( qty_at_date = fields.Float("Inventory Quantity", compute="_compute_inventory_value")
'Accounting Value', compute='_compute_inventory_value')
qty_at_date = fields.Float(
'Inventory Quantity', compute='_compute_inventory_value')
account_qty_at_date = fields.Float( account_qty_at_date = fields.Float(
'Accounting Quantity', compute='_compute_inventory_value') "Accounting Quantity", compute="_compute_inventory_value"
)
stock_fifo_real_time_aml_ids = fields.Many2many( stock_fifo_real_time_aml_ids = fields.Many2many(
'account.move.line', compute='_compute_inventory_value') "account.move.line", compute="_compute_inventory_value"
)
stock_fifo_manual_move_ids = fields.Many2many( stock_fifo_manual_move_ids = fields.Many2many(
'stock.move', compute='_compute_inventory_value') "stock.move", compute="_compute_inventory_value"
)
def _compute_inventory_value(self): def _compute_inventory_value(self):
stock_move = self.env['stock.move'] stock_move = self.env["stock.move"]
self.env['account.move.line'].check_access_rights('read') self.env["account.move.line"].check_access_rights("read")
to_date = self.env.context.get('to_date', False) to_date = self.env.context.get("to_date", False)
location = self.env.context.get('location', False) location = self.env.context.get("location", False)
accounting_values = {} accounting_values = {}
if not location: if not location:
query = """ query = """
@@ -40,18 +40,18 @@ class ProductProduct(models.Model):
params = (tuple(self._ids,), self.env.user.company_id.id) params = (tuple(self._ids,), self.env.user.company_id.id)
if to_date: if to_date:
# pylint: disable=sql-injection # pylint: disable=sql-injection
query = query % ('AND aml.date <= %s',) query = query % ("AND aml.date <= %s",)
params = params + (to_date,) params = params + (to_date,)
else: else:
query = query % ('',) query = query % ("",)
self.env.cr.execute(query, params=params) self.env.cr.execute(query, params=params)
res = self.env.cr.fetchall() res = self.env.cr.fetchall()
for row in res: for row in res:
accounting_values[(row[0], row[1])] = (row[2], row[3], accounting_values[(row[0], row[1])] = (row[2], row[3], list(row[4]))
list(row[4]))
stock_move_domain = [ stock_move_domain = [
('product_id', 'in', self._ids), ("product_id", "in", self._ids),
('date', '<=', to_date)] + stock_move._get_all_base_domain() ("date", "<=", to_date),
] + stock_move._get_all_base_domain()
moves = stock_move.search(stock_move_domain) moves = stock_move.search(stock_move_domain)
history = {} history = {}
if to_date: if to_date:
@@ -65,75 +65,94 @@ class ProductProduct(models.Model):
args = (to_date, tuple(self._ids)) args = (to_date, tuple(self._ids))
self.env.cr.execute(query, args) self.env.cr.execute(query, args)
for row in self.env.cr.dictfetchall(): for row in self.env.cr.dictfetchall():
history.update({ history.update({row["product_id"]: row["cost"]})
row['product_id']: row['cost']
})
quantities_dict = self._compute_quantities_dict( quantities_dict = self._compute_quantities_dict(
self._context.get('lot_id'), self._context.get('owner_id'), self._context.get("lot_id"),
self._context.get('package_id'), self._context.get('from_date'), self._context.get("owner_id"),
self._context.get('to_date')) self._context.get("package_id"),
self._context.get("from_date"),
self._context.get("to_date"),
)
for product in self: for product in self:
qty_available = quantities_dict[product.id]['qty_available'] qty_available = quantities_dict[product.id]["qty_available"]
# Retrieve the values from accounting # Retrieve the values from accounting
# We cannot provide location-specific accounting valuation, # We cannot provide location-specific accounting valuation,
# so better, leave the data empty in that case: # so better, leave the data empty in that case:
if product.valuation == 'real_time' and not location: if product.valuation == "real_time" and not location:
valuation_account_id = \ valuation_account_id = (
product.categ_id.property_stock_valuation_account_id.id product.categ_id.property_stock_valuation_account_id.id
)
value, quantity, aml_ids = accounting_values.get( value, quantity, aml_ids = accounting_values.get(
(product.id, valuation_account_id)) or (0, 0, []) (product.id, valuation_account_id)
) or (0, 0, [])
product.account_value = value product.account_value = value
product.account_qty_at_date = quantity product.account_qty_at_date = quantity
product.stock_fifo_real_time_aml_ids = \ product.stock_fifo_real_time_aml_ids = self.env[
self.env['account.move.line'].browse(aml_ids) "account.move.line"
].browse(aml_ids)
# Retrieve the values from inventory # Retrieve the values from inventory
if product.cost_method in ['standard', 'average']: if product.cost_method in ["standard", "average"]:
price_used = product.standard_price price_used = product.standard_price
if to_date: if to_date:
price_used = history.get(product.id, 0) price_used = history.get(product.id, 0)
product.stock_value = price_used * qty_available product.stock_value = price_used * qty_available
product.qty_at_date = qty_available product.qty_at_date = qty_available
elif product.cost_method == 'fifo': elif product.cost_method == "fifo":
if to_date: if to_date:
if product.product_tmpl_id.valuation == 'manual_periodic': if product.product_tmpl_id.valuation == "manual_periodic":
product.stock_value = sum(moves.mapped('value')) product.stock_value = sum(moves.mapped("value"))
product.qty_at_date = qty_available product.qty_at_date = qty_available
product.stock_fifo_manual_move_ids = stock_move.browse( product.stock_fifo_manual_move_ids = stock_move.browse(
moves.ids) moves.ids
)
else: else:
product.stock_value, moves = \ product.stock_value, moves = product._sum_remaining_values()
product._sum_remaining_values()
product.qty_at_date = qty_available product.qty_at_date = qty_available
product.stock_fifo_manual_move_ids = moves product.stock_fifo_manual_move_ids = moves
def action_view_amls(self): def action_view_amls(self):
self.ensure_one() self.ensure_one()
to_date = self.env.context.get('to_date') to_date = self.env.context.get("to_date")
tree_view_ref = self.env.ref('stock_account.view_stock_account_aml') tree_view_ref = self.env.ref("stock_account.view_stock_account_aml")
form_view_ref = self.env.ref('account.view_move_line_form') form_view_ref = self.env.ref("account.view_move_line_form")
action = {'name': _('Accounting Valuation at date'), action = {
'type': 'ir.actions.act_window', 'view_type': 'form', "name": _("Accounting Valuation at date"),
'view_mode': 'tree,form', 'context': self.env.context, "type": "ir.actions.act_window",
'res_model': 'account.move.line', "view_type": "form",
'domain': [('id', 'in', self.with_context( "view_mode": "tree,form",
to_date=to_date).stock_fifo_real_time_aml_ids.ids)], "context": self.env.context,
'views': [(tree_view_ref.id, 'tree'), "res_model": "account.move.line",
(form_view_ref.id, 'form')]} "domain": [
(
"id",
"in",
self.with_context(to_date=to_date).stock_fifo_real_time_aml_ids.ids,
)
],
"views": [(tree_view_ref.id, "tree"), (form_view_ref.id, "form")],
}
return action return action
def action_view_stock_moves(self): def action_view_stock_moves(self):
self.ensure_one() self.ensure_one()
to_date = self.env.context.get('to_date') to_date = self.env.context.get("to_date")
tree_view_ref = self.env.ref( tree_view_ref = self.env.ref("stock_account.view_move_tree_valuation_at_date")
'stock_account.view_move_tree_valuation_at_date') form_view_ref = self.env.ref("stock.view_move_form")
form_view_ref = self.env.ref('stock.view_move_form') action = {
action = {'name': _('Inventory Valuation'), "name": _("Inventory Valuation"),
'type': 'ir.actions.act_window', 'view_type': 'form', "type": "ir.actions.act_window",
'view_mode': 'tree,form', 'context': self.env.context, "view_type": "form",
'res_model': 'stock.move', "view_mode": "tree,form",
'domain': [('id', 'in', self.with_context( "context": self.env.context,
to_date=to_date).stock_fifo_manual_move_ids.ids)], "res_model": "stock.move",
'views': [(tree_view_ref.id, 'tree'), "domain": [
(form_view_ref.id, 'form')]} (
"id",
"in",
self.with_context(to_date=to_date).stock_fifo_manual_move_ids.ids,
)
],
"views": [(tree_view_ref.id, "tree"), (form_view_ref.id, "form")],
}
return action return action

View File

@@ -9,26 +9,39 @@
<field name="valuation" invisible="1" /> <field name="valuation" invisible="1" />
</field> </field>
<button name="action_valuation_at_date_details" position="attributes"> <button name="action_valuation_at_date_details" position="attributes">
<attribute name="attrs">{'invisible': [('valuation', '!=', 'real_time')]}</attribute> <attribute
name="attrs"
>{'invisible': [('valuation', '!=', 'real_time')]}</attribute>
</button> </button>
<button name="action_valuation_at_date_details" position="attributes"> <button name="action_valuation_at_date_details" position="attributes">
<attribute name="invisible">True</attribute> <attribute name="invisible">True</attribute>
</button> </button>
<button name="action_valuation_at_date_details" position="after"> <button name="action_valuation_at_date_details" position="after">
<button name="action_view_stock_moves" <button
type="object" icon="fa-info-circle" name="action_view_stock_moves"
attrs="{'invisible': [('cost_method', '!=', 'fifo')]}" /> type="object"
icon="fa-info-circle"
attrs="{'invisible': [('cost_method', '!=', 'fifo')]}"
/>
</button> </button>
<field name="stock_value" position="after"> <field name="stock_value" position="after">
<field name="account_qty_at_date" sum="Accounting Qty" <field
attrs="{'invisible': [('valuation', '!=', 'real_time')]}"/> name="account_qty_at_date"
<field name="account_value" sum="Accounting Qty"
attrs="{'invisible': [('valuation', '!=', 'real_time')]}"
/>
<field
name="account_value"
sum="Accounting Valuation" sum="Accounting Valuation"
widget="monetary" widget="monetary"
attrs="{'invisible': [('valuation', '!=', 'real_time')]}"/> attrs="{'invisible': [('valuation', '!=', 'real_time')]}"
<button name="action_view_amls" />
type="object" icon="fa-info-circle" <button
attrs="{'invisible': [('valuation', '!=', 'real_time')]}" /> name="action_view_amls"
type="object"
icon="fa-info-circle"
attrs="{'invisible': [('valuation', '!=', 'real_time')]}"
/>
</field> </field>
</field> </field>
</record> </record>

View File

@@ -4,10 +4,10 @@ from odoo import models
class StockQuantityHistory(models.TransientModel): class StockQuantityHistory(models.TransientModel):
_inherit = 'stock.quantity.history' _inherit = "stock.quantity.history"
def open_table(self): def open_table(self):
action = super(StockQuantityHistory, self).open_table() action = super(StockQuantityHistory, self).open_table()
if self.compute_at_date: if self.compute_at_date:
action['name'] = '%s (%s)' % (action['name'], self.date) action["name"] = "%s (%s)" % (action["name"], self.date)
return action return action