mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
codex: initiated changes
This commit is contained in:
@@ -31,44 +31,65 @@ func MapDepletions(recordingID uint, items []validation.Depletion) []entity.Reco
|
||||
return nil
|
||||
}
|
||||
|
||||
aggregate := make(map[uint]float64, len(items))
|
||||
type depletionKey struct {
|
||||
ProductWarehouseID uint
|
||||
SourceProductWarehouseID uint
|
||||
}
|
||||
|
||||
aggregate := make(map[depletionKey]float64, len(items))
|
||||
for _, item := range items {
|
||||
if item.ProductWarehouseId == 0 || item.Qty == 0 {
|
||||
continue
|
||||
}
|
||||
aggregate[item.ProductWarehouseId] += item.Qty
|
||||
key := depletionKey{ProductWarehouseID: item.ProductWarehouseId}
|
||||
if item.SourceProductWarehouseId != nil {
|
||||
key.SourceProductWarehouseID = *item.SourceProductWarehouseId
|
||||
}
|
||||
aggregate[key] += item.Qty
|
||||
}
|
||||
if len(aggregate) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make([]entity.RecordingDepletion, 0, len(aggregate))
|
||||
for warehouseID, qty := range aggregate {
|
||||
for key, qty := range aggregate {
|
||||
if qty == 0 {
|
||||
continue
|
||||
}
|
||||
var sourceWarehouseID *uint
|
||||
if key.SourceProductWarehouseID != 0 {
|
||||
sourceWarehouseID = new(uint)
|
||||
*sourceWarehouseID = key.SourceProductWarehouseID
|
||||
}
|
||||
result = append(result, entity.RecordingDepletion{
|
||||
RecordingId: recordingID,
|
||||
ProductWarehouseId: warehouseID,
|
||||
Qty: qty,
|
||||
RecordingId: recordingID,
|
||||
ProductWarehouseId: key.ProductWarehouseID,
|
||||
SourceProductWarehouseId: sourceWarehouseID,
|
||||
Qty: qty,
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func MapEggs(recordingID uint, createdBy uint, items []validation.Egg) []entity.RecordingEgg {
|
||||
func MapEggs(recordingID uint, projectFlockKandangID uint, createdBy uint, items []validation.Egg) []entity.RecordingEgg {
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make([]entity.RecordingEgg, 0, len(items))
|
||||
for _, item := range items {
|
||||
var sourceProjectFlockKandangID *uint
|
||||
if projectFlockKandangID != 0 {
|
||||
sourceProjectFlockKandangID = new(uint)
|
||||
*sourceProjectFlockKandangID = projectFlockKandangID
|
||||
}
|
||||
result = append(result, entity.RecordingEgg{
|
||||
RecordingId: recordingID,
|
||||
ProductWarehouseId: item.ProductWarehouseId,
|
||||
Qty: item.Qty,
|
||||
Weight: item.Weight,
|
||||
CreatedBy: createdBy,
|
||||
RecordingId: recordingID,
|
||||
ProductWarehouseId: item.ProductWarehouseId,
|
||||
ProjectFlockKandangId: sourceProjectFlockKandangID,
|
||||
Qty: item.Qty,
|
||||
Weight: item.Weight,
|
||||
CreatedBy: createdBy,
|
||||
})
|
||||
}
|
||||
return result
|
||||
@@ -79,6 +100,11 @@ type EggTotals struct {
|
||||
Weight float64
|
||||
}
|
||||
|
||||
type DepletionRoute struct {
|
||||
ProductWarehouseId uint
|
||||
SourceProductWarehouseId uint
|
||||
}
|
||||
|
||||
func StockUsageByWarehouse(items []entity.RecordingStock) map[uint]float64 {
|
||||
return TotalsByWarehouse(items, func(stock entity.RecordingStock) (uint, float64) {
|
||||
var usage float64
|
||||
@@ -121,6 +147,19 @@ func EggTotalsEqual(a, b map[uint]EggTotals) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func DepletionRouteMapsEqual(a, b map[DepletionRoute]float64) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for key, value := range a {
|
||||
other, ok := b[key]
|
||||
if !ok || !floatNearlyEqual(value, other) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func floatNearlyEqual(a, b float64) bool {
|
||||
return a-b <= 0.000001 && b-a <= 0.000001
|
||||
}
|
||||
@@ -134,6 +173,19 @@ func TotalsByWarehouse[T any](items []T, get func(T) (uint, float64)) map[uint]f
|
||||
return result
|
||||
}
|
||||
|
||||
func DepletionTotalsByRoute[T any](items []T, get func(T) (uint, *uint, float64)) map[DepletionRoute]float64 {
|
||||
result := make(map[DepletionRoute]float64)
|
||||
for _, item := range items {
|
||||
productWarehouseID, sourceProductWarehouseID, qty := get(item)
|
||||
key := DepletionRoute{ProductWarehouseId: productWarehouseID}
|
||||
if sourceProductWarehouseID != nil {
|
||||
key.SourceProductWarehouseId = *sourceProductWarehouseID
|
||||
}
|
||||
result[key] += qty
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func EggTotalsByWarehouse[T any](items []T, get func(T) (uint, int, *float64)) map[uint]EggTotals {
|
||||
result := make(map[uint]EggTotals)
|
||||
for _, item := range items {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package recording
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/validations"
|
||||
)
|
||||
|
||||
func TestMapDepletionsKeepsSourceWarehouseRoutes(t *testing.T) {
|
||||
sourceA := uint(11)
|
||||
sourceB := uint(12)
|
||||
|
||||
got := MapDepletions(99, []validation.Depletion{
|
||||
{ProductWarehouseId: 21, SourceProductWarehouseId: &sourceA, Qty: 3},
|
||||
{ProductWarehouseId: 21, SourceProductWarehouseId: &sourceA, Qty: 2},
|
||||
{ProductWarehouseId: 21, SourceProductWarehouseId: &sourceB, Qty: 4},
|
||||
})
|
||||
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("expected 2 depletion routes, got %d", len(got))
|
||||
}
|
||||
|
||||
routeQty := DepletionTotalsByRoute(got, func(item entity.RecordingDepletion) (uint, *uint, float64) {
|
||||
return item.ProductWarehouseId, item.SourceProductWarehouseId, item.Qty
|
||||
})
|
||||
|
||||
if routeQty[DepletionRoute{ProductWarehouseId: 21, SourceProductWarehouseId: sourceA}] != 5 {
|
||||
t.Fatalf("expected source A qty 5, got %.2f", routeQty[DepletionRoute{ProductWarehouseId: 21, SourceProductWarehouseId: sourceA}])
|
||||
}
|
||||
if routeQty[DepletionRoute{ProductWarehouseId: 21, SourceProductWarehouseId: sourceB}] != 4 {
|
||||
t.Fatalf("expected source B qty 4, got %.2f", routeQty[DepletionRoute{ProductWarehouseId: 21, SourceProductWarehouseId: sourceB}])
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEggsSetsProjectFlockKandangID(t *testing.T) {
|
||||
got := MapEggs(77, 44, 9, []validation.Egg{
|
||||
{ProductWarehouseId: 88, Qty: 10},
|
||||
})
|
||||
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("expected 1 egg row, got %d", len(got))
|
||||
}
|
||||
if got[0].ProjectFlockKandangId == nil || *got[0].ProjectFlockKandangId != 44 {
|
||||
t.Fatalf("expected project flock kandang id 44, got %+v", got[0].ProjectFlockKandangId)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user