<?php
session_start();
require_once 'db_connection.php'; 
$totalbillvalue = $_POST['totalbillvalue'];
$customer = $_POST['customer'];
$cashamount = $_POST['cashamount'];
$cardno = $_POST['cardno'];
$pay_type = $_POST['pay_type'];
$user_id = $_SESSION['user_id'];
$today = date('Y-m-d H:i:s');
if($totalbillvalue>0){

if($pay_type=='debit' && strlen($cardno)<12){
	echo "Enter valid debit card number..";
}elseif($pay_type=='cash' && $cashamount==0 || $pay_type=='cash' && $totalbillvalue>$cashamount ) {
	echo "Enter cash amount..";
}elseif($pay_type=='credit' && $cashamount!=$totalbillvalue ) {
	echo "Enter cash amount..";
}elseif($pay_type=='credit' && !$customer>0 ) {
	echo "Select credit customer..";
}else{

	$sql = "INSERT INTO mobileorders (pay_option, amount, card_no, customer, user_id, create_at)
	VALUES ('$pay_type', '$totalbillvalue', '$cardno', '$customer', '$user_id', '$today' )";
	if ($conn->query($sql) === TRUE) {
		$last_id = $conn->insert_id;
		if($last_id>0){
			$sql = "UPDATE mobiletemporarycart SET orderid='$last_id' WHERE orderid=0 AND user_id='$user_id'";

			if ($conn->query($sql) === TRUE) {
				echo $last_id;
			}
		}
	} else {
		echo "Error: " . $sql . "<br>" . $conn->error;
	}



}

}else{
	echo "Please refresh pos panel....";
}


?>