mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat/BE/US-76/TASK-122,133,121,120 Recording add create delete edit
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
|
||||
ALTER TABLE kandangs
|
||||
DROP CONSTRAINT IF EXISTS kandangs_project_flock_id_fkey;
|
||||
|
||||
ALTER TABLE kandangs
|
||||
DROP COLUMN IF EXISTS project_flock_id;
|
||||
|
||||
ALTER TABLE project_chickins
|
||||
DROP CONSTRAINT fk_project_flock_kandang_id,
|
||||
ADD CONSTRAINT fk_project_flock_kandang_id
|
||||
FOREIGN KEY (project_flock_kandang_id)
|
||||
REFERENCES project_flock_kandangs(id)
|
||||
ON UPDATE CASCADE
|
||||
ON DELETE CASCADE;
|
||||
|
||||
ALTER TABLE project_flock_populations
|
||||
DROP CONSTRAINT fk_project_flock_kandang_id,
|
||||
ADD CONSTRAINT fk_project_flock_kandang_id
|
||||
FOREIGN KEY (project_flock_kandang_id)
|
||||
REFERENCES project_flock_kandangs(id)
|
||||
ON UPDATE CASCADE
|
||||
ON DELETE CASCADE;
|
||||
+111
-214
@@ -51,12 +51,12 @@ func Run(db *gorm.DB) error {
|
||||
return err
|
||||
}
|
||||
|
||||
projectFlocks, err := seedProjectFlocks(tx, adminID, flocks, areas, fcrs, locations)
|
||||
if err != nil {
|
||||
|
||||
if err := seedProjectFlocks(tx, adminID, flocks, areas, fcrs, locations); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
kandangs, err := seedKandangs(tx, adminID, locations, users, projectFlocks)
|
||||
|
||||
kandangs, err := seedKandangs(tx, adminID, locations, users)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -243,7 +243,11 @@ 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 seedProjectFlocks(
|
||||
tx *gorm.DB,
|
||||
createdBy uint,
|
||||
flocks, areas, fcrs, locations map[string]uint,
|
||||
) error {
|
||||
seeds := []struct {
|
||||
Key string
|
||||
Flock string
|
||||
@@ -273,29 +277,30 @@ func seedProjectFlocks(tx *gorm.DB, createdBy uint, flocks, areas, fcrs, locatio
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
||||
return 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)
|
||||
return 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)
|
||||
return 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)
|
||||
return 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
|
||||
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,
|
||||
@@ -307,10 +312,10 @@ func seedProjectFlocks(tx *gorm.DB, createdBy uint, flocks, areas, fcrs, locatio
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&projectFlock).Error; err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
} else {
|
||||
if err := tx.Model(&entity.ProjectFlock{}).Where("id = ?", projectFlock.Id).Updates(map[string]any{
|
||||
"flock_id": flockID,
|
||||
@@ -320,17 +325,16 @@ func seedProjectFlocks(tx *gorm.DB, createdBy uint, flocks, areas, fcrs, locatio
|
||||
"location_id": locationID,
|
||||
"period": seed.Period,
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := ensureProjectFlockApprovals(tx, projectFlock.Id, createdBy); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
result[seed.Key] = projectFlock.Id
|
||||
}
|
||||
|
||||
return result, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureProjectFlockApprovals(tx *gorm.DB, projectFlockID uint, actorID uint) error {
|
||||
@@ -385,17 +389,16 @@ func ensureProjectFlockApprovals(tx *gorm.DB, projectFlockID uint, actorID uint)
|
||||
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) {
|
||||
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
|
||||
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,14 +414,6 @@ 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
|
||||
@@ -428,15 +423,11 @@ func seedKandangs(tx *gorm.DB, createdBy uint, locations map[string]uint, users
|
||||
Status: string(seed.Status),
|
||||
LocationId: locID,
|
||||
PicId: picID,
|
||||
ProjectFlockId: projectFlockID,
|
||||
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 +436,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,37 +446,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 {
|
||||
@@ -1133,153 +1085,71 @@ 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"},
|
||||
}
|
||||
// gunakan identitas yang stabil, bukan ID pivot
|
||||
seeds := []struct {
|
||||
KandangName string
|
||||
LocationName string
|
||||
Period int
|
||||
ChickInDate string
|
||||
Quantity float64
|
||||
Note string
|
||||
}{
|
||||
{"Singaparna 1", "Singaparna", 1, "2025-10-20", 100, "Seeder chickin 1"},
|
||||
{"Cikaum 1", "Cikaum", 1, "2025-10-21", 200, "Seeder chickin 2"},
|
||||
}
|
||||
|
||||
for _, seed := range seeds {
|
||||
chickinDate, err := time.Parse("2006-01-02", seed.ChickInDate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, s := range seeds {
|
||||
pfkID, err := ensurePFK(tx, s.KandangName, s.LocationName, s.Period)
|
||||
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
|
||||
}
|
||||
date, err := time.Parse("2006-01-02", s.ChickInDate)
|
||||
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
|
||||
}
|
||||
}
|
||||
// upsert project_chickin (idempotent)
|
||||
var chickin entity.ProjectChickin
|
||||
err = tx.Where("project_flock_kandang_id = ? AND chick_in_date = ?", pfkID, date).First(&chickin).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
chickin = entity.ProjectChickin{
|
||||
ProjectFlockKandangId: pfkID,
|
||||
ChickInDate: date,
|
||||
Quantity: s.Quantity,
|
||||
Note: s.Note,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&chickin).Error; err != nil { return err }
|
||||
} else if 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
|
||||
// upsert population
|
||||
var pop entity.ProjectFlockPopulation
|
||||
err = tx.Where("project_flock_kandang_id = ?", pfkID).First(&pop).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
pop = entity.ProjectFlockPopulation{
|
||||
ProjectFlockKandangId: pfkID,
|
||||
InitialQuantity: s.Quantity,
|
||||
CurrentQuantity: s.Quantity,
|
||||
ReservedQuantity: 0,
|
||||
CreatedBy: createdBy,
|
||||
}
|
||||
if err := tx.Create(&pop).Error; err != nil { return err }
|
||||
} else if err != nil {
|
||||
return err
|
||||
} else {
|
||||
if err := tx.Model(&entity.ProjectFlockPopulation{}).
|
||||
Where("id = ?", pop.Id).
|
||||
Updates(map[string]any{
|
||||
"initial_quantity": pop.InitialQuantity + s.Quantity,
|
||||
"current_quantity": pop.CurrentQuantity + s.Quantity,
|
||||
"reserved_quantity": 0,
|
||||
}).Error; err != nil { return err }
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func ptr[T any](v T) *T {
|
||||
return &v
|
||||
}
|
||||
@@ -1295,3 +1165,30 @@ func intPtr(v int) *int {
|
||||
func uintPtr(v uint) *uint {
|
||||
return &v
|
||||
}
|
||||
|
||||
func ensurePFK(tx *gorm.DB, kandangName, locationName string, period int) (uint, error) {
|
||||
var kandang entity.Kandang
|
||||
if err := tx.Where("name = ?", kandangName).First(&kandang).Error; err != nil {
|
||||
return 0, fmt.Errorf("kandang %q not found: %w", kandangName, err)
|
||||
}
|
||||
var loc entity.Location
|
||||
if err := tx.Where("name = ?", locationName).First(&loc).Error; err != nil {
|
||||
return 0, fmt.Errorf("location %q not found: %w", locationName, err)
|
||||
}
|
||||
var pf entity.ProjectFlock
|
||||
if err := tx.Where("location_id = ? AND period = ?", loc.Id, period).First(&pf).Error; err != nil {
|
||||
return 0, fmt.Errorf("project_flock for %s period %d not found: %w", locationName, period, err)
|
||||
}
|
||||
var pfk entity.ProjectFlockKandang
|
||||
if err := tx.Where("project_flock_id = ? AND kandang_id = ?", pf.Id, kandang.Id).First(&pfk).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
pfk = entity.ProjectFlockKandang{ ProjectFlockId: pf.Id, KandangId: kandang.Id }
|
||||
if err := tx.Create(&pfk).Error; err != nil {
|
||||
return 0, fmt.Errorf("create pivot pfk(%d,%d) failed: %w", pf.Id, kandang.Id, err)
|
||||
}
|
||||
} else {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return pfk.Id, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user