Merge branch 'cmd/consolidate-and-repoint-stocks' into 'development'

cmd: skip consolidation for incomplete locations (locations without farm-level warehouse)

See merge request mbugroup/lti-api!450
This commit is contained in:
Adnan Zahir
2026-04-24 00:27:36 +07:00
+19 -8
View File
@@ -26,13 +26,14 @@ const (
)
type options struct {
Apply bool
Output string
AreaName string
KandangLocationName string
DBSSLMode string
DeleteKandangWarehouses bool
SkipBlockedRefsCheck bool
Apply bool
Output string
AreaName string
KandangLocationName string
DBSSLMode string
DeleteKandangWarehouses bool
SkipBlockedRefsCheck bool
SkipIncompleteLocations bool
}
type consolidateRow struct {
@@ -159,6 +160,7 @@ func parseFlags() (*options, error) {
flag.StringVar(&opts.DBSSLMode, "db-sslmode", "", "Optional database sslmode override, for example: require")
flag.BoolVar(&opts.DeleteKandangWarehouses, "delete-kandang-warehouses", true, "Soft delete kandang warehouse rows after all stocks have been moved")
flag.BoolVar(&opts.SkipBlockedRefsCheck, "skip-blocked-refs-check", false, "Skip blocked references check (use with caution - only if you understand the stock_transfers references)")
flag.BoolVar(&opts.SkipIncompleteLocations, "skip-incomplete-locations", false, "Skip locations that don't have farm-level warehouses and process the rest")
flag.Parse()
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output))
@@ -188,6 +190,12 @@ func loadConsolidateRows(ctx context.Context, db *gorm.DB, opts *options) ([]con
args = append(args, opts.KandangLocationName)
}
// If skipping incomplete locations, filter out NULL farm warehouses
whereClause := ""
if opts.SkipIncompleteLocations {
whereClause = "AND fw.id IS NOT NULL"
}
query := fmt.Sprintf(`
SELECT
a.name AS area_name,
@@ -251,8 +259,11 @@ WHERE w.deleted_at IS NULL
AND sa.deleted_at IS NULL
)
%s
%s
ORDER BY a.name ASC, kl.name ASC, k.name ASC, wp.id ASC
`, andClause(filters))
`,
andClause(filters),
whereClause)
rows := make([]consolidateRow, 0)
if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {