feat: eliminar migración de inventario existente y ajustar estructura de base de datos
This commit is contained in:
parent
2db5cafb21
commit
d7503304f1
@ -1,112 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
|
|
||||||
return new class extends Migration
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Run the migrations.
|
|
||||||
*/
|
|
||||||
public function up()
|
|
||||||
{
|
|
||||||
// 1. Crear almacén principal
|
|
||||||
$mainWarehouse = DB::table('warehouses')->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();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Loading…
x
Reference in New Issue
Block a user