[FEAT/BE]Fix create avaible qty

This commit is contained in:
ragilap
2026-02-03 11:17:11 +07:00
parent d2ce0918b5
commit 82f0db107a
2 changed files with 46 additions and 14 deletions
@@ -1073,19 +1073,37 @@ func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm
var remainingChick float64
if totalChick > 0 {
totalChickFloat := float64(totalChick)
remainingChick = totalChickFloat - cumDepletionQty
if remainingChick < 0 {
remainingChick = 0
}
updates["total_chick_qty"] = remainingChick
recording.TotalChickQty = &remainingChick
if s.FifoSvc != nil {
// totalChick already represents available qty (total_qty - total_used_qty).
remainingChick = totalChickFloat
updates["total_chick_qty"] = remainingChick
recording.TotalChickQty = &remainingChick
cumRate := 0.0
if totalChickFloat > 0 {
cumRate = (cumDepletionQty / totalChickFloat) * 100
baseChick := initialChickin
if baseChick <= 0 {
baseChick = totalChickFloat + cumDepletionQty
}
cumRate := 0.0
if baseChick > 0 {
cumRate = (cumDepletionQty / baseChick) * 100
}
updates["cum_depletion_rate"] = cumRate
recording.CumDepletionRate = &cumRate
} else {
remainingChick = totalChickFloat - cumDepletionQty
if remainingChick < 0 {
remainingChick = 0
}
updates["total_chick_qty"] = remainingChick
recording.TotalChickQty = &remainingChick
cumRate := 0.0
if totalChickFloat > 0 {
cumRate = (cumDepletionQty / totalChickFloat) * 100
}
updates["cum_depletion_rate"] = cumRate
recording.CumDepletionRate = &cumRate
}
updates["cum_depletion_rate"] = cumRate
recording.CumDepletionRate = &cumRate
} else {
updates["total_chick_qty"] = gorm.Expr("NULL")
updates["cum_depletion_rate"] = gorm.Expr("NULL")
+17 -3
View File
@@ -28,12 +28,26 @@ func MapDepletions(recordingID uint, items []validation.Depletion) []entity.Reco
return nil
}
result := make([]entity.RecordingDepletion, 0, len(items))
aggregate := make(map[uint]float64, len(items))
for _, item := range items {
if item.ProductWarehouseId == 0 || item.Qty == 0 {
continue
}
aggregate[item.ProductWarehouseId] += item.Qty
}
if len(aggregate) == 0 {
return nil
}
result := make([]entity.RecordingDepletion, 0, len(aggregate))
for warehouseID, qty := range aggregate {
if qty == 0 {
continue
}
result = append(result, entity.RecordingDepletion{
RecordingId: recordingID,
ProductWarehouseId: item.ProductWarehouseId,
Qty: item.Qty,
ProductWarehouseId: warehouseID,
Qty: qty,
})
}
return result