File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/Imports/SubscribersImport.php
<?php
namespace App\Http\Controllers\Imports;
use App\Http\Controllers\Controller;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use App\Zone;
use App\Seat;
use App\Letter;
use App\Module;
use DB;
use App\PreSubscriber;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class SubscribersImport implements ToCollection, WithHeadingRow
{
public $answer = [];
public $edit = [
'creados' => 0,
];
public $is_error;
public $request;
/**
* @param Collection $collection
*/
public function collection(Collection $rows)
{
$firstrow = $rows->first()->toArray();
if (isset($firstrow['documento'])) {
$zones = Zone::select('name', 'id', 'zone_id')->where('active', true)->get();
$get_seat = Seat::select('zone_id')->where('active', true)->get();
$letters = Letter::select('id', 'name')->get();
$is_error = false;
DB::beginTransaction();
$cont = 0;
foreach ($rows as $row) {
if ($row["documento"] && $row["documento"] != "") {
$cont++;
$document_information = $row["documento"];
$user_email = $row["correo"];
$seat_information = $row["silla"];
$fila_information = $row["fila"];
$price_information = intval(preg_replace("/[^0-9]/", "", $row["precio"]));
$zone_information = $row["tribuna"];
$zone_information = explode(" ", $zone_information);
$is_credit = $row["credito"] && $row["credito"] == "x" ? true : false;
if ($zone_information[0] == "VIP" || $zone_information[0] == "VVIP") {
$zone_information[0] = "Occidental";
} else {
$zone_information;
}
// Elimino espacios en blanco inicio y final
$user_email = trim($user_email);
// Ignora lineas en blanco
if (!$user_email && !$zone_information) {
continue;
}
if (!$zone_information) {
$this->answer[] = [
'message' => 'No hay informacion de la zona',
'state' => false,
'line' => $cont
];
$is_error = true;
continue;
}
// obtenemos el id de la tribuna
$id_tribune = $zones->first(function ($item) use ($zone_information) {
return $item->name == $zone_information[0];
});
if (!$id_tribune) {
$this->answer[] = [
'message' => 'Información de la tribuna no encontrada' . $zone_information[0] . ' linea: ' . $cont,
'state' => false,
'line' => $cont
];
$is_error = true;
continue;
}
$sector_information = $row["sector"];
if (!$sector_information) {
$this->answer[] = [
'message' => 'Información de sector no especificada: ' . $sector_information . ' linea: ' . $cont,
'state' => false,
'line' => $cont
];
$is_error = true;
continue;
}
$zone_id_extracted = preg_replace('/[^0-9]/', '', $sector_information); // me extrae los numeros de un string
$integer = (int)$zone_id_extracted; //convertimos el strig en un numero
// filtro la zona id de la tabla seat
$num_zone_id = $get_seat->first(function ($item) use ($zone_id_extracted) {
return $item->zone_id == $zone_id_extracted;
});
if (!$num_zone_id) {
$this->answer[] = [
'message' => 'Información de zona no especificada: ' . $sector_information . ' linea: ' . $cont,
'state' => false,
'line' => $cont
];
$is_error = true;
continue;
}
//obtenemos el id de la letra
$id_letter = $letters->first(function ($item) use ($fila_information) {
return $item->name == $fila_information;
});
if (!$id_letter) {
$this->answer[] = [
'message' => 'Información de silla no especificada: ' . $fila_information . ' linea: ' . $cont,
'state' => false,
'line' => $cont
];
$is_error = true;
continue;
}
$seat = Seat::select('id')->where('code', $seat_information)->where('zone_id', $num_zone_id->zone_id)->where('letter_id', $id_letter->id)->first();
if (!$seat) {
$this->answer[] = [
'message' => 'Silla no encontrada.' . $seat_information . ' linea: ' . $cont,
'state' => false,
'line' => $cont
];
$is_error = true;
continue;
}
if ($seat) {
PreSubscriber::create([
'document' => $document_information,
'email' => $user_email,
'seat_id' => $seat->id,
'price' => $price_information,
'is_credit' => $is_credit,
]);
$this->edit['creados'] += 1;
}
}
}
if ($is_error || sizeof($rows) <= 0) {
DB::rollback();
return;
} else {
DB::commit();
$userModule = Module::where("route", "preSubscribers")->first();
$controller = new Controller;
$controller->registerLog(Auth::user()->id, 'Importar abonados', json_encode($rows), "Update", $userModule->id);
}
}
}
}