<?php
// include db connect class
	require_once __DIR__ . '/../DBConnect.php';
	if (isset($_POST['ClientID']) &&  isset($_POST['SessionDate'])) {
    $ClientID = $_POST['ClientID'];
    $SessionDate = $_POST['SessionDate'];

			$sql = "SELECT * FROM SalePur2 WHERE ClientID = $ClientID AND UpdatedDate > '$SessionDate'";
		//	$params = array();
		//	$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
		//	$result = sqlsrv_query( $conn, $sql , $params, $options );
		
			$stmt = $conn->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			
			
			if($result!=null &&  $result>0){
					  $response["SalePur2"] = array();
			 foreach($result as $row) { 
						$SalePur2["ID"] = $row["ID"];  
						$SalePur2["SalePur1ID"] = $row["SalePur1ID"];  
						$SalePur2["ItemSerial"] = $row["ItemSerial"];  
						$SalePur2["EntryType"] = $row["EntryType"];                                          
						$SalePur2["Item3NameID"] = $row["Item3NameID"]; 
						$SalePur2["ItemDescription"] = $row["ItemDescription"];
						$SalePur2["QtyAdd"] = $row["QtyAdd"];                                  
						$SalePur2["QtyLess"] = $row["QtyLess"];                                 
						$SalePur2["Qty"] = $row["Qty"];                                     
						$SalePur2["Price"] = $row["Price"];                                   
						$SalePur2["Total"] = $row["Total"];                                   
						$SalePur2["Location"] = $row["Location"];                                           
						$SalePur2["ClientID"] = $row["ClientID"];    
						$SalePur2["ClientUserID"] = $row["ClientUserID"];
						$SalePur2["NetCode"] = $row["NetCode"];                                            
						$SalePur2["SysCode"] = $row["SysCode"];                                            
						$SalePur2["UpdatedDate"] = $row["UpdatedDate"];
				$SalePur2["Total"] = $row["Total"];

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

						// push single product into final response array
						array_push($response["SalePur2"], $SalePur2);
					  }
					  // 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 if (isset($_POST['ClientID']) && isset($_POST['MaxID'])) {
			$ClientID = $_POST['ClientID'];
			$MaxID = $_POST['MaxID'];

			$sql = "SELECT * FROM SalePur2 WHERE ClientID = $ClientID AND SalePur2ID >= $MaxID";

			
			$params = array();
			$stmt = $conn->prepare($sql);
			$stmt->execute();
			$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
			
			
			
			if($result!=null &&  $result>0){
			  $response["SalePur2"] = array();
			 foreach($result as $row) { 
				$SalePur2["ID"] = $row["ID"];  
				$SalePur2["SalePur1ID"] = $row["SalePur1ID"];  
				$SalePur2["ItemSerial"] = $row["ItemSerial"];  
				$SalePur2["EntryType"] = $row["EntryType"];                                          
				$SalePur2["Item3NameID"] = $row["Item3NameID"]; 
				$SalePur2["ItemDescription"] = $row["ItemDescription"];
				$SalePur2["QtyAdd"] = $row["QtyAdd"];                                  
				$SalePur2["QtyLess"] = $row["QtyLess"];                                 
				$SalePur2["Qty"] = $row["Qty"];                                     
				$SalePur2["Price"] = $row["Price"];                                   
				$SalePur2["Total"] = $row["Total"];                                   
				$SalePur2["Location"] = $row["Location"];                                           
				$SalePur2["ClientID"] = $row["ClientID"];    
				$SalePur2["ClientUserID"] = $row["ClientUserID"];
				$SalePur2["NetCode"] = $row["NetCode"];                                            
				$SalePur2["SysCode"] = $row["SysCode"];                                            
				$SalePur2["UpdatedDate"] = $row["UpdatedDate"];

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

				  // push single product into final response array
				  array_push($response["SalePur2"], $SalePur2);
				}
					
				 // 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"] = "No Data Availaible";
        // echoing JSON response
    		echo json_encode($response);
	}

?>
