stock request table created
This commit is contained in:
@@ -26,6 +26,7 @@ type OrderRepository interface {
|
||||
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)
|
||||
GetSalesSummary(tid, lid int, fdate, tdate string) (*models.SalesSummaryResponse, error)
|
||||
}
|
||||
|
||||
type orderRepository struct {
|
||||
@@ -793,6 +794,100 @@ func (r *orderRepository) GetDistinctLocations() ([]models.OrderInsight, error)
|
||||
return locations, nil
|
||||
}
|
||||
|
||||
func (r *orderRepository) GetSalesSummary(tid, lid int, fdate, tdate string) (*models.SalesSummaryResponse, error) {
|
||||
var summary models.SalesSummaryResponse
|
||||
|
||||
whereClause := "tenantid = ? AND orderstatus IN ('delivered', 'completed') AND configid = 1"
|
||||
var params []interface{}
|
||||
params = append(params, tid)
|
||||
|
||||
if lid != 0 {
|
||||
whereClause += " AND locationid = ?"
|
||||
params = append(params, lid)
|
||||
}
|
||||
|
||||
if fdate != "" && tdate != "" {
|
||||
whereClause += " AND orderdate::date BETWEEN ? AND ?"
|
||||
params = append(params, fdate, tdate)
|
||||
}
|
||||
|
||||
totalsQuery := fmt.Sprintf(`
|
||||
SELECT
|
||||
COALESCE(SUM(COALESCE(ordervalue, 0) + COALESCE(orderamount, 0) + COALESCE(deliveryamt, 0)), 0) AS total_revenue,
|
||||
COUNT(orderheaderid) AS total_orders
|
||||
FROM orders
|
||||
WHERE %s`, whereClause)
|
||||
|
||||
var result struct {
|
||||
TotalRevenue float64
|
||||
TotalOrders int
|
||||
}
|
||||
|
||||
if err := r.db.Raw(totalsQuery, params...).Scan(&result).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
summary.TotalRevenue = result.TotalRevenue
|
||||
summary.TotalOrders = result.TotalOrders
|
||||
|
||||
if summary.TotalOrders > 0 {
|
||||
summary.AverageOrderValue = summary.TotalRevenue / float64(summary.TotalOrders)
|
||||
}
|
||||
|
||||
chartQuery := fmt.Sprintf(`
|
||||
SELECT
|
||||
CAST(orderdate AS DATE) AS date,
|
||||
COALESCE(SUM(COALESCE(ordervalue, 0) + COALESCE(orderamount, 0) + COALESCE(deliveryamt, 0)), 0) AS revenue,
|
||||
COUNT(orderheaderid) AS orders
|
||||
FROM orders
|
||||
WHERE %s
|
||||
GROUP BY CAST(orderdate AS DATE)
|
||||
ORDER BY date ASC`, whereClause)
|
||||
|
||||
var chartData []models.SalesSummaryChartData
|
||||
if err := r.db.Raw(chartQuery, params...).Scan(&chartData).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if chartData == nil {
|
||||
chartData = []models.SalesSummaryChartData{}
|
||||
}
|
||||
summary.ChartData = chartData
|
||||
|
||||
var topLocations []models.SalesSummaryTopLocation
|
||||
if lid == 0 {
|
||||
topWhere := "o.tenantid = ? AND o.orderstatus IN ('delivered', 'completed') AND o.configid = 1"
|
||||
var topParams []interface{}
|
||||
topParams = append(topParams, tid)
|
||||
if fdate != "" && tdate != "" {
|
||||
topWhere += " AND o.orderdate::date BETWEEN ? AND ?"
|
||||
topParams = append(topParams, fdate, tdate)
|
||||
}
|
||||
|
||||
cleanTopLocQuery := fmt.Sprintf(`
|
||||
SELECT
|
||||
COALESCE(l.locationname, 'Unknown') AS locationname,
|
||||
COALESCE(SUM(COALESCE(o.ordervalue, 0) + COALESCE(o.orderamount, 0) + COALESCE(o.deliveryamt, 0)), 0) AS revenue
|
||||
FROM orders o
|
||||
LEFT JOIN tenantlocations l ON o.locationid = l.locationid
|
||||
WHERE %s
|
||||
GROUP BY l.locationid, l.locationname
|
||||
ORDER BY revenue DESC
|
||||
LIMIT 5`, topWhere)
|
||||
|
||||
if err := r.db.Raw(cleanTopLocQuery, topParams...).Scan(&topLocations).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if topLocations == nil {
|
||||
topLocations = []models.SalesSummaryTopLocation{}
|
||||
}
|
||||
summary.TopLocations = topLocations
|
||||
|
||||
return &summary, nil
|
||||
}
|
||||
|
||||
func (r *orderRepository) GetMonthlyOrders(applocationid string) (*models.Ordermonths, error) {
|
||||
var orderMonths models.Ordermonths
|
||||
|
||||
@@ -920,13 +1015,14 @@ func (r *orderRepository) getSequenceno(tid int, prefix string) string {
|
||||
|
||||
var q1 string
|
||||
// Formats the ID as tenantid-subprefix+seqno (e.g., 908-20245189)
|
||||
if prefix == "ORD" {
|
||||
switch prefix {
|
||||
case "ORD":
|
||||
q1 = `SELECT CONCAT(tenantid, '-',
|
||||
CASE WHEN subprefix IS NULL OR CAST(subprefix AS TEXT) IN ('0', '0.0', '') THEN '' ELSE CAST(subprefix AS TEXT) END,
|
||||
COALESCE(MAX(orderseqno) + 1, 1)) AS orderseqno
|
||||
FROM ordersequences WHERE tenantid = ?
|
||||
GROUP BY tenantid, subprefix`
|
||||
} else if prefix == "INV" {
|
||||
case "INV":
|
||||
q1 = `SELECT CONCAT(tenantid, '-',
|
||||
CASE WHEN subprefix IS NULL OR CAST(subprefix AS TEXT) IN ('0', '0.0', '') THEN '' ELSE CAST(subprefix AS TEXT) END,
|
||||
COALESCE(MAX(invoiceseqno) + 1, 1)) AS orderseqno
|
||||
@@ -947,11 +1043,12 @@ func (r *orderRepository) getSequenceno(tid int, prefix string) string {
|
||||
|
||||
func (r *orderRepository) updateSeqno(tid int, prefix string) error {
|
||||
var field string
|
||||
if prefix == "ORD" {
|
||||
switch prefix {
|
||||
case "ORD":
|
||||
field = "orderseqno"
|
||||
} else if prefix == "INV" {
|
||||
case "INV":
|
||||
field = "invoiceseqno"
|
||||
} else {
|
||||
default:
|
||||
return fmt.Errorf("invalid prefix: %s", prefix)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user