BEGIN;

-- Konsolidasi 3 lokasi "Pullet Cikaum" jadi 1.
-- Pindahkan semua data di loc 18 (Pullet Cikaum 1) & 25 (Pullet Cikaum 2) ke loc 2 (Pullet Cikaum).
-- Urutan wajib: semua UPDATE child harus selesai SEBELUM soft-delete locations,
-- karena trigger trg_soft_delete_fk_locations akan RAISE EXCEPTION untuk FK
-- RESTRICT (project_flocks, kandangs, kandang_groups, expenses) atau SET NULL
-- untuk warehouses kalau masih ada child yang reference.

-- 1. project_flocks (PF 25, 30, 31)
UPDATE project_flocks SET location_id = 2, updated_at = NOW()
WHERE location_id IN (18, 25);

-- 2. kandangs (K9, K72, K117, K10, K73, K116)
UPDATE kandangs SET location_id = 2, updated_at = NOW()
WHERE location_id IN (18, 25);

-- 3. kandang_groups (KG 26, 68, 27, 67)
UPDATE kandang_groups SET location_id = 2, updated_at = NOW()
WHERE location_id IN (18, 25);

-- 4. warehouses (W3, W27, W145, W146, W152, W153)
UPDATE warehouses SET location_id = 2, updated_at = NOW()
WHERE location_id IN (18, 25);

-- 5. expenses (23 row BOP)
UPDATE expenses SET location_id = 2, updated_at = NOW()
WHERE location_id IN (18, 25);

-- 6. Soft-delete locations 18 & 25 (kosong, aman karena semua child sudah pindah)
UPDATE locations SET deleted_at = NOW()
WHERE id IN (18, 25) AND deleted_at IS NULL;

COMMIT;
