feat: Add totals from all filtered records to invoices, treasury, and trial balance
Previously, totals were computed client-side from only the current page data, which gave incorrect results when paginating. Now the backend calculates totals across ALL filtered records and returns them in the API response. - Invoice: Add total_sold_all field for sum of all filtered invoice balances - Treasury: Add sold_precedent_all, total_incasari_all, total_plati_all, sold_final_all - Trial Balance: Add 6-column totals (debit/credit for each balance type) - Frontend stores and views updated to use backend totals 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -142,7 +142,16 @@ class InvoiceService:
|
||||
count_query = f"SELECT COUNT(*) FROM ({base_query})"
|
||||
cursor.execute(count_query, params)
|
||||
total_count = cursor.fetchone()[0]
|
||||
|
||||
|
||||
# Query pentru TOTAL SOLD din TOATE facturile filtrate (nu doar pagina curentă)
|
||||
total_sold_query = f"""
|
||||
SELECT NVL(SUM(sold), 0) as total_sold
|
||||
FROM ({base_query})
|
||||
"""
|
||||
cursor.execute(total_sold_query, params)
|
||||
total_sold_result = cursor.fetchone()
|
||||
total_sold_all = Decimal(str(total_sold_result[0])) if total_sold_result else Decimal('0.00')
|
||||
|
||||
# Get accounting period - use params if provided, else from calendar
|
||||
if use_param_period:
|
||||
accounting_period = {
|
||||
@@ -226,7 +235,9 @@ class InvoiceService:
|
||||
page=filter_params.page,
|
||||
page_size=filter_params.page_size,
|
||||
has_more=len(invoices) == filter_params.page_size,
|
||||
accounting_period=accounting_period
|
||||
accounting_period=accounting_period,
|
||||
# Total sold din TOATE facturile filtrate
|
||||
total_sold_all=total_sold_all
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -253,6 +253,58 @@ class TreasuryService:
|
||||
cursor.execute(count_plsql, count_params)
|
||||
total_count = total_count_var.getvalue()
|
||||
|
||||
# Query pentru TOTALURI din TOATE înregistrările filtrate (nu doar pagina curentă)
|
||||
# sold_precedent = suma sold pentru rânduri cu dataact IS NULL
|
||||
# total_incasari = suma incasari pentru rânduri cu dataact IS NOT NULL
|
||||
# total_plati = suma plati pentru rânduri cu dataact IS NOT NULL
|
||||
totals_plsql = f"""
|
||||
DECLARE
|
||||
v_an NUMBER;
|
||||
v_luna NUMBER;
|
||||
BEGIN
|
||||
-- Obține anul și luna din parametri sau calendar
|
||||
{period_select}
|
||||
|
||||
{schema}.PACK_SESIUNE.SETAN(v_an);
|
||||
{schema}.PACK_SESIUNE.SETLUNA(v_luna);
|
||||
|
||||
-- Sold precedent: suma sold pentru rânduri fără dată (opening balance)
|
||||
SELECT NVL(SUM(sold), 0) INTO :sold_precedent_all
|
||||
FROM ({base_select}) sub{where_clause}
|
||||
WHERE dataact IS NULL;
|
||||
|
||||
-- Total încasări: suma incasari pentru rânduri cu dată (transactions)
|
||||
SELECT NVL(SUM(incasari), 0) INTO :total_incasari_all
|
||||
FROM ({base_select}) sub{where_clause}
|
||||
WHERE dataact IS NOT NULL;
|
||||
|
||||
-- Total plăți: suma plati pentru rânduri cu dată (transactions)
|
||||
SELECT NVL(SUM(plati), 0) INTO :total_plati_all
|
||||
FROM ({base_select}) sub{where_clause}
|
||||
WHERE dataact IS NOT NULL;
|
||||
END;
|
||||
"""
|
||||
|
||||
sold_precedent_all_var = cursor.var(oracledb.NUMBER)
|
||||
total_incasari_all_var = cursor.var(oracledb.NUMBER)
|
||||
total_plati_all_var = cursor.var(oracledb.NUMBER)
|
||||
|
||||
totals_params = {
|
||||
'sold_precedent_all': sold_precedent_all_var,
|
||||
'total_incasari_all': total_incasari_all_var,
|
||||
'total_plati_all': total_plati_all_var
|
||||
}
|
||||
if use_param_period:
|
||||
totals_params['param_an'] = filter_params.an
|
||||
totals_params['param_luna'] = filter_params.luna
|
||||
|
||||
cursor.execute(totals_plsql, totals_params)
|
||||
|
||||
sold_precedent_all = Decimal(str(sold_precedent_all_var.getvalue() or 0))
|
||||
total_incasari_all = Decimal(str(total_incasari_all_var.getvalue() or 0))
|
||||
total_plati_all = Decimal(str(total_plati_all_var.getvalue() or 0))
|
||||
sold_final_all = sold_precedent_all + total_incasari_all - total_plati_all
|
||||
|
||||
# Procesare rezultate
|
||||
registers = []
|
||||
total_incasari = Decimal('0.00')
|
||||
@@ -288,7 +340,12 @@ class TreasuryService:
|
||||
page=filter_params.page,
|
||||
page_size=filter_params.page_size,
|
||||
has_more=len(registers) == filter_params.page_size,
|
||||
accounting_period=AccountingPeriod(an=accounting_year, luna=accounting_month)
|
||||
accounting_period=AccountingPeriod(an=accounting_year, luna=accounting_month),
|
||||
# Totaluri din TOATE înregistrările filtrate
|
||||
sold_precedent_all=sold_precedent_all,
|
||||
total_incasari_all=total_incasari_all,
|
||||
total_plati_all=total_plati_all,
|
||||
sold_final_all=sold_final_all
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -135,6 +135,29 @@ class TrialBalanceService:
|
||||
cursor.execute(count_query, params)
|
||||
total_count = cursor.fetchone()[0]
|
||||
|
||||
# Query pentru TOTALURI din TOATE înregistrările filtrate (nu doar pagina curentă)
|
||||
totals_query = f"""
|
||||
SELECT
|
||||
NVL(SUM(PRECDEB), 0) as total_prec_deb,
|
||||
NVL(SUM(PRECCRED), 0) as total_prec_cred,
|
||||
NVL(SUM(RULDEB), 0) as total_rul_deb,
|
||||
NVL(SUM(RULCRED), 0) as total_rul_cred,
|
||||
NVL(SUM(SOLDDEB), 0) as total_sold_deb,
|
||||
NVL(SUM(SOLDCRED), 0) as total_sold_cred
|
||||
FROM ({base_query})
|
||||
"""
|
||||
cursor.execute(totals_query, params)
|
||||
totals_row = cursor.fetchone()
|
||||
|
||||
totals = {
|
||||
"total_sold_precedent_debit": Decimal(str(totals_row[0])) if totals_row else Decimal('0.00'),
|
||||
"total_sold_precedent_credit": Decimal(str(totals_row[1])) if totals_row else Decimal('0.00'),
|
||||
"total_rulaj_lunar_debit": Decimal(str(totals_row[2])) if totals_row else Decimal('0.00'),
|
||||
"total_rulaj_lunar_credit": Decimal(str(totals_row[3])) if totals_row else Decimal('0.00'),
|
||||
"total_sold_final_debit": Decimal(str(totals_row[4])) if totals_row else Decimal('0.00'),
|
||||
"total_sold_final_credit": Decimal(str(totals_row[5])) if totals_row else Decimal('0.00')
|
||||
}
|
||||
|
||||
# Add sorting
|
||||
base_query += f" ORDER BY {sort_by.upper()} {sort_order.upper()}"
|
||||
|
||||
@@ -189,5 +212,7 @@ class TrialBalanceService:
|
||||
"an": an,
|
||||
"cont_filter": cont_filter,
|
||||
"denumire_filter": denumire_filter
|
||||
}
|
||||
},
|
||||
# Totaluri din TOATE înregistrările filtrate (nu doar pagina curentă)
|
||||
"totals": totals
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user