
<?php
session_start();
require_once __DIR__."/allfunctions.php";

if(empty($_SESSION["MEMBER_ID"]) && empty($_SESSION["TYPE"]) && empty($_SESSION["FIRST_NAME"]))
{
	 $itemarray = array();
	 $memberid = "";
	$type = "";
	$fname = "";
}
else
{
	$memberid = $_SESSION['MEMBER_ID'];
	$type = $_SESSION['TYPE'];
	$fname = $_SESSION['FIRST_NAME'];
}

	$con = mysqli_connect(get_dbserver(),get_dbuser(),get_dbpassword(),get_dbname());
	$echo="";

	$voidtype = $_GET['voidtype'];
	$refno = $_GET['refno'];
	$notes = $_GET['notes'];



	$now =date("Y-m-d H:i:s");
	$status = "Paid";
	//$soldby = getRecord('soldby','sales','id ='.$array[$c].'');
	if($voidtype == "salesorder")
	{
		$getpaymentvoidstatus = getRecord('paymentstatus','payments','salesID ='.$refno);
		//echo $getpaymentvoidstatus;
		if($getpaymentvoidstatus =="Voided" OR empty($getpaymentvoidstatus))
		{
				$strSQL = "UPDATE sales SET status='0' WHERE id=".$refno;
				if(mysqli_query($con, $strSQL) or die("database error: ". mysqli_error($con)))
				{

					$insert_sql3 = "INSERT INTO void(id,voidtype,refno,notes,datecreated,approvedby,status)
					VALUES(NULL,'".$voidtype."','".$refno."', '".$notes."','".$now."','".$memberid."','1')";
					$rs = mysqli_query($con,$insert_sql3) or die(mysqli_error($con));
					if($rs)
					{
					echo 1;
					//mysqli_close($con);
					}
					else {
						echo 0;
					}
				}
				else
				{
					echo 0;
				}
		}
		else if($getpaymentvoidstatus =="Paid")
		{
			 echo "Paid";
		}
	}
	else if($voidtype == "payment")
	{
		$strSQL = "UPDATE payments SET paymentstatus='Voided' WHERE paymentref=".$refno;
		if(mysqli_query($con, $strSQL) or die("database error: ". mysqli_error($con)))
		{
			$getsalesID = getRecord('salesID','payments','paymentref ='.$refno);
			$strSQL2 = "UPDATE sales SET paymentstatus='Voided' WHERE id=".$getsalesID;
			if(mysqli_query($con, $strSQL2) or die("database error: ". mysqli_error($con)))
			{
				$insert_sql3 = "INSERT INTO void(id,voidtype,refno,notes,datecreated,approvedby,status)
				VALUES(NULL,'".$voidtype."','".$refno."', '".$notes."','".$now."','".$memberid."','1')";
				$rs = mysqli_query($con,$insert_sql3) or die(mysqli_error($con));
				if($rs)
				{
				echo 1;
				//mysqli_close($con);
				}
				else {
					echo 0;
				}
			}
			else {
					echo 0;
			}
		}
		else
		{
			echo 0;
		}
	}


//	echo $historyname;
?>
