variants as a single product

This commit is contained in:
2026-09-03 10:43:05 +05:30
parent 2401158190
commit da0e9d987e
7 changed files with 205 additions and 4 deletions

View File

@@ -59,6 +59,7 @@ type ProductRepository interface {
AddProductVariant(v models.Productvariant) (models.Productvariant, error)
RemoveProductVariant(tenantid, variantid int) error
VariantsForProducts(tenantid, locationid int, productids []int) (map[int][]models.Productvariant, error)
VariantChildIDs(tenantid int) (map[int]bool, error)
}
type productRepository struct {
@@ -1042,12 +1043,40 @@ func (r *productRepository) GetProductByVariant(tenantid, variantid, locationid,
}
if byParent, vErr := r.VariantsForProducts(tenantid, locationid, ids); vErr == nil {
for i := range data {
data[i].Variantoptions = byParent[data[i].Productid]
if data[i].Variantoptions == nil {
children := byParent[data[i].Productid]
if len(children) == 0 {
// An explicit empty list, never a JSON null: a client that does
// `variantoptions.length` must not have to null-check first.
// `variantoptions.length` must not have to null-check first, and
// an empty list is the complete answer “order this one directly”.
data[i].Variantoptions = []models.Productvariant{}
continue
}
// The product being looked at is one of its own sizes.
//
// Without this the picker on “Aachi Baby Fryums 100g” offers 500g and
// 1kg and not the 100g the shopper is already reading — so the size
// they came for is the one option they cannot choose, and there is no
// way back to it once they move off. Every screen in the group now
// shows the same complete list.
//
// Built from the product row already in hand rather than by another
// query: it carries the effective price and the live balance this
// query just computed, so the entry cannot disagree with the row it
// came from.
self := models.Productvariant{
Tenantid: data[i].Tenantid,
Productid: data[i].Productid,
Variantproductid: data[i].Productid,
Variantname: variantLabelFor(data[i]),
Status: "Active",
Variantproductname: data[i].Productname,
Variantprice: data[i].Price,
Variantstock: data[i].Productstock,
Variantunitvalue: data[i].Unitvalue,
Variantproductunit: data[i].Productunit,
}
data[i].Variantoptions = append([]models.Productvariant{self}, children...)
}
}