mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
FEAT[BE]: create marketing report API
This commit is contained in:
@@ -22,7 +22,7 @@ func NewRepportController(repportService service.RepportService) *RepportControl
|
||||
}
|
||||
|
||||
func (c *RepportController) GetExpense(ctx *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
query := &validation.ExpenseQuery{
|
||||
Page: ctx.QueryInt("page", 1),
|
||||
Limit: ctx.QueryInt("limit", 10),
|
||||
Search: ctx.Query("search", ""),
|
||||
@@ -59,3 +59,41 @@ func (c *RepportController) GetExpense(ctx *fiber.Ctx) error {
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *RepportController) GetMarketing(ctx *fiber.Ctx) error {
|
||||
query := &validation.MarketingQuery{
|
||||
Page: ctx.QueryInt("page", 1),
|
||||
Limit: ctx.QueryInt("limit", 10),
|
||||
Search: ctx.Query("search", ""),
|
||||
CustomerId: int64(ctx.QueryInt("customer_id", 0)),
|
||||
ProjectFlockKandangId: int64(ctx.QueryInt("project_flock_kandang_id", 0)),
|
||||
DeliveryDate: ctx.Query("delivery_date", ""),
|
||||
ProductId: int64(ctx.QueryInt("product_id", 0)),
|
||||
WarehouseId: int64(ctx.QueryInt("warehouse_id", 0)),
|
||||
SalesPersonId: int64(ctx.QueryInt("sales_person_id", 0)),
|
||||
MarketingId: int64(ctx.QueryInt("marketing_id", 0)),
|
||||
}
|
||||
|
||||
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.GetMarketing(ctx, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ctx.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.RepportMarketingListDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get marketing report successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: result,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,219 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
approvalDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/approvals/dto"
|
||||
marketingDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/dto"
|
||||
customerDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/dto"
|
||||
productDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/dto"
|
||||
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
|
||||
type RepportMarketingBaseDTO struct {
|
||||
Id uint `json:"id"`
|
||||
SoNumber string `json:"so_number"`
|
||||
SoDate time.Time `json:"so_date"`
|
||||
Customer *customerDTO.CustomerRelationDTO `json:"customer,omitempty"`
|
||||
SalesPerson *userDTO.UserRelationDTO `json:"sales_person,omitempty"`
|
||||
Notes string `json:"notes"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type RepportMarketingProductDTO struct {
|
||||
Id uint `json:"id"`
|
||||
MarketingProductId uint `json:"marketing_product_id"`
|
||||
Qty float64 `json:"qty"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
TotalWeight float64 `json:"total_weight"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
Product *productDTO.ProductRelationDTO `json:"product,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type RepportMarketingDeliveryDTO struct {
|
||||
Id uint `json:"id"`
|
||||
MarketingProductId uint `json:"marketing_product_id"`
|
||||
Qty float64 `json:"qty"`
|
||||
UnitPrice float64 `json:"unit_price"`
|
||||
TotalWeight float64 `json:"total_weight"`
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
TotalPrice float64 `json:"total_price"`
|
||||
DeliveryDate *time.Time `json:"delivery_date,omitempty"`
|
||||
VehicleNumber string `json:"vehicle_number"`
|
||||
DoNumber string `json:"do_number"`
|
||||
Product *productDTO.ProductRelationDTO `json:"product,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type RepportMarketingListDTO struct {
|
||||
RepportMarketingBaseDTO
|
||||
MarketingProduct RepportMarketingProductDTO `json:"marketing_product"`
|
||||
MarketingDelivery RepportMarketingDeliveryDTO `json:"marketing_delivery"`
|
||||
TotalMarketingProduct float64 `json:"total_marketing_product"`
|
||||
TotalMarketingDelivery float64 `json:"total_marketing_delivery"`
|
||||
LatestApproval *approvalDTO.ApprovalRelationDTO `json:"latest_approval,omitempty"`
|
||||
}
|
||||
|
||||
// === MAPPERS ===
|
||||
|
||||
func ToRepportMarketingBaseDTO(m *entity.Marketing) RepportMarketingBaseDTO {
|
||||
if m == nil {
|
||||
return RepportMarketingBaseDTO{}
|
||||
}
|
||||
|
||||
var customer *customerDTO.CustomerRelationDTO
|
||||
if m.Customer.Id != 0 {
|
||||
mapped := customerDTO.ToCustomerRelationDTO(m.Customer)
|
||||
customer = &mapped
|
||||
}
|
||||
|
||||
var salesPerson *userDTO.UserRelationDTO
|
||||
if m.SalesPerson.Id != 0 {
|
||||
mapped := userDTO.ToUserRelationDTO(m.SalesPerson)
|
||||
salesPerson = &mapped
|
||||
}
|
||||
|
||||
return RepportMarketingBaseDTO{
|
||||
Id: m.Id,
|
||||
SoNumber: m.SoNumber,
|
||||
SoDate: m.SoDate,
|
||||
Customer: customer,
|
||||
SalesPerson: salesPerson,
|
||||
Notes: m.Notes,
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToRepportMarketingProductDTO(mp *entity.MarketingProduct) RepportMarketingProductDTO {
|
||||
if mp == nil {
|
||||
return RepportMarketingProductDTO{}
|
||||
}
|
||||
|
||||
var product *productDTO.ProductRelationDTO
|
||||
if mp.ProductWarehouse.Product.Id != 0 {
|
||||
mapped := productDTO.ToProductRelationDTO(mp.ProductWarehouse.Product)
|
||||
product = &mapped
|
||||
}
|
||||
|
||||
return RepportMarketingProductDTO{
|
||||
Id: mp.Id,
|
||||
MarketingProductId: mp.Id,
|
||||
Qty: mp.Qty,
|
||||
UnitPrice: mp.UnitPrice,
|
||||
AvgWeight: mp.AvgWeight,
|
||||
TotalWeight: mp.TotalWeight,
|
||||
TotalPrice: mp.TotalPrice,
|
||||
Product: product,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func ToRepportMarketingDeliveryDTO(mdp *entity.MarketingDeliveryProduct, soNumber string) RepportMarketingDeliveryDTO {
|
||||
if mdp == nil {
|
||||
return RepportMarketingDeliveryDTO{}
|
||||
}
|
||||
|
||||
var product *productDTO.ProductRelationDTO
|
||||
if mdp.MarketingProduct.ProductWarehouse.Product.Id != 0 {
|
||||
mapped := productDTO.ToProductRelationDTO(mdp.MarketingProduct.ProductWarehouse.Product)
|
||||
product = &mapped
|
||||
}
|
||||
|
||||
warehouseId := uint(0)
|
||||
if mdp.MarketingProduct.ProductWarehouse.Id != 0 {
|
||||
warehouseId = mdp.MarketingProduct.ProductWarehouse.WarehouseId
|
||||
}
|
||||
|
||||
doNumber := marketingDTO.GenerateDeliveryOrderNumber(soNumber, mdp.DeliveryDate, warehouseId)
|
||||
|
||||
return RepportMarketingDeliveryDTO{
|
||||
Id: mdp.Id,
|
||||
MarketingProductId: mdp.MarketingProductId,
|
||||
Qty: mdp.Qty,
|
||||
UnitPrice: mdp.UnitPrice,
|
||||
TotalWeight: mdp.TotalWeight,
|
||||
AvgWeight: mdp.AvgWeight,
|
||||
TotalPrice: mdp.TotalPrice,
|
||||
DeliveryDate: mdp.DeliveryDate,
|
||||
VehicleNumber: mdp.VehicleNumber,
|
||||
DoNumber: doNumber,
|
||||
Product: product,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func ToRepportMarketingListDTO(baseDTO RepportMarketingBaseDTO, mp *entity.MarketingProduct, mdp *entity.MarketingDeliveryProduct, latestApproval *approvalDTO.ApprovalRelationDTO) RepportMarketingListDTO {
|
||||
var marketingProduct RepportMarketingProductDTO
|
||||
var marketingDelivery RepportMarketingDeliveryDTO
|
||||
|
||||
if mp != nil {
|
||||
marketingProduct = ToRepportMarketingProductDTO(mp)
|
||||
}
|
||||
|
||||
if mdp != nil {
|
||||
marketingDelivery = ToRepportMarketingDeliveryDTO(mdp, baseDTO.SoNumber)
|
||||
}
|
||||
|
||||
totalMarketingProduct := float64(0)
|
||||
totalMarketingDelivery := float64(0)
|
||||
|
||||
if mp != nil {
|
||||
totalMarketingProduct = mp.Qty * mp.UnitPrice
|
||||
}
|
||||
|
||||
if mdp != nil {
|
||||
totalMarketingDelivery = mdp.Qty * mdp.UnitPrice
|
||||
}
|
||||
|
||||
return RepportMarketingListDTO{
|
||||
RepportMarketingBaseDTO: baseDTO,
|
||||
MarketingProduct: marketingProduct,
|
||||
MarketingDelivery: marketingDelivery,
|
||||
TotalMarketingProduct: totalMarketingProduct,
|
||||
TotalMarketingDelivery: totalMarketingDelivery,
|
||||
LatestApproval: latestApproval,
|
||||
}
|
||||
}
|
||||
|
||||
func ToRepportMarketingListDTOs(deliveryProducts []entity.MarketingDeliveryProduct) []RepportMarketingListDTO {
|
||||
result := make([]RepportMarketingListDTO, 0, len(deliveryProducts))
|
||||
|
||||
marketingMap := make(map[uint]entity.MarketingDeliveryProduct)
|
||||
for _, dp := range deliveryProducts {
|
||||
if dp.MarketingProduct.Marketing.Id == 0 {
|
||||
continue
|
||||
}
|
||||
marketingID := dp.MarketingProduct.Marketing.Id
|
||||
if _, exists := marketingMap[marketingID]; !exists {
|
||||
marketingMap[marketingID] = dp
|
||||
}
|
||||
}
|
||||
|
||||
for _, deliveryProduct := range marketingMap {
|
||||
if deliveryProduct.MarketingProduct.Marketing.Id == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
marketing := &deliveryProduct.MarketingProduct.Marketing
|
||||
baseDTO := ToRepportMarketingBaseDTO(marketing)
|
||||
|
||||
var latestApproval *approvalDTO.ApprovalRelationDTO
|
||||
if marketing.LatestApproval != nil {
|
||||
mapped := approvalDTO.ToApprovalDTO(*marketing.LatestApproval)
|
||||
latestApproval = &mapped
|
||||
}
|
||||
|
||||
mdp := &deliveryProduct
|
||||
dto := ToRepportMarketingListDTO(baseDTO, &deliveryProduct.MarketingProduct, mdp, latestApproval)
|
||||
result = append(result, dto)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
sRepport "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/services"
|
||||
|
||||
expenseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/expenses/repositories"
|
||||
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
||||
)
|
||||
|
||||
type RepportModule struct{}
|
||||
@@ -17,10 +18,11 @@ type RepportModule struct{}
|
||||
func (RepportModule) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||
|
||||
expenseRealizationRepository := expenseRepo.NewExpenseRealizationRepository(db)
|
||||
marketingDeliveryProductRepository := marketingRepo.NewMarketingDeliveryProductRepository(db)
|
||||
approvalRepository := commonRepo.NewApprovalRepository(db)
|
||||
|
||||
approvalSvc := approvalService.NewApprovalService(approvalRepository)
|
||||
repportService := sRepport.NewRepportService(validate, expenseRealizationRepository, approvalSvc)
|
||||
repportService := sRepport.NewRepportService(validate, expenseRealizationRepository, marketingDeliveryProductRepository, approvalSvc)
|
||||
|
||||
RepportRoutes(router, repportService)
|
||||
}
|
||||
|
||||
@@ -13,5 +13,5 @@ func RepportRoutes(v1 fiber.Router, s repport.RepportService) {
|
||||
route := v1.Group("/reports")
|
||||
|
||||
route.Get("/expense", ctrl.GetExpense)
|
||||
// route.Get("/marketing", ctrl.GetMarketing)
|
||||
route.Get("/marketing", ctrl.GetMarketing)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
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"
|
||||
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@@ -16,26 +17,29 @@ import (
|
||||
)
|
||||
|
||||
type RepportService interface {
|
||||
GetExpense(ctx *fiber.Ctx, params *validation.Query) ([]dto.RepportExpenseListDTO, int64, error)
|
||||
GetExpense(ctx *fiber.Ctx, params *validation.ExpenseQuery) ([]dto.RepportExpenseListDTO, int64, error)
|
||||
GetMarketing(ctx *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingListDTO, int64, error)
|
||||
}
|
||||
|
||||
type repportService struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
|
||||
MarketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository
|
||||
ApprovalSvc approvalService.ApprovalService
|
||||
}
|
||||
|
||||
func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository, approvalSvc approvalService.ApprovalService) RepportService {
|
||||
func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository, marketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository, approvalSvc approvalService.ApprovalService) RepportService {
|
||||
return &repportService{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
ExpenseRealizationRepo: expenseRealizationRepo,
|
||||
MarketingDeliveryRepo: marketingDeliveryRepo,
|
||||
ApprovalSvc: approvalSvc,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.Query) ([]dto.RepportExpenseListDTO, int64, error) {
|
||||
func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.ExpenseQuery) ([]dto.RepportExpenseListDTO, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -72,3 +76,40 @@ func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.Query) ([]d
|
||||
|
||||
return result, total, nil
|
||||
}
|
||||
|
||||
func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingListDTO, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
deliveryProducts, total, err := s.MarketingDeliveryRepo.GetAllWithFilters(c.Context(), offset, params.Limit, params)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
marketingIDMap := make(map[uint]bool)
|
||||
marketingIDs := make([]uint, 0)
|
||||
for _, dp := range deliveryProducts {
|
||||
if marketingID := dp.MarketingProduct.Marketing.Id; marketingID > 0 && !marketingIDMap[marketingID] {
|
||||
marketingIDs = append(marketingIDs, marketingID)
|
||||
marketingIDMap[marketingID] = true
|
||||
}
|
||||
}
|
||||
|
||||
approvals, err := s.ApprovalSvc.LatestByTargets(c.Context(), utils.ApprovalWorkflowMarketing, marketingIDs, func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("ActionUser")
|
||||
})
|
||||
if err != nil {
|
||||
s.Log.Warnf("LatestByTargets error: %v", err)
|
||||
}
|
||||
|
||||
for i := range deliveryProducts {
|
||||
if approval, exists := approvals[deliveryProducts[i].MarketingProduct.Marketing.Id]; exists && approval != nil {
|
||||
deliveryProducts[i].MarketingProduct.Marketing.LatestApproval = approval
|
||||
}
|
||||
}
|
||||
|
||||
return dto.ToRepportMarketingListDTOs(deliveryProducts), total, nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package validation
|
||||
|
||||
type Query struct {
|
||||
type ExpenseQuery struct {
|
||||
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"`
|
||||
@@ -14,3 +14,16 @@ type Query struct {
|
||||
LocationId int64 `query:"location_id" validate:"omitempty"`
|
||||
RealizationDate string `query:"realization_date" validate:"omitempty"`
|
||||
}
|
||||
|
||||
type MarketingQuery struct {
|
||||
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"`
|
||||
CustomerId int64 `query:"customer_id" validate:"omitempty"`
|
||||
ProjectFlockKandangId int64 `query:"project_flock_kandang_id" validate:"omitempty"`
|
||||
DeliveryDate string `query:"delivery_date" validate:"omitempty"`
|
||||
ProductId int64 `query:"product_id" validate:"omitempty"`
|
||||
WarehouseId int64 `query:"warehouse_id" validate:"omitempty"`
|
||||
SalesPersonId int64 `query:"sales_person_id" validate:"omitempty"`
|
||||
MarketingId int64 `query:"marketing_id" validate:"omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user