diff --git a/database/migrations/2026_02_05_133708_create_inventory_movements_table.php b/database/migrations/2026_02_05_133708_create_inventory_movements_table.php index 269d067..ef558e9 100644 --- a/database/migrations/2026_02_05_133708_create_inventory_movements_table.php +++ b/database/migrations/2026_02_05_133708_create_inventory_movements_table.php @@ -18,10 +18,12 @@ public function up(): void $table->foreignId('warehouse_to_id')->nullable()->constrained('warehouses')->onDelete('restrict'); $table->enum('movement_type', ['entry', 'exit', 'transfer', 'sale', 'return']); $table->integer('quantity'); + $table->decimal('unit_cost', 15, 2)->nullable(); $table->string('reference_type')->nullable(); $table->unsignedBigInteger('reference_id')->nullable(); $table->foreignId('user_id')->constrained('users')->onDelete('restrict'); $table->text('notes')->nullable(); + $table->string('invoice_reference')->nullable(); $table->timestamp('created_at'); $table->index(['inventory_id', 'created_at']); 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 index f339c1f..3917f38 100644 --- 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 @@ -22,7 +22,10 @@ public function up() 'updated_at' => now(), ]); - // 2. Migrar stock de inventories a inventory_warehouse + // 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) { @@ -33,22 +36,74 @@ public function up() '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(), + ]); + } } - // 3. Asignar almacén principal a todos los seriales existentes + // 4. Asignar almacén principal a todos los seriales existentes DB::table('inventory_serials')->update([ 'warehouse_id' => $mainWarehouse, ]); - // 4. Asignar almacén principal a sale_details existentes (nullable, opcional) + // 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() { - // Rollback: eliminar asignaciones de warehouse + // 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(); diff --git a/database/migrations/2026_02_05_211236_add_invoice_reference_to_inventory_movements_table.php b/database/migrations/2026_02_05_211236_add_invoice_reference_to_inventory_movements_table.php deleted file mode 100644 index 6b819ca..0000000 --- a/database/migrations/2026_02_05_211236_add_invoice_reference_to_inventory_movements_table.php +++ /dev/null @@ -1,38 +0,0 @@ -string('invoice_reference')->nullable()->after('notes'); - }); - - // Eliminar stock de inventories (ahora vive en inventory_warehouse) - Schema::table('inventories', function (Blueprint $table) { - $table->dropColumn('stock'); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::table('inventory_movements', function (Blueprint $table) { - $table->dropColumn('invoice_reference'); - }); - - Schema::table('inventories', function (Blueprint $table) { - $table->integer('stock')->default(0)->after('sku'); - }); - } -}; diff --git a/database/migrations/2026_02_06_101139_add_unit_cost_to_inventory_movements_table.php b/database/migrations/2026_02_06_101139_add_unit_cost_to_inventory_movements_table.php deleted file mode 100644 index 6881f8e..0000000 --- a/database/migrations/2026_02_06_101139_add_unit_cost_to_inventory_movements_table.php +++ /dev/null @@ -1,28 +0,0 @@ -decimal('unit_cost', 15, 2)->nullable()->after('quantity'); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::table('inventory_movements', function (Blueprint $table) { - $table->dropColumn('unit_cost'); - }); - } -}; diff --git a/database/migrations/2026_02_06_103956_make_cost_nullable_in_prices_table.php b/database/migrations/2026_02_06_103956_make_cost_nullable_in_prices_table.php deleted file mode 100644 index 2e48f64..0000000 --- a/database/migrations/2026_02_06_103956_make_cost_nullable_in_prices_table.php +++ /dev/null @@ -1,28 +0,0 @@ -decimal('cost', 15, 2)->nullable()->change(); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::table('prices', function (Blueprint $table) { - $table->decimal('cost', 15, 2)->nullable(false)->change(); - }); - } -};