<?php

namespace App\Http\Controllers\RectificacionesAntiguas;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\User;

class c_rectificacionesAnt extends Controller
{

////////////////////////////////// MANZANILLO /////////////////////////////////
    public function recAntMZ(Request $request){
        $contRef = 0;
        $id = auth()->user()->id;
        $datos = DB::connection('usuariosMysql')->select("select * from vw_list_user WHERE id = '".$id."'");
        foreach($datos as $var){$mail = $var->email;}

        $ref = DB::select("SELECT * FROM rectificaciones_mz WHERE folio LIKE '".$request['ref']."%'");
        foreach($ref as $var){$contRef++;}
        $contRef = $contRef+1;

        $contRef = $request['ref'].'-'.$contRef;
        $cliente = $request['clt'];
        $especificar = $request['espRec'];
        $cargo = $request['cargoA'];

        $MZ = DB::select("SELECT * FROM rectificaciones_mz ORDER BY id DESC LIMIT 1");
        foreach($MZ as $var){ $pedimento_pro = $var->pedimento; }

        $pedimento_pro = $pedimento_pro+1;
        $caracteres = strlen ( $pedimento_pro );
        $age = config('constants.fechaBitacora');
        if($caracteres == 7){ $pedimento_pro = $pedimento_pro; }
        if($caracteres == 5){ $pedimento_pro = ''.$age.'0'.$pedimento_pro; }

        $MZ = DB::select("SELECT * FROM rectificaciones_mz ORDER BY id DESC LIMIT 1");
        foreach($MZ as $var){ $pedimento_verif = $var->pedimento; }

        if($pedimento_pro != $pedimento_verif){
            DB::statement('ALTER TABLE rectificaciones_mz AUTO_INCREMENT = 1');
            DB::insert("INSERT INTO rectificaciones_mz VALUES(null, '".$contRef."', '".$pedimento_pro."', '".$cliente."', '-', '-', '-', null, '".$mail."', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '".$especificar."', '".$cargo."')");

            $list = '<tr>';
            $list .= '<td>'.$pedimento_pro.'</td>';
            $list .= '<td>'.$contRef.'</td>';
            $list .= '</tr>';
            $list .= "<script>alertify.success('La rectificacion se registro con exito!');</script>";
        }else{
            $list = "<script>alertify.error('Alguien te gano por milisegundos');</script>";
        }

        return $list;
    }

///////////////////////////////////////////////// VERACRUZ ///////////////////////////////

    public function recAntVZ(Request $request){
        $contRef = 0;
        $id = auth()->user()->id;
        $datos = DB::connection('usuariosMysql')->select("select * from vw_list_user WHERE id = '".$id."'");
        foreach($datos as $var){$mail = $var->email;}

        $ref = DB::select("SELECT * FROM rectificaciones_vz WHERE folio LIKE '".$request['ref']."%'");
        foreach($ref as $var){$contRef++;}
        $contRef = $contRef+1;

        $contRef = $request['ref'].'-'.$contRef;
        $cliente = $request['clt'];
        $especificar = $request['espRec'];
        $cargo = $request['cargoA'];

        $VZ = DB::select("SELECT * FROM rectificaciones_vz ORDER BY id DESC LIMIT 1");
        foreach($VZ as $var){ $pedimento_pro = $var->pedimento; }

        $pedimento_pro = $pedimento_pro+1;
        $caracteres = strlen ( $pedimento_pro );
        $age = config('constants.fechaBitacora');
        if($caracteres == 7){ $pedimento_pro = $pedimento_pro; }
        if($caracteres == 4){ $pedimento_pro = ''.$age.'00'.$pedimento_pro; }

        $VZ = DB::select("SELECT * FROM rectificaciones_vz ORDER BY id DESC LIMIT 1");
        foreach($VZ as $var){ $pedimento_verif = $var->pedimento; }

        if($pedimento_pro != $pedimento_verif){
            DB::statement('ALTER TABLE rectificaciones_vz AUTO_INCREMENT = 1');
            DB::insert("INSERT INTO rectificaciones_vz VALUES(null, '".$contRef."', '".$pedimento_pro."', '".$cliente."', '-', '-', '-', null, '".$mail."', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '".$especificar."', '".$cargo."')");

            $list = '<tr>';
            $list .= '<td>'.$pedimento_pro.'</td>';
            $list .= '<td>'.$contRef.'</td>';
            $list .= '</tr>';
            $list .= "<script>alertify.success('La rectificacion se registro con exito!');</script>";
        }else{
            $list = "<script>alertify.error('Alguien te gano por milisegundos');</script>";
        }

        return $list;
    }

/////////////////////////////// LÁZARO CÁRDENAZ //////////////////////////////////////

    public function recAntLZ(Request $request){
        $contRef = 0;
        $id = auth()->user()->id;
        $datos = DB::connection('usuariosMysql')->select("select * from vw_list_user WHERE id = '".$id."'");
        foreach($datos as $var){$mail = $var->email;}

        $ref = DB::select("SELECT * FROM rectificaciones_lz WHERE folio LIKE '".$request['ref']."%'");
        foreach($ref as $var){$contRef++;}
        $contRef = $contRef+1;

        $contRef = $request['ref'].'-'.$contRef;
        $cliente = $request['clt'];
        $especificar = $request['espRec'];
        $cargo = $request['cargoA'];

        $LZ = DB::select("SELECT * FROM rectificaciones_lz ORDER BY id DESC LIMIT 1");
        foreach($LZ as $var){ $pedimento_pro = $var->pedimento; }

        $pedimento_pro = $pedimento_pro+1;
        $caracteres = strlen ( $pedimento_pro );
        $age = config('constants.fechaBitacora');
        if($caracteres == 7){ $pedimento_pro = $pedimento_pro; }
        if($caracteres == 4){ $pedimento_pro = ''.$age.'00'.$pedimento_pro; }

        $LZ = DB::select("SELECT * FROM rectificaciones_lz ORDER BY id DESC LIMIT 1");
        foreach($LZ as $var){ $pedimento_verif = $var->pedimento; }

        if($pedimento_pro != $pedimento_verif){
            DB::statement('ALTER TABLE rectificaciones_lz AUTO_INCREMENT = 1');
            DB::insert("INSERT INTO rectificaciones_lz VALUES(null, '".$contRef."', '".$pedimento_pro."', '".$cliente."', '-', '-', '-', null, '".$mail."', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '".$especificar."', '".$cargo."')");

            $list = '<tr>';
            $list .= '<td>'.$pedimento_pro.'</td>';
            $list .= '<td>'.$contRef.'</td>';
            $list .= '</tr>';
            $list .= "<script>alertify.success('La rectificacion se registro con exito!');</script>";
        }else{
            $list = "<script>alertify.error('Alguien te gano por milisegundos');</script>";
        }

        return $list;
    }

////////////////////////////////// MÉXICO ////////////////////////////

    public function recAntMX(Request $request){
        $contRef = 0;
        $id = auth()->user()->id;
        $datos = DB::connection('usuariosMysql')->select("select * from vw_list_user WHERE id = '".$id."'");
        foreach($datos as $var){$mail = $var->email;}

        $ref = DB::select("SELECT * FROM rectificaciones_mx WHERE folio LIKE '".$request['ref']."%'");
        foreach($ref as $var){$contRef++;}
        $contRef = $contRef+1;

        $contRef = $request['ref'].'-'.$contRef;
        $cliente = $request['clt'];
        $especificar = $request['espRec'];
        $cargo = $request['cargoA'];

        $MX = DB::select("SELECT * FROM rectificaciones_mx ORDER BY id DESC LIMIT 1");
        foreach($MX as $var){ $pedimento_pro = $var->pedimento; }

        $pedimento_pro = $pedimento_pro+1;
        $caracteres = strlen ( $pedimento_pro );
        $age = config('constants.fechaBitacora');
        if($caracteres == 7){ $pedimento_pro = $pedimento_pro; }
        if($caracteres == 4){ $pedimento_pro = ''.$age.'00'.$pedimento_pro; }

        $MX = DB::select("SELECT * FROM rectificaciones_mx ORDER BY id DESC LIMIT 1");
        foreach($MX as $var){ $pedimento_verif = $var->pedimento; }

        if($pedimento_pro != $pedimento_verif){
            DB::statement('ALTER TABLE rectificaciones_mx AUTO_INCREMENT = 1');
            DB::insert("INSERT INTO rectificaciones_mx VALUES(null, '".$contRef."', '".$pedimento_pro."', '".$cliente."', '-', '-', '-', null, '".$mail."', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '".$especificar."', '".$cargo."')");

            $list = '<tr>';
            $list .= '<td>'.$pedimento_pro.'</td>';
            $list .= '<td>'.$contRef.'</td>';
            $list .= '</tr>';
            $list .= "<script>alertify.success('La rectificacion se registro con exito!');</script>";
        }else{
            $list = "<script>alertify.error('Alguien te gano por milisegundos');</script>";
        }

        return $list;
    }
}
