mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-06-09 15:07:49 +00:00
initial refactori trasnfer to laying, and depretitation to 25 week
This commit is contained in:
@@ -6,8 +6,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
depreciationStartAgeDayCloseHouse = 155
|
||||
depreciationStartAgeDayOpenHouse = 176
|
||||
depreciationStartAgeDayCloseHouse = 175
|
||||
depreciationStartAgeDayOpenHouse = 175
|
||||
)
|
||||
|
||||
func NormalizeDepreciationHouseType(raw string) string {
|
||||
@@ -26,8 +26,8 @@ func DepreciationStartAgeDay(houseType string) int {
|
||||
}
|
||||
|
||||
func FlockAgeDay(originDate time.Time, periodDate time.Time) int {
|
||||
origin := time.Date(originDate.Year(), originDate.Month(), originDate.Day(), 0, 0, 0, 0, originDate.Location())
|
||||
period := time.Date(periodDate.Year(), periodDate.Month(), periodDate.Day(), 0, 0, 0, 0, periodDate.Location())
|
||||
origin := time.Date(originDate.Year(), originDate.Month(), originDate.Day(), 0, 0, 0, 0, time.UTC)
|
||||
period := time.Date(periodDate.Year(), periodDate.Month(), periodDate.Day(), 0, 0, 0, 0, time.UTC)
|
||||
if period.Before(origin) {
|
||||
return 0
|
||||
}
|
||||
@@ -47,9 +47,9 @@ func CalculateDepreciationAtDayN(
|
||||
initialPulletCost float64,
|
||||
dayN int,
|
||||
houseType string,
|
||||
percentByHouseType map[string]map[int]float64,
|
||||
multiplicationByHouseType map[string]map[int]float64,
|
||||
) (float64, float64, float64) {
|
||||
return CalculateDepreciationFromDayRange(initialPulletCost, 1, dayN, houseType, percentByHouseType)
|
||||
return CalculateDepreciationFromDayRange(initialPulletCost, 1, dayN, houseType, multiplicationByHouseType)
|
||||
}
|
||||
|
||||
func CalculateDepreciationFromDayRange(
|
||||
@@ -57,8 +57,8 @@ func CalculateDepreciationFromDayRange(
|
||||
startDay int,
|
||||
endDay int,
|
||||
houseType string,
|
||||
percentByHouseType map[string]map[int]float64,
|
||||
) (float64, float64, float64) {
|
||||
multiplicationByHouseType map[string]map[int]float64,
|
||||
) (pulletCostDayN, depreciationValue, multiplicationPercentage float64) {
|
||||
if initialPulletCost <= 0 || endDay <= 0 {
|
||||
return 0, 0, 0
|
||||
}
|
||||
@@ -70,30 +70,30 @@ func CalculateDepreciationFromDayRange(
|
||||
}
|
||||
|
||||
normalizedHouseType := NormalizeDepreciationHouseType(houseType)
|
||||
housePercent, exists := percentByHouseType[normalizedHouseType]
|
||||
houseMult, exists := multiplicationByHouseType[normalizedHouseType]
|
||||
if !exists {
|
||||
return 0, 0, 0
|
||||
}
|
||||
|
||||
current := initialPulletCost
|
||||
pulletCostDayN := 0.0
|
||||
depreciationValue := 0.0
|
||||
depreciationPercent := 0.0
|
||||
for day := startDay; day <= endDay; day++ {
|
||||
pct := housePercent[day]
|
||||
dep := current * (pct / 100)
|
||||
mult, ok := houseMult[day]
|
||||
if !ok {
|
||||
// No standard for this day → assume no depreciation (mult=1).
|
||||
mult = 1.0
|
||||
}
|
||||
if day == endDay {
|
||||
pulletCostDayN = current
|
||||
depreciationValue = dep
|
||||
depreciationPercent = pct
|
||||
multiplicationPercentage = mult
|
||||
depreciationValue = current * (1.0 - mult)
|
||||
}
|
||||
current -= dep
|
||||
current = current * mult
|
||||
if current < 0 {
|
||||
current = 0
|
||||
}
|
||||
}
|
||||
|
||||
return pulletCostDayN, depreciationValue, depreciationPercent
|
||||
return pulletCostDayN, depreciationValue, multiplicationPercentage
|
||||
}
|
||||
|
||||
func CalculateEffectiveDepreciationPercent(totalDepreciationValue, totalPulletCostDayN float64) float64 {
|
||||
|
||||
@@ -1191,26 +1191,72 @@ func (s *hppV2Service) getDepreciationComponent(
|
||||
}, nil
|
||||
}
|
||||
|
||||
if totalPulletCost <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
transferInput, err := s.hppRepo.GetLatestTransferInputByProjectFlockKandangID(context.Background(), projectFlockKandangId, periodDate)
|
||||
// Multi-source support: 1 target kandang bisa menerima dari MULTIPLE transfer terpisah
|
||||
// (tiap transfer = 1 source kandang). Depresiasi per target = SUM dari per-transfer depresiasi.
|
||||
// Setiap transfer dihitung dengan chick_in_date source-nya sendiri dan cost basis pro-rated
|
||||
// berdasarkan qty share (transfer.qty / totalTransferQty).
|
||||
transferInputs, err := s.hppRepo.GetAllTransferInputsByProjectFlockKandangID(context.Background(), projectFlockKandangId, periodDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var part *HppV2ComponentPart
|
||||
if transferInput != nil && transferInput.SourceProjectFlockID > 0 {
|
||||
part, err = s.buildNormalTransferDepreciationPart(contextRow, transferInput, periodDate, totalPulletCost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// Filter valid transfers (punya source flock id)
|
||||
validTransfers := make([]commonRepo.HppV2LatestTransferInputRow, 0, len(transferInputs))
|
||||
totalTransferQty := 0.0
|
||||
for _, t := range transferInputs {
|
||||
if t.SourceProjectFlockID == 0 {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
part, err = s.buildManualCutoverDepreciationPart(projectFlockKandangId, contextRow, periodDate, totalPulletCost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
validTransfers = append(validTransfers, t)
|
||||
totalTransferQty += t.TransferQty
|
||||
}
|
||||
|
||||
if len(validTransfers) > 0 {
|
||||
if totalPulletCost <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
totalDepreciation := 0.0
|
||||
parts := make([]HppV2ComponentPart, 0, len(validTransfers))
|
||||
for i := range validTransfers {
|
||||
t := validTransfers[i]
|
||||
// Pro-rate cost basis per transfer berdasarkan qty share.
|
||||
// CATATAN: pendekatan ini AKURAT kalau cost per ekor sama antar source flock.
|
||||
// Kalau cost per ekor berbeda signifikan antar source, follow-up: refactor
|
||||
// `buildGrowingUsagePart` untuk multi-source-flock cost computation.
|
||||
transferCostBasis := totalPulletCost
|
||||
if totalTransferQty > 0 && len(validTransfers) > 1 {
|
||||
transferCostBasis = totalPulletCost * (t.TransferQty / totalTransferQty)
|
||||
}
|
||||
|
||||
part, partErr := s.buildNormalTransferDepreciationPart(contextRow, &t, periodDate, transferCostBasis)
|
||||
if partErr != nil {
|
||||
return nil, partErr
|
||||
}
|
||||
if part == nil {
|
||||
continue
|
||||
}
|
||||
totalDepreciation += part.Total
|
||||
parts = append(parts, *part)
|
||||
}
|
||||
|
||||
if len(parts) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &HppV2Component{
|
||||
Code: hppV2ComponentDepreciation,
|
||||
Title: "Depreciation",
|
||||
Scopes: []string{hppV2ScopeProductionCost},
|
||||
Total: totalDepreciation,
|
||||
Parts: parts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Fallback: manual cut-over (kandang tanpa transfer record)
|
||||
part, err := s.buildManualCutoverDepreciationPart(projectFlockKandangId, contextRow, periodDate, totalPulletCost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if part == nil {
|
||||
return nil, nil
|
||||
@@ -1344,20 +1390,22 @@ func (s *hppV2Service) buildNormalTransferDepreciationPart(
|
||||
}
|
||||
|
||||
houseType := NormalizeDepreciationHouseType(contextRow.HouseType)
|
||||
percentByHouseType, err := s.hppRepo.GetDepreciationPercents(context.Background(), []string{houseType}, scheduleDay)
|
||||
multiplicationByHouseType, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, scheduleDay)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pulletCostDayN, depreciationValue, depreciationPercent := CalculateDepreciationAtDayN(
|
||||
pulletCostDayN, depreciationValue, multiplicationPercentage := CalculateDepreciationAtDayN(
|
||||
totalPulletCost,
|
||||
scheduleDay,
|
||||
contextRow.HouseType,
|
||||
percentByHouseType,
|
||||
multiplicationByHouseType,
|
||||
)
|
||||
if depreciationValue <= 0 {
|
||||
if depreciationValue <= 0 && pulletCostDayN <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
totalValueAfter := pulletCostDayN * multiplicationPercentage
|
||||
depreciationPercent := (1.0 - multiplicationPercentage) * 100.0
|
||||
|
||||
return &HppV2ComponentPart{
|
||||
Code: hppV2PartDepreciationNormal,
|
||||
@@ -1365,13 +1413,15 @@ func (s *hppV2Service) buildNormalTransferDepreciationPart(
|
||||
Scopes: []string{hppV2ScopeProductionCost},
|
||||
Total: depreciationValue,
|
||||
Details: map[string]any{
|
||||
"basis_total": totalPulletCost,
|
||||
"pullet_cost_day_n": pulletCostDayN,
|
||||
"depreciation_percent": depreciationPercent,
|
||||
"schedule_day": scheduleDay,
|
||||
"origin_date": formatDateOnly(*originDate),
|
||||
"transfer_date": formatDateOnly(transferInput.TransferDate),
|
||||
"source_project_flock_id": transferInput.SourceProjectFlockID,
|
||||
"basis_total": totalPulletCost,
|
||||
"pullet_cost_day_n": pulletCostDayN,
|
||||
"multiplication_percentage": multiplicationPercentage,
|
||||
"total_value_pullet_after_depreciation": totalValueAfter,
|
||||
"depreciation_percent": depreciationPercent,
|
||||
"schedule_day": scheduleDay,
|
||||
"origin_date": formatDateOnly(*originDate),
|
||||
"transfer_date": formatDateOnly(transferInput.TransferDate),
|
||||
"source_project_flock_id": transferInput.SourceProjectFlockID,
|
||||
},
|
||||
References: []HppV2Reference{
|
||||
{
|
||||
@@ -1392,7 +1442,7 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
|
||||
periodDate time.Time,
|
||||
totalPulletCost float64,
|
||||
) (*HppV2ComponentPart, error) {
|
||||
if contextRow == nil || totalPulletCost <= 0 {
|
||||
if contextRow == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -1407,6 +1457,21 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
populations, err := s.hppRepo.GetChickinPopulationByPFKForFarm(context.Background(), contextRow.ProjectFlockID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var totalPopulation float64
|
||||
for _, qty := range populations {
|
||||
totalPopulation += qty
|
||||
}
|
||||
kandangPopulation := populations[projectFlockKandangId]
|
||||
if totalPopulation <= 0 || kandangPopulation <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
populationShare := kandangPopulation / totalPopulation
|
||||
basis := manualInput.TotalCost * populationShare
|
||||
|
||||
originDate, err := s.hppRepo.GetEarliestChickInDateByProjectFlockID(context.Background(), contextRow.ProjectFlockID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -1427,21 +1492,24 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
|
||||
}
|
||||
|
||||
houseType := NormalizeDepreciationHouseType(contextRow.HouseType)
|
||||
percentByHouseType, err := s.hppRepo.GetDepreciationPercents(context.Background(), []string{houseType}, reportScheduleDay)
|
||||
multiplicationByHouseType, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, reportScheduleDay)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pulletCostDayN, depreciationValue, depreciationPercent := CalculateDepreciationFromDayRange(
|
||||
totalPulletCost,
|
||||
pulletCostDayN, depreciationValue, multiplicationPercentage := CalculateDepreciationFromDayRange(
|
||||
basis,
|
||||
startDay,
|
||||
reportScheduleDay,
|
||||
contextRow.HouseType,
|
||||
percentByHouseType,
|
||||
multiplicationByHouseType,
|
||||
)
|
||||
if depreciationValue <= 0 {
|
||||
if depreciationValue <= 0 && pulletCostDayN <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
totalValueAfter := pulletCostDayN * multiplicationPercentage
|
||||
depreciationPercent := (1.0 - multiplicationPercentage) * 100.0
|
||||
_ = totalPulletCost
|
||||
|
||||
return &HppV2ComponentPart{
|
||||
Code: hppV2PartDepreciationCutover,
|
||||
@@ -1449,15 +1517,19 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
|
||||
Scopes: []string{hppV2ScopeProductionCost},
|
||||
Total: depreciationValue,
|
||||
Details: map[string]any{
|
||||
"basis_total": totalPulletCost,
|
||||
"pullet_cost_day_n": pulletCostDayN,
|
||||
"depreciation_percent": depreciationPercent,
|
||||
"schedule_day": reportScheduleDay,
|
||||
"start_schedule_day": startDay,
|
||||
"origin_date": formatDateOnly(*originDate),
|
||||
"cutover_date": formatDateOnly(manualInput.CutoverDate),
|
||||
"manual_input_id": manualInput.ID,
|
||||
"project_flock_kandang": projectFlockKandangId,
|
||||
"basis_total": basis,
|
||||
"manual_input_total": manualInput.TotalCost,
|
||||
"population_share": populationShare,
|
||||
"pullet_cost_day_n": pulletCostDayN,
|
||||
"multiplication_percentage": multiplicationPercentage,
|
||||
"total_value_pullet_after_depreciation": totalValueAfter,
|
||||
"depreciation_percent": depreciationPercent,
|
||||
"schedule_day": reportScheduleDay,
|
||||
"start_schedule_day": startDay,
|
||||
"origin_date": formatDateOnly(*originDate),
|
||||
"cutover_date": formatDateOnly(manualInput.CutoverDate),
|
||||
"manual_input_id": manualInput.ID,
|
||||
"project_flock_kandang": projectFlockKandangId,
|
||||
},
|
||||
References: []HppV2Reference{
|
||||
{
|
||||
@@ -1465,7 +1537,7 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
|
||||
ID: manualInput.ID,
|
||||
Date: formatDateOnly(manualInput.CutoverDate),
|
||||
Qty: 1,
|
||||
Total: totalPulletCost,
|
||||
Total: manualInput.TotalCost,
|
||||
AppliedTotal: depreciationValue,
|
||||
},
|
||||
},
|
||||
@@ -1724,7 +1796,7 @@ func partHasScope(part *HppV2ComponentPart, scope string) bool {
|
||||
}
|
||||
|
||||
func dateOnly(value time.Time) time.Time {
|
||||
return time.Date(value.Year(), value.Month(), value.Day(), 0, 0, 0, 0, value.Location())
|
||||
return time.Date(value.Year(), value.Month(), value.Day(), 0, 0, 0, 0, time.UTC)
|
||||
}
|
||||
|
||||
func formatDateOnly(value time.Time) string {
|
||||
|
||||
@@ -57,6 +57,14 @@ func (s *hppV2RepoStub) GetLatestTransferInputByProjectFlockKandangID(_ context.
|
||||
return s.latestTransferByPFK[projectFlockKandangId], nil
|
||||
}
|
||||
|
||||
func (s *hppV2RepoStub) GetAllTransferInputsByProjectFlockKandangID(_ context.Context, projectFlockKandangId uint, _ time.Time) ([]commonRepo.HppV2LatestTransferInputRow, error) {
|
||||
row := s.latestTransferByPFK[projectFlockKandangId]
|
||||
if row == nil {
|
||||
return []commonRepo.HppV2LatestTransferInputRow{}, nil
|
||||
}
|
||||
return []commonRepo.HppV2LatestTransferInputRow{*row}, nil
|
||||
}
|
||||
|
||||
func (s *hppV2RepoStub) GetManualDepreciationInputByProjectFlockID(_ context.Context, projectFlockID uint) (*commonRepo.HppV2ManualDepreciationInputRow, error) {
|
||||
return s.manualInputByProject[projectFlockID], nil
|
||||
}
|
||||
@@ -93,6 +101,18 @@ func (s *hppV2RepoStub) GetDepreciationPercents(_ context.Context, houseTypes []
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetMultiplicationPercentages — alias yang sama dengan GetDepreciationPercents untuk match
|
||||
// interface HppV2CostRepository (interface dipakai method name baru ini).
|
||||
func (s *hppV2RepoStub) GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, error) {
|
||||
return s.GetDepreciationPercents(ctx, houseTypes, maxDay)
|
||||
}
|
||||
|
||||
// GetChickinPopulationByPFKForFarm — return populasi per PFK dari satu project flock.
|
||||
// Stub minimal: return empty map (depreciation manual cutover tidak di-test di sini).
|
||||
func (s *hppV2RepoStub) GetChickinPopulationByPFKForFarm(_ context.Context, _ uint) (map[uint]float64, error) {
|
||||
return map[uint]float64{}, nil
|
||||
}
|
||||
|
||||
func (s *hppV2RepoStub) ListUsageCostRowsByProductFlags(_ context.Context, projectFlockKandangIDs []uint, flagNames []string, _ *time.Time) ([]commonRepo.HppV2UsageCostRow, error) {
|
||||
return append([]commonRepo.HppV2UsageCostRow{}, s.usageRowsByKey[stubKey(projectFlockKandangIDs, flagNames)]...), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user