changes with app variant

This commit is contained in:
2026-08-28 13:02:03 +05:30
parent aef105979a
commit c31f3aca27
4 changed files with 165 additions and 15 deletions

View File

@@ -32,7 +32,7 @@ type ProductRepository interface {
GetLocationProductSummary(tenantID, locationID int) ([]models.ProductSummary, error)
GetSaleTemplate(tenantID, locationID int) (*models.SaleTemplate, error)
FetchFilteredProducts(categoryID, subcategoryID, productID, applocationID, tenantID, locationID int, keyword, productStatus, approve string, pageno, pagesize int) ([]models.Tenantproducts, error)
GetProductByVariant(tenantid, variantid, locationid int) ([]models.Products, error)
GetProductByVariant(tenantid, variantid, locationid, productid int) ([]models.Products, error)
GetSubcategories(categoryID int) ([]models.Subcategory, error)
GetProducts(params models.ProductFilter) ([]models.Products, error)
GetTenantInfo(tenantID, applocationID int) (map[string]interface{}, error)
@@ -842,10 +842,37 @@ func (r *productRepository) FetchFilteredProducts(
return results, nil
}
func (r *productRepository) GetProductByVariant(tenantid, variantid, locationid int) ([]models.Products, error) {
// GetProductByVariant returns what the app can offer for one tapped product.
//
// Two shapes, decided by the product rather than by the caller: a product that
// belongs to a variant group comes back with ALL its siblings, so the shopper
// picks a size; a product in no group comes back alone, ready to order. That is
// what the ordering screen needs, and until now only the first half existed.
//
// The query keyed solely on `p.variants = ?`, so an ungrouped product — which
// means `variants = 0`, and that is EVERY product for most tenants — could not
// be fetched at all. Asking for one returned an empty list and the app had
// nothing to place an order against. `productid` is the way in: given one, this
// reads that product's own group and answers accordingly.
//
// An explicit `variantid` still wins, so existing callers are unaffected.
func (r *productRepository) GetProductByVariant(tenantid, variantid, locationid, productid int) ([]models.Products, error) {
var data []models.Products
// Which group to return, resolved from the product when not named outright.
group := variantid
if group <= 0 && productid > 0 {
var found struct{ Variants int }
if err := r.db.Table("products").
Select("variants").
Where("productid = ? AND tenantid = ?", productid, tenantid).
Scan(&found).Error; err != nil {
return nil, err
}
group = found.Variants
}
// productstock/quantity are correlated subqueries (not a JOIN+GROUP BY) so
// they can coexist with `p.*` without having to enumerate every products
// column. quantity is duplicated on purpose: it's placed after `p.*` so it
@@ -855,7 +882,7 @@ func (r *productRepository) GetProductByVariant(tenantid, variantid, locationid
// productlocations join simply match nothing, so
// Productstock/Quantity/Locationstatus come back zero-valued — same
// response shape as before this field existed, not an error.
err := r.db.
q := r.db.
Table("products p").
Select(`
p.*,
@@ -864,6 +891,11 @@ func (r *productRepository) GetProductByVariant(tenantid, variantid, locationid
COALESCE(pd.discountvalue, 0) AS discountvalue,
pd.discountid,
pl.status AS locationstatus,
COALESCE(NULLIF((
SELECT pl2.price FROM productlocations pl2
WHERE pl2.productid = p.productid AND pl2.tenantid = p.tenantid AND pl2.locationid = ?
LIMIT 1
), 0), p.retailprice, 0) AS price,
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)
@@ -876,12 +908,25 @@ func (r *productRepository) GetProductByVariant(tenantid, variantid, locationid
FROM productstocks ps
WHERE ps.productid = p.productid AND ps.tenantid = p.tenantid AND ps.locationid = ?
), 0) AS quantity
`, locationid, locationid).
`, locationid, locationid, locationid).
Joins("LEFT JOIN productcategories c ON p.categoryid = c.categoryid").
Joins("LEFT JOIN productsubcategories d ON p.subcategoryid = d.subcatid").
Joins("LEFT JOIN productdiscounts pd ON pd.productid = p.productid").
Joins("LEFT JOIN productlocations pl ON pl.productid = p.productid AND pl.tenantid = p.tenantid AND pl.locationid = ?", locationid).
Where("p.tenantid = ? AND p.variants = ?", tenantid, variantid).
Joins("LEFT JOIN productlocations pl ON pl.productid = p.productid AND pl.tenantid = p.tenantid AND pl.locationid = ?", locationid)
// A group returns the whole family; no group returns the one product.
// Neither given keeps the original behaviour exactly, so nothing that
// called this before sees a different answer.
switch {
case group > 0:
q = q.Where("p.tenantid = ? AND p.variants = ?", tenantid, group)
case productid > 0:
q = q.Where("p.tenantid = ? AND p.productid = ?", tenantid, productid)
default:
q = q.Where("p.tenantid = ? AND p.variants = ?", tenantid, variantid)
}
err := q.
Order("p.productid DESC").
Scan(&data).Error