[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",
"depends": ["stock_account"],
"license": "AGPL-3",
"data": [
"views/product_product_views.xml",
],
'installable': True,
"data": ["views/product_product_views.xml"],
"installable": True,
}

View File

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

View File

@@ -1,34 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="view_stock_product_tree2" model="ir.ui.view">
<field name="name">product.stock.tree.2.inherit</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="stock_account.view_stock_product_tree2"/>
<field name="inherit_id" ref="stock_account.view_stock_product_tree2" />
<field name="arch" type="xml">
<field name="cost_method" position="after">
<field name="valuation" invisible="1"/>
<field name="valuation" invisible="1" />
</field>
<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 name="action_valuation_at_date_details" position="attributes">
<attribute name="invisible">True</attribute>
</button>
<button name="action_valuation_at_date_details" position="after">
<button name="action_view_stock_moves"
type="object" icon="fa-info-circle"
attrs="{'invisible': [('cost_method', '!=', 'fifo')]}" />
<button
name="action_view_stock_moves"
type="object"
icon="fa-info-circle"
attrs="{'invisible': [('cost_method', '!=', 'fifo')]}"
/>
</button>
<field name="stock_value" position="after">
<field name="account_qty_at_date" sum="Accounting Qty"
attrs="{'invisible': [('valuation', '!=', 'real_time')]}"/>
<field name="account_value"
sum="Accounting Valuation"
widget="monetary"
attrs="{'invisible': [('valuation', '!=', 'real_time')]}"/>
<button name="action_view_amls"
type="object" icon="fa-info-circle"
attrs="{'invisible': [('valuation', '!=', 'real_time')]}" />
<field
name="account_qty_at_date"
sum="Accounting Qty"
attrs="{'invisible': [('valuation', '!=', 'real_time')]}"
/>
<field
name="account_value"
sum="Accounting Valuation"
widget="monetary"
attrs="{'invisible': [('valuation', '!=', 'real_time')]}"
/>
<button
name="action_view_amls"
type="object"
icon="fa-info-circle"
attrs="{'invisible': [('valuation', '!=', 'real_time')]}"
/>
</field>
</field>
</record>

View File

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