<?php
session_start();
if (isset($_POST['getClaimStatus'])) {
  $empID = $_POST['searchEmpId'];
  $fromDate = $_POST['Fromdate'];
  $ToDate = $_POST['Todate'];
  $getClaimStatus = $_POST['getClaimStatus'];
  $tempClaimStatus = $_POST['getClaimStatus'];
  $loggedInUserID = $_SESSION["userId"];
  $loggedInUserDepartment = $_SESSION['teamLeads'];
  //$currentPageNo=$_POST["currentPageNo"];

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

class FetchClaimDetails
{
  private $DB_con;

  function __construct($loggedInUserID, $loggedInUserDepartment, $getClaimStatus, $tempClaimStatus, $empID, $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;

    // if ($getClaimStatus == CLAIM_TL_REJECTED && $loggedInUserDepartment == FINANCE_DEPARTMENT) {
    //   $getClaimStatus = CLAIM_FINANCE_REJECTED;
    // }

    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, $empID, $fromDate, $ToDate);
  }

  // finance Department users
  function fetchEmployeeClaimDetails($loggedInUserID, $loggedInUserDepartment, $getClaimStatus, $tempClaimStatus, $empID, $fromDate, $ToDate)
  {

//$start_from =0*$per_page_record;

       $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) {
      if ($getClaimStatus == CLAIM_PENDING) { //Team Lead New claims tab
        $test1 = $this->db->prepare($query . " WHERE  `normal_site_visit`.`ENGINEER_EMP_ID`=?  AND `normal_site_visit`.`SITE_VISIT_DATE` BETWEEN ? AND ?   ");
         $test1->bind_param("sss",  $empID, $fromDate, $ToDate);
         $test1->execute();
       } else {
         $test1 = $this->db->prepare($query . " WHERE  `normal_site_visit_2`.`ENGINEER_EMP_ID`=? AND `normal_site_visit_2`.`CLAIM_STATUS`=? AND `normal_site_visit_2`.`TL_EMP_ID`=? AND `normal_site_visit_2`.`SITE_VISIT_DATE` BETWEEN ? AND ?");
         $test1->bind_param("sisss", $empID, $getClaimStatus, $loggedInUserID, $fromDate, $ToDate);
         $test1->execute();
       }
    } elseif ($loggedInUserDepartment == FINANCE_DEPARTMENT) {
      if ($getClaimStatus == CLAIM_TL_APPROVED) { //ONLY APPROVED TAB or // All claims
       $test1 = $this->db->prepare($query . " WHERE  `normal_site_visit`.`ENGINEER_EMP_ID`=?  AND `normal_site_visit`.`SITE_VISIT_DATE` BETWEEN ? AND ?   ");
        $test1->bind_param("sss",  $empID, $fromDate, $ToDate);
        $test1->execute();
      } else {
        $test1 = $this->db->prepare($query . " WHERE  `normal_site_visit_2`.`ENGINEER_EMP_ID`=? AND `normal_site_visit_2`.`CLAIM_STATUS`=? AND `normal_site_visit_2`.`FINANCE_EMP_ID`=? AND `normal_site_visit_2`.`SITE_VISIT_DATE` BETWEEN ? AND ?");
        $test1->bind_param("sisss", $empID, $getClaimStatus, $loggedInUserID, $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");
?>
