Skip to content

Commit

Permalink
[FIX] stock_account: correct forecast report total amount and currency
Browse files Browse the repository at this point in the history
Steps:
- Go to Settings > Companies > Manage Companies
- Create a company (1) with a different currency than the one you're in
- Go to inventory > Products > Products > Create a product (2)
  - Storable Product
  - Cost: 10
- Click Update Quantity
- Create a new quantity line with 1 in On Hand Quantity
- Switch to company (1)
- Go to Inventory > Products > Products > Edit product (2)
  - Storable Product
  - Cost: 15
- Click Update Quantity
- Create a new quantity line with 1 in On Hand Quantity
- Go back to the product
- Click the "Forecasted" smart button

Bug:
Traceback here:
https://github.com/odoo/odoo/blob/47bfdf0592a5dd93870ca1a9da326351d70087bb/addons/stock_account/report/report_stock_forecasted.py#L17
ValueError: Expected singleton: res.currency(2, 1)

Explanation:
The amount is the sum of the `stock.valuation.layer`s of a product
across all companies and these companies may have different currencies.

This commit scopes the computation of the amount inside the company of
the current warehouse.

Also the `stock.quant` values displayed in the "On Hand" report are not
correct for the other companies when displaying their data by selecting
all the checkboxes in the global company dropdown.
This commit also fixes the amounts in the On Hand view by using the
standard price per company and attributing the correct currency to the
`stock.quant`.

opw:2444593

closes odoo#65267

X-original-commit: 67d975b
Signed-off-by: backspac <backspac@users.noreply.github.com>
  • Loading branch information
backspac committed Jan 29, 2021
1 parent d70e126 commit 2dd697f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 3 additions & 3 deletions addons/stock_account/models/stock_quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def _compute_value(self):
average cost is the same for all location and the valuation field is
a estimation more than a real value).
"""
self.currency_id = self.env.company.currency_id
for quant in self:
quant.currency_id = quant.company_id.currency_id
# If the user didn't enter a location yet while enconding a quant.
if not quant.location_id:
quant.value = 0
Expand All @@ -36,10 +36,10 @@ def _compute_value(self):
if float_is_zero(quantity, precision_rounding=quant.product_id.uom_id.rounding):
quant.value = 0.0
continue
average_cost = quant.product_id.value_svl / quantity
average_cost = quant.product_id.with_company(quant.company_id).value_svl / quantity
quant.value = quant.quantity * average_cost
else:
quant.value = quant.quantity * quant.product_id.standard_price
quant.value = quant.quantity * quant.product_id.with_company(quant.company_id).standard_price

@api.model
def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
Expand Down
11 changes: 9 additions & 2 deletions addons/stock_account/report/report_stock_forecasted.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ def _compute_draft_quantity_count(self, product_template_ids, product_variant_id
""" Overrides to computes the valuations of the stock. """
res = super()._compute_draft_quantity_count(product_template_ids, product_variant_ids, wh_location_ids)
domain = self._product_domain(product_template_ids, product_variant_ids)
svl = self.env['stock.valuation.layer'].search(domain)
company = self.env['stock.location'].browse(wh_location_ids).mapped('company_id')
svl = self.env['stock.valuation.layer'].search(domain + [('company_id', '=', company.id)])
currency = svl.currency_id or self.env.company.currency_id
value = float_repr(sum(svl.mapped('value')), precision_digits=currency.decimal_places)
quantity = sum(svl.filtered(lambda layer: layer.stock_move_id.location_dest_id.id in wh_location_ids).mapped('quantity'))
if quantity:
total_quantity = sum(svl.mapped('quantity'))
value = sum(svl.mapped('value')) * (quantity / total_quantity)
else:
value = 0
value = float_repr(value, precision_digits=currency.decimal_places)
if currency.position == 'after':
value = '%s %s' % (value, currency.symbol)
else:
Expand Down

0 comments on commit 2dd697f

Please sign in to comment.