From 56a4bd9e82fd6541a5711deebcc38a9c1d7b0c33 Mon Sep 17 00:00:00 2001 From: laurentbarontini Date: Sun, 4 Jan 2026 21:06:11 +0100 Subject: [PATCH] 04.01.26 --- modules/purchase_trade/dashboard.py | 11 +++++-- modules/purchase_trade/valuation.py | 51 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/modules/purchase_trade/dashboard.py b/modules/purchase_trade/dashboard.py index 43f3082..6b5cb63 100755 --- a/modules/purchase_trade/dashboard.py +++ b/modules/purchase_trade/dashboard.py @@ -238,9 +238,12 @@ class Dashboard(ModelSQL, ModelView): Configuration = Pool().get('gr.configuration') config = Configuration.search(['id','>',0])[0] f1,f2,f3,f4,f5,d1,d2,d3,d4,d5 = self.get_last_five_fx_rates() - Pnl = Pool().get('valuation.valuation.line') - pnls = Pnl.search(['id','>',0]) - pnl_amount = "{:,.0f}".format(round(sum([e.amount for e in pnls]),0)) + Valuation = Pool().get('valuation.valuation') + total_t, total_t1, 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)) Open = Pool().get('open.position') opens = Open.search(['id','>',0]) exposure = "{:,.0f}".format(round(sum([e.net_exposure for e in opens]),0)) @@ -303,6 +306,8 @@ class Dashboard(ModelSQL, ModelView): config.dashboard + "/dashboard/index.html?pnl_amount=" + str(pnl_amount) + + "&pnl_variation=" + + str(pnl_variation) + "&exposure=" + str(exposure) + "&topay=" diff --git a/modules/purchase_trade/valuation.py b/modules/purchase_trade/valuation.py index 6977301..34cdcbb 100644 --- a/modules/purchase_trade/valuation.py +++ b/modules/purchase_trade/valuation.py @@ -353,6 +353,57 @@ class Valuation(ValuationBase, ModelView): "Valuation" __name__ = 'valuation.valuation' +class Valuation(ModelSQL): + __name__ = 'valuation.valuation' + + @classmethod + def get_totals(cls): + cursor = Transaction().connection.cursor() + table = cls.__table__() + + sql = f""" + WITH ranked 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 + FROM {table} + WHERE line IS NOT NULL + OR sale_line IS NOT NULL + ), + current_prev 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 + ) + 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 + """ + + cursor.execute(sql) + total_t, total_t1, variation = cursor.fetchone() + + return total_t, total_t1, variation + class ValuationLine(ValuationBase, ModelView): "Last Valuation" __name__ = 'valuation.valuation.line'