mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-22 22:35:43 +00:00
207 lines
7.2 KiB
Go
207 lines
7.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"
|
|
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
|
purchaseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/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
|
|
PurchaseRepo purchaseRepo.PurchaseRepository
|
|
RecordingRepo recordingRepo.RecordingRepository
|
|
ApprovalSvc approvalService.ApprovalService
|
|
}
|
|
|
|
func NewRepportService(validate *validator.Validate, expenseRealizationRepo expenseRepo.ExpenseRealizationRepository, marketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository, purchaseRepo purchaseRepo.PurchaseRepository, recordingRepo recordingRepo.RecordingRepository, approvalSvc approvalService.ApprovalService) RepportService {
|
|
return &repportService{
|
|
Log: utils.Log,
|
|
Validate: validate,
|
|
ExpenseRealizationRepo: expenseRealizationRepo,
|
|
MarketingDeliveryRepo: marketingDeliveryRepo,
|
|
PurchaseRepo: purchaseRepo,
|
|
RecordingRepo: recordingRepo,
|
|
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 := dto.ToRepportMarketingItemDTOsWithHppMap(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 {
|
|
category := s.getProjectFlockCategory(deliveryProducts, projectFlockID)
|
|
hppPerKg := s.calculateHppByCategory(ctx, category, projectFlockID, deliveryProducts)
|
|
hppMap[projectFlockID] = hppPerKg
|
|
}
|
|
return hppMap
|
|
}
|
|
|
|
func (s *repportService) calculateHppByCategory(ctx context.Context, category string, projectFlockID uint, deliveryProducts []entity.MarketingDeliveryProduct) float64 {
|
|
switch utils.ProjectFlockCategory(category) {
|
|
case utils.ProjectFlockCategoryGrowing:
|
|
return s.calculateHppPricePerKg(ctx, projectFlockID, deliveryProducts)
|
|
case utils.ProjectFlockCategoryLaying:
|
|
return 0
|
|
default:
|
|
return 0
|
|
}
|
|
}
|
|
|
|
func (s *repportService) getProjectFlockCategory(deliveryProducts []entity.MarketingDeliveryProduct, projectFlockID uint) string {
|
|
for _, dp := range deliveryProducts {
|
|
if projectFlockKandang := dp.MarketingProduct.ProductWarehouse.ProjectFlockKandang; projectFlockKandang != nil {
|
|
if projectFlockKandang.ProjectFlockId == projectFlockID {
|
|
return projectFlockKandang.ProjectFlock.Category
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (s *repportService) calculateHppPricePerKg(ctx context.Context, projectFlockID uint, deliveryProducts []entity.MarketingDeliveryProduct) float64 {
|
|
if projectFlockID == 0 {
|
|
return 0
|
|
}
|
|
|
|
purchaseItems, err := s.PurchaseRepo.GetItemsByProjectFlockID(ctx, projectFlockID)
|
|
if err != nil {
|
|
s.Log.Warnf("GetItemsByProjectFlockID error: %v", err)
|
|
}
|
|
|
|
costPurchase := float64(0)
|
|
for _, item := range purchaseItems {
|
|
costPurchase += item.TotalPrice
|
|
}
|
|
|
|
realizations, err := s.ExpenseRealizationRepo.GetByProjectFlockID(ctx, projectFlockID)
|
|
if err != nil {
|
|
s.Log.Warnf("GetByProjectFlockID error: %v", err)
|
|
}
|
|
|
|
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 := costPurchase + costBop
|
|
|
|
if totalActualCost == 0 {
|
|
return 0
|
|
}
|
|
|
|
totalWeightProduced, _, err := s.RecordingRepo.GetProductionWeightAndQtyByProjectFlockID(ctx, projectFlockID)
|
|
if err != nil {
|
|
s.Log.Warnf("GetProductionWeightAndQtyByProjectFlockID error: %v", err)
|
|
}
|
|
|
|
if totalWeightProduced == 0 {
|
|
return 0
|
|
}
|
|
|
|
hppPerKg := totalActualCost / totalWeightProduced
|
|
return hppPerKg
|
|
}
|