main #7

Merged
admin merged 620 commits from main into dev 2026-03-29 13:03:25 +00:00
2 changed files with 19 additions and 32 deletions
Showing only changes of commit 93456e9530 - Show all commits

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() f1,f2,f3,f4,f5,d1,d2,d3,d4,d5 = self.get_last_five_fx_rates()
Valuation = Pool().get('valuation.valuation') 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_amount = "{:,.0f}".format(round(total_t,0))
pnl_variation = 0 pnl_variation = 0
if total_t1: if last_total:
pnl_variation = "{:,.2f}".format(round((total_t/total_t1 - 1)*100,0)) pnl_variation = "{:,.2f}".format(round((last_variation/last_total)*100,0))
Open = Pool().get('open.position') Open = Pool().get('open.position')
opens = Open.search(['id','>',0]) opens = Open.search(['id','>',0])
exposure = "{:,.0f}".format(round(sum([e.net_exposure for e in opens]),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__() table = cls.__table__()
sql = f""" sql = f"""
WITH ranked AS ( WITH totals AS (
SELECT 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, date,
amount, SUM(amount) AS total_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} FROM {table}
WHERE line IS NOT NULL 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 SELECT
block_key, date,
MAX(CASE WHEN rn = 1 THEN amount END) AS amount_t, total_amount,
MAX(CASE WHEN rn = 2 THEN amount END) AS amount_t1 LAG(total_amount) OVER (ORDER BY date) AS previous_total,
FROM ranked ROW_NUMBER() OVER (ORDER BY date DESC) AS rn
WHERE rn <= 2 FROM totals
GROUP BY block_key
) )
SELECT SELECT
COALESCE(SUM(amount_t), 0) AS total_t, total_amount AS last_total,
COALESCE(SUM(amount_t1), 0) AS total_t1, total_amount - previous_total AS last_variation
COALESCE(SUM(amount_t), 0) FROM ranked
- COALESCE(SUM(amount_t1), 0) AS variation WHERE rn = 1;
FROM current_prev
""" """
cursor.execute(sql) 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): class ValuationLine(ValuationBase, ModelView):
"Last Valuation" "Last Valuation"