mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Feat[BE]: refactored Chickin createone and implement approvals and add more needed constant
This commit is contained in:
+136
-136
@@ -93,9 +93,9 @@ func Run(db *gorm.DB) error {
|
||||
if err := seedTransferStock(tx, adminID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := seedChickin(tx, adminID); err != nil {
|
||||
return err
|
||||
}
|
||||
// if err := seedChickin(tx, adminID); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
fmt.Println("✅ Master data seeding completed")
|
||||
return nil
|
||||
@@ -1134,151 +1134,151 @@ func seedTransferStock(tx *gorm.DB, createdBy uint) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedChickin(tx *gorm.DB, createdBy uint) error {
|
||||
seeds := []struct {
|
||||
ProjectFlockKandangId uint
|
||||
ChickInDate string
|
||||
Quantity float64
|
||||
Note string
|
||||
}{
|
||||
{ProjectFlockKandangId: 1, ChickInDate: "2025-10-20", Quantity: 100, Note: "Seeder chickin 1"},
|
||||
{ProjectFlockKandangId: 2, ChickInDate: "2025-10-21", Quantity: 200, Note: "Seeder chickin 2"},
|
||||
}
|
||||
// func seedChickin(tx *gorm.DB, createdBy uint) error {
|
||||
// seeds := []struct {
|
||||
// ProjectFlockKandangId uint
|
||||
// ChickInDate string
|
||||
// Quantity float64
|
||||
// Note string
|
||||
// }{
|
||||
// {ProjectFlockKandangId: 1, ChickInDate: "2025-10-20", Quantity: 100, Note: "Seeder chickin 1"},
|
||||
// {ProjectFlockKandangId: 2, ChickInDate: "2025-10-21", Quantity: 200, Note: "Seeder chickin 2"},
|
||||
// }
|
||||
|
||||
for _, seed := range seeds {
|
||||
chickinDate, err := time.Parse("2006-01-02", seed.ChickInDate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// for _, seed := range seeds {
|
||||
// chickinDate, err := time.Parse("2006-01-02", seed.ChickInDate)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// Insert ProjectChickin jika belum ada
|
||||
var chickin entity.ProjectChickin
|
||||
err = tx.Where("project_flock_kandang_id = ? AND chick_in_date = ?", seed.ProjectFlockKandangId, chickinDate).
|
||||
First(&chickin).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
chickin = entity.ProjectChickin{
|
||||
ProjectFlockKandangId: seed.ProjectFlockKandangId,
|
||||
ChickInDate: chickinDate,
|
||||
Quantity: seed.Quantity,
|
||||
Note: seed.Note,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&chickin).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
// // Insert ProjectChickin jika belum ada
|
||||
// var chickin entity.ProjectChickin
|
||||
// err = tx.Where("project_flock_kandang_id = ? AND chick_in_date = ?", seed.ProjectFlockKandangId, chickinDate).
|
||||
// First(&chickin).Error
|
||||
// if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// chickin = entity.ProjectChickin{
|
||||
// ProjectFlockKandangId: seed.ProjectFlockKandangId,
|
||||
// ChickInDate: chickinDate,
|
||||
// Quantity: seed.Quantity,
|
||||
// Note: seed.Note,
|
||||
// CreatedBy: createdBy,
|
||||
// }
|
||||
// if err := tx.Create(&chickin).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// } else if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
var population entity.ProjectFlockPopulation
|
||||
err = tx.Where("project_flock_kandang_id = ?", seed.ProjectFlockKandangId).First(&population).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
population = entity.ProjectFlockPopulation{
|
||||
ProjectFlockKandangId: seed.ProjectFlockKandangId,
|
||||
InitialQuantity: seed.Quantity,
|
||||
CurrentQuantity: seed.Quantity,
|
||||
ReservedQuantity: 0,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&population).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else {
|
||||
// Update population quantities
|
||||
if err := tx.Model(&entity.ProjectFlockPopulation{}).
|
||||
Where("id = ?", population.Id).
|
||||
Updates(map[string]any{
|
||||
"initial_quantity": population.InitialQuantity + seed.Quantity,
|
||||
"current_quantity": population.CurrentQuantity + seed.Quantity,
|
||||
"reserved_quantity": 0,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// var population entity.ProjectFlockPopulation
|
||||
// err = tx.Where("project_flock_kandang_id = ?", seed.ProjectFlockKandangId).First(&population).Error
|
||||
// if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// population = entity.ProjectFlockPopulation{
|
||||
// ProjectFlockKandangId: seed.ProjectFlockKandangId,
|
||||
// InitialQuantity: seed.Quantity,
|
||||
// CurrentQuantity: seed.Quantity,
|
||||
// ReservedQuantity: 0,
|
||||
// CreatedBy: createdBy,
|
||||
// }
|
||||
// if err := tx.Create(&population).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// } else if err != nil {
|
||||
// return err
|
||||
// } else {
|
||||
// // Update population quantities
|
||||
// if err := tx.Model(&entity.ProjectFlockPopulation{}).
|
||||
// Where("id = ?", population.Id).
|
||||
// Updates(map[string]any{
|
||||
// "initial_quantity": population.InitialQuantity + seed.Quantity,
|
||||
// "current_quantity": population.CurrentQuantity + seed.Quantity,
|
||||
// "reserved_quantity": 0,
|
||||
// }).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
||||
var pfk entity.ProjectFlockKandang
|
||||
if err := tx.Where("id = ?", seed.ProjectFlockKandangId).First(&pfk).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// no pivot found; skip creating details
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
// var pfk entity.ProjectFlockKandang
|
||||
// if err := tx.Where("id = ?", seed.ProjectFlockKandangId).First(&pfk).Error; err != nil {
|
||||
// if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// // no pivot found; skip creating details
|
||||
// continue
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
|
||||
var warehouse entity.Warehouse
|
||||
if err := tx.Where("kandang_id = ?", pfk.KandangId).First(&warehouse).Error; err != nil {
|
||||
// if warehouse not found, cannot create details
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
// var warehouse entity.Warehouse
|
||||
// if err := tx.Where("kandang_id = ?", pfk.KandangId).First(&warehouse).Error; err != nil {
|
||||
// // if warehouse not found, cannot create details
|
||||
// if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// continue
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
|
||||
var productWarehouses []entity.ProductWarehouse
|
||||
err = tx.Table("product_warehouses").
|
||||
Select("product_warehouses.*").
|
||||
Joins("JOIN products ON products.id = product_warehouses.product_id").
|
||||
Joins("JOIN product_categories ON product_categories.id = products.product_category_id").
|
||||
Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", "DOC", warehouse.Id).
|
||||
Order("product_warehouses.created_at DESC").
|
||||
Find(&productWarehouses).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// var productWarehouses []entity.ProductWarehouse
|
||||
// err = tx.Table("product_warehouses").
|
||||
// Select("product_warehouses.*").
|
||||
// Joins("JOIN products ON products.id = product_warehouses.product_id").
|
||||
// Joins("JOIN product_categories ON product_categories.id = products.product_category_id").
|
||||
// Where("product_categories.code = ? AND product_warehouses.warehouse_id = ?", "DOC", warehouse.Id).
|
||||
// Order("product_warehouses.created_at DESC").
|
||||
// Find(&productWarehouses).Error
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// If no product warehouses found, keep existing chickin.Quantity and skip details
|
||||
if len(productWarehouses) == 0 {
|
||||
continue
|
||||
}
|
||||
// // If no product warehouses found, keep existing chickin.Quantity and skip details
|
||||
// if len(productWarehouses) == 0 {
|
||||
// continue
|
||||
// }
|
||||
|
||||
// sum all pw quantities and set chickin.Quantity to that total (mimic CreateOne)
|
||||
totalQty := 0.0
|
||||
for _, pw := range productWarehouses {
|
||||
totalQty += pw.Quantity
|
||||
}
|
||||
// // sum all pw quantities and set chickin.Quantity to that total (mimic CreateOne)
|
||||
// totalQty := 0.0
|
||||
// for _, pw := range productWarehouses {
|
||||
// totalQty += pw.Quantity
|
||||
// }
|
||||
|
||||
if chickin.Quantity != totalQty {
|
||||
if err := tx.Model(&entity.ProjectChickin{}).Where("id = ?", chickin.Id).Update("quantity", totalQty).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
chickin.Quantity = totalQty
|
||||
}
|
||||
// if chickin.Quantity != totalQty {
|
||||
// if err := tx.Model(&entity.ProjectChickin{}).Where("id = ?", chickin.Id).Update("quantity", totalQty).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// chickin.Quantity = totalQty
|
||||
// }
|
||||
|
||||
for _, pw := range productWarehouses {
|
||||
// ensure detail exists or create it with full pw.Quantity
|
||||
var detail entity.ProjectChickinDetail
|
||||
err = tx.Where("project_chickin_id = ? AND product_warehouse_id = ?", chickin.Id, pw.Id).First(&detail).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
detail = entity.ProjectChickinDetail{
|
||||
ProjectChickinId: chickin.Id,
|
||||
ProductWarehouseId: pw.Id,
|
||||
Quantity: pw.Quantity,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&detail).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else {
|
||||
if detail.Quantity != pw.Quantity {
|
||||
if err := tx.Model(&entity.ProjectChickinDetail{}).Where("id = ?", detail.Id).Update("quantity", pw.Quantity).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
// for _, pw := range productWarehouses {
|
||||
// // ensure detail exists or create it with full pw.Quantity
|
||||
// var detail entity.ProjectChickinDetail
|
||||
// err = tx.Where("project_chickin_id = ? AND product_warehouse_id = ?", chickin.Id, pw.Id).First(&detail).Error
|
||||
// if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// detail = entity.ProjectChickinDetail{
|
||||
// ProjectChickinId: chickin.Id,
|
||||
// ProductWarehouseId: pw.Id,
|
||||
// Quantity: pw.Quantity,
|
||||
// CreatedBy: createdBy,
|
||||
// }
|
||||
// if err := tx.Create(&detail).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// } else if err != nil {
|
||||
// return err
|
||||
// } else {
|
||||
// if detail.Quantity != pw.Quantity {
|
||||
// if err := tx.Model(&entity.ProjectChickinDetail{}).Where("id = ?", detail.Id).Update("quantity", pw.Quantity).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// zero out pw quantity
|
||||
if err := tx.Model(&entity.ProductWarehouse{}).Where("id = ?", pw.Id).Update("quantity", 0).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
// // zero out pw quantity
|
||||
// if err := tx.Model(&entity.ProductWarehouse{}).Where("id = ?", pw.Id).Update("quantity", 0).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
// return nil
|
||||
// }
|
||||
|
||||
func ptr[T any](v T) *T {
|
||||
return &v
|
||||
|
||||
Reference in New Issue
Block a user