warehouse: switch stock pricing to weighted_avg

This commit is contained in:
Mikhail Chusavitin
2026-02-18 19:45:32 +03:00
parent 9eb573ca90
commit b94dd3d015
3 changed files with 29 additions and 6 deletions

View File

@@ -66,7 +66,7 @@ func TestCreateWarehousePricelistFromStockLog(t *testing.T) {
if items[0].LotName != "CPU_X" {
t.Fatalf("unexpected lot name: %s", items[0].LotName)
}
if math.Abs(items[0].Price-200) > 0.001 {
t.Fatalf("expected weighted median price 200, got %f", items[0].Price)
if math.Abs(items[0].Price-180) > 0.001 {
t.Fatalf("expected weighted average price 180, got %f", items[0].Price)
}
}

View File

@@ -58,11 +58,11 @@ func ComputePricelistItemsFromStockLog(db *gorm.DB) ([]SnapshotItem, error) {
items := make([]SnapshotItem, 0, len(grouped))
for lot, values := range grouped {
price := weightedMedian(values)
price := weightedAverage(values)
if price <= 0 {
continue
}
items = append(items, SnapshotItem{LotName: lot, Price: price, PriceMethod: "weighted_median", Category: ""})
items = append(items, SnapshotItem{LotName: lot, Price: price, PriceMethod: "weighted_avg", Category: ""})
}
// Load categories for all lots in a single query
@@ -272,3 +272,26 @@ func median(values []float64) float64 {
}
return cp[n/2]
}
func weightedAverage(values []weightedPricePoint) float64 {
if len(values) == 0 {
return 0
}
sumWeighted := 0.0
sumWeight := 0.0
prices := make([]float64, 0, len(values))
for _, v := range values {
if v.price <= 0 {
continue
}
prices = append(prices, v.price)
if v.weight > 0 {
sumWeighted += v.price * v.weight
sumWeight += v.weight
}
}
if sumWeight > 0 {
return sumWeighted / sumWeight
}
return median(prices)
}

View File

@@ -45,8 +45,8 @@ func TestComputePricelistItemsFromStockLog(t *testing.T) {
if items[0].LotName != "CPU_X" {
t.Fatalf("expected lot CPU_X, got %s", items[0].LotName)
}
if math.Abs(items[0].Price-200) > 0.001 {
t.Fatalf("expected weighted median 200, got %f", items[0].Price)
if math.Abs(items[0].Price-190) > 0.001 {
t.Fatalf("expected weighted average 190, got %f", items[0].Price)
}
}