mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat[BE]: enhance expense management with location and project flock integration, including updates to migrations, entities, services, and validations
This commit is contained in:
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
@@ -144,11 +145,8 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
|
||||
|
||||
supplierID := uint(req.SupplierID)
|
||||
|
||||
supplierExistsFunc := func(ctx context.Context, id uint) (bool, error) {
|
||||
return commonRepo.Exists[entity.Supplier](ctx, s.SupplierRepo.DB(), id)
|
||||
}
|
||||
if err := commonSvc.EnsureRelations(c.Context(),
|
||||
commonSvc.RelationCheck{Name: "Supplier", ID: &supplierID, Exists: supplierExistsFunc},
|
||||
commonSvc.RelationCheck{Name: "Supplier", ID: &supplierID, Exists: s.SupplierRepo.IdExists},
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -199,11 +197,47 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
|
||||
return fiber.NewError(fiber.StatusUnauthorized, "Failed to get actor ID from context")
|
||||
}
|
||||
createdBy := uint64(actorID)
|
||||
|
||||
hasKandang := false
|
||||
for _, ens := range req.ExpenseNonstocks {
|
||||
if ens.KandangID != nil {
|
||||
hasKandang = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var projectFlockIdJSON *string
|
||||
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")
|
||||
}
|
||||
|
||||
if len(activeProjectFlocks) == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "No active project flocks found for this location")
|
||||
}
|
||||
|
||||
projectFlockIDs := make([]uint64, len(activeProjectFlocks))
|
||||
for i, pf := range activeProjectFlocks {
|
||||
projectFlockIDs[i] = uint64(pf.Id)
|
||||
}
|
||||
|
||||
projectFlockIdsJSON, err := json.Marshal(projectFlockIDs)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to marshal project_flock_ids")
|
||||
}
|
||||
jsonStr := string(projectFlockIdsJSON)
|
||||
projectFlockIdJSON = &jsonStr
|
||||
}
|
||||
|
||||
expense = &entity.Expense{
|
||||
ReferenceNumber: referenceNumber,
|
||||
PoNumber: req.PoNumber,
|
||||
Category: req.Category,
|
||||
SupplierId: req.SupplierID,
|
||||
LocationId: req.LocationID,
|
||||
ProjectFlockId: projectFlockIdJSON,
|
||||
TransactionDate: expenseDate,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
@@ -216,35 +250,36 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
|
||||
|
||||
for _, expenseNonstock := range req.ExpenseNonstocks {
|
||||
|
||||
isAttachingToKandang := (expenseNonstock.KandangID != nil)
|
||||
|
||||
var projectFlockKandangId *uint64
|
||||
var kandangId *uint64
|
||||
|
||||
if req.Category == string(utils.ExpenseCategoryBOP) {
|
||||
if isAttachingToKandang {
|
||||
kandangId = expenseNonstock.KandangID
|
||||
|
||||
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID))
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "No active project flock kandang found for this kandang")
|
||||
if req.Category == string(utils.ExpenseCategoryBOP) {
|
||||
|
||||
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(*kandangId))
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "No active project flock kandang found for this kandang")
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to find project flock kandang for this kandang")
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to find project flock kandang for this kandang")
|
||||
id := uint64(projectFlockKandang.Id)
|
||||
projectFlockKandangId = &id
|
||||
}
|
||||
id := uint64(projectFlockKandang.Id)
|
||||
projectFlockKandangId = &id
|
||||
|
||||
} else {
|
||||
kandangId = nil
|
||||
projectFlockKandangId = nil
|
||||
}
|
||||
|
||||
for _, costItem := range expenseNonstock.CostItems {
|
||||
|
||||
nonstockId := costItem.NonstockID
|
||||
var kandangId *uint64
|
||||
if req.Category == string(utils.ExpenseCategoryNonBOP) {
|
||||
id := uint64(expenseNonstock.KandangID)
|
||||
kandangId = &id
|
||||
} else if req.Category == string(utils.ExpenseCategoryBOP) {
|
||||
if projectFlockKandangId != nil {
|
||||
kandangId = &expenseNonstock.KandangID
|
||||
}
|
||||
}
|
||||
|
||||
expenseNonstock := &entity.ExpenseNonstock{
|
||||
newExpenseNonstock := &entity.ExpenseNonstock{
|
||||
ExpenseId: &expense.Id,
|
||||
ProjectFlockKandangId: projectFlockKandangId,
|
||||
KandangId: kandangId,
|
||||
@@ -254,7 +289,7 @@ func (s *expenseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*expen
|
||||
Notes: costItem.Notes,
|
||||
}
|
||||
|
||||
if err := expenseNonstockRepoTx.CreateOne(c.Context(), expenseNonstock, nil); err != nil {
|
||||
if err := expenseNonstockRepoTx.CreateOne(c.Context(), newExpenseNonstock, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create expense cost item")
|
||||
}
|
||||
}
|
||||
@@ -361,6 +396,11 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
updateBody["supplier_id"] = *req.SupplierID
|
||||
}
|
||||
|
||||
if req.LocationID != nil {
|
||||
locationID := uint(*req.LocationID)
|
||||
updateBody["location_id"] = locationID
|
||||
}
|
||||
|
||||
if len(updateBody) == 0 && req.ExpenseNonstocks == nil && len(req.Documents) == 0 {
|
||||
|
||||
responseDTO, err := s.GetOne(c, id)
|
||||
@@ -475,18 +515,26 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
|
||||
for _, expenseNonstock := range *req.ExpenseNonstocks {
|
||||
var projectFlockKandangId *uint64
|
||||
var kandangId *uint64
|
||||
|
||||
if updatedExpense.Category == string(utils.ExpenseCategoryBOP) {
|
||||
projectFlockKandangRepoTx := projectFlockKandangRepo.NewProjectFlockKandangRepository(tx)
|
||||
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(expenseNonstock.KandangID))
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "No active project flock kandang found for this kandang")
|
||||
// Check if attaching to kandang
|
||||
if expenseNonstock.KandangID != nil {
|
||||
kandangId = expenseNonstock.KandangID
|
||||
|
||||
if updatedExpense.Category == string(utils.ExpenseCategoryBOP) {
|
||||
// BOP with kandang: Get active project flock kandang
|
||||
projectFlockKandangRepoTx := projectFlockKandangRepo.NewProjectFlockKandangRepository(tx)
|
||||
projectFlockKandang, err := projectFlockKandangRepoTx.GetActiveByKandangID(c.Context(), uint(*kandangId))
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "No active project flock kandang found for this kandang")
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to find project flock kandang for this kandang")
|
||||
}
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to find project flock kandang for this kandang")
|
||||
id := uint64(projectFlockKandang.Id)
|
||||
projectFlockKandangId = &id
|
||||
}
|
||||
id := uint64(projectFlockKandang.Id)
|
||||
projectFlockKandangId = &id
|
||||
// NON-BOP: projectFlockKandangId stays nil
|
||||
}
|
||||
|
||||
for _, costItem := range expenseNonstock.CostItems {
|
||||
@@ -498,18 +546,8 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
return err
|
||||
}
|
||||
|
||||
var kandangId *uint64
|
||||
if updatedExpense.Category == string(utils.ExpenseCategoryNonBOP) {
|
||||
id := uint64(expenseNonstock.KandangID)
|
||||
kandangId = &id
|
||||
} else if updatedExpense.Category == string(utils.ExpenseCategoryBOP) {
|
||||
if projectFlockKandangId != nil {
|
||||
kandangId = &expenseNonstock.KandangID
|
||||
}
|
||||
}
|
||||
|
||||
expenseId := uint64(id)
|
||||
expenseNonstock := &entity.ExpenseNonstock{
|
||||
newExpenseNonstock := &entity.ExpenseNonstock{
|
||||
ExpenseId: &expenseId,
|
||||
ProjectFlockKandangId: projectFlockKandangId,
|
||||
KandangId: kandangId,
|
||||
@@ -519,7 +557,7 @@ func (s expenseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
Notes: costItem.Notes,
|
||||
}
|
||||
|
||||
if err := expenseNonstockRepoTx.CreateOne(c.Context(), expenseNonstock, nil); err != nil {
|
||||
if err := expenseNonstockRepoTx.CreateOne(c.Context(), newExpenseNonstock, nil); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, "Failed to create expense cost item")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user