<?php
include('config.php');
include('code.php');
$name=$_SESSION['admin_name'];
$bcode=$_SESSION['branchcode'];

function getReceipt($link,$bcode){
		$sq=mysqli_query($link,"select * from payment where BranchCode='$bcode'");
	$row_just = mysqli_fetch_assoc($sq);
	$nm=mysqli_num_rows($sq);
	$num=$nm + 1;
	return $num;
}

function getItemBal($link,$bcode,$name){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode' and name='$name'");
		$row=mysqli_fetch_array($result);
		$cpos=$row['quantity'];
		return $cpos;
	}
	function getItemCost($link,$bcode,$itname){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode' and name='$itname'");
		$row=mysqli_fetch_array($result);
		$cpos=$row['cost'];
		return $cpos;
	}
	function getItemCate($link,$bcode,$itname,$date,$qnt){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode' and name='$itname'");
		$row=mysqli_fetch_array($result);
		$cate=$row['category'];
		if($cate=='BOTTLE'){
			$reslt=mysqli_query($link,"select * from dailyemptystock where BranchCode='$bcode' and name='$itname' and date='$date'");
			$rw=mysqli_fetch_array($reslt);
			$sin=$rw['StockIN'];
			$tqnt=$qnt + $sin;

		$rest=mysqli_query($link,"update dailyemptystock set StockIN='$tqnt' where BranchCode='$bcode' and name='$itname' and date='$date'");	
			return "1";

		}else{return "";}
	}

	function UpdateSummary($link,$date,$bcode,$name,$qnt,$status){
	
		if($status=="nprice")
		{
			$sq =$link->query("update summary set quantity='$qnt' where BranchCode='$bcode' and name='$name' and date='$date'");
			return "";	
		}elseif($status=="lar"){
			$sq =$link->query("update summary set lquan='$qnt' where BranchCode='$bcode' and name='$name' and date='$date'");
			return "";	
		}elseif($status=="vsm"){
			$sq =$link->query("update summary set vquan='$qnt' where BranchCode='$bcode' and name='$name' and date='$date'");
			return "";	
		}elseif($status=="psn"){
			$sq =$link->query("update summary set pquan='$qnt' where BranchCode='$bcode' and name='$name' and date='$date'");
			return "";	
		}
	}		

function getDebtors($link,$bcode,$dbt,$date,$name,$officer){
		if($dbt>0){
		$sq =mysqli_query($link,"insert into debtors values(0,'$bcode','$date','$name','$dbt','$officer')");
		return "";	
		}else{
			return 0;		
		}
	}
if(isset($_POST['payment'])){
			$amnt1=$_POST['amnt1'];
			$pos=$_POST['pos'];
			$trans=$_POST['trans'];
			$cash=$_POST['cash'];
			$amt=$pos + $trans + $cash;
			$dbt=floatval($_POST['debit']);
			$amnt=$amt + $dbt;
			$customer=$_POST['cusname'];
			$imei=getReceipt($link,$bcode);
			$mode=$_POST['mode'];
			$date=date('Y-m-d');
			$mon=date('m');
			$yr=date('Y');
			$time=date("h:i a" );
			$tell="IN".$date.rand(0,7729); 
			$teller=$imei.$tell;
	if($amnt > $amnt1 || $amnt < $amnt1){
		echo "<script>alert('Amount Not Balance')</script>";
		echo "<script>location='pos.php'</script>";
	}else if($mode=='Null'){
		echo "<script>alert('Please Select The Correct Mode Of Payment')</script>";
		echo "<script>location='pos.php'</script>";
	}
	else if($amnt==''){
		echo "<script>alert('Please Enter An Actual Amount')</script>";
		echo "<script>location='pos.php'</script>";
	}
	else{
			$sql =$link->query("select * from cart where BranchCode='$bcode' and date='$date' and officer='$name'");
				while($reply = mysqli_fetch_array($sql)){
						$itname=$reply['name'];
						 $quan=floatval($reply['quantity']);
						$price=$reply['price'];
						$total=$reply['total'];
						$admin=$reply['officer'];
						$status=$reply['cate'];
						$qnt= getItemBal($link,$bcode,$itname);
						$sqnt=floatval(getQntSummary($link,$date,$bcode,$itname,$status));
						$cost=getItemCost($link,$bcode,$itname);
						$qbal=$qnt - $quan;
						$sqntbal=$sqnt + $quan;
						$tcost=$quan*$cost;
						$it=getItemCate($link,$bcode,$itname,$date,$quan);
						$profit=$total-$tcost;
						
					$sq =$link->query("update items set quantity='$qbal' where BranchCode='$bcode' and name='$itname'");	
					$sql2 =$link->query("insert into invoice values(0,'$bcode','$teller','$date','$time','$customer','$itname','$quan','$price','$total','$imei','$mode',$mon,$yr,'$name')");
					$sq2 =$link->query("insert into profit values(0,'$bcode','$date','$itname','$quan','$profit',$mon,$yr,'$name')");		
					$val=UpdateSummary($link,$date,$bcode,$itname,$sqntbal,$status);
					}
			
				$sql3 =$link->query("insert into payment values(0, '$bcode','$date','$teller','$customer','Cash','$cash',$mon,$yr,'$name')");
				$sql4 =$link->query("insert into payment values(0, '$bcode','$date','$teller','$customer','POS','$pos',$mon,$yr,'$name')");
				$sql4 =$link->query("insert into payment values(0, '$bcode','$date','$teller','$customer','TRANSFER','$trans',$mon,$yr,'$name')");
				$dt=getDebtors($link,$bcode,$dbt,$date,$customer,$name);
								if($mode=="Cash"){
								$sql4 =$link->query("insert into cashbook values(0, '$bcode','$date','$teller','$time','$customer','$amnt','0','0','0',$mon,$yr,'$name')");
								$sql7 =$link->query("insert into journal values(0, '$bcode','$date','100','Cash Account','0','$amnt',$mon,$yr,'$name')");
								$sql8 =$link->query("insert into journal values(0, '$bcode','$date','101','Sales Account','$amnt','0',$mon,$yr,'$name')");
								
								}else{
									$sql5 =$link->query("insert into cashbook values(0, '$bcode','$date','$teller','$time','$customer','0','$amnt','0','0',$mon,$yr,'$admin')");
									$sql6 =$link->query("insert into bcashbook values(0, '$bcode','$date','$teller','$customer','$amnt','0','0',$mon,$yr,'$admin')");
									$sql9 =$link->query("insert into journal values(0, '$bcode','$date','100','Cash Account','0','$amnt',$mon,$yr,'$admin')");
									$sql10 =$link->query("insert into journal values(0, '$bcode','$date','101','Sales Account','$amnt','0',$mon,$yr,'$admin')");
									}
									
						$sql10 =$link->query("delete from cart where BranchCode='$bcode' and officer='$name'");			
						//echo "<script>alert('Payment Made Succefully
						//echo "<script>location='receipt.php?teller=$teller&date=$dat
						header("location: receipt.php?teller=$teller&date=$date&time=$time");
	}

}
?>