fix: arreglo de migraciones

This commit is contained in:
Juan Felipe Zapata Moreno 2026-02-06 23:45:47 -06:00
parent 516ad1cae6
commit 3f4a03c9c5
5 changed files with 61 additions and 98 deletions

View File

@ -18,10 +18,12 @@ public function up(): void
$table->foreignId('warehouse_to_id')->nullable()->constrained('warehouses')->onDelete('restrict'); $table->foreignId('warehouse_to_id')->nullable()->constrained('warehouses')->onDelete('restrict');
$table->enum('movement_type', ['entry', 'exit', 'transfer', 'sale', 'return']); $table->enum('movement_type', ['entry', 'exit', 'transfer', 'sale', 'return']);
$table->integer('quantity'); $table->integer('quantity');
$table->decimal('unit_cost', 15, 2)->nullable();
$table->string('reference_type')->nullable(); $table->string('reference_type')->nullable();
$table->unsignedBigInteger('reference_id')->nullable(); $table->unsignedBigInteger('reference_id')->nullable();
$table->foreignId('user_id')->constrained('users')->onDelete('restrict'); $table->foreignId('user_id')->constrained('users')->onDelete('restrict');
$table->text('notes')->nullable(); $table->text('notes')->nullable();
$table->string('invoice_reference')->nullable();
$table->timestamp('created_at'); $table->timestamp('created_at');
$table->index(['inventory_id', 'created_at']); $table->index(['inventory_id', 'created_at']);

View File

@ -22,7 +22,10 @@ public function up()
'updated_at' => now(), '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(); $inventories = DB::table('inventories')->whereNull('deleted_at')->get();
foreach ($inventories as $inventory) { foreach ($inventories as $inventory) {
@ -33,22 +36,74 @@ public function up()
'created_at' => now(), 'created_at' => now(),
'updated_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([ DB::table('inventory_serials')->update([
'warehouse_id' => $mainWarehouse, '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') DB::table('sale_details')
->whereNull('warehouse_id') ->whereNull('warehouse_id')
->update(['warehouse_id' => $mainWarehouse]); ->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() 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('inventory_serials')->update(['warehouse_id' => null]);
DB::table('sale_details')->update(['warehouse_id' => null]); DB::table('sale_details')->update(['warehouse_id' => null]);
DB::table('inventory_warehouse')->truncate(); DB::table('inventory_warehouse')->truncate();

View File

@ -1,38 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// Agregar invoice_reference a inventory_movements
Schema::table('inventory_movements', function (Blueprint $table) {
$table->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');
});
}
};

View File

@ -1,28 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('inventory_movements', function (Blueprint $table) {
$table->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');
});
}
};

View File

@ -1,28 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('prices', function (Blueprint $table) {
$table->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();
});
}
};