mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
Merge branch 'development-before-sso' of https://gitlab.com/mbugroup/lti-api into dev/teguh
This commit is contained in:
+105
-415
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
approvalutils "gitlab.com/mbugroup/lti-api.git/internal/utils/approvals"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -41,22 +40,15 @@ func Run(db *gorm.DB) error {
|
||||
return err
|
||||
}
|
||||
|
||||
flocks, err := seedFlocks(tx, adminID)
|
||||
if err != nil {
|
||||
if _, err := seedFlocks(tx, adminID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fcrs, err := seedFcr(tx, adminID)
|
||||
if err != nil {
|
||||
if _, err := seedFcr(tx, adminID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
projectFlocks, err := seedProjectFlocks(tx, adminID, flocks, areas, fcrs, locations)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
kandangs, err := seedKandangs(tx, adminID, locations, users, projectFlocks)
|
||||
kandangs, err := seedKandangs(tx, adminID, locations, users)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -93,10 +85,6 @@ func Run(db *gorm.DB) error {
|
||||
if err := seedTransferStock(tx, adminID); err != nil {
|
||||
return err
|
||||
}
|
||||
// if err := seedChickin(tx, adminID); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
fmt.Println("✅ Master data seeding completed")
|
||||
return nil
|
||||
})
|
||||
@@ -243,159 +231,16 @@ func seedFlocks(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func seedProjectFlocks(tx *gorm.DB, createdBy uint, flocks, areas, fcrs, locations map[string]uint) (map[string]uint, error) {
|
||||
func seedKandangs(tx *gorm.DB, createdBy uint, locations map[string]uint, users map[string]uint) (map[string]uint, error) {
|
||||
seeds := []struct {
|
||||
Key string
|
||||
Flock string
|
||||
Area string
|
||||
Category utils.ProjectFlockCategory
|
||||
Fcr string
|
||||
Name string
|
||||
Status utils.KandangStatus
|
||||
Location string
|
||||
Period int
|
||||
PicKey string
|
||||
}{
|
||||
{
|
||||
Key: "Singaparna Period 1",
|
||||
Flock: "Flock Priangan",
|
||||
Area: "Priangan",
|
||||
Category: utils.ProjectFlockCategoryGrowing,
|
||||
Fcr: "FCR DOC",
|
||||
Location: "Singaparna",
|
||||
Period: 1,
|
||||
},
|
||||
{
|
||||
Key: "Cikaum Period 1",
|
||||
Flock: "Flock Banten",
|
||||
Area: "Banten",
|
||||
Category: utils.ProjectFlockCategoryGrowing,
|
||||
Fcr: "FCR DOC",
|
||||
Location: "Cikaum",
|
||||
Period: 1,
|
||||
},
|
||||
}
|
||||
|
||||
result := make(map[string]uint, len(seeds))
|
||||
|
||||
for _, seed := range seeds {
|
||||
flockID, ok := flocks[seed.Flock]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("floc %s not seeded", seed.Flock)
|
||||
}
|
||||
areaID, ok := areas[seed.Area]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("area %s not seeded", seed.Area)
|
||||
}
|
||||
fcrID, ok := fcrs[seed.Fcr]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("fcr %s not seeded", seed.Fcr)
|
||||
}
|
||||
locationID, ok := locations[seed.Location]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("location %s not seeded", seed.Location)
|
||||
}
|
||||
|
||||
var projectFlock entity.ProjectFlock
|
||||
err := tx.Where("flock_id = ? AND area_id = ? AND category = ? AND fcr_id = ? AND location_id = ? AND period = ?",
|
||||
flockID, areaID, seed.Category, fcrID, locationID, seed.Period).First(&projectFlock).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
projectFlock = entity.ProjectFlock{
|
||||
FlockId: flockID,
|
||||
AreaId: areaID,
|
||||
Category: string(seed.Category),
|
||||
FcrId: fcrID,
|
||||
LocationId: locationID,
|
||||
Period: seed.Period,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&projectFlock).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
if err := tx.Model(&entity.ProjectFlock{}).Where("id = ?", projectFlock.Id).Updates(map[string]any{
|
||||
"flock_id": flockID,
|
||||
"area_id": areaID,
|
||||
"category": string(seed.Category),
|
||||
"fcr_id": fcrID,
|
||||
"location_id": locationID,
|
||||
"period": seed.Period,
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := ensureProjectFlockApprovals(tx, projectFlock.Id, createdBy); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[seed.Key] = projectFlock.Id
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func ensureProjectFlockApprovals(tx *gorm.DB, projectFlockID uint, actorID uint) error {
|
||||
if projectFlockID == 0 || actorID == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
workflow := utils.ApprovalWorkflowProjectFlock.String()
|
||||
|
||||
steps := []struct {
|
||||
step approvalutils.ApprovalStep
|
||||
action entity.ApprovalAction
|
||||
}{
|
||||
{step: utils.ProjectFlockStepPengajuan, action: entity.ApprovalActionCreated},
|
||||
{step: utils.ProjectFlockStepAktif, action: entity.ApprovalActionApproved},
|
||||
}
|
||||
|
||||
for _, cfg := range steps {
|
||||
var count int64
|
||||
if err := tx.Model(&entity.Approval{}).
|
||||
Where("approvable_type = ? AND approvable_id = ? AND step_number = ?", workflow, projectFlockID, uint16(cfg.step)).
|
||||
Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
stepName, ok := utils.ProjectFlockApprovalSteps[cfg.step]
|
||||
if !ok || strings.TrimSpace(stepName) == "" {
|
||||
stepName = fmt.Sprintf("Step %d", cfg.step)
|
||||
}
|
||||
|
||||
var actionPtr *entity.ApprovalAction
|
||||
action := cfg.action
|
||||
actionPtr = &action
|
||||
|
||||
record := entity.Approval{
|
||||
ApprovableType: workflow,
|
||||
ApprovableId: projectFlockID,
|
||||
StepNumber: uint16(cfg.step),
|
||||
StepName: stepName,
|
||||
Action: actionPtr,
|
||||
ActionBy: uintPtr(actorID),
|
||||
}
|
||||
|
||||
if err := tx.Create(&record).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedKandangs(tx *gorm.DB, createdBy uint, locations map[string]uint, users map[string]uint, projectFlocks map[string]uint) (map[string]uint, error) {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
Status utils.KandangStatus
|
||||
Location string
|
||||
PicKey string
|
||||
ProjectFlockKey *string
|
||||
}{
|
||||
{Name: "Singaparna 1", Status: utils.KandangStatusActive, Location: "Singaparna", PicKey: "admin", ProjectFlockKey: strPtr("Singaparna Period 1")},
|
||||
{Name: "Singaparna 1", Status: utils.KandangStatusNonActive, Location: "Singaparna", PicKey: "admin"},
|
||||
{Name: "Singaparna 2", Status: utils.KandangStatusNonActive, Location: "Singaparna", PicKey: "admin"},
|
||||
{Name: "Cikaum 1", Status: utils.KandangStatusActive, Location: "Cikaum", PicKey: "admin", ProjectFlockKey: strPtr("Cikaum Period 1")},
|
||||
{Name: "Cikaum 1", Status: utils.KandangStatusNonActive, Location: "Cikaum", PicKey: "admin"},
|
||||
{Name: "Cikaum 2", Status: utils.KandangStatusNonActive, Location: "Cikaum", PicKey: "admin"},
|
||||
}
|
||||
|
||||
@@ -411,32 +256,19 @@ func seedKandangs(tx *gorm.DB, createdBy uint, locations map[string]uint, users
|
||||
return nil, fmt.Errorf("user %s not seeded", seed.PicKey)
|
||||
}
|
||||
|
||||
var projectFlockID *uint
|
||||
if seed.ProjectFlockKey != nil {
|
||||
pfID, ok := projectFlocks[*seed.ProjectFlockKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("project flock %s not seeded", *seed.ProjectFlockKey)
|
||||
}
|
||||
projectFlockID = uintPtr(pfID)
|
||||
}
|
||||
|
||||
var kandang entity.Kandang
|
||||
err := tx.Where("name = ?", seed.Name).First(&kandang).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
kandang = entity.Kandang{
|
||||
Name: seed.Name,
|
||||
Status: string(seed.Status),
|
||||
LocationId: locID,
|
||||
PicId: picID,
|
||||
ProjectFlockId: projectFlockID,
|
||||
CreatedBy: createdBy,
|
||||
Name: seed.Name,
|
||||
Status: string(seed.Status),
|
||||
LocationId: locID,
|
||||
PicId: picID,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&kandang).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := syncPivotRelation(tx, projectFlockID, kandang.Id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
@@ -445,17 +277,9 @@ func seedKandangs(tx *gorm.DB, createdBy uint, locations map[string]uint, users
|
||||
"pic_id": picID,
|
||||
"status": string(seed.Status),
|
||||
}
|
||||
if projectFlockID != nil {
|
||||
updates["project_flock_id"] = *projectFlockID
|
||||
} else {
|
||||
updates["project_flock_id"] = nil
|
||||
}
|
||||
if err := tx.Model(&entity.Kandang{}).Where("id = ?", kandang.Id).Updates(updates).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := syncPivotRelation(tx, projectFlockID, kandang.Id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
result[seed.Name] = kandang.Id
|
||||
}
|
||||
@@ -463,38 +287,6 @@ func seedKandangs(tx *gorm.DB, createdBy uint, locations map[string]uint, users
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func syncPivotRelation(tx *gorm.DB, projectFlockID *uint, kandangID uint) error {
|
||||
if err := detachActivePivot(tx, kandangID); err != nil {
|
||||
return err
|
||||
}
|
||||
if projectFlockID == nil {
|
||||
return nil
|
||||
}
|
||||
return ensureActivePivot(tx, *projectFlockID, kandangID)
|
||||
}
|
||||
|
||||
func detachActivePivot(tx *gorm.DB, kandangID uint) error {
|
||||
return tx.Where("kandang_id = ?", kandangID).
|
||||
Delete(&entity.ProjectFlockKandang{}).Error
|
||||
}
|
||||
|
||||
func ensureActivePivot(tx *gorm.DB, projectFlockID, kandangID uint) error {
|
||||
var pivot entity.ProjectFlockKandang
|
||||
err := tx.Where("project_flock_id = ? AND kandang_id = ?", projectFlockID, kandangID).
|
||||
First(&pivot).Error
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
newRecord := entity.ProjectFlockKandang{
|
||||
ProjectFlockId: projectFlockID,
|
||||
KandangId: kandangID,
|
||||
}
|
||||
return tx.Create(&newRecord).Error
|
||||
}
|
||||
|
||||
func seedWarehouses(tx *gorm.DB, createdBy uint, areas map[string]uint, locations map[string]uint, kandangs map[string]uint) error {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
@@ -571,9 +363,10 @@ func seedProductCategories(tx *gorm.DB, createdBy uint) (map[string]uint, error)
|
||||
Name string
|
||||
Code string
|
||||
}{
|
||||
{"Pullet", "PLT"},
|
||||
{"Bahan Baku", "RAW"},
|
||||
{"Day Old Chick", "DOC"},
|
||||
{"Pullet", "PULLET"},
|
||||
{"Telur", "EGG"},
|
||||
}
|
||||
|
||||
result := make(map[string]uint, len(seeds))
|
||||
@@ -697,25 +490,14 @@ func seedFcr(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
||||
}
|
||||
}{
|
||||
{
|
||||
Name: "FCR DOC",
|
||||
Name: "FCR Layer",
|
||||
Standards: []struct {
|
||||
Weight float64
|
||||
FcrNumber float64
|
||||
Mortality float64
|
||||
}{
|
||||
{Weight: 0.1, FcrNumber: 1.20, Mortality: 1.0},
|
||||
{Weight: 0.3, FcrNumber: 1.35, Mortality: 1.5},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "FCR Pullet",
|
||||
Standards: []struct {
|
||||
Weight float64
|
||||
FcrNumber float64
|
||||
Mortality float64
|
||||
}{
|
||||
{Weight: 0.5, FcrNumber: 1.45, Mortality: 2.0},
|
||||
{Weight: 0.8, FcrNumber: 1.50, Mortality: 2.5},
|
||||
{Weight: 0.8, FcrNumber: 1.60, Mortality: 2.0},
|
||||
{Weight: 1.5, FcrNumber: 1.75, Mortality: 3.5},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -788,6 +570,56 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
||||
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
||||
Flags: []utils.FlagType{utils.FlagDOC},
|
||||
},
|
||||
{
|
||||
Name: "Ayam Pullet",
|
||||
Brand: "MBU Pullet",
|
||||
Sku: "PLT0001",
|
||||
Uom: "Ekor",
|
||||
Category: "Pullet",
|
||||
Price: 15000,
|
||||
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
||||
Flags: []utils.FlagType{utils.FlagPullet},
|
||||
},
|
||||
{
|
||||
Name: "Ayam Afkir",
|
||||
Brand: "-",
|
||||
Sku: "1",
|
||||
Uom: "Ekor",
|
||||
Category: "Day Old Chick",
|
||||
Price: 1,
|
||||
},
|
||||
{
|
||||
Name: "Ayam Mati",
|
||||
Brand: "-",
|
||||
Sku: "2",
|
||||
Uom: "Ekor",
|
||||
Category: "Day Old Chick",
|
||||
Price: 1,
|
||||
},
|
||||
{
|
||||
Name: "Ayam Culling",
|
||||
Brand: "-",
|
||||
Sku: "3",
|
||||
Uom: "Ekor",
|
||||
Category: "Day Old Chick",
|
||||
Price: 1,
|
||||
},
|
||||
{
|
||||
Name: "Telur Konsumsi Baik",
|
||||
Brand: "-",
|
||||
Sku: "4",
|
||||
Uom: "Unit",
|
||||
Category: "Telur",
|
||||
Price: 1,
|
||||
},
|
||||
{
|
||||
Name: "Telur Pecah",
|
||||
Brand: "-",
|
||||
Sku: "5",
|
||||
Uom: "Unit",
|
||||
Category: "Telur",
|
||||
Price: 1,
|
||||
},
|
||||
{
|
||||
Name: "281 SPECIAL STARTER",
|
||||
Brand: "281 STARTER",
|
||||
@@ -799,26 +631,6 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
||||
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
||||
Flags: []utils.FlagType{utils.FlagPakan, utils.FlagStarter},
|
||||
},
|
||||
{
|
||||
Name: "DOC MAlindo",
|
||||
Brand: "MAlindo",
|
||||
Sku: "MAL0001",
|
||||
Uom: "Ekor",
|
||||
Category: "Day Old Chick",
|
||||
Price: 8000,
|
||||
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
||||
Flags: []utils.FlagType{utils.FlagDOC},
|
||||
},
|
||||
{
|
||||
Name: "Ayam Pullet",
|
||||
Brand: "MBU Pullet",
|
||||
Sku: "PUL0001",
|
||||
Uom: "Ekor",
|
||||
Category: "Pullet",
|
||||
Price: 15000,
|
||||
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
||||
Flags: []utils.FlagType{utils.FlagPullet},
|
||||
},
|
||||
}
|
||||
|
||||
for _, seed := range seeds {
|
||||
@@ -1058,25 +870,44 @@ func seedBanks(tx *gorm.DB, createdBy uint) error {
|
||||
}
|
||||
|
||||
func seedProductWarehouse(tx *gorm.DB, createdBy uint) error {
|
||||
|
||||
seeds := []struct {
|
||||
ProductID uint
|
||||
WarehouseID uint
|
||||
Quantity float64
|
||||
ProductName string
|
||||
WarehouseName string
|
||||
Quantity float64
|
||||
}{
|
||||
{ProductID: 1, WarehouseID: 1, Quantity: 100},
|
||||
{ProductID: 2, WarehouseID: 2, Quantity: 200},
|
||||
{ProductID: 2, WarehouseID: 1, Quantity: 300},
|
||||
{ProductID: 1, WarehouseID: 3, Quantity: 5000},
|
||||
{ProductName: "DOC Broiler", WarehouseName: "Gudang Priangan", Quantity: 100},
|
||||
{ProductName: "281 SPECIAL STARTER", WarehouseName: "Gudang Singaparna", Quantity: 200},
|
||||
{ProductName: "281 SPECIAL STARTER", WarehouseName: "Gudang Banten", Quantity: 300},
|
||||
{ProductName: "DOC Broiler", WarehouseName: "Gudang Singaparna 1", Quantity: 5000},
|
||||
{ProductName: "Telur Konsumsi Baik", WarehouseName: "Gudang Singaparna 1", Quantity: 600},
|
||||
{ProductName: "Telur Pecah", WarehouseName: "Gudang Singaparna 1", Quantity: 80},
|
||||
{ProductName: "Telur Konsumsi Baik", WarehouseName: "Gudang Cikaum 1", Quantity: 450},
|
||||
{ProductName: "Telur Pecah", WarehouseName: "Gudang Cikaum 1", Quantity: 60},
|
||||
}
|
||||
|
||||
for _, seed := range seeds {
|
||||
var product entity.Product
|
||||
if err := tx.Where("name = ?", seed.ProductName).First(&product).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("product %q not found for product warehouse seeding", seed.ProductName)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var warehouse entity.Warehouse
|
||||
if err := tx.Where("name = ?", seed.WarehouseName).First(&warehouse).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("warehouse %q not found for product warehouse seeding", seed.WarehouseName)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var productWarehouse entity.ProductWarehouse
|
||||
err := tx.Where("product_id = ? AND warehouse_id = ?", seed.ProductID, seed.WarehouseID).First(&productWarehouse).Error
|
||||
err := tx.Where("product_id = ? AND warehouse_id = ?", product.Id, warehouse.Id).First(&productWarehouse).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
productWarehouse = entity.ProductWarehouse{
|
||||
ProductId: seed.ProductID,
|
||||
WarehouseId: seed.WarehouseID,
|
||||
ProductId: product.Id,
|
||||
WarehouseId: warehouse.Id,
|
||||
Quantity: seed.Quantity,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
@@ -1085,6 +916,12 @@ func seedProductWarehouse(tx *gorm.DB, createdBy uint) error {
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else {
|
||||
if err := tx.Model(&productWarehouse).Updates(map[string]any{
|
||||
"quantity": seed.Quantity,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1165,153 +1002,6 @@ 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"},
|
||||
// }
|
||||
|
||||
// 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
|
||||
// }
|
||||
|
||||
// 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 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
|
||||
// }
|
||||
|
||||
// // 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
|
||||
// }
|
||||
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// // zero out pw quantity
|
||||
// if err := tx.Model(&entity.ProductWarehouse{}).Where("id = ?", pw.Id).Update("quantity", 0).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// return nil
|
||||
// }
|
||||
|
||||
func ptr[T any](v T) *T {
|
||||
return &v
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user