variants as a single product
This commit is contained in:
@@ -2,6 +2,7 @@ package repositories
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"nearle/models"
|
||||
|
||||
@@ -158,3 +159,54 @@ func (r *productRepository) VariantsForProducts(tenantid, locationid int, produc
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// variantLabelFor names the product's own size for the picker.
|
||||
//
|
||||
// The unit if it has one — "500g" is what a shopper is choosing between. A
|
||||
// product with no unit recorded falls back to its name rather than to an empty
|
||||
// label, because a blank option in a size picker is unpickable.
|
||||
func variantLabelFor(p models.Products) string {
|
||||
unit := strings.TrimSpace(strings.TrimSpace(p.Unitvalue) + " " + strings.TrimSpace(p.Productunit))
|
||||
if unit != "" {
|
||||
return unit
|
||||
}
|
||||
return p.Productname
|
||||
}
|
||||
|
||||
// VariantChildIDs is every product that is a size OF something else.
|
||||
//
|
||||
// These are the rows the customer app must not list on their own. A shop that
|
||||
// stocks Aachi Baby Fryums in 100g, 500g and 1kg has three product rows, and a
|
||||
// shopper should meet one product with three sizes — not three products that
|
||||
// happen to share a name and differ by a suffix they have to read carefully.
|
||||
//
|
||||
// One query for the whole tenant rather than per product: the alternative is a
|
||||
// lookup inside the loop that builds the response, which is the classic N+1 on
|
||||
// a listing that can run to hundreds of rows.
|
||||
//
|
||||
// A product that is a size of something is NOT deleted or hidden from the
|
||||
// merchant — the console still lists all three, because each has its own price,
|
||||
// its own barcode and its own shelf to restock.
|
||||
func (r *productRepository) VariantChildIDs(tenantid int) (map[int]bool, error) {
|
||||
out := map[int]bool{}
|
||||
if tenantid <= 0 {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
var ids []int
|
||||
err := r.db.Table("productvariants").
|
||||
Where("tenantid = ?", tenantid).
|
||||
Where("LOWER(COALESCE(status, 'active')) <> 'inactive'").
|
||||
// A row that is its own parent would remove the group's only visible
|
||||
// product. It should not exist — AddProductVariant refuses it — but a
|
||||
// listing is the wrong place to discover that it does.
|
||||
Where("variantproductid <> productid").
|
||||
Pluck("variantproductid", &ids).Error
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
for _, id := range ids {
|
||||
out[id] = true
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user