mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 15:25:43 +00:00
feat[BE}: change get penjualan repport dto an add more params
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
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"
|
||||
@@ -18,7 +21,7 @@ import (
|
||||
|
||||
type RepportService interface {
|
||||
GetExpense(ctx *fiber.Ctx, params *validation.ExpenseQuery) ([]dto.RepportExpenseListDTO, int64, error)
|
||||
GetMarketing(ctx *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingListDTO, int64, error)
|
||||
GetMarketing(ctx *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingItemDTO, int64, error)
|
||||
}
|
||||
|
||||
type repportService struct {
|
||||
@@ -77,7 +80,7 @@ func (s *repportService) GetExpense(c *fiber.Ctx, params *validation.ExpenseQuer
|
||||
return result, total, nil
|
||||
}
|
||||
|
||||
func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.MarketingQuery) ([]dto.RepportMarketingListDTO, int64, error) {
|
||||
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
|
||||
}
|
||||
@@ -89,27 +92,88 @@ func (s *repportService) GetMarketing(c *fiber.Ctx, params *validation.Marketing
|
||||
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
|
||||
}
|
||||
}
|
||||
projectFlockIDs := s.collectProjectFlockIDs(deliveryProducts)
|
||||
hppMap := s.buildHppMap(c.Context(), projectFlockIDs, deliveryProducts)
|
||||
items := s.mapDeliveryProductsToDTOs(deliveryProducts, hppMap)
|
||||
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
totalActualCost := float64(0)
|
||||
for _, realization := range realizations {
|
||||
cost := realization.Price * realization.Qty
|
||||
totalActualCost += cost
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user