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

This commit is contained in:
Adnan Zahir
2026-04-24 00:25:38 +07:00
parent 46737e4a96
commit 101a83cd9e
+19 -8
View File
@@ -26,13 +26,14 @@ const (
) )
type options struct { type options struct {
Apply bool Apply bool
Output string Output string
AreaName string AreaName string
KandangLocationName string KandangLocationName string
DBSSLMode string DBSSLMode string
DeleteKandangWarehouses bool DeleteKandangWarehouses bool
SkipBlockedRefsCheck bool SkipBlockedRefsCheck bool
SkipIncompleteLocations bool
} }
type consolidateRow struct { 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.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.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.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() flag.Parse()
opts.Output = strings.ToLower(strings.TrimSpace(opts.Output)) 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) 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(` query := fmt.Sprintf(`
SELECT SELECT
a.name AS area_name, a.name AS area_name,
@@ -251,8 +259,11 @@ WHERE w.deleted_at IS NULL
AND sa.deleted_at IS NULL AND sa.deleted_at IS NULL
) )
%s %s
%s
ORDER BY a.name ASC, kl.name ASC, k.name ASC, wp.id ASC ORDER BY a.name ASC, kl.name ASC, k.name ASC, wp.id ASC
`, andClause(filters)) `,
andClause(filters),
whereClause)
rows := make([]consolidateRow, 0) rows := make([]consolidateRow, 0)
if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil { if err := db.WithContext(ctx).Raw(query, args...).Scan(&rows).Error; err != nil {