diff --git a/internal/modules/expenses/services/expense.service.go b/internal/modules/expenses/services/expense.service.go index dbfb00c2..24ba4f2e 100644 --- a/internal/modules/expenses/services/expense.service.go +++ b/internal/modules/expenses/services/expense.service.go @@ -213,7 +213,7 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen var projectFlockKandangId *uint64 - if req.Category == "BOP" { + if req.Category == string(utils.ExpenseCategoryBOP) { projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID)) if err != nil { @@ -230,10 +230,10 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen nonstockId := costItem.NonstockID var kandangId *uint64 - if req.Category == "NON-BOP" { + if req.Category == string(utils.ExpenseCategoryNonBOP) { id := uint64(expenseNonstock.KandangID) kandangId = &id - } else if req.Category == "BOP" { + } else if req.Category == string(utils.ExpenseCategoryBOP) { if projectFlockKandangId != nil { kandangId = &expenseNonstock.KandangID } @@ -385,7 +385,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) } if categoryChanged { - if currentExpense.Category == "BOP" && newCategory == "NON-BOP" { + if currentExpense.Category == string(utils.ExpenseCategoryBOP) && newCategory == string(utils.ExpenseCategoryNonBOP) { var existingExpenseNonstocks []entity.ExpenseNonstock if err := tx.Where("expense_id = ?", id).Find(&existingExpenseNonstocks).Error; err != nil { @@ -400,7 +400,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) return fiber.NewError(fiber.StatusInternalServerError, "Failed to update project flock kandang id to null") } } - } else if currentExpense.Category == "NON-BOP" && newCategory == "BOP" { + } else if currentExpense.Category == string(utils.ExpenseCategoryNonBOP) && newCategory == string(utils.ExpenseCategoryBOP) { var existingExpenseNonstocks []entity.ExpenseNonstock if err := tx.Where("expense_id = ?", id).Find(&existingExpenseNonstocks).Error; err != nil { @@ -457,7 +457,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) for _, expenseNonstock := range *req.ExpenseNonstocks { var projectFlockKandangId *uint64 - if updatedExpense.Category == "BOP" { + if updatedExpense.Category == string(utils.ExpenseCategoryBOP) { projectFlockKandangRepoTx := projectFlockKandangRepo.NewProjectFlockKandangRepository(tx) projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID)) if err != nil { @@ -480,10 +480,10 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) } var kandangId *uint64 - if updatedExpense.Category == "NON-BOP" { + if updatedExpense.Category == string(utils.ExpenseCategoryNonBOP) { id := uint64(expenseNonstock.KandangID) kandangId = &id - } else if updatedExpense.Category == "BOP" { + } else if updatedExpense.Category == string(utils.ExpenseCategoryBOP) { if projectFlockKandangId != nil { kandangId = &expenseNonstock.KandangID } diff --git a/internal/utils/constant.go b/internal/utils/constant.go index d4f6ec02..85b33f9b 100644 --- a/internal/utils/constant.go +++ b/internal/utils/constant.go @@ -135,6 +135,17 @@ const ( SupplierCategorySapronak SupplierCategory = "SAPRONAK" ) +// ------------------------------------------------------------------- +// ExpenseCategory +// ------------------------------------------------------------------- + +type ExpenseCategory string + +const ( + ExpenseCategoryBOP ExpenseCategory = "BOP" + ExpenseCategoryNonBOP ExpenseCategory = "NON-BOP" +) + // ------------------------------------------------------------------- // Kandang Status // ------------------------------------------------------------------- @@ -429,6 +440,14 @@ func IsValidSupplierCategory(v string) bool { return false } +func IsValidExpenseCategory(v string) bool { + switch ExpenseCategory(v) { + case ExpenseCategoryBOP, ExpenseCategoryNonBOP: + return true + } + return false +} + // example use // Recording helper