corrections in stockrequest table
This commit is contained in:
@@ -27,6 +27,7 @@ type OrderRepository interface {
|
||||
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)
|
||||
GetTimeSeries(tenantID, locationID int, granularity, fromDate, toDate string) ([]models.TimeSeriesData, error)
|
||||
}
|
||||
|
||||
type orderRepository struct {
|
||||
@@ -1325,3 +1326,87 @@ func (r *orderRepository) GetTenantLocationOrders(input models.DeliveryQuery) ([
|
||||
result := r.db.Raw(query, params...).Find(&data)
|
||||
return data, result.Error
|
||||
}
|
||||
|
||||
func (r *orderRepository) GetTimeSeries(tenantID, locationID int, granularity, fromDate, toDate string) ([]models.TimeSeriesData, error) {
|
||||
var data []models.TimeSeriesData
|
||||
|
||||
var dateFunc, dateFuncO2 string
|
||||
switch granularity {
|
||||
case "month":
|
||||
dateFunc = "TO_CHAR(o.orderdate::date, 'YYYY-MM')"
|
||||
dateFuncO2 = "TO_CHAR(o2.orderdate::date, 'YYYY-MM')"
|
||||
case "year":
|
||||
dateFunc = "TO_CHAR(o.orderdate::date, 'YYYY')"
|
||||
dateFuncO2 = "TO_CHAR(o2.orderdate::date, 'YYYY')"
|
||||
case "day":
|
||||
fallthrough
|
||||
default:
|
||||
dateFunc = "TO_CHAR(o.orderdate::date, 'YYYY-MM-DD')"
|
||||
dateFuncO2 = "TO_CHAR(o2.orderdate::date, 'YYYY-MM-DD')"
|
||||
}
|
||||
|
||||
var locFilter, dateFilter string
|
||||
var params []interface{}
|
||||
|
||||
// subquery params
|
||||
params = append(params, tenantID)
|
||||
if locationID != 0 {
|
||||
locFilter = " AND o2.locationid = ?"
|
||||
params = append(params, locationID)
|
||||
}
|
||||
if fromDate != "" && toDate != "" {
|
||||
dateFilter = " AND o2.orderdate::date BETWEEN ? AND ?"
|
||||
params = append(params, fromDate, toDate)
|
||||
}
|
||||
|
||||
// main query params
|
||||
params = append(params, tenantID)
|
||||
if locationID != 0 {
|
||||
params = append(params, locationID)
|
||||
}
|
||||
if fromDate != "" && toDate != "" {
|
||||
params = append(params, fromDate, toDate)
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
SELECT
|
||||
%s AS label,
|
||||
COUNT(o.orderheaderid) AS orders,
|
||||
COALESCE(SUM(o.orderamount), 0) AS revenue,
|
||||
COALESCE(SUM(CASE WHEN o.orderstatus = 'cancelled' THEN 1 ELSE 0 END), 0) AS cancelled,
|
||||
COALESCE(SUM(CASE WHEN o.orderstatus IN ('delivered', 'completed') THEN 1 ELSE 0 END), 0) AS delivered,
|
||||
COALESCE(MAX(sku_counts.activeskus), 0) AS activeskus
|
||||
FROM orders o
|
||||
LEFT JOIN (
|
||||
SELECT %s AS label, COUNT(DISTINCT d.productid) AS activeskus
|
||||
FROM orders o2
|
||||
JOIN orderdetails d ON o2.orderheaderid = d.orderheaderid
|
||||
WHERE o2.tenantid = ? AND o2.configid = 1
|
||||
%s
|
||||
%s
|
||||
GROUP BY %s
|
||||
) sku_counts ON %s = sku_counts.label
|
||||
WHERE o.tenantid = ?
|
||||
AND o.configid = 1
|
||||
`, dateFunc, dateFuncO2, locFilter, dateFilter, dateFuncO2, dateFunc)
|
||||
|
||||
if locationID != 0 {
|
||||
query += " AND o.locationid = ?"
|
||||
}
|
||||
|
||||
if fromDate != "" && toDate != "" {
|
||||
query += " AND o.orderdate::date BETWEEN ? AND ?"
|
||||
}
|
||||
|
||||
query += fmt.Sprintf(" GROUP BY %s ORDER BY %s", dateFunc, dateFunc)
|
||||
|
||||
if err := r.db.Raw(query, params...).Scan(&data).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if data == nil {
|
||||
data = []models.TimeSeriesData{}
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
type StockRequestRepository interface {
|
||||
CreateStockRequest(req *models.StockRequest) error
|
||||
GetStockRequests(tenantID int, locationID int, status string, pageNo int, pageSize int) ([]models.StockRequest, error)
|
||||
GetStockRequests(tenantID int, locationID int, status string, date string, pageNo int, pageSize int) ([]models.StockRequest, error)
|
||||
GetStockRequestByID(requestID int) (*models.StockRequest, error)
|
||||
UpdateStockRequest(requestID int, status string) error
|
||||
}
|
||||
@@ -22,10 +22,30 @@ func NewStockRequestRepository(db *gorm.DB) StockRequestRepository {
|
||||
}
|
||||
|
||||
func (r *stockRequestRepository) CreateStockRequest(req *models.StockRequest) error {
|
||||
// Fetch product details
|
||||
var prod struct {
|
||||
Productname string
|
||||
Productimage string
|
||||
}
|
||||
r.db.Table("products").Select("productname, productimage").Where("productid = ?", req.Productid).Scan(&prod)
|
||||
if req.Productname == "" {
|
||||
req.Productname = prod.Productname
|
||||
}
|
||||
if req.Productimage == "" {
|
||||
req.Productimage = prod.Productimage
|
||||
}
|
||||
|
||||
// Fetch tenant name
|
||||
var tenantName string
|
||||
r.db.Table("tenants").Select("tenantname").Where("tenantid = ?", req.Tenantid).Scan(&tenantName)
|
||||
if req.Tenantname == "" {
|
||||
req.Tenantname = tenantName
|
||||
}
|
||||
|
||||
return r.db.Create(req).Error
|
||||
}
|
||||
|
||||
func (r *stockRequestRepository) GetStockRequests(tenantID int, locationID int, status string, pageNo int, pageSize int) ([]models.StockRequest, error) {
|
||||
func (r *stockRequestRepository) GetStockRequests(tenantID int, locationID int, status string, date string, pageNo int, pageSize int) ([]models.StockRequest, error) {
|
||||
var requests []models.StockRequest
|
||||
query := r.db.Table("stockrequests").
|
||||
Select("stockrequests.*, products.productname, products.productimage").
|
||||
@@ -40,6 +60,10 @@ func (r *stockRequestRepository) GetStockRequests(tenantID int, locationID int,
|
||||
query = query.Where("stockrequests.status = ?", status)
|
||||
}
|
||||
|
||||
if date != "" {
|
||||
query = query.Where("DATE(stockrequests.created) = ?", date)
|
||||
}
|
||||
|
||||
offset := (pageNo - 1) * pageSize
|
||||
err := query.Order("stockrequests.created DESC").Offset(offset).Limit(pageSize).Find(&requests).Error
|
||||
return requests, err
|
||||
@@ -47,7 +71,12 @@ func (r *stockRequestRepository) GetStockRequests(tenantID int, locationID int,
|
||||
|
||||
func (r *stockRequestRepository) GetStockRequestByID(requestID int) (*models.StockRequest, error) {
|
||||
var req models.StockRequest
|
||||
err := r.db.Table("stockrequests").Where("requestid = ?", requestID).First(&req).Error
|
||||
err := r.db.Table("stockrequests").
|
||||
Select("stockrequests.*, products.productname, products.productimage, tenants.tenantname, tenantlocations.locationname").
|
||||
Joins("left join products on products.productid = stockrequests.productid").
|
||||
Joins("left join tenants on tenants.tenantid = stockrequests.tenantid").
|
||||
Joins("left join tenantlocations on tenantlocations.locationid = stockrequests.locationid").
|
||||
Where("requestid = ?", requestID).First(&req).Error
|
||||
return &req, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user