diff --git a/internal/common/repository/common.hppv2.repository.go b/internal/common/repository/common.hppv2.repository.go index beedd7ce..dfd9c29f 100644 --- a/internal/common/repository/common.hppv2.repository.go +++ b/internal/common/repository/common.hppv2.repository.go @@ -878,6 +878,25 @@ func (r *HppV2RepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKanda return 0, 0, err } + var adjustmentTotals struct { + TotalQty float64 + TotalWeight float64 + } + err = r.db.WithContext(ctx). + Table("adjustment_stocks AS ast"). + Select("COALESCE(SUM(ast.total_qty), 0) AS total_qty, COALESCE(SUM(ast.price), 0) AS total_weight"). + Joins("JOIN product_warehouses AS pw ON pw.id = ast.product_warehouse_id"). + Where("pw.project_flock_kandang_id IN (?)", projectFlockKandangIDs). + Where("ast.function_code = ?", string(utils.AdjustmentTransactionSubtypeRecordingEggIn)). + Where("ast.created_at <= ?", *date). + Scan(&adjustmentTotals).Error + if err != nil { + return 0, 0, err + } + + totals.TotalPieces += adjustmentTotals.TotalQty + totals.TotalWeightKg += adjustmentTotals.TotalWeight + return totals.TotalPieces, totals.TotalWeightKg, nil }