mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat(BE-229,234,235,230,231,232,233): purchase request and purchase order and fix master data dto
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/dto"
|
||||
service "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/services"
|
||||
@@ -19,6 +22,74 @@ func NewPurchaseController(s service.PurchaseService) *PurchaseController {
|
||||
return &PurchaseController{service: s}
|
||||
}
|
||||
|
||||
func (ctrl *PurchaseController) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.PurchaseQuery{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: strings.TrimSpace(c.Query("search")),
|
||||
PrNumber: strings.TrimSpace(c.Query("pr_number")),
|
||||
CreatedFrom: strings.TrimSpace(c.Query("created_from")),
|
||||
CreatedTo: strings.TrimSpace(c.Query("created_to")),
|
||||
}
|
||||
|
||||
if supplierID := c.QueryInt("supplier_id", 0); supplierID > 0 {
|
||||
query.SupplierID = uint(supplierID)
|
||||
}
|
||||
|
||||
if status := strings.TrimSpace(c.Query("status")); status != "" {
|
||||
query.Status = strings.ToUpper(status)
|
||||
}
|
||||
|
||||
results, total, err := ctrl.service.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
limit := query.Limit
|
||||
if limit <= 0 {
|
||||
limit = 10
|
||||
}
|
||||
page := query.Page
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.PurchaseListItemDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Purchase fetched successfully",
|
||||
Meta: response.Meta{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
TotalPages: int64(math.Ceil(float64(total) / float64(limit))),
|
||||
TotalResults: total,
|
||||
},
|
||||
Data: dto.ToPurchaseListDTOs(results),
|
||||
})
|
||||
}
|
||||
|
||||
func (ctrl *PurchaseController) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
id, err := strconv.ParseUint(param, 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
||||
}
|
||||
|
||||
result, err := ctrl.service.GetOne(c, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Purchase fetched successfully",
|
||||
Data: dto.ToPurchaseDetailDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (ctrl *PurchaseController) CreateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.CreatePurchaseRequest)
|
||||
|
||||
@@ -35,7 +106,7 @@ func (ctrl *PurchaseController) CreateOne(c *fiber.Ctx) error {
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Purchase requisition created successfully",
|
||||
Message: "Purchase created successfully",
|
||||
Data: dto.ToPurchaseDetailDTO(*result),
|
||||
})
|
||||
}
|
||||
@@ -44,12 +115,12 @@ func (ctrl *PurchaseController) ApproveStaffPurchase(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
id, err := strconv.ParseUint(param, 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase requisition id")
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
||||
}
|
||||
|
||||
req := new(validation.ApproveStaffPurchaseRequest)
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("Invalid request body: %v", err))
|
||||
}
|
||||
|
||||
result, err := ctrl.service.ApproveStaffPurchase(c, id, req)
|
||||
@@ -65,3 +136,102 @@ func (ctrl *PurchaseController) ApproveStaffPurchase(c *fiber.Ctx) error {
|
||||
Data: dto.ToPurchaseDetailDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func (ctrl *PurchaseController) ApproveManagerPurchase(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
id, err := strconv.ParseUint(param, 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
||||
}
|
||||
|
||||
req := new(validation.ApproveManagerPurchaseRequest)
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := ctrl.service.ApproveManagerPurchase(c, id, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Manager purchase approval recorded successfully",
|
||||
Data: dto.ToPurchaseDetailDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (ctrl *PurchaseController) ReceiveProducts(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
id, err := strconv.ParseUint(param, 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
||||
}
|
||||
|
||||
req := new(validation.ReceivePurchaseRequest)
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := ctrl.service.ReceiveProducts(c, id, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Purchase receiving recorded successfully",
|
||||
Data: dto.ToPurchaseDetailDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (ctrl *PurchaseController) DeleteItems(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
id, err := strconv.ParseUint(param, 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
||||
}
|
||||
|
||||
req := new(validation.DeletePurchaseItemsRequest)
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := ctrl.service.DeleteItems(c, id, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Purchase items deleted successfully",
|
||||
Data: dto.ToPurchaseDetailDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (ctrl *PurchaseController) DeletePurchase(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
id, err := strconv.ParseUint(param, 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid purchase id")
|
||||
}
|
||||
|
||||
if err := ctrl.service.DeletePurchase(c, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Purchase deleted successfully",
|
||||
Data: nil,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user