<?php



// FILE FOR AUTHENTICATION 

/*

All possible functions

- login

- logout

- forgotpassword

- signup

- emailverify

*/



if ($_SERVER['REQUEST_METHOD'] == 'GET') {

  $action = $_GET['action'];



  include('config.php');

  include('common_functions.php');

  include('common_functions_app.php');



  switch ($action) {



    case 'analytic_report_users': {

        getAnalyticReportUsers($_GET);

        break;
      } 

    


     
       
 

     

    

     

    

    


   
   
        
   
   










    default:

      $record = array('success' => false, 'msg' => 'Invalid Action', 'data' => array());

      jsonSendEncode($record);
  }
}



if ($_SERVER['REQUEST_METHOD'] == 'POST') {

  $action = $_POST['action'];



  include('config.php');

  include('common_functions.php');

  include('common_functions_app.php');



  switch ($action) {





    case 'show_tabular_user_report': {
        ShowTabularUserReport($_POST);
        break;
      }

       case 'show_tabular_inspection_history_report': {
        ShowTabularUserInspectionHistoryReport($_POST);
        break;
      }

      

      

      

     

      

     

     

      
       
   
   
    
    
   
  



   

   

    
   
 











    default:

      include_once('language_message.php');

      $record = array('success' => false, 'msg' => $msgValidAction, 'data' => array());

      jsonSendEncode($record);
  }
}





function getAnalyticReportUsers($data)
{

  global $mysqli;

  include_once('language_message.php');



  //print_r($data);



  if (empty($data['type'])) {

    $record = array('success' => false, 'msg' => $msgEmptyAll, 'data' => array());

    jsonSendEncode($record);
  } else {

    $type     = $data['type'];



    $month_arr = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');



    if ($type == 'monthly') {

      for ($i = 0; $i < count($month_arr); $i++) {

        $current_year   =   date('Y');



        $month_text = $month_arr[$i];

        $month_number = $i + 1;



        $check_user_report = $mysqli->prepare("SELECT user_id FROM user_master where  user_type='1'  AND delete_flag=0 AND profile_complete = 1   AND YEAR(createtime) = ? AND  MONTH(createtime) = ?");

        $check_user_report->bind_param("ss", $current_year, $month_number);

        $check_user_report->execute();

        $check_user_report->store_result();

        $check_user_report_all = $check_user_report->num_rows;  //0 1



        $total_user = $check_user_report_all;

        $report_arr[] = array('month' => $month_text, 'users' => $total_user);
      }
    } else if ($type == 'yearly') {

      for ($i = date("Y") - 5; $i <= date("Y"); $i++) {

        $current_year   =   $i;



        $check_user_report = $mysqli->prepare("SELECT user_id FROM user_master where  user_type='1'  AND delete_flag=0 AND profile_complete = 1 AND YEAR(createtime) = ? ");

        $check_user_report->bind_param("s", $current_year);

        $check_user_report->execute();

        $check_user_report->store_result();

        $check_user_report_all = $check_user_report->num_rows;  //0 1



        $total_user = $check_user_report_all;

        $report_arr[] = array('year' => $current_year, 'users' => $total_user);
      }
    }



    $record = array('success' => true, 'msg' => $msgDataFound, 'data' => $report_arr);

    jsonSendEncode($record);
  }
}









































function ShowTabularUserReport($data)
{
  global $mysqli;
  global $onerror;
  global $country_code;
  include_once('language_message.php');

  // print_r($data);
  if (empty($data['s_date']) || empty($data['e_date'])) {
    $record = array('success' => false, 'msg' => $msgEmptyAll, 'data' => array());
    jsonSendEncode($record);
  } else {
    $srno = 1;
    $s_date = $data['s_date'];
    $e_date = $data['e_date'] . ' 23:59:59';

    $start_date = date('Y-m-d 01:00:00', strtotime($s_date));
    $end_date = date('Y-m-d 23:59:59', strtotime($e_date));



    // echo $end_date;
    // echo $start_date;
    // exit(); 

    $user_arr = array();
    $sno = 1;
    // echo "SELECT user_id,f_name,l_name,`bio`, `innocence_project_id`, `lead_attorney`, `acknowledge_box`,name,email,mobile,image, active_flag,createtime from user_master where delete_flag='0'and user_type = '1' and profile_complete=1 and active_flag = 1 and createtime BETWEEN $start_date AND $end_date order by user_id desc";
    // die();
    $sql = "SELECT user_id,name,mobile,email,image, active_flag,createtime,phone_code,user_type,role_id from user_master where delete_flag='0'and user_type =1 and profile_complete=1  and createtime BETWEEN ? AND ? order by user_id desc";
    $check_data = $mysqli->prepare($sql);
    //$result->bind_param('i',$vendor_id);
    $check_data->bind_param('ss', $start_date, $end_date);

    $check_data->execute();
    $check_data->store_result();
    $check_data_count = $check_data->num_rows;

    if ($check_data_count > 0) {
      $check_data->bind_result($user_id,  $username, $mobile,$email, $image, $active_flag, $createtime,$phone_code,$user_type,$role);
      while ($check_data->fetch()) {
        
        $createtime = date('d-m-Y h:i A', strtotime($createtime));
        //$countrycode = getCountryCode($country_id);
        //$user_id = encode($user_id);
        $image_html = '<img  width="50px" src="../webservice/images/' . $image . '" class="rounded-circle circle-border m-b-md img-fix img-responsive" name="profile_image" id="previewholder" style="width: 70px; height: 70px;" alt="" onerror="this.src=' . $onerror . ';" >';

        $getRole =getRole($role);
        $role_text ='<label class="badge badge-primary">'.$getRole.'</label>';

       


        if ($active_flag == '1') {
          $active_flag_text = '0';
          $active_flag_html = ' <span class="badge rounded-pill bg-success inv-badge">Activate</span>';
        } else {
          $active_flag_text = '1';
          $active_flag_html = '<span class="badge rounded-pill bg-danger inv-badge">Deactivate</span>';
        }

        if ($user_type == '1') {
          
          $user_flag_html = ' <span class="badge rounded-pill bg-success inv-badge">Business</span>';
        } else {
          
          $user_flag_html = '<span class="badge rounded-pill bg-info inv-badge">Trade Association</span>';
        }
        if (empty($email)) {
            $email ='NA';
        }
        if (empty($mobile)) {
            $mobile ='NA';
        }else{
          $mobile =$mobile;
        }


        $action_html = '

        <div class="btn-group">

                                                    <button type="button" class="btn btn-primary btn-outline  dropdown-toggle float-button-light" id="primary-dropdown-outline" data-bs-toggle="dropdown" aria-expanded="false">

                                                        Action

                                                    </button>

                                                    <ul class="dropdown-menu dropdown-menu-primary" style=" margin: 2px -56px 0;" aria-labelledby="primary-dropdown" role="menu">

                                                        <li><a href="view_user.php?id=' . encode($user_id) . '"" class="dropdown-item" target="_blank"><i class="fa fa-eye"></i>&nbsp;View</a></li>

                                                           


                                                        

                                                          

                                                

                                                    </ul>

                                                </div>




      
';
        $user_arr[] =array('srno' => $srno, 'user_id' => $user_id, 'mobile' => $mobile, 'active_flag' => $active_flag_html, 'action' => $action_html, 'createtime' => $createtime, 'username' => $username,'user_type'=>$user_flag_html,'email'=>$email,'image'=>$image_html,'role_text'=>$role_text);
        $srno++;
      }
    }

    if (empty($user_arr)) {
      $user_arr = array();
    }

    $record = array('success' => true, 'msg' => $msgDataFound, 'user_arr' => $user_arr);
    jsonSendEncode($record);
  }
}




function ShowTabularUserInspectionHistoryReport($data)
{
  global $mysqli;
  global $onerror;
  global $country_code;
  include_once('language_message.php');

  // print_r($data);
  if (empty($data['s_date']) || empty($data['e_date'])) {
    $record = array('success' => false, 'msg' => $msgEmptyAll, 'data' => array());
    jsonSendEncode($record);
  } else {
    $srno = 1;
    $s_date = $data['s_date'];
    $e_date = $data['e_date'] . ' 23:59:59';

    $start_date = date('Y-m-d 01:00:00', strtotime($s_date));
    $end_date = date('Y-m-d 23:59:59', strtotime($e_date));



    // echo $end_date;
    // echo $start_date;
    // exit(); 

    $user_arr = array();
    $sno = 1;
    // echo "SELECT user_id,f_name,l_name,`bio`, `innocence_project_id`, `lead_attorney`, `acknowledge_box`,name,email,mobile,image, active_flag,createtime from user_master where delete_flag='0'and user_type = '1' and profile_complete=1 and active_flag = 1 and createtime BETWEEN $start_date AND $end_date order by user_id desc";
    // die();
    $sql = "SELECT `inspection_request_id`, `inspection_id`, `user_id`, `inspection_type_id`, `inspection_title`, `inspection_date`,createtime from inspection_request_master where delete_flag='0'  and createtime BETWEEN ? AND ? order by inspection_request_id desc";
    $check_data = $mysqli->prepare($sql);
    //$result->bind_param('i',$vendor_id);
    $check_data->bind_param('ss', $start_date, $end_date);

    $check_data->execute();
    $check_data->store_result();
    $check_data_count = $check_data->num_rows;

    if ($check_data_count > 0) {
      $check_data->bind_result( $inspection_request_id, $inspection_id, $user_id, $inspection_type_id, $inspection_title, $inspection_date,$createtime );
      while ($check_data->fetch()) {
        
        $createtime = date('d-m-Y h:i A', strtotime($createtime));
        $inspection_date =date('d-m-Y',strtotime($inspection_date));
  
        

       


        $inspection_type_name =getInspectionTypeName($inspection_type_id);

        // $getAllInspectionDueDate =getAllInspectionRequestDueDate($inspection_request_id);
        // $getInspectionDueDate =getInspectionRequestDueDate($inspection_request_id);
       
         $userData = getUserDetails($user_id);
         if($userData != 'NA'){
              $userName =$userData['name'];
              $userMobile =$userData['mobile'];
              $userEmail =$userData['email'];
              $userImage =$userData['image'];
              $image_html = '<img  width="50px" src="../webservice/images/' . $userImage . '" class="rounded-circle circle-border m-b-md img-fix img-responsive" name="profile_image" id="previewholder" style="width: 70px; height: 70px;" alt="" onerror="this.src=' . $onerror . ';" >';
         }else{
           $userName ='NA';
             $userMobile ='NA';
              $userEmail ='NA';
              $image_html ='NA';
         }


        $action_html = '

        <div class="btn-group">

                                                    <button type="button" class="btn btn-primary btn-outline  dropdown-toggle float-button-light" id="primary-dropdown-outline" data-bs-toggle="dropdown" aria-expanded="false">

                                                        Action

                                                    </button>

                                                    <ul class="dropdown-menu dropdown-menu-primary" style=" margin: 2px -56px 0;" aria-labelledby="primary-dropdown" role="menu">

                                                        <li><a href="view_inspection_history.php?id=' . encode($inspection_request_id) . '"" class="dropdown-item" target="_blank"><i class="fa fa-eye"></i>&nbsp;View</a></li>

                                                           


                                                        

                                                          

                                                

                                                    </ul>

                                                </div>




      
';
        $user_arr[] =array('srno' => $srno,  'action' => $action_html, 'createtime' => $createtime,'userName'=>$userName,'userEmail'=>$userEmail,'userMobile'=>$userMobile,'userImage'=>$userImage,'inspection_title'=>$inspection_title,'inspection_type_name'=>$inspection_type_name,'inspection_date'=>$inspection_date);
        $srno++;
      }
    }

    if (empty($user_arr)) {
      $user_arr = array();
    }

    $record = array('success' => true, 'msg' => $msgDataFound, 'user_arr' => $user_arr);
    jsonSendEncode($record);
  }
}


































































