FEAT[BE]: implement expense report retrieval with filtering options

This commit is contained in:
aguhh18
2025-12-15 09:11:26 +07:00
parent c79e35c217
commit cbb3368141
12 changed files with 330 additions and 158 deletions
@@ -2,7 +2,6 @@ package controller
import (
"math"
"strconv"
"gitlab.com/mbugroup/lti-api.git/internal/modules/repports/dto"
service "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/services"
@@ -22,27 +21,35 @@ func NewRepportController(repportService service.RepportService) *RepportControl
}
}
func (c *RepportController) GetAll(ctx *fiber.Ctx) error {
func (c *RepportController) GetExpense(ctx *fiber.Ctx) error {
query := &validation.Query{
Page: ctx.QueryInt("page", 1),
Limit: ctx.QueryInt("limit", 10),
Search: ctx.Query("search", ""),
Page: ctx.QueryInt("page", 1),
Limit: ctx.QueryInt("limit", 10),
Search: ctx.Query("search", ""),
Category: ctx.Query("category", ""),
SupplierId: int64(ctx.QueryInt("supplier_id", 0)),
KandangId: int64(ctx.QueryInt("kandang_id", 0)),
ProjectFlockKandangId: int64(ctx.QueryInt("project_flock_kandang_id", 0)),
NonstockId: int64(ctx.QueryInt("nonstock_id", 0)),
AreaId: int64(ctx.QueryInt("area_id", 0)),
LocationId: int64(ctx.QueryInt("location_id", 0)),
RealizationDate: ctx.Query("realization_date", ""),
}
if query.Page < 1 || query.Limit < 1 {
return fiber.NewError(fiber.StatusBadRequest, "page and limit must be greater than 0")
}
result, totalResults, err := c.RepportService.GetAll(ctx, query)
result, totalResults, err := c.RepportService.GetExpense(ctx, query)
if err != nil {
return err
}
return ctx.Status(fiber.StatusOK).
JSON(response.SuccessWithPaginate[dto.RepportListDTO]{
JSON(response.SuccessWithPaginate[dto.RepportExpenseListDTO]{
Code: fiber.StatusOK,
Status: "success",
Message: "Get all reports successfully",
Message: "Get expense report successfully",
Meta: response.Meta{
Page: query.Page,
Limit: query.Limit,
@@ -52,47 +59,3 @@ func (c *RepportController) GetAll(ctx *fiber.Ctx) error {
Data: result,
})
}
func (c *RepportController) GetOne(ctx *fiber.Ctx) error {
param := ctx.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
result, err := c.RepportService.GetOne(ctx, uint(id))
if err != nil {
return err
}
return ctx.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get report successfully",
Data: result,
})
}
func (c *RepportController) GetExpense(ctx *fiber.Ctx) error {
param := ctx.Params("id")
id, err := strconv.Atoi(param)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
}
result, err := c.RepportService.GetOne(ctx, uint(id))
if err != nil {
return err
}
return ctx.Status(fiber.StatusOK).
JSON(response.Success{
Code: fiber.StatusOK,
Status: "success",
Message: "Get report successfully",
Data: result,
})
}
@@ -1,16 +0,0 @@
package dto
import "time"
// === DTO Structs ===
type RepportListDTO struct {
Id uint `json:"id"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type RepportDetailDTO struct {
RepportListDTO
}
@@ -0,0 +1,173 @@
package dto
import (
"time"
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
kandangDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/kandangs/dto"
nonstockDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/nonstocks/dto"
supplierDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/suppliers/dto"
)
// === DTO Structs ===
type RepportExpenseBaseDTO struct {
Id uint64 `json:"id"`
ReferenceNumber string `json:"reference_number"`
PoNumber string `json:"po_number"`
Category string `json:"category"`
Supplier *supplierDTO.SupplierRelationDTO `json:"supplier,omitempty"`
RealizationDate *time.Time `json:"realization_date,omitempty"`
TransactionDate time.Time `json:"transaction_date"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type RepportExpensePengajuanDTO struct {
Id uint64 `json:"id"`
ExpenseId *uint64 `json:"expense_id,omitempty"`
ProjectFlockKandangId *uint64 `json:"project_flock_kandang_id,omitempty"`
Qty float64 `json:"qty"`
Price float64 `json:"price"`
Notes string `json:"notes"`
Nonstock *nonstockDTO.NonstockRelationDTO `json:"nonstock,omitempty"`
Kandang *kandangDTO.KandangRelationDTO `json:"kandang,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
type RepportExpenseRealisasiDTO struct {
Id *uint64 `json:"id,omitempty"`
ExpenseNonstockId *uint64 `json:"expense_nonstock_id,omitempty"`
Qty float64 `json:"qty"`
Price float64 `json:"price"`
Notes string `json:"notes"`
Nonstock *nonstockDTO.NonstockRelationDTO `json:"nonstock,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
type RepportExpenseListDTO struct {
RepportExpenseBaseDTO
Pengajuan RepportExpensePengajuanDTO `json:"pengajuan"`
Realisasi RepportExpenseRealisasiDTO `json:"realisasi"`
TotalPengajuan float64 `json:"total_pengajuan"`
TotalRealisasi float64 `json:"total_realisasi"`
LatestApproval *approvalDTO.ApprovalRelationDTO `json:"latest_approval,omitempty"`
}
// === MAPPERS ===
func ToRepportExpenseBaseDTO(e *entity.Expense) RepportExpenseBaseDTO {
var realizationDate *time.Time
if !e.RealizationDate.IsZero() {
realizationDate = &e.RealizationDate
}
var supplier *supplierDTO.SupplierRelationDTO
if e.Supplier != nil && e.Supplier.Id != 0 {
mapped := supplierDTO.ToSupplierRelationDTO(*e.Supplier)
supplier = &mapped
}
return RepportExpenseBaseDTO{
Id: e.Id,
ReferenceNumber: e.ReferenceNumber,
PoNumber: e.PoNumber,
Category: e.Category,
Supplier: supplier,
RealizationDate: realizationDate,
TransactionDate: e.TransactionDate,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
}
}
func ToRepportExpensePengajuanDTO(ns *entity.ExpenseNonstock) RepportExpensePengajuanDTO {
var nonstock *nonstockDTO.NonstockRelationDTO
if ns.Nonstock != nil && ns.Nonstock.Id != 0 {
mapped := nonstockDTO.ToNonstockRelationDTO(*ns.Nonstock)
nonstock = &mapped
}
var kandang *kandangDTO.KandangRelationDTO
if ns.Kandang != nil && ns.Kandang.Id != 0 {
mapped := kandangDTO.ToKandangRelationDTO(*ns.Kandang)
kandang = &mapped
}
return RepportExpensePengajuanDTO{
Id: ns.Id,
ExpenseId: ns.ExpenseId,
ProjectFlockKandangId: ns.ProjectFlockKandangId,
Qty: ns.Qty,
Price: ns.Price,
Notes: ns.Notes,
Nonstock: nonstock,
Kandang: kandang,
CreatedAt: ns.CreatedAt,
}
}
func ToRepportExpenseRealisasiDTO(r *entity.ExpenseRealization) RepportExpenseRealisasiDTO {
var nonstock *nonstockDTO.NonstockRelationDTO
if r.ExpenseNonstock != nil && r.ExpenseNonstock.Nonstock != nil && r.ExpenseNonstock.Nonstock.Id != 0 {
mapped := nonstockDTO.ToNonstockRelationDTO(*r.ExpenseNonstock.Nonstock)
nonstock = &mapped
}
return RepportExpenseRealisasiDTO{
Id: r.ExpenseNonstockId,
ExpenseNonstockId: r.ExpenseNonstockId,
Qty: r.Qty,
Price: r.Price,
Notes: r.Notes,
Nonstock: nonstock,
CreatedAt: r.CreatedAt,
}
}
func ToRepportExpenseListDTO(baseDTO RepportExpenseBaseDTO, ns *entity.ExpenseNonstock, latestApproval *approvalDTO.ApprovalRelationDTO) RepportExpenseListDTO {
var realisasi RepportExpenseRealisasiDTO
if ns.Realization != nil {
realisasi = ToRepportExpenseRealisasiDTO(ns.Realization)
}
totalPengajuan := ns.Qty * ns.Price
totalRealisasi := float64(0)
if ns.Realization != nil {
totalRealisasi = ns.Realization.Qty * ns.Realization.Price
}
return RepportExpenseListDTO{
RepportExpenseBaseDTO: baseDTO,
Pengajuan: ToRepportExpensePengajuanDTO(ns),
Realisasi: realisasi,
TotalPengajuan: totalPengajuan,
TotalRealisasi: totalRealisasi,
LatestApproval: latestApproval,
}
}
func ToRepportExpenseListDTOs(realizations []entity.ExpenseRealization) []RepportExpenseListDTO {
result := make([]RepportExpenseListDTO, 0, len(realizations))
for _, realization := range realizations {
if realization.ExpenseNonstock == nil || realization.ExpenseNonstock.Expense == nil {
continue
}
expense := realization.ExpenseNonstock.Expense
baseDTO := ToRepportExpenseBaseDTO(expense)
var latestApproval *approvalDTO.ApprovalRelationDTO
if expense.LatestApproval != nil {
mapped := approvalDTO.ToApprovalDTO(*expense.LatestApproval)
latestApproval = &mapped
}
dto := ToRepportExpenseListDTO(baseDTO, realization.ExpenseNonstock, latestApproval)
result = append(result, dto)
}
return result
}
+7 -4
View File
@@ -5,6 +5,8 @@ import (
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
approvalService "gitlab.com/mbugroup/lti-api.git/internal/common/service"
sRepport "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/services"
expenseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
@@ -13,11 +15,12 @@ import (
type RepportModule struct{}
func (RepportModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
// Initialize expense realization repository
expRealizationRepo := expenseRepo.NewExpenseRealizationRepository(db)
// Initialize report service with expense realization repo
repportService := sRepport.NewRepportService(validate, expRealizationRepo)
expenseRealizationRepository := expenseRepo.NewExpenseRealizationRepository(db)
approvalRepository := commonRepo.NewApprovalRepository(db)
approvalSvc := approvalService.NewApprovalService(approvalRepository)
repportService := sRepport.NewRepportService(validate, expenseRealizationRepository, approvalSvc)
RepportRoutes(router, repportService)
}
-4
View File
@@ -1,7 +1,6 @@
package repports
import (
controller "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/controllers"
repport "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/services"
@@ -13,8 +12,5 @@ func RepportRoutes(v1 fiber.Router, s repport.RepportService) {
route := v1.Group("/repports")
route.Get("/", ctrl.GetAll)
route.Get("/:id", ctrl.GetOne)
route.Get("expense", ctrl.GetExpense)
}
@@ -1,106 +1,74 @@
package service
import (
"strings"
"time"
"gitlab.com/mbugroup/lti-api.git/internal/modules/repports/dto"
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/validations"
"gitlab.com/mbugroup/lti-api.git/internal/utils"
approvalService "gitlab.com/mbugroup/lti-api.git/internal/common/service"
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
expenseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type RepportService interface {
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]dto.RepportListDTO, int64, error)
GetOne(ctx *fiber.Ctx, id uint) (*dto.RepportListDTO, error)
GetExpense(ctx *fiber.Ctx, id uint) (*dto.RepportListDTO, error)
GetExpense(ctx *fiber.Ctx, params *validation.Query) ([]dto.RepportExpenseListDTO, int64, error)
}
type repportService struct {
Log *logrus.Logger
Validate *validator.Validate
dummyData map[uint]dto.RepportListDTO
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
ApprovalSvc approvalService.ApprovalService
}
func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository) RepportService {
// Initialize with dummy data
now := time.Now()
dummyData := map[uint]dto.RepportListDTO{
1: {
Id: 1,
Name: "Sales Report",
CreatedAt: now,
UpdatedAt: now,
},
2: {
Id: 2,
Name: "Inventory Report",
CreatedAt: now,
UpdatedAt: now,
},
3: {
Id: 3,
Name: "Production Report",
CreatedAt: now,
UpdatedAt: now,
},
}
func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository, approvalSvc approvalService.ApprovalService) RepportService {
return &repportService{
Log: utils.Log,
Validate: validate,
dummyData: dummyData,
ExpenseRealizationRepo: expenseRealizationRepo,
ApprovalSvc: approvalSvc,
}
}
func (s *repportService) GetAll(c *fiber.Ctx, params *validation.Query) ([]dto.RepportListDTO, int64, error) {
func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.Query) ([]dto.RepportExpenseListDTO, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
}
// Convert map to slice
var results []dto.RepportListDTO
for _, v := range s.dummyData {
// Apply search filter if provided
if params.Search != "" && !strings.Contains(strings.ToLower(v.Name), strings.ToLower(params.Search)) {
continue
}
results = append(results, v)
}
// Apply pagination
total := int64(len(results))
offset := (params.Page - 1) * params.Limit
if offset >= int(total) {
return []dto.RepportListDTO{}, total, nil
realizations, total, err := s.ExpenseRealizationRepo.GetAllWithFilters(c.Context(), offset, params.Limit, params)
if err != nil {
s.Log.Errorf("GetAllWithFilters error: %v", err)
return nil, 0, err
}
end := offset + params.Limit
if end > int(total) {
end = int(total)
result := dto.ToRepportExpenseListDTOs(realizations)
expenseIDs := make([]uint, 0, len(result))
for i := range result {
expenseIDs = append(expenseIDs, uint(result[i].Id))
}
return results[offset:end], total, nil
}
func (s *repportService) GetOne(c *fiber.Ctx, id uint) (*dto.RepportListDTO, error) {
if data, ok := s.dummyData[id]; ok {
return &data, nil
}
return nil, fiber.NewError(fiber.StatusNotFound, "Report not found")
}
func (s *repportService) GetExpense(c *fiber.Ctx, id uint) (*dto.RepportListDTO, error) {
if data, ok := s.dummyData[id]; ok {
return &data, nil
}
return nil, fiber.NewError(fiber.StatusNotFound, "Report not found")
approvals, err := s.ApprovalSvc.LatestByTargets(c.Context(), utils.ApprovalWorkflowExpense, expenseIDs, func(db *gorm.DB) *gorm.DB {
return db.Preload("ActionUser")
})
if err != nil {
s.Log.Warnf("LatestByTargets error: %v", err)
}
for i := range result {
expenseIDAsUint := uint(result[i].Id)
if approval, exists := approvals[expenseIDAsUint]; exists && approval != nil {
mapped := approvalDTO.ToApprovalDTO(*approval)
result[i].LatestApproval = &mapped
}
}
return result, total, nil
}
@@ -1,7 +1,16 @@
package validation
type Query struct {
Page int `query:"page" validate:"omitempty,number,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100,gt=0"`
Search string `query:"search" validate:"omitempty,max=50"`
Page int `query:"page" validate:"omitempty,min=1,gt=0"`
Limit int `query:"limit" validate:"omitempty,min=1,max=100,gt=0"`
Search string `query:"search" validate:"omitempty,max=100"`
Category string `query:"category" validate:"omitempty,oneof=BOP NON-BOP"`
SupplierId int64 `query:"supplier_id" validate:"omitempty"`
KandangId int64 `query:"kandang_id" validate:"omitempty"`
ProjectFlockKandangId int64 `query:"project_flock_kandang_id" validate:"omitempty"`
ProjectFlockId int64 `query:"project_flock_id" validate:"omitempty"`
NonstockId int64 `query:"nonstock_id" validate:"omitempty"`
AreaId int64 `query:"area_id" validate:"omitempty"`
LocationId int64 `query:"location_id" validate:"omitempty"`
RealizationDate string `query:"realization_date" validate:"omitempty"`
}