mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
268 lines
9.1 KiB
Go
268 lines
9.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/modules/repports/dto"
|
|
repportRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/repports/repositories"
|
|
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"
|
|
chickinRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/chickins/repositories"
|
|
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
|
purchaseRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/purchases/repositories"
|
|
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
|
|
"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)
|
|
GetPurchaseSupplier(ctx *fiber.Ctx, params *validation.PurchaseSupplierQuery) ([]dto.PurchaseSupplierDTO, int64, error)
|
|
}
|
|
|
|
type repportService struct {
|
|
Log *logrus.Logger
|
|
Validate *validator.Validate
|
|
ExpenseRealizationRepo expenseRepo.ExpenseRealizationRepository
|
|
MarketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository
|
|
PurchaseRepo purchaseRepo.PurchaseRepository
|
|
ChickinRepo chickinRepo.ProjectChickinRepository
|
|
RecordingRepo recordingRepo.RecordingRepository
|
|
ApprovalSvc approvalService.ApprovalService
|
|
PurchaseSupplierRepo repportRepo.PurchaseSupplierRepository
|
|
}
|
|
|
|
func NewRepportService(
|
|
validate *validator.Validate,
|
|
expenseRealizationRepo expenseRepo.ExpenseRealizationRepository,
|
|
marketingDeliveryRepo marketingRepo.MarketingDeliveryProductRepository,
|
|
purchaseRepo purchaseRepo.PurchaseRepository,
|
|
chickinRepo chickinRepo.ProjectChickinRepository,
|
|
recordingRepo recordingRepo.RecordingRepository,
|
|
approvalSvc approvalService.ApprovalService,
|
|
purchaseSupplierRepo repportRepo.PurchaseSupplierRepository,
|
|
) RepportService {
|
|
return &repportService{
|
|
Log: utils.Log,
|
|
Validate: validate,
|
|
ExpenseRealizationRepo: expenseRealizationRepo,
|
|
MarketingDeliveryRepo: marketingDeliveryRepo,
|
|
PurchaseRepo: purchaseRepo,
|
|
ChickinRepo: chickinRepo,
|
|
RecordingRepo: recordingRepo,
|
|
ApprovalSvc: approvalSvc,
|
|
PurchaseSupplierRepo: purchaseSupplierRepo,
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
projectFlockIDMap := make(map[uint]bool)
|
|
hppMap := make(map[uint]float64)
|
|
|
|
for _, dp := range deliveryProducts {
|
|
if projectFlockKandang := dp.MarketingProduct.ProductWarehouse.ProjectFlockKandang; projectFlockKandang != nil {
|
|
projectFlockID := projectFlockKandang.ProjectFlockId
|
|
if projectFlockID > 0 && !projectFlockIDMap[projectFlockID] {
|
|
projectFlockIDMap[projectFlockID] = true
|
|
|
|
category := projectFlockKandang.ProjectFlock.Category
|
|
hppPerKg := s.calculateHppPricePerKg(c.Context(), projectFlockID, category)
|
|
hppMap[projectFlockID] = hppPerKg
|
|
}
|
|
}
|
|
}
|
|
|
|
items := dto.ToRepportMarketingItemDTOsWithHppMap(deliveryProducts, hppMap)
|
|
return items, total, nil
|
|
}
|
|
|
|
func (s *repportService) calculateHppPricePerKg(ctx context.Context, projectFlockID uint, category string) float64 {
|
|
totalCost := s.getTotalProjectCost(ctx, projectFlockID)
|
|
if totalCost == 0 {
|
|
s.Log.Warnf("HPP calculation: No cost found for project flock ID %d. Check if purchase items are linked to project_flock_kandang_id", projectFlockID)
|
|
return 0
|
|
}
|
|
|
|
chickinQty, err := s.ChickinRepo.GetTotalChickinQtyByProjectFlockID(ctx, projectFlockID)
|
|
if err != nil {
|
|
s.Log.Warnf("HPP calculation: Failed to get chickin qty for project flock ID %d: %v", projectFlockID, err)
|
|
}
|
|
|
|
depletion, err := s.RecordingRepo.GetTotalDepletionByProjectFlockID(ctx, projectFlockID)
|
|
if err != nil {
|
|
s.Log.Warnf("HPP calculation: Failed to get depletion for project flock ID %d: %v", projectFlockID, err)
|
|
}
|
|
|
|
avgWeight, err := s.RecordingRepo.GetLatestAvgWeightByProjectFlockID(ctx, projectFlockID)
|
|
if err != nil {
|
|
s.Log.Warnf("HPP calculation: Failed to get avg weight for project flock ID %d: %v", projectFlockID, err)
|
|
}
|
|
|
|
var totalWeight float64
|
|
if utils.ProjectFlockCategory(category) == utils.ProjectFlockCategoryGrowing {
|
|
totalWeight = (chickinQty - depletion) * avgWeight
|
|
} else {
|
|
eggWeight, err := s.RecordingRepo.GetTotalEggProductionWeightByProjectFlockID(ctx, projectFlockID)
|
|
if err != nil {
|
|
s.Log.Warnf("HPP calculation: Failed to get egg weight for project flock ID %d: %v", projectFlockID, err)
|
|
}
|
|
totalWeight = (chickinQty-depletion)*avgWeight + eggWeight
|
|
}
|
|
|
|
if totalWeight == 0 {
|
|
return 0
|
|
}
|
|
|
|
hppPricePerKg := totalCost / totalWeight
|
|
return hppPricePerKg
|
|
}
|
|
|
|
func (s *repportService) getTotalProjectCost(ctx context.Context, projectFlockID uint) float64 {
|
|
if projectFlockID == 0 {
|
|
return 0
|
|
}
|
|
|
|
purchases, err := s.PurchaseRepo.GetItemsByProjectFlockID(ctx, projectFlockID)
|
|
if err != nil {
|
|
s.Log.Errorf("getTotalProjectCost: GetItemsByProjectFlockID error for project flock ID %d: %v", projectFlockID, err)
|
|
return 0
|
|
}
|
|
|
|
cost := float64(0)
|
|
purchaseCost := float64(0)
|
|
for _, p := range purchases {
|
|
purchaseCost += p.TotalPrice
|
|
}
|
|
cost += purchaseCost
|
|
|
|
realizations, err := s.ExpenseRealizationRepo.GetByProjectFlockID(ctx, projectFlockID)
|
|
if err != nil {
|
|
s.Log.Warnf("getTotalProjectCost: GetByProjectFlockID error for project flock ID %d: %v", projectFlockID, err)
|
|
}
|
|
|
|
bopCost := float64(0)
|
|
for _, r := range realizations {
|
|
if r.ExpenseNonstock != nil && r.ExpenseNonstock.Expense != nil &&
|
|
r.ExpenseNonstock.Expense.Category == string(utils.ExpenseCategoryBOP) {
|
|
bopCost += r.Price * r.Qty
|
|
}
|
|
}
|
|
cost += bopCost
|
|
|
|
return cost
|
|
}
|
|
|
|
func (s *repportService) GetPurchaseSupplier(c *fiber.Ctx, params *validation.PurchaseSupplierQuery) ([]dto.PurchaseSupplierDTO, int64, error) {
|
|
if err := s.Validate.Struct(params); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (params.Page - 1) * params.Limit
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
|
|
suppliers, totalSuppliers, err := s.PurchaseSupplierRepo.GetSuppliersWithPurchases(c.Context(), offset, params.Limit, params)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if totalSuppliers == 0 || len(suppliers) == 0 {
|
|
return []dto.PurchaseSupplierDTO{}, totalSuppliers, nil
|
|
}
|
|
|
|
supplierMap := make(map[uint]entity.Supplier, len(suppliers))
|
|
supplierIDs := make([]uint, 0, len(suppliers))
|
|
for _, supplier := range suppliers {
|
|
supplierMap[supplier.Id] = supplier
|
|
supplierIDs = append(supplierIDs, supplier.Id)
|
|
}
|
|
|
|
items, err := s.PurchaseSupplierRepo.GetItemsBySuppliers(c.Context(), supplierIDs, params)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
itemsBySupplier := make(map[uint][]entity.PurchaseItem)
|
|
for _, item := range items {
|
|
if item.Purchase == nil {
|
|
continue
|
|
}
|
|
supplierID := item.Purchase.SupplierId
|
|
itemsBySupplier[supplierID] = append(itemsBySupplier[supplierID], item)
|
|
}
|
|
|
|
result := make([]dto.PurchaseSupplierDTO, 0, len(supplierIDs))
|
|
for _, supplierID := range supplierIDs {
|
|
supplier, exists := supplierMap[supplierID]
|
|
if !exists {
|
|
continue
|
|
}
|
|
|
|
supplierItems := itemsBySupplier[supplierID]
|
|
dtoItem := dto.ToPurchaseSupplierDTO(supplier, supplierItems)
|
|
result = append(result, dtoItem)
|
|
}
|
|
|
|
return result, totalSuppliers, nil
|
|
}
|