variant and price

This commit is contained in:
2026-08-28 16:54:55 +05:30
parent 6c1ad472f2
commit 40500f936a
3 changed files with 101 additions and 21 deletions

View File

@@ -748,6 +748,21 @@ func (r *productRepository) FetchFilteredProducts(
// dropped from the balance.
// locationID 0 means "not scoped to an outlet": stock is then the tenant's
// total across outlets, which is what an unscoped listing should show.
//
// `price` is computed here for the same reason it is computed in GetProducts
// and GetProductByVariant, and its absence was the same bug all three times.
// `a.*` carries `products.price`, which nothing ever writes — the selling
// price lives in `productlocations.price`, set per outlet when the admin
// prices a product, with `products.retailprice` as the tenant-wide fallback.
// Without this the endpoint returned price 0 for every row while
// retailprice held the real figure, so an app reading `price` showed nothing
// at all. Measured on tenant 1135: price 0 / retailprice 50 on this
// endpoint, against price 50 on the other three.
//
// A correlated subquery rather than a read off the joined `b`, because that
// derived table takes MAX(locationid) to collapse duplicates and would hand
// back an arbitrary outlet's price whenever the caller did not scope to one.
// Ordering by locationid at least makes the unscoped answer deterministic.
query := r.db.
Table("products a").
Select(`
@@ -756,9 +771,16 @@ func (r *productRepository) FetchFilteredProducts(
b.locationid,
c.categoryname,
d.subcatname AS subcategoryname,
COALESCE(NULLIF((
SELECT pl2.price FROM productlocations pl2
WHERE pl2.productid = a.productid AND pl2.tenantid = a.tenantid
AND (? = 0 OR pl2.locationid = ?)
ORDER BY pl2.locationid
LIMIT 1
), 0), a.retailprice, 0) AS price,
COALESCE(ps.quantity, 0) AS productstock,
COALESCE(ps.quantity, 0) AS quantity
`).
`, locationID, locationID).
Joins(`
LEFT JOIN (
SELECT productid, tenantid,