04.01.26
This commit is contained in:
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user