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

	$discount = $_POST['discount']?? null;
	$payment = $_POST['payment']?? null;
	$amount_due = $_POST['total']?? null;
	$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=2;
	$typerko=$_POST['typerko']?? null;
	
	$querydeliver=mysqli_query($con,"select * from ratedeliver where id='$typerko'")or die(mysqli_error());
    $rowdeliver=mysqli_fetch_array($querydeliver);
	
	$delivername=$rowdeliver['truckname'];
	$deliverfee=$rowdeliver['fee1'];
	$deliverqtylimit=$rowdeliver['qty'];
	
	
	$total1=$amount_due;
	$cid=$_REQUEST['cid'];

	$tendered = $_POST['tendered']?? null;
	$change = $_POST['change']?? null;
		
		
	$dodo=$discount/100;
	$dodokoko=$dodo * $total1;
	
	
	
	
	$total=$total1-$dodokoko;
	
	$changenew=$tendered-$total;	
	$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){
			
		$discount = $_POST['discount']?? null;
	$payment = $_POST['payment']?? null;
	$amount_due = $_POST['total']?? null;
	$password = $_POST['password']?? null;
	date_default_timezone_set("Asia/Manila"); 
	$date = date("Y-m-d H:i:s");
	$cid=$_REQUEST['cid'];
	$branch=2;
	$typerko=$_POST['typerko']?? null;
	$total1=$amount_due;
	$cid=$_REQUEST['cid'];

	$tendered = $_POST['tendered']?? null;
	$change = $_POST['change']?? null;	
	$dodo=$discount/100;
	$dodokoko=$dodo * $total1;
	$total=$total1-$dodokoko;
	
	$changenew=$tendered-$total;	
	$game=$changenew * -1;

		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,delivery,mydate,ratedelivery) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','cash','$tendered','$changenew','$branch','cash','$delivername','$date1','$deliverfee')")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 cust_id='$cid'")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'];
			$golop=$hh*$price;
			$branchpo=$row['branch_id'];
			$variation=$row['variation'];
			$color=$row['color'];
			$thicknew=$row['thick'];
			
			$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,branch,thick) VALUES('$pid','$qty','$golop','$mysales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew')")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','$mysales_id','$or')")or die(mysqli_error($con));
				
		
		$result=mysqli_query($con,"DELETE FROM temp_trans where cust_id='$cid'")	or die(mysqli_error($con));
		//echo "<script>document.location='receipt.php?cid=$cid'</script>";  	
		
		echo "<script>document.location='receipt1.php?sales_id=$mysales_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,delivery,mydate,ratedelivery) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','partial','$tendered','$changenew','$branch','partial','$tendered','$delivername','$date1','$deliverfee')")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 cust_id='$cid'")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'];
			$golop=$hh*$price;
			$branchpo=$row['branch_id'];
			$variation=$row['variation'];
			$color=$row['color'];
			$thicknew=$row['thick'];
			$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,branch,thick) VALUES('$pid','$qty','$golop','$mysales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew')")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','$mysales_id','$or')")or die(mysqli_error($con));
				
		
		$result=mysqli_query($con,"DELETE FROM temp_trans where cust_id='$cid'")	or die(mysqli_error($con));
		//echo "<script>document.location='receipt.php?cid=$cid'</script>";  	
		
		echo "<script>document.location='receipt1.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,delivery,mydate,ratedelivery) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','quote','$tendered','$changenew','$branch','quote','$delivername','$date1','$deliverfee')")or die(mysqli_error($con));
		
	$sales_id=mysqli_insert_id($con);
	$_SESSION['sid']=$sales_id;
	$query=mysqli_query($con,"select * from temp_trans where cust_id='$cid'")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'];
			$thicknew=$row['thick'];
			
			$golop=$hh*$price;
			$branchpo=$row['branch_id'];
			mysqli_query($con,"INSERT INTO sales_details(prod_id,qty,price,sales_id,hh,ww,variation,color,branch,thick) VALUES('$pid','$qty','$golop','$sales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew')")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'];	

				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 cust_id='$cid'")	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_transaction1.php?cid=$cid'</script>"; 
	}
	
	
	}else{
		
		if(isset($_POST['cash'])){
			
			if($tendered >= $amount_due){
				
			$discount = $_POST['discount']?? null;
	$payment = $_POST['payment']?? null;
	$amount_due = $_POST['total']?? null;
	$password = $_POST['password']?? null;
	date_default_timezone_set("Asia/Manila"); 
	$date = date("Y-m-d H:i:s");
	$cid=$_REQUEST['cid'];
	$branch=2;
	$typerko=$_POST['typerko']?? null;
	$total1=$amount_due;
	$cid=$_REQUEST['cid'];

	$tendered = $_POST['tendered']?? null;
	$change = $_POST['change']?? null;
		
		
	$dodo=$discount/100;
	$dodokoko=$dodo * $total1;
	
	
	
	
	$total=$total1-$dodokoko;
	
	$changenew=$tendered-$total;	
	$game=$changenew * -1;
		
		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,delivery,mydate,ratedelivery) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','cash','$tendered','$changenew','$branch','cash','$delivername','$date1','$deliverfee')")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 cust_id='$cid'")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'];
			$golop=$hh*$price;
			$branchpo=$row['branch_id'];
			$variation=$row['variation'];
			$color=$row['color'];
			$thicknew=$row['thick'];
			
			$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,branch,thick) VALUES('$pid','$qty','$golop','$mysales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew')")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','$mysales_id','$or')")or die(mysqli_error($con));
				
		
		$result=mysqli_query($con,"DELETE FROM temp_trans where cust_id='$cid'")	or die(mysqli_error($con));
		//echo "<script>document.location='receipt.php?cid=$cid'</script>";  	
		
		echo "<script>document.location='receipt1.php?sales_id=$mysales_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,delivery,mydate,ratedelivery) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','partial','$tendered','$changenew','$branch','partial','$tendered','$delivername','$date1','$deliverfee')")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 cust_id='$cid'")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'];
			$golop=$hh*$price;
			$branchpo=$row['branch_id'];
			$variation=$row['variation'];
			$color=$row['color'];
			$thicknew=$row['thick'];
			
		$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,branch,thick) VALUES('$pid','$qty','$golop','$mysales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew')")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','$mysales_id','$or')")or die(mysqli_error($con));
				
		
		$result=mysqli_query($con,"DELETE FROM temp_trans where cust_id='$cid'")	or die(mysqli_error($con));
		//echo "<script>document.location='receipt.php?cid=$cid'</script>";  	
		
		echo "<script>document.location='receipt1.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,delivery,mydate,ratedelivery) 
	VALUES('$cid','$id','$dodokoko','$discount','$amount_due','$total','$date','quote','$tendered','$changenew','$branch','quote','$delivername','$date1','$deliverfee')")or die(mysqli_error($con));
		
	$sales_id=mysqli_insert_id($con);
	$_SESSION['sid']=$sales_id;
	$query=mysqli_query($con,"select * from temp_trans where cust_id='$cid'")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'];
			$thicknew=$row['thick'];
			
			$golop=$hh*$price;
			$branchpo=$row['branch_id'];
			mysqli_query($con,"INSERT INTO sales_details(prod_id,qty,price,sales_id,hh,ww,variation,color,branch,thick) VALUES('$pid','$qty','$golop','$sales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew')")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'];	

				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 cust_id='$cid'")	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>";  		
}

	}
		
		
		
		
	
?>