mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
init depresiasi
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
@@ -358,6 +359,7 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, uint(expense.Id), expenseDate, nil)
|
||||
return responseDTO, nil
|
||||
}
|
||||
|
||||
@@ -385,6 +387,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
}
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
var requestedTransactionDate *time.Time
|
||||
|
||||
if req.TransactionDate != nil {
|
||||
expenseDate, err := utils.ParseDateString(*req.TransactionDate)
|
||||
@@ -392,6 +395,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "Invalid transaction_date format")
|
||||
}
|
||||
updateBody["transaction_date"] = expenseDate
|
||||
requestedTransactionDate = &expenseDate
|
||||
}
|
||||
|
||||
if req.Category != nil {
|
||||
@@ -429,6 +433,8 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
return responseDTO, nil
|
||||
}
|
||||
|
||||
var invalidationFromDate time.Time
|
||||
var invalidationFarmIDs []uint
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
expenseRepoTx := repository.NewExpenseRepository(tx)
|
||||
@@ -446,6 +452,16 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
if err := s.ensureProjectFlockNotClosedForExpense(c.Context(), currentExpense); err != nil {
|
||||
return err
|
||||
}
|
||||
oldFarmIDs, resolveOldFarmErr := commonSvc.ResolveProjectFlockIDsByExpenseID(c.Context(), tx, id)
|
||||
if resolveOldFarmErr != nil {
|
||||
s.Log.Warnf("Failed to resolve old expense farm ids for invalidation (expense_id=%d): %+v", id, resolveOldFarmErr)
|
||||
}
|
||||
invalidationFarmIDs = append(invalidationFarmIDs, oldFarmIDs...)
|
||||
|
||||
invalidationFromDate = currentExpense.TransactionDate
|
||||
if requestedTransactionDate != nil {
|
||||
invalidationFromDate = commonSvc.MinNonZeroDateOnlyUTC(currentExpense.TransactionDate, *requestedTransactionDate)
|
||||
}
|
||||
categoryChanged := false
|
||||
var newCategory string
|
||||
if req.Category != nil && *req.Category != currentExpense.Category {
|
||||
@@ -631,6 +647,12 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
}
|
||||
}
|
||||
|
||||
newFarmIDs, resolveNewFarmErr := commonSvc.ResolveProjectFlockIDsByExpenseID(c.Context(), tx, id)
|
||||
if resolveNewFarmErr != nil {
|
||||
s.Log.Warnf("Failed to resolve new expense farm ids for invalidation (expense_id=%d): %+v", id, resolveNewFarmErr)
|
||||
}
|
||||
invalidationFarmIDs = append(invalidationFarmIDs, newFarmIDs...)
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -645,6 +667,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(c.Context(), nil, invalidationFarmIDs, invalidationFromDate)
|
||||
return responseDTO, nil
|
||||
}
|
||||
|
||||
@@ -671,6 +694,10 @@ func (s expenseService) DeleteOne(c *fiber.Ctx, id uint64) error {
|
||||
if err := s.ensureProjectFlockNotClosedForExpense(c.Context(), expense); err != nil {
|
||||
return err
|
||||
}
|
||||
farmIDs, resolveFarmErr := commonSvc.ResolveProjectFlockIDsByExpenseID(c.Context(), s.Repository.DB(), idUint)
|
||||
if resolveFarmErr != nil {
|
||||
s.Log.Warnf("Failed to resolve expense farm ids before delete (expense_id=%d): %+v", idUint, resolveFarmErr)
|
||||
}
|
||||
if err := s.Repository.DeleteOne(c.Context(), idUint); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
s.Log.Errorf("Expense not found for ID %d: %+v", id, err)
|
||||
@@ -680,6 +707,8 @@ func (s expenseService) DeleteOne(c *fiber.Ctx, id uint64) error {
|
||||
return err
|
||||
}
|
||||
s.Log.Infof("Successfully deleted expense with ID %d", id)
|
||||
invalidationFromDate := commonSvc.MinNonZeroDateOnlyUTC(expense.TransactionDate, expense.RealizationDate)
|
||||
s.invalidateDepreciationSnapshots(c.Context(), nil, farmIDs, invalidationFromDate)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -800,6 +829,8 @@ func (s *expenseService) CreateRealization(c *fiber.Ctx, expenseID uint, req *va
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
invalidateFromDate := commonSvc.MinNonZeroDateOnlyUTC(expense.TransactionDate, realizationDate, expense.RealizationDate)
|
||||
s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, expenseID, invalidateFromDate, nil)
|
||||
return responseDTO, nil
|
||||
}
|
||||
|
||||
@@ -857,6 +888,13 @@ func (s *expenseService) CompleteExpense(c *fiber.Ctx, id uint, notes *string) (
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
expense, expenseErr := s.Repository.GetByID(c.Context(), id, nil)
|
||||
if expenseErr != nil {
|
||||
s.Log.Warnf("Failed to load expense for depreciation invalidation after complete (expense_id=%d): %+v", id, expenseErr)
|
||||
} else {
|
||||
invalidateFromDate := commonSvc.MinNonZeroDateOnlyUTC(expense.TransactionDate, expense.RealizationDate)
|
||||
s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, id, invalidateFromDate, nil)
|
||||
}
|
||||
return responseDTO, nil
|
||||
}
|
||||
|
||||
@@ -884,6 +922,12 @@ func (s *expenseService) UpdateRealization(c *fiber.Ctx, expenseID uint, req *va
|
||||
if err := s.ensureProjectFlockNotClosedForExpense(c.Context(), expense); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
invalidateFromDate := commonSvc.MinNonZeroDateOnlyUTC(expense.TransactionDate, expense.RealizationDate)
|
||||
if req.RealizationDate != nil {
|
||||
if parsedDate, parseErr := utils.ParseDateString(*req.RealizationDate); parseErr == nil {
|
||||
invalidateFromDate = commonSvc.MinNonZeroDateOnlyUTC(invalidateFromDate, parsedDate)
|
||||
}
|
||||
}
|
||||
latestApproval, err := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowExpense, expenseID, nil)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to validate workflow")
|
||||
@@ -996,6 +1040,7 @@ func (s *expenseService) UpdateRealization(c *fiber.Ctx, expenseID uint, req *va
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, expenseID, invalidateFromDate, nil)
|
||||
return responseDTO, nil
|
||||
}
|
||||
|
||||
@@ -1057,6 +1102,7 @@ func (s *expenseService) Approval(c *fiber.Ctx, req *validation.ApprovalRequest,
|
||||
}
|
||||
|
||||
var results []expenseDto.ExpenseDetailDTO
|
||||
invalidateFromDateByExpenseID := make(map[uint]time.Time)
|
||||
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(tx *gorm.DB) error {
|
||||
|
||||
@@ -1069,6 +1115,17 @@ func (s *expenseService) Approval(c *fiber.Ctx, req *validation.ApprovalRequest,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
expenseForInvalidation, err := expenseRepoTx.GetByID(c.Context(), id, nil)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Expense not found")
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to load expense")
|
||||
}
|
||||
invalidateFromDateByExpenseID[id] = commonSvc.MinNonZeroDateOnlyUTC(
|
||||
expenseForInvalidation.TransactionDate,
|
||||
expenseForInvalidation.RealizationDate,
|
||||
)
|
||||
|
||||
latestApproval, err := s.ApprovalSvc.LatestByTarget(c.Context(), utils.ApprovalWorkflowExpense, id, nil)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -1170,10 +1227,73 @@ func (s *expenseService) Approval(c *fiber.Ctx, req *validation.ApprovalRequest,
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed approve expenses")
|
||||
}
|
||||
for expenseID, invalidateFromDate := range invalidateFromDateByExpenseID {
|
||||
s.invalidateDepreciationSnapshotsByExpense(c.Context(), nil, expenseID, invalidateFromDate, nil)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (s *expenseService) invalidateDepreciationSnapshotsByExpense(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
expenseID uint,
|
||||
fromDate time.Time,
|
||||
fallbackFarmIDs []uint,
|
||||
) {
|
||||
targetDB := s.Repository.DB()
|
||||
if tx != nil {
|
||||
targetDB = tx
|
||||
}
|
||||
|
||||
farmIDs := append([]uint{}, fallbackFarmIDs...)
|
||||
if expenseID != 0 {
|
||||
resolvedFarmIDs, err := commonSvc.ResolveProjectFlockIDsByExpenseID(ctx, targetDB, expenseID)
|
||||
if err != nil {
|
||||
s.Log.Warnf("Failed to resolve expense farm ids for invalidation (expense_id=%d): %+v", expenseID, err)
|
||||
} else {
|
||||
farmIDs = append(farmIDs, resolvedFarmIDs...)
|
||||
}
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(ctx, tx, farmIDs, fromDate)
|
||||
}
|
||||
|
||||
func (s *expenseService) 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
|
||||
}
|
||||
farmIDs = utils.UniqueUintSlice(farmIDs)
|
||||
if len(farmIDs) == 0 {
|
||||
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, nil, fromDate); err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to invalidate depreciation snapshots globally (from=%s): %+v",
|
||||
fromDate.Format("2006-01-02"),
|
||||
err,
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, farmIDs, fromDate); err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to invalidate depreciation snapshots (farm_ids=%v, from=%s): %+v",
|
||||
farmIDs,
|
||||
fromDate.Format("2006-01-02"),
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *expenseService) generatePoNumber(ctx *gorm.DB, expenseID uint) (string, error) {
|
||||
|
||||
expenseRepoTx := repository.NewExpenseRepository(ctx)
|
||||
|
||||
@@ -419,6 +419,11 @@ func (s *chickinService) CreateOne(c *fiber.Ctx, req *validation.Create) ([]enti
|
||||
if len(result) == 0 {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to load created chickins")
|
||||
}
|
||||
invalidateFromDate := time.Time{}
|
||||
for i := range result {
|
||||
invalidateFromDate = commonSvc.MinNonZeroDateOnlyUTC(invalidateFromDate, result[i].ChickInDate)
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(c.Context(), nil, []uint{req.ProjectFlockKandangId}, invalidateFromDate)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
@@ -462,6 +467,8 @@ func (s chickinService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
invalidateFromDate := commonSvc.MinNonZeroDateOnlyUTC(chickin.ChickInDate, updated.ChickInDate)
|
||||
s.invalidateDepreciationSnapshots(c.Context(), nil, []uint{updated.ProjectFlockKandangId}, invalidateFromDate)
|
||||
|
||||
if updated.UsageQty > 0 {
|
||||
if err := s.syncChickinTraceForProductWarehouse(c.Context(), nil, updated.ProductWarehouseId); err != nil {
|
||||
@@ -566,6 +573,7 @@ func (s chickinService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
consumeAllocAfter,
|
||||
traceAllocAfter,
|
||||
)
|
||||
s.invalidateDepreciationSnapshots(c.Context(), tx, []uint{lockedChickin.ProjectFlockKandangId}, lockedChickin.ChickInDate)
|
||||
|
||||
return nil
|
||||
})
|
||||
@@ -1160,6 +1168,7 @@ func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entit
|
||||
if action == entity.ApprovalActionApproved {
|
||||
step = utils.ChickinStepDisetujui
|
||||
}
|
||||
invalidateFromByPFK := make(map[uint]time.Time, len(approvableIDs))
|
||||
|
||||
err = s.Repository.DB().WithContext(c.Context()).Transaction(func(dbTransaction *gorm.DB) error {
|
||||
if err := s.ensurePopulationRouteScope(c.Context(), dbTransaction); err != nil {
|
||||
@@ -1204,6 +1213,12 @@ func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entit
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get chickins for approval %d", approvableID))
|
||||
}
|
||||
for _, chickin := range chickins {
|
||||
invalidateFromByPFK[approvableID] = commonSvc.MinNonZeroDateOnlyUTC(
|
||||
invalidateFromByPFK[approvableID],
|
||||
chickin.ChickInDate,
|
||||
)
|
||||
}
|
||||
|
||||
kandangForApproval, err := s.ProjectflockKandangRepo.GetByID(c.Context(), approvableID)
|
||||
if err != nil {
|
||||
@@ -1281,6 +1296,12 @@ func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entit
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("Failed to get pending chickins for rejection %d", approvableID))
|
||||
}
|
||||
for _, chickin := range chickins {
|
||||
invalidateFromByPFK[approvableID] = commonSvc.MinNonZeroDateOnlyUTC(
|
||||
invalidateFromByPFK[approvableID],
|
||||
chickin.ChickInDate,
|
||||
)
|
||||
}
|
||||
|
||||
if len(chickins) == 0 {
|
||||
continue
|
||||
@@ -1328,6 +1349,9 @@ func (s chickinService) Approval(c *fiber.Ctx, req *validation.Approve) ([]entit
|
||||
}
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to record approval")
|
||||
}
|
||||
for projectFlockKandangID, invalidateFromDate := range invalidateFromByPFK {
|
||||
s.invalidateDepreciationSnapshots(c.Context(), nil, []uint{projectFlockKandangID}, invalidateFromDate)
|
||||
}
|
||||
|
||||
updated := make([]entity.ProjectChickin, 0)
|
||||
for _, kandangID := range approvableIDs {
|
||||
@@ -1837,6 +1861,57 @@ 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 (s chickinService) invalidateDepreciationSnapshots(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
projectFlockKandangIDs []uint,
|
||||
fromDate time.Time,
|
||||
) {
|
||||
if fromDate.IsZero() {
|
||||
return
|
||||
}
|
||||
|
||||
projectFlockKandangIDs = uniqueUint(projectFlockKandangIDs)
|
||||
if len(projectFlockKandangIDs) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
targetDB := s.Repository.DB()
|
||||
if tx != nil {
|
||||
targetDB = tx
|
||||
}
|
||||
|
||||
farmIDs, err := commonSvc.ResolveProjectFlockIDsByProjectFlockKandangIDs(ctx, targetDB, projectFlockKandangIDs)
|
||||
if err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to resolve farm ids for chickin depreciation invalidation (pfk_ids=%v): %+v",
|
||||
projectFlockKandangIDs,
|
||||
err,
|
||||
)
|
||||
farmIDs = nil
|
||||
}
|
||||
|
||||
if len(farmIDs) == 0 {
|
||||
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, nil, fromDate); err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to invalidate depreciation snapshots globally (from=%s): %+v",
|
||||
fromDate.Format("2006-01-02"),
|
||||
err,
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, farmIDs, fromDate); err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to invalidate depreciation snapshots (farm_ids=%v, from=%s): %+v",
|
||||
farmIDs,
|
||||
fromDate.Format("2006-01-02"),
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *chickinService) syncChickinTraceForProductWarehouse(ctx context.Context, tx *gorm.DB, productWarehouseID uint) error {
|
||||
if productWarehouseID == 0 {
|
||||
return nil
|
||||
|
||||
@@ -13,11 +13,11 @@ func ProjectFlockKandangRoutes(v1 fiber.Router, u user.UserService, s projectFlo
|
||||
ctrl := controller.NewProjectFlockKandangController(s)
|
||||
|
||||
route := v1.Group("/project-flock-kandangs")
|
||||
route.Use(m.Auth(u))
|
||||
route.Get("/",m.RequirePermissions(m.P_ProjectFlockKandangsGetAll), ctrl.GetAll)
|
||||
route.Get("/:id",m.RequirePermissions(m.P_ProjectFlockKandangsGetOne), ctrl.GetOne)
|
||||
// route.Use(m.Auth(u))
|
||||
route.Get("/", ctrl.GetAll)
|
||||
route.Get("/:id", m.RequirePermissions(m.P_ProjectFlockKandangsGetOne), ctrl.GetOne)
|
||||
// route.Post("/:id/closing", m.RequirePermissions(m.PermissionProjectFlockClosing), ctrl.Closing)
|
||||
// route.Get("/:id/closing/check", m.RequirePermissions(m.PermissionProjectFlockClosing), ctrl.CheckClosing)
|
||||
route.Post("/:id/closing",m.RequirePermissions(m.P_ProjectFlockKandangsClosing), ctrl.Closing)
|
||||
route.Post("/:id/closing", m.RequirePermissions(m.P_ProjectFlockKandangsClosing), ctrl.Closing)
|
||||
route.Get("/:id/closing/check", m.RequirePermissions(m.P_ProjectFlockKandangsCheckClosing), ctrl.CheckClosing)
|
||||
}
|
||||
|
||||
@@ -517,7 +517,14 @@ func (s *recordingService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
||||
return nil, transactionErr
|
||||
}
|
||||
|
||||
return s.GetOne(c, createdRecording.Id)
|
||||
created, err := s.GetOne(c, createdRecording.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if created != nil {
|
||||
s.invalidateDepreciationSnapshots(c.Context(), nil, created.ProjectFlockKandangId, created.RecordDatetime)
|
||||
}
|
||||
return created, nil
|
||||
}
|
||||
|
||||
func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.Recording, error) {
|
||||
@@ -848,6 +855,13 @@ func (s recordingService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
if err := recordingutil.AttachProductionStandards(ctx, s.Repository.DB(), false, s.Log, updatedRecording); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
invalidateFromDate := commonSvc.MinNonZeroDateOnlyUTC(recordingEntity.RecordDatetime, updatedRecording.RecordDatetime)
|
||||
s.invalidateDepreciationSnapshots(
|
||||
c.Context(),
|
||||
nil,
|
||||
updatedRecording.ProjectFlockKandangId,
|
||||
invalidateFromDate,
|
||||
)
|
||||
return updatedRecording, nil
|
||||
}
|
||||
|
||||
@@ -965,6 +979,12 @@ func (s recordingService) Approval(c *fiber.Ctx, req *validation.Approve) ([]ent
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(
|
||||
c.Context(),
|
||||
nil,
|
||||
recording.ProjectFlockKandangId,
|
||||
recording.RecordDatetime,
|
||||
)
|
||||
updated = append(updated, *recording)
|
||||
}
|
||||
|
||||
@@ -985,7 +1005,7 @@ func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
}
|
||||
note := recordingutil.RecordingNote("Delete", id)
|
||||
|
||||
return s.Repository.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
err = s.Repository.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
recording, err := s.Repository.WithTx(tx).GetByID(ctx, id, nil)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -1029,9 +1049,60 @@ func (s recordingService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
s.Log.Errorf("Failed to recalculate recordings after delete: %+v", err)
|
||||
return err
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(ctx, tx, recording.ProjectFlockKandangId, recording.RecordDatetime)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s recordingService) invalidateDepreciationSnapshots(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
projectFlockKandangID uint,
|
||||
fromDate time.Time,
|
||||
) {
|
||||
if projectFlockKandangID == 0 || fromDate.IsZero() {
|
||||
return
|
||||
}
|
||||
|
||||
targetDB := s.Repository.DB()
|
||||
if tx != nil {
|
||||
targetDB = tx
|
||||
}
|
||||
|
||||
farmIDs, err := commonSvc.ResolveProjectFlockIDsByProjectFlockKandangIDs(ctx, targetDB, []uint{projectFlockKandangID})
|
||||
if err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to resolve farm for recording depreciation invalidation (pfk=%d): %+v",
|
||||
projectFlockKandangID,
|
||||
err,
|
||||
)
|
||||
farmIDs = nil
|
||||
}
|
||||
|
||||
if len(farmIDs) == 0 {
|
||||
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, nil, fromDate); err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to invalidate depreciation snapshots globally (from=%s): %+v",
|
||||
fromDate.Format("2006-01-02"),
|
||||
err,
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, farmIDs, fromDate); err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to invalidate depreciation snapshots (farm_ids=%v, from=%s): %+v",
|
||||
farmIDs,
|
||||
fromDate.Format("2006-01-02"),
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *recordingService) resolveRecordingCategory(ctx context.Context, recording *entity.Recording) (string, error) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -675,6 +675,12 @@ func (s *purchaseService) CreateOne(c *fiber.Ctx, req *validation.CreatePurchase
|
||||
if err := s.attachLatestApproval(c.Context(), created); err != nil {
|
||||
s.Log.Warnf("Unable to attach latest approval for purchase %d: %+v", created.Id, err)
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(
|
||||
c.Context(),
|
||||
nil,
|
||||
collectPFKIDsFromPurchase(created),
|
||||
resolvePurchaseDepreciationInvalidateDate(created, created.Items, now),
|
||||
)
|
||||
|
||||
return created, nil
|
||||
}
|
||||
@@ -826,6 +832,12 @@ func (s *purchaseService) ApproveStaffPurchase(c *fiber.Ctx, id uint, req *valid
|
||||
if err := s.attachLatestApproval(c.Context(), updated); err != nil {
|
||||
s.Log.Warnf("Unable to attach latest approval for purchase %d: %+v", updated.Id, err)
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(
|
||||
c.Context(),
|
||||
nil,
|
||||
collectPFKIDsFromPurchase(updated),
|
||||
resolvePurchaseDepreciationInvalidateDate(updated, updated.Items, time.Now().UTC()),
|
||||
)
|
||||
|
||||
return updated, nil
|
||||
}
|
||||
@@ -934,6 +946,12 @@ func (s *purchaseService) ApproveManagerPurchase(c *fiber.Ctx, id uint, req *val
|
||||
if err := s.attachLatestApproval(c.Context(), updated); err != nil {
|
||||
s.Log.Warnf("Unable to attach latest approval for purchase %d: %+v", updated.Id, err)
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(
|
||||
c.Context(),
|
||||
nil,
|
||||
collectPFKIDsFromPurchase(updated),
|
||||
resolvePurchaseDepreciationInvalidateDate(updated, updated.Items, now),
|
||||
)
|
||||
|
||||
return updated, nil
|
||||
}
|
||||
@@ -1421,6 +1439,16 @@ func (s *purchaseService) ReceiveProducts(c *fiber.Ctx, id uint, req *validation
|
||||
if err := s.attachLatestApproval(c.Context(), updated); err != nil {
|
||||
s.Log.Warnf("Unable to attach latest approval for purchase %d: %+v", updated.Id, err)
|
||||
}
|
||||
invalidateFromDate := resolvePurchaseDepreciationInvalidateDate(updated, updated.Items, time.Now().UTC())
|
||||
if earliestReceived != nil {
|
||||
invalidateFromDate = commonSvc.MinNonZeroDateOnlyUTC(invalidateFromDate, *earliestReceived)
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(
|
||||
c.Context(),
|
||||
nil,
|
||||
collectPFKIDsFromPurchase(updated),
|
||||
invalidateFromDate,
|
||||
)
|
||||
|
||||
receivingPayloads := make([]ExpenseReceivingPayload, 0, len(prepared))
|
||||
for _, prep := range prepared {
|
||||
@@ -1628,6 +1656,12 @@ func (s *purchaseService) DeleteItems(c *fiber.Ctx, id uint, req *validation.Del
|
||||
if err := s.attachLatestApproval(ctx, updated); err != nil {
|
||||
s.Log.Warnf("Unable to attach latest approval for purchase %d: %+v", updated.Id, err)
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(
|
||||
ctx,
|
||||
nil,
|
||||
collectPFKIDsFromPurchaseItems(itemsToDelete),
|
||||
resolvePurchaseDepreciationInvalidateDate(purchase, itemsToDelete, time.Now().UTC()),
|
||||
)
|
||||
|
||||
return updated, nil
|
||||
}
|
||||
@@ -1721,6 +1755,12 @@ func (s *purchaseService) DeletePurchase(c *fiber.Ctx, id uint) error {
|
||||
return utils.Internal("Failed to sync expense")
|
||||
}
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(
|
||||
ctx,
|
||||
nil,
|
||||
collectPFKIDsFromPurchaseItems(itemsToDelete),
|
||||
resolvePurchaseDepreciationInvalidateDate(purchase, itemsToDelete, time.Now().UTC()),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -2391,7 +2431,17 @@ func (s *purchaseService) rejectAndReload(
|
||||
if err := s.createPurchaseApproval(c.Context(), nil, purchaseID, step, entity.ApprovalActionRejected, actorID, notes, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.loadPurchase(c.Context(), purchaseID)
|
||||
updated, err := s.loadPurchase(c.Context(), purchaseID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.invalidateDepreciationSnapshots(
|
||||
c.Context(),
|
||||
nil,
|
||||
collectPFKIDsFromPurchase(updated),
|
||||
resolvePurchaseDepreciationInvalidateDate(updated, updated.Items, time.Now().UTC()),
|
||||
)
|
||||
return updated, nil
|
||||
}
|
||||
func (s *purchaseService) loadPurchase(
|
||||
ctx context.Context,
|
||||
@@ -2522,10 +2572,17 @@ func (s *purchaseService) resolveChickinLockedItemIDsByItemID(ctx context.Contex
|
||||
}
|
||||
|
||||
func collectPFKIDsFromPurchase(p *entity.Purchase) []uint {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return collectPFKIDsFromPurchaseItems(p.Items)
|
||||
}
|
||||
|
||||
func collectPFKIDsFromPurchaseItems(items []entity.PurchaseItem) []uint {
|
||||
seen := make(map[uint]struct{})
|
||||
ids := make([]uint, 0)
|
||||
|
||||
for _, item := range p.Items {
|
||||
for _, item := range items {
|
||||
if item.ProjectFlockKandangId == nil || *item.ProjectFlockKandangId == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -2538,6 +2595,82 @@ func collectPFKIDsFromPurchase(p *entity.Purchase) []uint {
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func resolvePurchaseDepreciationInvalidateDate(
|
||||
purchase *entity.Purchase,
|
||||
items []entity.PurchaseItem,
|
||||
fallback time.Time,
|
||||
) time.Time {
|
||||
fromDate := time.Time{}
|
||||
if purchase != nil {
|
||||
fromDate = commonSvc.MinNonZeroDateOnlyUTC(fromDate, purchase.CreatedAt)
|
||||
if purchase.PoDate != nil {
|
||||
fromDate = commonSvc.MinNonZeroDateOnlyUTC(fromDate, *purchase.PoDate)
|
||||
}
|
||||
}
|
||||
for _, item := range items {
|
||||
if item.ReceivedDate == nil {
|
||||
continue
|
||||
}
|
||||
fromDate = commonSvc.MinNonZeroDateOnlyUTC(fromDate, *item.ReceivedDate)
|
||||
}
|
||||
if fromDate.IsZero() {
|
||||
fromDate = fallback
|
||||
}
|
||||
return fromDate
|
||||
}
|
||||
|
||||
func (s *purchaseService) invalidateDepreciationSnapshots(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
projectFlockKandangIDs []uint,
|
||||
fromDate time.Time,
|
||||
) {
|
||||
if fromDate.IsZero() {
|
||||
return
|
||||
}
|
||||
projectFlockKandangIDs = utils.UniqueUintSlice(projectFlockKandangIDs)
|
||||
|
||||
targetDB := s.PurchaseRepo.DB()
|
||||
if tx != nil {
|
||||
targetDB = tx
|
||||
}
|
||||
|
||||
var farmIDs []uint
|
||||
if len(projectFlockKandangIDs) > 0 {
|
||||
resolvedFarmIDs, err := commonSvc.ResolveProjectFlockIDsByProjectFlockKandangIDs(ctx, targetDB, projectFlockKandangIDs)
|
||||
if err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to resolve farm ids for purchase depreciation invalidation (pfk_ids=%v): %+v",
|
||||
projectFlockKandangIDs,
|
||||
err,
|
||||
)
|
||||
} else {
|
||||
farmIDs = resolvedFarmIDs
|
||||
}
|
||||
}
|
||||
|
||||
if len(farmIDs) == 0 {
|
||||
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, nil, fromDate); err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to invalidate depreciation snapshots globally (from=%s): %+v",
|
||||
fromDate.Format("2006-01-02"),
|
||||
err,
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := commonSvc.InvalidateFarmDepreciationSnapshotsFromDate(ctx, targetDB, farmIDs, fromDate); err != nil {
|
||||
s.Log.Warnf(
|
||||
"Failed to invalidate depreciation snapshots (farm_ids=%v, from=%s): %+v",
|
||||
farmIDs,
|
||||
fromDate.Format("2006-01-02"),
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *purchaseService) ensureProjectFlockNotClosedForPurchase(
|
||||
ctx context.Context,
|
||||
purchase *entity.Purchase,
|
||||
|
||||
@@ -90,6 +90,75 @@ func (c *RepportController) GetExpense(ctx *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (c *RepportController) GetExpenseDepreciation(ctx *fiber.Ctx) error {
|
||||
rows, meta, err := c.RepportService.GetExpenseDepreciation(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp := struct {
|
||||
Code int `json:"code"`
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Meta dto.ExpenseDepreciationMetaDTO `json:"meta"`
|
||||
Data []dto.ExpenseDepreciationRowDTO `json:"data"`
|
||||
}{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get expense depreciation report successfully",
|
||||
Meta: *meta,
|
||||
Data: rows,
|
||||
}
|
||||
|
||||
return ctx.Status(fiber.StatusOK).JSON(resp)
|
||||
}
|
||||
|
||||
func (c *RepportController) GetExpenseDepreciationManualInputs(ctx *fiber.Ctx) error {
|
||||
rows, meta, err := c.RepportService.GetExpenseDepreciationManualInputs(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp := struct {
|
||||
Code int `json:"code"`
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Meta dto.ExpenseDepreciationMetaDTO `json:"meta"`
|
||||
Data []dto.ExpenseDepreciationManualInputRowDTO `json:"data"`
|
||||
}{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get expense depreciation manual inputs successfully",
|
||||
Meta: *meta,
|
||||
Data: rows,
|
||||
}
|
||||
|
||||
return ctx.Status(fiber.StatusOK).JSON(resp)
|
||||
}
|
||||
|
||||
func (c *RepportController) UpsertExpenseDepreciationManualInput(ctx *fiber.Ctx) error {
|
||||
req := new(validation.ExpenseDepreciationManualInputUpsert)
|
||||
if err := ctx.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
if err := m.EnsureProjectFlockAccess(ctx, c.RepportService.DB(), req.ProjectFlockID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result, err := c.RepportService.UpsertExpenseDepreciationManualInput(ctx, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ctx.Status(fiber.StatusOK).JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Upsert expense depreciation manual input successfully",
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *RepportController) GetMarketing(ctx *fiber.Ctx) error {
|
||||
query := &validation.MarketingQuery{
|
||||
Page: ctx.QueryInt("page", 1),
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package dto
|
||||
|
||||
type ExpenseDepreciationFiltersDTO struct {
|
||||
AreaID string `json:"area_id"`
|
||||
LocationID string `json:"location_id"`
|
||||
ProjectFlockID string `json:"project_flock_id"`
|
||||
Period string `json:"period"`
|
||||
}
|
||||
|
||||
type ExpenseDepreciationMetaDTO struct {
|
||||
Page int `json:"page"`
|
||||
Limit int `json:"limit"`
|
||||
TotalPages int64 `json:"total_pages"`
|
||||
TotalResults int64 `json:"total_results"`
|
||||
Filters ExpenseDepreciationFiltersDTO `json:"filters"`
|
||||
}
|
||||
|
||||
type ExpenseDepreciationRowDTO struct {
|
||||
ProjectFlockID int64 `json:"project_flock_id"`
|
||||
FarmName string `json:"farm_name"`
|
||||
Period string `json:"period"`
|
||||
DepreciationPercentEffective float64 `json:"depreciation_percent_effective"`
|
||||
DepreciationValue float64 `json:"depreciation_value"`
|
||||
PulletCostDayNTotal float64 `json:"pullet_cost_day_n_total"`
|
||||
Components any `json:"components"`
|
||||
}
|
||||
|
||||
type ExpenseDepreciationManualInputRowDTO struct {
|
||||
ID int64 `json:"id"`
|
||||
ProjectFlockID int64 `json:"project_flock_id"`
|
||||
FarmName string `json:"farm_name"`
|
||||
TotalCost float64 `json:"total_cost"`
|
||||
Note *string `json:"note"`
|
||||
}
|
||||
|
||||
func NewExpenseDepreciationFiltersDTO(area, location, projectFlockID, period string) ExpenseDepreciationFiltersDTO {
|
||||
return ExpenseDepreciationFiltersDTO{
|
||||
AreaID: area,
|
||||
LocationID: location,
|
||||
ProjectFlockID: projectFlockID,
|
||||
Period: period,
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ func (RepportModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *
|
||||
purchaseSupplierRepository := repportRepo.NewPurchaseSupplierRepository(db)
|
||||
debtSupplierRepository := repportRepo.NewDebtSupplierRepository(db)
|
||||
hppPerKandangRepository := repportRepo.NewHppPerKandangRepository(db)
|
||||
expenseDepreciationRepository := repportRepo.NewExpenseDepreciationRepository(db)
|
||||
productionResultRepository := repportRepo.NewProductionResultRepository(db)
|
||||
customerPaymentRepository := repportRepo.NewCustomerPaymentRepository(db)
|
||||
customerRepository := customerRepo.NewCustomerRepository(db)
|
||||
@@ -45,7 +46,27 @@ func (RepportModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *
|
||||
|
||||
approvalSvc := approvalService.NewApprovalService(approvalRepository)
|
||||
hppSvc := approvalService.NewHppService(hppCostRepository)
|
||||
repportService := sRepport.NewRepportService(db, validate, expenseRealizationRepository, marketingDeliveryProductRepository, purchaseRepository, chickinRepository, recordingRepository, approvalSvc, hppSvc, purchaseSupplierRepository, debtSupplierRepository, hppPerKandangRepository, productionResultRepository, customerPaymentRepository, customerRepository, standardGrowthDetailRepository, productionStandardDetailRepository)
|
||||
repportService := sRepport.NewRepportService(
|
||||
db,
|
||||
validate,
|
||||
expenseRealizationRepository,
|
||||
expenseDepreciationRepository,
|
||||
marketingDeliveryProductRepository,
|
||||
purchaseRepository,
|
||||
chickinRepository,
|
||||
recordingRepository,
|
||||
approvalSvc,
|
||||
hppSvc,
|
||||
hppCostRepository,
|
||||
purchaseSupplierRepository,
|
||||
debtSupplierRepository,
|
||||
hppPerKandangRepository,
|
||||
productionResultRepository,
|
||||
customerPaymentRepository,
|
||||
customerRepository,
|
||||
standardGrowthDetailRepository,
|
||||
productionStandardDetailRepository,
|
||||
)
|
||||
userService := sUser.NewUserService(userRepository, validate)
|
||||
|
||||
RepportRoutes(router, userService, repportService)
|
||||
|
||||
@@ -0,0 +1,326 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type FarmDepreciationCandidateRow struct {
|
||||
ProjectFlockID uint
|
||||
FarmName string
|
||||
}
|
||||
|
||||
type FarmDepreciationLatestTransferRow struct {
|
||||
ProjectFlockID uint
|
||||
FarmName string
|
||||
ProjectFlockKandangID uint
|
||||
KandangID uint
|
||||
KandangName string
|
||||
HouseType *string
|
||||
SourceProjectFlockID uint
|
||||
TransferDate time.Time
|
||||
TransferQty float64
|
||||
TransferID uint
|
||||
}
|
||||
|
||||
type FarmDepreciationManualInputRow struct {
|
||||
Id uint
|
||||
ProjectFlockID uint
|
||||
FarmName string
|
||||
TotalCost float64
|
||||
Note *string
|
||||
}
|
||||
|
||||
type houseDepreciationPercentRow struct {
|
||||
HouseType string
|
||||
Day int
|
||||
DepreciationPercent float64
|
||||
}
|
||||
|
||||
type ExpenseDepreciationRepository interface {
|
||||
GetCandidateFarms(ctx context.Context, period time.Time, areaIDs, locationIDs, projectFlockIDs []int64) ([]FarmDepreciationCandidateRow, error)
|
||||
GetSnapshotsByPeriodAndFarmIDs(ctx context.Context, period time.Time, farmIDs []uint) ([]entity.FarmDepreciationSnapshot, error)
|
||||
UpsertSnapshots(ctx context.Context, rows []entity.FarmDepreciationSnapshot) error
|
||||
DeleteSnapshotsFromDate(ctx context.Context, fromDate time.Time, farmIDs []uint) error
|
||||
GetLatestTransferInputsByFarms(ctx context.Context, period time.Time, farmIDs []uint) ([]FarmDepreciationLatestTransferRow, error)
|
||||
GetDepreciationPercents(ctx context.Context, houseTypes []string, maxDay int) (map[string]map[int]float64, error)
|
||||
GetLatestManualInputsByFarms(ctx context.Context, areaIDs, locationIDs, projectFlockIDs []int64) ([]FarmDepreciationManualInputRow, error)
|
||||
UpsertManualInput(ctx context.Context, row *entity.FarmDepreciationManualInput) error
|
||||
DB() *gorm.DB
|
||||
}
|
||||
|
||||
type expenseDepreciationRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewExpenseDepreciationRepository(db *gorm.DB) ExpenseDepreciationRepository {
|
||||
return &expenseDepreciationRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *expenseDepreciationRepository) DB() *gorm.DB {
|
||||
return r.db
|
||||
}
|
||||
|
||||
func (r *expenseDepreciationRepository) GetCandidateFarms(
|
||||
ctx context.Context,
|
||||
period time.Time,
|
||||
areaIDs, locationIDs, projectFlockIDs []int64,
|
||||
) ([]FarmDepreciationCandidateRow, error) {
|
||||
rows := make([]FarmDepreciationCandidateRow, 0)
|
||||
|
||||
query := r.db.WithContext(ctx).
|
||||
Table("project_flocks AS pf").
|
||||
Select("DISTINCT pf.id AS project_flock_id, pf.flock_name AS farm_name").
|
||||
Joins("JOIN project_flock_kandangs AS pfk ON pfk.project_flock_id = pf.id").
|
||||
Where("pf.deleted_at IS NULL").
|
||||
Where("pf.category = ?", utils.ProjectFlockCategoryLaying).
|
||||
Where("(pfk.closed_at IS NULL OR DATE(pfk.closed_at) >= DATE(?))", period)
|
||||
|
||||
if len(areaIDs) > 0 {
|
||||
query = query.Where("pf.area_id IN ?", areaIDs)
|
||||
}
|
||||
if len(locationIDs) > 0 {
|
||||
query = query.Where("pf.location_id IN ?", locationIDs)
|
||||
}
|
||||
if len(projectFlockIDs) > 0 {
|
||||
query = query.Where("pf.id IN ?", projectFlockIDs)
|
||||
}
|
||||
|
||||
if err := query.Order("pf.id ASC").Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (r *expenseDepreciationRepository) GetSnapshotsByPeriodAndFarmIDs(
|
||||
ctx context.Context,
|
||||
period time.Time,
|
||||
farmIDs []uint,
|
||||
) ([]entity.FarmDepreciationSnapshot, error) {
|
||||
if len(farmIDs) == 0 {
|
||||
return []entity.FarmDepreciationSnapshot{}, nil
|
||||
}
|
||||
|
||||
rows := make([]entity.FarmDepreciationSnapshot, 0)
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("project_flock_id IN ?", farmIDs).
|
||||
Where("period_date = DATE(?)", period).
|
||||
Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (r *expenseDepreciationRepository) UpsertSnapshots(ctx context.Context, rows []entity.FarmDepreciationSnapshot) error {
|
||||
if len(rows) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return r.db.WithContext(ctx).
|
||||
Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{
|
||||
{Name: "project_flock_id"},
|
||||
{Name: "period_date"},
|
||||
},
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"depreciation_percent_effective",
|
||||
"depreciation_value",
|
||||
"pullet_cost_day_n_total",
|
||||
"components",
|
||||
"updated_at",
|
||||
}),
|
||||
}).
|
||||
Create(&rows).Error
|
||||
}
|
||||
|
||||
func (r *expenseDepreciationRepository) DeleteSnapshotsFromDate(
|
||||
ctx context.Context,
|
||||
fromDate time.Time,
|
||||
farmIDs []uint,
|
||||
) error {
|
||||
if fromDate.IsZero() {
|
||||
return nil
|
||||
}
|
||||
|
||||
query := r.db.WithContext(ctx).
|
||||
Table("farm_depreciation_snapshots").
|
||||
Where("period_date >= DATE(?)", fromDate)
|
||||
if len(farmIDs) > 0 {
|
||||
query = query.Where("project_flock_id IN ?", farmIDs)
|
||||
}
|
||||
return query.Delete(nil).Error
|
||||
}
|
||||
|
||||
func (r *expenseDepreciationRepository) GetLatestTransferInputsByFarms(
|
||||
ctx context.Context,
|
||||
period time.Time,
|
||||
farmIDs []uint,
|
||||
) ([]FarmDepreciationLatestTransferRow, error) {
|
||||
if len(farmIDs) == 0 {
|
||||
return []FarmDepreciationLatestTransferRow{}, nil
|
||||
}
|
||||
|
||||
rows := make([]FarmDepreciationLatestTransferRow, 0)
|
||||
query := `
|
||||
WITH latest_transfer_approval AS (
|
||||
SELECT a.approvable_id, a.action
|
||||
FROM approvals a
|
||||
JOIN (
|
||||
SELECT approvable_id, MAX(action_at) AS latest_action_at
|
||||
FROM approvals
|
||||
WHERE approvable_type = @approval_type
|
||||
GROUP BY approvable_id
|
||||
) la
|
||||
ON la.approvable_id = a.approvable_id
|
||||
AND la.latest_action_at = a.action_at
|
||||
WHERE a.approvable_type = @approval_type
|
||||
),
|
||||
approved_transfers AS (
|
||||
SELECT
|
||||
lt.id,
|
||||
lt.from_project_flock_id,
|
||||
lt.to_project_flock_id,
|
||||
COALESCE(DATE(lt.effective_move_date), DATE(lt.economic_cutoff_date), DATE(lt.transfer_date)) AS effective_date
|
||||
FROM laying_transfers lt
|
||||
JOIN latest_transfer_approval lta ON lta.approvable_id = lt.id
|
||||
WHERE lt.deleted_at IS NULL
|
||||
AND lt.executed_at IS NOT NULL
|
||||
AND lta.action = 'APPROVED'
|
||||
)
|
||||
SELECT DISTINCT ON (ltt.target_project_flock_kandang_id)
|
||||
pf.id AS project_flock_id,
|
||||
pf.flock_name AS farm_name,
|
||||
pfk.id AS project_flock_kandang_id,
|
||||
k.id AS kandang_id,
|
||||
k.name AS kandang_name,
|
||||
k.house_type::text AS house_type,
|
||||
at.from_project_flock_id AS source_project_flock_id,
|
||||
at.effective_date AS transfer_date,
|
||||
ltt.total_qty AS transfer_qty,
|
||||
at.id AS transfer_id
|
||||
FROM laying_transfer_targets ltt
|
||||
JOIN approved_transfers at ON at.id = ltt.laying_transfer_id
|
||||
JOIN project_flock_kandangs pfk ON pfk.id = ltt.target_project_flock_kandang_id
|
||||
JOIN project_flocks pf ON pf.id = pfk.project_flock_id
|
||||
JOIN kandangs k ON k.id = pfk.kandang_id
|
||||
WHERE ltt.deleted_at IS NULL
|
||||
AND pf.id IN @farm_ids
|
||||
AND at.effective_date <= DATE(@period_date)
|
||||
ORDER BY ltt.target_project_flock_kandang_id, at.effective_date DESC, at.id DESC
|
||||
`
|
||||
|
||||
if err := r.db.WithContext(ctx).Raw(query, map[string]any{
|
||||
"approval_type": utils.ApprovalWorkflowTransferToLaying.String(),
|
||||
"farm_ids": farmIDs,
|
||||
"period_date": period,
|
||||
}).Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (r *expenseDepreciationRepository) GetDepreciationPercents(
|
||||
ctx context.Context,
|
||||
houseTypes []string,
|
||||
maxDay int,
|
||||
) (map[string]map[int]float64, error) {
|
||||
result := make(map[string]map[int]float64)
|
||||
if len(houseTypes) == 0 || maxDay <= 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
rows := make([]houseDepreciationPercentRow, 0)
|
||||
if err := r.db.WithContext(ctx).
|
||||
Table("house_depreciation_standards").
|
||||
Select("house_type::text AS house_type, day, depreciation_percent").
|
||||
Where("house_type::text IN ?", houseTypes).
|
||||
Where("day <= ?", maxDay).
|
||||
Order("house_type ASC, day ASC").
|
||||
Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
if _, exists := result[row.HouseType]; !exists {
|
||||
result[row.HouseType] = make(map[int]float64)
|
||||
}
|
||||
result[row.HouseType][row.Day] = row.DepreciationPercent
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *expenseDepreciationRepository) GetLatestManualInputsByFarms(
|
||||
ctx context.Context,
|
||||
areaIDs, locationIDs, projectFlockIDs []int64,
|
||||
) ([]FarmDepreciationManualInputRow, error) {
|
||||
rows := make([]FarmDepreciationManualInputRow, 0)
|
||||
|
||||
query := r.db.WithContext(ctx).
|
||||
Table("farm_depreciation_manual_inputs AS fdmi").
|
||||
Select(`
|
||||
fdmi.id AS id,
|
||||
fdmi.project_flock_id AS project_flock_id,
|
||||
pf.flock_name AS farm_name,
|
||||
fdmi.total_cost AS total_cost,
|
||||
fdmi.note AS note
|
||||
`).
|
||||
Joins("JOIN project_flocks AS pf ON pf.id = fdmi.project_flock_id").
|
||||
Where("pf.deleted_at IS NULL").
|
||||
Where("pf.category = ?", utils.ProjectFlockCategoryLaying)
|
||||
|
||||
if len(areaIDs) > 0 {
|
||||
query = query.Where("pf.area_id IN ?", areaIDs)
|
||||
}
|
||||
if len(locationIDs) > 0 {
|
||||
query = query.Where("pf.location_id IN ?", locationIDs)
|
||||
}
|
||||
if len(projectFlockIDs) > 0 {
|
||||
query = query.Where("pf.id IN ?", projectFlockIDs)
|
||||
}
|
||||
|
||||
if err := query.
|
||||
Order("pf.id ASC").
|
||||
Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (r *expenseDepreciationRepository) UpsertManualInput(ctx context.Context, row *entity.FarmDepreciationManualInput) error {
|
||||
if row == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
err := r.db.WithContext(ctx).
|
||||
Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{
|
||||
{Name: "project_flock_id"},
|
||||
},
|
||||
DoUpdates: clause.Assignments(map[string]any{
|
||||
"total_cost": row.TotalCost,
|
||||
"note": row.Note,
|
||||
"updated_at": now,
|
||||
}),
|
||||
}).
|
||||
Create(row).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return r.db.WithContext(ctx).
|
||||
Table("farm_depreciation_manual_inputs").
|
||||
Select("id, project_flock_id, total_cost, note").
|
||||
Where("project_flock_id = ?", row.ProjectFlockId).
|
||||
Take(row).Error
|
||||
}
|
||||
@@ -16,6 +16,9 @@ func RepportRoutes(v1 fiber.Router, u user.UserService, s repport.RepportService
|
||||
route.Use(m.Auth(u))
|
||||
|
||||
route.Get("/expense", m.RequirePermissions(m.P_ReportExpenseGetAll), ctrl.GetExpense)
|
||||
route.Get("/expense/depreciation", ctrl.GetExpenseDepreciation)
|
||||
route.Get("/expense/depreciation/manual-inputs", ctrl.GetExpenseDepreciationManualInputs)
|
||||
route.Put("/expense/depreciation/manual-inputs", ctrl.UpsertExpenseDepreciationManualInput)
|
||||
route.Get("/marketing", m.RequirePermissions(m.P_ReportDeliveryGetAll), ctrl.GetMarketing)
|
||||
route.Get("/purchase-supplier", m.RequirePermissions(m.P_ReportPurchaseSupplierGetAll), ctrl.GetPurchaseSupplier)
|
||||
route.Get("/debt-supplier", m.RequirePermissions(m.P_ReportDebtSupplierGetAll), ctrl.GetDebtSupplier)
|
||||
|
||||
@@ -42,6 +42,9 @@ import (
|
||||
|
||||
type RepportService interface {
|
||||
GetExpense(ctx *fiber.Ctx, params *validation.ExpenseQuery) ([]dto.RepportExpenseListDTO, int64, error)
|
||||
GetExpenseDepreciation(ctx *fiber.Ctx) ([]dto.ExpenseDepreciationRowDTO, *dto.ExpenseDepreciationMetaDTO, error)
|
||||
GetExpenseDepreciationManualInputs(ctx *fiber.Ctx) ([]dto.ExpenseDepreciationManualInputRowDTO, *dto.ExpenseDepreciationMetaDTO, error)
|
||||
UpsertExpenseDepreciationManualInput(ctx *fiber.Ctx, req *validation.ExpenseDepreciationManualInputUpsert) (*dto.ExpenseDepreciationManualInputRowDTO, error)
|
||||
GetMarketing(ctx *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingItemDTO, int64, error)
|
||||
GetPurchaseSupplier(ctx *fiber.Ctx, params *validation.PurchaseSupplierQuery) ([]dto.PurchaseSupplierDTO, int64, error)
|
||||
GetDebtSupplier(ctx *fiber.Ctx, params *validation.DebtSupplierQuery) ([]dto.DebtSupplierDTO, int64, error)
|
||||
@@ -56,12 +59,14 @@ type repportService struct {
|
||||
Validate *validator.Validate
|
||||
db *gorm.DB
|
||||
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
|
||||
ExpenseDepreciationRepo repportRepo.ExpenseDepreciationRepository
|
||||
MarketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository
|
||||
PurchaseRepo purchaseRepo.PurchaseRepository
|
||||
ChickinRepo chickinRepo.ProjectChickinRepository
|
||||
RecordingRepo recordingRepo.RecordingRepository
|
||||
ApprovalSvc approvalService.ApprovalService
|
||||
HppSvc approvalService.HppService
|
||||
HppCostRepo commonRepo.HppCostRepository
|
||||
PurchaseSupplierRepo repportRepo.PurchaseSupplierRepository
|
||||
DebtSupplierRepo repportRepo.DebtSupplierRepository
|
||||
HppPerKandangRepo repportRepo.HppPerKandangRepository
|
||||
@@ -85,12 +90,14 @@ func NewRepportService(
|
||||
db *gorm.DB,
|
||||
validate *validator.Validate,
|
||||
expenseRealizationRepo expenseRepo.ExpenseRealizationRepository,
|
||||
expenseDepreciationRepo repportRepo.ExpenseDepreciationRepository,
|
||||
marketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository,
|
||||
purchaseRepo purchaseRepo.PurchaseRepository,
|
||||
chickinRepo chickinRepo.ProjectChickinRepository,
|
||||
recordingRepo recordingRepo.RecordingRepository,
|
||||
approvalSvc approvalService.ApprovalService,
|
||||
hppSvc approvalService.HppService,
|
||||
hppCostRepo commonRepo.HppCostRepository,
|
||||
purchaseSupplierRepo repportRepo.PurchaseSupplierRepository,
|
||||
debtSupplierRepo repportRepo.DebtSupplierRepository,
|
||||
hppPerKandangRepo repportRepo.HppPerKandangRepository,
|
||||
@@ -105,12 +112,14 @@ func NewRepportService(
|
||||
Validate: validate,
|
||||
db: db,
|
||||
ExpenseRealizationRepo: expenseRealizationRepo,
|
||||
ExpenseDepreciationRepo: expenseDepreciationRepo,
|
||||
MarketingDeliveryRepo: marketingDeliveryRepo,
|
||||
PurchaseRepo: purchaseRepo,
|
||||
ChickinRepo: chickinRepo,
|
||||
RecordingRepo: recordingRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
HppSvc: hppSvc,
|
||||
HppCostRepo: hppCostRepo,
|
||||
PurchaseSupplierRepo: purchaseSupplierRepo,
|
||||
DebtSupplierRepo: debtSupplierRepo,
|
||||
HppPerKandangRepo: hppPerKandangRepo,
|
||||
@@ -164,6 +173,495 @@ func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.ExpenseQuer
|
||||
return result, total, nil
|
||||
}
|
||||
|
||||
func (s *repportService) GetExpenseDepreciation(ctx *fiber.Ctx) ([]dto.ExpenseDepreciationRowDTO, *dto.ExpenseDepreciationMetaDTO, error) {
|
||||
params, filters, err := s.parseExpenseDepreciationQuery(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
if s.ExpenseDepreciationRepo == nil {
|
||||
return nil, nil, fiber.NewError(fiber.StatusInternalServerError, "expense depreciation repository is not configured")
|
||||
}
|
||||
|
||||
location, err := time.LoadLocation("Asia/Jakarta")
|
||||
if err != nil {
|
||||
return nil, nil, fiber.NewError(fiber.StatusInternalServerError, "failed to load timezone configuration")
|
||||
}
|
||||
periodDate, err := time.ParseInLocation("2006-01-02", params.Period, location)
|
||||
if err != nil {
|
||||
return nil, nil, fiber.NewError(fiber.StatusBadRequest, "period must follow format YYYY-MM-DD")
|
||||
}
|
||||
|
||||
candidateRows, err := s.ExpenseDepreciationRepo.GetCandidateFarms(
|
||||
ctx.Context(),
|
||||
periodDate,
|
||||
params.AreaIDs,
|
||||
params.LocationIDs,
|
||||
params.ProjectFlockIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
limit := params.Limit
|
||||
if limit <= 0 {
|
||||
limit = 10
|
||||
}
|
||||
if len(candidateRows) == 0 {
|
||||
meta := &dto.ExpenseDepreciationMetaDTO{
|
||||
Page: params.Page,
|
||||
Limit: limit,
|
||||
TotalPages: 1,
|
||||
TotalResults: 0,
|
||||
Filters: filters,
|
||||
}
|
||||
return []dto.ExpenseDepreciationRowDTO{}, meta, nil
|
||||
}
|
||||
|
||||
farmIDs := make([]uint, 0, len(candidateRows))
|
||||
farmNameByID := make(map[uint]string, len(candidateRows))
|
||||
for _, row := range candidateRows {
|
||||
farmIDs = append(farmIDs, row.ProjectFlockID)
|
||||
farmNameByID[row.ProjectFlockID] = row.FarmName
|
||||
}
|
||||
|
||||
snapshots, err := s.ExpenseDepreciationRepo.GetSnapshotsByPeriodAndFarmIDs(ctx.Context(), periodDate, farmIDs)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
snapshotByFarmID := make(map[uint]entity.FarmDepreciationSnapshot, len(snapshots))
|
||||
for _, row := range snapshots {
|
||||
snapshotByFarmID[row.ProjectFlockId] = row
|
||||
}
|
||||
|
||||
missingFarmIDs := make([]uint, 0)
|
||||
for _, farmID := range farmIDs {
|
||||
if _, exists := snapshotByFarmID[farmID]; exists {
|
||||
continue
|
||||
}
|
||||
missingFarmIDs = append(missingFarmIDs, farmID)
|
||||
}
|
||||
|
||||
if len(missingFarmIDs) > 0 {
|
||||
computedSnapshots, computeErr := s.computeExpenseDepreciationSnapshots(ctx.Context(), periodDate, missingFarmIDs, farmNameByID)
|
||||
if computeErr != nil {
|
||||
return nil, nil, computeErr
|
||||
}
|
||||
if len(computedSnapshots) > 0 {
|
||||
if err := s.ExpenseDepreciationRepo.UpsertSnapshots(ctx.Context(), computedSnapshots); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, row := range computedSnapshots {
|
||||
snapshotByFarmID[row.ProjectFlockId] = row
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rows := make([]dto.ExpenseDepreciationRowDTO, 0, len(candidateRows))
|
||||
for _, candidate := range candidateRows {
|
||||
snapshot, exists := snapshotByFarmID[candidate.ProjectFlockID]
|
||||
if !exists {
|
||||
rows = append(rows, dto.ExpenseDepreciationRowDTO{
|
||||
ProjectFlockID: int64(candidate.ProjectFlockID),
|
||||
FarmName: candidate.FarmName,
|
||||
Period: params.Period,
|
||||
DepreciationPercentEffective: 0,
|
||||
DepreciationValue: 0,
|
||||
PulletCostDayNTotal: 0,
|
||||
Components: map[string]any{},
|
||||
})
|
||||
continue
|
||||
}
|
||||
rows = append(rows, dto.ExpenseDepreciationRowDTO{
|
||||
ProjectFlockID: int64(snapshot.ProjectFlockId),
|
||||
FarmName: candidate.FarmName,
|
||||
Period: params.Period,
|
||||
DepreciationPercentEffective: snapshot.DepreciationPercentEffective,
|
||||
DepreciationValue: snapshot.DepreciationValue,
|
||||
PulletCostDayNTotal: snapshot.PulletCostDayNTotal,
|
||||
Components: parseSnapshotComponents(snapshot.Components),
|
||||
})
|
||||
}
|
||||
|
||||
totalResults := int64(len(rows))
|
||||
totalPages := int64(0)
|
||||
if totalResults > 0 {
|
||||
totalPages = int64(math.Ceil(float64(totalResults) / float64(limit)))
|
||||
}
|
||||
if totalPages == 0 {
|
||||
totalPages = 1
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * limit
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
if offset > len(rows) {
|
||||
offset = len(rows)
|
||||
}
|
||||
end := offset + limit
|
||||
if end > len(rows) {
|
||||
end = len(rows)
|
||||
}
|
||||
|
||||
meta := &dto.ExpenseDepreciationMetaDTO{
|
||||
Page: params.Page,
|
||||
Limit: limit,
|
||||
TotalPages: totalPages,
|
||||
TotalResults: totalResults,
|
||||
Filters: filters,
|
||||
}
|
||||
|
||||
return rows[offset:end], meta, nil
|
||||
}
|
||||
|
||||
func (s *repportService) GetExpenseDepreciationManualInputs(ctx *fiber.Ctx) ([]dto.ExpenseDepreciationManualInputRowDTO, *dto.ExpenseDepreciationMetaDTO, error) {
|
||||
params, filters, err := s.parseExpenseDepreciationQuery(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if s.ExpenseDepreciationRepo == nil {
|
||||
return nil, nil, fiber.NewError(fiber.StatusInternalServerError, "expense depreciation repository is not configured")
|
||||
}
|
||||
|
||||
repoRows, err := s.ExpenseDepreciationRepo.GetLatestManualInputsByFarms(
|
||||
ctx.Context(),
|
||||
params.AreaIDs,
|
||||
params.LocationIDs,
|
||||
params.ProjectFlockIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
rows := make([]dto.ExpenseDepreciationManualInputRowDTO, 0, len(repoRows))
|
||||
for _, row := range repoRows {
|
||||
rows = append(rows, dto.ExpenseDepreciationManualInputRowDTO{
|
||||
ID: int64(row.Id),
|
||||
ProjectFlockID: int64(row.ProjectFlockID),
|
||||
FarmName: row.FarmName,
|
||||
TotalCost: row.TotalCost,
|
||||
Note: row.Note,
|
||||
})
|
||||
}
|
||||
|
||||
limit := params.Limit
|
||||
if limit <= 0 {
|
||||
limit = 10
|
||||
}
|
||||
totalResults := int64(len(rows))
|
||||
totalPages := int64(0)
|
||||
if totalResults > 0 {
|
||||
totalPages = int64(math.Ceil(float64(totalResults) / float64(limit)))
|
||||
}
|
||||
if totalPages == 0 {
|
||||
totalPages = 1
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * limit
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
if offset > len(rows) {
|
||||
offset = len(rows)
|
||||
}
|
||||
end := offset + limit
|
||||
if end > len(rows) {
|
||||
end = len(rows)
|
||||
}
|
||||
|
||||
meta := &dto.ExpenseDepreciationMetaDTO{
|
||||
Page: params.Page,
|
||||
Limit: limit,
|
||||
TotalPages: totalPages,
|
||||
TotalResults: totalResults,
|
||||
Filters: filters,
|
||||
}
|
||||
|
||||
return rows[offset:end], meta, nil
|
||||
}
|
||||
|
||||
func (s *repportService) UpsertExpenseDepreciationManualInput(ctx *fiber.Ctx, req *validation.ExpenseDepreciationManualInputUpsert) (*dto.ExpenseDepreciationManualInputRowDTO, error) {
|
||||
if req == nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, "request is required")
|
||||
}
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
if s.ExpenseDepreciationRepo == nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "expense depreciation repository is not configured")
|
||||
}
|
||||
|
||||
row := entity.FarmDepreciationManualInput{
|
||||
ProjectFlockId: req.ProjectFlockID,
|
||||
TotalCost: req.TotalCost,
|
||||
Note: req.Note,
|
||||
}
|
||||
if err := s.ExpenseDepreciationRepo.UpsertManualInput(ctx.Context(), &row); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response := &dto.ExpenseDepreciationManualInputRowDTO{
|
||||
ID: int64(row.Id),
|
||||
ProjectFlockID: int64(row.ProjectFlockId),
|
||||
TotalCost: row.TotalCost,
|
||||
Note: row.Note,
|
||||
}
|
||||
|
||||
listRows, listErr := s.ExpenseDepreciationRepo.GetLatestManualInputsByFarms(
|
||||
ctx.Context(),
|
||||
nil,
|
||||
nil,
|
||||
[]int64{int64(row.ProjectFlockId)},
|
||||
)
|
||||
if listErr == nil {
|
||||
for _, listRow := range listRows {
|
||||
if listRow.ProjectFlockID == row.ProjectFlockId {
|
||||
response.FarmName = listRow.FarmName
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
type depreciationKandangComponent struct {
|
||||
ProjectFlockKandangID uint `json:"project_flock_kandang_id"`
|
||||
KandangID uint `json:"kandang_id"`
|
||||
KandangName string `json:"kandang_name"`
|
||||
TransferID uint `json:"transfer_id"`
|
||||
TransferDate string `json:"transfer_date"`
|
||||
SourceProjectFlockID uint `json:"source_project_flock_id"`
|
||||
HouseType string `json:"house_type"`
|
||||
DayN int `json:"day_n"`
|
||||
DepreciationPercent float64 `json:"depreciation_percent"`
|
||||
TransferQty float64 `json:"transfer_qty"`
|
||||
PulletCostDayN float64 `json:"pullet_cost_day_n"`
|
||||
DepreciationValue float64 `json:"depreciation_value"`
|
||||
}
|
||||
|
||||
type depreciationFarmComponents struct {
|
||||
KandangCount int `json:"kandang_count"`
|
||||
Kandang []depreciationKandangComponent `json:"kandang"`
|
||||
}
|
||||
|
||||
func (s *repportService) computeExpenseDepreciationSnapshots(
|
||||
ctx context.Context,
|
||||
periodDate time.Time,
|
||||
farmIDs []uint,
|
||||
farmNameByID map[uint]string,
|
||||
) ([]entity.FarmDepreciationSnapshot, error) {
|
||||
if len(farmIDs) == 0 {
|
||||
return []entity.FarmDepreciationSnapshot{}, nil
|
||||
}
|
||||
|
||||
inputRows, err := s.ExpenseDepreciationRepo.GetLatestTransferInputsByFarms(ctx, periodDate, farmIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
groupedByFarm := make(map[uint][]repportRepo.FarmDepreciationLatestTransferRow, len(farmIDs))
|
||||
houseTypeSet := make(map[string]struct{})
|
||||
maxDay := 0
|
||||
|
||||
for _, row := range inputRows {
|
||||
groupedByFarm[row.ProjectFlockID] = append(groupedByFarm[row.ProjectFlockID], row)
|
||||
dayN := depreciationDayNumber(row.TransferDate, periodDate)
|
||||
if dayN > maxDay {
|
||||
maxDay = dayN
|
||||
}
|
||||
houseType := strings.TrimSpace(strings.ToLower(valueOrEmptyString(row.HouseType)))
|
||||
if houseType != "" {
|
||||
houseTypeSet[houseType] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
houseTypes := make([]string, 0, len(houseTypeSet))
|
||||
for houseType := range houseTypeSet {
|
||||
houseTypes = append(houseTypes, houseType)
|
||||
}
|
||||
sort.Strings(houseTypes)
|
||||
|
||||
percentByHouseType, err := s.ExpenseDepreciationRepo.GetDepreciationPercents(ctx, houseTypes, maxDay)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
type sourceCostCacheItem struct {
|
||||
totalDepCost float64
|
||||
}
|
||||
sourceCostCache := make(map[string]sourceCostCacheItem)
|
||||
sourcePopulationCache := make(map[uint]float64)
|
||||
|
||||
result := make([]entity.FarmDepreciationSnapshot, 0, len(farmIDs))
|
||||
for _, farmID := range farmIDs {
|
||||
farmRows := groupedByFarm[farmID]
|
||||
components := depreciationFarmComponents{
|
||||
KandangCount: len(farmRows),
|
||||
Kandang: make([]depreciationKandangComponent, 0, len(farmRows)),
|
||||
}
|
||||
|
||||
totalDepreciationValue := 0.0
|
||||
totalPulletCostDayN := 0.0
|
||||
for _, row := range farmRows {
|
||||
dayN := depreciationDayNumber(row.TransferDate, periodDate)
|
||||
houseType := strings.TrimSpace(strings.ToLower(valueOrEmptyString(row.HouseType)))
|
||||
|
||||
transferDateKey := row.TransferDate.Format("2006-01-02")
|
||||
cacheKey := fmt.Sprintf("%d|%s", row.SourceProjectFlockID, transferDateKey)
|
||||
cached, exists := sourceCostCache[cacheKey]
|
||||
if !exists {
|
||||
endOfDay := row.TransferDate.Add(24 * time.Hour)
|
||||
sourceDepCost, calcErr := s.HppSvc.GetTotalDepresiasiFlockGrowing(row.SourceProjectFlockID, &endOfDay)
|
||||
if calcErr != nil {
|
||||
return nil, calcErr
|
||||
}
|
||||
cached = sourceCostCacheItem{totalDepCost: sourceDepCost}
|
||||
sourceCostCache[cacheKey] = cached
|
||||
}
|
||||
|
||||
sourcePopulation, popExists := sourcePopulationCache[row.SourceProjectFlockID]
|
||||
if !popExists {
|
||||
if s.HppCostRepo == nil {
|
||||
sourcePopulation = 0
|
||||
} else {
|
||||
kandangIDs, idsErr := s.HppCostRepo.GetProjectFlockKandangIDs(ctx, row.SourceProjectFlockID)
|
||||
if idsErr != nil {
|
||||
return nil, idsErr
|
||||
}
|
||||
population, popErr := s.HppCostRepo.GetTotalPopulation(ctx, kandangIDs)
|
||||
if popErr != nil {
|
||||
return nil, popErr
|
||||
}
|
||||
sourcePopulation = population
|
||||
}
|
||||
sourcePopulationCache[row.SourceProjectFlockID] = sourcePopulation
|
||||
}
|
||||
|
||||
initialPulletCost := 0.0
|
||||
if sourcePopulation > 0 {
|
||||
initialPulletCost = (cached.totalDepCost * row.TransferQty) / sourcePopulation
|
||||
}
|
||||
|
||||
pulletCostDayN, depreciationValue, depreciationPercent := calculateDepreciationAtDayN(
|
||||
initialPulletCost,
|
||||
dayN,
|
||||
houseType,
|
||||
percentByHouseType,
|
||||
)
|
||||
|
||||
totalPulletCostDayN += pulletCostDayN
|
||||
totalDepreciationValue += depreciationValue
|
||||
|
||||
components.Kandang = append(components.Kandang, depreciationKandangComponent{
|
||||
ProjectFlockKandangID: row.ProjectFlockKandangID,
|
||||
KandangID: row.KandangID,
|
||||
KandangName: row.KandangName,
|
||||
TransferID: row.TransferID,
|
||||
TransferDate: row.TransferDate.Format("2006-01-02"),
|
||||
SourceProjectFlockID: row.SourceProjectFlockID,
|
||||
HouseType: houseType,
|
||||
DayN: dayN,
|
||||
DepreciationPercent: depreciationPercent,
|
||||
TransferQty: row.TransferQty,
|
||||
PulletCostDayN: pulletCostDayN,
|
||||
DepreciationValue: depreciationValue,
|
||||
})
|
||||
}
|
||||
|
||||
effectivePercent := 0.0
|
||||
effectivePercent = calculateEffectiveDepreciationPercent(totalDepreciationValue, totalPulletCostDayN)
|
||||
|
||||
componentsJSON, marshalErr := json.Marshal(components)
|
||||
if marshalErr != nil {
|
||||
return nil, marshalErr
|
||||
}
|
||||
|
||||
result = append(result, entity.FarmDepreciationSnapshot{
|
||||
ProjectFlockId: farmID,
|
||||
PeriodDate: periodDate,
|
||||
DepreciationPercentEffective: effectivePercent,
|
||||
DepreciationValue: totalDepreciationValue,
|
||||
PulletCostDayNTotal: totalPulletCostDayN,
|
||||
Components: componentsJSON,
|
||||
})
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func depreciationDayNumber(transferDate time.Time, periodDate time.Time) int {
|
||||
transfer := time.Date(transferDate.Year(), transferDate.Month(), transferDate.Day(), 0, 0, 0, 0, transferDate.Location())
|
||||
period := time.Date(periodDate.Year(), periodDate.Month(), periodDate.Day(), 0, 0, 0, 0, periodDate.Location())
|
||||
if period.Before(transfer) {
|
||||
return 0
|
||||
}
|
||||
return int(period.Sub(transfer).Hours()/24) + 1
|
||||
}
|
||||
|
||||
func calculateDepreciationAtDayN(
|
||||
initialPulletCost float64,
|
||||
dayN int,
|
||||
houseType string,
|
||||
percentByHouseType map[string]map[int]float64,
|
||||
) (float64, float64, float64) {
|
||||
if initialPulletCost <= 0 || dayN <= 0 || houseType == "" {
|
||||
return 0, 0, 0
|
||||
}
|
||||
|
||||
housePercent, exists := percentByHouseType[houseType]
|
||||
if !exists {
|
||||
return 0, 0, 0
|
||||
}
|
||||
|
||||
current := initialPulletCost
|
||||
pulletCostDayN := 0.0
|
||||
depreciationValue := 0.0
|
||||
depreciationPercent := 0.0
|
||||
for day := 1; day <= dayN; day++ {
|
||||
pct := housePercent[day]
|
||||
dep := current * (pct / 100)
|
||||
if day == dayN {
|
||||
pulletCostDayN = current
|
||||
depreciationValue = dep
|
||||
depreciationPercent = pct
|
||||
}
|
||||
current -= dep
|
||||
if current < 0 {
|
||||
current = 0
|
||||
}
|
||||
}
|
||||
return pulletCostDayN, depreciationValue, depreciationPercent
|
||||
}
|
||||
|
||||
func calculateEffectiveDepreciationPercent(totalDepreciationValue, totalPulletCostDayN float64) float64 {
|
||||
if totalPulletCostDayN <= 0 {
|
||||
return 0
|
||||
}
|
||||
return (totalDepreciationValue / totalPulletCostDayN) * 100
|
||||
}
|
||||
|
||||
func parseSnapshotComponents(raw []byte) any {
|
||||
if len(raw) == 0 {
|
||||
return map[string]any{}
|
||||
}
|
||||
var out any
|
||||
if err := json.Unmarshal(raw, &out); err != nil {
|
||||
return map[string]any{}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func valueOrEmptyString(v *string) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingItemDTO, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
@@ -2133,6 +2631,84 @@ func (s *repportService) parseHppPerKandangQuery(ctx *fiber.Ctx) (*validation.Hp
|
||||
return params, filters, nil
|
||||
}
|
||||
|
||||
func (s *repportService) parseExpenseDepreciationQuery(ctx *fiber.Ctx) (*validation.ExpenseDepreciationQuery, dto.ExpenseDepreciationFiltersDTO, error) {
|
||||
page := ctx.QueryInt("page", 1)
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
limit := ctx.QueryInt("limit", 10)
|
||||
if limit < 1 {
|
||||
limit = 10
|
||||
}
|
||||
|
||||
rawArea := ctx.Query("area_id", "")
|
||||
rawLocation := ctx.Query("location_id", "")
|
||||
rawProjectFlock := ctx.Query("project_flock_id", "")
|
||||
period := strings.TrimSpace(ctx.Query("period", ""))
|
||||
|
||||
areaIDs, err := parseCommaSeparatedInt64s(rawArea)
|
||||
if err != nil {
|
||||
return nil, dto.ExpenseDepreciationFiltersDTO{}, fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
locationIDs, err := parseCommaSeparatedInt64s(rawLocation)
|
||||
if err != nil {
|
||||
return nil, dto.ExpenseDepreciationFiltersDTO{}, fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
projectFlockIDs, err := parseCommaSeparatedInt64s(rawProjectFlock)
|
||||
if err != nil {
|
||||
return nil, dto.ExpenseDepreciationFiltersDTO{}, fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
locationScope, err := m.ResolveLocationScope(ctx, s.ExpenseRealizationRepo.DB())
|
||||
if err != nil {
|
||||
return nil, dto.ExpenseDepreciationFiltersDTO{}, err
|
||||
}
|
||||
areaScope, err := m.ResolveAreaScope(ctx, s.ExpenseRealizationRepo.DB())
|
||||
if err != nil {
|
||||
return nil, dto.ExpenseDepreciationFiltersDTO{}, err
|
||||
}
|
||||
|
||||
if locationScope.Restrict {
|
||||
allowed := toInt64Slice(locationScope.IDs)
|
||||
if len(allowed) == 0 {
|
||||
locationIDs = []int64{-1}
|
||||
} else if len(locationIDs) > 0 {
|
||||
locationIDs = intersectInt64(locationIDs, allowed)
|
||||
} else {
|
||||
locationIDs = allowed
|
||||
}
|
||||
}
|
||||
|
||||
if areaScope.Restrict {
|
||||
allowed := toInt64Slice(areaScope.IDs)
|
||||
if len(allowed) == 0 {
|
||||
areaIDs = []int64{-1}
|
||||
} else if len(areaIDs) > 0 {
|
||||
areaIDs = intersectInt64(areaIDs, allowed)
|
||||
} else {
|
||||
areaIDs = allowed
|
||||
}
|
||||
}
|
||||
|
||||
params := &validation.ExpenseDepreciationQuery{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
Period: period,
|
||||
ProjectFlockIDs: projectFlockIDs,
|
||||
AreaIDs: areaIDs,
|
||||
LocationIDs: locationIDs,
|
||||
}
|
||||
|
||||
filters := dto.NewExpenseDepreciationFiltersDTO(
|
||||
rawArea,
|
||||
rawLocation,
|
||||
rawProjectFlock,
|
||||
period,
|
||||
)
|
||||
|
||||
return params, filters, nil
|
||||
}
|
||||
|
||||
func parseCommaSeparatedInt64s(raw string) ([]int64, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
|
||||
@@ -75,6 +75,21 @@ type HppPerKandangQuery struct {
|
||||
WeightMax *float64 `query:"-"`
|
||||
}
|
||||
|
||||
type ExpenseDepreciationQuery struct {
|
||||
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,min=1,max=1000,gt=0"`
|
||||
Period string `query:"period" validate:"required,datetime=2006-01-02"`
|
||||
ProjectFlockIDs []int64 `query:"-"`
|
||||
AreaIDs []int64 `query:"-"`
|
||||
LocationIDs []int64 `query:"-"`
|
||||
}
|
||||
|
||||
type ExpenseDepreciationManualInputUpsert struct {
|
||||
ProjectFlockID uint `json:"project_flock_id" validate:"required,gt=0"`
|
||||
TotalCost float64 `json:"total_cost" validate:"required,gte=0"`
|
||||
Note *string `json:"note" validate:"omitempty,max=1000"`
|
||||
}
|
||||
|
||||
type ProductionResultQuery struct {
|
||||
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
|
||||
Limit int `query:"limit" validate:"omitempty,min=1,gt=0"`
|
||||
|
||||
Reference in New Issue
Block a user