mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat(BE): add grand total calculation to ExpenseListDTO and update CreateOne method in expense service
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user