From d7503304f107cad04db3a2b873288bc9f2d9b892 Mon Sep 17 00:00:00 2001 From: Juan Felipe Zapata Moreno Date: Tue, 10 Mar 2026 09:27:42 -0600 Subject: [PATCH] =?UTF-8?q?feat:=20eliminar=20migraci=C3=B3n=20de=20invent?= =?UTF-8?q?ario=20existente=20y=20ajustar=20estructura=20de=20base=20de=20?= =?UTF-8?q?datos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...grate_existing_inventory_to_warehouses.php | 112 ------------------ 1 file changed, 112 deletions(-) delete mode 100644 database/migrations/2026_02_05_141627_migrate_existing_inventory_to_warehouses.php diff --git a/database/migrations/2026_02_05_141627_migrate_existing_inventory_to_warehouses.php b/database/migrations/2026_02_05_141627_migrate_existing_inventory_to_warehouses.php deleted file mode 100644 index 3917f38..0000000 --- a/database/migrations/2026_02_05_141627_migrate_existing_inventory_to_warehouses.php +++ /dev/null @@ -1,112 +0,0 @@ -insertGetId([ - 'code' => 'ALM-PRINCIPAL', - 'name' => 'Almacén Principal', - 'is_active' => true, - 'is_main' => true, - 'created_at' => now(), - 'updated_at' => now(), - ]); - - // 2. Obtener usuario admin para los movimientos - $adminUser = DB::table('users')->first(); - - // 3. Migrar stock de inventories a inventory_warehouse y generar movimientos entry - $inventories = DB::table('inventories')->whereNull('deleted_at')->get(); - - foreach ($inventories as $inventory) { - DB::table('inventory_warehouse')->insert([ - 'inventory_id' => $inventory->id, - 'warehouse_id' => $mainWarehouse, - 'stock' => $inventory->stock, - 'created_at' => now(), - 'updated_at' => now(), - ]); - - // Generar movimiento de entrada inicial solo si tiene stock - if ($inventory->stock > 0) { - $cost = DB::table('prices') - ->where('inventory_id', $inventory->id) - ->value('cost'); - - DB::table('inventory_movements')->insert([ - 'inventory_id' => $inventory->id, - 'warehouse_from_id' => null, - 'warehouse_to_id' => $mainWarehouse, - 'movement_type' => 'entry', - 'quantity' => $inventory->stock, - 'unit_cost' => $cost ?? 0, - 'user_id' => $adminUser->id, - 'notes' => 'Saldo inicial - migración de inventario existente', - 'invoice_reference' => 'MIGRACIÓN', - 'created_at' => now(), - ]); - } - } - - // 4. Asignar almacén principal a todos los seriales existentes - DB::table('inventory_serials')->update([ - 'warehouse_id' => $mainWarehouse, - ]); - - // 5. Asignar almacén principal a sale_details existentes - DB::table('sale_details') - ->whereNull('warehouse_id') - ->update(['warehouse_id' => $mainWarehouse]); - - // 6. Eliminar columna stock de inventories (ahora vive en inventory_warehouse) - Schema::table('inventories', function (Blueprint $table) { - $table->dropColumn('stock'); - }); - - // 7. Hacer cost nullable en prices (para productos nuevos sin entrada aún) - Schema::table('prices', function (Blueprint $table) { - $table->decimal('cost', 15, 2)->nullable()->change(); - }); - } - - public function down() - { - // Restaurar cost como no nullable - Schema::table('prices', function (Blueprint $table) { - $table->decimal('cost', 15, 2)->nullable(false)->change(); - }); - - // Restaurar columna stock en inventories - Schema::table('inventories', function (Blueprint $table) { - $table->integer('stock')->default(0)->after('barcode'); - }); - - // Restaurar stock desde inventory_warehouse - $records = DB::table('inventory_warehouse')->get(); - foreach ($records as $record) { - DB::table('inventories') - ->where('id', $record->inventory_id) - ->increment('stock', $record->stock); - } - - // Limpiar datos migrados - DB::table('inventory_movements') - ->where('notes', 'Saldo inicial - migración de inventario existente') - ->delete(); - - DB::table('inventory_serials')->update(['warehouse_id' => null]); - DB::table('sale_details')->update(['warehouse_id' => null]); - DB::table('inventory_warehouse')->truncate(); - DB::table('warehouses')->truncate(); - } -};