Fixed GetProducts
This commit is contained in:
@@ -685,10 +685,29 @@ func (r *productRepository) GetProducts(params models.ProductFilter) ([]models.P
|
||||
)
|
||||
}
|
||||
|
||||
// productstock/quantity are correlated subqueries computing the live
|
||||
// SUM(in)-SUM(out) balance from productstocks, scoped to params.LocationID
|
||||
// (0 if the caller didn't scope to a store, matching nothing so both come
|
||||
// back zero). quantity is placed after `a.*` so it overwrites the static,
|
||||
// never-decremented products.quantity column — same fix as
|
||||
// GetLocationProducts/GetProductByVariant, otherwise this endpoint would
|
||||
// keep showing stock that never reduces after an order.
|
||||
err := q.Select(`
|
||||
a.*,
|
||||
COALESCE(pd.discountvalue, 0) AS discountvalue
|
||||
`).Find(&products).Error
|
||||
COALESCE(pd.discountvalue, 0) AS discountvalue,
|
||||
COALESCE((
|
||||
SELECT SUM(CASE WHEN LOWER(ps.stocktype) = 'in' THEN ps.quantity ELSE 0 END) -
|
||||
SUM(CASE WHEN LOWER(ps.stocktype) = 'out' THEN ps.quantity ELSE 0 END)
|
||||
FROM productstocks ps
|
||||
WHERE ps.productid = a.productid AND ps.tenantid = a.tenantid AND ps.locationid = ?
|
||||
), 0) AS productstock,
|
||||
COALESCE((
|
||||
SELECT SUM(CASE WHEN LOWER(ps.stocktype) = 'in' THEN ps.quantity ELSE 0 END) -
|
||||
SUM(CASE WHEN LOWER(ps.stocktype) = 'out' THEN ps.quantity ELSE 0 END)
|
||||
FROM productstocks ps
|
||||
WHERE ps.productid = a.productid AND ps.tenantid = a.tenantid AND ps.locationid = ?
|
||||
), 0) AS quantity
|
||||
`, params.LocationID, params.LocationID).Find(&products).Error
|
||||
|
||||
return products, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user