package dto import ( entity "gitlab.com/mbugroup/lti-api.git/internal/entities" customerDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/customers/dto" productDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/dto" warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto" userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto" ) // === Main Report Item DTO === type RepportMarketingItemDTO struct { DoDate string `json:"do_date"` RealizationDate string `json:"realization_date"` AgingDays int `json:"aging_days"` Warehouse *warehouseDTO.WarehouseRelationDTO `json:"warehouse,omitempty"` Customer *customerDTO.CustomerRelationDTO `json:"customer,omitempty"` DoNumber string `json:"do_number"` Sales *userDTO.UserRelationDTO `json:"sales,omitempty"` VehicleNumber string `json:"vehicle_number"` Product *productDTO.ProductRelationDTO `json:"product,omitempty"` MarketingType string `json:"marketing_type"` Qty float64 `json:"qty"` AverageWeightKg float64 `json:"average_weight_kg"` TotalWeightKg float64 `json:"total_weight_kg"` SalesPricePerKg float64 `json:"sales_price_per_kg"` HppPricePerKg float64 `json:"hpp_price_per_kg"` SalesAmount float64 `json:"sales_amount"` HppAmount float64 `json:"hpp_amount"` } // === Report Response DTO === type RepportMarketingResponseDTO struct { Items []RepportMarketingItemDTO `json:"items"` } // === MAPPERS === // ToRepportMarketingItemDTO maps marketing delivery product to detailed report item func ToRepportMarketingItemDTO(mdp entity.MarketingDeliveryProduct, hppPricePerKg float64) RepportMarketingItemDTO { agingDays := 0 doDate := "" if mdp.DeliveryDate != nil { doDate = mdp.DeliveryDate.Format("02-Jan-2006") } realizationDate := "" if mdp.DeliveryDate != nil { realizationDate = mdp.DeliveryDate.Format("02-Jan-2006") } // Calculate sales_amount = total_weight_kg * sales_price_per_kg salesAmount := mdp.TotalWeight * mdp.UnitPrice // Calculate hpp_amount = total_weight_kg * hpp_price_per_kg hppAmount := mdp.TotalWeight * hppPricePerKg item := RepportMarketingItemDTO{ DoDate: doDate, RealizationDate: realizationDate, AgingDays: agingDays, DoNumber: mdp.MarketingProduct.Marketing.SoNumber, MarketingType: "ayam", Qty: mdp.Qty, AverageWeightKg: mdp.AvgWeight, TotalWeightKg: mdp.TotalWeight, SalesPricePerKg: mdp.UnitPrice, HppPricePerKg: hppPricePerKg, SalesAmount: salesAmount, HppAmount: hppAmount, } // Map warehouse with full details if mdp.MarketingProduct.ProductWarehouse.WarehouseId != 0 { mapped := warehouseDTO.ToWarehouseRelationDTO(mdp.MarketingProduct.ProductWarehouse.Warehouse) item.Warehouse = &mapped } // Map customer using CustomerRelationDTO if mdp.MarketingProduct.Marketing.CustomerId != 0 { mapped := customerDTO.ToCustomerRelationDTO(mdp.MarketingProduct.Marketing.Customer) item.Customer = &mapped } // Map sales person if mdp.MarketingProduct.Marketing.SalesPersonId != 0 { mapped := userDTO.ToUserRelationDTO(mdp.MarketingProduct.Marketing.SalesPerson) item.Sales = &mapped } // Map vehicle number item.VehicleNumber = mdp.VehicleNumber // Map product using ProductRelationDTO if mdp.MarketingProduct.ProductWarehouse.ProductId != 0 { mapped := productDTO.ToProductRelationDTO(mdp.MarketingProduct.ProductWarehouse.Product) item.Product = &mapped } return item } // ToRepportMarketingItemDTOs maps array of delivery products to report items with HPP calculation func ToRepportMarketingItemDTOs(mdps []entity.MarketingDeliveryProduct, hppPricePerKg float64) []RepportMarketingItemDTO { items := make([]RepportMarketingItemDTO, 0, len(mdps)) for _, mdp := range mdps { items = append(items, ToRepportMarketingItemDTO(mdp, hppPricePerKg)) } return items } // ToRepportMarketingResponseDTO creates complete marketing report response func ToRepportMarketingResponseDTO(mdps []entity.MarketingDeliveryProduct, hppPricePerKg float64) RepportMarketingResponseDTO { items := ToRepportMarketingItemDTOs(mdps, hppPricePerKg) return RepportMarketingResponseDTO{ Items: items, } }