mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-23 14:55:42 +00:00
190 lines
6.2 KiB
Go
190 lines
6.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
"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"
|
|
marketingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/marketing/repositories"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type RepportService interface {
|
|
GetExpense(ctx *fiber.Ctx, params *validation.ExpenseQuery) ([]dto.RepportExpenseListDTO, int64, error)
|
|
GetMarketing(ctx *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingItemDTO, 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, 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.ExpenseQuery) ([]dto.RepportExpenseListDTO, int64, error) {
|
|
if err := s.Validate.Struct(params); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (params.Page - 1) * params.Limit
|
|
|
|
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
|
|
}
|
|
|
|
result := dto.ToRepportExpenseListDTOs(realizations)
|
|
|
|
expenseIDs := make([]uint, 0, len(result))
|
|
for i := range result {
|
|
expenseIDs = append(expenseIDs, uint(result[i].Id))
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingItemDTO, 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
|
|
}
|
|
|
|
projectFlockIDs := s.collectProjectFlockIDs(deliveryProducts)
|
|
hppMap := s.buildHppMap(c.Context(), projectFlockIDs, deliveryProducts)
|
|
items := s.mapDeliveryProductsToDTOs(deliveryProducts, hppMap)
|
|
|
|
return items, total, nil
|
|
}
|
|
|
|
func (s *repportService) collectProjectFlockIDs(deliveryProducts []entity.MarketingDeliveryProduct) []uint {
|
|
projectFlockIDMap := make(map[uint]bool)
|
|
projectFlockIDs := make([]uint, 0)
|
|
|
|
for _, dp := range deliveryProducts {
|
|
if projectFlockKandang := dp.MarketingProduct.ProductWarehouse.ProjectFlockKandang; projectFlockKandang != nil {
|
|
if projectFlockKandang.ProjectFlockId > 0 && !projectFlockIDMap[projectFlockKandang.ProjectFlockId] {
|
|
projectFlockIDs = append(projectFlockIDs, projectFlockKandang.ProjectFlockId)
|
|
projectFlockIDMap[projectFlockKandang.ProjectFlockId] = true
|
|
}
|
|
}
|
|
}
|
|
|
|
return projectFlockIDs
|
|
}
|
|
|
|
func (s *repportService) buildHppMap(ctx context.Context, projectFlockIDs []uint, deliveryProducts []entity.MarketingDeliveryProduct) map[uint]float64 {
|
|
hppMap := make(map[uint]float64)
|
|
for _, projectFlockID := range projectFlockIDs {
|
|
hppPerKg := s.calculateHppPricePerKg(ctx, projectFlockID, deliveryProducts)
|
|
hppMap[projectFlockID] = hppPerKg
|
|
}
|
|
return hppMap
|
|
}
|
|
|
|
func (s *repportService) mapDeliveryProductsToDTOs(deliveryProducts []entity.MarketingDeliveryProduct, hppMap map[uint]float64) []dto.RepportMarketingItemDTO {
|
|
items := make([]dto.RepportMarketingItemDTO, 0, len(deliveryProducts))
|
|
for _, dp := range deliveryProducts {
|
|
hppPerKg := float64(0)
|
|
if projectFlockKandang := dp.MarketingProduct.ProductWarehouse.ProjectFlockKandang; projectFlockKandang != nil {
|
|
if hpp, exists := hppMap[projectFlockKandang.ProjectFlockId]; exists {
|
|
hppPerKg = hpp
|
|
}
|
|
}
|
|
items = append(items, dto.ToRepportMarketingItemDTO(dp, hppPerKg))
|
|
}
|
|
return items
|
|
}
|
|
|
|
func (s *repportService) calculateHppPricePerKg(ctx context.Context, projectFlockID uint, deliveryProducts []entity.MarketingDeliveryProduct) float64 {
|
|
if projectFlockID == 0 {
|
|
return 0
|
|
}
|
|
|
|
realizations, err := s.ExpenseRealizationRepo.GetByProjectFlockID(ctx, projectFlockID)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
|
|
if len(realizations) == 0 {
|
|
return 0
|
|
}
|
|
|
|
costBop := float64(0)
|
|
|
|
for _, realization := range realizations {
|
|
cost := realization.Price * realization.Qty
|
|
category := ""
|
|
if realization.ExpenseNonstock != nil && realization.ExpenseNonstock.Expense != nil {
|
|
category = realization.ExpenseNonstock.Expense.Category
|
|
}
|
|
|
|
if category == "BOP" {
|
|
costBop += cost
|
|
}
|
|
}
|
|
|
|
totalActualCost := costBop
|
|
|
|
if totalActualCost == 0 {
|
|
return 0
|
|
}
|
|
|
|
totalWeightSold := float64(0)
|
|
for _, dp := range deliveryProducts {
|
|
if dp.MarketingProduct.ProductWarehouse.ProjectFlockKandang != nil &&
|
|
dp.MarketingProduct.ProductWarehouse.ProjectFlockKandang.ProjectFlockId == projectFlockID {
|
|
totalWeightSold += dp.TotalWeight
|
|
}
|
|
}
|
|
|
|
if totalWeightSold == 0 {
|
|
return 0
|
|
}
|
|
|
|
hppPerKg := totalActualCost / totalWeightSold
|
|
return hppPerKg
|
|
}
|