init depresiasi

This commit is contained in:
giovanni
2026-04-17 21:26:56 +07:00
parent a54c6184a2
commit fcde3b0a36
34 changed files with 3588 additions and 46 deletions
@@ -377,6 +377,7 @@ func (s *transferLayingService) CreateOne(c *fiber.Ctx, req *validation.Create)
if err != nil {
return nil, err
}
s.invalidateDepreciationSnapshots(c.Context(), nil, []uint{req.TargetProjectFlockId}, transferDate)
return laying_transfer, nil
}
@@ -588,6 +589,13 @@ func (s *transferLayingService) UpdateOne(c *fiber.Ctx, req *validation.Update,
}
layingTransfer, _, err := s.GetOne(c, id)
invalidateFromDate := commonSvc.MinNonZeroDateOnlyUTC(existingTransfer.TransferDate, transferDate)
s.invalidateDepreciationSnapshots(
c.Context(),
nil,
[]uint{existingTransfer.ToProjectFlockId, req.TargetProjectFlockId},
invalidateFromDate,
)
return layingTransfer, err
}
@@ -661,6 +669,7 @@ func (s transferLayingService) DeleteOne(c *fiber.Ctx, id uint) error {
s.Log.Errorf("Failed to delete transferLaying: %+v", err)
return fiber.NewError(fiber.StatusInternalServerError, "Failed to delete transfer laying")
}
s.invalidateDepreciationSnapshots(c.Context(), nil, []uint{transfer.ToProjectFlockId}, transfer.TransferDate)
return nil
}
@@ -798,6 +807,14 @@ func (s transferLayingService) Approval(c *fiber.Ctx, req *validation.Approve) (
if err != nil {
return nil, err
}
if transfer != nil {
s.invalidateDepreciationSnapshots(
c.Context(),
nil,
[]uint{transfer.ToProjectFlockId},
resolveDepreciationEffectiveDateForTransfer(transfer),
)
}
updated = append(updated, *transfer)
}
@@ -837,6 +854,14 @@ func (s transferLayingService) Execute(c *fiber.Ctx, id uint) (*entity.LayingTra
if err != nil {
return nil, err
}
if transfer != nil {
s.invalidateDepreciationSnapshots(
c.Context(),
nil,
[]uint{transfer.ToProjectFlockId},
resolveDepreciationEffectiveDateForTransfer(transfer),
)
}
return transfer, nil
}
@@ -873,6 +898,14 @@ func (s transferLayingService) ExecuteWithBusinessDate(c *fiber.Ctx, id uint, bu
if err != nil {
return nil, err
}
if transfer != nil {
s.invalidateDepreciationSnapshots(
c.Context(),
nil,
[]uint{transfer.ToProjectFlockId},
resolveDepreciationEffectiveDateForTransfer(transfer),
)
}
return transfer, nil
}
@@ -1226,6 +1259,14 @@ func (s transferLayingService) Unexecute(c *fiber.Ctx, id uint) (*entity.LayingT
if err != nil {
return nil, err
}
if transfer != nil {
s.invalidateDepreciationSnapshots(
c.Context(),
nil,
[]uint{transfer.ToProjectFlockId},
resolveDepreciationEffectiveDateForTransfer(transfer),
)
}
return transfer, nil
}
@@ -1678,6 +1719,43 @@ func normalizeDateOnlyUTC(value time.Time) time.Time {
return time.Date(value.UTC().Year(), value.UTC().Month(), value.UTC().Day(), 0, 0, 0, 0, time.UTC)
}
func resolveDepreciationEffectiveDateForTransfer(transfer *entity.LayingTransfer) time.Time {
if transfer == nil {
return time.Time{}
}
if transfer.EffectiveMoveDate != nil && !transfer.EffectiveMoveDate.IsZero() {
return *transfer.EffectiveMoveDate
}
if transfer.EconomicCutoffDate != nil && !transfer.EconomicCutoffDate.IsZero() {
return *transfer.EconomicCutoffDate
}
return transfer.TransferDate
}
func (s transferLayingService) invalidateDepreciationSnapshots(
ctx context.Context,
tx *gorm.DB,
farmIDs []uint,
fromDate time.Time,
) {
if fromDate.IsZero() {
return
}
targetDB := s.Repository.DB()
if tx != nil {
targetDB = tx
}
uniqueFarmIDs := utils.UniqueUintSlice(farmIDs)
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, uniqueFarmIDs, fromDate); err != nil {
s.Log.Warnf(
"Failed to invalidate farm depreciation snapshots (farms=%v, from=%s): %+v",
uniqueFarmIDs,
fromDate.Format("2006-01-02"),
err,
)
}
}
func isLegacyTransfer(transfer *entity.LayingTransfer) bool {
if transfer == nil {
return false