Skip to content
Snippets Groups Projects
Commit 083a3776 authored by Tiffany Chang (tic)'s avatar Tiffany Chang (tic)
Browse files

[FIX] purchase: correct amounts for purchase report multi-company/currency


Selection of multiple companies to view multi-company data was added
in v13, but at the time there was no way to have reports correctly
take into account currency rates when also working with multi-currency.
v14 onwards is able to correctly apply the currency rates, therefore we
fix the purchase report to do so.

Steps to reproduce:
1. Start with existing demo data + add a new company with currency = EUR
2. Activate multi-currencies + set a currency rate (not 1) for Euro to $
3. Open Purchase Report (Purchase > Reporting > Dashboard)
4. Activate demo company + new EUR company
5. Switch between USD and EUR company as selected company

Expected result:
  Dashboard monetary quantities switch between $ and EUR, i.e. both the
  amount changes according to current exchange rate and symbol.

Actual result:
  Currency symbol changes, but amount stays the same.

closes odoo/odoo#75651

Signed-off-by: default avatarArnold Moyaux <arm@odoo.com>
parent 36e20a34
No related branches found
No related tags found
No related merge requests found
...@@ -58,14 +58,10 @@ class PurchaseReport(models.Model): ...@@ -58,14 +58,10 @@ class PurchaseReport(models.Model):
qty_billed = fields.Float('Qty Billed', readonly=True) qty_billed = fields.Float('Qty Billed', readonly=True)
qty_to_be_billed = fields.Float('Qty to be Billed', readonly=True) qty_to_be_billed = fields.Float('Qty to be Billed', readonly=True)
def init(self): @property
# self._table = sale_report def _table_query(self):
tools.drop_view_if_exists(self.env.cr, self._table) ''' Report needs to be dynamic to take into account multi-company selected + multi-currency rates '''
self.env.cr.execute("""CREATE or REPLACE VIEW %s as ( return '%s %s %s' % (self._select(), self._from(), self._group_by())
%s
FROM ( %s )
%s
)""" % (self._table, self._select(), self._from(), self._group_by()))
def _select(self): def _select(self):
select_str = """ select_str = """
...@@ -89,14 +85,14 @@ class PurchaseReport(models.Model): ...@@ -89,14 +85,14 @@ class PurchaseReport(models.Model):
extract(epoch from age(po.date_approve,po.date_order))/(24*60*60)::decimal(16,2) as delay, extract(epoch from age(po.date_approve,po.date_order))/(24*60*60)::decimal(16,2) as delay,
extract(epoch from age(l.date_planned,po.date_order))/(24*60*60)::decimal(16,2) as delay_pass, extract(epoch from age(l.date_planned,po.date_order))/(24*60*60)::decimal(16,2) as delay_pass,
count(*) as nbr_lines, count(*) as nbr_lines,
sum(l.price_total / COALESCE(po.currency_rate, 1.0))::decimal(16,2) as price_total, sum(l.price_total / COALESCE(po.currency_rate, 1.0))::decimal(16,2) * currency_table.rate as price_total,
(sum(l.product_qty * l.price_unit / COALESCE(po.currency_rate, 1.0))/NULLIF(sum(l.product_qty/line_uom.factor*product_uom.factor),0.0))::decimal(16,2) as price_average, (sum(l.product_qty * l.price_unit / COALESCE(po.currency_rate, 1.0))/NULLIF(sum(l.product_qty/line_uom.factor*product_uom.factor),0.0))::decimal(16,2) * currency_table.rate as price_average,
partner.country_id as country_id, partner.country_id as country_id,
partner.commercial_partner_id as commercial_partner_id, partner.commercial_partner_id as commercial_partner_id,
analytic_account.id as account_analytic_id, analytic_account.id as account_analytic_id,
sum(p.weight * l.product_qty/line_uom.factor*product_uom.factor) as weight, sum(p.weight * l.product_qty/line_uom.factor*product_uom.factor) as weight,
sum(p.volume * l.product_qty/line_uom.factor*product_uom.factor) as volume, sum(p.volume * l.product_qty/line_uom.factor*product_uom.factor) as volume,
sum(l.price_subtotal / COALESCE(po.currency_rate, 1.0))::decimal(16,2) as untaxed_total, sum(l.price_subtotal / COALESCE(po.currency_rate, 1.0))::decimal(16,2) * currency_table.rate as untaxed_total,
sum(l.product_qty / line_uom.factor * product_uom.factor) as qty_ordered, sum(l.product_qty / line_uom.factor * product_uom.factor) as qty_ordered,
sum(l.qty_received / line_uom.factor * product_uom.factor) as qty_received, sum(l.qty_received / line_uom.factor * product_uom.factor) as qty_received,
sum(l.qty_invoiced / line_uom.factor * product_uom.factor) as qty_billed, sum(l.qty_invoiced / line_uom.factor * product_uom.factor) as qty_billed,
...@@ -109,6 +105,7 @@ class PurchaseReport(models.Model): ...@@ -109,6 +105,7 @@ class PurchaseReport(models.Model):
def _from(self): def _from(self):
from_str = """ from_str = """
FROM
purchase_order_line l purchase_order_line l
join purchase_order po on (l.order_id=po.id) join purchase_order po on (l.order_id=po.id)
join res_partner partner on po.partner_id = partner.id join res_partner partner on po.partner_id = partner.id
...@@ -121,7 +118,10 @@ class PurchaseReport(models.Model): ...@@ -121,7 +118,10 @@ class PurchaseReport(models.Model):
cr.company_id = po.company_id and cr.company_id = po.company_id and
cr.date_start <= coalesce(po.date_order, now()) and cr.date_start <= coalesce(po.date_order, now()) and
(cr.date_end is null or cr.date_end > coalesce(po.date_order, now()))) (cr.date_end is null or cr.date_end > coalesce(po.date_order, now())))
""" left join {currency_table} ON currency_table.company_id = po.company_id
""".format(
currency_table=self.env['res.currency']._get_query_currency_table({'multi_company': True, 'date': {'date_to': fields.Date.today()}}),
)
return from_str return from_str
def _group_by(self): def _group_by(self):
...@@ -152,7 +152,8 @@ class PurchaseReport(models.Model): ...@@ -152,7 +152,8 @@ class PurchaseReport(models.Model):
partner.country_id, partner.country_id,
partner.commercial_partner_id, partner.commercial_partner_id,
analytic_account.id, analytic_account.id,
po.id po.id,
currency_table.rate
""" """
return group_by_str return group_by_str
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment