<?php
// include db connect class
	require_once __DIR__ . '/../DBConnect.php';

	if (isset($_POST['ClientID']) && isset($_POST['MaxID']) && isset($_POST['SessionDate'])) {
      $ClientID = $_POST['ClientID'];
      $MaxID = $_POST['MaxID'];
      $SessionDate = $_POST['SessionDate'];

		$sql = "SELECT * FROM CashBook WHERE ClientID = $ClientID AND CashBookID <= $MaxID AND UpdatedDate > '$SessionDate'";

	$stmt = $conn->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			   

		   if($result!=null &&  $result>0){
        $response["CashBook"] = array();
        foreach($result as $row) { 
          $CashBook["CashBookID"] = $row["CashBookID"];
          $CashBook["CBDate"] = $row["CBDate"];
          $CashBook["DebitAccount"] = $row["DebitAccount"];
          $CashBook["CreditAccount"] = $row["CreditAccount"];
          $CashBook["CBRemarks"] = $row["CBRemarks"];
          $CashBook["Amount"] = $row["Amount"];
          $CashBook["ClientID"] = $row["ClientID"];
          $CashBook["ClientUserID"] = $row["ClientUserID"];
          $CashBook["NetCode"] = $row["NetCode"];
          $CashBook["SysCode"] = $row["SysCode"];
          $CashBook["UpdatedDate"] = $row["UpdatedDate"];
		  $CashBook["TableID"] = $row["TableID"];
        //  $CashBook["BookingID"] = $row["BookingID"];
		  $CashBook["SerialNo"] = $row["SerialNo"];
		  $CashBook["TableName"] = $row["TableName"];

		  $current_date = date("Y-m-d H:i:s");
		  $CashBook["SessionDate"] = $current_date;

          // push single product into final response array
          array_push($response["CashBook"], $CashBook);
        }
        // success
        $response["success"] = 1;
        // echoing JSON response
        echo json_encode($response);
      }else {
        // success
        $response["success"] = 0;
        $response["message"] = "No Data Availaible for Update";
        // echoing JSON response
    		echo json_encode($response);
      }
	}
	
	else if (isset($_POST['ClientID']) && isset($_POST['MaxID'])) {
    $ClientID = $_POST['ClientID'];
    $MaxID = $_POST['MaxID'];

    $sql = "SELECT * FROM CashBook WHERE ClientID = $ClientID AND CashBookID > $MaxID";

		$params = array();
		$stmt = $conn->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			   

		if($result!=null &&  $result>0){
      $response["CashBook"] = array();
      foreach($result as $row) { 
         $CashBook["CashBookID"] = $row["CashBookID"];
          $CashBook["CBDate"] = $row["CBDate"];
          $CashBook["DebitAccount"] = $row["DebitAccount"];
          $CashBook["CreditAccount"] = $row["CreditAccount"];
          $CashBook["CBRemarks"] = $row["CBRemarks"];
          $CashBook["Amount"] = $row["Amount"];
          $CashBook["ClientID"] = $row["ClientID"];
          $CashBook["ClientUserID"] = $row["ClientUserID"];
          $CashBook["NetCode"] = $row["NetCode"];
          $CashBook["SysCode"] = $row["SysCode"];
          $CashBook["UpdatedDate"] = $row["UpdatedDate"];
		  $CashBook["TableID"] = $row["TableID"];
         // $CashBook["BookingID"] = $row["BookingID"];
		  $CashBook["SerialNo"] = $row["SerialNo"];
		   $CashBook["TableName"] = $row["TableName"];

    $current_date = date("Y-m-d H:i:s");
    $CashBook["SessionDate"] = $current_date;

        // push single product into final response array
        array_push($response["CashBook"], $CashBook);
      }
      // success
      $response["success"] = 1;
      // echoing JSON response
      echo json_encode($response);
    }else {
      // success
      $response["success"] = 0;
      $response["message"] = "No Data Availaible";
      // echoing JSON response
      echo json_encode($response);
    }
  }else {
        // success
        $response["success"] = 0;
        $response["message"] = "Parameters Not Send";
        // echoing JSON response
    		echo json_encode($response);
  }

?>
