<file_sep><?php

namespace App\Http\Controllers\Secure;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Usuario;
use App\Http\Requests\ValCrearUsuario;

class UsuariosController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $usuarios = Usuario::orderBy('id', 'ASC')->paginate(5);
        return view('admin/usuarios/index')-> with('usuarios', $usuarios);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('secure/create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ValCrearUsuario $request)
    {
        $usuario = new Usuario($request->all());
        $usuario->password = <PASSWORD>($request->password);
        $usuario->rol =2;
        $usuario->save();

        flash('Usuario registrado exitosamente')->success();
        return redirect()->route('login')->with('mensaje', 'Se ha registrado con exito');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
<file_sep><?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Tasa;
use App\Models\Pais;
use App\Models\Moneda;
use Illuminate\Support\Facades\DB;

class TasaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $tasas = DB::table('tasas')->select('tasas.id as idTasa', 'tasas.tasa as tasa', 'paises.nombre as pais', 'paises.moneda as pmoneda',
                                                 'monedas.moneda as moneda')
                                    ->join('paises', 'tasas.pais', 'paises.id')
                                    ->join('monedas', 'tasas.moneda', 'monedas.id')
                                     ->orderBy('paises.id', 'ASC')->paginate(5);
        //dd($tasas);
        return view('admin/tasas/index')->with('tasas', $tasas);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $paises = Pais::pluck('nombre', 'id');
        $monedas = Moneda::pluck('moneda', 'id');
        return view('admin/tasas/create', compact('paises', 'monedas'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $Tasas = new Tasa($request->all());
        $Tasas->save();

        flash('Tasa registrada')->success();
        return redirect()->route('tasas.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $tasa = Tasa::select('tasas.id as idTasa', 'tasas.tasa as tasa', 'paises.nombre as pais', 'monedas.moneda as moneda')
                            ->join('paises', 'tasas.pais', 'paises.id')
                            ->join('monedas', 'tasas.moneda', 'monedas.id')
                            ->where('tasas.id', $id)->get()->toArray();
                            //dd($tasa);
        return view('admin/tasas/edit')->with('tasa', $tasa);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $tasa = Tasa::find($id);
        $tasa->tasa = $request->tasa;
        $tasa->save();

        flash('Tasa actualizada')->success();
        return redirect()->route('tasas.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       $user = Tasa::find($id);
       $user->delete();

       flash('Tasa eliminada con éxito')->error();

       return redirect()->route('tasas.index');
    }
}
<file_sep><?php

use Illuminate\Database\Seeder;

class TablaPaisSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('paises')->insert([

            ['nombre' => 'Colombia', 'bandera' => null ,'moneda' => 'COP'],
            ['nombre' => 'Venezuela', 'bandera' => null ,'moneda' => 'BsS'],
            ['nombre' => 'Chile', 'bandera' => null ,'moneda' => 'CLP'],
            ['nombre' => 'Perú', 'bandera' => null ,'moneda' => 'PEN'],
            ['nombre' => 'EEUU', 'bandera' => null ,'moneda' => 'USD'],
            ['nombre' => 'Todos', 'bandera' => null ,'moneda' => null],


    
        ]);
    }
}
<file_sep><?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Domicilio extends Model
{
    protected $table = "domicilios";
    protected $fillable = ['usuario', 'pais', 'ciudad', 'codigo_postal', 'direccion'];
    protected $guarded = ['id'];

    public function usuario()
    {
        return $this->belongsTo(Usuario::class, 'usuarios');
    }
}
<file_sep><?php

use Illuminate\Database\Seeder;

class TablaMonedaSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('monedas')->insert([

            [
            'moneda' => 'Peso Colombiano',
            'abrev' => 'COP'
            ],
            [
            'moneda' => 'Bolivar Soberano',
            'abrev' => 'BsS'
            ],
            [
            'moneda' => 'Peso Chileno',
            'abrev' => 'CLP'
            ],
            [
            'moneda' => 'Sol',
            'abrev' => 'PEN'
            ],
            [
            'moneda' => 'Dólar Americano',
            'abrev' => 'USD'
            ],
            [
            'moneda' => 'PayPal',
            'abrev' => 'PPL'
            ]
        ]);
    }
}
<file_sep><?php

namespace  App\Models;


use Illuminate\Foundation\Auth\User as Autenticable;
use Illuminate\Support\Facades\Session;

class Usuario extends Autenticable
{
    protected $remember_token = false;
    protected $table = "usuarios";
    protected $fillable = ['email', 'password'];
    protected $guarded = ['id'];

    public function rol()
    {
        return $this->belongsTo(Rol::class, 'rol');
    }

    public function setSession($rol)
    {
        $usd= Usuario::where('id', $this->id)->get()->toArray();
        $datosp = DatosPersonales::where('usuario', $this->id)->get()->toArray();
        $datosd = Domicilio::select('domicilios.*', 'paises.nombre as paisd')->where('usuario', $this->id)->
                    join('paises', 'domicilios.pais', 'paises.id')->get()->toArray();
        Session::put(
            [
                'rol' => $rol[0]['rol'],
                'rol_id' => $this->rol,
                'idUsuario' => $this->id,
                'emailUsuario' => $this->email,
            ]);
            if ($datosp) {
               
                Session::put([
                    'idDat' =>  $datosp[0]['id'],
                    'nombre' => $datosp[0]['nombre'],
                    'apellido' => $datosp[0]['apellido'],
                    'iddoctype' => $datosp[0]['iddoctype'],
                    'iddocnum' => $datosp[0]['iddocnum'],                    
                ]);
            }else {

                $datp = new DatosPersonales($usd);
                $datp->usuario = $usd[0]['id'];
                $datp->save();

                $datosp = DatosPersonales::where('usuario', $this->id)->get()->toArray();

                Session::put([

                    'idDat' =>  $datosp[0]['id'],
                    'nombre' => 'no especifica',
                    'apellido' => 'no especifica',
                    'iddoctype' => 'no especifica',
                    'iddocnum' => 'no especifica',                    
                ]);
            }

            if ($datosd) {
               
                Session::put([

                    'pais' => $datosd[0]['pais'],
                    'idDom' => $datosd[0]['id'],
                    'paisd' => $datosd[0]['paisd'],
                    'ciudad' => $datosd[0]['ciudad'],
                    'codigopl' => $datosd[0]['codigo_postal'],
                    'direccion' => $datosd[0]['direccion']                   
                ]);
            }else {

                $datd = new Domicilio($usd);
                $datd->usuario = $usd[0]['id'];
                $datd->save();

                $datosd = Domicilio::where('usuario', $usd[0]['id'])->get()->toArray();

                Session::put([

                    'pais' => 'no especifica',
                    'idDom' => $datosd[0]['id'],
                    'paisd' => 'no especifica',
                    'ciudad' => 'no especifica',
                    'codigopl' => 'no especifica',
                    'direccion' => 'no especifica'                   
                ]);
                
            }

    }
}

<file_sep><?php

use Illuminate\Database\Seeder;

class TablaDatosPersonalesSeeders extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('datos_personales')->insert([
            'usuario' => 1,
            'nombre' => 'John',
            'apellido' => 'Doe',
            'iddoctype' => 'V',
            'iddocnum' => '12345678998'
              ]);
    
         DB::table('datos_personales')->insert([
            'usuario' => 2,
            'nombre' => 'Jane',
            'apellido' => 'Doe',
            'iddoctype' => 'E',
            'iddocnum' => '89987654321'
              ]);             
    }
}
<file_sep><?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->truncateTablas([
            'roles', 'usuarios', 'datos_personales', 'paises', 'bancos', 'monedas', 'tasas'       
        ]);

        $this->call(TablaPaisSeeder::class);
        $this->call(TablaRolSeeder::class);
        $this->call(TablaUsuarioSeeder::class);
        $this->call(TablaDatosPersonalesSeeders::class);
        $this->call(TablaBancosSeeder::class);
        $this->call(TablaMonedaSeeder::class);
        $this->call(TablaTasasSeeder::class);

    }
    protected function truncateTablas(array $tablas)
    {
            DB::statement('SET FOREIGN_KEY_CHECKS = 0;');
            foreach($tablas as $tabla){
                DB::table($tabla)->truncate();
            }
            DB::statement('SET FOREIGN_KEY_CHECKS = 1;');
    }
}
<file_sep><?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CrearTablaDatosPersonales extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('datos_personales', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
             $table->unsignedBigInteger('usuario');
            $table->foreign('usuario')->references('id')->on('usuarios')->onDelete('cascade')->onUpdate('cascade');
            $table->string('nombre',20)->nullable();
            $table->string('apellido',20)->nullable();
            $table->string('iddoctype', 2)->nullable();
            $table->string('iddocnum', 20)->unique()->nullable();
            $table->charset ='utf8mb4';
            $table->collation = 'utf8mb4_spanish_ci';
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('datos_personales');
    }
}
<file_sep><?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'Secure\LoginController@index')->name('login');
Route::get('/secure/login', 'Secure\LoginController@index')->name('login');
Route::post('/secure/login', 'Secure\LoginController@login')->name('login_post');
Route::get('secure/logout', 'Secure\loginController@logout')->name('logout');
Route::get('/midred', 'MidRedController@index')->name('midred');
Route::get('/secure/create', 'Secure\UsuariosController@create');
Route::get('/secure/store', 'Secure\UsuariosController@store');


//RUTAS MODO ADMINISTRADOR
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'], function(){


    Route::get('/adminhome', 'AdminController@index')->name('adminhome');

    //PAISES
    Route::resource('paises', 'PaisController');

    //BANCOS
    Route::resource('bancos', 'BancosController');
    Route::get('bancos/{id}/destroy', [
        'uses' => 'BancosController@destroy',
        'as' => 'bancos.destroy'
    ]);
    //TASAS
    Route::resource('tasas', 'TasaController');
    Route::get('tasas/{id}/destroy', [
        'uses' => 'TasaController@destroy',
        'as' => 'tasas.destroy'
    ]);

    //USUARIOS
    Route::resource('usuarios', 'UsuariosController');
    
    //PERFIL DE USUARIO
    Route::resource('perfil', 'PerfilUsuarioController');
    Route::resource('datospersonales', 'DatosPersonalesController');
    Route::resource('datosbancarios', 'DatosBancariosController');
    Route::resource('domicilios', 'DomicilioController');

});

//RUTAS MODO USUARIO
Route::group(['prefix' => 'user', 'namespace' => 'User', 'middleware' => 'auth'], function(){

    Route::get('/usershome', 'UserController@index')->name('usershome');
    //BANCOS
    //USUARIOS


});







<file_sep><?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class DatosPersonales extends Model
{
    protected $table = "datos_personales";
    protected $fillable = ['nombre', 'apellido', 'iddoctype', 'iddocnum'];
    protected $guarded = ['id'];

    public function usuario()
    {
        return $this->belongsTo(Usuario::class, 'usuarios');
    }
}
<file_sep><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MidRedController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if(session()->get('rol_id') == 1){

            return redirect('/admin/adminhome');
        }
        else {
            return redirect('/user/usershome');
        }
    }

  
}
<file_sep><?php

namespace App\Http\Controllers\Secure;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/midred';
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('secure/index');
    }

    protected function authenticated(Request $request, $user)
    {
        $rol = $user->rol()->get();
      auth()->user()->setSession($rol->toArray());

    }

}
<file_sep><?php

use Illuminate\Database\Seeder;

class TablaBancosSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('bancos')->insert([
            'pais' => 2,
            'nombre' => 'Banco Nacional de Credito',
            'abrev' => 'BNC',
            'prefijo' => '0191',
              ]);
    }
}
<file_sep><?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Pais;
use App\Models\Domicilio;
use Illuminate\Support\Facades\Session;

class DomicilioController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $paises = Pais::pluck('nombre', 'id');
        return view('admin/usuarios/editarDomicilio')->with('paises', $paises);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $dom = Domicilio::find($id);
        $dom->pais = $request->pais;
        $dom->ciudad = $request->ciudad;
        $dom->codigo_postal = $request->codigopl;
        $dom->direccion = $request->direccion;

        $dom->save();
        $datosd = Domicilio::select('domicilios.*', 'paises.nombre as paisd')->where('usuario', session()->get('idUsuario'))->
        join('paises', 'domicilios.pais', 'paises.id')->get()->toArray();
        Session::put(
            [
                'paisd' => $datosd[0]['paisd'],
                'pais' => $datosd[0]['pais'],
                'ciudad' => $datosd[0]['ciudad'],
                'codigopl' => $datosd[0]['codigo_postal'],
                'direccion' => $datosd[0]['direccion']
            ]);

        flash('Domicilio actualizado')->success();
        return redirect()->route('perfil.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
<file_sep><?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ValCrearUsuario extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [

            'email' => 'required|max:50|unique:usuarios,email,' . $this->route('id'),
            'password' => '<PASSWORD>|max:12|min:10',
            'checkpp' => 'required'

        ];
    }
    public function messages()
    {
        return [

            'email.required' => 'El campo correo es obligatorio',
            'pass.required' => 'El campo contraseña es obligatorio',
            'checkpp.required' => 'Para registrarse, debe aceptar los términos y condiciones',
        ];
    }
}
<file_sep><?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CrearTablaTasas extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tasas', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('pais');
            $table->foreign('pais')->references('id')->on('paises')->onDelete('cascade')->onUpdate('cascade');
            $table->unsignedBigInteger('moneda');
            $table->foreign('moneda')->references('id')->on('monedas')->onDelete('cascade')->onUpdate('cascade');
            $table->float('tasa', 255,5)->nullable();
            $table->charset ='utf8mb4';
            $table->collation = 'utf8mb4_spanish_ci';

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tasas');
    }
}
<file_sep><?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Tasa;
use App\Models\Pais;
use App\Models\PerfilUsuario;
use Illuminate\Support\Facades\Session;
use App\Models\DatosPersonales;
use App\Models\Domicilio;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {


         $tasas = Tasa::join('paises', 'tasas.pais', 'paises.id' )
                     ->join('monedas', 'tasas.moneda', 'monedas.id')
                     ->whereIn('tasas.pais', [session()->get('pais'), 6])->get()->toArray();
        //dd($datosp);
        return view('/admin/admin/inicio', compact('tasas'));
    }

}
<file_sep><?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Pais extends Model
{
    protected $table ="paises";


    protected $fillable = ['nombre', 'bandera', 'moneda'];
    protected $guarded = ['id'];

    public function pais()
    {
        return $this->belongsTo(Pais::class, 'paises');
    }
}
<file_sep><?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Tasa extends Model
{
    protected $table = "tasas";
    protected $fillable = ['pais', 'moneda', 'tasa'];
    protected $guarded = ['id'];

    public function pais()
    {
        return $this->belongsTo(Pais::class, 'paises');
    }

}
<file_sep><?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class DatosPersonales extends Model
{
    protected $table = "domicilios";
    protected $fillable = ['usuario', 'pais', 'ciudad', 'codigo_postal', 'direccion'];
    protected $guarded = ['id'];

    public function usuario()
    {
        return $this->belongsTo(Usuario::class, 'usuarios');
    }
    public function pais()
    {
        return $this->belongsTo(Pais::class, 'paises');
    }
}
<file_sep><?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Banco extends Model
{
    protected $table = "bancos";

    protected $fillable = ['nombre',
                           'prefijo'
                          ];
}
<file_sep><?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class DatosBancarios extends Model
{
    protected $table = "datos_bancarios";
    protected $fillable = ['usuario', 'banco', 'tcuenta', 'cuenta'];
    protected $guarded = ['id'];

    public function usuario()
    {
        return $this->belongsTo(Usuario::class, 'usuarios');
    }
    public function banco()
    {
        return $this->belongsTo(Banco::class, 'bancos');
    }
}
<file_sep><?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Tasa;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tasas = Tasa::join('paises', 'tasas.pais', 'paises.id' )
        ->join('monedas', 'tasas.moneda', 'monedas.id')
        ->whereIn('tasas.pais', [session()->get('pais'), 6])->get()->toArray();

        return view('/users/users/inicio', compact('tasas'));
    }


}
