HEX
Server: Apache/2.4.41 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.40
System: Linux ip-172-31-40-18 4.14.146-93.123.amzn1.x86_64 #1 SMP Tue Sep 24 00:45:23 UTC 2019 x86_64
User: apache (48)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: /var/www/html/taxicamera/old/applicationold/controllers/Api.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Api extends CI_Controller
{
  var $arr;
  var $obj;

  function __construct()
  {
    parent::__construct();
    $this->load->model('mapi');
    $this->arr = array();
    $this->obj = new stdClass();
    $this->http_methods = array('POST', 'GET', 'PUT', 'DELETE');
    $this->logo = base_url() . 'public/images/logo_new.jpg';
    //$this->load->library('notification');
  }

  private function displayOutput($response)
  {
    header('Content-Type: application/json');
    echo json_encode($response);
    exit(0);
  }

  // register
  public function signup()
  {

    //$ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (!empty($this->input->post())) {

        // if (empty($ap['access_token_key'])) {
        //   $response = array('status' => array('error_code' => 3, 'message' => 'Access Token key is required'), 'result' => array('details' => $this->obj));
        //   $this->displayOutput($response);
        // }
        if (empty($this->input->post('first_name'))) {
          $response = array('status'  => 1, 'message' => 'First Name field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('last_name'))) {
          $response = array('status' => 1, 'message' => 'Last Name field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('email'))) {
          $response = array('status'  => 1, 'message' => 'Email field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (!empty($this->input->post('email'))) {

          $result = $this->mapi->checkUserRegistered(array('email' => $_POST['email']));
          if (!empty($result)) {
            $response = array('status' => 1, 'message' => 'Email Already registered', 'driver_details' => $this->obj);
            $this->displayOutput($response);
          }
        }
        if (empty($_POST['dc_no'])) {
          $response = array('status' => 1, 'message' => 'DC Number field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (!empty($this->input->post('dc_no'))) {

          $result = $this->mapi->checkUserRegistered(array('dc_no' => $_POST['dc_no']));
          if (!empty($result)) {
            $response = array('status' => 1, 'message' => 'DC Number Already registered', 'driver_details' => $this->obj);
            $this->displayOutput($response);
          }
        }
        if (empty($this->input->post('dr_licence_no'))) {
          $response = array('status' => 1, 'message' => 'Licence Number field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }

        $user_data['role_id'] = 2;
        $user_data['dc_no'] = $this->input->post('dc_no');
        $user_data['email'] = $this->input->post('email');
        $user_data['password'] = '';
        $user_data['created_by'] = 0;
        $user_data['is_active'] = '0';
        $user_data['status'] = '0';
        $user_data['created_ts'] = date('Y-m-d H:i:s');

        $user_id = $this->mapi->insert('master_user', $user_data);

        if ($user_id) {

          $profile_data['user_id'] = $user_id;
          $profile_data['first_name'] = $this->input->post('first_name');
          $profile_data['middle_name'] = $this->input->post('middle_name');
          $profile_data['last_name'] = $this->input->post('last_name');
          $profile_data['dob'] = $this->input->post('dob');
          $profile_data['mobile'] = $this->input->post('mobile');
          $profile_data['landline_no'] = $this->input->post('landline_no');
          $profile_data['abn'] = $this->input->post('abn');
          $profile_data['flat_no'] = $this->input->post('flat_no');
          $profile_data['street_no'] = $this->input->post('street_no');
          $profile_data['street_name'] = $this->input->post('street_name');
          $profile_data['suburb'] = $this->input->post('suburb');
          $profile_data['state'] = $this->input->post('state');
          $profile_data['pin'] = $this->input->post('pin');
          $profile_data['bank_name'] = $this->input->post('bank_name');
          $profile_data['bsb'] = $this->input->post('bsb');
          $profile_data['account_no'] = $this->input->post('account_no');
          $profile_data['dr_driver_type'] = $this->input->post('dr_driver_type');
          $profile_data['dr_licence_no'] = $this->input->post('dr_licence_no');


          if ($this->input->post('dr_licence_expiry')) {
            $profile_data['dr_licence_expiry'] =  date('Y-m-d', strtotime(str_replace('/', '-', $this->input->post('dr_licence_expiry'))));
          }
          if ($this->input->post('dr_dc_expiry')) {
            $profile_data['dr_dc_expiry'] =  date('Y-m-d', strtotime(str_replace('/', '-', $this->input->post('dr_dc_expiry'))));
          }

          $profile_data['dr_commission_id'] = '';
          $profile_data['dr_rental'] = '';
          $profile_data['created_by'] = $user_id;
          $profile_data['created_ts'] = date('Y-m-d H:i:s');

          //echo '<pre>'; print_r($_FILES['profile_photo']);die;
          if (isset($_FILES['profile_photo']['name'])) {

            $path = './public/upload_image/profile_photo/';
            $upload_file = $this->single_image_upload($path, $_FILES['profile_photo'], 'profile_photo');

            if ($upload_file['status'] == 1) {
              $profile_data['profile_photo'] = $upload_file['result'];
            }
            // else{
            //   $response = array('status' => 1, 'message' => $upload_file['result'], 'driver_details' => $this->obj);
            //   $this->displayOutput($response);
            // }
          }

          $profile_id = $this->mapi->insert('user_profile', $profile_data);

          if (isset($_FILES['driver_certificate']['name'][0])) {

            $path = './public/upload_image/driver_certificate/';
            $upload_file = $this->upload_Product_images($path, $_FILES['driver_certificate'], $user_id, 'driver_certificate');
          }

          if (isset($_FILES['driving_license']['name'][0])) {

            $path = './public/upload_image/driving_license/';
            $upload_file = $this->upload_Product_images($path, $_FILES['driving_license'], $user_id, 'driving_license');
          }

          if (isset($_FILES['login_card']['name'][0])) {

            $path = './public/upload_image/login_card/';
            $upload_file = $this->upload_Product_images($path, $_FILES['login_card'], $user_id, 'login_card');
          }

          if (isset($_FILES['paper_dc']['name'][0])) {

            $path = './public/upload_image/paper_dc/';
            $upload_file = $this->upload_Product_images($path, $_FILES['paper_dc'], $user_id, 'paper_dc');
          }

          if (isset($_FILES['signature']['name'][0])) {

            $path = './public/upload_image/signature/';
            $upload_file = $this->upload_Product_images($path, $_FILES['signature'], $user_id, 'signature');
          }
        } else {
          $response = array('status' => 1, 'message' => 'Oops!Something went wrong...', 'driver_details' => $this->obj);
        }
        $response = array('status' => 0, 'message' => 'Registration successful,an email will be send to you with your login credential after admin approval', 'driver_details' => $this->obj);
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'driver_details' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'driver_details' => $this->obj);
    }
    $this->displayOutput($response);
  }

  //Login
  public function login()
  {

    $ap = json_decode(file_get_contents('php://input'), true);

    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }

        if (empty($ap['dc_no'])) {
          $response = array('status' =>  1, 'message' => 'DC Number field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }

        if (empty($ap['password'])) {
          $response = array('status' => 1, 'message' => 'Password field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }

        $ap['password'] = md5($ap['password']);
        $check_user_condition = array('dc_no' => $ap['dc_no'], 'password' => $ap['password'], 'role_id' => '2');
        $userdetails = $this->mapi->getRow('master_user', $check_user_condition);


        if (empty($userdetails)) {

          $response = array('status' =>  1, 'message' => 'Invalid username or password', 'driver_details' => $this->obj);
        } elseif ($userdetails['is_active'] == 0) {

          $response = array('status' =>  1, 'message' => 'Account Deactivated', 'driver_details' => $this->obj);
        } else {

          $condition = array('user_id' => $userdetails['user_id']);
          $login_data['login_status'] = 1;
          $login_data['date_of_last_logged_in'] = date('Y-m-d H:i:s');
          $update_login_status = $this->mapi->update('master_user', $condition, $login_data);
          if ($update_login_status) {
            $response = array('status' => 0, 'message' => 'Login Successfully', 'driver_details' => $userdetails);


            $api_token_details = $this->mapi->getRow('api_token', $condition);
            $profile_details = $this->mapi->getRow('user_profile', $condition);
            $userdetails['full_name'] = trim(trim($profile_details['first_name']) . ' ' . trim($profile_details['middle_name']) . ' ' . trim($profile_details['last_name']));

            $userdetails['dr_driver_type'] = $profile_details['dr_driver_type'];

            if ($profile_details['profile_photo']) {
              $userdetails['profile_photo'] = '/public/upload_image/profile_photo/' . $profile_details['profile_photo'];
            } else {
              $userdetails['profile_photo'] = $profile_details['profile_photo'];
            }

            //--------------------------------------------------Shift Details-------------------------------------------------------------//
            $shift_details = array();

            $ongoing_shift_details = $this->mapi->get_ongoing_shift_details($userdetails['user_id']);

            if (!empty($ongoing_shift_details)) {

              if ($ongoing_shift_details['shift_status'] == 1) {
                $shift_details['status_code'] = 2;
                $shift_details['status'] = 'Ongoing Shift';
                $shift_details['shift_id'] = $ongoing_shift_details['shift_id'];
                $shift_details['shift_no'] = $ongoing_shift_details['shift_no'];
                $shift_details['car_id'] = $ongoing_shift_details['car_id'];
                $shift_details['car_no'] = $ongoing_shift_details['registration_no'];
              } else {
                
                $shift_details['status_code'] = 0;
                $shift_details['status'] = 'Shift Ended';
                $shift_details['shift_id'] = '';
                $shift_details['shift_no'] = '';
                $shift_details['car_id'] = '';
                $shift_details['car_no'] = '';

                //$today_shift_details = $this->mapi->check_today_shift_availibility_after_end_shift($userdetails['user_id']);
                $today_shift_details = $this->mapi->get_today_shift_details($userdetails['user_id']);
                
                  if (!empty($today_shift_details)) { 
                    $shift_details['status_code'] = 1;
                    $shift_details['status'] = 'Shift Available Today';
                    $shift_details['shift_id'] = '';
                    $shift_details['shift_no'] = '';
                    $shift_details['car_id'] = $today_shift_details['car_id'];
                    $shift_details['car_no'] = $today_shift_details['registration_no'];
                  } 
              }
            } else {

              $today_shift_details = $this->mapi->get_today_shift_details($userdetails['user_id']);
              if (empty($today_shift_details)) {
                $shift_details['status_code'] = 0;
                $shift_details['status'] = 'No Shift Available Today';
                $shift_details['shift_id'] = '';
                $shift_details['shift_no'] = '';
                $shift_details['car_id'] = '';
                $shift_details['car_no'] = '';
              } else {

                $shift_details['status_code'] = 1;
                $shift_details['status'] = 'Shift Available Today';
                $shift_details['shift_id'] = '';
                $shift_details['shift_no'] = '';
                $shift_details['car_id'] = $today_shift_details['car_id'];
                $shift_details['car_no'] = $today_shift_details['registration_no'];
                //print_r($today_shift_details);die;
              }
            }
            //---------------------------------------------------END---------------------------------------------------------------//

            if (empty($api_token_details)) {

              $api_token_data['user_id'] = $userdetails['user_id'];
              $api_token_data['device_type'] = $ap['device_type'];
              $api_token_data['token_key'] = md5(mt_rand() . '_' . $userdetails['user_id']);
              $api_token_data['date_of_creation'] = date('Y-m-d H:i:s');
              $api_token_data['session_start_time'] = date('Y-m-d H:i:s');
              $api_token_data['session_end_time'] = '';

              $insert_data = $this->mapi->insert('api_token', $api_token_data);
              $api_token_details = $this->mapi->getRow('api_token', $condition);
              if ($insert_data) {
                $response = array('status' => 0, 'message' => 'Login Successfully', 'driver_details' => $userdetails, 'api_token_details' => $api_token_details, 'shift_details' => $shift_details);
              } else {
                $response = array('status' => 1, 'message' => 'Unable to generate access token', 'driver_details' => $userdetails, 'api_token_details' => $api_token_details, 'shift_details' => $shift_details);
              }
            } else {
              $condition_api_token = array('user_id' => $userdetails['user_id']);
              $api_token_data['token_key'] = md5(mt_rand() . '_' . $userdetails['user_id']);
              $api_token_data['device_type'] = $ap['device_type'];

              $update_data = $this->mapi->update('api_token', $condition_api_token, $api_token_data);
              $api_token_details = $this->mapi->getRow('api_token', $condition);

              if ($update_data) {
                $response = array('status' => 0, 'message' => 'Login Successfully', 'driver_details' => $userdetails, 'api_token_details' => $api_token_details, 'shift_details' => $shift_details);
              } else {
                $response = array('status' =>  1, 'message' => 'Unable to update access token', 'driver_details' => $userdetails, 'api_token_details' => $api_token_details, 'shift_details' => $shift_details);
              }
            }
          } else {
            $response = array('status' => 1, 'message' => 'Oops!something went wrong...', 'driver_details' => $this->obj);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'driver_details' => $this->obj);
      }
    } else {
      $response = array('status' =>  1, 'message' => 'Wrong http method type', 'driver_details' => $this->obj);
    }
    $this->displayOutput($response);
  }

  public function get_docket_list()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'docket_list' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'docket_list' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'docket_list' => $this->obj);
          $this->displayOutput($response);
        } else {
          $docket_list = $this->mapi->getDetails('master_docket', array('is_active' => 1));
          if (!empty($docket_list)) {
            $response = array('status' => 0, 'message' => 'Docket List Fetched Successfully', 'docket_list' => $docket_list);
          } else {
            $response = array('status' => 0, 'message' => 'No Data Found', 'docket_list' => $this->obj);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'docket_list' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'docket_list' => $this->obj);
    }
    $this->displayOutput($response);
  }

  public function get_breakdown_number()
  {
    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'breakdown_data' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'breakdown_data' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'breakdown_data' => $this->obj);
          $this->displayOutput($response);
        } else {
          $breakdown_data = $this->mapi->getRow('master_breakdown_alert', array());
          if (!empty($breakdown_data)) {
            $response = array('status' => 0, 'message' => 'Breakdown Data Fetched Successfully', 'breakdown_data' => $breakdown_data);
          } else {
            $response = array('status' => 0, 'message' => 'No Data Found', 'breakdown_data' => $this->obj);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'breakdown_data' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'breakdown_data' => $this->obj);
    }
    $this->displayOutput($response);
  }

  public function updateProfileData()
  {

    //$ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (!empty($this->input->post())) {

        if (empty($this->input->post('token_key'))) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('device_type'))) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }

        if (empty($this->input->post('user_id'))) {
          $response = array('status'  => 1, 'message' => 'User ID is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('first_name'))) {
          $response = array('status'  => 1, 'message' => 'First Name field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('last_name'))) {
          $response = array('status' => 1, 'message' => 'Last Name field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('email'))) {
          $response = array('status'  => 1, 'message' => 'Email field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (!empty($this->input->post('email'))) {

          $result = $this->mapi->checkUserRegistered(array('email' => $_POST['email'], 'user_id !=' => $_POST['user_id']));
          if (!empty($result)) {
            $response = array('status' => 1, 'message' => 'Email Already registered', 'driver_details' => $this->obj);
            $this->displayOutput($response);
          }
        }
        if (empty($_POST['dc_no'])) {
          $response = array('status' => 1, 'message' => 'DC Number field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (!empty($this->input->post('dc_no'))) {

          $result = $this->mapi->checkUserRegistered(array('dc_no' => $_POST['dc_no'], 'user_id !=' => $_POST['user_id']));
          if (!empty($result)) {
            $response = array('status' => 1, 'message' => 'DC Number Already registered', 'driver_details' => $this->obj);
            $this->displayOutput($response);
          }
        }
        if (empty($this->input->post('dr_licence_no'))) {
          $response = array('status' => 1, 'message' => 'Licence Number field is required', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($this->input->post('token_key'), $this->input->post('device_type'));

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'driver_details' => $this->obj);
          $this->displayOutput($response);
        } else {

          $user_id = $this->input->post('user_id');
          $condition_update = array('user_id' => $user_id);

          $user_data['role_id'] = 2;
          $user_data['dc_no'] = $this->input->post('dc_no');
          $user_data['email'] = $this->input->post('email');
          $user_data['updated_by'] = $user_id;
          $user_data['updated_ts'] = date('Y-m-d H:i:s');

          $this->mapi->update('master_user', $condition_update, $user_data);

          if ($user_id) {

            $profile_data['user_id'] = $user_id;
            $profile_data['first_name'] = $this->input->post('first_name');
            $profile_data['middle_name'] = $this->input->post('middle_name');
            $profile_data['last_name'] = $this->input->post('last_name');
            $profile_data['dob'] = $this->input->post('dob');
            $profile_data['mobile'] = $this->input->post('mobile');
            $profile_data['landline_no'] = $this->input->post('landline_no');
            $profile_data['abn'] = $this->input->post('abn');
            $profile_data['flat_no'] = $this->input->post('flat_no');
            $profile_data['street_no'] = $this->input->post('street_no');
            $profile_data['street_name'] = $this->input->post('street_name');
            $profile_data['suburb'] = $this->input->post('suburb');
            $profile_data['state'] = $this->input->post('state');
            $profile_data['pin'] = $this->input->post('pin');
            $profile_data['bank_name'] = $this->input->post('bank_name');
            $profile_data['bsb'] = $this->input->post('bsb');
            $profile_data['account_no'] = $this->input->post('account_no');
            $profile_data['dr_driver_type'] = $this->input->post('dr_driver_type');
            $profile_data['dr_licence_no'] = $this->input->post('dr_licence_no');


            if ($this->input->post('dr_licence_expiry')) {
              $profile_data['dr_licence_expiry'] =  date('Y-m-d', strtotime(str_replace('/', '-', $this->input->post('dr_licence_expiry'))));
            }
            if ($this->input->post('dr_dc_expiry')) {
              $profile_data['dr_dc_expiry'] =  date('Y-m-d', strtotime(str_replace('/', '-', $this->input->post('dr_dc_expiry'))));
            }

            $profile_data['updated_by'] = $user_id;
            $profile_data['updated_ts'] = date('Y-m-d H:i:s');



            if (isset($_FILES['profile_photo']['name'])) {

              $path = './public/upload_image/profile_photo/';
              $upload_file = $this->single_image_upload($path, $_FILES['profile_photo'], 'profile_photo');

              if ($upload_file['status'] == 1) {
                $profile_data['profile_photo'] = $upload_file['result'];
              }
            }

            $this->mapi->update('user_profile', $condition_update, $profile_data);

            if (isset($_FILES['driver_certificate']['name'][0])) {

              $path = './public/upload_image/driver_certificate/';
              $upload_file = $this->upload_Product_images($path, $_FILES['driver_certificate'], $user_id, 'driver_certificate');
            }

            if (isset($_FILES['driving_license']['name'][0])) {

              $path = './public/upload_image/driving_license/';
              $upload_file = $this->upload_Product_images($path, $_FILES['driving_license'], $user_id, 'driving_license');
            }

            if (isset($_FILES['login_card']['name'][0])) {

              $path = './public/upload_image/login_card/';
              $upload_file = $this->upload_Product_images($path, $_FILES['login_card'], $user_id, 'login_card');
            }

            if (isset($_FILES['paper_dc']['name'][0])) {

              $path = './public/upload_image/paper_dc/';
              $upload_file = $this->upload_Product_images($path, $_FILES['paper_dc'], $user_id, 'paper_dc');
            }

            if (isset($_FILES['signature']['name'][0])) {

              $path = './public/upload_image/signature/';
              $upload_file = $this->upload_Product_images($path, $_FILES['signature'], $user_id, 'signature');
            }

            $condition = array('user_id' => $this->input->post('user_id'));
            $user_details = $this->mapi->getRow('master_user', $condition);
            $profile_details = $this->mapi->getRow('user_profile', $condition);
            $driver_details = array();

            $driver_details['full_name'] = trim(trim($profile_details['first_name']) . ' ' . trim($profile_details['middle_name']) . ' ' . trim($profile_details['last_name']));
            if ($profile_details['profile_photo']) {
              $driver_details['profile_photo'] = '/public/upload_image/profile_photo/' . $profile_details['profile_photo'];
            } else {
              $driver_details['profile_photo'] = $profile_details['profile_photo'];
            }
            $response = array('status' => 0, 'message' => 'Updated successfully', 'driver_details' => $driver_details);
          } else {
            $response = array('status' => 1, 'message' => 'Oops!Something went wrong...', 'driver_details' => $this->obj);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'driver_details' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'driver_details' => $this->obj);
    }
    $this->displayOutput($response);
  }

  //Profile Details
  public function profileDetails()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'profile_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'profile_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['user_id'])) {
          $response = array('status' => 1, 'message' => 'User Id field is required', 'profile_details' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'profile_details' => $this->obj);
          $this->displayOutput($response);
        } else {

          $condition = array('user_id' => $ap['user_id']);
          $user_details = $this->mapi->getRow('master_user', $condition);
          $profile_details = $this->mapi->getRow('user_profile', $condition);

          if (!empty($user_details) && !empty($profile_details)) {

            $basic_info['user_id'] = $user_details['user_id'];
            $basic_info['role_id'] = $user_details['role_id'];
            $basic_info['email'] = $user_details['email'];
            $basic_info['first_name'] = $profile_details['first_name'];
            $basic_info['middle_name'] = $profile_details['middle_name'];
            $basic_info['last_name'] = $profile_details['last_name'];
            $basic_info['dob'] = $profile_details['dob'];
            $basic_info['mobile'] = $profile_details['mobile'];
            $basic_info['landline_no'] = $profile_details['landline_no'];
            $basic_info['abn'] = $profile_details['abn'];


            $address_info['flat_no'] = $profile_details['flat_no'];
            $address_info['street_no'] = $profile_details['street_no'];
            $address_info['street_name'] = $profile_details['street_name'];
            $address_info['suburb'] = $profile_details['suburb'];
            $address_info['state'] = $profile_details['state'];
            $address_info['pin'] = $profile_details['pin'];

            $bank_info['bank_name'] = $profile_details['bank_name'];
            $bank_info['bsb'] = $profile_details['bsb'];
            $bank_info['account_no'] = $profile_details['account_no'];

            $personal_info['dr_driver_type'] = $profile_details['dr_driver_type'];
            $personal_info['dr_licence_no'] = $profile_details['dr_licence_no'];
            $personal_info['dr_licence_expiry'] = ($profile_details['dr_licence_expiry']) ? date("d/m/Y", strtotime($profile_details['dr_licence_expiry'])) : '';
            $personal_info['dc_no'] = $user_details['dc_no'];
            $personal_info['dr_dc_expiry'] = ($profile_details['dr_dc_expiry']) ? date("d/m/Y", strtotime($profile_details['dr_dc_expiry'])) : '';

            if ($profile_details['profile_photo']) {
              $personal_info['profile_photo'] = '/public/upload_image/profile_photo/' . $profile_details['profile_photo'];
              $personal_info['profile_photo_flag'] = 1;
            } else {
              $personal_info['profile_photo'] = $profile_details['profile_photo'];
              $personal_info['profile_photo_flag'] = 0;
            }

            $certificate_docs_condition = array('user_id' => $ap['user_id'], 'document_type' => 'driver_certificate');
            $certificate_docs_details = $this->mapi->getRow('user_docs', $certificate_docs_condition);

            if (isset($certificate_docs_details['document_path'])) {
              $personal_info['driver_certificate'] = '/public/upload_image/driver_certificate/' . $certificate_docs_details['document_path'];
              $personal_info['driver_certificate_flag'] = 1;
            } else {
              $personal_info['driver_certificate'] = '';
              $personal_info['driver_certificate_flag'] = 0;
            }

            $driving_license_condition = array('user_id' => $ap['user_id'], 'document_type' => 'driving_license');
            $driving_license_details = $this->mapi->getRow('user_docs', $driving_license_condition);

            if (isset($driving_license_details['document_path'])) {
              $personal_info['driving_license'] = '/public/upload_image/driving_license/' . $driving_license_details['document_path'];
              $personal_info['driving_license_flag'] = 1;
            } else {
              $personal_info['driving_license'] = '';
              $personal_info['driving_license_flag'] = 0;
            }

            $login_card_condition = array('user_id' => $ap['user_id'], 'document_type' => 'login_card');
            $login_card_details = $this->mapi->getRow('user_docs', $login_card_condition);

            if (isset($login_card_details['document_path'])) {
              $personal_info['login_card'] = '/public/upload_image/login_card/' . $login_card_details['document_path'];
              $personal_info['login_card_flag'] = 1;
            } else {
              $personal_info['login_card'] = '';
              $personal_info['login_card_flag'] = 0;
            }


            $paper_dc_condition = array('user_id' => $ap['user_id'], 'document_type' => 'paper_dc');
            $paper_dc_details = $this->mapi->getRow('user_docs', $paper_dc_condition);

            if (isset($paper_dc_details['document_path'])) {
              $personal_info['paper_dc'] = '/public/upload_image/paper_dc/' . $paper_dc_details['document_path'];
              $personal_info['paper_dc_flag'] = 1;
            } else {
              $personal_info['paper_dc'] = '';
              $personal_info['paper_dc_flag'] = 0;
            }

            $signature_condition = array('user_id' => $ap['user_id'], 'document_type' => 'signature');
            $signature_details = $this->mapi->getRow('user_docs', $signature_condition);

            if (isset($signature_details['document_path'])) {
              $personal_info['signature'] = '/public/upload_image/signature/' . $paper_dc_details['document_path'];
              $personal_info['signature_flag'] = 1;
            } else {
              $personal_info['signature'] = '';
              $personal_info['signature_flag'] = 0;
            }





            $response = array('status' => 0, 'message' => 'Profile Details Fetched Successfully', 'basic_info' => $basic_info, 'address_info' => $address_info, 'bank_info' => $bank_info, 'personal_info' => $personal_info);
          } else {

            $response = array('status' => 1, 'message' => 'No Data Found', 'profile_details' => $this->obj);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'driver_details' => $this->obj);
      }
    } else {
      $response = array('status' =>  1, 'message' => 'Wrong http method type', 'driver_details' => $this->obj);
    }
    $this->displayOutput($response);
  }

  public function get_question_list()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'question_list' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'question_list' => $this->obj);
          $this->displayOutput($response);
        }



        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'question_list' => $this->obj);
          $this->displayOutput($response);
        } else {

          $condition = array('is_active' => "1");
          $question_list = $this->mapi->getDetails('master_question', $condition);


          if (!empty($question_list)) {

            $response = array('status' => 0, 'message' => 'Question List Fetched Successfully', 'question_list' => $question_list);
          } else {

            $response = array('status' => 1, 'message' => 'No Data Found', 'question_list' => $this->obj);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'question_list' => $this->obj);
      }
    } else {
      $response = array('status' =>  1, 'message' => 'Wrong http method type', 'question_list' => $this->obj);
    }
    $this->displayOutput($response);
  }


  public function submit_inspection_data()
  {

    // echo '<pre>';print_r($this->input->post());

    // die;
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($this->input->post())) {

        if (empty($this->input->post('token_key'))) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('device_type'))) {
          $response = array('status' =>  1, 'message' => 'Device Type is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('driver_id'))) {
          $response = array('status' =>  1, 'message' => 'Driver ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('car_id'))) {
          $response = array('status' =>  1, 'message' => 'Car ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('shift_id'))) {
          $response = array('status' =>  1, 'message' => 'Shift ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        //echo '<pre>';print_r($inspection[$_FILES['images']]);die;

        $access_token_result = $this->check_access_token($this->input->post('token_key'), $this->input->post('device_type'));

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'result' => $this->obj);
          $this->displayOutput($response);
        } else {

          if (!empty($this->input->post('inspection'))) {

            $inspection_data['driver_id'] = $this->input->post('driver_id');
            $inspection_data['car_id'] = $this->input->post('car_id');
            $inspection_data['shift_id'] = $this->input->post('shift_id');
            $inspection_data['inspection_date'] = date('Y-m-d H:i:s');
            $inspection_data['created_ts'] = date('Y-m-d H:i:s');

            $inspection_id = $this->mapi->insert('inspection', $inspection_data);
            if ($inspection_id) {
              $inspection_list = $this->input->post('inspection');
              //echo '<pre>';print_r($inspection_list);
              $test_array = array('status' => true, 'list' => 'abcd');
              //strval($inspection_list[0]);
              //strval($test_array);die;

              //print_r($inspection_list[0]['question_id']);
              //print_r($test_array['status']);die;
              //echo $inspection_list[0]['question_id'];
              //echo $inspection_list[0]['answer'];

              $count =0;
              foreach ($inspection_list as $inspection) {

                $inspection_answer['inspection_id'] = $inspection_id;
                $inspection_answer['question_id'] = $inspection['question_id'];
                $inspection_answer['answer'] = $inspection['answer'];

                $inspection_answer_id = $this->mapi->insert('inspection_answer', $inspection_answer);
                if ($inspection_answer_id) {

                  //echo '<pre>';print_r($_FILES['images_'.$inspection['question_id']]); die;

                  if (isset($_FILES['images_' . $inspection['question_id']]) && !empty($_FILES['images_' . $inspection['question_id']])) {

                    $path = './public/upload_image/inspection_answer/';
                    $upload_file = $this->upload_inspection_images($path, $_FILES['images_' . $inspection['question_id']], $inspection_answer_id, 'inspection_answer');
                  }
                } else {
                  $response = array('status' =>  1, 'message' => 'Oops!something went wrong...', 'result' => $this->obj);
                  $this->displayOutput($response);
                }
                $response = array('status' => 0, 'message' => 'Inspection data saved successfully', 'result' => $this->obj);
              
                //----------------------------------------------------Inspection Notification--------------------------------------------------//
                  $car_data=$this->mapi->getRow('master_car',array('car_id'=>$this->input->post('car_id')));
                  $car_no = (isset($car_data['registration_no']))?$car_data['registration_no']:'';
                  $instdata   = array(
                    'notification_flag'   => 0,
                    'notification_type'   => 'inspection',
                    'notification_title'  => 'Inspection Data Notification',
                    'notification_description' => 'Inspection Data Notification for car -'.$car_no,
                    'notification_link'   => 'admin/inspection-details/'.$inspection_id,
                    'referrence_id'    => $inspection_id,
                    'created_on'    => date('Y-m-d H:i:s')
                  );
                  if($inspection['answer'] == '0' && $count == 0){$count++;
                    $this->mcommon->insert('notification',$instdata);
                  }
                //-------------------------------------------------------------END-------------------------------------------------------------//
              
              }
             
            
            } else {
              $response = array('status' =>  1, 'message' => 'Oops!something went wrong...', 'result' => $this->obj);
              $this->displayOutput($response);
            }
          } else {

            $response = array('status' =>  1, 'message' => 'Please provide inspection data', 'result' => $this->obj);
            $this->displayOutput($response);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'result' => $this->obj);
      }
    } else {
      $response = array('status' =>  1, 'message' => 'Wrong http method type', 'result' => $this->obj);
    }
    $this->displayOutput($response);
  }

  public function submit_accident_data()
  {
    // echo '<pre>';print_r($this->input->post());

    // die;
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($this->input->post())) {

        if (empty($this->input->post('token_key'))) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('device_type'))) {
          $response = array('status' =>  1, 'message' => 'Device Type is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('driver_id'))) {
          $response = array('status' =>  1, 'message' => 'Driver ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('car_id'))) {
          $response = array('status' =>  1, 'message' => 'Car ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('shift_id'))) {
          $response = array('status' =>  1, 'message' => 'Shift ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        //echo '<pre>';print_r($inspection[$_FILES['images']]);die;

        $access_token_result = $this->check_access_token($this->input->post('token_key'), $this->input->post('device_type'));

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'result' => $this->obj);
          $this->displayOutput($response);
        } else {

          $accident_data['driver_id'] = $this->input->post('driver_id');
          $accident_data['car_id'] = $this->input->post('car_id');
          $accident_data['shift_id'] = $this->input->post('shift_id');
          $accident_data['no_of_car'] = $this->input->post('no_of_car');
          $accident_data['accident_datetime'] =  date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $this->input->post('accident_datetime'))));
          $accident_data['place'] = $this->input->post('place');
          $accident_data['description'] = $this->input->post('description');
          $accident_data['created_by'] = $this->input->post('driver_id');
          $accident_data['created_ts'] = date('Y-m-d H:i:s');
          $accident_data['fault_type'] = $this->input->post('fault_type');

          $accident_id = $this->mapi->insert('master_accident', $accident_data);
          if ($accident_id) {
            $involved_car_list = $this->input->post('involved_car');

            foreach ($involved_car_list as $involved_car) {

              $involved_car_data['accident_id'] = $accident_id;
              $involved_car_data['make'] = $involved_car['make'];
              $involved_car_data['rego'] = $involved_car['rego'];
              $involved_car_data['is_insured'] = $involved_car['is_insured'];
              $involved_car_data['insurance_company'] = $involved_car['insurance_company'];
              $involved_car_data['owner_driver_flag'] = $involved_car['owner_driver_flag'];
              $involved_car_data['owner_name'] = $involved_car['owner_name'];
              $involved_car_data['owner_address'] = $involved_car['owner_address'];
              $involved_car_data['owner_contact_no'] = $involved_car['owner_contact_no'];
              $involved_car_data['owner_email'] = $involved_car['owner_email'];
              $involved_car_data['driver_name'] = $involved_car['driver_name'];
              $involved_car_data['driver_address'] = $involved_car['driver_address'];
              $involved_car_data['driver_contact_no'] = $involved_car['driver_contact_no'];
              $involved_car_data['driver_email'] = $involved_car['driver_email'];

              $involved_car_id = $this->mapi->insert('accident_involved_car', $involved_car_data);
            }
            //echo '<pre>';print_r($_FILES['images_'.$inspection['question_id']]); die;

            if (isset($_FILES['accident_media']) && !empty($_FILES['accident_media'])) {

              $path = './public/upload_image/accident_media/';
              $upload_file = $this->upload_accident_images($path, $_FILES['accident_media'], $accident_id, 'accident_media');
            }

            if (isset($_FILES['driving_licence_front']) && !empty($_FILES['driving_licence_front'])) {
              $path = './public/upload_image/accident_media/';
              $upload_file = $this->single_image_upload($path, $_FILES['driving_licence_front'], 'driving_licence_front');

              if ($upload_file['status'] == 1) {
                $ap['accident_id'] = $accident_id;
                $ap['media_link'] = $upload_file['result'];
                $ap['type'] = 'driving_licence_front';
                $this->mapi->insert('accident_media', $ap);
              }
            }

            if (isset($_FILES['car_number_plate']) && !empty($_FILES['car_number_plate'])) {
              $path = './public/upload_image/accident_media/';
              $upload_file = $this->single_image_upload($path, $_FILES['car_number_plate'], 'car_number_plate');

              if ($upload_file['status'] == 1) {
                $ap['accident_id'] = $accident_id;
                $ap['media_link'] = $upload_file['result'];
                $ap['type'] = 'car_number_plate';
                $this->mapi->insert('accident_media', $ap);
              }
            }

            if (isset($_FILES['driving_licence_back']) && !empty($_FILES['driving_licence_back'])) {
              $path = './public/upload_image/accident_media/';
              $upload_file = $this->single_image_upload($path, $_FILES['driving_licence_back'], 'driving_licence_back');

              if ($upload_file['status'] == 1) {
                $ap['accident_id'] = $accident_id;
                $ap['media_link'] = $upload_file['result'];
                $ap['type'] = 'driving_licence_back';
                $this->mapi->insert('accident_media', $ap);
              }
            }


            $witness_data['accident_id'] = $accident_id;
            $witness_data['witness_name'] = $this->input->post('witness_name');
            $witness_data['witness_address'] = $this->input->post('witness_address');
            $witness_data['witness_mobile'] = $this->input->post('witness_mobile');
            $witness_data['is_police_attended'] = $this->input->post('is_police_attended');
            $witness_data['officer_name'] = $this->input->post('officer_name');
            $witness_data['police_station'] = $this->input->post('police_station');
            $witness_data['officer_phone_no'] = $this->input->post('officer_phone_no');
            $witness_data['officer_reference_no'] = $this->input->post('officer_reference_no');

            $this->mapi->insert('accident_witness', $witness_data);


            $response = array('status' => 0, 'message' => 'Accident data saved successfully', 'result' => $this->obj);
          } else {
            $response = array('status' =>  1, 'message' => 'Oops!something went wrong...', 'result' => $this->obj);
            $this->displayOutput($response);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Post Data Empty', 'result' => $this->obj);
      }
    } else {
      $response = array('status' =>  1, 'message' => 'Wrong http method type', 'result' => $this->obj);
    }
    $this->displayOutput($response);
  }


  private function check_access_token($token_key, $device_type)
  {

    $condition_token = array('token_key' => $token_key, 'device_type' => $device_type);
    $access_token_result = $this->mapi->getRow('api_token', $condition_token);
    return $access_token_result;
  }



  //Forgot password
  public function forgotPassword()
  {

    $ap = json_decode(file_get_contents('php://input'), true);

    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {
        // if (empty($ap['token_key'])) {
        //   $response = array('status' => array('error_code' => 3, 'message' => 'Token key is required'), 'result' => array('details' => $this->obj));
        //   $this->displayOutput($response);
        // }
        if (empty($ap['dc_no'])) {
          $response = array('status' => 1, 'message' => 'DC Number field is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        $condition['dc_no'] = $ap['dc_no'];
        $user_details = $this->mapi->getRow('master_user', $condition);

        if (empty($user_details)) {
          $response = array('status' => 1, 'message' => 'Sorry! User does not exist in our database', 'result' => $this->obj);
          $this->displayOutput($response);
        } else {
          $encoded_key = base64_encode(rand());
          $user_data['recovery_key'] = $encoded_key;
          $condition_user = array('user_id' => $user_details['user_id']);
          $this->mapi->update('master_user', $condition_user, $user_data);

          $profile_details = $this->mapi->getRow('user_profile', $condition_user);

          if (!empty($profile_details)) {
            $mail['name'] = $profile_details['first_name'] . ' ' . $profile_details['last_name'];
          }
          $mail['to'] = $user_details['email'];
          $mail['subject'] = 'Punjabmotors Recover Password';

          $link = base_url('recover_password_user/' . $encoded_key);
          $mail_temp = file_get_contents('./global/mail/forgotpassword_template.html');
          $mail_temp = str_replace("{web_url}", SITEURL, $mail_temp);
          $mail_temp = str_replace("{logo}", base_url('public/admin_assets/images/logo.png'), $mail_temp);
          $mail_temp = str_replace("{name}", $mail['name'], $mail_temp);
          $mail_temp = str_replace("{link}", $link, $mail_temp);
          $mail_temp = str_replace("{current_year}", CURRENT_YEAR, $mail_temp);
          $mail['message'] = $mail_temp;
          //echo '<pre>';print_r($mail);die;
          if ($this->sendMail($mail)) {
            $response = array('status' => 0, 'message' => 'Password recovery mail has been sent to your email', 'result' => $this->obj);
          } else {
            $response = array('status' => 0, 'message' => 'Uanble to send recovery mail.', 'result' => $this->obj);
          }
        }
      } else {
        $response = array('status' =>  1, 'message' => 'Please fill up all required fields', 'result' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'result' => $this->obj);
    }
    $this->displayOutput($response);
  }


  public function get_payin_onload_value()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['driver_id'])) {
          $response = array('status' =>  1, 'message' => 'Driver ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'result' => $this->obj);
          $this->displayOutput($response);
        } else {


          $driver_details = $this->mapi->getRow('user_profile', array('user_id' => $ap['driver_id']));

          $ongoing_shift_details = $this->mapi->get_ongoing_shift_details($ap['driver_id']);
          
          $levy_rate = 0;
          if(!empty($ongoing_shift_details['levy_applicable']) && ($ongoing_shift_details['levy_applicable'] == 1)){
            $levy_rate = $this->mapi->getRow('master_levy', array());
          }
          
          
          $lifting_fees = $this->mapi->getRow('master_lifting_fee', array());
          $result = array();
          //print_r($driver_details['dr_commission_id']);die;
          if (!empty($driver_details['dr_commission_id'])) {
            $commission_rate = $this->mapi->getRow('master_commission', array('commission_id' => $driver_details['dr_commission_id']));
            $result['commission_rate_driver'] = $commission_rate['owner_share']; // For Logical changes we  swap the values.
            $result['commission_rate_owner'] = $commission_rate['driver_share']; // For Logical changes we  swap the values.
          } else {
            $result['commission_rate_driver'] = "0";
            $result['commission_rate_owner'] = "0";
          }

          $bond_data = $this->mcommon->getRow('master_bond',array('bond_settlement_flag !='=>2,'driver_id'=>$ap['driver_id']));
          $bond_value=!empty($bond_data['installment_shift'])?$bond_data['installment_shift']:"0";
          $result['bond_value'] = $bond_value;
          $result['levy_rate'] = !empty($levy_rate['levy_rate'])?$levy_rate['levy_rate']:'0';
          $result['lifting_fees'] = !empty($lifting_fees['gtn_share'])?$lifting_fees['gtn_share']:'0'; 


          if (!empty($result)) {

            $response = array('status' => 0, 'message' => 'Data Fetched Successfully', 'result' => $result);
          } else {

            $response = array('status' => 1, 'message' => 'No Data Found', 'result' => $this->obj);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'result' => $this->obj);
      }
    } else {
      $response = array('status' =>  1, 'message' => 'Wrong http method type', 'result' => $this->obj);
    }
    $this->displayOutput($response);
  }

  public function get_previous_payin_data()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        if (empty($ap['car_id'])) {
          $response = array('status' =>  1, 'message' => 'Car ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'result' => $this->obj);
          $this->displayOutput($response);
        } else {


          $payin_details = $this->mapi->get_previous_payin_data($ap['car_id']);

          if (!empty($payin_details)) {

            $previous_payin_details['payin_id'] = $payin_details['payin_id'];
            $previous_payin_details['total_extra_start'] = $payin_details['total_extra_end'];
            $previous_payin_details['paid_km_start'] = $payin_details['paid_km_end'];
            $previous_payin_details['total_km_start'] = $payin_details['total_km_end'];
            $previous_payin_details['no_of_hiring_start'] = $payin_details['no_of_hiring_end'];
            $previous_payin_details['extra_start'] = $payin_details['extra_end'];
            $previous_payin_details['speedo_reading_start'] = $payin_details['speedo_reading_end'];
            
            
            
          } else {

            $previous_payin_details['payin_id'] = '';
            $previous_payin_details['total_extra_start'] = '0.00';
            $previous_payin_details['paid_km_start'] = '0';
            $previous_payin_details['total_km_start'] = '0';
            $previous_payin_details['no_of_hiring_start'] = '0';
            $previous_payin_details['extra_start'] = '0';
            $previous_payin_details['speedo_reading_start'] = '0';
          }
          $response = array('status' => 0, 'message' => 'Data Fetched Successfully', 'previous_payin_details' => $previous_payin_details);
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'result' => $this->obj);
      }
    } else {
      $response = array('status' =>  1, 'message' => 'Wrong http method type', 'result' => $this->obj);
    }
    $this->displayOutput($response);
  }

  public function get_expense_list()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'expense_list' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'expense_list' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'expense_list' => $this->obj);
          $this->displayOutput($response);
        } else {
          $expense_list = $this->mapi->getDetails('master_expense', array('is_active' => 1));
          if (!empty($expense_list)) {
            $response = array('status' => 0, 'message' => 'Expense List Fetched Successfully', 'expense_list' => $expense_list);
          } else {
            $response = array('status' => 0, 'message' => 'No Data Found', 'expense_list' => $this->obj);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'expense_list' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'expense_list' => $this->obj);
    }
    $this->displayOutput($response);
  }

  public function submit_payin_data(){
    //  echo '<pre>';
    //   print_r($this->input->post());
    //   print_r($_FILES);
    //  die;
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($this->input->post())) {

        if (empty($this->input->post('token_key'))) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('device_type'))) {
          $response = array('status' =>  1, 'message' => 'Device Type is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($this->input->post('driver_id'))) {
          $response = array('status' =>  1, 'message' => 'Driver ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        if (empty($this->input->post('shift_id'))) {
          $response = array('status' =>  1, 'message' => 'Shift ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }


        // if (empty($this->input->post('shift_id'))) {
        //   $response = array('status' =>  1, 'message' => 'Shift ID is required', 'result' => $this->obj);
        //   $this->displayOutput($response);
        // }

        //echo '<pre>';print_r($inspection[$_FILES['images']]);die;

        $access_token_result = $this->check_access_token($this->input->post('token_key'), $this->input->post('device_type'));

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'result' => $this->obj);
          $this->displayOutput($response);
        } else {

          if (!empty($this->input->post())) {

            $payin_data['driver_id'] = $this->input->post('driver_id');
            $payin_data['shift_id'] = $this->input->post('shift_id');
            $payin_data['total_extra_start'] = $this->input->post('total_extra_start');
            $payin_data['total_extra_end'] = $this->input->post('total_extra_end');
            $payin_data['paid_km_start'] = $this->input->post('paid_km_start');
            $payin_data['paid_km_end'] = $this->input->post('paid_km_end');
            $payin_data['total_km_start'] = $this->input->post('total_km_start');
            $payin_data['total_km_end'] = $this->input->post('total_km_end');
            $payin_data['no_of_hiring_start'] = $this->input->post('no_of_hiring_start');
            $payin_data['no_of_hiring_end'] = $this->input->post('no_of_hiring_end');
            $payin_data['extra_start'] = $this->input->post('extra_start');
            $payin_data['extra_end'] = $this->input->post('extra_end');
            $payin_data['speedo_reading_start'] = $this->input->post('speedo_reading_start');
            $payin_data['speedo_reading_end'] = $this->input->post('speedo_reading_end');
            $payin_data['km_traveled'] = $this->input->post('km_traveled');
            $payin_data['metered_fares'] = $this->input->post('metered_fares');
            $payin_data['bond_charges'] = $this->input->post('bond_charges');
            $payin_data['subtotal'] = $this->input->post('subtotal');
            $payin_data['levy'] = $this->input->post('levy');
            $payin_data['no_of_whl'] = $this->input->post('no_of_whl');
            $payin_data['total_payin_payout'] = $this->input->post('total_payin_payout');
            $payin_data['created_by'] = $this->input->post('driver_id');
            $payin_data['created_ts'] = date('Y-m-d H:i:s');

            if (isset($_FILES['payin_reference_doc']) && !empty($_FILES['payin_reference_doc'])) {

              $path = './public/upload_image/payin_images/';
              $upload_file = $this->single_image_upload($path, $_FILES['payin_reference_doc'], 'payin_reference_doc');

              if ($upload_file['status'] == 1) {
                $payin_data['payin_reference_doc'] = $upload_file['result'];
              }
            }
            
            
            $payin_id = $this->mapi->insert('payin', $payin_data);
            if ($payin_id) {
              $payin_dockets_list = $this->input->post('payin_dockets');
              if(isset($payin_dockets_list) && !empty($payin_dockets_list)){
                foreach ($payin_dockets_list as $payin_dockets) {
                  //print_r($payin_dockets);die;
                  $dockets=array();
                  if(!empty($payin_dockets['docket_id']) && !empty($payin_dockets['docket_value'])){
                    $dockets['payin_id'] = $payin_id;
                    $dockets['docket_id'] = $payin_dockets['docket_id'];
                    $dockets['value'] = $payin_dockets['docket_value'];
                  }
                  if (isset($_FILES[$payin_dockets['docket_images_key']]) && !empty($_FILES[$payin_dockets['docket_images_key']])) {

                    $path = './public/upload_image/payin_images/';
                    $upload_file = $this->single_image_upload($path, $_FILES[$payin_dockets['docket_images_key']], $payin_dockets['docket_images_key']);

                    if ($upload_file['status'] == 1) {
                      $dockets['doc_link'] = $upload_file['result'];
                    }
                    // else{
                    //     $response = array('status' => 1, 'message' => $upload_file['result'], 'driver_details' => $this->obj);
                    //     $this->displayOutput($response);
                    //   }
                  }
                  if(!empty($dockets)){
                    $payin_dockets_id = $this->mapi->insert('payin_dockets', $dockets);
                  }
                }
              }
              
              $payin_expense_list = $this->input->post('payin_expense');
              if(isset($payin_expense_list) && !empty($payin_expense_list)){
                foreach ($payin_expense_list as $payin_expense) {
                  $expense=array();
                  if(!empty($payin_expense['expense_id']) && !empty($payin_expense['expense_value'])){
                    $expense['payin_id'] = $payin_id;
                    $expense['expense_id'] = $payin_expense['expense_id'];
                    $expense['value'] = $payin_expense['expense_value'];
                  }

                  if (isset($_FILES[$payin_expense['expense_images_key']]) && !empty($_FILES[$payin_expense['expense_images_key']])) {

                    $path = './public/upload_image/payin_images/';
                    $upload_file = $this->single_image_upload($path, $_FILES[$payin_expense['expense_images_key']], $payin_expense['expense_images_key']);

                    if ($upload_file['status'] == 1) {
                      $expense['doc_link'] = $upload_file['result'];
                    }
                    // else{
                    //     $response = array('status' => 1, 'message' => $upload_file['result'], 'driver_details' => $this->obj);
                    //     $this->displayOutput($response);
                    //   }
                    
                  }
                  if(!empty($expense)){
                    $payin_expense_id = $this->mapi->insert('payin_expenses', $expense);
                  }
                }
              }
              //------------------------------------------Change Shift Status-------------------------------------------//
              $condition_shift = array('shift_id' => $this->input->post('shift_id'));
              
              $shift_details = $this->mapi->getRow('driver_shift_details',$condition_shift);
              if(!empty($shift_details)){
                
                $insert_shift_payin = $this->mapi->insert_driver_shift_payin($shift_details['shift_start_time'],date('Y-m-d H:i:s'),$this->input->post('driver_id'),$this->input->post('shift_id'));
              
                if($insert_shift_payin){  
                  $shift_data['shift_status'] = 2;
                  $shift_data['shift_end_time'] = date('Y-m-d H:i:s');
                  $this->mapi->update('driver_shift_details', $condition_shift, $shift_data);
                }
              
              }
              //--------------------------------------------------END--------------------------------------------------//
              
              //------------------------------------------Check Service Notification-----------------------------------//
              $check_service_notification =  $this->mapi->check_service_notification($this->input->post('shift_id'), $payin_id);
              $response = array('status' => 0, 'message' => 'Payin data saved successfully', 'result' => $this->obj);
              //---------------------------------------------------END-------------------------------------------------//
            } else {
              $response = array('status' =>  1, 'message' => 'Oops!something went wrong...', 'result' => $this->obj);
              $this->displayOutput($response);
            }
          } else {

            $response = array('status' =>  1, 'message' => 'Please provide Pay in data', 'result' => $this->obj);
            $this->displayOutput($response);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'result' => $this->obj);
      }
    } else {
      $response = array('status' =>  1, 'message' => 'Wrong http method type', 'result' => $this->obj);
    }
    $this->displayOutput($response);
  }



  public function get_payin_list()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'payin_list' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'payin_list' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['driver_id'])) {
          $response = array('status' =>  1, 'message' => 'Driver ID is required', 'payin_list' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'payin_list' => $this->obj);
          $this->displayOutput($response);
        } else {
          $payin_list = $this->mapi->get_payin_list($ap['driver_id']);
          if (!empty($payin_list)) {
            $response = array('status' => 0, 'message' => 'Payin List Fetched Successfully', 'payin_list' => $payin_list);
          } else {
            $response = array('status' => 0, 'message' => 'No Data Found', 'payin_list' => array());
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'payin_list' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'payin_list' => $this->obj);
    }
    $this->displayOutput($response);
  }

  public function get_payin_details()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'payin_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'payin_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['payin_id'])) {
          $response = array('status' =>  1, 'message' => 'Payin ID is required', 'payin_details' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'payin_details' => $this->obj);
          $this->displayOutput($response);
        } else {
          $payin_details = $this->mapi->get_payin_details($ap['payin_id']);
          $docket_details = $this->mapi->get_payin_docket_details($ap['payin_id']);
          $expense_details = $this->mapi->get_payin_expense_details($ap['payin_id']);
          if (!empty($payin_details)) {
            $response = array('status' => 0, 'message' => 'Payin Details Fetched Successfully', 'payin_details' => $payin_details, 'docket_details' => $docket_details, 'expense_details' => $expense_details);
          } else {
            $response = array('status' => 0, 'message' => 'No Data Found', 'payin_details' => array(), 'docket_details' => array(), 'expense_details' => array());
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'payin_details' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'payin_details' => $this->obj);
    }
    $this->displayOutput($response);
  }


  public function get_roster_list()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'roster_list' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'roster_list' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['driver_id'])) {
          $response = array('status' =>  1, 'message' => 'Driver ID is required', 'roster_list' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'roster_list' => $this->obj);
          $this->displayOutput($response);
        } else {
          $payin_list = $this->mapi->get_roster_list($ap['driver_id']);
          if (!empty($payin_list)) {
            $response = array('status' => 0, 'message' => 'Roster List Fetched Successfully', 'roster_list' => $payin_list);
          } else {
            $response = array('status' => 0, 'message' => 'No Data Found', 'roster_list' => array());
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'roster_list' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'roster_list' => $this->obj);
    }
    $this->displayOutput($response);
  }


  public function get_shift_details()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'shift_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'shift_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['driver_id'])) {
          $response = array('status' =>  1, 'message' => 'Driver ID is required', 'shift_details' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'shift_details' => $this->obj);
          $this->displayOutput($response);
        } else {

          //--------------------------------------------------Shift Details-------------------------------------------------------------//
          $shift_details = array();

          $ongoing_shift_details = $this->mapi->get_ongoing_shift_details($ap['driver_id']);

          if (!empty($ongoing_shift_details)) {

            if ($ongoing_shift_details['shift_status'] == 1) {
              $shift_details['status_code'] = 2;
              $shift_details['status'] = 'Ongoing Shift';
              $shift_details['shift_id'] = $ongoing_shift_details['shift_id'];
              $shift_details['shift_no'] = $ongoing_shift_details['shift_no'];
              $shift_details['car_id'] = $ongoing_shift_details['car_id'];
              $shift_details['car_no'] = $ongoing_shift_details['registration_no'];
              $shift_details['roster_id'] = $ongoing_shift_details['roster_id'];
              $shift_details['roster_driver_id'] = $ongoing_shift_details['roster_driver_id'];
              $shift_details['shift_name'] = $ongoing_shift_details['shift_name'];
              $shift_details['day_date'] = $ongoing_shift_details['day_date'];
              $shift_details['dayname'] = $ongoing_shift_details['dayname'];
            } else {
              $shift_details['status_code'] = 0;
              $shift_details['status'] = 'Shift Ended';
              $shift_details['shift_id'] = '';
              $shift_details['shift_no'] = '';
              $shift_details['car_id'] = '';
              $shift_details['car_no'] = $ongoing_shift_details['registration_no'];
              $shift_details['roster_id'] = $ongoing_shift_details['roster_id'];
              $shift_details['roster_driver_id'] = $ongoing_shift_details['roster_driver_id'];
              $shift_details['shift_name'] = $ongoing_shift_details['shift_name'];
              $shift_details['day_date'] = $ongoing_shift_details['day_date'];
              $shift_details['dayname'] = $ongoing_shift_details['dayname'];

              $today_shift_details = $this->mapi->get_today_shift_details($ap['driver_id']);
                  if (!empty($today_shift_details)) {
                    $shift_details['status_code'] = 1;
                $shift_details['status'] = 'Shift Available Today';
                $shift_details['shift_id'] = '';
                $shift_details['shift_no'] = '';
                $shift_details['car_id'] = $today_shift_details['car_id'];
                $shift_details['car_no'] = $today_shift_details['registration_no'];
                $shift_details['roster_id'] = $today_shift_details['roster_id'];
                $shift_details['roster_driver_id'] = $today_shift_details['roster_driver_id'];
                $shift_details['shift_name'] = $today_shift_details['shift_name'];
                $shift_details['day_date'] = $today_shift_details['day_date'];
                $shift_details['dayname'] = $today_shift_details['dayname'];
                } 
              
              
              // $today_shift_details = $this->mapi->get_today_shift_details($ap['driver_id']);
              // if (empty($today_shift_details)) {
              //   $shift_details['status_code'] = 0;
              //   $shift_details['status'] = 'No Shift Available Today';
              //   $shift_details['shift_id'] = '';
              //   $shift_details['shift_no'] = '';
              //   $shift_details['car_id'] = '';
              //   $shift_details['car_no'] = '';
              //   $shift_details['roster_id'] = '';
              //   $shift_details['roster_driver_id'] = '';
              //   $shift_details['shift_name'] = '';
              //   $shift_details['day_date'] = '';
              //   $shift_details['dayname'] = '';
              // } else {

              //   $shift_details['status_code'] = 1;
              //   $shift_details['status'] = 'Shift Available Today';
              //   $shift_details['shift_id'] = '';
              //   $shift_details['shift_no'] = '';
              //   $shift_details['car_id'] = $today_shift_details['car_id'];
              //   $shift_details['car_no'] = $today_shift_details['registration_no'];
              //   $shift_details['roster_id'] = $today_shift_details['roster_id'];
              //   $shift_details['roster_driver_id'] = $today_shift_details['roster_driver_id'];
              //   $shift_details['shift_name'] = $today_shift_details['shift_name'];
              //   $shift_details['day_date'] = $today_shift_details['day_date'];
              //   $shift_details['dayname'] = $today_shift_details['dayname'];
              //   //print_r($today_shift_details);die;
              // }
            }
          } else {

            $today_shift_details = $this->mapi->get_today_shift_details($ap['driver_id']);
            if (empty($today_shift_details)) {
              $shift_details['status_code'] = 0;
              $shift_details['status'] = 'No Shift Available Today';
              $shift_details['shift_id'] = '';
              $shift_details['shift_no'] = '';
              $shift_details['car_id'] = '';
              $shift_details['car_no'] = '';
              $shift_details['roster_id'] = '';
              $shift_details['roster_driver_id'] = '';
              $shift_details['shift_name'] = '';
              $shift_details['day_date'] = '';
              $shift_details['dayname'] = '';
            } else {

              $shift_details['status_code'] = 1;
              $shift_details['status'] = 'Shift Available Today';
              $shift_details['shift_id'] = '';
              $shift_details['shift_no'] = '';
              $shift_details['car_id'] = $today_shift_details['car_id'];
              $shift_details['car_no'] = $today_shift_details['registration_no'];
              $shift_details['roster_id'] = $today_shift_details['roster_id'];
              $shift_details['roster_driver_id'] = $today_shift_details['roster_driver_id'];
              $shift_details['shift_name'] = $today_shift_details['shift_name'];
              $shift_details['day_date'] = $today_shift_details['day_date'];
              $shift_details['dayname'] = $today_shift_details['dayname'];
              //print_r($today_shift_details);die;
            }
          }
          //---------------------------------------------------END---------------------------------------------------------------//





          //$shift_details = $this->mapi->get_today_shift_details($ap['driver_id']);
          if (!empty($shift_details)) {
            $response = array('status' => 0, 'message' => 'Shift Details Fetched Successfully', 'shift_details' => $shift_details);
          } else {
            $response = array('status' => 0, 'message' => 'No Shift Found', 'shift_details' => $this->obj);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'shift_details' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'shift_details' => $this->obj);
    }
    $this->displayOutput($response);
  }



  public function start_shift()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'shift_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'shift_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['roster_driver_id'])) {
          $response = array('status' =>  1, 'message' => 'Roaster Driver ID is required', 'shift_details' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'shift_details' => $this->obj);
          $this->displayOutput($response);
        } else {
          $shift_details = $this->mapi->getDetails('driver_shift_details', array('roster_driver_id' => $ap['roster_driver_id']));
          if (empty($shift_details)) {

            $shift_number = $this->mapi->get_shift_number($ap['roster_driver_id']);
            if (empty($shift_number)) {
              $response = array('status' =>  1, 'message' => 'Unable to generate shift number', 'shift_details' => $this->obj);
              $this->displayOutput($response);
            } else {
              $shift_data = array();
              $shift_data['roster_driver_id'] = $ap['roster_driver_id'];
              $shift_data['shift_no'] = $shift_number['shift_no'];
              $shift_data['shift_start_time'] = date('Y-m-d H:i:s');
              $shift_data['shift_status'] = 1;
              $shift_data['created_ts'] = date('Y-m-d H:i:s');

              $shift_id = $this->mapi->insert('driver_shift_details', $shift_data);


              if ($shift_id) {
                $response = array('status' => 0, 'message' => 'Shift Started Successfully', 'shift_details' => array('shift_id' => $shift_id, 'shift_no' => $shift_number['shift_no']));
              } else {
                $response = array('status' => 0, 'message' => 'Oops!something went wrong...', 'shift_details' => $this->obj);
              }
            }
          } else {
            $response = array('status' => 1, 'message' => 'Shift Already Started', 'shift_details' => $this->obj);
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'shift_details' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'shift_details' => $this->obj);
    }
    $this->displayOutput($response);
  }


  public function dashboard(){

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'inspection_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'inspection_details' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['user_id'])) {
          $response = array('status' =>  1, 'message' => 'User ID is required', 'inspection_details' => $this->obj);
          $this->displayOutput($response);
        }


        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'inspection_details' => $this->obj);
          $this->displayOutput($response);
        } else {

          if (!empty(trim($ap['car_id']," ")) && !empty(trim($ap['shift_id']," "))) {


            $inspection_status = $this->mapi->check_inspection_status($ap['car_id']);
            //echo '<pre>';print_r($inspection_status);die;
            $inspection_details = array();
            if (empty($inspection_status)) {
              $inspection_details['status_code'] = 0;
              $inspection_details['status'] = 'Inspection mandatory for first time';
            } else {

              if ($inspection_status['inspection_status'] == 0) {
                $inspection_details['status_code'] = 0;
                $inspection_details['status'] = 'Inspection mandatory for first time';
              } else {
                $inspection_details['status_code'] = 1;
                $inspection_details['status'] = 'Inspection report OK';
              }
            }
          } else {
            $inspection_details['status_code'] = 2;
            $inspection_details['status'] = 'Inspection Not needed for no shift';
          }
          
          $payout_amount=$this->mapi->get_total_payout_amount($ap['user_id']);
          //echo (!empty($payout_amount['tot_final_settlement_amt']))?$payout_amount['tot_final_settlement_amt']:'0.00'; die;
          $bond_accumulated=$this->mapi->get_bond_accumulated_amount($ap['user_id']);
          
          $inspection_details['total_payout'] = "$".((!empty($payout_amount['tot_final_settlement_amt']))?$payout_amount['tot_final_settlement_amt']:'0.00');
          $inspection_details['bond_accumulated'] = "$".((!empty($bond_accumulated['tot_bond_accumulated_amt']))?$bond_accumulated['tot_bond_accumulated_amt']:'0.00');
          //echo $inspection_details['total_payout'] ;die;
          $response = array('status' => 0, 'message' => 'Dashboard data fetched successfully', 'inspection_details' => $inspection_details);
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'inspection_details' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'inspection_details' => $this->obj);
    }
    $this->displayOutput($response);
  }



  //Change Password
  public function changePassword()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {
      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        if (empty($ap['old_password'])) {
          $response = array('status' =>  1, 'message' => 'Old password is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        if (empty($ap['new_password'])) {
          $response = array('status' =>  1, 'message' => 'New password is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        if (empty($ap['user_id'])) {
          $response = array('status' =>  1, 'message' => 'User ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'result' => $this->obj);
          $this->displayOutput($response);
        } else {

          $condition = array('user_id' => $ap['user_id']);
          $userdetails = $this->mapi->getRow('master_user', $condition);

          if ($userdetails['password'] != md5($ap['old_password'])) {
            $response = array('status' =>  1, 'message' => "Old password didn't match with previous one", 'result' => $this->obj);
            $this->displayOutput($response);
          }
          if (strtolower($ap['new_password']) == strtolower($ap['old_password'])) {
            $response = array('status' =>  1, 'message' => "New password same as old password,try something else", 'result' => $this->obj);
            $this->displayOutput($response);
          }

          $data = array('updated_ts' => date('Y-m-d H:i:s'), 'password' => md5($ap['new_password']),'org_password'=>$ap['new_password']);
          $this->mapi->update('master_user', $condition, $data);
          $response = array('status' =>  0, 'message' => "Password changed successfully", 'result' => $this->obj);
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'result' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'result' => $this->obj);
    }
    $this->displayOutput($response);
  }





  public function get_levy_report()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'levy_list' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'levy_list' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['driver_id'])) {
          $response = array('status' =>  1, 'message' => 'Driver ID is required', 'levy_list' => $this->obj);
          $this->displayOutput($response);
        }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'levy_list' => $this->obj);
          $this->displayOutput($response);
        } else {
          $start_date = '';
          $end_date = '';
          if (!empty($ap['start_date'])  && !empty($ap['end_date'])) {
            $start_date = date('Y-m-d', strtotime(str_replace('/', '-', $ap['start_date'])));
            $end_date = date('Y-m-d', strtotime(str_replace('/', '-', $ap['end_date'])));
          }
          $levy_list = $this->mapi->get_levy_report($ap['driver_id'], $start_date, $end_date);
          if (!empty($levy_list)) {
            $response = array('status' => 0, 'message' => 'Levy List Fetched Successfully', 'levy_list' => $levy_list);
          } else {
            $response = array('status' => 0, 'message' => 'No Data Found', 'levy_list' => array());
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'levy_list' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'levy_list' => $this->obj);
    }
    $this->displayOutput($response);
  }

  public function get_cms_page_list(){
    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'cms_list' => array());
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'cms_list' => array());
          $this->displayOutput($response);
        }
        
        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'cms_list' => array());
          $this->displayOutput($response);
        } else {
          
          $cms_list = $this->mapi->get_cms_page_list();
          if (!empty($cms_list)) {
            $response = array('status' => 0, 'message' => 'CMS List Fetched Successfully', 'cms_list' => $cms_list);
          } else {
            $response = array('status' => 0, 'message' => 'No Data Found', 'cms_list' => array());
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'cms_list' => array());
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'cms_list' => array());
    }
    $this->displayOutput($response);



  }

  public function update_car_current_location(){
    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {

      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'location_list' => array());
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'location_list' => array());
          $this->displayOutput($response);
        }

        if (empty($ap['shift_id'])) {
          $response = array('status' =>  1, 'message' => 'Shift ID is required', 'location_list' => array());
          $this->displayOutput($response);
        }

        if (empty($ap['car_id'])) {
          $response = array('status' =>  1, 'message' => 'Car ID is required', 'location_list' => array());
          $this->displayOutput($response);
        }

        if (empty($ap['lat'])) {
          $response = array('status' =>  1, 'message' => 'Lattitude is required', 'location_list' => array());
          $this->displayOutput($response);
        }

        if (empty($ap['lon'])) {
          $response = array('status' =>  1, 'message' => 'Longitude is required', 'location_list' => array());
          $this->displayOutput($response);
        }
        
        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'location_list' => array());
          $this->displayOutput($response);
        } else {
          
          $check_previous_data=$this->mapi->getRow('driver_tracking',array('car_id'=>$ap['car_id']));
          if(empty($check_previous_data)){
            $driver_tracking_data=array();
            $driver_tracking_data['shift_id'] = $ap['shift_id'];
            $driver_tracking_data['car_id'] = $ap['car_id'];
            $driver_tracking_data['last_lat'] = $ap['lat'];
            $driver_tracking_data['last_lon'] = $ap['lon'];
            $driver_tracking_data['created_ts'] = date('Y-m-d H:i:s');
            $driver_tracking_id = $this->mapi->insert('driver_tracking',$driver_tracking_data);
          } else{
            $driver_tracking_data=array();
            $driver_tracking_data['shift_id'] = $ap['shift_id'];
            $driver_tracking_data['last_lat'] = $ap['lat'];
            $driver_tracking_data['last_lon'] = $ap['lon'];
            $driver_tracking_data['updated_ts'] = date('Y-m-d H:i:s');
            $driver_tracking_id = $this->mapi->update('driver_tracking',array('driver_tracking_id'=>$check_previous_data['driver_tracking_id']),$driver_tracking_data);
          }
          if ($driver_tracking_id) {
            $response = array('status' => 0, 'message' => 'Location Updated Successfully', 'location_list' =>array());
          } else {
            $response = array('status' => 0, 'message' => 'Oops!something went wrong...', 'location_list' => array());
          }
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'location_list' => array());
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'location_list' => array());
    }
    $this->displayOutput($response);



  }

  // Logout
  public function logout()
  {

    $ap = json_decode(file_get_contents('php://input'), true);
    if ($this->checkHttpMethods($this->http_methods[0])) {
      if (sizeof($ap)) {

        if (empty($ap['token_key'])) {
          $response = array('status' =>  1, 'message' => 'Token Key is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }
        if (empty($ap['device_type'])) {
          $response = array('status' =>  1, 'message' => 'Device type is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        if (empty($ap['user_id'])) {
          $response = array('status' =>  1, 'message' => 'User ID is required', 'result' => $this->obj);
          $this->displayOutput($response);
        }

        // if (empty($ap['shift_id'])) {
        // 	$response = array('status' =>  1, 'message' => 'Shift ID is required', 'result' => $this->obj);
        // 	$this->displayOutput($response);
        // }

        $access_token_result = $this->check_access_token($ap['token_key'], $ap['device_type']);

        if (empty($access_token_result)) {

          $response = array('status' => 2, 'message' => 'Unauthorize Token', 'result' => $this->obj);
          $this->displayOutput($response);
        } else {

          if (!empty($ap['shift_id'])) {
            $condition_payin = array('shift_id' => $ap['shift_id']);
            $payin_data = $this->mapi->getRow('payin', $condition_payin);

            if (empty($payin_data)) {

              $response = array('status' =>  1, 'message' => "Please submit your payin first", 'result' => $this->obj);
              $this->displayOutput($response);
            }
          }
          $condition = array('user_id' => $ap['user_id']);
          $user_data['login_status'] = 0;

          $this->mapi->update('master_user', $condition, $user_data);
          $response = array('status' =>  0, 'message' => "Logout Successfully", 'result' => $this->obj);
        }
      } else {
        $response = array('status' => 1, 'message' => 'Please fill up all required fields', 'result' => $this->obj);
      }
    } else {
      $response = array('status' => 1, 'message' => 'Wrong http method type', 'result' => $this->obj);
    }
    $this->displayOutput($response);
  }

  private function sendMail($data)
  {

    $config['protocol']    = 'smtp';
    $config['smtp_host']    = 'ssl://mail.met-technologies.com';
    $config['smtp_port']    = '465';
    $config['smtp_user']    = 'developer.net@met-technologies.com';
    $config['smtp_pass']    = 'Dot123@#$%';
    $config['charset']    = 'utf-8';
    $config['newline']    = "\r\n";
    $config['mailtype'] = 'html'; // or html
    $config['validation'] = TRUE;

    $this->email->initialize($config);

    $this->email->set_crlf("\r\n");

    $this->email->from('developer.net@met-technologies.com', 'Punjabmotors');
    $this->email->to($data['to']);

    $this->email->subject($data['subject']);
    $this->email->message($data['message']);

    $this->email->send();
    //echo $this->email->print_debugger(); die;
    return true;
  }

  private function checkHttpMethods($http_method_type)
  {
    if ($_SERVER['REQUEST_METHOD'] == $http_method_type) {
      return 1;
    }
  }



  private function upload_Product_images($path, $files, $user_id, $document_type)
  {
    //echo"<pre>";print_r($product_id); die;
    $config = array(
      'upload_path'   => $path,
      'allowed_types' => 'gif|jpg|png|jpeg|pdf|xls|xlsx|doc|docx',
      'overwrite'     => 1,

    );

    $this->load->library('upload', $config);

    $images = array();

    foreach ($files['name'] as $key => $image) {
      $_FILES['images[]']['name'] = $files['name'][$key];
      $_FILES['images[]']['type'] = $files['type'][$key];
      $_FILES['images[]']['tmp_name'] = $files['tmp_name'][$key];
      $_FILES['images[]']['error'] = $files['error'][$key];
      $_FILES['images[]']['size'] = $files['size'][$key];

      $fileName = $document_type . '_' . time() . '_' . $key . '_' . str_replace(" ", "_", $image);

      $images[] = $fileName;

      $config['file_name'] = $fileName;

      $this->upload->initialize($config);

      if ($this->upload->do_upload('images[]')) {

        $ap['user_id'] = $user_id;
        $ap['document_type'] = $document_type;
        $ap['document_path'] = $fileName;
        $ap['created_ts']=date('Y-m-d H:i:s');
        $this->mapi->insert('user_docs', $ap);
      } else {
        //echo $this->upload->display_errors(); die;
        return false;
      }
    }

    return $images;
  }

  private function upload_inspection_images($path, $files, $inspection_answer_id, $document_type)
  {
    //echo"<pre>";print_r($product_id); die;
    $config = array(
      'upload_path'   => $path,
      'allowed_types' => 'gif|jpg|png|jpeg|pdf|xls|xlsx|doc|docx',
      'overwrite'     => 1,

    );

    $this->load->library('upload', $config);

    $images = array();

    foreach ($files['name'] as $key => $image) {
      $_FILES['images[]']['name'] = $files['name'][$key];
      $_FILES['images[]']['type'] = $files['type'][$key];
      $_FILES['images[]']['tmp_name'] = $files['tmp_name'][$key];
      $_FILES['images[]']['error'] = $files['error'][$key];
      $_FILES['images[]']['size'] = $files['size'][$key];

      $fileName = $document_type . '_' . time() . '_' . $inspection_answer_id . '_' . $key . '_' . str_replace(" ", "_", $image);

      $images[] = $fileName;

      $config['file_name'] = $fileName;

      $this->upload->initialize($config);

      if ($this->upload->do_upload('images[]')) {

        $ap['inspection_answer_id'] = $inspection_answer_id;
        $ap['image'] = $fileName;
        $ap['created_ts'] = date('Y-m-d H:i:s');
        $this->mapi->insert('inspection_answer_image', $ap);
      } else {
        //echo $this->upload->display_errors(); die;
        return false;
      }
    }

    return $images;
  }

  private function upload_accident_images($path, $files, $accident_id, $document_type)
  {
    //echo"<pre>";print_r($product_id); die;
    $config = array(
      'upload_path'   => $path,
      'allowed_types' => 'gif|jpg|png|jpeg|pdf|xls|xlsx|doc|docx',
      'overwrite'     => 1,

    );

    $this->load->library('upload', $config);

    $images = array();

    foreach ($files['name'] as $key => $image) {
      $_FILES['images[]']['name'] = $files['name'][$key];
      $_FILES['images[]']['type'] = $files['type'][$key];
      $_FILES['images[]']['tmp_name'] = $files['tmp_name'][$key];
      $_FILES['images[]']['error'] = $files['error'][$key];
      $_FILES['images[]']['size'] = $files['size'][$key];

      $fileName = $document_type . '_' . time() . '_' . $accident_id . '_' . $key . '_' . str_replace(" ", "_", $image);

      $images[] = $fileName;

      $config['file_name'] = $fileName;

      $this->upload->initialize($config);

      if ($this->upload->do_upload('images[]')) {

        $ap['accident_id'] = $accident_id;
        $ap['media_link'] = $fileName;
        $ap['type'] = $document_type;
        $this->mapi->insert('accident_media', $ap);
      } else {
        //echo $this->upload->display_errors(); die;
        return false;
      }
    }

    return $images;
  }


  private function single_image_upload($path, $files, $document_type)
  {
    //echo $document_type;die;
    $fileName = $document_type . '_' . time() . '_' . str_replace(" ", "_", $files['name']);
    $config = array(
      'upload_path'   => $path,
      'allowed_types' => 'gif|jpg|png|jpeg|pdf|xls|xlsx|doc|docx',
      'overwrite'     => 1,
      'file_name'    => $fileName
    );
    //print_r($config);die;
    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    if (!$this->upload->do_upload($document_type)) {
      //echo $this->upload->display_errors();die;
      $message = array('result' => $this->upload->display_errors(), 'status' => 0);
    } else {
      //echo 'success';die;
      $data = array('upload_data' => $this->upload->data());
      //print_r($data);die;
      $message = array('result' => $data['upload_data']['file_name'], 'status' => 1);
    }
    return $message;
  }
}