From 0d17f46fcf14bc8b72c6315de88bfa6bdf77a09d Mon Sep 17 00:00:00 2001 From: abhishek Date: Thu, 3 Sep 2026 15:44:09 +0530 Subject: [PATCH] pricing in app --- repositories/productRepository.go | 14 +++++++++----- repositories/productVariantLink.go | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/repositories/productRepository.go b/repositories/productRepository.go index edc6f98..014b8ae 100644 --- a/repositories/productRepository.go +++ b/repositories/productRepository.go @@ -1065,11 +1065,15 @@ func (r *productRepository) GetProductByVariant(tenantid, variantid, locationid, // 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", + Tenantid: data[i].Tenantid, + Productid: data[i].Productid, + Variantproductid: data[i].Productid, + Variantname: variantLabelFor(data[i]), + Status: "Active", + // The parent is one of its own sizes, so its entry has to carry + // money in the same field the others do. Without this the first + // option in every picker read 0 while the rest were priced. + Price: data[i].Price, Variantproductname: data[i].Productname, Variantprice: data[i].Price, Variantstock: data[i].Productstock, diff --git a/repositories/productVariantLink.go b/repositories/productVariantLink.go index a467e21..1b0496f 100644 --- a/repositories/productVariantLink.go +++ b/repositories/productVariantLink.go @@ -118,6 +118,17 @@ func (r *productRepository) VariantsForProducts(tenantid, locationid int, produc return out, nil } + // `price` is emitted as the EFFECTIVE price, not the raw override. + // + // The column holds an override where 0 means "no override", so a client + // reading the obvious field name got 0 for every size while the real figure + // sat in `variantprice` beside it. Measured on R mart: a grouped product + // whose three sizes are priced 50, 250 and 500 was emitting price 0 on all + // three, so the app showed them as free. + // + // Selected after `v.*` so it overwrites the raw column on the way out — the + // same trick GetProductByVariant uses for `quantity`. The stored override is + // untouched; only what is emitted changes. var rows []models.Productvariant err := r.db. Table("productvariants v"). @@ -126,6 +137,11 @@ func (r *productRepository) VariantsForProducts(tenantid, locationid int, produc p.productname AS variantproductname, COALESCE(p.unitvalue, '') AS variantunitvalue, COALESCE(p.productunit, '') AS variantproductunit, + COALESCE(NULLIF(v.price, 0), NULLIF(( + SELECT pl.price FROM productlocations pl + WHERE pl.productid = v.variantproductid AND pl.tenantid = v.tenantid AND pl.locationid = ? + LIMIT 1 + ), 0), p.retailprice, 0) AS price, -- The variant’s own override wins, then the outlet’s price, then the -- master price. The override was being ignored entirely: a size -- priced differently from its product was sold at the product’s @@ -142,7 +158,7 @@ func (r *productRepository) VariantsForProducts(tenantid, locationid int, produc FROM productstocks ps WHERE ps.productid = v.variantproductid AND ps.tenantid = v.tenantid AND ps.locationid = ? ), 0) AS variantstock - `, locationid, locationid). + `, locationid, locationid, locationid). // INNER, and that is the intended filter: a variant whose product was // deleted is not a size a shopper can be offered. Joins("JOIN products p ON p.productid = v.variantproductid AND p.tenantid = v.tenantid").