diff --git a/internal/modules/closings/dto/closingMarketing.dto.go b/internal/modules/closings/dto/closingMarketing.dto.go index eb6ff23f..d725b430 100644 --- a/internal/modules/closings/dto/closingMarketing.dto.go +++ b/internal/modules/closings/dto/closingMarketing.dto.go @@ -15,6 +15,7 @@ type SalesDTO struct { Id uint `json:"id"` RealizationDate time.Time `json:"realization_date"` Age int `json:"age"` + Week int `json:"week"` DoNumber string `json:"do_number"` Product *productDTO.ProductRelationDTO `json:"product,omitempty"` Customer *customerDTO.CustomerRelationDTO `json:"customer,omitempty"` @@ -43,7 +44,7 @@ type PenjualanRealisasiResponseDTO struct { func ToSalesDTO(e entity.MarketingDeliveryProduct) SalesDTO { - age := calculateAgeFromChickin(e.MarketingProduct.ProductWarehouse.ProjectFlockKandang, e.DeliveryDate) + ageInDay, ageInWeeks := calculateAgeFromChickin(e.MarketingProduct.ProductWarehouse.ProjectFlockKandang, e.DeliveryDate) var product *productDTO.ProductRelationDTO if e.MarketingProduct.ProductWarehouse.Product.Id != 0 { @@ -73,7 +74,8 @@ func ToSalesDTO(e entity.MarketingDeliveryProduct) SalesDTO { return SalesDTO{ Id: e.Id, RealizationDate: realizationDate, - Age: age, + Age: ageInDay, + Week: ageInWeeks, DoNumber: doNumber, Product: product, Customer: customer, @@ -124,9 +126,9 @@ func ToPenjualanRealisasiResponseDTO(e []entity.MarketingDeliveryProduct) Penjua } } -func calculateAgeFromChickin(projectFlockKandang *entity.ProjectFlockKandang, deliveryDate *time.Time) int { +func calculateAgeFromChickin(projectFlockKandang *entity.ProjectFlockKandang, deliveryDate *time.Time) (int, int) { if projectFlockKandang == nil || deliveryDate == nil || len(projectFlockKandang.Chickins) == 0 { - return 0 + return 0, 0 } earliestChickinDate := projectFlockKandang.Chickins[0].ChickInDate @@ -136,7 +138,16 @@ func calculateAgeFromChickin(projectFlockKandang *entity.ProjectFlockKandang, de } } - ageInDays := int(deliveryDate.Sub(earliestChickinDate).Hours() / 24) - ageInWeeks := ageInDays / 7 - return ageInWeeks + diff := deliveryDate.Sub(earliestChickinDate) + ageInDays := int(diff.Hours() / 24) + + var ageInWeeks int + if ageInDays <= 0 { + ageInWeeks = 0 + } else { + + ageInWeeks = ((ageInDays - 1) / 7) + 1 + } + + return ageInDays, ageInWeeks }