Example #1
0
 public static function DriverResponse($driver)
 {
     $driver_id = $driver->id;
     $count = VehicleDriverOwner::where('vehicle_driver_owners_driver_id', $driver_id)->count();
     if ($count) {
         $driver_vehicle_owner_id = VehicleDriverOwner::where('vehicle_driver_owners_driver_id', $driver_id)->first()->vehicle_driver_owners_vehicle_id;
         if (isset($driver_vehicle_owner_id)) {
             $vehicle_data = Vehicle::where('id', $driver_vehicle_owner_id)->first();
             if (empty($vehicle_data)) {
                 $owner_data = array();
             } else {
                 $owner_data = Helpers::search_data($vehicle_data, $driver_id);
             }
         } else {
             $owner_data = array();
         }
     } else {
         $owner_data = array();
     }
     //check if user is authorised
     $driver_vehicle_auth_count = VehicleDriverAuthorized::where('vehicle_driver_authorized_driver_id', $driver_id)->count();
     if ($driver_vehicle_auth_count) {
         //if found return data
         $driver_vehicle_auth_id = VehicleDriverAuthorized::where('vehicle_driver_authorized_driver_id', $driver_id)->first()->vehicle_driver_authorized_vehicle_id;
         if (!empty($driver_vehicle_auth_id)) {
             $vehicle_data = Vehicle::where('id', $driver_vehicle_auth_id)->first();
             if (empty($vehicle_data)) {
                 $auth_data = array();
             } else {
                 $auth_data = Helpers::search_data($vehicle_data, $driver_id);
             }
         } else {
             $auth_data = array();
         }
     } else {
         $auth_data = array();
     }
     $data = array('owners' => $owner_data, 'auth' => $auth_data);
     echo Helpers::response_data('200', 'Success', $data);
 }
Example #2
0
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id)
 {
     $vehicle = Vehicle::find($id);
     // LOGS
     $oldVehiclesStatus = $vehicle->vehicles_status;
     $newVehiclesStatus = Input::get('vehicles_status');
     $oldvehicleColor = $vehicle->color->id;
     $newVehicleColor = Input::get('vehicles_color');
     $oldVehicleUseType = $vehicle->vehicles_use_type;
     $newVehicleUseType = Input::get('vehicles_use_type');
     $vehicles_class = Input::get('vehicles_class');
     $oldVehiclesRegistrationNo = $vehicle->registration->registrations_no;
     $newVehiclesRegistrationNo = Input::get('registrations_no');
     $oldOdometer = $vehicle->vehicles_odometer;
     $newOdometer = Input::get('vehicles_odometer');
     // IF VEHICLE STATUS IS CHANGED, ADD LOG
     if ($oldVehiclesStatus != $newVehiclesStatus) {
         LogVehicleStatus::create(['log_vehicle_status_vehicle_id' => $id, 'log_vehicle_status_vehicle_status_id' => $newVehiclesStatus]);
     }
     // IF VEHICLE COLOR IS CHANGED, ADD LOG
     if ($oldvehicleColor != $newVehicleColor) {
         LogVehicleColor::create(['log_vehicle_color_vehicle_id' => $id, 'log_vehicle_color_color_id' => $newVehicleColor]);
     }
     // IF VEHICLE USE TYPE IS CHANGED, TAKE LOG (ALSO TAKING LOG OF ODOMETER AND INCREMENT SEQUENCE)
     if ($oldVehicleUseType != $newVehicleUseType) {
         LogVehicleUseType::create(['log_vehicle_use_type_vehicle_id' => $id, 'log_vehicle_use_type_use_type' => $newVehicleUseType, 'log_vehicle_use_type_registration_no' => $newVehiclesRegistrationNo]);
         // INCREMENTING SEQUENCE
         $vehicles_use_type_table_name = VehicleUseType::find($newVehicleUseType)->vehicle_use_types_table_name;
         $vehicles_sequence_col_name = $vehicles_use_type_table_name . '_sequence';
         $currentSequence = DB::table($vehicles_use_type_table_name)->find($vehicles_class)->{$vehicles_sequence_col_name};
         $newSequence = $currentSequence + 1;
         DB::table($vehicles_use_type_table_name)->where('id', $vehicles_class)->update([$vehicles_sequence_col_name => $newSequence]);
         // TAKING LOG OF ODOMETER
         $vehicles_odometer = Input::get('vehicles_odometer');
         LogVehicleOdometer::create(['log_vehicle_odometer_vehicle_id' => $id, 'log_vehicle_odometer_old_odometer' => $vehicles_odometer, 'log_vehicle_odometer_status' => 2]);
     } elseif ($oldOdometer != $newOdometer) {
         LogVehicleOdometer::create(['log_vehicle_odometer_vehicle_id' => $id, 'log_vehicle_odometer_old_odometer' => $newOdometer, 'log_vehicle_odometer_status' => 4]);
     }
     // BEGIN UPDATE VEHICLE DATA
     $vehicle = Input::only(['vehicles_use_type', 'vehicles_class', 'vehicles_color', 'vehicles_status', 'vehicles_odometer']);
     Vehicle::where('id', $id)->update($vehicle);
     // END UPDATE VEHICLE DATA
     // BEGIN OWNERS
     //OLD OWNERS
     $old_owners = Vehicle::find($id)->owners->lists('id');
     //UPDATED OLD OWNERS
     $updated_old_owners_index = Input::get('old_owners_index');
     $updated_owners_index_arr = array_filter(explode(',', $updated_old_owners_index));
     //DELETED OWNERS
     $deleted_owners = array_diff($old_owners, $updated_owners_index_arr);
     // DELETE EACH DELETED OWNER FROM DATABASE
     foreach ($deleted_owners as $deleted_owner_id) {
         VehicleDriverOwner::where('vehicle_driver_owners_vehicle_id', $id)->where('vehicle_driver_owners_driver_id', $deleted_owner_id)->delete();
     }
     //RETAINED OWNERS
     $retained_owners = array_intersect($old_owners, $updated_owners_index_arr);
     // UPDATE RETAINED OWNERS
     foreach ($retained_owners as $retained_owner) {
         $retained_owner_info['drivers_fname'] = Input::get('drivers_fname' . $retained_owner);
         $retained_owner_info['drivers_mname'] = Input::get('drivers_mname' . $retained_owner);
         $retained_owner_info['drivers_lname'] = Input::get('drivers_lname' . $retained_owner);
         $retained_owner_info['drivers_nin'] = Input::get('drivers_nin' . $retained_owner);
         $retained_owner_info['drivers_license_no'] = Input::get('drivers_license_no' . $retained_owner);
         $retained_owner_info['drivers_street'] = Input::get('drivers_street' . $retained_owner);
         $retained_owner_info['drivers_city'] = Input::get('drivers_city' . $retained_owner);
         $retained_owner_info['drivers_po_box'] = Input::get('drivers_po_box' . $retained_owner);
         $retained_owner_info['drivers_country'] = Input::get('drivers_country' . $retained_owner);
         $retained_owner_info['drivers_cell1'] = Input::get('drivers_cell1' . $retained_owner);
         $retained_owner_info['drivers_cell2'] = Input::get('drivers_cell2' . $retained_owner);
         $retained_owner_info['drivers_email'] = Input::get('drivers_email' . $retained_owner);
         $retained_owner_info['drivers_driving_status'] = Input::get('drivers_driving_status' . $retained_owner);
         Driver::find($retained_owner)->update($retained_owner_info);
     }
     // END OWNERS
     //BEGIN AUTHORIZED DRIVERS
     $old_authorized = Vehicle::find($id)->authorizedDrivers->lists('id');
     //UPDATED OLD AUTHORIZED DRIVERS
     $updated_old_authorized_index = Input::get('old_authorized_index');
     $updated_authorized_index_arr = array_filter(explode(',', $updated_old_authorized_index));
     //DELETED AUTHORIZED DRIVERS
     $deleted_authorized = array_diff($old_authorized, $updated_authorized_index_arr);
     //DELETE EACH DELETED AUTHORIZED DRIVER FROM DATABASE
     foreach ($deleted_authorized as $deleted_authorized_id) {
         VehicleDriverAuthorized::where('vehicle_driver_authorized_vehicle_id', $id)->where('vehicle_driver_authorized_driver_id', $deleted_authorized_id)->delete();
     }
     //RETAINED AUTHORIZED DRIVERS
     $retained_authorized = array_intersect($old_authorized, $updated_authorized_index_arr);
     // UPDATE RETAINED AUTHORIZED DRIVERS
     foreach ($retained_authorized as $retained_authorize) {
         $retained_authorized_info['drivers_fname'] = Input::get('authorized_fname' . $retained_authorize);
         $retained_authorized_info['drivers_mname'] = Input::get('authorized_mname' . $retained_authorize);
         $retained_authorized_info['drivers_lname'] = Input::get('authorized_lname' . $retained_authorize);
         $retained_authorized_info['drivers_nin'] = Input::get('authorized_nin' . $retained_authorize);
         $retained_authorized_info['drivers_license_no'] = Input::get('authorized_license_no' . $retained_authorize);
         $retained_authorized_info['drivers_street'] = Input::get('authorized_street' . $retained_authorize);
         $retained_authorized_info['drivers_city'] = Input::get('authorized_city' . $retained_authorize);
         $retained_authorized_info['drivers_po_box'] = Input::get('authorized_po_box' . $retained_authorize);
         $retained_authorized_info['drivers_country'] = Input::get('authorized_country' . $retained_authorize);
         $retained_authorized_info['drivers_cell1'] = Input::get('authorized_cell1' . $retained_authorize);
         $retained_authorized_info['drivers_cell2'] = Input::get('authorized_cell2' . $retained_authorize);
         $retained_authorized_info['drivers_email'] = Input::get('authorized_email' . $retained_authorize);
         $retained_authorized_info['drivers_driving_status'] = Input::get('authorized_driving_status' . $retained_authorize);
         Driver::find($retained_authorize)->update($retained_authorized_info);
     }
     //END AUTHORIZED DRIVERS
     // BEGIN NEW OWNERS
     $owners_index = Input::get('owners_index');
     $owners_index_arr = array_filter(explode(',', $owners_index));
     // LOOP THROUGH EACH OWNER AND ASSOCIATE WITH DRIVERS
     foreach ($owners_index_arr as $owners_index_id) {
         $drivers_license_no = Input::get('drivers_license_no' . $owners_index_id);
         if (trim($drivers_license_no) != "") {
             $drivers_status = Driver::where('drivers_license_no', $drivers_license_no)->first();
             // IF DRIVER DOESN'T EXISTS, CREATE DRIVER
             if ($drivers_status == NULL) {
                 $owner_info['drivers_fname'] = Input::get('drivers_fname' . $owners_index_id);
                 $owner_info['drivers_mname'] = Input::get('drivers_mname' . $owners_index_id);
                 $owner_info['drivers_lname'] = Input::get('drivers_lname' . $owners_index_id);
                 $owner_info['drivers_nin'] = Input::get('drivers_nin' . $owners_index_id);
                 $owner_info['drivers_license_no'] = Input::get('drivers_license_no' . $owners_index_id);
                 $owner_info['drivers_street'] = Input::get('drivers_street' . $owners_index_id);
                 $owner_info['drivers_city'] = Input::get('drivers_city' . $owners_index_id);
                 $owner_info['drivers_po_box'] = Input::get('drivers_po_box' . $owners_index_id);
                 $owner_info['drivers_country'] = Input::get('drivers_country' . $owners_index_id);
                 $owner_info['drivers_cell1'] = Input::get('drivers_cell1' . $owners_index_id);
                 $owner_info['drivers_cell2'] = Input::get('drivers_cell2' . $owners_index_id);
                 $owner_info['drivers_email'] = Input::get('drivers_email' . $owners_index_id);
                 $owner_info['drivers_driving_status'] = Input::get('drivers_driving_status' . $owners_index_id);
                 $insert_owner_into_driver_table = Driver::create($owner_info);
                 $owner_ids[] = $insert_owner_into_driver_table->id;
             } else {
                 $owner_ids[] = Driver::where('drivers_license_no', $drivers_license_no)->first()->id;
             }
         }
     }
     // LINK VEHICLE WITH NEW OWNERS
     if (!empty($owner_ids)) {
         foreach ($owner_ids as $owner_ids_fet) {
             $owner_ids_fet = (int) $owner_ids_fet;
             //VehicleDriverOwner::create(['vehicle_driver_owners_vehicle_id' => $id, 'vehicle_driver_owners_driver_id' => $owner_ids_fet]);
             Vehicle::find($id)->owners()->attach($owner_ids_fet);
         }
     }
     // END NEW OWNERS
     //BEGIN NEW AUTHORIZED DRIVERS
     $authorized_index = Input::get('authorized_index');
     // IF AUTHORIZED DRIVERS EXIST, ASSOCIATE WITH DRIVERS.(CHECKING USING LICENSE)
     if ($authorized_index != "") {
         $authorized_index_arr = explode(',', $authorized_index);
         foreach ($authorized_index_arr as $authorized_index_id) {
             $authorized_license_no = Input::get('authorized_license_no' . $authorized_index_id);
             $authorized_status = Driver::where('drivers_license_no', $authorized_license_no)->first();
             //IF DRIVER DOESN'T EXISTS, CREATE DRIVER
             if ($authorized_status == NULL) {
                 $authorized_info['drivers_fname'] = Input::get('authorized_fname' . $authorized_index_id);
                 $authorized_info['drivers_mname'] = Input::get('authorized_mname' . $authorized_index_id);
                 $authorized_info['drivers_lname'] = Input::get('authorized_lname' . $authorized_index_id);
                 $authorized_info['drivers_nin'] = Input::get('authorized_nin' . $authorized_index_id);
                 $authorized_info['drivers_license_no'] = Input::get('authorized_license_no' . $authorized_index_id);
                 $authorized_info['drivers_street'] = Input::get('authorized_street' . $authorized_index_id);
                 $authorized_info['drivers_city'] = Input::get('authorized_city' . $authorized_index_id);
                 $authorized_info['drivers_po_box'] = Input::get('authorized_po_box' . $authorized_index_id);
                 $authorized_info['drivers_country'] = Input::get('authorized_country' . $authorized_index_id);
                 $authorized_info['drivers_cell1'] = Input::get('authorized_cell1' . $authorized_index_id);
                 $authorized_info['drivers_cell2'] = Input::get('authorized_cell2' . $authorized_index_id);
                 $authorized_info['drivers_email'] = Input::get('authorized_email' . $authorized_index_id);
                 $authorized_info['drivers_driving_status'] = Input::get('authorized_driving_status' . $authorized_index_id);
                 $insert_authorized_into_driver_table = Driver::create($authorized_info);
                 $authorized_ids[] = $insert_authorized_into_driver_table->id;
             } else {
                 $authorized_ids[] = Driver::where('drivers_license_no', $authorized_license_no)->first()->id;
             }
         }
         // LINK VEHICLE WITH AUTHORIZED DRIVERS
         foreach ($authorized_ids as $authorized_ids_fet) {
             $authorized_ids_fet = (int) $authorized_ids_fet;
             //VehicleDriverAuthorized::create(['vehicle_driver_authorized_vehicle_id' => $id, 'vehicle_driver_authorized_driver_id' => $authorized_ids_fet]);
             Vehicle::find($id)->authorizedDrivers()->attach($authorized_ids_fet);
         }
     }
     // END NEW AUTHORIZED DRIVERS
     // BEGIN INSURANCE
     if (Input::get('insurances_company') != "") {
         $insurance_arr = Input::only('insurances_company', 'insurances_policy_no', 'insurances_exp_date', 'insurances_agent_fname', 'insurances_agent_lname', 'insurances_agent_cell', 'insurances_agent_city', 'insurances_agent_email');
         if (Insurance::where('insurances_vehicle_id', $id)->first()->id) {
             Insurance::where('insurances_vehicle_id', $id)->update($insurance_arr);
         } else {
             //$insurance_arr['insurances_vehicle_id'] = $id;
             //Insurance::create($insurance_arr);
             Vehicle::find($id)->insurance()->save($insurance_arr);
         }
     }
     // END INSURANCE
     // BEGIN LIEN HOLDER
     if (Input::get('lien_index')) {
         $lien_arr = Input::only('liens_collateral_id', 'liens_collateral_value', 'liens_bank_name', 'liens_bank_branch', 'liens_bank_street', 'liens_bank_city');
         if (Lien::where('liens_vehicle_id', $id)->first()->id) {
             Lien::where('liens_vehicle_id', $id)->update($lien_arr);
         } else {
             //$lien_arr['liens_vehicle_id'] = $id;
             //Lien::create($lien_arr);
             Vehicle::find($id)->lien()->save($lien_arr);
         }
     }
     // END LIEN HOLDER
     // BEGIN OLD FILES
     $old_documents_index = Vehicle::find($id)->documents->lists('id');
     $documents_index = explode(',', Input::get('documents_index'));
     $deleted_documents = array_diff($old_documents_index, $documents_index);
     if (!empty($deleted_documents)) {
         foreach ($deleted_documents as $deleted_document) {
             Document::find($deleted_document)->delete();
         }
     }
     // END OLD FILES
     // BEGIN NEW FILES
     $files_col = Input::file();
     $files_count = count(array_filter($files_col['files']));
     // IF FILES EXISTS
     if ($files_count) {
         foreach ($files_col['files'] as $files) {
             $filename_ar = explode('.', $files->getClientOriginalName());
             $document_ext = end($filename_ar);
             $path = public_path() . '/vehicledata/documents';
             // GENERATE RANDOM FILE NAME AND ADD EXTENSION
             $randName = md5(rand() * time());
             $document_name = $randName . '.' . $document_ext;
             // MOVE FILE AND ENTRY INTO DATABASE
             if ($files->move($path, $document_name)) {
                 //Document::create(['documents_vehicle_id' => $id, 'documents_name' => $document_name]);
                 Vehicle::find($id)->documents()->save(new Document(['documents_name' => $document_name]));
             }
         }
     }
     // END NEW FILES
     // BEGIN REGISTRATION
     if (isset($id) && $id != "") {
         $registrations_vehicle_id = $id;
         $registrations_no = Input::get('registrations_no');
         $registrations_licence_plate_no = Input::get('registrations_licence_plate_no');
         if ($registrations_no != "" && $registrations_licence_plate_no != "" && $registrations_vehicle_id != "") {
             $vehicles_use_type = Input::get('vehicles_use_type');
             $vehicles_class = Input::get('vehicles_class');
             if ($vehicles_use_type != "" && $vehicles_class != "") {
                 $vehicles_use_type_table_name = VehicleUseType::find($vehicles_use_type)->vehicle_use_types_table_name;
                 //REGISTRATION FEES
                 $vehicles_class_col_name = $vehicles_use_type_table_name . '_total';
                 $registrations_fees = DB::table($vehicles_use_type_table_name)->find($vehicles_class)->{$vehicles_class_col_name};
                 $registration_ar = compact('registrations_vehicle_id', 'registrations_no', 'registrations_licence_plate_no', 'registrations_fees');
                 Registration::where('registrations_vehicle_id', $id)->update($registration_ar);
                 return Redirect::route('vehicle.print', [$registrations_vehicle_id]);
             }
         }
     } else {
         echo "Nothing is saved because fields are not filled";
         die;
     }
     // END REGISTRATION
 }