mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Feat[BE-261,265]: createing BOP and BOP realization(Unfinished)
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/services"
|
||||
@@ -29,7 +32,7 @@ func (u *ExpenseController) GetAll(c *fiber.Ctx) error {
|
||||
Search: c.Query("search", ""),
|
||||
}
|
||||
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
if query.Page < 1 || query.Limit < 1 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
||||
}
|
||||
|
||||
@@ -49,7 +52,7 @@ func (u *ExpenseController) GetAll(c *fiber.Ctx) error {
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: dto.ToExpenseListDTOs(result),
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -71,15 +74,39 @@ func (u *ExpenseController) GetOne(c *fiber.Ctx) error {
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get expense successfully",
|
||||
Data: dto.ToExpenseListDTO(*result),
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) CreateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Create)
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
req.TransactionDate = c.FormValue("transaction_date")
|
||||
|
||||
supplierID, err := strconv.ParseUint(c.FormValue("supplier_id"), 10, 64)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid supplier_id format")
|
||||
}
|
||||
req.SupplierID = supplierID
|
||||
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid multipart form")
|
||||
}
|
||||
req.Documents = form.File["documents"]
|
||||
|
||||
costPerKandangJSON := c.FormValue("cost_per_kandangs")
|
||||
if costPerKandangJSON != "" {
|
||||
|
||||
if err := json.Unmarshal([]byte(costPerKandangJSON), &req.CostPerKandangs); err != nil {
|
||||
|
||||
var singleCostPerKandang validation.CostPerKandang
|
||||
if err := json.Unmarshal([]byte(costPerKandangJSON), &singleCostPerKandang); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid cost_per_kandang JSON: %v", err))
|
||||
}
|
||||
|
||||
req.CostPerKandangs = []validation.CostPerKandang{singleCostPerKandang}
|
||||
}
|
||||
}
|
||||
|
||||
result, err := u.ExpenseService.CreateOne(c, req)
|
||||
@@ -92,7 +119,7 @@ func (u *ExpenseController) CreateOne(c *fiber.Ctx) error {
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Create expense successfully",
|
||||
Data: dto.ToExpenseListDTO(*result),
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -119,7 +146,7 @@ func (u *ExpenseController) UpdateOne(c *fiber.Ctx) error {
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update expense successfully",
|
||||
Data: dto.ToExpenseListDTO(*result),
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -142,3 +169,126 @@ func (u *ExpenseController) DeleteOne(c *fiber.Ctx) error {
|
||||
Message: "Delete expense successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) ApproveExpense(c *fiber.Ctx) error {
|
||||
expenseID := c.Params("id")
|
||||
id, err := strconv.Atoi(expenseID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid expense ID")
|
||||
}
|
||||
|
||||
// Extract step from URL path (manager or finance)
|
||||
path := c.Path()
|
||||
var step string
|
||||
if strings.Contains(path, "/approvals/manager") {
|
||||
step = "Manager"
|
||||
} else if strings.Contains(path, "/approvals/finance") {
|
||||
step = "Finance"
|
||||
} else {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid approval step")
|
||||
}
|
||||
|
||||
// Parse approval request
|
||||
var req validation.ApprovalRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
// Approve expense
|
||||
expense, err := u.ExpenseService.ApproveExpense(c, uint(id), step, req.Action, req.Notes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Approve expense successfully",
|
||||
Data: expense,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) CreateRealization(c *fiber.Ctx) error {
|
||||
expenseID := c.Params("id")
|
||||
id, err := strconv.Atoi(expenseID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid expense ID")
|
||||
}
|
||||
|
||||
req := new(validation.CreateRealization)
|
||||
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid multipart form")
|
||||
}
|
||||
req.Documents = form.File["documents"]
|
||||
|
||||
// Parse realizations JSON
|
||||
realizationsJSON := c.FormValue("realizations")
|
||||
if realizationsJSON != "" {
|
||||
if err := json.Unmarshal([]byte(realizationsJSON), &req.Realizations); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid realizations JSON: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
expense, err := u.ExpenseService.CreateRealization(c, uint(id), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Create realization successfully",
|
||||
Data: expense,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) UpdateRealization(c *fiber.Ctx) error {
|
||||
expenseID := c.Params("id")
|
||||
id, err := strconv.Atoi(expenseID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid expense ID")
|
||||
}
|
||||
|
||||
var req validation.UpdateRealization
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
expense, err := u.ExpenseService.UpdateRealization(c, uint(id), &req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update realization successfully",
|
||||
Data: expense,
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ExpenseController) CompleteExpense(c *fiber.Ctx) error {
|
||||
expenseID := c.Params("id")
|
||||
id, err := strconv.Atoi(expenseID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid expense ID")
|
||||
}
|
||||
|
||||
expense, err := u.ExpenseService.CompleteExpense(c, uint(id), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Complete expense successfully",
|
||||
Data: expense,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user