package dto import ( "fmt" "time" entity "gitlab.com/mbugroup/lti-api.git/internal/entities" userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto" ) // === DTO Structs === type ClosingRelationDTO struct { Id uint `json:"id"` Name string `json:"name"` } type ClosingListDTO struct { Id uint `json:"id"` Name string `json:"name"` CreatedUser *userDTO.UserRelationDTO `json:"created_user"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } type ClosingDetailDTO struct { ClosingListDTO } type ClosingListItemDTO struct { Id uint `json:"id"` LocationID uint `json:"location_id"` LocationName string `json:"location_name"` ProjectCategory string `json:"project_category"` Period int `json:"period"` ClosingDate string `json:"closing_date"` ShedLabel string `json:"shed_label"` ShedCount int `json:"shed_count"` SalesPaidAmount int64 `json:"sales_paid_amount"` SalesRemainingAmount int64 `json:"sales_remaining_amount"` SalesPaymentStatus string `json:"sales_payment_status"` ProjectStatus string `json:"project_status"` } type ClosingSummaryDTO struct { FlockID uint `json:"flock_id"` Period int `json:"period"` // JenisProduk string `json:"jenis_produk"` // LabelPopulasi string `json:"label_populasi"` Population int `json:"population"` PopulationFormatted string `json:"population_formatted"` ProjectType string `json:"project_type"` ActiveHouseCount int `json:"active_house_count"` ActiveHouseLabel string `json:"active_house_label"` SalesPaymentStatus string `json:"sales_payment_status"` // StatusPembayaranMitra string `json:"status_pembayaran_mitra"` StatusProject string `json:"project_status"` StatusClosing string `json:"closing_status"` } type ClosingPurchaseDTO struct { InitialPopulation int `json:"initial_population"` ClaimCulling int `json:"claim_culling"` FinalPopulation int `json:"final_population"` FeedIn float64 `json:"feed_in"` FeedUsed float64 `json:"feed_used"` FeedUsedPerHead float64 `json:"feed_used_per_head"` } type ClosingSalesDTO struct { SalesPopulation int `json:"sales_population"` SalesWeight float64 `json:"sales_weight"` AverageWeight float64 `json:"average_weight"` AverageSellingPrice float64 `json:"chicken_average_selling_price"` } type ClosingEggSalesDTO struct { EggPieces int `json:"egg_pieces"` EggMassKg float64 `json:"egg_mass_kg"` AverageEggWeightKg float64 `json:"average_egg_weight_kg"` AverageSellingPrice float64 `json:"egg_average_selling_price"` } type ClosingPerformanceDTO struct { Depletion float64 `json:"depletion"` Age float64 `json:"age_day"` MortalityStd float64 `json:"mortality_std"` MortalityAct float64 `json:"mortality_act"` DeffMortality float64 `json:"deff_mortality"` FcrStd float64 `json:"fcr_std"` FcrAct float64 `json:"fcr_act"` DeffFcr float64 `json:"deff_fcr"` Awg float64 `json:"awg"` } type ClosingSalesGroupDTO struct { Chicken ClosingSalesDTO `json:"chicken"` Egg *ClosingEggSalesDTO `json:"egg,omitempty"` } type ClosingProductionReportDTO struct { Purchase ClosingPurchaseDTO `json:"purchase"` Sales ClosingSalesGroupDTO `json:"sales"` Performance ClosingPerformanceDTO `json:"performance"` } func ToClosingSummaryDTO(project entity.ProjectFlock, statusProject, statusClosing string) ClosingSummaryDTO { history := project.KandangHistory period := maxPeriod(history) kandangCount := len(history) population := sumPopulation(history) populationInt := int(population) return ClosingSummaryDTO{ FlockID: project.Id, Period: period, // JenisProduk: project.Category, // LabelPopulasi: "", Population: populationInt, PopulationFormatted: fmt.Sprintf("%d Ekor", populationInt), ProjectType: project.Category, ActiveHouseCount: kandangCount, ActiveHouseLabel: fmt.Sprintf("%d Kandang", kandangCount), SalesPaymentStatus: "Tempo", // StatusPembayaranMitra: "", StatusProject: statusProject, StatusClosing: statusClosing, } } func ToClosingListItemDTO(project entity.ProjectFlock, projectStatus string) ClosingListItemDTO { shedCount := len(project.KandangHistory) return ClosingListItemDTO{ Id: project.Id, LocationID: project.LocationId, LocationName: project.Location.Name, ProjectCategory: project.Category, Period: maxPeriod(project.KandangHistory), ClosingDate: "17-Nov-2025", ShedLabel: fmt.Sprintf("%d Kandang", shedCount), ShedCount: shedCount, SalesPaidAmount: 21993726, SalesRemainingAmount: 11075919, SalesPaymentStatus: "Lunas", ProjectStatus: projectStatus, } } func maxPeriod(history []entity.ProjectFlockKandang) int { max := 0 for _, h := range history { if h.Period > max { max = h.Period } } return max } func sumPopulation(history []entity.ProjectFlockKandang) float64 { var total float64 for _, h := range history { for _, chickin := range h.Chickins { total += chickin.UsageQty + chickin.PendingUsageQty } } return total } // === Mapper Functions === func ToClosingRelationDTO(e entity.ProjectFlock) ClosingRelationDTO { return ClosingRelationDTO{ Id: e.Id, } } func ToClosingListDTO(e entity.ProjectFlock) ClosingListDTO { var createdUser *userDTO.UserRelationDTO if e.CreatedUser.Id != 0 { mapped := userDTO.ToUserRelationDTO(e.CreatedUser) createdUser = &mapped } return ClosingListDTO{ Id: e.Id, CreatedAt: e.CreatedAt, UpdatedAt: e.UpdatedAt, CreatedUser: createdUser, } } func ToClosingListDTOs(e []entity.ProjectFlock) []ClosingListDTO { result := make([]ClosingListDTO, len(e)) for i, r := range e { result[i] = ToClosingListDTO(r) } return result } func ToClosingDetailDTO(e entity.ProjectFlock) ClosingDetailDTO { return ClosingDetailDTO{ ClosingListDTO: ToClosingListDTO(e), } }