Overview
The Account Statements module provides a complete view of customer credit accounts, aggregating sales totals, payment history, and current balances. This view serves as the foundation for customer statements and accounts receivable reporting.Database View: vw_EstadoCuenta
The account statement view consolidates credit sales with their payment history:View Model Structure
TheVwEstadoCuentum class (namespace: TechCore.Models) maps to the view:
Unique order number identifying the credit sale (e.g., “ORD-2024-001”)
Customer name from the
clientes tableOriginal sale amount including IVA (tax). This value never changes after the sale is created
Current outstanding balance. Automatically updated by the
TR_ActualizarSaldo trigger when payments are postedNumber of months in the installment plan. Null for credit sales without structured payment plans
Sum of all payments from
abonosVentas. Calculated by the view using SUM(ISNULL(a.monto,0)). Returns 0 for sales with no paymentsData Flow and Relationships
Credit Sale Created
A sale record is created in the
ventas table with:tipoPago = 'CREDITO'total= sale amount including taxsaldo= initially equal tototalmeses= payment plan duration (if applicable)
Payment Plan Generation
If installments are required, records are created in
planPagos with installment amounts and due datesCustomer Payments
As payments are received, records are inserted into
abonosVentas with:norden= order numbermonto= payment amountnumeroCuota= installment being paid
Automatic Balance Update
The
TR_ActualizarSaldo trigger executes on abonosVentas insert:- Reduces
ventas.saldoby payment amount - Marks corresponding
planPagosrecord as paid (pagada = 1)
Key Components
The LEFT JOIN Pattern
The view uses aLEFT JOIN to include sales even if no payments have been made:
- Sales with zero payments still appear in the statement
totalAbonadoreturns 0 (viaISNULL(a.monto,0)) instead of NULL
The GROUP BY Aggregation
The view groups by sale attributes to aggregate all payments:SUM(ISNULL(a.monto,0)) to calculate the total of all payments for each sale.
Account Statement Components
A complete account statement includes:Original Amount
The
Total field shows the original sale amount including taxTotal Paid
The
TotalAbonado field aggregates all payments from the abonosVentas tableCurrent Balance
The
Saldo field shows the outstanding amount maintained by the triggerPayment Terms
The
Meses field indicates the payment plan durationUsage Examples
Generate Customer Account Statement
Find Accounts with Outstanding Balances
Calculate Payment Progress
Export Statement for Reporting
Statement Accuracy and Data Integrity
Trigger-Based Balance Management
TheTR_ActualizarSaldo trigger ensures balance accuracy:
The trigger executes automatically on every payment insert, ensuring:
- The sale balance is immediately updated
- The installment status is synchronized
- No manual balance calculations are required
Balance Verification
While the trigger maintains the balance, you can verify accuracy:Filtering Options
View-Level Filters
The view includes built-in filters:Credit Sales Only
Credit Sales Only
tipoPago = 'CONTADO') which don’t generate account statementsActive Sales Only
Active Sales Only
Application-Level Filters
Add additional filters in your application code:Performance Considerations
The account statement view benefits from several database indexes:Related Views and Tables
ventas
Source table for credit sales including total amount, balance, and payment terms
abonosVentas
Payment transaction records aggregated in the
totalAbonado fieldclientes
Customer master data providing the
cliente name fieldplanPagos
Installment schedule (not directly joined but related through
norden)Common Statement Scenarios
Scenario 1: New Credit Sale
Scenario 2: Partial Payment
Scenario 3: Paid in Full
See Also
- Accounts Receivable - Overview of receivables tracking system
- Overdue Payments - Monitor late payments and calculate penalties