add counting hpp-expedition by project

This commit is contained in:
ragilap
2025-12-14 23:15:30 +07:00
parent e98d0a9fa1
commit fc49cef781
5 changed files with 143 additions and 0 deletions
@@ -31,6 +31,7 @@ type ClosingService interface {
GetClosingSummary(ctx *fiber.Ctx, projectFlockID uint) (*dto.ClosingSummaryDTO, error)
GetOverhead(ctx *fiber.Ctx, projectFlockID uint) (*dto.OverheadListDTO, error)
GetClosingSapronak(ctx *fiber.Ctx, projectFlockID uint, params *validation.SapronakQuery) ([]dto.ClosingSapronakItemDTO, int64, error)
GetExpeditionHPP(ctx *fiber.Ctx, projectFlockID uint, projectFlockKandangID *uint) (*dto.ExpeditionHPPDTO, error)
}
type closingService struct {
@@ -379,3 +380,45 @@ func (s closingService) GetOverhead(c *fiber.Ctx, projectFlockID uint) (*dto.Ove
return &result, nil
}
// GetExpeditionHPP menghitung HPP ekspedisi per vendor untuk sebuah project flock.
// Jika projectFlockKandangID tidak nil, maka hanya data untuk kandang tersebut yang dihitung.
func (s closingService) GetExpeditionHPP(c *fiber.Ctx, projectFlockID uint, projectFlockKandangID *uint) (*dto.ExpeditionHPPDTO, error) {
if projectFlockID == 0 {
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid project flock id")
}
rows, err := s.Repository.GetExpeditionHPP(c.Context(), projectFlockID, projectFlockKandangID)
if err != nil {
s.Log.Errorf("Failed to get expedition HPP for project flock %d: %+v", projectFlockID, err)
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to fetch expedition HPP")
}
expeditionCosts := make([]dto.ExpeditionCostItemDTO, 0, len(rows))
var totalHPP float64
for idx, row := range rows {
unitPrice := 0.0
if row.Qty > 0 {
unitPrice = row.TotalAmount / row.Qty
}
expeditionCosts = append(expeditionCosts, dto.ExpeditionCostItemDTO{
Id: uint64(idx + 1),
ExpeditionVendorID: row.SupplierID,
ExpeditionVendorName: row.SupplierName,
Qty: row.Qty,
UnitPrice: unitPrice,
HPPAmount: row.TotalAmount,
})
totalHPP += row.TotalAmount
}
result := &dto.ExpeditionHPPDTO{
ExpeditionCosts: expeditionCosts,
TotalHPPAmount: totalHPP,
}
return result, nil
}