mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
177 lines
4.6 KiB
Go
177 lines
4.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/closings/dto"
|
|
service "gitlab.com/mbugroup/lti-api.git/internal/modules/closings/services"
|
|
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/closings/validations"
|
|
"gitlab.com/mbugroup/lti-api.git/internal/response"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type ClosingController struct {
|
|
ClosingService service.ClosingService
|
|
}
|
|
|
|
func NewClosingController(closingService service.ClosingService) *ClosingController {
|
|
return &ClosingController{
|
|
ClosingService: closingService,
|
|
}
|
|
}
|
|
|
|
func (u *ClosingController) GetAll(c *fiber.Ctx) error {
|
|
query := &validation.Query{
|
|
Page: c.QueryInt("page", 1),
|
|
Limit: c.QueryInt("limit", 10),
|
|
Search: c.Query("search", ""),
|
|
}
|
|
|
|
if query.Page < 1 || query.Limit < 1 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
|
}
|
|
|
|
result, totalResults, err := u.ClosingService.GetAll(c, query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.SuccessWithPaginate[dto.ClosingListDTO]{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get all closings successfully",
|
|
Meta: response.Meta{
|
|
Page: query.Page,
|
|
Limit: query.Limit,
|
|
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
|
TotalResults: totalResults,
|
|
},
|
|
Data: dto.ToClosingListDTOs(result),
|
|
})
|
|
}
|
|
|
|
func (u *ClosingController) GetOne(c *fiber.Ctx) error {
|
|
param := c.Params("id")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid id")
|
|
}
|
|
|
|
result, err := u.ClosingService.GetProjectFlockByID(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Retrieved closing information successfully",
|
|
Data: result,
|
|
})
|
|
}
|
|
|
|
func (u *ClosingController) GetClosingSummary(c *fiber.Ctx) error {
|
|
param := c.Params("projectFlockId")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid projectFlockId")
|
|
}
|
|
|
|
result, err := u.ClosingService.GetClosingSummary(c, uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Retrieved project information successfully",
|
|
Data: result,
|
|
})
|
|
}
|
|
|
|
func (u *ClosingController) GetPenjualan(c *fiber.Ctx) error {
|
|
param := c.Params("project_flock_id")
|
|
|
|
projectFlockID, err := strconv.Atoi(param)
|
|
if err != nil {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid Project Flock Id")
|
|
}
|
|
|
|
projectFlock, err := u.ClosingService.GetProjectFlockByID(c, uint(projectFlockID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
result, err := u.ClosingService.GetPenjualan(c, uint(projectFlockID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(response.Success{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Get closing penjualan successfully",
|
|
Data: dto.ToPenjualanRealisasiResponseDTO(projectFlock.Category, uint(projectFlockID), result),
|
|
})
|
|
}
|
|
|
|
func (u *ClosingController) GetClosingSapronak(c *fiber.Ctx) error {
|
|
param := c.Params("projectFlockId")
|
|
|
|
id, err := strconv.Atoi(param)
|
|
if err != nil || id <= 0 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "Invalid projectFlockId")
|
|
}
|
|
|
|
query := &validation.SapronakQuery{
|
|
Type: strings.ToLower(c.Query("type")),
|
|
Page: c.QueryInt("page", 1),
|
|
Limit: c.QueryInt("limit", 10),
|
|
}
|
|
|
|
if query.Page < 1 || query.Limit < 1 {
|
|
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
|
|
}
|
|
|
|
if query.Type != validation.SapronakTypeIncoming && query.Type != validation.SapronakTypeOutgoing {
|
|
return fiber.NewError(fiber.StatusBadRequest, "type must be either incoming or outgoing")
|
|
}
|
|
|
|
result, totalResults, err := u.ClosingService.GetClosingSapronak(c, uint(id), query)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp := struct {
|
|
Code int `json:"code"`
|
|
Status string `json:"status"`
|
|
Message string `json:"message"`
|
|
Meta response.Meta `json:"meta"`
|
|
Data interface{} `json:"data"`
|
|
}{
|
|
Code: fiber.StatusOK,
|
|
Status: "success",
|
|
Message: "Retrieved closing report (sapronak) successfully",
|
|
Meta: response.Meta{
|
|
Page: query.Page,
|
|
Limit: query.Limit,
|
|
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
|
TotalResults: totalResults,
|
|
},
|
|
Data: result,
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).
|
|
JSON(resp)
|
|
}
|