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

//check for required fields
if (isset($_POST['SalePur1ID']) && 
isset($_POST['ItemSerial']) &&
isset($_POST['EntryType']) &&
isset($_POST['Item3NameID']) &&
isset($_POST['ItemDescription'])&&
isset($_POST['QtyAdd']) &&                                 
isset($_POST['QtyLess']) &&                                                                  
isset($_POST['Price']) &&                                                                  
isset($_POST['Location']) &&                                          
isset($_POST['ClientID']) &&   
isset($_POST['ClientUserID']) &&
isset($_POST['NetCode']) &&
isset($_POST['SysCode']) && 
isset($_POST['OrderStatus']) && 
isset($_POST['AddItemTime']) && 
isset($_POST['Color']) && 
isset($_POST['Model']) && 
isset($_POST['Brand']) && 
isset($_POST['Size']) && 
isset($_POST['Other']) &&    
isset($_POST['PurPrice']) &&   
isset($_POST['SoldPrice']) &&   
isset($_POST['SoldClientUserID']) &&   
isset($_POST['SalePur1IDForSale']) &&   
isset($_POST['SaleTime'])){

 	$SalePur1ID = $_POST['SalePur1ID']; 
 	$ItemSerial = $_POST['ItemSerial']; 
 	$EntryType = $_POST['EntryType'];                                         
 	$Item3NameID = $_POST['Item3NameID']; 
 	$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'];
	$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';
	$UpdatedDate = date("Y-m-d H:i:s");

	$sql = 
		"UPDATE 
			QRSalePur2 
		SET 
			Total = '$Total',
			SalePur1ID = '$SalePur1ID',
			ItemSerial = '$ItemSerial',
			Item3NameID = '0',
			ItemDescription = '$ItemDescription',
			QtyAdd = '$QtyAdd',
			QtyLess = '$QtyLess',
			Price = '$Price',
			Location = '$Location',
			ClientUserID = '$ClientUserID',
			NetCode = '$NetCode',
			SysCode = '$SysCode',
			UpdatedDate = '$UpdatedDate',
			OrderStatus = '$OrderStatus',
			AddItemTime = '$AddItemTime',
			Color = '$Color',
			Model = '$Model',
			Brand = '$Brand',
			Size = '$Size',
			Other = '$Other',
			PurPrice = '$PurPrice',
			SoldPrice = '$SoldPrice',
			SoldClientUserID = '$SoldClientUserID',
			SalePur1IDForSale = '$SalePur1IDForSale',
			SaleTime = '$SaleTime'
		WHERE 
			ClientID = $ClientID AND
			EntryType = '$EntryType' AND
			SalePur1ID = $SalePur1ID AND 
			ItemSerial = $ItemSerial";

// 	$sql2 = "SELECT
// 			SUM(QRSalePur2.Total) As amount
// 		FROM
// 			QRSalePur1
// 		LEFT JOIN QRSalePur2 ON QRSalePur2.SalePur1ID = QRSalePur1.SalePur1ID AND QRSalePur2.ClientID = QRSalePur1.ClientID
// 		WHERE
// 			QRSalePur1.SalePur1ID = $SalePur1ID AND QRSalePur1.EntryType = 'QRPur' AND QRSalePur1.ClientID = $ClientID
// 		GROUP BY
// 			QRSalePur1.SalePur1ID,
// 			QRSalePur1.EntryType";
			
// 	$sql3 = "Select
//                 QRSalePur2.SalePur1IDForSale,
//                 QRSalePur2.ClientID,
//                 Sum(QRSalePur2.SoldPrice) As amount
//             From
//                 QRSalePur2
//             where
//                 QRSalePur2.SalePur1IDForSale = $SalePur1ID AND
//                 QRSalePur2.ClientID = $ClientID 
//             Group By
//                 QRSalePur2.SalePur1IDForSale,
//                 QRSalePur2.ClientID"

	$params = array();

	$result=0;
	try{	
		$stmt = $conn->prepare($sql);
		$stmt->execute();
		$result= $stmt->rowCount();
	}
	catch(PDOException $e){
		echo $sql . "<br>" . $e->getMessage();
	}
	if ( $result!= 0) {

// 		$stmt2 = $conn->prepare($sql2);
// 		$stmt2->execute();
// 		$row = $stmt2->fetch(PDO::FETCH_ASSOC); 	
// 		$amount = $row['amount'];
		
// 		$stmt = $conn->prepare($sql3);
// 		$stmt->execute();
// 		$row2 = $stmt->fetch(PDO::FETCH_ASSOC); 	
// 		$amount2 = $row2['amount'];

// 		//if($row != null && $row > 0 && $amount != null){
// 			try{	
// 					//$query = "UPDATE QRSalePur1 SET BillAmount = '$amount' , UpdatedDate = '$UpdatedDate' WHERE SalePur1ID = '$SalePur1ID' AND EntryType = 'QRPur' AND ClientID = $ClientID";
// 					$query2 = "UPDATE CashBook SET Amount = '$amount', UpdatedDate = '$UpdatedDate' WHERE TableID = '$SalePur1ID' AND TableName = 'QRPur' AND ClientID = $ClientID";
					
// 					//$query3 = "UPDATE QRSalePur1 SET BillAmount = '$amount2' , UpdatedDate = '$UpdatedDate' WHERE SalePur1ID = '$SalePur1ID' AND EntryType = 'QRSale' AND ClientID = $ClientID";
// 					$query4 = "UPDATE CashBook SET Amount = '$amount2', UpdatedDate = '$UpdatedDate' WHERE TableID = '$SalePur1ID' AND TableName = 'QRSale' AND ClientID = $ClientID";
					
// 					//$stmt2 = $conn->prepare($query);
// 					//$stmt2->execute();
					
// 				    $stmt4 = $conn->prepare($query2);
// 				    $stmt4->execute();
				    
// 			        //$stmt5 = $conn->prepare($query3);
// 				    //$stmt5->execute();
				    
// 				    $stmt6 = $conn->prepare($query4);
// 				    $stmt6->execute();
// 			}
// 			catch(PDOException $e){
// 				echo $query . "<br>" . $e->getMessage();
// 			}
		//} 

		$rows_affected= "No information available.";
		$response["success"] = 1;
		$response["roweffected"] = $result;
		$response["UpdatedDate"] = $UpdatedDate;
		$response["message"] = "Update Successfully";
		// echoing JSON response
		echo json_encode($response);
	}else{
		$response["success"] = 0;
		$response["roweffected"] = $result;      
		$response["message"] = "Not updated row ".$result;
		// echoing JSON response
		echo json_encode($response);
	}
		  
}else{
	// success
	$response["success"] = 0;
	$response["message"] = "No Update";
	// echoing JSON response
	echo json_encode($response);
}
?>
