<?php
session_start();
if (isset($_POST['getClaimStatus'])) {

  $getClaimStatus = $_POST['getClaimStatus'];
  $tempClaimStatus = $_POST['getClaimStatus'];
  $loggedInUserID = $_SESSION["userId"];
  $loggedInUserDepartment = $_SESSION['teamLeads'];
  $currentPageNo=$_POST["currentPageNo"];

  $classObject = new FetchClaimDetails($loggedInUserID, $loggedInUserDepartment, $getClaimStatus, $tempClaimStatus, $currentPageNo);
} else {
  header('location: ../login.php');
}

class FetchClaimDetails
{
  private $DB_con;

  function __construct($loggedInUserID, $loggedInUserDepartment, $getClaimStatus, $tempClaimStatus, $currentPageNo)
  {
    require_once dirname(__DIR__) . '../../php/DB_Connect.php';
    $this->DB_con = new DB_conncect(); //encapsulation
    $this->db = $this->DB_con->connectToDB();
    $per_page_record=NO_OF_RECORDS_PER_PAGE;


    // in TL rejected Tab conatins "TL rejected data and finance rejected Data"
    if ($getClaimStatus == CLAIM_TL_REJECTED && $loggedInUserDepartment == TL_DEPARTMENT) {
      $tempClaimStatus = CLAIM_FINANCE_REJECTED;
    }
        // in TL  FINANCE-APPROVED Tab conatins "Finance approved claims  data and finance paid claim Data"
        if ($getClaimStatus == CLAIM_FINANCE_APPROVED && $loggedInUserDepartment == TL_DEPARTMENT) {
          $tempClaimStatus = CLAIM_PAID;
        }

    switch ($getClaimStatus) {
      case CLAIM_PENDING:
        $sortBy = "`tesf-2`.`CLAIM_DATE` ASC";
        break;
      case CLAIM_TL_APPROVED:
        $sortBy = "`tesf-2`.`TL_APPROVED_DATE` DESC";
        break;
      case CLAIM_FINANCE_APPROVED:
        $sortBy = "`tesf-2`.`FINANCE_APPROVED_DATE` DESC";
        break;
      case CLAIM_TL_REJECTED:
        $sortBy = "`tesf-2`.`TL_OR_FINANCE_REJECTED_DATE` DESC";
        break;
      case CLAIM_FINANCE_REJECTED:
        $sortBy = "`tesf-2`.`TL_OR_FINANCE_REJECTED_DATE` DESC";
        break;  
        default:
        $sortBy ="";
      break;
    }

    $this->fetchEmployeeClaimDetails($loggedInUserID, $loggedInUserDepartment, $getClaimStatus, $tempClaimStatus, $sortBy, $currentPageNo, $per_page_record);
  }

  // finance Department users
  function fetchEmployeeClaimDetails($loggedInUserID, $loggedInUserDepartment, $getClaimStatus, $tempClaimStatus, $sortBy, $currentPageNo, $per_page_record)
  {

$start_from =($currentPageNo-1)*$per_page_record;

    //$query = "SELECT tesf.NAME,tesf.`SITE_VISITED_ORDER`,tesf.`EMP_ID`,tesf.`SITE_VISIT_DATE`,tesf.`SITE_ID`, tesf.`STAGE_OF_VISIT_OR_AUDIT`, 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`, tesf.`SITE_ID_LATITUDE`, tesf.`SITE_ID_LONGITUDE`, tesf.`SITE_ID_PROJECT`, sitesinfo.`CIRCLE`, sitesinfo.`DISTRICT`, sitesinfo.`CUSTOMER_NAME`,IF(`CLAIM_STATUS` =" . CLAIM_REIMBURSED . " OR `CLAIM_STATUS` =" . CLAIM_FINANCE_REJECTED . ", `tesf-2`.`FINANCE_APPROVED_OTHER_BILLS`,  `tesf-2`.`APPROVED_OTHER_BILLS`),IF(`CLAIM_STATUS` =" . CLAIM_REIMBURSED . " OR `CLAIM_STATUS` =" . CLAIM_FINANCE_REJECTED . ", `tesf-2`.`FINANCE_APPROVED_HOTEL_BILLS`,  `tesf-2`.`APPROVED_HOTEL_BILLS`), `tesf-2`.`CLAIM_STATUS`, `tesf-2`.`CLAIM_DATE`,IF(`CLAIM_STATUS` =" . CLAIM_REIMBURSED . " OR `CLAIM_STATUS` =" . CLAIM_FINANCE_REJECTED . ", `tesf-2`.`FINANCE_APPROVED_DEPUTATION_ALLOWANCE`,  `tesf-2`.`APPROVED_DEPUTATION_ALLOWANCE`),IF(`CLAIM_STATUS` =" . CLAIM_REIMBURSED . " OR `CLAIM_STATUS` =" . CLAIM_FINANCE_REJECTED . ", `tesf-2`.`FINANCE_APPROVED_DA_AMOUNT`,  `tesf-2`.`APPROVED_DA_AMOUNT`),IF(`CLAIM_STATUS` =" . CLAIM_REIMBURSED . " OR `CLAIM_STATUS` =" . CLAIM_FINANCE_REJECTED . ", `tesf-2`.`FINANCE_APPROVED_FINAL_AMOUNT`,  `tesf-2`.`ENGINEER_FINAL_AMOUNT`), `tesf-2`.`REJECT_REASON`, `tesf-2`.`CLAIM_STATUS_UPDATED_BY`, `tesf-2`.`TL_DEDUCTED_AMOUNT`, `tesf-2`.`FINANCE_DEDUCTED_AMOUNT`, `tesf-2`.`TL_APPROVED_DATE`, `tesf-2`.`FINANCE_APPROVED_DATE` FROM `tesf-2` LEFT JOIN tesf ON tesf.`SITE_VISIT_DATE`=`tesf-2`.`SITE_VISIT_DATE` AND tesf.`EMP_ID`=`tesf-2`.`EMP_ID` LEFT JOIN sitesinfo ON sitesinfo.`SITE_ID`=tesf.`SITE_ID` ";
    $query = "SELECT tesf.NAME,tesf.`SITE_VISITED_ORDER`,tesf.`EMP_ID`,tesf.`SITE_VISIT_DATE`,tesf.`SITE_ID`, tesf.`STAGE_OF_VISIT_OR_AUDIT`, 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`, tesf.`SITE_ID_LATITUDE`, tesf.`SITE_ID_LONGITUDE`, tesf.`SITE_ID_PROJECT`, sitesinfo.`CIRCLE`, sitesinfo.`DISTRICT`, sitesinfo.`CUSTOMER_NAME`,  `tesf-2`.`ENGINEER_OTHER_BILLS_AMOUNT`, `tesf-2`.`TL_APPROVED_OTHER_BILLS`,`tesf-2`.`FINANCE_APPROVED_OTHER_BILLS`, `tesf-2`.`ENGINEER_HOTEL_BILLS_AMOUNT`,  `tesf-2`.`TL_APPROVED_HOTEL_BILLS` , `tesf-2`.`FINANCE_APPROVED_HOTEL_BILLS`, `tesf-2`.`CLAIM_STATUS`, `tesf-2`.`CLAIM_DATE`, `tesf-2`.`ENGINEER_DEPUTATION_ALLOWANCE`, `tesf-2`.`TL_APPROVED_DEPUTATION_ALLOWANCE`,`tesf-2`.`FINANCE_APPROVED_DEPUTATION_ALLOWANCE`, `tesf-2`.`ENGINEER_DA_AMOUNT`, `tesf-2`.`TL_APPROVED_DA_AMOUNT`,  `tesf-2`.`FINANCE_APPROVED_DA_AMOUNT`, `tesf-2`.`ENGINEER_FINAL_AMOUNT`, `tesf-2`.`TL_APPROVED_FINAL_AMOUNT`, `tesf-2`.`FINANCE_APPROVED_FINAL_AMOUNT`, `tesf-2`.`REJECT_REASON`, `tesf-2`.`CLAIM_STATUS_UPDATED_BY`, `tesf-2`.`TL_DEDUCTED_AMOUNT`, `tesf-2`.`FINANCE_DEDUCTED_AMOUNT`, `tesf-2`.`TL_APPROVED_DATE`, `tesf-2`.`FINANCE_APPROVED_DATE`, `tesf-2`.`TL_NAME`, `tesf-2`.`TL_EMP_ID`, `tesf-2`.`TL_DEDUCTED_REASON`, `tesf-2`.`FINANCE_DEDUCTED_REASON`, `tesf-2`.`TL_OR_FINANCE_REJECTED_DATE`, `tesf-2`.`TOTAL_DEDUCTED_AMOUNT`, `tesf-2`.`PAID_DATE`, `tesf-2`.`PAID_BY`  FROM `tesf-2` LEFT JOIN tesf ON tesf.`SITE_VISIT_DATE`=`tesf-2`.`SITE_VISIT_DATE` AND tesf.`EMP_ID`=`tesf-2`.`ENGINEER_EMP_ID` LEFT JOIN sitesinfo ON sitesinfo.`SITE_ID`=tesf.`SITE_ID` ";

    if ($loggedInUserDepartment == TL_DEPARTMENT) {
      $test1 = $this->db->prepare($query . "  WHERE (`tesf-2`.`CLAIM_STATUS`=? OR `tesf-2`.`CLAIM_STATUS`=?) AND `tesf-2`.`TL_EMP_ID`=? ORDER BY " . $sortBy . ", `tesf-2`.`SITE_VISIT_DATE` ASC,`tesf-2`.`ENGINEER_EMP_ID` ASC LIMIT ?,?");
      $test1->bind_param("iisii", $getClaimStatus, $tempClaimStatus, $loggedInUserID, $start_from, $per_page_record);
      $test1->execute();
    } elseif ($loggedInUserDepartment == FINANCE_DEPARTMENT) {
      if ($getClaimStatus == CLAIM_TL_APPROVED) { //ONLY APPROVED TAB
        $test1 = $this->db->prepare($query . " WHERE `tesf-2`.`CLAIM_STATUS`=? ORDER BY `tesf-2`.`TL_APPROVED_DATE` ASC, `tesf-2`.`SITE_VISIT_DATE` ASC,`tesf-2`.`ENGINEER_EMP_ID` ASC LIMIT ?,?");
        $test1->bind_param("iii", $getClaimStatus, $start_from, $per_page_record);
        $test1->execute();
      } else {
        $test1 = $this->db->prepare($query . " WHERE `tesf-2`.`CLAIM_STATUS`=? AND `tesf-2`.`FINANCE_EMP_ID`=? ORDER BY  `tesf-2`.`FINANCE_APPROVED_DATE` DESC, `tesf-2`.`SITE_VISIT_DATE` ASC, `tesf-2`.`ENGINEER_EMP_ID` ASC LIMIT ?,?");
        $test1->bind_param("isii", $getClaimStatus, $loggedInUserID, $start_from, $per_page_record);
        $test1->execute();
      }
    }


    $test1->bind_result($tesf_name, $tesf_SiteVisitedOrder, $tesf_empid, $tesf_date, $tesf_siteID, $tesf_stageOfvisit, $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_Circle, $sitesinfo_District, $sitesinfo_CustomerName, $tesf2_engineerOtherBillsAmount, $tesf2_TLApprovedOtherBillsAmount, $tesf2_FinanceApprovedOtherBillsAmount, $tesf2_engineerHotelBillsAmount, $tesf2_TLApprovedHotelBillsAmount, $tesf2_FinanceApprovedHotelBillsAmount, $tesf2_claimstatus, $tesf2_claimdate, $tesf2_engineerDA, $tesf2_TLApprovedDA, $tesf2_financeApprovedDA, $tesf2_engineerDAAmount, $tesf2_TLApprovedDAAmount, $tesf2_financeApprovedDAAmount, $tesf2_engineerFinalamount, $tesf2_TLApprovedFinalAmount, $tesf2_FinanceApprovedFinalAmount, $tesf2_rejectReason, $tesf2_claimStatusUpdatedBy, $tesf2_TLDeductedAmount, $tesf2_financeDeductedAmount, $tesf2_tlApproved_date, $tesf2_financeApproved_date, $tesf2_TLName, $tesf2_TLEmpId, $tesf2_TLDeductedReason, $tesf2_financeDeductedReason, $tesf2_tlOrFinanceRejectedDate, $tesf2TotalDeductedAmount, $tesf2PaidDate, $tesf2PaidBy);
    $test1->store_result();
    $mainArray = array();
    if ($test1->num_rows() > 0) {
      while ($test1->fetch()) {

        $return_arr["date"] = $tesf_date;
        $return_arr["employeeImages"] = $tesf_EmployeeImages;
        $return_arr["claimstatus"] = $tesf2_claimstatus;
        $return_arr["claimdate"] = $tesf2_claimdate;
        $return_arr["userLocationAddress"] = $tesf_user_location_address;
        $return_arr["userLat"] = $tesf_user_lat;
        $return_arr["userLng"] = $tesf_user_lng;
        $return_arr["siteVisitedOrder"] = $tesf_SiteVisitedOrder;
        $return_arr["reportSubmissionState"] = $tesf_report_submission_state;
        $return_arr["modeOfTransport"] = $tesf_mode_of_transport;
        $return_arr["supportingBillsAvailable"] = $tesf_SupportingBillsAvaliable;
        $return_arr["from"] = $tesf_logIn;
        $return_arr["to"] = $tesf_logOut;
        $return_arr["distance"] = $tesf_distance;
        $return_arr["fta"] = $tesf_FTA;
        $return_arr["remark"] = $tesf_remark;
        $return_arr["sitelat"] = $sitesinfo_latitude;
        $return_arr["siteLong"] = $sitesinfo_longitude;
        $return_arr["siteID"] = $tesf_siteID;
        $return_arr["siteCircle"] = $sitesinfo_Circle;
        $return_arr["siteDistrict"] = $sitesinfo_District;
        $return_arr["stageOfvisit"] = $tesf_stageOfvisit;
        $return_arr["siteName"] = $sitesinfo_SiteName;
        $return_arr["project"] = $sitesinfo_project;
        $return_arr["customer"] = $sitesinfo_CustomerName;
        $return_arr["totalDistance"] = $tesf2_TotalDistance;

        $return_arr["engDeputation"] = $tesf2_engineerDA;
        $return_arr["TLDeputation"] = $tesf2_TLApprovedDA;
        $return_arr["financeDeputation"] = $tesf2_financeApprovedDA;

        $return_arr["engDAAmount"] = $tesf2_engineerDAAmount;
        $return_arr["TLDAAmount"] = $tesf2_TLApprovedDAAmount;
        $return_arr["financeDAAmount"] = $tesf2_financeApprovedDAAmount;

        $return_arr["engOtherBill"] = $tesf2_engineerOtherBillsAmount;
        $return_arr["TLOtherBill"] = $tesf2_TLApprovedOtherBillsAmount;
        $return_arr["financeOtherBill"] = $tesf2_FinanceApprovedOtherBillsAmount;

        $return_arr["engHotelBill"] = $tesf2_engineerHotelBillsAmount;
        $return_arr["TLHotelBill"] = $tesf2_TLApprovedHotelBillsAmount;
        $return_arr["financeHotelBill"] = $tesf2_FinanceApprovedHotelBillsAmount;

        $return_arr["engTotalAmount"] =  $tesf2_engineerFinalamount;
        $return_arr["TLTotalAmount"] =  $tesf2_TLApprovedFinalAmount;
        $return_arr["financeTotalAmount"] =  $tesf2_FinanceApprovedFinalAmount;

        $return_arr["empName"] = $tesf_name;
        $return_arr["empId"] = $tesf_empid;
        $return_arr["claimStatusUpdatedBy"] = $tesf2_claimStatusUpdatedBy;
        $return_arr["rejectReason"] = $tesf2_rejectReason;
        $return_arr["tlApprovedDate"] = $tesf2_tlApproved_date;
        $return_arr["financeApprovedDate"] = $tesf2_financeApproved_date;
        $return_arr["tlDeductedAmount"] = $tesf2_TLDeductedAmount;
        $return_arr["financeDeductedAmount"] = $tesf2_financeDeductedAmount;
        $return_arr["totalDeductedAmount"] = $tesf2TotalDeductedAmount;
        $return_arr["approvedTL"]= $tesf2_TLName.",".$tesf2_TLEmpId;
        $return_arr["TLDeductedReason"]= $tesf2_TLDeductedReason;
        $return_arr["financeDeductedReason"]=$tesf2_financeDeductedReason;
        $return_arr["tlOrFinanceRejectedDate"] = $tesf2_tlOrFinanceRejectedDate;
        $return_arr["paidDate"]=$tesf2PaidDate;
        $return_arr["paidBy"]=$tesf2PaidBy;

        array_push($mainArray, $return_arr);
      }


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

//$r = new FetchClaimDetails($empID, $getClaimStatus);

//$r = new FetchClaimDetails("18029111", "1");
?>