new changes

This commit is contained in:
Suriya
2026-07-01 10:53:57 +05:30
parent 0d42ac84e1
commit f220c24c17
8 changed files with 369 additions and 1 deletions

View File

@@ -25,6 +25,7 @@ type OrderRepository interface {
UpdateOrder(order *models.Orders) error
CreateOrder(order models.Orders) (models.Orders, error)
GetCustomerOrdersv3(customerID, tenantID, moduleID, fromDate, toDate, orderStatus, keyword string, pageSize, offset int) ([]models.CustomerOrder, error)
GetRevenueSummary(tid, lid int, fdate, tdate string) (*models.TenantRevenueSummary, error)
}
type orderRepository struct {
@@ -681,6 +682,102 @@ func (r *orderRepository) GetLocationOrderSummary(tenantID int) ([]models.Orders
return data, nil
}
func (r *orderRepository) GetRevenueSummary(tid, lid int, fdate, tdate string) (*models.TenantRevenueSummary, error) {
var summary models.TenantRevenueSummary
// 1. If tid is 0 and lid is not 0, lookup the tenantid from tenantlocations
if tid == 0 && lid != 0 {
var tenantLoc struct {
Tenantid int
}
if err := r.db.Table("tenantlocations").Select("tenantid").Where("locationid = ?", lid).Scan(&tenantLoc).Error; err != nil {
return nil, err
}
tid = tenantLoc.Tenantid
}
if tid == 0 {
return nil, fmt.Errorf("tenant ID is required or could not be determined")
}
summary.Tenantid = tid
// 2. Fetch the tenant name
var tenant struct {
Tenantname string
}
if err := r.db.Table("tenants").Select("tenantname").Where("tenantid = ?", tid).Scan(&tenant).Error; err != nil {
return nil, err
}
summary.Tenantname = tenant.Tenantname
// 3. Fetch overall revenue for this tenant
overallQuery := `
SELECT COALESCE(SUM(orderamount), 0) AS overall_revenue
FROM orders
WHERE tenantid = ? AND orderstatus = 'delivered' AND configid = 1
`
var overallParams []interface{}
overallParams = append(overallParams, tid)
if lid != 0 {
overallQuery += " AND locationid = ?"
overallParams = append(overallParams, lid)
}
if fdate != "" && tdate != "" {
overallQuery += " AND orderdate::date BETWEEN ? AND ?"
overallParams = append(overallParams, fdate, tdate)
}
var overallRev float64
if err := r.db.Raw(overallQuery, overallParams...).Scan(&overallRev).Error; err != nil {
return nil, err
}
summary.OverallRevenue = overallRev
// 4. Fetch revenue details by location
locationQuery := `
SELECT
l.locationid,
l.locationname,
COALESCE(SUM(o.orderamount), 0) AS revenue
FROM tenantlocations l
LEFT JOIN orders o
ON l.locationid = o.locationid
AND o.orderstatus = 'delivered'
AND o.configid = 1
`
var locParams []interface{}
if fdate != "" && tdate != "" {
locationQuery += " AND o.orderdate::date BETWEEN ? AND ?"
locParams = append(locParams, fdate, tdate)
}
locationQuery += " WHERE l.tenantid = ?"
locParams = append(locParams, tid)
if lid != 0 {
locationQuery += " AND l.locationid = ?"
locParams = append(locParams, lid)
}
locationQuery += " GROUP BY l.locationid, l.locationname ORDER BY l.locationid"
var locRevenues []models.LocationRevenueDetails
if err := r.db.Raw(locationQuery, locParams...).Scan(&locRevenues).Error; err != nil {
return nil, err
}
if locRevenues == nil {
locRevenues = []models.LocationRevenueDetails{}
}
summary.LocationRevenue = locRevenues
return &summary, nil
}
func (r *orderRepository) GetDistinctLocations() ([]models.OrderInsight, error) {
var locations []models.OrderInsight