fix calculate egg mass and hen house recordings

This commit is contained in:
giovanni
2026-04-10 17:16:28 +07:00
parent 3eb225cca8
commit b79738dbe1
3 changed files with 410 additions and 6 deletions
@@ -758,15 +758,39 @@ func (r *RecordingRepositoryImpl) GetCumulativeEggQtyByProjectFlockKandang(
return 0, nil
}
var result float64
var cumulativeEggQty float64
err := tx.
Table("recording_eggs").
Select("COALESCE(SUM(recording_eggs.qty), 0)").
Joins("JOIN recordings ON recordings.id = recording_eggs.recording_id").
Where("recordings.project_flock_kandangs_id = ?", projectFlockKandangId).
Where("recordings.record_datetime <= ?", recordTime).
Scan(&result).Error
return result, err
Scan(&cumulativeEggQty).Error
if err != nil {
return 0, err
}
productWarehouseSubQuery := tx.
Table("recording_eggs").
Select("DISTINCT recording_eggs.product_warehouse_id").
Joins("JOIN recordings ON recordings.id = recording_eggs.recording_id").
Where("recordings.project_flock_kandangs_id = ?", projectFlockKandangId).
Where("recordings.record_datetime <= ?", recordTime)
var adjustmentEggQty float64
err = tx.
Table("adjustment_stocks").
Select("COALESCE(SUM(adjustment_stocks.total_qty), 0)").
Where("adjustment_stocks.product_warehouse_id IN (?)", productWarehouseSubQuery).
Where("adjustment_stocks.function_code = ?", "RECORDING_EGG_IN").
Where("adjustment_stocks.transaction_type = ?", "RECORDING").
Where("adjustment_stocks.created_at <= ?", recordTime).
Scan(&adjustmentEggQty).Error
if err != nil {
return 0, err
}
return cumulativeEggQty + adjustmentEggQty, nil
}
func (r *RecordingRepositoryImpl) GetProductionWeightAndQtyByProjectFlockID(ctx context.Context, projectFlockID uint) (totalWeight float64, totalQty float64, err error) {
// Body-weight tracking is removed; keep stub for report compatibility.
@@ -1989,9 +1989,9 @@ func (s *recordingService) computeAndUpdateMetrics(ctx context.Context, tx *gorm
}
var eggMass float64
if remainingChick > 0 && totalEggWeightGrams > 0 {
// totalEggWeightGrams is in grams; egg mass is grams per hen.
eggMass = totalEggWeightGrams / remainingChick
if initialChickin > 0 && totalEggWeightGrams > 0 {
// totalEggWeightGrams is in grams; egg mass uses initial chick population.
eggMass = totalEggWeightGrams / initialChickin
updates["egg_mass"] = eggMass
recording.EggMass = &eggMass
} else {