[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")