<?php
include("code.php");
authorize();
$name=$_SESSION['admin_name'];
$bcode=$_SESSION['branchcode'];
$date=date('Y-m-d');





if (isset($_POST['pos'])){
	$date=date('Y-m-d');
	$item=$_POST['item'];
	$qnt=$_POST['quan'];
	$price=$_POST['price'];
	$amount=getPAmount($link,$bcode,$item,$price);
	//$itcode= getProductcode($link,$bcode,$item);
	$amnt=getAmount($link,$bcode,$item);
	$qnts= getItemBal($link,$bcode,$item);
	//$status=getstatus($link,$bcode,$item);
	$tot=$qnt*$amount;
	$officer=$name;
		if($qnt!=""){
			$ssqll =$link->query("select * from dailystockreport where date='$date'");
			$num=mysqli_num_rows($ssqll);
			if($num>0){
		
						 if($qnts >=$qnt){
							 
							 	if($amount >= $amnt){
							 
								$ssql3 =$link->query("insert into cart values(0,'$bcode', '$date','$item','$qnt','$amount','$tot','$price','$officer')");
					
								if($ssql3){
											echo "<script>location='pos.php?name=$name&bcode=$bcode'</script>";} else {$result = "<div class='error'>Savings not Succesfull. Try again</div>";};
											
									}else{
									echo "<script>alert('This Amount Is Too Low')</script>";
												echo "<script>location='pos.php'</script>";
										}
											
							}else{
								echo "<script>alert('Insufficient Quantity')</script>";
											echo "<script>location='pos.php'</script>";
							}
			}else{
				echo "<script>alert('Daily Stock Not Yet Done')</script>";
										echo "<script>location='pos.php'</script>";
			}
						
										
				}else{
					
					echo "<script>alert('Please Supply Quantity')</script>";
										echo "<script>location='pos.php'</script>";
				}
		
}

if (isset($_GET['pname'])){
	$date=date('Y-m-d');
	$item=$_GET['pname'];
	$qnt=1;
	$price='nprice';
	$amount=getPAmount($link,$bcode,$item,$price);
	$amnt=getAmount($link,$bcode,$item);
	$qnts= getItemBal($link,$bcode,$item);
	$status=getstatus($link,$branch,$item);
	$tot=$qnt*$amount;
	$officer=$name;
		if($qnt!=""){
			$ssqll =$link->query("select * from dailystockreport where date='$date'");
			$num=mysqli_num_rows($ssqll);
			if($num>0){
			
						 if($qnts >=$qnt){
							 
							 	if($amount >= $amnt){
							 
								$ssql3 =$link->query("insert into cart values(0,'$bcode', '$date','$item','$qnt','$amount','$tot','$price','$officer')");
					
								if($ssql3){
											echo "<script>location='pos.php'</script>";} else {$result = "<div class='error'>Savings not Succesfull. Try again</div>";};
											
									}else{
									echo "<script>alert('This Amount Is Too Low')</script>";
												echo "<script>location='pos.php'</script>";
										}
											
							}else{
								echo "<script>alert('Insufficient Quantity')</script>";
											echo "<script>location='pos.php'</script>";
							}
			}else{
				echo "<script>alert('Daily Stock Not Yet Done')</script>";
										echo "<script>location='pos.php'</script>";
			}
						
										
				}else{
					
					echo "<script>alert('Please Supply Quantity')</script>";
										echo "<script>location='pos.php'</script>";
				}
		
		}



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 getTotalCart($link,$bcode,$date,$name){
		$result=mysqli_query($link,"select * from cart where BranchCode='$bcode' and date='$date' and officer='$name'");
		$bal=0;
		while($reply = mysqli_fetch_array($result)){
			$amnt=$reply['total'];
			$bal+=$amnt;
		}
		return $bal;
}
	function getLimit($link,$bcode,$itname){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode' and name='$itname'");
		$reply = mysqli_fetch_array($result);
			$amnt=$reply['price'];
			$bal=$amnt;
		return $bal;
}

function getPAmount($link,$bcode,$itname,$price){
	if($price=="nprice"){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode' and name='$itname'");
		$reply = mysqli_fetch_array($result);
			$amnt=$reply['nprice'];
			$bal=$amnt;
		return $bal;
	}elseif($price=="lar"){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode' and name='$itname'");
		$reply = mysqli_fetch_array($result);
			$amnt=$reply['lar'];
			$bal=$amnt;
		return $bal;
	}elseif($price=="vsm"){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode' and name='$itname'");
		$reply = mysqli_fetch_array($result);
			$amnt=$reply['vsm'];
			$bal=$amnt;
		return $bal;
	}elseif($price=="psn"){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode' and name='$itname'");
		$reply = mysqli_fetch_array($result);
			$amnt=$reply['psn'];
			$bal=$amnt;
		return $bal;
	}else{
		return $bal=0;
	}
	
}


//==================================================================================
function getItemBal($link,$bcode,$code){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode' and name='$code'");
		$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 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;		
		}
	}
function getQntSummary($link,$date,$bcode,$code,$status){
		$result=mysqli_query($link,"select * from summary where date='$date' and BranchCode='$bcode' and code='$code'");
		$row=mysqli_fetch_array($result);
		if($status=="nprice")
		{
			$qnt=$row['quantity'];
			return $qnt;
		}elseif($status=="lar"){
			$qnt=$row['lquan'];
			return $qnt;
		}elseif($status=="vsm"){
			$qnt=$row['vquan'];
			return $qnt;
		}elseif($status=="psn"){
			$qnt=$row['pquan'];
			return $qnt;
		}else{
			return 0;		}
	}	
	
	function getSearchItem($link,$itname){
		$search="%{$itname}%";
		$sql = "SELECT * FROM items where name like '$search'";
		$rst = mysqli_query($link,$sql);
		$row_just = mysqli_fetch_assoc($rst);
		$num=mysqli_num_rows($rst);
		return $cpos;
	}	
	
	function UpdateSummary($link,$date,$bcode,$code,$qnt,$status){
	
		if($status=="nprice")
		{
			$sq =$link->query("update summary set quantity='$qnt' where BranchCode='$bcode' and code='$code' and date='$date'");
			return "";	
		}elseif($status=="lar"){
			$sq =$link->query("update summary set lquan='$qnt' where BranchCode='$bcode' and code='$code' and date='$date'");
			return "";	
		}elseif($status=="vsm"){
			$sq =$link->query("update summary set vquan='$qnt' where BranchCode='$bcode' and code='$code' and date='$date'");
			return "";	
		}elseif($status=="psn"){
			$sq =$link->query("update summary set pquan='$qnt' where BranchCode='$bcode' and code='$code' and date='$date'");
			return "";	
		}
	}		
//======================================================================================


?>