fix sales
This commit is contained in:
parent
eb95459578
commit
3542104050
@ -77,33 +77,43 @@ func (r *AnalyticsRepositoryImpl) GetSalesAnalytics(ctx context.Context, organiz
|
|||||||
dateFormat = "DATE(o.created_at)"
|
dateFormat = "DATE(o.created_at)"
|
||||||
}
|
}
|
||||||
|
|
||||||
query := r.db.WithContext(ctx).
|
outletFilter := ""
|
||||||
Table("orders o").
|
args := []interface{}{organizationID, false, false, string(entities.PaymentStatusCompleted), dateFrom, dateTo}
|
||||||
Select(`
|
if outletID != nil {
|
||||||
|
outletFilter = "AND o.outlet_id = ?"
|
||||||
|
args = append(args, *outletID)
|
||||||
|
}
|
||||||
|
|
||||||
|
rawQuery := `
|
||||||
|
SELECT
|
||||||
` + dateFormat + ` as date,
|
` + dateFormat + ` as date,
|
||||||
COALESCE(SUM(o.total_amount), 0) as sales,
|
COALESCE(SUM(o.total_amount), 0) as sales,
|
||||||
COUNT(o.id) as orders,
|
COUNT(o.id) as orders,
|
||||||
COALESCE(SUM(CASE WHEN oi.status != 'cancelled' AND oi.is_fully_refunded = false THEN oi.quantity - COALESCE(oi.refund_quantity, 0) ELSE 0 END), 0) as items,
|
COALESCE(SUM(oi_agg.total_items), 0) as items,
|
||||||
COALESCE(SUM(o.tax_amount), 0) as tax,
|
COALESCE(SUM(o.tax_amount), 0) as tax,
|
||||||
COALESCE(SUM(o.discount_amount), 0) as discount,
|
COALESCE(SUM(o.discount_amount), 0) as discount,
|
||||||
COALESCE(SUM(o.total_amount - o.tax_amount - o.discount_amount), 0) as net_sales
|
COALESCE(SUM(o.total_amount - o.tax_amount - o.discount_amount), 0) as net_sales
|
||||||
`).
|
FROM orders o
|
||||||
Joins("LEFT JOIN order_items oi ON o.id = oi.order_id").
|
LEFT JOIN (
|
||||||
Where("o.organization_id = ?", organizationID).
|
SELECT
|
||||||
Where("o.is_void = ?", false).
|
oi.order_id,
|
||||||
Where("o.is_refund = ?", false).
|
SUM(oi.quantity - COALESCE(oi.refund_quantity, 0)) as total_items
|
||||||
Where("o.payment_status = ?", entities.PaymentStatusCompleted).
|
FROM order_items oi
|
||||||
Where("o.created_at >= ? AND o.created_at <= ?", dateFrom, dateTo)
|
WHERE oi.status != 'cancelled'
|
||||||
|
AND oi.is_fully_refunded = false
|
||||||
if outletID != nil {
|
GROUP BY oi.order_id
|
||||||
query = query.Where("o.outlet_id = ?", *outletID)
|
) oi_agg ON oi_agg.order_id = o.id
|
||||||
}
|
WHERE o.organization_id = ?
|
||||||
|
AND o.is_void = ?
|
||||||
err := query.
|
AND o.is_refund = ?
|
||||||
Group("date").
|
AND o.payment_status = ?
|
||||||
Order("date ASC").
|
AND o.created_at >= ? AND o.created_at <= ?
|
||||||
Scan(&results).Error
|
` + outletFilter + `
|
||||||
|
GROUP BY date
|
||||||
|
ORDER BY date ASC
|
||||||
|
`
|
||||||
|
|
||||||
|
err := r.db.WithContext(ctx).Raw(rawQuery, args...).Scan(&results).Error
|
||||||
return results, err
|
return results, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user