/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { $success = Vehicle::find($id)->delete(); if ($success) { return redirect('/vehicles'); } else { return redirect('/vehicles', 422); } }
/** * Function that updates for a vehicle. * * @return Response */ public function updateVehicle() { // Validate Input. $validator = Validator::make(Input::all(), array('vehicle' => 'required', 'brand' => 'required', 'model' => 'required', 'plate' => 'required', 'type' => 'required', 'efficiency' => 'required', 'repair' => 'required', 'value' => 'required', 'depreciation' => 'required')); if ($validator->fails()) { return response()->json(['error' => 'Informacion incompleta!']); } // Check that user is part of authorized staff. if (Auth::user()->Type != 1) { // If they are unauthorized no point in returning anything. return response()->json(array()); } // Get vehicle. $vehicle = Vehicle::find(Input::get('vehicle')); $vehicle->Brand = Input::get('brand'); $vehicle->Model = Input::get('model'); $vehicle->PlateNumber = Input::get('plate'); $vehicle->Type = Input::get('type'); $vehicle->Efficiency = Input::get('efficiency'); $vehicle->Repair = Input::get('repair'); $vehicle->Value = Input::get('value'); $vehicle->Depreciation = Input::get('depreciation'); $vehicle->save(); $response['state'] = 'Success'; $response['vehicle'] = $vehicle; return response()->json($response); }
/** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $vehicle = Vehicle::findOrFail($id); $license_types = VehicleType::find($vehicle->vehicle_type_id)->license_types->toArray(); $vehicle_types = VehicleType::all()->toArray(); $vehicle_type_options = array(); if (!empty($vehicle_types)) { foreach ($vehicle_types as $vehicle_type) { $vehicle_type_options[$vehicle_type['id']] = $vehicle_type['type']; } } $license_type_options = array(); if (!empty($license_types)) { foreach ($license_types as $license_type) { $license_type_options[$license_type['id']] = $license_type['type']; } } $clients = User::select('id', 'first_name', 'last_name')->where('user_role', '=', '2')->get()->toArray(); $client_options = array(); if (!empty($clients)) { foreach ($clients as $client) { $client_options[$client['id']] = $client['first_name'] . " " . $client['last_name']; } } $license_type_arr = array(); $license_types = Vehicle::find($id)->license_types->toArray(); if (!empty($license_types)) { foreach ($license_types as $license_type) { $license_type_arr[] = $license_type['id']; } } return view('vehicle.update', compact('vehicle', 'vehicle_type_options', 'license_type_options', 'client_options', 'license_type_arr')); }
/** * Execute the console command. * * @return mixed */ public function handle() { // Get all transport requests for today group them by vehicle. $transportRequests = Transport::where('Date', '=', date('Y-m-d'))->where('State', '=', 2)->orderBy('VehicleId')->get(); if (count($transportRequests) > 0) { // Seperate all transport request of first vehicle. $vehicleId = 0; $selectedTransport = array(); $sortedByVehicle = array(); foreach ($transportRequests as $transport) { if ($vehicleId == 0) { $vehicleId = $transport->VehicleId; } if ($vehicleId != $transport->VehicleId) { array_push($sortedByVehicle, $selectedTransport); $selectedTransport = array(); $vehicleId = $transport->VehicleId; } else { array_push($selectedTransport, $transport); } } // Add final set to sortedByVehicle array. array_push($sortedByVehicle, $selectedTransport); $clusterArray = array(); foreach ($sortedByVehicle as $s) { // Get other available vehicles of selected vehicle type. $vehicle = Vehicle::find($s[0]->VehicleId); $vehicles = Vehicle::where('Type', '=', $vehicle->Type)->where('Repair', '=', false)->get(); // Check if we have more transport requests than vehicles. if (count($s) >= count($vehicles)) { // If we do go ahead with K-means algorithm. $kmean = array(); $used = array(); foreach ($vehicles as $v) { // Randomly select x amount of indexes from lat lon cluster array and add them to k object. array_push($kmean, array('VehicleId' => $v->Id, 'Mean' => $this->getRandomLocation($s, $used), 'Cluster' => array())); } // Now compare all lat lon points by distance to selected points and add them to closest point cluster. foreach ($s as $t) { $index = $this->getClosest($t, $kmean); array_push($kmean[$index]['Cluster'], $t); } // Start loop until no further changes are made. $changed = true; $limit = 500; while ($changed && $limit > 1) { $changed = false; // Update each mean in k object. foreach ($kmean as $i => $k) { $newMean = $this->calculateMean($k['Cluster']); if ($newMean['Lat'] != $k['Mean']['Lat'] || $newMean['Lat'] != $k['Mean']['Lat']) { $kmean[$i]['Mean'] = $newMean; $changed = true; } // Clean cluster array in each k-object. $kmean[$i]['Cluster'] = array(); } // Compare lat lon points by distance again. foreach ($s as $t) { $index = $this->getClosest($t, $kmean); array_push($kmean[$index]['Cluster'], $t); } // Emergency exit. $limit--; } // Now that this set has been organized update them in database. foreach ($kmean as $k) { foreach ($k['Cluster'] as $index => $t) { $t->Order = $index + 1; $t->VehicleId = $k['VehicleId']; print_r($t); $t->save(); } } } else { // TODO: This is here to improve transport selection when there is very little to do. echo 'less transport requests'; } } } }