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

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

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

class FetchClaimDetails
{
  private $DB_con;

  //function __construct($loggedInUserID, $loggedInUserDepartment, $getClaimStatus, $tempClaimStatus, $currentPageNo)
  function __construct($loggedInUserID, $loggedInUserDepartment, $getClaimStatus, $tempClaimStatus, $fromDate, $toDate)
  {
    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 = "`normal_site_visit_2`.`CLAIM_DATE` ASC";
        break;
      case CLAIM_TL_APPROVED:
        $sortBy = "`normal_site_visit_2`.`TL_APPROVED_DATE` DESC";
        break;
      case CLAIM_FINANCE_APPROVED:
        $sortBy = "`normal_site_visit_2`.`FINANCE_APPROVED_DATE` DESC";
        break;
      case CLAIM_TL_REJECTED:
        $sortBy = "`normal_site_visit_2`.`TL_OR_FINANCE_REJECTED_DATE` DESC";
        break;
      case CLAIM_FINANCE_REJECTED:
        $sortBy = "`normal_site_visit_2`.`TL_OR_FINANCE_REJECTED_DATE` DESC";
        break;
        default:
        $sortBy ="";
      break;
    }*/

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

  // finance Department users
  //function fetchEmployeeClaimDetails($loggedInUserID, $loggedInUserDepartment, $getClaimStatus, $tempClaimStatus, $sortBy, $currentPageNo, $per_page_record)
  function fetchEmployeeClaimDetails($loggedInUserID, $loggedInUserDepartment, $getClaimStatus, $tempClaimStatus, $fromDate, $toDate)
  {
//$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`,`normal_site_visit_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 . ", `normal_site_visit_2`.`FINANCE_APPROVED_OTHER_BILLS`,  `normal_site_visit_2`.`APPROVED_OTHER_BILLS`),IF(`CLAIM_STATUS` =" . CLAIM_REIMBURSED . " OR `CLAIM_STATUS` =" . CLAIM_FINANCE_REJECTED . ", `normal_site_visit_2`.`FINANCE_APPROVED_HOTEL_BILLS`,  `normal_site_visit_2`.`APPROVED_HOTEL_BILLS`), `normal_site_visit_2`.`CLAIM_STATUS`, `normal_site_visit_2`.`CLAIM_DATE`,IF(`CLAIM_STATUS` =" . CLAIM_REIMBURSED . " OR `CLAIM_STATUS` =" . CLAIM_FINANCE_REJECTED . ", `normal_site_visit_2`.`FINANCE_APPROVED_DEPUTATION_ALLOWANCE`,  `normal_site_visit_2`.`APPROVED_DEPUTATION_ALLOWANCE`),IF(`CLAIM_STATUS` =" . CLAIM_REIMBURSED . " OR `CLAIM_STATUS` =" . CLAIM_FINANCE_REJECTED . ", `normal_site_visit_2`.`FINANCE_APPROVED_DA_AMOUNT`,  `normal_site_visit_2`.`APPROVED_DA_AMOUNT`),IF(`CLAIM_STATUS` =" . CLAIM_REIMBURSED . " OR `CLAIM_STATUS` =" . CLAIM_FINANCE_REJECTED . ", `normal_site_visit_2`.`FINANCE_APPROVED_FINAL_AMOUNT`,  `normal_site_visit_2`.`ENGINEER_FINAL_AMOUNT`), `normal_site_visit_2`.`REJECT_REASON`, `normal_site_visit_2`.`CLAIM_STATUS_UPDATED_BY`, `normal_site_visit_2`.`TL_DEDUCTED_AMOUNT`, `normal_site_visit_2`.`FINANCE_DEDUCTED_AMOUNT`, `normal_site_visit_2`.`TL_APPROVED_DATE`, `normal_site_visit_2`.`FINANCE_APPROVED_DATE` FROM `normal_site_visit_2` LEFT JOIN tesf ON tesf.`SITE_VISIT_DATE`=`normal_site_visit_2`.`SITE_VISIT_DATE` AND tesf.`EMP_ID`=`normal_site_visit_2`.`EMP_ID` LEFT JOIN sitesinfo ON sitesinfo.`SITE_ID`=tesf.`SITE_ID` ";
    $query = "SELECT `normal_site_visit`.ID, `normal_site_visit`.`ENGINEER_EMP_NAME`,`normal_site_visit`.`SITE_VISIT_ORDER`,`normal_site_visit`.`ENGINEER_EMP_ID`,`normal_site_visit`.`SITE_VISIT_DATE`, `normal_site_visit`.`DISTANCE_(KMS)`,`normal_site_visit`.`STARTING_LOCATION`, `normal_site_visit`.`DESTINATION_LOCATION`,"
    ."`normal_site_visit_2`.`TOTAL_DISTANCE`, `normal_site_visit_2`.`ENGINEER_OTHER_BILLS_AMOUNT`, `normal_site_visit_2`.`TL_APPROVED_OTHER_BILLS_AMOUNT`,`normal_site_visit_2`.`FINANCE_APPROVED_OTHER_BILLS_AMOUNT`, `normal_site_visit_2`.`ENGINEER_HOTEL_BILLS_AMOUNT`,  `normal_site_visit_2`.`TL_APPROVED_HOTEL_BILLS_AMOUNT` ,"
    ."`normal_site_visit_2`.`FINANCE_APPROVED_HOTEL_BILLS_AMOUNT`, COALESCE(`normal_site_visit_2`.`CLAIM_STATUS`,0), `normal_site_visit_2`.`CLAIM_DATE`, `normal_site_visit_2`.`ENGINEER_DEPUTATION_ALLOWANCE`, `normal_site_visit_2`.`TL_APPROVED_DEPUTATION_ALLOWANCE`,`normal_site_visit_2`.`FINANCE_APPROVED_DEPUTATION_ALLOWANCE`,"
    ."`normal_site_visit_2`.`ENGINEER_DA_AMOUNT`, `normal_site_visit_2`.`TL_APPROED_DA_AMOUNT`,  `normal_site_visit_2`.`FINANCE_APPROVED_DA_AMOUNT`, `normal_site_visit_2`.`ENGINEER_FINAL_AMOUNT`, `normal_site_visit_2`.`TL_APPROVED_FINAL_AMOUNT`, `normal_site_visit_2`.`FINANCE_APPROVED_FINAL_AMOUNT`, "
    ."`normal_site_visit_2`.`REJECT_REASON`, `normal_site_visit_2`.`CLAIM_STATUS_UPDATED_BY`, `normal_site_visit_2`.`TL_DEDUCTED_AMOUNT`, `normal_site_visit_2`.`FINANCE_DEDUCTED_AMOUNT`, `normal_site_visit_2`.`TL_APPROVED_DATE`, `normal_site_visit_2`.`FINANCE_APPROVED_DATE`, `normal_site_visit_2`.`TL_NAME`,"
    ."`normal_site_visit_2`.`TL_EMP_ID`, `normal_site_visit_2`.`TL_DEDUCTED_REASON`, `normal_site_visit_2`.`FINANCE_DEDUCTED_REASON`, `normal_site_visit_2`.`TL_OR_FINANCE_REJECTED_DATE`, `normal_site_visit_2`.`TOTAL_DEDUCTED_AMOUNT`, `normal_site_visit_2`.`PAID_DATE`, `normal_site_visit_2`.`PAID_BY`,`emp_on_of`.DAY_VISITS_COUNT "
    ." FROM `normal_site_visit` LEFT JOIN `normal_site_visit_2` ON `normal_site_visit_2`.`UNIQUE_ID`=CONCAT(`normal_site_visit`.`ENGINEER_EMP_ID`,'_',`normal_site_visit`.`SITE_VISIT_DATE`)"
    ." LEFT JOIN (SELECT ENGINEER_EMP_ID,SITE_VISIT_DATE,COUNT(*) AS DAY_VISITS_COUNT FROM normal_site_visit GROUP BY `normal_site_visit`.ENGINEER_EMP_ID,`normal_site_visit`.`SITE_VISIT_DATE`) AS `emp_on_of` ON `normal_site_visit`.`ENGINEER_EMP_ID`=`emp_on_of`.`ENGINEER_EMP_ID` AND `normal_site_visit`.`SITE_VISIT_DATE`=`emp_on_of`.SITE_VISIT_DATE";


    if ($loggedInUserDepartment == TL_DEPARTMENT) {
      $test1 = $this->db->prepare($query . "  WHERE  `normal_site_visit_2`.`TL_EMP_ID`=? AND (`normal_site_visit_2`.`CLAIM_STATUS`=? OR `normal_site_visit_2`.`CLAIM_STATUS`=?) AND `normal_site_visit_2`.`SITE_VISIT_DATE` BETWEEN ? AND ? ");
     // $test1->bind_param("iisii", $getClaimStatus, $tempClaimStatus, $loggedInUserID, $start_from, $per_page_record);
      $test1->bind_param("siiss", $loggedInUserID, $getClaimStatus,  $tempClaimStatus, $fromDate, $toDate);//where(claim-status=3,claim-status=5)
      $test1->execute();
    } elseif ($loggedInUserDepartment == FINANCE_DEPARTMENT) {
      if ($getClaimStatus == CLAIM_TL_APPROVED) { //ONLY APPROVED TAB
        $test1 = $this->db->prepare($query . " WHERE  `normal_site_visit`.`SITE_VISIT_DATE` BETWEEN ? AND ?");
        //$test1->bind_param("iii", $getClaimStatus, $start_from, $per_page_record);
        $test1->bind_param("ss", $fromDate, $toDate);
        $test1->execute();
      } else {
        $test1 = $this->db->prepare($query . " WHERE `normal_site_visit_2`.`FINANCE_EMP_ID`=?  AND `normal_site_visit_2`.`CLAIM_STATUS`=? AND `normal_site_visit_2`.`SITE_VISIT_DATE` BETWEEN ? AND ?");
        //$test1->bind_param("isii", $getClaimStatus, $loggedInUserID, $start_from, $per_page_record);
        $test1->bind_param("siss",$loggedInUserID, $getClaimStatus, $fromDate, $toDate);
        $test1->execute();
      }
    }



    $test1->bind_result($normal_site_visit_id, $normalSiteVisit_name, $normalSiteVisit_SiteVisitedOrder, $normalSiteVisit_empid, $normalSiteVisit_date, $normalSiteVisit_distance, $normalSiteVisit_Starting_location, $normalSiteVisit_Destination_location,
                        $normalSiteVisit2_TotalDistance, $normalSiteVisit2_engineerOtherBillsAmount, $normalSiteVisit2_TLApprovedOtherBillsAmount, $normalSiteVisit2_FinanceApprovedOtherBillsAmount, $normalSiteVisit2_engineerHotelBillsAmount, $normalSiteVisit2_TLApprovedHotelBillsAmount,
                        $normalSiteVisit2_FinanceApprovedHotelBillsAmount, $normalSiteVisit2_claimstatus, $normalSiteVisit2_claimdate, $normalSiteVisit2_engineerDA, $normalSiteVisit2_TLApprovedDA, $normalSiteVisit2_financeApprovedDA, 
                        $normalSiteVisit2_engineerDAAmount, $normalSiteVisit2_TLApprovedDAAmount, $normalSiteVisit2_financeApprovedDAAmount, $normalSiteVisit2_engineerFinalamount, $normalSiteVisit2_TLApprovedFinalAmount, $normalSiteVisit2_FinanceApprovedFinalAmount,
                        $normalSiteVisit2_rejectReason, $normalSiteVisit2_claimStatusUpdatedBy, $normalSiteVisit2_TLDeductedAmount, $normalSiteVisit2_financeDeductedAmount, $normalSiteVisit2_tlApproved_date, $normalSiteVisit2_financeApproved_date, $normalSiteVisit2_TLName, 
                        $normalSiteVisit2_TLEmpId, $normalSiteVisit2_TLDeductedReason, $normalSiteVisit2_financeDeductedReason, $normalSiteVisit2_tlOrFinanceRejectedDate, $normalSiteVisit2_TotalDeductedAmount, $normalSiteVisit2_PaidDate, $normalSiteVisit2_PaidBy, $empOnOfDayVisitCount);
    $test1->store_result();
    $mainArray = array();
    if ($test1->num_rows() > 0) {
      while ($test1->fetch()) {
          
        $return_arr["normalSiteVisitId"] = $normal_site_visit_id;
        $return_arr["date"] = $normalSiteVisit_date;//
        $return_arr["claimstatus"] = $normalSiteVisit2_claimstatus;
        $return_arr["claimdate"] = $normalSiteVisit2_claimdate;
        $return_arr["startingLocationAddress"] = $normalSiteVisit_Starting_location;
        $return_arr["destinationLocationAddress"] = $normalSiteVisit_Destination_location;
         $return_arr["siteVisitedOrder"] = $normalSiteVisit_SiteVisitedOrder;
        $return_arr["distance"] = $normalSiteVisit_distance;
        $return_arr["totalDistance"] = $normalSiteVisit2_TotalDistance;

        $return_arr["engDeputation"] = $normalSiteVisit2_engineerDA;
        $return_arr["TLDeputation"] = $normalSiteVisit2_TLApprovedDA;
        $return_arr["financeDeputation"] = $normalSiteVisit2_financeApprovedDA;

        $return_arr["engDAAmount"] = $normalSiteVisit2_engineerDAAmount;
        $return_arr["TLDAAmount"] = $normalSiteVisit2_TLApprovedDAAmount;
        $return_arr["financeDAAmount"] = $normalSiteVisit2_financeApprovedDAAmount;

        $return_arr["engOtherBill"] = $normalSiteVisit2_engineerOtherBillsAmount;
        $return_arr["TLOtherBill"] = $normalSiteVisit2_TLApprovedOtherBillsAmount;
        $return_arr["financeOtherBill"] = $normalSiteVisit2_FinanceApprovedOtherBillsAmount;

        $return_arr["engHotelBill"] = $normalSiteVisit2_engineerHotelBillsAmount;
        $return_arr["TLHotelBill"] = $normalSiteVisit2_TLApprovedHotelBillsAmount;
        $return_arr["financeHotelBill"] = $normalSiteVisit2_FinanceApprovedHotelBillsAmount;

        $return_arr["engTotalAmount"] =  $normalSiteVisit2_engineerFinalamount;
        $return_arr["TLTotalAmount"] =  $normalSiteVisit2_TLApprovedFinalAmount;
        $return_arr["financeTotalAmount"] =  $normalSiteVisit2_FinanceApprovedFinalAmount;

        $return_arr["empName"] = $normalSiteVisit_name;
        $return_arr["empId"] = $normalSiteVisit_empid;
        $return_arr["claimStatusUpdatedBy"] = $normalSiteVisit2_claimStatusUpdatedBy;
        $return_arr["rejectReason"] = $normalSiteVisit2_rejectReason;
        $return_arr["tlApprovedDate"] = $normalSiteVisit2_tlApproved_date;
        $return_arr["financeApprovedDate"] = $normalSiteVisit2_financeApproved_date;
        $return_arr["tlDeductedAmount"] = $normalSiteVisit2_TLDeductedAmount;
        $return_arr["financeDeductedAmount"] = $normalSiteVisit2_financeDeductedAmount;
        $return_arr["totalDeductedAmount"] = $normalSiteVisit2_TotalDeductedAmount;
        $return_arr["approvedTL"]= $normalSiteVisit2_TLName.",".$normalSiteVisit2_TLEmpId;
        $return_arr["TLDeductedReason"]= $normalSiteVisit2_TLDeductedReason;
        $return_arr["financeDeductedReason"]=$normalSiteVisit2_financeDeductedReason;
        $return_arr["tlOrFinanceRejectedDate"] = $normalSiteVisit2_tlOrFinanceRejectedDate;
        $return_arr["paidDate"]=$normalSiteVisit2_PaidDate;
        $return_arr["paidBy"]=$normalSiteVisit2_PaidBy;
        $return_arr["siteOnOfTheDayVisitCount"]=$empOnOfDayVisitCount;
        array_push($mainArray, $return_arr);
      }


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

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

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