/home/dvjjulio/softtrash/app/Http/Controllers/RouteController.php
<?php
namespace Trash\Http\Controllers;
use Illuminate\Http\Request;
use Trash\Http\Requests;
use Trash\Http\Controllers\Controller;
use Trash\User;
use Trash\Clientes;
use Trash\Ordenes;
use Trash\Reciclables;
use Trash\Histories;
use Trash\CombustibleOrden;
use Trash\VertederoOrden;
class RouteController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
if(isset($_COOKIE['id'])){
$users = new User();
$user = $users->user_camion($_COOKIE['id']);
$clientes = Clientes::all();
$ordenes = new Ordenes();
$orden = $ordenes->checkOrdenInicio($_COOKIE['id']);
$reciclables = Reciclables::all();
$histories = new Histories();
$km_day = $histories->km_inicio($_COOKIE['id']);
$km;
$fecha_ = date('Y-m-d H:i:s');
$orden_no = $ordenes->orden_inicio($_COOKIE['id']);
if($orden_no->getData()->status == true){
$orden_no = $orden_no->getData()->data[0]->orden_no;
}else{
$orden_no = '';
}
if($km_day->getData()->status != false){
$km_orden = $ordenes->km_day($km_day->getData()->data[0]->kilometraje_inicio, $_COOKIE['id']);
if($km_orden->getData()->status!=false){
$km = $km_orden->getData()->data[0]->km;
$fecha_= $km_orden->getData()->data[0]->fecha_termino;
}else{
$km = $km_day->getData()->data[0]->kilometraje_inicio;
$fecha_= $km_day->getData()->data[0]->fecha_inicio;
}
return view('movil.route')->with(compact('user','clientes','orden','reciclables','km','orden_no','fecha_'));
}else{
return redirect('movil/index');
}
}else{
//return 'no id';
header("Location: ".\URL::To("/")."/movil/login");
exit;
// redirect(\URL::To("/")."/movil/login");
}
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function clientes()
{
//
$data = \Input::all();
$clientes = new Clientes();
$cliente = $clientes->getCliente_route();
if($cliente!= null){
return response()->json(array('status'=>true, 'data'=>$cliente));
}else{
return response()->json(array('status'=>false, 'message'=>'Error en la conexión'));
}
}
public function saveRouteInicio(){
$data = \Input::all();
$validator = $this->get_validate($data);
if( $validator->fails() )
return response()->json( array( 'status' => false, 'message' => $validator->errors()->all() ));
return response()->json( $this->store($data));
}
public function get_validate( $data ){
$array_rules = array(
'orden' => 'required',
'latitude' => 'required',
'longitude' => 'required',
'user_id' => 'required|integer',
'cliente_id' => 'required|integer',
'camion_id' => 'required|integer' );
$messages = array(
'orden.required' => 'Por favor ingrese el Kilometraje',
// 'orden.integer' => 'Por favor ingrese el formato correcto',
'latitude.required' => 'Error con la latitude encienda su GPS',
'longitude.required' => 'Error con la longitude encienda su GPS',
'user_id.required' => 'Error en el usuario favor de salir y volver a logearte',
'user_id.integer' => 'Error en el usuario favor de salir y volver a logearte',
'cliente_id.required' => 'Error en el cliente favor de seleccionar un cliente',
'cliente_id.integer' => 'Error en el formato de cliente',
'camion_id.required' => 'Error en el camion favor de salir y volver a logearte',
'camion_id.integer' => 'Error en el camion favor de salir y volver a logearte',
);
return \Validator::make( $data, $array_rules, $messages );
}
public function saveRouteTermino()
{
//
$data = \Input::all();
if ($data['giro'] === 'Combustible') {
$validator = $this->get_validate_termino_gasolinera($data);
} else if ($data['giro'] === 'Vertedero') {
$validator = $this->get_validate_termino_vertedero($data);
} else {
$validator = $this->get_validate_termino($data);
}
if( $validator->fails() )
return response()->json( array( 'status' => false, 'message' => $validator->errors()->all() ));
return response()->json( $this->store($data));
}
public function get_validate_termino( $data ) {
$array_rules = array(
'km' => 'required|integer',
'cantidad' => 'required',
'latitude_termino' => 'required',
'longitude_termino' => 'required');
$messages = array(
'km_termino.required' => 'Por favor ingrese el Kilometraje',
'km_termino.integer' => 'Por favor ingrese el formato correcto',
'cantidad.required' => 'Por favor ingrese la cantidad',
'latitude_termino.required' => 'Error con la latitude encienda su GPS',
'longitude_termino.required' => 'Error con la longitude encienda su GPS'
);
return \Validator::make( $data, $array_rules, $messages );
}
public function get_validate_termino_gasolinera( $data ){
$array_rules = array(
'km' => 'required|integer',
'nota' => 'required',
'hora' => 'required',
'litros' => 'required',
'precio' => 'required',
'latitude_termino' => 'required',
'longitude_termino' => 'required');
$messages = array(
'km_termino.required' => 'Por favor ingrese el Kilometraje',
'km_termino.integer' => 'Por favor ingrese el formato correcto',
'nota.required' => 'Por favor ingrese la nota',
'hora.required' => 'Por favor ingrese la nota',
'litros.required' => 'Por favor ingrese los litros',
'precio.required' => 'Por favor ingrese el precio por litro',
'latitude_termino.required' => 'Error con la latitude encienda su GPS',
'longitude_termino.required' => 'Error con la longitude encienda su GPS'
);
return \Validator::make( $data, $array_rules, $messages );
}
public function get_validate_termino_vertedero( $data ){
$array_rules = array(
'km' => 'required|integer',
'nota' => 'required',
'hora' => 'required',
'peso' => 'required',
'precio' => 'required',
'latitude_termino' => 'required',
'longitude_termino' => 'required');
$messages = array(
'km_termino.required' => 'Por favor ingrese el Kilometraje',
'km_termino.integer' => 'Por favor ingrese el formato correcto',
'nota.required' => 'Por favor ingrese la nota',
'hora.required' => 'Por favor ingrese la nota',
'peso.required' => 'Por favor ingrese el peso neto',
'precio.required' => 'Por favor ingrese el precio por litro',
'latitude_termino.required' => 'Error con la latitude encienda su GPS',
'longitude_termino.required' => 'Error con la longitude encienda su GPS'
);
return \Validator::make( $data, $array_rules, $messages );
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store($data, Ordenes $orden = null)
{
try{
\DB::beginTransaction();
$fecha_termio = date('Y-m-d H:i:s');
if( isset( $data[ 'id' ] ) ){
$orden = Ordenes::find($data[ 'id' ]);
if($data['check_reciclable'] == 'true'){
$orden->reciclable_id = $data['reciclable'];
$orden->peso_reciclable = $data['reciclable_cantidad'];
}
if ($data['giro'] === 'Combustible') {
$total = ($data['litros'] * $data['precio']);
$iva = ($total * .16);
$subtotal = ($total - $iva);
$this->updateYield($data['id'], $data['km'], $data['litros']);
$combustible = new CombustibleOrden();
$combustible->orden_id = $data['id'];
$combustible->nota = $data['nota'];
$combustible->hora = $data['hora'];
$combustible->litros = $data['litros'];
$combustible->precio_litro = $data['precio'];
$combustible->subtotal = $subtotal;
$combustible->iva = $iva;
$combustible->total = $total;
$combustible->save();
} else if ($data['giro'] === 'Vertedero') {
$total = ($data['peso'] * $data['precio']);
$vertedero = new VertederoOrden();
$vertedero->orden_id = $data['id'];
$vertedero->nota = $data['nota'];
$vertedero->hora = $data['hora'];
$vertedero->peso_neto = $data['peso'];
$vertedero->precio = $data['precio'];
$vertedero->total = $total;
$vertedero->save();
} else {
$orden->cantidad = $data['cantidad'];
}
$orden->fecha_termino = $fecha_termio;
//$orden->peso = $data['peso'];
$orden->km = $data['km'];
$orden->km_dif = $data['km_dif'];
$orden->observaciones = $data['observaciones'];
$orden->latitude_termino = $data['latitude_termino'];
$orden->longitude_termino = $data['longitude_termino'];
}
else {
$orden = new ordenes();
$existOrden = $orden->checkOrden($data);
if( $existOrden == false )throw new \Exception('El Numero de Orden ya fue utilizado ');
$fecha_inicio = date('Y-m-d H:i:s');
$fecha_traslado = \Helper::getDif($data['fecha_'], $fecha_inicio);
$orden->orden_no = $data['orden'];
$orden->user_id = $_COOKIE['id'];;
$orden->cliente_id = $data['cliente_id'];
$orden->camion_id = $data['camion_id'];
$orden->fecha_inicio = $fecha_inicio;
$orden->tiempo_traslado = $fecha_traslado;
$orden->latitude_inicio = $data['latitude'];
$orden->longitude_inicio = $data['longitude'];
}
$orden->save();
} catch( \Exception $e ){
\DB::rollback();
return array( 'status' => false, 'message' => $e->getMessage() );
}
\DB::commit();
return array( 'status' => true , 'data' => $orden );
}
public function createUpdateCombustible($data) {
try {
if (isset($data['id'])) {
$combustible = CombustibleOrden::where('orden_id', $data['id'])->first();
} else {
$combustible = new CombustibleOrden();
}
$total = (floatval($data['litros']) * floatval($data['precio']));
$iva = ($total * .16);
$subtotal = ($total - $iva);
$combustible->orden_id = $data['id'];
$combustible->nota = $data['nota'];
$combustible->hora = $data['hora'];
$combustible->litros = floatval($data['litros']);
$combustible->precio_litro = floatval($data['precio']);
$combustible->subtotal = $subtotal;
$combustible->iva = $iva;
$combustible->total = $total;
if ($combustible->save()) {
return true;
} else {
return false;
}
} catch (\Throwable $th) {
throw new \Exception('Error en el guardado de Combustible'. $th->getMessage());
}
}
public function createUpdateVertedero($data) {
try {
$total = (floatval($data['peso']) * floatval($data['precio']));
if (isset($data['id'])) {
$vertedero = VertederoOrden::where('orden_id', $data['id'])->first();
} else {
$vertedero = new VertederoOrden();
}
$vertedero->orden_id = $data['id'];
$vertedero->nota = $data['nota'];
$vertedero->hora = $data['hora'];
$vertedero->peso_neto = floatval($data['peso']);
$vertedero->precio = floatval($data['precio']);
$vertedero->total = $total;
if ($vertedero->save()) {
return true;
} else {
return false;
}
} catch (\Throwable $th) {
throw new \Exception('Error en el guardado de Combustible');
}
}
public function updateYield($ordenId, $km_current) {
$combustible = new CombustibleOrden();
$dataYield = $combustible->getLastKm($ordenId);
try {
if (!$dataYield) return;
$combustible_copy = $combustible::where('orden_id', $dataYield[0]->id)->get()[0];
if ($dataYield[0]) {
$km_recorridos = $km_current - $dataYield[0]->km;
$rendimiento = $dataYield[0]->litros / $km_recorridos;
$combustible_copy->rendimiento = $rendimiento;
return $combustible_copy->save();
// return $combustible->updateYield($dataYield[0]->id, $rendimiento);
}
} catch (\Exception $e) {
$combustible_copy->rendimiento = 0;
return $combustible_copy->save();
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function clientesNear()
{
$data = \Input::all();
$clientes = new Clientes();
return $cliente = $clientes->clientesNear($data['latitude'],$data['longitude']);
}
/**
* 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)
{
//
}
}