This commit is contained in:
2026-02-14 20:12:56 +01:00
parent 8ee9354327
commit 93456e9530
2 changed files with 19 additions and 32 deletions

View File

@@ -290,11 +290,11 @@ class Dashboard(ModelSQL, ModelView):
f1,f2,f3,f4,f5,d1,d2,d3,d4,d5 = self.get_last_five_fx_rates()
Valuation = Pool().get('valuation.valuation')
total_t, total_t1, variation = Valuation.get_totals()
last_total,last_variation = Valuation.get_totals()
pnl_amount = "{:,.0f}".format(round(total_t,0))
pnl_variation = 0
if total_t1:
pnl_variation = "{:,.2f}".format(round((total_t/total_t1 - 1)*100,0))
if last_total:
pnl_variation = "{:,.2f}".format(round((last_variation/last_total)*100,0))
Open = Pool().get('open.position')
opens = Open.search(['id','>',0])
exposure = "{:,.0f}".format(round(sum([e.net_exposure for e in opens]),0))

View File

@@ -420,47 +420,34 @@ class Valuation(ValuationBase, ModelView):
table = cls.__table__()
sql = f"""
WITH ranked AS (
WITH totals AS (
SELECT
CASE
WHEN line IS NOT NULL THEN 'P:' || line::text
WHEN sale_line IS NOT NULL THEN 'S:' || sale_line::text
END AS block_key,
date,
amount,
ROW_NUMBER() OVER (
PARTITION BY
CASE
WHEN line IS NOT NULL THEN 'P:' || line::text
WHEN sale_line IS NOT NULL THEN 'S:' || sale_line::text
END
ORDER BY date DESC
) AS rn
SUM(amount) AS total_amount
FROM {table}
WHERE line IS NOT NULL
OR sale_line IS NOT NULL
OR sale_line IS NOT NULL
GROUP BY date
),
current_prev AS (
ranked AS (
SELECT
block_key,
MAX(CASE WHEN rn = 1 THEN amount END) AS amount_t,
MAX(CASE WHEN rn = 2 THEN amount END) AS amount_t1
FROM ranked
WHERE rn <= 2
GROUP BY block_key
date,
total_amount,
LAG(total_amount) OVER (ORDER BY date) AS previous_total,
ROW_NUMBER() OVER (ORDER BY date DESC) AS rn
FROM totals
)
SELECT
COALESCE(SUM(amount_t), 0) AS total_t,
COALESCE(SUM(amount_t1), 0) AS total_t1,
COALESCE(SUM(amount_t), 0)
- COALESCE(SUM(amount_t1), 0) AS variation
FROM current_prev
total_amount AS last_total,
total_amount - previous_total AS last_variation
FROM ranked
WHERE rn = 1;
"""
cursor.execute(sql)
total_t, total_t1, variation = cursor.fetchone()
last_total, last_variation = cursor.fetchone()
return total_t, total_t1, variation
return last_total, last_variation
class ValuationLine(ValuationBase, ModelView):
"Last Valuation"