<?php 
session_start();
$id=$_SESSION['id'];	
include('../dist/includes/dbcon.php');
    $bank=$_POST['bank']?? null;
    $delivername=$_POST['comment']?? null;
	$discount = $_POST['discount']?? null;
	$discount1 = $_POST['discount1']?? 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=3;
	
	$typerko=$_POST['typerko']?? null;
	$deliverdis=$_POST['deliverdis']?? null;
	
	$querydeliver=mysqli_query($con,"select * from ratedeliver where id='$typerko'")or die(mysqli_error());
    $rowdeliver=mysqli_fetch_array($querydeliver);
	$deliverfeetata=$rowdeliver['fee1'];
	$delivername22=$rowdeliver['truckname'];
	
	
	
	$deliverfee=$_POST['bank']?? null;
	$deliverqtylimit=$rowdeliver['qty'];
	
	
	$total1=$amount_due;
	$cid=$_REQUEST['cid'];

	$tendered = $_POST['tendered']?? null;
	$change = $_POST['change']?? null;
		
		
	$dodo=$discount1/100;
	$dodokoko=($dodo * $total1) + $discount;
	
	
	
	
	$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 || $discount1 > 0){
 
	if($mypasscode = $password){	
		
if(isset($_POST['cash'])){
		
		if($tendered >= $amount_due){
			$discount1 = $_POST['discount1']?? null;
		$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=3;
	$typerko=$_POST['typerko']?? null;
	$total1=$amount_due;
	$cid=$_REQUEST['cid'];

	$tendered = $_POST['tendered']?? null;
	$change = $_POST['change']?? null;	
	$dodo=$discount1/100;
	$dodokoko=$dodo * $total1 + $discount;
	$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','$discount1','$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;
			$totaldet=$qty * $price * $hh;
			$branchpo=$row['branch_id'];
			$variation=$row['variation'];
			$color=$row['color'];
			$thicknew=$row['thick'];
			$mystatus=$row['status'];
			
			$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,total,sales_id,hh,ww,variation,color,branch,thick,status) VALUES('$pid','$qty','$golop','$totaldet','$mysales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew','$mystatus')")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='hardwarereceipt.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','$discount1','$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;
			$totaldet=$qty * $price * $hh;
			$branchpo=$row['branch_id'];
			$variation=$row['variation'];
			$color=$row['color'];
			$thicknew=$row['thick'];
			$mystatus=$row['status'];
			$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,total,sales_id,hh,ww,variation,color,branch,thick,status) VALUES('$pid','$qty','$golop','$totaldet','$mysales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew','$mystatus')")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='hardwarereceipt.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','$discount1','$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'];
			$mystatus=$row['status'];
			$golop=$hh*$price;
			$totaldet=$qty * $price * $hh;
			$branchpo=$row['branch_id'];
			mysqli_query($con,"INSERT INTO sales_details(prod_id,qty,price,total,sales_id,hh,ww,variation,color,branch,thick,status) VALUES('$pid','$qty','$golop','$totaldet','$sales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew','$mystatus')")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='hardwarereceipt.php?sales_id=$sales_id'</script>";  		
}

	}else{
		echo "<script type='text/javascript'>alert('PASSCODE INCORRECT!');</script>";
			echo "<script>document.location='hardwarecash_transaction.php?cid=$cid'</script>"; 
	}
	
	
	}else{
		
		if(isset($_POST['cash'])){
			
			if($tendered >= $amount_due){
				$discount1 = $_POST['discount1']?? null;
			$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=3;
	$typerko=$_POST['typerko']?? null;
	$total1=$amount_due;
	$cid=$_REQUEST['cid'];

	$tendered = $_POST['tendered']?? null;
	$change = $_POST['change']?? null;
		
		
	$dodo=$discount1/100;
	$dodokoko=$dodo * $total1 + $discount;
	
	
	
	
	$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','$discount1','$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;
			$totaldet=$qty * $price * $hh;
			$branchpo=$row['branch_id'];
			$variation=$row['variation'];
			$color=$row['color'];
			$thicknew=$row['thick'];
			$mystatus=$row['status'];
			$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,total,sales_id,hh,ww,variation,color,branch,thick,status) VALUES('$pid','$qty','$golop','$totaldet','$mysales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew','$mystatus')")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','$discount1','$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;
			$totaldet=$qty * $price * $hh;
			$branchpo=$row['branch_id'];
			$variation=$row['variation'];
			$color=$row['color'];
			$thicknew=$row['thick'];
			$mystatus=$row['status'];
			
		$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,total,sales_id,hh,ww,variation,color,branch,thick,status) VALUES('$pid','$qty','$golop','$totaldet','$mysales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew','$mystatus')")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='hardwarereceipt.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','$discount1','$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'];
			$mystatus=$row['status'];
			
			$golop=$hh*$price;
			$totaldet=$qty * $price * $hh;
			$branchpo=$row['branch_id'];
			mysqli_query($con,"INSERT INTO sales_details(prod_id,qty,price,total,sales_id,hh,ww,variation,color,branch,thick,status) VALUES('$pid','$qty','$golop','$totaldet','$sales_id','$hh','$ww','$variation','$color','$branchpo','$thicknew','$mystatus')")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='hardwarereceipt.php?sales_id=$sales_id'</script>";  		
}

	}
		
		
		
		
	
?>