Merge branch 'Fix/BE/recording_avaible_qty' into 'development'

[FEAT/BE]Fix create avaible qty

See merge request mbugroup/lti-api!300
This commit is contained in:
Hafizh A. Y.
2026-02-03 04:18:06 +00:00
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 var remainingChick float64
if totalChick > 0 { if totalChick > 0 {
totalChickFloat := float64(totalChick) totalChickFloat := float64(totalChick)
remainingChick = totalChickFloat - cumDepletionQty if s.FifoSvc != nil {
if remainingChick < 0 { // totalChick already represents available qty (total_qty - total_used_qty).
remainingChick = 0 remainingChick = totalChickFloat
} updates["total_chick_qty"] = remainingChick
updates["total_chick_qty"] = remainingChick recording.TotalChickQty = &remainingChick
recording.TotalChickQty = &remainingChick
cumRate := 0.0 baseChick := initialChickin
if totalChickFloat > 0 { if baseChick <= 0 {
cumRate = (cumDepletionQty / totalChickFloat) * 100 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 { } else {
updates["total_chick_qty"] = gorm.Expr("NULL") updates["total_chick_qty"] = gorm.Expr("NULL")
updates["cum_depletion_rate"] = 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 return nil
} }
result := make([]entity.RecordingDepletion, 0, len(items)) aggregate := make(map[uint]float64, len(items))
for _, item := range 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{ result = append(result, entity.RecordingDepletion{
RecordingId: recordingID, RecordingId: recordingID,
ProductWarehouseId: item.ProductWarehouseId, ProductWarehouseId: warehouseID,
Qty: item.Qty, Qty: qty,
}) })
} }
return result return result