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

//check for required fields
if (isset($_POST['EntryType']) && isset($_POST['SPDate']) && isset($_POST['AcNameID']) && isset($_POST['Remarks']) && isset($_POST['ClientID']) && isset($_POST['ClientUserID']) && isset($_POST['NetCode']) && isset($_POST['SysCode']) && isset($_POST['NameOfPerson']) && isset($_POST['PayAfterDay'])) {
    
    $EntryType    = $_POST['EntryType'];
    $SPDate       = $_POST['SPDate'];
    $AcNameID     = $_POST['AcNameID'];
    $Remarks      = $_POST['Remarks'];
    $ClientID     = $_POST['ClientID'];
    $ClientUserID = $_POST['ClientUserID'];
    $NetCode      = $_POST['NetCode'];
    $SysCode      = $_POST['SysCode'];
    $NameOfPerson = $_POST['NameOfPerson'];
    $PayAfterDay  = $_POST['PayAfterDay'];
	$BillAmount   = $_POST['BillAmount'];
    $BillStatus   = $_POST['BillStatus'];
    $ContactNo    = $_POST['ContactNo'];
    
    // include db connect class
    require_once __DIR__ . '/../DBConnect.php';
    $parm  = array();
    $sql = "select max(SalePur1ID) as maxid from QRSalePur1 where ClientID=" . $ClientID . " 
               AND EntryType='" . $EntryType . "'";
	try {
        $stmt = $conn->prepare($sql);
        $stmt->execute();
        $users = $stmt->fetchAll();
		$salepur1id = 0;
        foreach ($users as $user) {
			$salepur1id = $user['maxid'] + 1;
        }
    }
    catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
   ///////////////////////////////now Importin in DB
   
	$UpdatedDate = date("Y-m-d H:i:s");
    $sql = "INSERT INTO `QRSalePur1` ( `SalePur1ID`, `EntryType`, `SPDate`, `AcNameID`, `Remarks`, `ClientID`, `ClientUserID`, `NetCode`, `SysCode`, `UpdatedDate`, `NameOfPerson`, `PayAfterDay`, `BillStatus`, `ContactNo`) 
	VALUES (  '$salepur1id', '$EntryType', '$SPDate', '$AcNameID', '$Remarks', '$ClientID', '$ClientUserID', '$NetCode', '$SysCode', '$UpdatedDate', '$NameOfPerson', '$PayAfterDay', '$BillStatus', '$ContactNo');";
	
      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["SalePur1Id"] = $salepur1id;
            $response["UpdatedDate"] = $UpdatedDate;
            $response["message"] = "User Register";
			echo json_encode($response);
			}
		}
    catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
    
   
} else {
    // success
    $response["success"] = 0;
    $response["message"] = "Send All Required Fields(EntryType,SPDate ,AcNameID  Remarks ClientID ClientUserID NetCode SysCode  NameOfPerson PayAfterDay)";
    // echoing JSON response
    echo json_encode($response);
}
?>