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

//check for required fields
if (isset($_POST['SalePur1ID'])) {
                                    

	$SalePur1ID = $_POST['SalePur1ID'];
	$EntryType = $_POST['EntryType'];
	$ItemDescription = $_POST['ItemDescription'];
	$QtyAdd = $_POST['QtyAdd'];
	$QtyLess = $_POST['QtyLess'];
	$Price = $_POST['Price'];
	$Location = $_POST['Location'];
	$ClientID = $_POST['ClientID'];
	$ClientUserID = $_POST['ClientUserID'];
	$NetCode = $_POST['NetCode'];
	$SysCode = $_POST['SysCode'];
	$Item3NameID = $_POST['Item3NameID'];
	$Qty = $_POST['Qty'];
	$Total = $_POST['Total'];
	$OrderStatus = $_POST['OrderStatus'];  
	$AddItemTime = $_POST['AddItemTime'];  
	$Color = $_POST['Color'];  
	$Brand = $_POST['Brand'];  
	$Model = $_POST['Model'];  
	$Size = $_POST['Size'];  
	$PurPrice = $_POST['PurPrice'];  
	$SoldPrice = $_POST['SoldPrice'];  
	$SoldClientUserID = $_POST['SoldClientUserID'];  
	$SalePur1IDForSale = $_POST['SalePur1IDForSale'];  
	$SaleTime = $_POST['SaleTime'];  

	// include db connect class
	require_once __DIR__ . '/../DBConnect.php';
	$parm=array();
	$query="select max(ItemSerial) as maxid from QRSalePur2 where ClientID=$ClientID";
	try {
        // $result=$conn->query($sql);
        $stmt = $conn->prepare($query);
        
        // execute the query
        $stmt->execute();
        $users = $stmt->fetchAll();
		$ItemSerial = 0;
        foreach ($users as $user) {
          
			$ItemSerial = $user['maxid'] + 1;
        }
        // echo a message to say the UPDATE succeeded
       // $result = $stmt->rowCount(); // . " records UPDATED successfully";
    }
    catch (PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
	  
	$UpdatedDate = date("Y-m-d H:i:s");
	$sql = "INSERT INTO QRSalePur2 
				(SalePur1ID, EntryType, ItemSerial, ItemDescription, QtyAdd, QtyLess, Price,
				Location, ClientID, ClientUserID, NetCode, SysCode, UpdatedDate, Item3NameID, Qty,Total,
				OrderStatus,AddItemTime,Color,Model,Brand,Size,Other,PurPrice,SoldPrice,SoldClientUserID,SalePur1IDForSale,SaleTime)
			VALUES(
				'$SalePur1ID','$EntryType','$ItemSerial','$ItemDescription',
				'$QtyAdd','$QtyLess','$Price','$Location',
				'$ClientID','$ClientUserID','$NetCode','$SysCode',
				'$UpdatedDate','0','$Qty','$Total',
				'$OrderStatus','$AddItemTime','$Color','$Model',
				'$Brand','$Size','$Other','$PurPrice',
				'$SoldPrice','$SoldClientUserID','$SalePur1IDForSale','$SaleTime');";
			
		
	try {
		$result= $conn->exec($sql);	   

		if( $result === false) {
			/////////////not inserted
			$response["success"] = 0;  
			$response["message"] = "User Not Register";
			echo json_encode($response);
		}else{


			/////////////inserted
			$salepur2id='null';
			$response["success"] = 1;
			$response["salepur2id"] = $salepur2id;
			$response["ItemSerial"] = $ItemSerial;
			$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(SalePur1ID, EntryType, ItemDescription, QtyAdd, QtyLess, Price, Location, ClientID, ClientUserID, NetCode, SysCode, Item3NameID,Qty,Total)";
	// echoing JSON response
	echo json_encode($response);
}
?>
