store wise orders change
This commit is contained in:
@@ -209,6 +209,14 @@ func (ctl *DeliveriesController) GetDeliveries(c *fiber.Ctx) error {
|
|||||||
stat := c.Query("status")
|
stat := c.Query("status")
|
||||||
keyword := c.Query("keyword")
|
keyword := c.Query("keyword")
|
||||||
|
|
||||||
|
if pid == 0 && tid == 0 && uid == 0 && cid == 0 && aid == 0 && aud == 0 {
|
||||||
|
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"code": http.StatusBadRequest,
|
||||||
|
"message": "At least one of tenantid, partnerid, customerid, applocationid, userid or appuserid is required",
|
||||||
|
"status": false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
info := models.DeliveryQuery{
|
info := models.DeliveryQuery{
|
||||||
Partnerid: pid,
|
Partnerid: pid,
|
||||||
Tenantid: tid,
|
Tenantid: tid,
|
||||||
|
|||||||
@@ -100,8 +100,13 @@ func (ctl *OrderController) GetOrders(c *fiber.Ctx) error {
|
|||||||
orders, err = ctl.orderService.GetUserOrders(stat, fdate, tdate, uid, pageno, pagesize, keyword)
|
orders, err = ctl.orderService.GetUserOrders(stat, fdate, tdate, uid, pageno, pagesize, keyword)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// All orders
|
// No scoping id supplied (tenantid/partnerid/customerid/applocationid/appuserid).
|
||||||
orders, err = ctl.orderService.GetAllOrders(stat, fdate, tdate, pageno, pagesize, keyword)
|
// Refuse instead of silently returning every order in the database.
|
||||||
|
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"status": false,
|
||||||
|
"code": http.StatusBadRequest,
|
||||||
|
"message": "At least one of tenantid, partnerid, customerid, applocationid or appuserid is required",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -129,6 +134,14 @@ func (ctl *OrderController) GetOrderSummary(c *fiber.Ctx) error {
|
|||||||
fdate := c.Query("fromdate")
|
fdate := c.Query("fromdate")
|
||||||
tdate := c.Query("todate")
|
tdate := c.Query("todate")
|
||||||
|
|
||||||
|
if tid == 0 && pid == 0 && cid == 0 && lid == 0 {
|
||||||
|
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"code": http.StatusBadRequest,
|
||||||
|
"message": "At least one of tenantid, partnerid, customerid or locationid is required",
|
||||||
|
"status": false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
data, err := ctl.orderService.GetOrderSummary(tid, pid, cid, lid, fdate, tdate)
|
data, err := ctl.orderService.GetOrderSummary(tid, pid, cid, lid, fdate, tdate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(http.StatusConflict).JSON(fiber.Map{
|
return c.Status(http.StatusConflict).JSON(fiber.Map{
|
||||||
@@ -151,6 +164,14 @@ func (ctl *OrderController) GetlocationOrderSummary(c *fiber.Ctx) error {
|
|||||||
tenantIDStr := c.Query("tenantid")
|
tenantIDStr := c.Query("tenantid")
|
||||||
tenantID, _ := strconv.Atoi(tenantIDStr)
|
tenantID, _ := strconv.Atoi(tenantIDStr)
|
||||||
|
|
||||||
|
if tenantID == 0 {
|
||||||
|
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"status": false,
|
||||||
|
"code": http.StatusBadRequest,
|
||||||
|
"message": "tenantid is required",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
data, err := ctl.orderService.GetLocationOrderSummary(tenantID)
|
data, err := ctl.orderService.GetLocationOrderSummary(tenantID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
|
||||||
@@ -336,6 +357,14 @@ func (ctl *OrderController) GetCustomerOrders(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
offset := (pageNo - 1) * pageSize
|
offset := (pageNo - 1) * pageSize
|
||||||
|
|
||||||
|
if customerID == "" {
|
||||||
|
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"code": http.StatusBadRequest,
|
||||||
|
"status": false,
|
||||||
|
"message": "customerid is required",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
orders, err := ctl.orderService.GetCustomerOrdersv3(customerID, tenantID, moduleID, fromDate, toDate, orderStatus, keyword, pageSize, offset)
|
orders, err := ctl.orderService.GetCustomerOrdersv3(customerID, tenantID, moduleID, fromDate, toDate, orderStatus, keyword, pageSize, offset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("GetCustomerOrders error:", err)
|
log.Println("GetCustomerOrders error:", err)
|
||||||
|
|||||||
@@ -21,6 +21,15 @@ func (ctl *PartnerController) GetActiveRiders(c *fiber.Ctx) error {
|
|||||||
aid, _ := strconv.Atoi(c.Query("applocationid"))
|
aid, _ := strconv.Atoi(c.Query("applocationid"))
|
||||||
uid, _ := strconv.Atoi(c.Query("userid"))
|
uid, _ := strconv.Atoi(c.Query("userid"))
|
||||||
tid, _ := strconv.Atoi(c.Query("tenantid"))
|
tid, _ := strconv.Atoi(c.Query("tenantid"))
|
||||||
|
|
||||||
|
if pid == 0 && aid == 0 && uid == 0 && tid == 0 {
|
||||||
|
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"status": false,
|
||||||
|
"code": http.StatusBadRequest,
|
||||||
|
"message": "At least one of tenantid, partnerid, applocationid or userid is required",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
result, err := ctl.partnerService.GetActiveRiders(pid, aid, uid, tid)
|
result, err := ctl.partnerService.GetActiveRiders(pid, aid, uid, tid)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -113,7 +122,15 @@ func (ctl *PartnerController) GetRiderLogs(c *fiber.Ctx) error {
|
|||||||
pid, _ := strconv.Atoi(c.Query("partnerid"))
|
pid, _ := strconv.Atoi(c.Query("partnerid"))
|
||||||
aid, _ := strconv.Atoi(c.Query("applocationid"))
|
aid, _ := strconv.Atoi(c.Query("applocationid"))
|
||||||
fdate := c.Query("fromdate")
|
fdate := c.Query("fromdate")
|
||||||
tdate := c.Query("fromdate")
|
tdate := c.Query("todate")
|
||||||
|
|
||||||
|
if pid == 0 && aid == 0 {
|
||||||
|
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"status": false,
|
||||||
|
"code": http.StatusBadRequest,
|
||||||
|
"message": "At least one of partnerid or applocationid is required",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
data, err := ctl.partnerService.GetRiderLogs(pid, aid, fdate, tdate)
|
data, err := ctl.partnerService.GetRiderLogs(pid, aid, fdate, tdate)
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,14 @@ func (ctl *UserController) GetAllUsers(c *fiber.Ctx) error {
|
|||||||
pagesize, _ := strconv.Atoi(c.Query("pagesize", "10"))
|
pagesize, _ := strconv.Atoi(c.Query("pagesize", "10"))
|
||||||
keyword := c.Query("keyword", "")
|
keyword := c.Query("keyword", "")
|
||||||
|
|
||||||
|
if tenantID == 0 {
|
||||||
|
return c.Status(http.StatusBadRequest).JSON(fiber.Map{
|
||||||
|
"code": http.StatusBadRequest,
|
||||||
|
"message": "tenantid is required",
|
||||||
|
"status": false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
users, err := ctl.userService.GetAllUsers(roleID, tenantID, pageno, pagesize, keyword)
|
users, err := ctl.userService.GetAllUsers(roleID, tenantID, pageno, pagesize, keyword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
|
||||||
|
|||||||
@@ -26,6 +26,11 @@ var catalogueBrandTables = map[string]string{
|
|||||||
|
|
||||||
var ErrUnknownBrand = errors.New("unknown brand")
|
var ErrUnknownBrand = errors.New("unknown brand")
|
||||||
|
|
||||||
|
// ErrCatalogueDBUnavailable is returned by every method below when the
|
||||||
|
// catalogue DB connection wasn't configured (db.CatalogueDB is nil), so
|
||||||
|
// callers get a normal error instead of a nil-pointer panic.
|
||||||
|
var ErrCatalogueDBUnavailable = errors.New("catalogue database is not configured")
|
||||||
|
|
||||||
// catalogueProductColumns casts the text[] columns to text: GORM's raw
|
// catalogueProductColumns casts the text[] columns to text: GORM's raw
|
||||||
// scan-into-struct silently drops slice-kind destination fields, so they
|
// scan-into-struct silently drops slice-kind destination fields, so they
|
||||||
// are read as text here and parsed into []string in scanProductRow.
|
// are read as text here and parsed into []string in scanProductRow.
|
||||||
@@ -108,6 +113,10 @@ func tableForBrand(brand string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *catalogueRepository) GetBrands() ([]models.CatalogueBrand, error) {
|
func (r *catalogueRepository) GetBrands() ([]models.CatalogueBrand, error) {
|
||||||
|
if r.db == nil {
|
||||||
|
return nil, ErrCatalogueDBUnavailable
|
||||||
|
}
|
||||||
|
|
||||||
var brands []models.CatalogueBrand
|
var brands []models.CatalogueBrand
|
||||||
|
|
||||||
for brand, table := range catalogueBrandTables {
|
for brand, table := range catalogueBrandTables {
|
||||||
@@ -122,6 +131,10 @@ func (r *catalogueRepository) GetBrands() ([]models.CatalogueBrand, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *catalogueRepository) GetCategories(brand string) ([]string, error) {
|
func (r *catalogueRepository) GetCategories(brand string) ([]string, error) {
|
||||||
|
if r.db == nil {
|
||||||
|
return nil, ErrCatalogueDBUnavailable
|
||||||
|
}
|
||||||
|
|
||||||
table, err := tableForBrand(brand)
|
table, err := tableForBrand(brand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -141,6 +154,10 @@ func (r *catalogueRepository) GetCategories(brand string) ([]string, error) {
|
|||||||
// everything, then let the store owner choose" — brand/category/keyword are
|
// everything, then let the store owner choose" — brand/category/keyword are
|
||||||
// optional narrowing filters on top of that, not prerequisites.
|
// optional narrowing filters on top of that, not prerequisites.
|
||||||
func (r *catalogueRepository) GetProducts(brand, category, keyword string, pageno, pagesize int) ([]models.CatalogueProduct, int64, error) {
|
func (r *catalogueRepository) GetProducts(brand, category, keyword string, pageno, pagesize int) ([]models.CatalogueProduct, int64, error) {
|
||||||
|
if r.db == nil {
|
||||||
|
return nil, 0, ErrCatalogueDBUnavailable
|
||||||
|
}
|
||||||
|
|
||||||
if pagesize <= 0 {
|
if pagesize <= 0 {
|
||||||
pagesize = 20
|
pagesize = 20
|
||||||
}
|
}
|
||||||
@@ -253,6 +270,10 @@ func catalogueWhereClause(category, keyword string) (string, []interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *catalogueRepository) GetProductBySKU(brand, sku string) (*models.CatalogueProduct, error) {
|
func (r *catalogueRepository) GetProductBySKU(brand, sku string) (*models.CatalogueProduct, error) {
|
||||||
|
if r.db == nil {
|
||||||
|
return nil, ErrCatalogueDBUnavailable
|
||||||
|
}
|
||||||
|
|
||||||
table, err := tableForBrand(brand)
|
table, err := tableForBrand(brand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -276,6 +297,10 @@ func (r *catalogueRepository) GetProductBySKU(brand, sku string) (*models.Catalo
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *catalogueRepository) GetProductByID(brand string, id int64) (*models.CatalogueProduct, error) {
|
func (r *catalogueRepository) GetProductByID(brand string, id int64) (*models.CatalogueProduct, error) {
|
||||||
|
if r.db == nil {
|
||||||
|
return nil, ErrCatalogueDBUnavailable
|
||||||
|
}
|
||||||
|
|
||||||
table, err := tableForBrand(brand)
|
table, err := tableForBrand(brand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"nearle/models"
|
"nearle/models"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -599,39 +598,47 @@ func (r *orderRepository) GetAllOrders(stat, fdate, tdate string, pageno, pagesi
|
|||||||
|
|
||||||
func (r *orderRepository) GetOrderSummary(tid, pid, cid, lid int, fdate, tdate string) ([]models.Ordersummarydaily, error) {
|
func (r *orderRepository) GetOrderSummary(tid, pid, cid, lid int, fdate, tdate string) ([]models.Ordersummarydaily, error) {
|
||||||
var data []models.Ordersummarydaily
|
var data []models.Ordersummarydaily
|
||||||
var q1 string
|
|
||||||
|
|
||||||
// Base SELECT
|
// Base SELECT
|
||||||
const base = `
|
const base = `
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(*) AS total,
|
COUNT(*) AS total,
|
||||||
SUM(CASE WHEN o.orderstatus = 'created' THEN 1 ELSE 0 END) AS created,
|
SUM(CASE WHEN o.orderstatus = 'created' THEN 1 ELSE 0 END) AS created,
|
||||||
SUM(CASE WHEN o.orderstatus = 'pending' THEN 1 ELSE 0 END) AS pending,
|
SUM(CASE WHEN o.orderstatus = 'pending' THEN 1 ELSE 0 END) AS pending,
|
||||||
SUM(CASE WHEN o.orderstatus = 'processing' THEN 1 ELSE 0 END) AS processing,
|
SUM(CASE WHEN o.orderstatus = 'processing' THEN 1 ELSE 0 END) AS processing,
|
||||||
SUM(CASE WHEN o.orderstatus = 'delivered' THEN 1 ELSE 0 END) AS delivered,
|
SUM(CASE WHEN o.orderstatus = 'delivered' THEN 1 ELSE 0 END) AS delivered,
|
||||||
SUM(CASE WHEN o.orderstatus = 'cancelled' THEN 1 ELSE 0 END) AS cancelled,
|
SUM(CASE WHEN o.orderstatus = 'cancelled' THEN 1 ELSE 0 END) AS cancelled,
|
||||||
t.tenantid,
|
t.tenantid,
|
||||||
t.tenantname
|
t.tenantname
|
||||||
FROM orders o
|
FROM orders o
|
||||||
INNER JOIN tenants t ON o.tenantid = t.tenantid
|
INNER JOIN tenants t ON o.tenantid = t.tenantid
|
||||||
`
|
`
|
||||||
|
|
||||||
// Apply filters
|
var params []interface{}
|
||||||
if tid != 0 {
|
var q1 string
|
||||||
q1 = base + " WHERE o.configid = 1 AND o.tenantid = " + strconv.Itoa(tid)
|
|
||||||
} else if pid != 0 {
|
// Apply filters (at least one scoping id is required — enforced by the caller)
|
||||||
q1 = base + " WHERE o.configid = 1 AND o.partnerid = " + strconv.Itoa(pid)
|
switch {
|
||||||
} else if cid != 0 {
|
case tid != 0:
|
||||||
q1 = base + " WHERE o.configid = 1 AND o.customerid = " + strconv.Itoa(cid)
|
q1 = base + " WHERE o.configid = 1 AND o.tenantid = ?"
|
||||||
} else if lid != 0 {
|
params = append(params, tid)
|
||||||
q1 = base + " WHERE o.configid = 1 AND o.locationid = " + strconv.Itoa(lid)
|
case pid != 0:
|
||||||
} else {
|
q1 = base + " WHERE o.configid = 1 AND o.partnerid = ?"
|
||||||
q1 = base + " WHERE o.configid = 1"
|
params = append(params, pid)
|
||||||
|
case cid != 0:
|
||||||
|
q1 = base + " WHERE o.configid = 1 AND o.customerid = ?"
|
||||||
|
params = append(params, cid)
|
||||||
|
case lid != 0:
|
||||||
|
q1 = base + " WHERE o.configid = 1 AND o.locationid = ?"
|
||||||
|
params = append(params, lid)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("at least one of tenantid, partnerid, customerid or locationid is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Date filter
|
// Date filter
|
||||||
if fdate != "" && tdate != "" {
|
if fdate != "" && tdate != "" {
|
||||||
q1 += " AND o.orderdate::date BETWEEN '" + fdate + "' AND '" + tdate + "'"
|
q1 += " AND o.orderdate::date BETWEEN ? AND ?"
|
||||||
|
params = append(params, fdate, tdate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group by tenant
|
// Group by tenant
|
||||||
@@ -640,7 +647,7 @@ func (r *orderRepository) GetOrderSummary(tid, pid, cid, lid int, fdate, tdate s
|
|||||||
// Debug
|
// Debug
|
||||||
fmt.Println("Executing GetOrderSummary query:", q1)
|
fmt.Println("Executing GetOrderSummary query:", q1)
|
||||||
|
|
||||||
if err := r.db.Raw(q1).Scan(&data).Error; err != nil {
|
if err := r.db.Raw(q1, params...).Scan(&data).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user