<?php 
session_start();
$id=$_SESSION['id'];	
include('../dist/includes/dbcon.php');

	$discount = $_POST['discount'];
	$payment = $_POST['payment']?? null;
	$amount_due = $_POST['total'];
	$password = $_POST['password']?? null;
	date_default_timezone_set("Asia/Manila"); 
	$date = date("Y-m-d H:i:s");
	$date1 = date("Y-m-d");
	$cid=$_REQUEST['cid'];
	$branch=1;
	$tendered = $_POST['tendered'];
	$change = $_POST['change'];
	$dodo=$discount/100;
	$dodokoko=$dodo * $amount_due;
	$total=$amount_due-$dodokoko;
	$changenew=$tendered - $total;
	$cid=$_REQUEST['cid'];

		$game=$changenew * -1;
		
	$queryvv=mysqli_query($con,"select * from passcode where id='1'")or die(mysqli_error());
    $rowvv=mysqli_fetch_array($queryvv);
	$mypasscode=$rowvv['passcode'];
	
	
	if($discount > 0){
 
	if($mypasscode = $password){

if(isset($_POST['cash'])){
		
		
		if($tendered >= $amount_due){

		mysqli_query($con,"INSERT INTO sales(cust_id,user_id,discount,discko,amount_due,total,date_added,modeofpayment,cash_tendered,cash_change,branch_id,status,mydate) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','cash','$tendered','$changenew','$branch','cash','$date1')")or die(mysqli_error($con));
		
		
	$sales_id=mysqli_insert_id($con);
	$_SESSION['sid']=$sales_id;
	$query=mysqli_query($con,"select * from temp_trans where branch_id='$branch'")or die(mysqli_error($con));
		while ($row=mysqli_fetch_array($query))
		{
			$pid=$row['prod_id'];	
 			$qty=$row['qty'];
			$price=$row['price'];
			$hh=$row['hh'];
			$ww=$row['ww'];
			$variation=$row['variation'];
			$color=$row['color'];
			
			
			mysqli_query($con,"INSERT INTO sales_details(prod_id,qty,price,sales_id,hh,ww,variation,color) VALUES('$pid','$qty','$price','$sales_id','$hh','$ww','$variation','$color')")or die(mysqli_error($con));
			mysqli_query($con,"UPDATE product SET prod_qty=prod_qty-'$qty' where prod_id='$pid' and branch_id='$branch'") or die(mysqli_error($con)); 
		}
		
		$query1=mysqli_query($con,"SELECT or_no FROM payment NATURAL JOIN sales WHERE modeofpayment =  'cash' ORDER BY payment_id DESC LIMIT 0 , 1")or die(mysqli_error($con));

			$row1=mysqli_fetch_array($query1);
				$or=$row1['or_no'];	

				if ($or==0)
				{
					$or=1901;
				}
				else
				{
					$or=$or+1;
				}

				mysqli_query($con,"INSERT INTO payment(cust_id,user_id,payment,payment_date,branch_id,payment_for,due,status,sales_id,or_no) 
	VALUES('$cid','$id','$total','$date','$branch','$date','$total','paid','$sales_id','$or')")or die(mysqli_error($con));
				 	
		
		$result=mysqli_query($con,"DELETE FROM temp_trans where branch_id='$branch'")	or die(mysqli_error($con));
		//echo "<script>document.location='receipt.php?cid=$cid'</script>";  	
		echo "<script>document.location='receipt1.php?sales_id=$sales_id'</script>"; 
		
		}else{
			
			mysqli_query($con,"INSERT INTO sales(cust_id,user_id,discount,discko,amount_due,total,date_added,modeofpayment,cash_tendered,cash_change,branch_id,status,partial1,mydate) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','partial','$tendered','$changenew','$branch','partial','$tendered','$date1')")or die(mysqli_error($con));
		mysqli_query($con,"UPDATE customer SET balance=balance + '$game' where cust_id='$cid'") or die(mysqli_error($con)); 
	$sales_id=mysqli_insert_id($con);
	$_SESSION['sid']=$sales_id;
	$query=mysqli_query($con,"select * from temp_trans where branch_id='$branch'")or die(mysqli_error($con));
		while ($row=mysqli_fetch_array($query))
		{
			$pid=$row['prod_id'];	
 			$qty=$row['qty'];
			$price=$row['price'];
			$hh=$row['hh'];
			$ww=$row['ww'];
			$variation=$row['variation'];
			$color=$row['color'];
			
			$queryww=mysqli_query($con,"select * from sales order by sales_id DESC")or die(mysqli_error($con));
		$rowww=mysqli_fetch_array($queryww);
			$mysales_id=$rowww['sales_id'];
			mysqli_query($con,"INSERT INTO sales_details(prod_id,qty,price,sales_id,hh,ww,variation,color) VALUES('$pid','$qty','$price','$mysales_id','$hh','$ww','$variation','$color')")or die(mysqli_error($con));
			mysqli_query($con,"UPDATE product SET prod_qty=prod_qty-'$qty' where prod_id='$pid' and branch_id='$branch'") or die(mysqli_error($con)); 
		}
		
		$query1=mysqli_query($con,"SELECT or_no FROM payment NATURAL JOIN sales WHERE modeofpayment =  'cash' ORDER BY payment_id DESC LIMIT 0 , 1")or die(mysqli_error($con));

			$row1=mysqli_fetch_array($query1);
				$or=$row1['or_no']?? null;	

				if ($or==0)
				{
					$or=1901;
				}
				else
				{
					$or=$or+1;
				}

				mysqli_query($con,"INSERT INTO payment(cust_id,user_id,payment,payment_date,branch_id,payment_for,due,status,sales_id,or_no) 
	VALUES('$cid','$id','$total','$date','$branch','$date','$total','paid','$mysales_id','$or')")or die(mysqli_error($con));
				 	
		
		$result=mysqli_query($con,"DELETE FROM temp_trans where branch_id='$branch'")	or die(mysqli_error($con));
		//echo "<script>document.location='receipt.php?cid=$cid'</script>";  	
		echo "<script>document.location='receipt.php?sales_id=$mysales_id'</script>"; 
			
			
		}
		
		
}

if(isset($_POST['quote'])){
		
		
		

		mysqli_query($con,"INSERT INTO sales(cust_id,user_id,discount,discko,amount_due,total,date_added,modeofpayment,cash_tendered,cash_change,branch_id,status,mydate) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','quote','$tendered','$changenew','$branch','quote','$date1')")or die(mysqli_error($con));
		
	$sales_id=mysqli_insert_id($con);
	$_SESSION['sid']=$sales_id;
	$query=mysqli_query($con,"select * from temp_trans where branch_id='$branch'")or die(mysqli_error($con));
		while ($row=mysqli_fetch_array($query))
		{
			$pid=$row['prod_id'];	
 			$qty=$row['qty'];
			$price=$row['price'];
			$hh=$row['hh'];
			$ww=$row['ww'];
			$variation=$row['variation'];
			$color=$row['color'];
			
			
			mysqli_query($con,"INSERT INTO sales_details(prod_id,qty,price,sales_id,hh,ww,variation,color) VALUES('$pid','$qty','$price','$sales_id','$hh','$ww','$variation','$color')")or die(mysqli_error($con));
			mysqli_query($con,"UPDATE product SET prod_qty=prod_qty-'$qty' where prod_id='$pid' and branch_id='$branch'") or die(mysqli_error($con)); 
		}
		
		$query1=mysqli_query($con,"SELECT or_no FROM payment NATURAL JOIN sales WHERE modeofpayment =  'quote' ORDER BY payment_id DESC LIMIT 0 , 1")or die(mysqli_error($con));

			$row1=mysqli_fetch_array($query1);
				$or=$row1['or_no']?? null;	

				if ($or==0)
				{
					$or=1901;
				}
				else
				{
					$or=$or+1;
				}

				mysqli_query($con,"INSERT INTO payment(cust_id,user_id,payment,payment_date,branch_id,payment_for,due,status,sales_id,or_no) 
	VALUES('$cid','$id','$total','$date','$branch','$date','$total','quote','$sales_id','$or')")or die(mysqli_error($con));
					
		
		$result=mysqli_query($con,"DELETE FROM temp_trans where branch_id='$branch'")	or die(mysqli_error($con));
		//echo "<script>document.location='receipt.php?cid=$cid'</script>";  	
		echo "<script>document.location='receipt.php?sales_id=$sales_id'</script>";  
		
		
		
}
		
	}else{
		echo "<script type='text/javascript'>alert('PASSCODE INCORRECT!');</script>";
			echo "<script>document.location='cash_transaction.php?cid=$cid'</script>";  
	}
		
		
		}else{
			
if(isset($_POST['cash'])){
		
		
		if($tendered >= $amount_due){

		mysqli_query($con,"INSERT INTO sales(cust_id,user_id,discount,discko,amount_due,total,date_added,modeofpayment,cash_tendered,cash_change,branch_id,status,mydate) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','cash','$tendered','$changenew','$branch','cash','$date1')")or die(mysqli_error($con));
		
	$sales_id=mysqli_insert_id($con);
	$_SESSION['sid']=$sales_id;
	$query=mysqli_query($con,"select * from temp_trans where branch_id='$branch'")or die(mysqli_error($con));
		while ($row=mysqli_fetch_array($query))
		{
			$pid=$row['prod_id'];	
 			$qty=$row['qty'];
			$price=$row['price'];
			$hh=$row['hh'];
			$ww=$row['ww'];
			$variation=$row['variation'];
			$color=$row['color'];
			
			
			mysqli_query($con,"INSERT INTO sales_details(prod_id,qty,price,sales_id,hh,ww,variation,color) VALUES('$pid','$qty','$price','$sales_id','$hh','$ww','$variation','$color')")or die(mysqli_error($con));
			mysqli_query($con,"UPDATE product SET prod_qty=prod_qty-'$qty' where prod_id='$pid' and branch_id='$branch'") or die(mysqli_error($con)); 
		}
		
		$query1=mysqli_query($con,"SELECT or_no FROM payment NATURAL JOIN sales WHERE modeofpayment =  'cash' ORDER BY payment_id DESC LIMIT 0 , 1")or die(mysqli_error($con));

			$row1=mysqli_fetch_array($query1);
				$or=$row1['or_no']?? null;	

				if ($or==0)
				{
					$or=1901;
				}
				else
				{
					$or=$or+1;
				}

				mysqli_query($con,"INSERT INTO payment(cust_id,user_id,payment,payment_date,branch_id,payment_for,due,status,sales_id,or_no) 
	VALUES('$cid','$id','$total','$date','$branch','$date','$total','paid','$sales_id','$or')")or die(mysqli_error($con));
				 	
		
		$result=mysqli_query($con,"DELETE FROM temp_trans where branch_id='$branch'")	or die(mysqli_error($con));
		//echo "<script>document.location='receipt.php?cid=$cid'</script>";  	
		echo "<script>document.location='receipt.php?sales_id=$sales_id'</script>"; 
		
		}else{
			
			mysqli_query($con,"INSERT INTO sales(cust_id,user_id,discount,discko,amount_due,total,date_added,modeofpayment,cash_tendered,cash_change,branch_id,status,partial1,mydate) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','partial','$tendered','$changenew','$branch','partial','$tendered','$date1')")or die(mysqli_error($con));
		
		mysqli_query($con,"UPDATE customer SET balance=balance+'$game' where cust_id='$cid'") or die(mysqli_error($con)); 
		
		
	$sales_id=mysqli_insert_id($con);
	$_SESSION['sid']=$sales_id;
	$query=mysqli_query($con,"select * from temp_trans where branch_id='$branch'")or die(mysqli_error($con));
		while ($row=mysqli_fetch_array($query))
		{
			$pid=$row['prod_id'];	
 			$qty=$row['qty'];
			$price=$row['price'];
			$hh=$row['hh'];
			$ww=$row['ww'];
			$variation=$row['variation'];
			$color=$row['color'];
			
			$queryww=mysqli_query($con,"select * from sales order by sales_id DESC")or die(mysqli_error($con));
		$rowww=mysqli_fetch_array($queryww);
			$mysales_id=$rowww['sales_id'];
			mysqli_query($con,"INSERT INTO sales_details(prod_id,qty,price,sales_id,hh,ww,variation,color) VALUES('$pid','$qty','$price','$mysales_id','$hh','$ww','$variation','$color')")or die(mysqli_error($con));
			mysqli_query($con,"UPDATE product SET prod_qty=prod_qty-'$qty' where prod_id='$pid' and branch_id='$branch'") or die(mysqli_error($con)); 
		}
		
		$query1=mysqli_query($con,"SELECT or_no FROM payment NATURAL JOIN sales WHERE modeofpayment =  'cash' ORDER BY payment_id DESC LIMIT 0 , 1")or die(mysqli_error($con));

			$row1=mysqli_fetch_array($query1);
				$or=$row1['or_no']?? null;	

				if ($or==0)
				{
					$or=1901;
				}
				else
				{
					$or=$or+1;
				}

				mysqli_query($con,"INSERT INTO payment(cust_id,user_id,payment,payment_date,branch_id,payment_for,due,status,sales_id,or_no) 
	VALUES('$cid','$id','$total','$date','$branch','$date','$total','paid','$mysales_id','$or')")or die(mysqli_error($con));
				 	
		
		$result=mysqli_query($con,"DELETE FROM temp_trans where branch_id='$branch'")	or die(mysqli_error($con));
		//echo "<script>document.location='receipt.php?cid=$cid'</script>";  	
		echo "<script>document.location='receipt.php?sales_id=$mysales_id'</script>"; 
			
			
		}
		
}

if(isset($_POST['quote'])){
		
		
		

		mysqli_query($con,"INSERT INTO sales(cust_id,user_id,discount,discko,amount_due,total,date_added,modeofpayment,cash_tendered,cash_change,branch_id,status,mydate) VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','quote','$tendered','$changenew','$branch','quote','$date1')")or die(mysqli_error($con));
		
	$sales_id=mysqli_insert_id($con);
	$_SESSION['sid']=$sales_id;
	$query=mysqli_query($con,"select * from temp_trans where branch_id='$branch'")or die(mysqli_error($con));
		while ($row=mysqli_fetch_array($query))
		{
			$pid=$row['prod_id'];	
 			$qty=$row['qty'];
			$price=$row['price'];
			$hh=$row['hh'];
			$ww=$row['ww'];
			$variation=$row['variation'];
			$color=$row['color'];
			
			
			mysqli_query($con,"INSERT INTO sales_details(prod_id,qty,price,sales_id,hh,ww,variation,color) VALUES('$pid','$qty','$price','$sales_id','$hh','$ww','$variation','$color')")or die(mysqli_error($con));
			mysqli_query($con,"UPDATE product SET prod_qty=prod_qty-'$qty' where prod_id='$pid' and branch_id='$branch'") or die(mysqli_error($con)); 
		}
		
		$query1=mysqli_query($con,"SELECT or_no FROM payment NATURAL JOIN sales WHERE modeofpayment =  'quote' ORDER BY payment_id DESC LIMIT 0 , 1")or die(mysqli_error($con));

			$row1=mysqli_fetch_array($query1);
				$or=$row1['or_no']?? null;	

				if ($or==0)
				{
					$or=1901;
				}
				else
				{
					$or=$or+1;
				}

				mysqli_query($con,"INSERT INTO payment(cust_id,user_id,payment,payment_date,branch_id,payment_for,due,status,sales_id,or_no) 
	VALUES('$cid','$id','$total','$date','$branch','$date','$total','quote','$sales_id','$or')")or die(mysqli_error($con));
					
		
		$result=mysqli_query($con,"DELETE FROM temp_trans where branch_id='$branch'")	or die(mysqli_error($con));
		//echo "<script>document.location='receipt.php?cid=$cid'</script>";  	
		echo "<script>document.location='receipt.php?sales_id=$sales_id'</script>";  
		
		
		
}  
		
	}
		
	



	
	
?>