feat(BE): add grand total calculation to ExpenseListDTO and update CreateOne method in expense service

This commit is contained in:
aguhh18
2026-01-06 11:12:41 +07:00
parent c9ebd88e9d
commit d8fb427734
2 changed files with 28 additions and 0 deletions
@@ -9,6 +9,7 @@ import (
nonstockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks/dto" nonstockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks/dto"
supplierDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/dto" supplierDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/dto"
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto" userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
) )
// === DTO Structs === // === DTO Structs ===
@@ -32,6 +33,7 @@ type ExpenseBaseDTO struct {
type ExpenseListDTO struct { type ExpenseListDTO struct {
ExpenseBaseDTO ExpenseBaseDTO
GrandTotal float64 `json:"grand_total"`
CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"` CreatedUser *userDTO.UserRelationDTO `json:"created_user,omitempty"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"` UpdatedAt time.Time `json:"updated_at"`
@@ -140,8 +142,11 @@ func ToExpenseListDTO(e entity.Expense) ExpenseListDTO {
latestApproval = &mapped latestApproval = &mapped
} }
grandTotal := calculateGrandTotal(&e)
return ExpenseListDTO{ return ExpenseListDTO{
ExpenseBaseDTO: ToExpenseBaseDTO(&e), ExpenseBaseDTO: ToExpenseBaseDTO(&e),
GrandTotal: grandTotal,
CreatedUser: createdUser, CreatedUser: createdUser,
CreatedAt: e.CreatedAt, CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt, UpdatedAt: e.UpdatedAt,
@@ -344,3 +349,25 @@ func ToKandangGroupDTO(pengajuans []ExpenseNonstockDTO, realisasi []ExpenseReali
return kandangs 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
}
@@ -211,6 +211,7 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
if !hasKandang && req.Category == string(utils.ExpenseCategoryBOP) { if !hasKandang && req.Category == string(utils.ExpenseCategoryBOP) {
projectFlockRepoTx := projectFlockKandangRepo.NewProjectflockRepository(dbTransaction) projectFlockRepoTx := projectFlockKandangRepo.NewProjectflockRepository(dbTransaction)
activeProjectFlocks, err := projectFlockRepoTx.GetActiveByLocationID(c.Context(), req.LocationID) activeProjectFlocks, err := projectFlockRepoTx.GetActiveByLocationID(c.Context(), req.LocationID)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, "Failed to get active project flocks for location") return fiber.NewError(fiber.StatusInternalServerError, "Failed to get active project flocks for location")
} }