<?php
session_start();
if (isset($_POST['empId'])) {
  $empID = $_POST['empId'];
  $fromDate = $_POST['fromDate'];
  $toDate = $_POST['toDate'];

  $r = new Fetchdata($empID, $fromDate, $toDate);
}else{
  header("location: ../login.php");
}

class Fetchdata
{



  function __construct($empID, $fromDate, $toDate)
  {
// ******************connection need to be change in server ********************************//
$this->db=new mysqli("localhost", "root", "", "fta new");
    $this->FnDepartment($empID, $fromDate, $toDate);
  }

 // finance Department users
 function FnDepartment($empId, $fromDate, $toDate)
 {
   $sitevistOrder = 1;
    $test1 = $this->db->prepare("SELECT tesf.NAME,tesf.`SITE_VISITED_ORDER`,tesf.`EMP_ID`,tesf.`SITE_VISIT_DATE`,tesf.`SITE_ID`,tesf.`DISTANCE_(KMS)`,tesf.`USER_LAT`,tesf.`USER_LNG`,tesf.`USER_LOCATION_ADDRESS`,`tesf-2`.`TOTAL_DISTANCE`,tesf.FTA,tesf.`REPORT_SUBMISSION_STATE`,tesf.`SUPPORTING_BILLS_AVAILABLE_(Y/N)`,tesf.`MODE_OF_TRANSPORT`,tesf.REMARK,tesf.EMPLOYEE_IMAGES,tesf.SITE_LOG_IN,tesf.SITE_LOG_OUT,sitesinfo.`SITE_NAME`,sitesinfo.LATITUDE,sitesinfo.LONGITUDE,sitesinfo.PROJECT,sitesinfo.`CUSTOMER_NAME`,`tesf-2`.`ALL_BILLS_AMOUNT`,`tesf-2`.`CLAIM_STATUS`,`tesf-2`.`CLAIM_DATE`,`tesf-2`.`DEPUTATION_ALLOWANCE`,`tesf-2`.`DA_AMOUNT`,`tesf-2`.`FINAL_AMOUNT`,`tesf-2`.`CLAIM_STATUS_UPDATED_BY`,`tesf-2`.`REJECT_REASON` FROM `tesf` LEFT JOIN sitesinfo ON sitesinfo.`SITE_ID`=tesf.`SITE_ID` LEFT JOIN `tesf-2` ON `tesf-2`.`EMP_ID`= tesf.`EMP_ID` AND `tesf-2`.`SITE_VISIT_DATE`=tesf.`SITE_VISIT_DATE` AND tesf.`SITE_VISITED_ORDER`=?  WHERE tesf.`EMP_ID`=? AND tesf.`SITE_VISIT_DATE` BETWEEN ? AND ? ORDER BY tesf.`SITE_VISIT_DATE`,tesf.`SITE_VISITED_ORDER`ASC");
    $test1->bind_param("isss", $sitevistOrder, $empId, $fromDate, $toDate);
    $test1->execute();
     $test1->bind_result($tesf_name, $tesf_SiteVisitedOrder, $tesf_empid, $tesf_date, $tesf_siteID, $tesf_distance, $tesf_user_lat, $tesf_user_lng, $tesf_user_location_address, $tesf2_TotalDistance, $tesf_FTA, $tesf_report_submission_state, $tesf_SupportingBillsAvaliable, $tesf_mode_of_transport, $tesf_remark,$tesf_EmployeeImages, $tesf_logIn, $tesf_logOut, $sitesinfo_SiteName, $sitesinfo_latitude, $sitesinfo_longitude, $sitesinfo_project, $sitesinfo_CustomerName, $tesf2_allBillsAmount, $tesf2_claimstatus, $tesf2_claimdate, $tesf2_da, $tesf2_daa, $tesf2_finalamount, $tesf2_rejectedBy, $tesf2_rejectedReason);
   $test1->store_result();
   $return_arr = array();
   if ($test1->num_rows() > 0) {
     while ($test1->fetch()) {
      $return_arr[] =
         array(

           "date" => $tesf_date,
           "employeeImages" => $tesf_EmployeeImages,
           "claimstatus" => $tesf2_claimstatus,
           "userLocationAddress" => $tesf_user_location_address,
           "userLat" => $tesf_user_lat,
           "userLng" => $tesf_user_lng,
           "siteVisitedOrder" => $tesf_SiteVisitedOrder,
           "reportSubmissionState" => $tesf_report_submission_state,
           "modeOfTransport" => $tesf_mode_of_transport,
           "supportingBillsAvailable" => $tesf_SupportingBillsAvaliable,
           "from" => $tesf_logIn,
           "to" => $tesf_logOut,
           "distance" => $tesf_distance,
           "fta" => $tesf_FTA,
           "remark" => $tesf_remark,
           "sitelat" => $sitesinfo_latitude,
           "siteLong" => $sitesinfo_longitude,
           "siteID" => $tesf_siteID,
           "site" => $sitesinfo_SiteName,
           "project" => $sitesinfo_project,
           "customer" => $sitesinfo_CustomerName,
           "totalDistance" => $tesf2_TotalDistance,
           "deputation" => $tesf2_da,
           "daAmount" => $tesf2_daa,
           "hotel" => $tesf2_allBillsAmount,
           "total" => $tesf2_finalamount,
           "name" => $tesf_name,
           "id" => $tesf_empid,
           "rejectedBy" => $tesf2_rejectedBy,
           "rejectedReason" => $tesf2_rejectedReason
         );
     }


     echo  json_encode($return_arr);
   } else {
     echo json_encode($return_arr);
   }
 }

}


//$r = new Fetchdata("180291","2019-11-11","2019-12-12");
