141 lines
4.7 KiB
PHP
141 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Owner;
|
|
use App\Models\Vehicle;
|
|
use App\Models\Tag;
|
|
use App\Models\CatalogTagStatus;
|
|
use App\Models\VehicleTagLog;
|
|
use Illuminate\Console\Command;
|
|
|
|
class SeedOwnerVehicle extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'seed:owner-vehicle';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Crear un propietario y un vehículo de ejemplo';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
try {
|
|
$this->info('Creando propietario...');
|
|
|
|
// Crear un propietario
|
|
$owner = Owner::create([
|
|
'name' => 'Juan',
|
|
'paternal' => 'Pérez',
|
|
'maternal' => 'García',
|
|
'rfc' => 'PEGJ850101ABC',
|
|
'curp' => 'PEGJ850101HDFRRN01',
|
|
'address' => 'Calle Principal 123',
|
|
'tipopers' => true,
|
|
'pasaporte' => null,
|
|
'licencia' => 'LIC123456789',
|
|
'ent_fed' => 'Ciudad de México',
|
|
'munic' => 'Benito Juárez',
|
|
'callep' => 'Calle Principal',
|
|
'num_ext' => '123',
|
|
'num_int' => 'A',
|
|
'colonia' => 'Centro',
|
|
'cp' => '03100',
|
|
'telefono' => '5551234567',
|
|
]);
|
|
|
|
$this->info("✓ Propietario creado: {$owner->full_name} (ID: {$owner->id})");
|
|
|
|
$this->info('Creando vehículo...');
|
|
|
|
// Crear un vehículo asociado al propietario
|
|
$vehicle = Vehicle::create([
|
|
'placa' => 'ABC-123',
|
|
'niv' => '1HGBH41JXMN109186',
|
|
'marca' => 'Honda',
|
|
'linea' => 'Civic',
|
|
'sublinea' => 'Sedan',
|
|
'modelo' => '2020',
|
|
'color' => 'Blanco',
|
|
'numero_motor' => 'ENG123456789',
|
|
'clase_veh' => 'Automóvil',
|
|
'tipo_servicio' => 'Particular',
|
|
'rfv' => 'RFV123456789',
|
|
'ofcexpedicion' => 'Oficina Central',
|
|
'fechaexpedicion' => '2020-01-15',
|
|
'tipo_veh' => 'Sedan',
|
|
'numptas' => '4',
|
|
'observac' => 'Vehículo en buen estado',
|
|
'cve_vehi' => 'CVE001',
|
|
'tipo_mov' => 'Alta',
|
|
'owner_id' => $owner->id,
|
|
]);
|
|
|
|
$this->info("✓ Vehículo creado: {$vehicle->marca} {$vehicle->linea} - Placa: {$vehicle->placa} (ID: {$vehicle->id})");
|
|
$this->info("✓ Asociado al propietario: {$owner->full_name}");
|
|
|
|
$this->info('Creando tag...');
|
|
|
|
// Obtener el status "available" para crear el tag
|
|
$statusAvailable = CatalogTagStatus::where('code', Tag::STATUS_AVAILABLE)->first();
|
|
|
|
if (!$statusAvailable) {
|
|
throw new \Exception('No se encontró el status "available" en el catálogo. Ejecuta el seeder CatalogTagStatusSeeder primero.');
|
|
}
|
|
|
|
// Generar un folio único para el tag
|
|
$folio = str_pad(rand(1, 99999999), 8, '0', STR_PAD_LEFT);
|
|
|
|
// Verificar que el folio no exista
|
|
while (Tag::where('folio', $folio)->exists()) {
|
|
$folio = str_pad(rand(1, 99999999), 8, '0', STR_PAD_LEFT);
|
|
}
|
|
|
|
// Crear un tag disponible
|
|
$tag = Tag::create([
|
|
'folio' => $folio,
|
|
'tag_number' => 'TAG' . str_pad(rand(1, 999999), 10, '0', STR_PAD_LEFT),
|
|
'vehicle_id' => null,
|
|
'package_id' => null,
|
|
'module_id' => null,
|
|
'status_id' => $statusAvailable->id,
|
|
]);
|
|
|
|
$this->info("✓ Tag creado: Folio {$tag->folio} - Tag Number: {$tag->tag_number} (ID: {$tag->id})");
|
|
|
|
// Asignar el tag al vehículo
|
|
$tag->markAsAssigned($vehicle->id, $folio);
|
|
|
|
$this->info("✓ Tag asignado al vehículo: {$vehicle->placa}");
|
|
|
|
// Crear registro en el log de tags
|
|
VehicleTagLog::create([
|
|
'vehicle_id' => $vehicle->id,
|
|
'tag_id' => $tag->id,
|
|
'action_type' => 'inscripcion',
|
|
'performed_by' => null,
|
|
]);
|
|
|
|
$this->info("✓ Registro de log creado");
|
|
$this->newLine();
|
|
$this->info('✓ Proceso completado exitosamente!');
|
|
|
|
return Command::SUCCESS;
|
|
} catch (\Exception $e) {
|
|
$this->error("✗ Error: " . $e->getMessage());
|
|
return Command::FAILURE;
|
|
}
|
|
}
|
|
}
|
|
|