diff --git a/internal/modules/expenses/dto/expense.dto.go b/internal/modules/expenses/dto/expense.dto.go index 129c2e96..30cecd99 100644 --- a/internal/modules/expenses/dto/expense.dto.go +++ b/internal/modules/expenses/dto/expense.dto.go @@ -9,6 +9,7 @@ import ( nonstockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks/dto" supplierDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/dto" userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto" + "gitlab.com/mbugroup/lti-api.git/internal/utils" ) // === DTO Structs === @@ -32,6 +33,7 @@ type ExpenseBaseDTO struct { type ExpenseListDTO struct { ExpenseBaseDTO + GrandTotal float64 `json:"grand_total"` CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` @@ -140,8 +142,11 @@ func ToExpenseListDTO(e entity.Expense) ExpenseListDTO { latestApproval = &mapped } + grandTotal := calculateGrandTotal(&e) + return ExpenseListDTO{ ExpenseBaseDTO: ToExpenseBaseDTO(&e), + GrandTotal: grandTotal, CreatedUser: createdUser, CreatedAt: e.CreatedAt, UpdatedAt: e.UpdatedAt, @@ -344,3 +349,25 @@ func ToKandangGroupDTO(pengajuans []ExpenseNonstockDTO, realisasi []ExpenseReali return kandangs } + +func calculateGrandTotal(expense *entity.Expense) float64 { + + useRealization := expense.LatestApproval != nil && expense.LatestApproval.StepNumber >= uint16(utils.ExpenseStepRealisasi) + + if useRealization { + + var total float64 + for _, ns := range expense.Nonstocks { + if ns.Realization != nil { + total += ns.Realization.Qty * ns.Realization.Price + } + } + return total + } + + var total float64 + for _, ns := range expense.Nonstocks { + total += ns.Qty * ns.Price + } + return total +} diff --git a/internal/modules/expenses/services/expense.service.go b/internal/modules/expenses/services/expense.service.go index 50646ed6..3e50da26 100644 --- a/internal/modules/expenses/services/expense.service.go +++ b/internal/modules/expenses/services/expense.service.go @@ -211,6 +211,7 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen if !hasKandang && req.Category == string(utils.ExpenseCategoryBOP) { projectFlockRepoTx := projectFlockKandangRepo.NewProjectflockRepository(dbTransaction) activeProjectFlocks, err := projectFlockRepoTx.GetActiveByLocationID(c.Context(), req.LocationID) + if err != nil { return fiber.NewError(fiber.StatusInternalServerError, "Failed to get active project flocks for location") }