MINI MINI MANI MO

Path : /var/www/html/sistemas/saplxi/app/Imports/
File Upload :
Current File : /var/www/html/sistemas/saplxi/app/Imports/ImportVotacion.php

<?php

namespace App\Imports;

use App\Models\CddDiputadosLxi;
use App\Models\CddIniciativas;
use App\Models\cddVotacion;
use App\Models\Decretos;
use App\Models\VotacionDecretos;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class ImportVotacion implements ToCollection
{
    public $tipo;

    public function __construct($tipo)
    {
        $this->tipo = $tipo;
    }

    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {


        $tipo = $this->tipo;
        unset($collection[0][0]);
        $iniciativas = $collection[0][1];
        $iniciativas = explode(",", $iniciativas);
        unset($collection[0], $collection[1]);

        foreach ($iniciativas as $item) {
            if (!is_null($item)) {
                $iniciativa = CddIniciativas::where("folio", $item)->where("categoria", $tipo)->first();

                if ($iniciativa) {
                    $i = $iniciativa->update([
                        "status" => 'Aprobada',
                    ]);
                    foreach ($iniciativa->decretos as $item){
                        foreach ($item->votacionDecreto as $q){
                            $q->delete();
                        }
                    }
                    if($iniciativa->decretos){
//                        dd($iniciativa->decretos);
//                        $iniciativa->decretos()->votacionDecreto()->delete();

                    }
                        $decreto = Decretos::create([
                            "num_decreto" => '222',
                            "fecha_decreto" => '2023-12-05',
                            "nombre_decreto" => 'POR EL QUE SE APRUEBAN LAS TABLAS DE VALORES UNITARIOS DE SUELO Y DE CONSTRUCCIONES PARA EL EJERCICIO FISCAL 2024. ',
                            "decreto" => 'DECRETO 222.pdf',
                            "id_iniciativa" => $iniciativa->id,
                        ]);

//                    dd($collection);
//                    $iniciativa->votacion()->delete();
                    foreach ($collection as $it) {
                        $nombre = explode(" ", $it[1]);
                        $diputado = CddDiputadosLxi::where("apaterno", "LIKE", "%" . $nombre[0] . "%")
                            ->where("nombres", "LIKE", "%" . $nombre[count($nombre) - 1] . "%")->withTrashed()->first();

                        if ($diputado) {
                            if ($it[2] == "A favor") {
                                $voto = 1;
                            } elseif ($it[2] == "En contra" || $it[2] == "Contra") {
                                $voto = 2;
                            } elseif ($it[2] == "Abstención" || $it[2] == "Abstencion") {
                                $voto = 3;
                            } else {
                                $voto = 4;
                            }

                            $data = [
                                "id_decretos" => $decreto->id,
                                "id_diputado" => $diputado->id,
                                "id_partido" =>  $diputado->partido->id,
                                "voto" => $voto
                            ];
                            $v = VotacionDecretos::create($data);
                        }
                    }
                } else {
                    return redirect()->back()->with("error", "No existe");
                }
            }
        }
//        dd('listo');
    }
}

OHA YOOOO