<?php
	include'connection.php';
	
	$dateStyle = Date("Ymd");
	$userid = $_GET['userid'];
	$TotAmt = $_GET['TotAmt'];
	$OpBal = $_GET['OpBal'];
	$clBal = $OpBal-$TotAmt;
	
	$quInsertTxn="INSERT INTO shopping_wallet(Date,Time,Userid,TxnType,TxnBy,OpBal,Txn,ClBal) 
	VALUES('$date','$time','$userid','Shopping','$userid','$OpBal','-$TotAmt','$clBal')";
	if(mysqli_query($con,$quInsertTxn))
	{
		$walletId = mysqli_insert_id($con);

		$quInsertInvoice="INSERT INTO invoice(Date,Time,Userid,Amount,WalletId) 
		VALUES('$date','$time','$userid','$TotAmt','$walletId')";
		if(mysqli_query($con,$quInsertInvoice))
		{
			$invoiceId = mysqli_insert_id($con);
			$invoiceNo = "RTD".$dateStyle.$invoiceId;
			
			$updateInvoice1="UPDATE shopping_wallet SET InvoiceId='$invoiceNo' WHERE Id='$walletId'";
			if(mysqli_query($con,$updateInvoice1))
			{
			
				$updateInvoice2="UPDATE invoice SET InvoiceNo='$invoiceNo' WHERE Id='$invoiceId'";
				if(mysqli_query($con,$updateInvoice2))
				{
					$updateCart="UPDATE cart SET InvoiceNo='$invoiceNo' WHERE Userid='$userid' AND Date='$date' AND Status='P'";
					if(mysqli_query($con,$updateCart))
					{
						$updateCartStatus="UPDATE cart SET Status='S' WHERE InvoiceNo='$invoiceNo' AND Userid='$userid' AND Date='$date'";
						if(mysqli_query($con,$updateCartStatus))
						{
							echo "<div class='msg-div'>
								<form method='post'>
								<h3>Payment Done Successfully !</h3><br>
								<i class='fa fa-check-circle' aria-hidden='true' id='okIcon'></i><br><br>
								<button type='submit' class='btnPopup'>OK</button><br><br>
								</form>
							</div>";
						}
					}
					else
					{
						echo "<div class='msg-div'>
								<form method='post'>
								<h3>Something Error !</h3><br>
								<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
								<button type='submit' class='btnPopup'>OK</button><br><br>
								</form>
							</div>";
					}
				}
				else
				{
					echo "<div class='msg-div'>
							<form method='post'>
							<h3>Something Error !</h3><br>
							<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
							<button type='submit' class='btnPopup'>OK</button><br><br>
							</form>
						</div>";
				}
			}
		}
		else
		{
			echo "<div class='msg-div'>
					<form method='post'>
					<h3>Something Error !</h3><br>
					<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
					<button type='submit' class='btnPopup'>OK</button><br><br>
					</form>
				</div>";
		}
	}
	else
	{
		echo "<div class='msg-div'>
				<form method='post'>
				<h3>Something Error !</h3><br>
				<i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				<button type='submit' class='btnPopup'>OK</button><br><br>
				</form>
			</div>";
	}

?>