codex/fix: purchase receivement error and recording doesn't show depletion/egg

This commit is contained in:
Adnan Zahir
2026-04-01 11:03:35 +07:00
parent be00837148
commit 030284a9b5
5 changed files with 308 additions and 6 deletions
@@ -53,6 +53,24 @@ func (s productWarehouseService) withRelations(db *gorm.DB) *gorm.DB {
Preload("ProjectFlockKandang.Chickins")
}
func applyWarehouseSelectionFilter(db *gorm.DB, kandangID, locationID uint) *gorm.DB {
switch {
case kandangID != 0 && locationID != 0:
return db.Where(
"w_scope.location_id = ? AND (w_scope.type = ? OR w_scope.kandang_id = ?)",
locationID,
"LOKASI",
kandangID,
)
case kandangID != 0:
return db.Where("w_scope.kandang_id = ?", kandangID)
case locationID != 0:
return db.Where("w_scope.location_id = ?", locationID)
default:
return db
}
}
func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.ProductWarehouse, int64, error) {
if err := s.Validate.Struct(params); err != nil {
return nil, 0, err
@@ -133,10 +151,7 @@ func (s productWarehouseService) GetAll(c *fiber.Ctx, params *validation.Query)
db = db.Where("product_id = ?", params.ProductId)
}
if params.KandangId != 0 {
db = db.Joins("JOIN warehouses ON product_warehouses.warehouse_id = warehouses.id").
Where("warehouses.kandang_id = ?", params.KandangId)
}
db = applyWarehouseSelectionFilter(db, params.KandangId, params.LocationId)
if params.WarehouseId != 0 {
db = db.Where("warehouse_id = ?", params.WarehouseId)
@@ -0,0 +1,111 @@
package service
import (
"testing"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
func TestApplyWarehouseSelectionFilterIncludesFarmAndSelectedKandangInLocation(t *testing.T) {
db := setupProductWarehouseServiceTestDB(t)
var ids []uint
err := applyWarehouseSelectionFilter(baseProductWarehouseSelectionQuery(db), 11, 101).
Order("product_warehouses.id").
Pluck("product_warehouses.id", &ids).Error
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertUintIDs(t, ids, []uint{1, 2})
}
func TestApplyWarehouseSelectionFilterPreservesKandangOnlyBehavior(t *testing.T) {
db := setupProductWarehouseServiceTestDB(t)
var ids []uint
err := applyWarehouseSelectionFilter(baseProductWarehouseSelectionQuery(db), 11, 0).
Order("product_warehouses.id").
Pluck("product_warehouses.id", &ids).Error
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertUintIDs(t, ids, []uint{1})
}
func TestApplyWarehouseSelectionFilterSupportsLocationOnlyQuery(t *testing.T) {
db := setupProductWarehouseServiceTestDB(t)
var ids []uint
err := applyWarehouseSelectionFilter(baseProductWarehouseSelectionQuery(db), 0, 101).
Order("product_warehouses.id").
Pluck("product_warehouses.id", &ids).Error
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertUintIDs(t, ids, []uint{1, 2, 3})
}
func setupProductWarehouseServiceTestDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=private"), &gorm.Config{})
if err != nil {
t.Fatalf("failed opening sqlite db: %v", err)
}
statements := []string{
`CREATE TABLE warehouses (
id INTEGER PRIMARY KEY,
type TEXT NOT NULL,
location_id INTEGER NULL,
kandang_id INTEGER NULL,
deleted_at TIMESTAMP NULL
)`,
`CREATE TABLE product_warehouses (
id INTEGER PRIMARY KEY,
warehouse_id INTEGER NOT NULL
)`,
`INSERT INTO warehouses (id, type, location_id, kandang_id, deleted_at) VALUES
(1, 'KANDANG', 101, 11, NULL),
(2, 'LOKASI', 101, NULL, NULL),
(3, 'KANDANG', 101, 12, NULL),
(4, 'LOKASI', 102, NULL, NULL)`,
`INSERT INTO product_warehouses (id, warehouse_id) VALUES
(1, 1),
(2, 2),
(3, 3),
(4, 4)`,
}
for _, stmt := range statements {
if err := db.Exec(stmt).Error; err != nil {
t.Fatalf("failed preparing schema: %v", err)
}
}
return db
}
func baseProductWarehouseSelectionQuery(db *gorm.DB) *gorm.DB {
return db.Table("product_warehouses").
Joins("JOIN warehouses w_scope ON product_warehouses.warehouse_id = w_scope.id").
Where("w_scope.deleted_at IS NULL")
}
func assertUintIDs(t *testing.T, got []uint, want []uint) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("expected ids %v, got %v", want, got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("expected ids %v, got %v", want, got)
}
}
}
@@ -17,6 +17,7 @@ type Query struct {
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
ProductId uint `query:"product_id" validate:"omitempty,number,min=1"`
WarehouseId uint `query:"warehouse_id" validate:"omitempty,number,min=1"`
LocationId uint `query:"location_id" validate:"omitempty,number,min=1"`
Flags string `query:"flags" validate:"omitempty"`
KandangId uint `query:"kandang_id" validate:"omitempty,number,min=1"`
TransferContext string `query:"transfer_context" validate:"omitempty,oneof=inventory_transfer"`