mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
FEAT[BE] :add week calculation and chickin preload to product warehouse services
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
productDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/products/dto"
|
||||
warehouseDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/warehouses/dto"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
@@ -22,6 +23,7 @@ type ProductWarehouseListDTO struct {
|
||||
Product *productDTO.ProductRelationDTO `json:"product,omitempty"`
|
||||
Warehouse *warehouseDTO.WarehouseRelationDTO `json:"warehouse,omitempty"`
|
||||
ProjectFlockKandang *ProjectFlockKandangRelationDTO `json:"project_flock_kandang,omitempty"`
|
||||
Week int `json:"week"`
|
||||
CreatedUser *UserRelationDTO `json:"created_user,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
@@ -109,6 +111,22 @@ func ToProductWarehouseListDTO(e entity.ProductWarehouse) ProductWarehouseListDT
|
||||
}
|
||||
|
||||
dto.ProjectFlockKandang = pfkDTO
|
||||
|
||||
// Calculate week for AYAM_PULLET/AYAM products
|
||||
productFlags := make([]string, len(e.Product.Flags))
|
||||
for i, f := range e.Product.Flags {
|
||||
productFlags[i] = f.Name
|
||||
}
|
||||
|
||||
var category string
|
||||
if e.ProjectFlockKandang.ProjectFlock.Id != 0 {
|
||||
category = e.ProjectFlockKandang.ProjectFlock.Category
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
_, ageInWeeks := calculateAgeFromChickin(e.ProjectFlockKandang, &now, productFlags, category)
|
||||
|
||||
dto.Week = ageInWeeks
|
||||
}
|
||||
|
||||
return dto
|
||||
@@ -138,3 +156,58 @@ func ToProductWarehouseNestedDTO(e entity.ProductWarehouse) ProductWarehousNeste
|
||||
Warehouse: &warehouse,
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to calculate age from chickin (same logic as closingMarketing.dto.go)
|
||||
func calculateAgeFromChickin(projectFlockKandang *entity.ProjectFlockKandang, currentDate *time.Time, productFlags []string, category string) (int, int) {
|
||||
if projectFlockKandang == nil || currentDate == nil || len(projectFlockKandang.Chickins) == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
// Return 0 for TRADING, TELUR, and AYAM flags (only AYAM_PULLET should have week)
|
||||
for _, flag := range productFlags {
|
||||
if flag == string(utils.FlagOVK) ||
|
||||
flag == string(utils.FlagPakan) ||
|
||||
flag == string(utils.FlagPreStarter) ||
|
||||
flag == string(utils.FlagStarter) ||
|
||||
flag == string(utils.FlagFinisher) ||
|
||||
flag == string(utils.FlagObat) ||
|
||||
flag == string(utils.FlagVitamin) ||
|
||||
flag == string(utils.FlagKimia) ||
|
||||
flag == string(utils.FlagEkspedisi) ||
|
||||
flag == string(utils.FlagTelur) ||
|
||||
flag == string(utils.FlagTelurUtuh) ||
|
||||
flag == string(utils.FlagTelurPecah) ||
|
||||
flag == string(utils.FlagTelurPutih) ||
|
||||
flag == string(utils.FlagTelurRetak) ||
|
||||
flag == string(utils.FlagAyamAfkir) ||
|
||||
flag == string(utils.FlagAyamCulling) ||
|
||||
flag == string(utils.FlagAyamMati) {
|
||||
return 0, 0
|
||||
}
|
||||
}
|
||||
|
||||
// Find earliest chickin date
|
||||
earliestChickinDate := projectFlockKandang.Chickins[0].ChickInDate
|
||||
for _, chickin := range projectFlockKandang.Chickins {
|
||||
if chickin.ChickInDate.Before(earliestChickinDate) {
|
||||
earliestChickinDate = chickin.ChickInDate
|
||||
}
|
||||
}
|
||||
|
||||
diff := currentDate.Sub(earliestChickinDate)
|
||||
ageInDays := int(diff.Hours() / 24)
|
||||
|
||||
var ageInWeeks int
|
||||
if ageInDays <= 0 {
|
||||
ageInWeeks = 0
|
||||
} else {
|
||||
if category == string(utils.ProjectFlockCategoryLaying) {
|
||||
ageInDays = ageInDays + 119
|
||||
ageInWeeks = ((ageInDays - 1) / 7) + 1
|
||||
} else {
|
||||
ageInWeeks = ((ageInDays - 1) / 7) + 1
|
||||
}
|
||||
}
|
||||
|
||||
return ageInDays, ageInWeeks
|
||||
}
|
||||
|
||||
@@ -46,7 +46,8 @@ func (s productWarehouseService) withRelations(db *gorm.DB) *gorm.DB {
|
||||
Preload("Warehouse.Area").
|
||||
Preload("Warehouse.Kandang").
|
||||
Preload("ProjectFlockKandang").
|
||||
Preload("ProjectFlockKandang.ProjectFlock")
|
||||
Preload("ProjectFlockKandang.ProjectFlock").
|
||||
Preload("ProjectFlockKandang.Chickins")
|
||||
}
|
||||
|
||||
func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProductWarehouse, int64, error) {
|
||||
|
||||
Reference in New Issue
Block a user