init add function command for create seed depretitaion standard

This commit is contained in:
giovanni
2026-06-01 21:07:30 +07:00
parent 09b1f19d19
commit 44b82a8e38
8 changed files with 747 additions and 9 deletions
@@ -112,7 +112,7 @@ type HppV2CostRepository interface {
GetFarmDepreciationSnapshotByProjectFlockIDAndPeriod(ctx context.Context, projectFlockID uint, periodDate time.Time) (*HppV2FarmDepreciationSnapshotRow, error)
GetEarliestChickInDateByProjectFlockID(ctx context.Context, projectFlockID uint) (*time.Time, error)
GetChickinPopulationByPFKForFarm(ctx context.Context, projectFlockID uint) (map[uint]float64, error)
GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, map[string]*time.Time, error)
GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int, projectFlockID uint) (map[string]map[int]float64, map[string]*time.Time, error)
ListUsageCostRowsByProductFlags(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string, date *time.Time) ([]HppV2UsageCostRow, error)
ListAdjustmentCostRowsByProductFlags(ctx context.Context, projectFlockKandangIDs []uint, flagNames []string, date *time.Time) ([]HppV2AdjustmentCostRow, error)
ListExpenseRealizationRowsByProjectFlockKandangIDs(ctx context.Context, projectFlockKandangIDs []uint, date *time.Time, ekspedisi bool) ([]HppV2ExpenseCostRow, error)
@@ -466,6 +466,7 @@ func (r *HppV2RepositoryImpl) GetMultiplicationPercentages(
ctx context.Context,
houseTypes []string,
maxDay int,
projectFlockID uint,
) (map[string]map[int]float64, map[string]*time.Time, error) {
result := make(map[string]map[int]float64)
effectiveDates := make(map[string]*time.Time)
@@ -486,8 +487,9 @@ func (r *HppV2RepositoryImpl) GetMultiplicationPercentages(
house_type::text AS house_type, day, multiplication_percentage, effective_date
FROM house_depreciation_standards
WHERE house_type::text IN ? AND day <= ?
ORDER BY house_type, day, effective_date DESC NULLS LAST
`, houseTypes, maxDay).Scan(&rows).Error
AND (project_flock_id = ? OR project_flock_id IS NULL)
ORDER BY house_type, day, (project_flock_id IS NOT NULL) DESC, effective_date DESC NULLS LAST
`, houseTypes, maxDay, projectFlockID).Scan(&rows).Error
if err != nil {
return nil, nil, err
}
@@ -1390,7 +1390,7 @@ func (s *hppV2Service) buildNormalTransferDepreciationPart(
}
houseType := NormalizeDepreciationHouseType(contextRow.HouseType)
multiplicationByHouseType, effectiveDates, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, scheduleDay)
multiplicationByHouseType, effectiveDates, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, scheduleDay, contextRow.ProjectFlockID)
if err != nil {
return nil, err
}
@@ -1499,7 +1499,7 @@ func (s *hppV2Service) buildManualCutoverDepreciationPart(
}
houseType := NormalizeDepreciationHouseType(contextRow.HouseType)
multiplicationByHouseType, effectiveDates, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, reportScheduleDay)
multiplicationByHouseType, effectiveDates, err := s.hppRepo.GetMultiplicationPercentages(context.Background(), []string{houseType}, reportScheduleDay, contextRow.ProjectFlockID)
if err != nil {
return nil, err
}
@@ -103,7 +103,7 @@ func (s *hppV2RepoStub) GetDepreciationPercents(_ context.Context, houseTypes []
// 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, map[string]*time.Time, error) {
func (s *hppV2RepoStub) GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int, _ uint) (map[string]map[int]float64, map[string]*time.Time, error) {
vals, err := s.GetDepreciationPercents(ctx, houseTypes, maxDay)
return vals, make(map[string]*time.Time), err
}
@@ -0,0 +1,15 @@
-- Hapus baris per-flock dulu supaya unique lama (house_type, day, effective_date)
-- bisa dipulihkan tanpa konflik duplikat antar-flock.
DELETE FROM house_depreciation_standards WHERE project_flock_id IS NOT NULL;
DROP INDEX IF EXISTS idx_hds_project_flock_id;
ALTER TABLE house_depreciation_standards
DROP CONSTRAINT house_depreciation_standards_htype_day_eff_pf_unique;
ALTER TABLE house_depreciation_standards
DROP COLUMN project_flock_id;
ALTER TABLE house_depreciation_standards
ADD CONSTRAINT house_depreciation_standards_house_type_day_eff_unique
UNIQUE (house_type, day, effective_date);
@@ -0,0 +1,18 @@
-- Tambah dimensi per-project-flock ke standar depresiasi.
-- Baris dengan project_flock_id NON-NULL = kurva khusus flock tsb.
-- Baris dengan project_flock_id NULL = kurva global default (fallback).
ALTER TABLE house_depreciation_standards
ADD COLUMN project_flock_id BIGINT NULL REFERENCES project_flocks(id);
-- Unique lama (house_type, day, effective_date) tidak bisa membedakan baris per-flock.
-- Ganti agar menyertakan project_flock_id (NULL dianggap distinct di PostgreSQL,
-- sehingga baris global lama tidak konflik dengan baris per-flock baru).
ALTER TABLE house_depreciation_standards
DROP CONSTRAINT house_depreciation_standards_house_type_day_eff_unique;
ALTER TABLE house_depreciation_standards
ADD CONSTRAINT house_depreciation_standards_htype_day_eff_pf_unique
UNIQUE (house_type, day, effective_date, project_flock_id);
CREATE INDEX idx_hds_project_flock_id
ON house_depreciation_standards (project_flock_id);
@@ -51,7 +51,7 @@ type ExpenseDepreciationRepository interface {
DeleteSnapshotsFromDate(ctx context.Context, fromDate time.Time, farmIDs []uint) error
DeleteSnapshotsByFarmIDs(ctx context.Context, farmIDs []uint) error
GetLatestTransferInputsByFarms(ctx context.Context, period time.Time, farmIDs []uint) ([]FarmDepreciationLatestTransferRow, error)
GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, map[string]*time.Time, error)
GetMultiplicationPercentages(ctx context.Context, houseTypes []string, maxDay int, projectFlockID uint) (map[string]map[int]float64, map[string]*time.Time, error)
GetLatestManualInputsByFarms(ctx context.Context, areaIDs, locationIDs, projectFlockIDs []int64) ([]FarmDepreciationManualInputRow, error)
UpsertManualInput(ctx context.Context, row *entity.FarmDepreciationManualInput) error
DB() *gorm.DB
@@ -245,6 +245,7 @@ func (r *expenseDepreciationRepository) GetMultiplicationPercentages(
ctx context.Context,
houseTypes []string,
maxDay int,
projectFlockID uint,
) (map[string]map[int]float64, map[string]*time.Time, error) {
result := make(map[string]map[int]float64)
effectiveDates := make(map[string]*time.Time)
@@ -258,8 +259,9 @@ func (r *expenseDepreciationRepository) GetMultiplicationPercentages(
house_type::text AS house_type, day, multiplication_percentage, effective_date
FROM house_depreciation_standards
WHERE house_type::text IN ? AND day <= ?
ORDER BY house_type, day, effective_date DESC NULLS LAST
`, houseTypes, maxDay).Scan(&rows).Error; err != nil {
AND (project_flock_id = ? OR project_flock_id IS NULL)
ORDER BY house_type, day, (project_flock_id IS NOT NULL) DESC, effective_date DESC NULLS LAST
`, houseTypes, maxDay, projectFlockID).Scan(&rows).Error; err != nil {
return nil, nil, err
}