FEAT[BE[: enhance marketing report items with aging days calculation

This commit is contained in:
aguhh18
2026-01-20 22:28:34 +07:00
parent dd4dcc1c39
commit ad3bb0e29a
2 changed files with 54 additions and 67 deletions
@@ -53,11 +53,10 @@ type ProductRelationDTOFixed struct {
SellingPrice *float64 `json:"selling_price,omitempty"`
}
func ToMarketingReportItems(mdps []entity.MarketingDeliveryProduct, hppMap map[uint]float64) []RepportMarketingItemDTO {
func ToMarketingReportItems(mdps []entity.MarketingDeliveryProduct, hppMap map[uint]float64, agingMap map[int]int) []RepportMarketingItemDTO {
items := make([]RepportMarketingItemDTO, 0, len(mdps))
for _, mdp := range mdps {
// Get HPP and category from map
hppPerKg := float64(0)
category := ""
if projectFlockKandang := mdp.MarketingProduct.ProductWarehouse.ProjectFlockKandang; projectFlockKandang != nil {
@@ -67,12 +66,15 @@ func ToMarketingReportItems(mdps []entity.MarketingDeliveryProduct, hppMap map[u
category = projectFlockKandang.ProjectFlock.Category
}
// Calculate dates
soDate := time.Time{}
agingDays := 0
if mdp.MarketingProduct.Marketing.SoDate.Year() > 1 {
soDate = mdp.MarketingProduct.Marketing.SoDate
agingDays = int(time.Since(soDate).Hours() / 24)
if ag, exists := agingMap[int(mdp.Id)]; exists {
agingDays = ag
} else {
agingDays = int(time.Since(soDate).Hours() / 24)
}
}
realizationDate := time.Time{}
@@ -106,7 +108,6 @@ func ToMarketingReportItems(mdps []entity.MarketingDeliveryProduct, hppMap map[u
}
}
// Determine marketing type
marketingType := "trading"
if hasTrading {
marketingType = "trading"
@@ -196,13 +197,9 @@ func ToSummaryFromDTOItems(items []RepportMarketingItemDTO) *Summary {
totalHppPricePerKg = float64(totalHppAmount) / totalWeightKg
}
if len(items) > 0 {
avgSalesAmount = float64(totalSalesAmount) / float64(len(items))
}
if totalQty > 0 {
avgWeightKg = totalWeightKg / float64(totalQty)
avgSalesAmount = float64(totalSalesAmount) / float64(totalQty) // ← TAMBAHAN INI
avgSalesAmount = float64(totalSalesAmount) / float64(totalQty)
}
return &Summary{