From 93456e9530e9844fdc87c92e409201c5bdb925af Mon Sep 17 00:00:00 2001 From: laurentbarontini Date: Sat, 14 Feb 2026 20:12:56 +0100 Subject: [PATCH] 14.02.26 --- modules/purchase_trade/dashboard.py | 6 ++-- modules/purchase_trade/valuation.py | 45 ++++++++++------------------- 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/modules/purchase_trade/dashboard.py b/modules/purchase_trade/dashboard.py index 34b9996..61aa362 100755 --- a/modules/purchase_trade/dashboard.py +++ b/modules/purchase_trade/dashboard.py @@ -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)) diff --git a/modules/purchase_trade/valuation.py b/modules/purchase_trade/valuation.py index ea36166..6b91ab2 100644 --- a/modules/purchase_trade/valuation.py +++ b/modules/purchase_trade/valuation.py @@ -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"