[IMP]purchase: Format currency on the dashboard of Purchase List View

closes odoo/odoo#81535

X-original-commit: 33c7e5df6ca5aae4e53b5f3ae954a69820da3e20
Signed-off-by: Tiffany Chang <tic@odoo.com>
This commit is contained in:
lathuat1997
2021-12-16 15:57:47 +00:00
committed by Tiffany Chang (tic)
parent 7213ee5527
commit f37d609bf9
+6 -9
View File
@@ -10,7 +10,7 @@ from werkzeug.urls import url_encode
from odoo import api, fields, models, _
from odoo.osv import expression
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT, formatLang, get_lang, groupby
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT, format_amount, formatLang, get_lang, groupby
from odoo.tools.float_utils import float_compare, float_is_zero, float_round
from odoo.exceptions import AccessError, UserError, ValidationError
@@ -674,24 +674,21 @@ class PurchaseOrder(models.Model):
result['my_late'] = po.search_count([('state', 'in', ['draft', 'sent', 'to approve']), ('date_order', '<', fields.Datetime.now()), ('user_id', '=', self.env.uid)])
# Calculated values ('avg order value', 'avg days to purchase', and 'total last 7 days') note that 'avg order value' and
# 'total last 7 days' takes into account exchange rate and current company's currency's precision. Min of currency precision
# is taken to easily extract it from query.
# 'total last 7 days' takes into account exchange rate and current company's currency's precision.
# This is done via SQL for scalability reasons
query = """SELECT AVG(COALESCE(po.amount_total / NULLIF(po.currency_rate, 0), po.amount_total)),
AVG(extract(epoch from age(po.date_approve,po.create_date)/(24*60*60)::decimal(16,2))),
SUM(CASE WHEN po.date_approve >= %s THEN COALESCE(po.amount_total / NULLIF(po.currency_rate, 0), po.amount_total) ELSE 0 END),
MIN(curr.decimal_places)
SUM(CASE WHEN po.date_approve >= %s THEN COALESCE(po.amount_total / NULLIF(po.currency_rate, 0), po.amount_total) ELSE 0 END)
FROM purchase_order po
JOIN res_company comp ON (po.company_id = comp.id)
JOIN res_currency curr ON (comp.currency_id = curr.id)
WHERE po.state in ('purchase', 'done')
AND po.company_id = %s
"""
self._cr.execute(query, (one_week_ago, self.env.company.id))
res = self.env.cr.fetchone()
result['all_avg_order_value'] = round(res[0] or 0, res[3])
result['all_avg_days_to_purchase'] = round(res[1] or 0, 2)
result['all_total_last_7_days'] = round(res[2] or 0, res[3])
currency = self.env.company.currency_id
result['all_avg_order_value'] = format_amount(self.env, res[0] or 0, currency)
result['all_total_last_7_days'] = format_amount(self.env, res[2] or 0, currency)
return result