Added measurements, and devices statsh

Added stats endpoints
This commit is contained in:
Nedim
2023-10-20 12:03:59 +02:00
parent 6892c56c1e
commit d40b225e4e
12 changed files with 1120 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package invoice
import (
"fmt"
"strings"
"time"
"gitlab.com/pactual1/backend/models"
"gitlab.com/pactual1/backend/shared"
@@ -66,3 +67,48 @@ func GetInvoices(buyerName string, sortBy string, limit int, offset int, ids []i
return invoices, total, nil
}
func CountInvoicesByMultipleStatuses(companyID uint, startTime, endTime time.Time) (int64, int64, map[string]map[string]int64, error) {
var claimed, issued int64
monthlyCounts := make(map[string]map[string]int64)
// Iterate through each month within the specified date range
for dt := startTime; dt.Before(endTime); dt = dt.AddDate(0, 1, 0) {
monthEnd := dt.AddDate(0, 1, 0)
if monthEnd.After(endTime) {
monthEnd = endTime
}
// Initialize monthlyCounts for the current month
monthKey := dt.Format("2006-01")
monthlyCounts[monthKey] = map[string]int64{"insurance_claimed": 0, "invoice_issued": 0}
// Query for 'insurance_claimed' invoices for the current month
var monthlyClaimed int64
err := shared.GetDb().Model(&models.Invoice{}).
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND invoice_date >= ? AND invoice_due_date <= ?", "insurance_claimed", companyID, companyID, dt, monthEnd).
Count(&monthlyClaimed).Error
if err != nil {
return 0, 0, nil, err
}
// Query for 'invoice_issued' invoices for the current month
var monthlyIssued int64
err = shared.GetDb().Model(&models.Invoice{}).
Where("status = ? AND (buyer_id = ? OR seller_id = ?) AND invoice_date >= ? AND invoice_due_date <= ?", "invoice_issued", companyID, companyID, dt, monthEnd).
Count(&monthlyIssued).Error
if err != nil {
return 0, 0, nil, err
}
// Update the counts for the current month
monthlyCounts[monthKey]["insurance_claimed"] = monthlyClaimed
monthlyCounts[monthKey]["invoice_issued"] = monthlyIssued
// Update the total counts
claimed += monthlyClaimed
issued += monthlyIssued
}
return claimed, issued, monthlyCounts, nil
}