From 8389bd579bbd8bf5fd323e863945b67c209b197f Mon Sep 17 00:00:00 2001 From: Adnan Zahir Date: Fri, 24 Apr 2026 03:10:24 +0700 Subject: [PATCH] fix: revert removal of adjustment egg weight --- .../repository/common.hppv2.repository.go | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/internal/common/repository/common.hppv2.repository.go b/internal/common/repository/common.hppv2.repository.go index beedd7ce..0bc03edb 100644 --- a/internal/common/repository/common.hppv2.repository.go +++ b/internal/common/repository/common.hppv2.repository.go @@ -878,6 +878,37 @@ func (r *HppV2RepositoryImpl) GetEggProduksiPiecesAndWeightKgByProjectFlockKanda return 0, 0, err } + var adjustmentTotals struct { + TotalQty float64 + TotalWeight float64 + } + adjustmentSubQuery := r.db.WithContext(ctx). + Table("recordings AS r"). + Select("DISTINCT ast.id AS adjustment_id, ast.total_qty AS total_qty, ast.price AS price"). + Joins("JOIN recording_eggs AS re ON re.recording_id = r.id"). + Joins("JOIN stock_transfer_details AS std ON std.dest_product_warehouse_id = re.product_warehouse_id"). + Joins( + "JOIN stock_allocations AS sa ON sa.usable_type = ? AND sa.usable_id = std.id AND sa.stockable_type = ? AND sa.status = ? AND sa.allocation_purpose = ?", + fifo.UsableKeyStockTransferOut.String(), + fifo.StockableKeyAdjustmentIn.String(), + entity.StockAllocationStatusActive, + entity.StockAllocationPurposeConsume, + ). + Joins("JOIN adjustment_stocks AS ast ON ast.id = sa.stockable_id AND ast.product_warehouse_id = std.source_product_warehouse_id"). + Where("r.project_flock_kandangs_id IN (?)", projectFlockKandangIDs). + Where("r.record_datetime <= ?", *date) + + err = r.db.WithContext(ctx). + Table("(?) AS adjustment_sources", adjustmentSubQuery). + Select("COALESCE(SUM(adjustment_sources.total_qty), 0) AS total_qty, COALESCE(SUM(adjustment_sources.price), 0) AS total_weight"). + Scan(&adjustmentTotals).Error + if err != nil { + return 0, 0, err + } + + totals.TotalPieces += adjustmentTotals.TotalQty + totals.TotalWeightKg += adjustmentTotals.TotalWeight + return totals.TotalPieces, totals.TotalWeightKg, nil }