<?php
  // array for JSON response
  $response = array();

  //check for required fields
	if (isset($_POST['CBDate']) &&
	    isset($_POST['DebitAccount']) && 
	    isset($_POST['CreditAccount']) && 
	    isset($_POST['Amount']) && 
	    isset($_POST['ClientID']) &&
	    isset($_POST['ClientUserID']) &&
	    isset($_POST['NetCode']) &&
	    isset($_POST['SysCode']) && 
	    isset($_POST['TableID']) && 
	    isset($_POST['SerialNo']) &&
	    isset($_POST['TableName'])) {

        $CB_Date = $_POST['CBDate'];
        $CBDate = date("Y-m-d", strtotime($CB_Date));
    		$DebitAccount = $_POST['DebitAccount'];
    		$CreditAccount = $_POST['CreditAccount'];
    		$CBRemarks = $_POST['CBRemarks'];
    		$Amount = $_POST['Amount'];
    		$ClientID = $_POST['ClientID'];
    		$ClientUserID = $_POST['ClientUserID'];
    		$NetCode = $_POST['NetCode'];
    		$SysCode = $_POST['SysCode'];
    		$TableID = $_POST['TableID'];
		    $SerialNo = $_POST['SerialNo'];
			$TableName = $_POST['TableName'];
		

        // include db connect class
    		require_once __DIR__ . '/../DBConnect.php';
			//$tablename="CashBook";
               $parm=array();
	           $query="select max(CashBookID) as maxid from CashBook where ClientID=$ClientID";
			   
			  try {
					$stmt = $conn->prepare($query);
					$stmt->execute();
					$users = $stmt->fetchAll();
					$CashBookID = 0;
					foreach ($users as $user) {
						$CashBookID = $user['maxid'] + 1;
					}
				}
				catch (PDOException $e) {
					echo $sql . "<br>" . $e->getMessage();
				}
				 
				 
				 
        $UpdatedDate = date("Y-m-d H:i:s");

        $sql = "INSERT INTO CashBook (CashBookID,CBDate, DebitAccount, CreditAccount, CBRemarks, Amount, ClientID, ClientUserID, NetCode, SysCode, UpdatedDate, TableID, SerialNo, TableName) VALUES ('$CashBookID', '$CBDate', '$DebitAccount', '$CreditAccount', '$CBRemarks', '$Amount', '$ClientID', '$ClientUserID', '$NetCode', '$SysCode', '$UpdatedDate', '$TableID', '$SerialNo', '$TableName');";
		
				try {
				   $result= $conn->exec($sql);		   
						if( $result === false ) {
						/////////////not inserted
							$response["success"] = 0;  
							$response["message"] = "User Not Register";
						echo json_encode($response);
						}else{
							/////////////inserted
						$response["success"] = 1;
						$response["CashBookID"] = $CashBookID;
						$response["UpdatedDate"] = $UpdatedDate;
						$response["message"] = "User Register";
						echo json_encode($response);
						}
					}
				catch (PDOException $e) {
					echo $sql . "<br>" . $e->getMessage();
				}

      }else{
          // no number found
          $response["success"] = 0;
          $response["message"] = "Send All Parameter List";
		  // echo no users JSON
		  echo json_encode($response);	
        }
?>
