mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
Merge branch 'fix/setup-seeder' into 'development'
fix: setup seeder for development See merge request mbugroup/lti-api!117
This commit is contained in:
File diff suppressed because it is too large
Load Diff
+139
-734
@@ -4,7 +4,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -25,66 +24,20 @@ func Run(db *gorm.DB) error {
|
||||
return err
|
||||
}
|
||||
|
||||
areas, err := seedAreas(tx, adminID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
locations, err := seedLocations(tx, adminID, areas)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
productCategories, err := seedProductCategories(tx, adminID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := seedFlocks(tx, adminID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := seedFcr(tx, adminID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
kandangs, err := seedKandangs(tx, adminID, locations, users)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := seedWarehouses(tx, adminID, areas, locations, kandangs); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
suppliers, err := seedSuppliers(tx, adminID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := seedCustomers(tx, adminID, users); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := seedProducts(tx, adminID, uoms, productCategories, suppliers); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := seedNonstocks(tx, adminID, uoms, suppliers); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := seedBanks(tx, adminID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := seedProductWarehouse(tx, adminID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := seedTransferStock(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("✅ Master data seeding completed")
|
||||
return nil
|
||||
})
|
||||
@@ -141,224 +94,6 @@ func seedUoms(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func seedAreas(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
||||
names := []string{"Priangan", "Banten"}
|
||||
result := make(map[string]uint, len(names))
|
||||
|
||||
for _, name := range names {
|
||||
var area entity.Area
|
||||
err := tx.Where("name = ?", name).First(&area).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
area = entity.Area{Name: name, CreatedBy: createdBy}
|
||||
if err := tx.Create(&area).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[name] = area.Id
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func seedLocations(tx *gorm.DB, createdBy uint, areas map[string]uint) (map[string]uint, error) {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
Address string
|
||||
Area string
|
||||
}{
|
||||
{"Singaparna", "Tasik", "Priangan"},
|
||||
{"Cikaum", "Cikaum", "Banten"},
|
||||
}
|
||||
|
||||
result := make(map[string]uint, len(seeds))
|
||||
|
||||
for _, seed := range seeds {
|
||||
areaID, ok := areas[seed.Area]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("area %s not seeded", seed.Area)
|
||||
}
|
||||
|
||||
var loc entity.Location
|
||||
err := tx.Where("name = ?", seed.Name).First(&loc).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
loc = entity.Location{
|
||||
Name: seed.Name,
|
||||
Address: seed.Address,
|
||||
AreaId: areaID,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&loc).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[seed.Name] = loc.Id
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func seedFlocks(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
||||
names := []string{"Flock Priangan", "Flock Banten"}
|
||||
result := make(map[string]uint, len(names))
|
||||
|
||||
for _, name := range names {
|
||||
var flock entity.Flock
|
||||
err := tx.Where("name = ?", name).First(&flock).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
flock = entity.Flock{
|
||||
Name: name,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&flock).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
if err := tx.Model(&entity.Flock{}).Where("id = ?", flock.Id).Updates(map[string]any{
|
||||
"created_by": createdBy,
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
result[name] = flock.Id
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func seedKandangs(tx *gorm.DB, createdBy uint, locations map[string]uint, users map[string]uint) (map[string]uint, error) {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
Status utils.KandangStatus
|
||||
Capacity float64
|
||||
Location string
|
||||
PicKey string
|
||||
}{
|
||||
{Name: "Singaparna 1", Status: utils.KandangStatusNonActive, Capacity: 50000, Location: "Singaparna", PicKey: "admin"},
|
||||
{Name: "Singaparna 2", Status: utils.KandangStatusNonActive, Capacity: 50000, Location: "Singaparna", PicKey: "admin"},
|
||||
{Name: "Cikaum 1", Status: utils.KandangStatusNonActive, Capacity: 50000, Location: "Cikaum", PicKey: "admin"},
|
||||
{Name: "Cikaum 2", Status: utils.KandangStatusNonActive, Capacity: 50000, Location: "Cikaum", PicKey: "admin"},
|
||||
}
|
||||
|
||||
result := make(map[string]uint, len(seeds))
|
||||
|
||||
for _, seed := range seeds {
|
||||
locID, ok := locations[seed.Location]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("location %s not seeded", seed.Location)
|
||||
}
|
||||
picID, ok := users[seed.PicKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("user %s not seeded", seed.PicKey)
|
||||
}
|
||||
|
||||
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,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&kandang).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
updates := map[string]any{
|
||||
"location_id": locID,
|
||||
"pic_id": picID,
|
||||
"status": string(seed.Status),
|
||||
}
|
||||
if err := tx.Model(&entity.Kandang{}).Where("id = ?", kandang.Id).Updates(updates).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
result[seed.Name] = kandang.Id
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func seedWarehouses(tx *gorm.DB, createdBy uint, areas map[string]uint, locations map[string]uint, kandangs map[string]uint) error {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
Type string
|
||||
Area string
|
||||
Location *string
|
||||
Kandang *string
|
||||
}{
|
||||
{Name: "Gudang Priangan", Type: string(utils.WarehouseTypeArea), Area: "Priangan"},
|
||||
{Name: "Gudang Singaparna", Type: string(utils.WarehouseTypeLokasi), Area: "Priangan", Location: strPtr("Singaparna")},
|
||||
{Name: "Gudang Singaparna 1", Type: string(utils.WarehouseTypeKandang), Area: "Priangan", Location: strPtr("Singaparna"), Kandang: strPtr("Singaparna 1")},
|
||||
{Name: "Gudang Singaparna 2", Type: string(utils.WarehouseTypeKandang), Area: "Priangan", Location: strPtr("Singaparna"), Kandang: strPtr("Singaparna 2")},
|
||||
{Name: "Gudang Banten", Type: string(utils.WarehouseTypeArea), Area: "Banten"},
|
||||
{Name: "Gudang Cikaum", Type: string(utils.WarehouseTypeLokasi), Area: "Banten", Location: strPtr("Cikaum")},
|
||||
{Name: "Gudang Cikaum 1", Type: string(utils.WarehouseTypeKandang), Area: "Banten", Location: strPtr("Cikaum"), Kandang: strPtr("Cikaum 1")},
|
||||
{Name: "Gudang Cikaum 2", Type: string(utils.WarehouseTypeKandang), Area: "Banten", Location: strPtr("Cikaum"), Kandang: strPtr("Cikaum 2")},
|
||||
}
|
||||
|
||||
for _, seed := range seeds {
|
||||
areaID, ok := areas[seed.Area]
|
||||
if !ok {
|
||||
return fmt.Errorf("area %s not seeded", seed.Area)
|
||||
}
|
||||
|
||||
var warehouse entity.Warehouse
|
||||
err := tx.Where("name = ?", seed.Name).First(&warehouse).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
warehouse = entity.Warehouse{
|
||||
Name: seed.Name,
|
||||
Type: seed.Type,
|
||||
AreaId: areaID,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if seed.Location != nil {
|
||||
locID, ok := locations[*seed.Location]
|
||||
if !ok {
|
||||
return fmt.Errorf("location %s not seeded", *seed.Location)
|
||||
}
|
||||
warehouse.LocationId = uintPtr(locID)
|
||||
}
|
||||
if seed.Kandang != nil {
|
||||
kandangID, ok := kandangs[*seed.Kandang]
|
||||
if !ok {
|
||||
return fmt.Errorf("kandang %s not seeded", *seed.Kandang)
|
||||
}
|
||||
warehouse.KandangId = uintPtr(kandangID)
|
||||
}
|
||||
|
||||
if warehouse.Id == 0 {
|
||||
if err := tx.Create(&warehouse).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := tx.Model(&entity.Warehouse{}).Where("id = ?", warehouse.Id).Updates(map[string]any{
|
||||
"type": warehouse.Type,
|
||||
"area_id": warehouse.AreaId,
|
||||
"location_id": warehouse.LocationId,
|
||||
"kandang_id": warehouse.KandangId,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedProductCategories(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
@@ -440,113 +175,6 @@ func seedSuppliers(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func seedCustomers(tx *gorm.DB, createdBy uint, users map[string]uint) error {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
PicKey string
|
||||
Address string
|
||||
Phone string
|
||||
Email string
|
||||
}{
|
||||
{"Abdul Azis", "admin", "Jl. Raya Utama 1, Bekasi", "082100000001", "abdul.azis@gmail.com"},
|
||||
}
|
||||
|
||||
for idx, seed := range seeds {
|
||||
picID, ok := users[seed.PicKey]
|
||||
if !ok {
|
||||
return fmt.Errorf("user %s not seeded", seed.PicKey)
|
||||
}
|
||||
|
||||
var customer entity.Customer
|
||||
err := tx.Where("name = ?", seed.Name).First(&customer).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
customer = entity.Customer{
|
||||
Name: seed.Name,
|
||||
PicId: picID,
|
||||
Type: string(utils.CustomerSupplierTypeBisnis),
|
||||
Address: seed.Address,
|
||||
Phone: seed.Phone,
|
||||
Email: seed.Email,
|
||||
AccountNumber: *strPtr(fmt.Sprintf("%03d", idx+1)),
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&customer).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedFcr(tx *gorm.DB, createdBy uint) (map[string]uint, error) {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
Standards []struct {
|
||||
Weight float64
|
||||
FcrNumber float64
|
||||
Mortality float64
|
||||
}
|
||||
}{
|
||||
{
|
||||
Name: "FCR Layer",
|
||||
Standards: []struct {
|
||||
Weight float64
|
||||
FcrNumber float64
|
||||
Mortality float64
|
||||
}{
|
||||
{Weight: 0.8, FcrNumber: 1.60, Mortality: 2.0},
|
||||
{Weight: 1.5, FcrNumber: 1.75, Mortality: 3.5},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result := make(map[string]uint, len(seeds))
|
||||
|
||||
for _, seed := range seeds {
|
||||
var fcr entity.Fcr
|
||||
err := tx.Where("name = ?", seed.Name).First(&fcr).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
fcr = entity.Fcr{Name: seed.Name, CreatedBy: createdBy}
|
||||
if err := tx.Create(&fcr).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[seed.Name] = fcr.Id
|
||||
|
||||
for _, std := range seed.Standards {
|
||||
var standard entity.FcrStandard
|
||||
err := tx.Where("fcr_id = ? AND weight = ?", fcr.Id, std.Weight).First(&standard).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
standard = entity.FcrStandard{
|
||||
FcrID: fcr.Id,
|
||||
Weight: std.Weight,
|
||||
FcrNumber: std.FcrNumber,
|
||||
Mortality: std.Mortality,
|
||||
}
|
||||
if err := tx.Create(&standard).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
if err := tx.Model(&entity.FcrStandard{}).Where("id = ?", standard.Id).Updates(map[string]any{
|
||||
"fcr_number": std.FcrNumber,
|
||||
"mortality": std.Mortality,
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories map[string]uint, suppliers map[string]uint) error {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
@@ -560,92 +188,88 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
||||
Expiry *int
|
||||
Suppliers []string
|
||||
Flags []utils.FlagType
|
||||
IsVisible bool
|
||||
}{
|
||||
{
|
||||
Name: "DOC Broiler",
|
||||
Brand: "MBU Broiler",
|
||||
Sku: "BRO0001",
|
||||
Name: "ISA Brown",
|
||||
Brand: "ISA Brown",
|
||||
Sku: "ISA0001",
|
||||
Uom: "Ekor",
|
||||
Category: "Day Old Chick",
|
||||
Price: 7500,
|
||||
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
||||
Flags: []utils.FlagType{utils.FlagDOC},
|
||||
Flags: []utils.FlagType{utils.FlagDOC, utils.FlagPullet, utils.FlagLayer},
|
||||
IsVisible: true,
|
||||
},
|
||||
{
|
||||
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,
|
||||
Flags: []utils.FlagType{utils.FlagAyamAfkir},
|
||||
},
|
||||
{
|
||||
Name: "Ayam Mati",
|
||||
Brand: "-",
|
||||
Sku: "2",
|
||||
Uom: "Ekor",
|
||||
Category: "Day Old Chick",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagAyamMati},
|
||||
},
|
||||
{
|
||||
Name: "Ayam Culling",
|
||||
Brand: "-",
|
||||
Sku: "3",
|
||||
Uom: "Ekor",
|
||||
Category: "Day Old Chick",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagAyamCulling},
|
||||
},
|
||||
{
|
||||
Name: "Telur Konsumsi Baik",
|
||||
Brand: "-",
|
||||
Sku: "4",
|
||||
Uom: "Unit",
|
||||
Category: "Telur",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagTelurUtuh},
|
||||
},
|
||||
{
|
||||
Name: "Telur Pecah",
|
||||
Brand: "-",
|
||||
Sku: "5",
|
||||
Uom: "Unit",
|
||||
Category: "Telur",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagTelurPecah},
|
||||
},
|
||||
{
|
||||
Name: "281 SPECIAL STARTER",
|
||||
Brand: "281 STARTER",
|
||||
Sku: "281",
|
||||
Uom: "Kilogram",
|
||||
Category: "Bahan Baku",
|
||||
Price: 7850,
|
||||
Expiry: intPtr(60),
|
||||
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
||||
Flags: []utils.FlagType{utils.FlagPakan, utils.FlagStarter},
|
||||
},
|
||||
{
|
||||
Name: "Ayam Layer",
|
||||
Name: "Ayam Afkir",
|
||||
Brand: "-",
|
||||
Sku: "LYR0001",
|
||||
Sku: "1",
|
||||
Uom: "Ekor",
|
||||
Category: "Pullet",
|
||||
Price: 20000,
|
||||
Suppliers: []string{"PT CHAROEN POKPHAND INDONESIA Tbk"},
|
||||
Flags: []utils.FlagType{utils.FlagLayer},
|
||||
Category: "Day Old Chick",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagAyamAfkir},
|
||||
IsVisible: false,
|
||||
},
|
||||
{
|
||||
Name: "Ayam Mati",
|
||||
Brand: "-",
|
||||
Sku: "2",
|
||||
Uom: "Ekor",
|
||||
Category: "Day Old Chick",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagAyamMati},
|
||||
IsVisible: false,
|
||||
},
|
||||
{
|
||||
Name: "Ayam Culling",
|
||||
Brand: "-",
|
||||
Sku: "3",
|
||||
Uom: "Ekor",
|
||||
Category: "Day Old Chick",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagAyamCulling},
|
||||
IsVisible: false,
|
||||
},
|
||||
{
|
||||
Name: "Telur Utuh",
|
||||
Brand: "-",
|
||||
Sku: "4",
|
||||
Uom: "Gram",
|
||||
Category: "Telur",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagTelurUtuh},
|
||||
IsVisible: false,
|
||||
},
|
||||
{
|
||||
Name: "Telur Pecah",
|
||||
Brand: "-",
|
||||
Sku: "5",
|
||||
Uom: "Gram",
|
||||
Category: "Telur",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagTelurPecah},
|
||||
IsVisible: false,
|
||||
},
|
||||
{
|
||||
Name: "Telur Putih",
|
||||
Brand: "-",
|
||||
Sku: "6",
|
||||
Uom: "Gram",
|
||||
Category: "Telur",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagTelurPutih},
|
||||
IsVisible: false,
|
||||
},
|
||||
{
|
||||
Name: "Telur Retak",
|
||||
Brand: "-",
|
||||
Sku: "7",
|
||||
Uom: "Gram",
|
||||
Category: "Telur",
|
||||
Price: 1,
|
||||
Flags: []utils.FlagType{utils.FlagTelurRetak},
|
||||
IsVisible: false,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -724,78 +348,78 @@ func seedProducts(tx *gorm.DB, createdBy uint, uoms map[string]uint, categories
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedNonstocks(tx *gorm.DB, createdBy uint, uoms map[string]uint, suppliers map[string]uint) error {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
Uom string
|
||||
Suppliers []string
|
||||
Flags []utils.FlagType
|
||||
}{
|
||||
{
|
||||
Name: "Expedisi DOC",
|
||||
Uom: "Ekor",
|
||||
Suppliers: []string{"Ekspedisi"},
|
||||
Flags: []utils.FlagType{utils.FlagEkspedisi},
|
||||
},
|
||||
{
|
||||
Name: "Solar",
|
||||
Uom: "Liter",
|
||||
Suppliers: []string{"BOP Vendor"},
|
||||
Flags: []utils.FlagType{},
|
||||
},
|
||||
}
|
||||
// func seedNonstocks(tx *gorm.DB, createdBy uint, uoms map[string]uint, suppliers map[string]uint) error {
|
||||
// seeds := []struct {
|
||||
// Name string
|
||||
// Uom string
|
||||
// Suppliers []string
|
||||
// Flags []utils.FlagType
|
||||
// }{
|
||||
// {
|
||||
// Name: "LAJ",
|
||||
// Uom: "Unit",
|
||||
// Suppliers: []string{"Ekspedisi"},
|
||||
// Flags: []utils.FlagType{utils.FlagEkspedisi},
|
||||
// },
|
||||
// {
|
||||
// Name: "Solar",
|
||||
// Uom: "Liter",
|
||||
// Suppliers: []string{"BOP Vendor"},
|
||||
// Flags: []utils.FlagType{},
|
||||
// },
|
||||
// }
|
||||
|
||||
for _, seed := range seeds {
|
||||
uomID, ok := uoms[seed.Uom]
|
||||
if !ok {
|
||||
return fmt.Errorf("uom %s not seeded", seed.Uom)
|
||||
}
|
||||
// for _, seed := range seeds {
|
||||
// uomID, ok := uoms[seed.Uom]
|
||||
// if !ok {
|
||||
// return fmt.Errorf("uom %s not seeded", seed.Uom)
|
||||
// }
|
||||
|
||||
var nonstock entity.Nonstock
|
||||
err := tx.Where("name = ?", seed.Name).First(&nonstock).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
nonstock = entity.Nonstock{
|
||||
Name: seed.Name,
|
||||
UomId: uomID,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&nonstock).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else {
|
||||
if err := tx.Model(&entity.Nonstock{}).Where("id = ?", nonstock.Id).Updates(map[string]any{
|
||||
"uom_id": uomID,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// var nonstock entity.Nonstock
|
||||
// err := tx.Where("name = ?", seed.Name).First(&nonstock).Error
|
||||
// if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// nonstock = entity.Nonstock{
|
||||
// Name: seed.Name,
|
||||
// UomId: uomID,
|
||||
// CreatedBy: createdBy,
|
||||
// }
|
||||
// if err := tx.Create(&nonstock).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// } else if err != nil {
|
||||
// return err
|
||||
// } else {
|
||||
// if err := tx.Model(&entity.Nonstock{}).Where("id = ?", nonstock.Id).Updates(map[string]any{
|
||||
// "uom_id": uomID,
|
||||
// }).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
||||
for _, supplierName := range seed.Suppliers {
|
||||
supplierID, ok := suppliers[supplierName]
|
||||
if !ok {
|
||||
return fmt.Errorf("supplier %s not seeded", supplierName)
|
||||
}
|
||||
var existing entity.NonstockSupplier
|
||||
err := tx.Where("nonstock_id = ? AND supplier_id = ?", nonstock.Id, supplierID).First(&existing).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
link := entity.NonstockSupplier{NonstockId: nonstock.Id, SupplierId: supplierID}
|
||||
if err := tx.Create(&link).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// for _, supplierName := range seed.Suppliers {
|
||||
// supplierID, ok := suppliers[supplierName]
|
||||
// if !ok {
|
||||
// return fmt.Errorf("supplier %s not seeded", supplierName)
|
||||
// }
|
||||
// var existing entity.NonstockSupplier
|
||||
// err := tx.Where("nonstock_id = ? AND supplier_id = ?", nonstock.Id, supplierID).First(&existing).Error
|
||||
// if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// link := entity.NonstockSupplier{NonstockId: nonstock.Id, SupplierId: supplierID}
|
||||
// if err := tx.Create(&link).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// } else if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
||||
if err := seedFlags(tx, nonstock.Id, entity.FlagableTypeNonstock, seed.Flags); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// if err := seedFlags(tx, nonstock.Id, entity.FlagableTypeNonstock, seed.Flags); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// nanti saya isi
|
||||
|
||||
@@ -823,225 +447,6 @@ func seedFlags(tx *gorm.DB, flagableID uint, flagableType string, flags []utils.
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedBanks(tx *gorm.DB, createdBy uint) error {
|
||||
seeds := []struct {
|
||||
Name string
|
||||
Alias string
|
||||
Owner *string
|
||||
AccountNumber string
|
||||
}{
|
||||
{
|
||||
Name: "Bank Central Asia",
|
||||
Alias: "BCA",
|
||||
AccountNumber: "1234567890",
|
||||
Owner: ptr("PT MBU Group"),
|
||||
},
|
||||
{
|
||||
Name: "Bank Rakyat Indonesia",
|
||||
Alias: "BRI",
|
||||
AccountNumber: "9876543210",
|
||||
Owner: ptr("PT MBU Group"),
|
||||
},
|
||||
{
|
||||
Name: "Bank Mandiri",
|
||||
Alias: "MAND",
|
||||
AccountNumber: "1122334455",
|
||||
Owner: ptr("PT MBU Group"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, seed := range seeds {
|
||||
var bank entity.Bank
|
||||
err := tx.Where("name = ?", seed.Name).First(&bank).Error
|
||||
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
bank = entity.Bank{
|
||||
Name: seed.Name,
|
||||
Alias: seed.Alias,
|
||||
Owner: seed.Owner,
|
||||
AccountNumber: seed.AccountNumber,
|
||||
CreatedBy: createdBy,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
if err := tx.Create(&bank).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else {
|
||||
// update data jika sudah ada
|
||||
if err := tx.Model(&entity.Bank{}).Where("id = ?", bank.Id).Updates(map[string]any{
|
||||
"alias": seed.Alias,
|
||||
"owner": seed.Owner,
|
||||
"account_number": seed.AccountNumber,
|
||||
"updated_at": time.Now(),
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedProductWarehouse(tx *gorm.DB, createdBy uint) error {
|
||||
seeds := []struct {
|
||||
ProductName string
|
||||
WarehouseName string
|
||||
Quantity float64
|
||||
}{
|
||||
{ProductName: "DOC Broiler", WarehouseName: "Gudang Priangan", Quantity: 0},
|
||||
{ProductName: "281 SPECIAL STARTER", WarehouseName: "Gudang Singaparna", Quantity: 0},
|
||||
{ProductName: "281 SPECIAL STARTER", WarehouseName: "Gudang Banten", Quantity: 0},
|
||||
{ProductName: "DOC Broiler", WarehouseName: "Gudang Singaparna 1", Quantity: 0},
|
||||
{ProductName: "Telur Konsumsi Baik", WarehouseName: "Gudang Singaparna 1", Quantity: 0},
|
||||
{ProductName: "Telur Pecah", WarehouseName: "Gudang Singaparna 1", Quantity: 0},
|
||||
{ProductName: "Telur Konsumsi Baik", WarehouseName: "Gudang Cikaum 1", Quantity: 0},
|
||||
{ProductName: "Telur Pecah", WarehouseName: "Gudang Cikaum 1", Quantity: 0},
|
||||
}
|
||||
|
||||
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 = ?", product.Id, warehouse.Id).First(&productWarehouse).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
productWarehouse = entity.ProductWarehouse{
|
||||
ProductId: product.Id,
|
||||
WarehouseId: warehouse.Id,
|
||||
Quantity: seed.Quantity,
|
||||
// CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&productWarehouse).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else {
|
||||
if err := tx.Model(&productWarehouse).Updates(map[string]any{
|
||||
"quantity": seed.Quantity,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func seedTransferStock(tx *gorm.DB) error {
|
||||
|
||||
transfer := entity.StockTransfer{
|
||||
FromWarehouseId: 1,
|
||||
ToWarehouseId: 2,
|
||||
Reason: "Seed transfer stock",
|
||||
TransferDate: time.Now(),
|
||||
MovementNumber: "SEED-TRF-00001",
|
||||
CreatedBy: 1,
|
||||
}
|
||||
if err := tx.Create(&transfer).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
details := []entity.StockTransferDetail{
|
||||
{
|
||||
StockTransferId: transfer.Id,
|
||||
ProductId: 1,
|
||||
|
||||
SourceProductWarehouseID: func() *uint64 { id := uint64(1); return &id }(),
|
||||
DestProductWarehouseID: func() *uint64 { id := uint64(2); return &id }(),
|
||||
UsageQty: 10,
|
||||
PendingQty: 0,
|
||||
TotalQty: 10,
|
||||
TotalUsed: 0,
|
||||
},
|
||||
{
|
||||
StockTransferId: transfer.Id,
|
||||
ProductId: 2,
|
||||
|
||||
SourceProductWarehouseID: func() *uint64 { id := uint64(1); return &id }(),
|
||||
DestProductWarehouseID: func() *uint64 { id := uint64(2); return &id }(),
|
||||
UsageQty: 5,
|
||||
PendingQty: 0,
|
||||
TotalQty: 5,
|
||||
TotalUsed: 0,
|
||||
},
|
||||
}
|
||||
for i := range details {
|
||||
if err := tx.Create(&details[i]).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
deliveries := []entity.StockTransferDelivery{
|
||||
{
|
||||
StockTransferId: transfer.Id,
|
||||
SupplierId: 1,
|
||||
VehiclePlate: "B 1234 XYZ",
|
||||
DriverName: "Driver Seed",
|
||||
DocumentPath: "seed.pdf",
|
||||
ShippingCostItem: 1000,
|
||||
ShippingCostTotal: 2000,
|
||||
},
|
||||
}
|
||||
for i := range deliveries {
|
||||
if err := tx.Create(&deliveries[i]).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
detailMap := make(map[uint64]uint64)
|
||||
for _, d := range details {
|
||||
detailMap[d.ProductId] = d.Id
|
||||
}
|
||||
|
||||
deliveryItems := []entity.StockTransferDeliveryItem{
|
||||
{
|
||||
StockTransferDeliveryId: deliveries[0].Id,
|
||||
StockTransferDetailId: detailMap[1],
|
||||
Quantity: 50,
|
||||
},
|
||||
{
|
||||
StockTransferDeliveryId: deliveries[0].Id,
|
||||
StockTransferDetailId: detailMap[2],
|
||||
Quantity: 30,
|
||||
},
|
||||
}
|
||||
for i := range deliveryItems {
|
||||
if err := tx.Create(&deliveryItems[i]).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func ptr[T any](v T) *T {
|
||||
return &v
|
||||
}
|
||||
|
||||
func strPtr(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
func intPtr(v int) *int {
|
||||
return &v
|
||||
}
|
||||
|
||||
func uintPtr(v uint) *uint {
|
||||
return &v
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user