Refactor date handling in DashboardService for invoice queries

- Updated the date calculations in the DashboardService to use the `invoice_date` field instead of `created_at` for querying previous totals and monthly data.
- Changed the date variables to return only the date part, improving clarity and consistency in date handling.

These changes enhance the accuracy of invoice data retrieval in the dashboard service.
This commit is contained in:
2026-03-20 11:52:47 -05:00
parent 06eb461181
commit a8d548d91e

View File

@@ -68,13 +68,13 @@ class DashboardService:
)
# Total del mes anterior para comparación
last_month = datetime.utcnow() - timedelta(days=30)
last_month = (datetime.utcnow() - timedelta(days=30)).date()
previous_total = (
self.db.query(func.count(InvoiceHeader.id))
.filter(
InvoiceHeader.tenant_id == self.tenant_id,
InvoiceHeader.company_id == self.company_id,
InvoiceHeader.created_at < last_month,
InvoiceHeader.invoice_date < last_month,
)
.scalar()
or 0
@@ -91,17 +91,17 @@ class DashboardService:
)
# Facturas por mes (últimos 6 meses)
six_months_ago = datetime.utcnow() - timedelta(days=180)
six_months_ago = (datetime.utcnow() - timedelta(days=180)).date()
monthly_data = (
self.db.query(
func.date_trunc("month", InvoiceHeader.created_at).label("month"),
func.date_trunc("month", InvoiceHeader.invoice_date).label("month"),
func.count(InvoiceHeader.id).label("count"),
)
.filter(
InvoiceHeader.tenant_id == self.tenant_id,
InvoiceHeader.company_id == self.company_id,
InvoiceHeader.created_at >= six_months_ago,
InvoiceHeader.invoice_date >= six_months_ago,
)
.group_by("month")
.order_by("month")