<?php
include('config.php');
include('code.php');


if(isset($_GET['admin']))
$officer=$_GET['admin'];
if(isset($_GET['bcode']))
$bcode=$_GET['bcode'];

$date=date('Y-m-d');
$mon=date('m');
$yr=date('Y');

$sql =$link->query("select * from dailystockreport where BranchCode='$bcode' and date='$date'");
$num = mysqli_num_rows($sql);
		if($num>0){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode'");
		$sq =$link->query("update login set status='NO' where usertype='Sales'");
				$prev=0;
				while($reply = mysqli_fetch_array($result)){
					$code=$reply['code'];
					$name=$reply['name'];
					$quan=$reply['quantity'];
					
					$sql =$link->query("update dailystockreport set closing='$quan' where BranchCode='$bcode' and code='$code' and date='$date' ");
					
				}
				CloseUnit1($link,$date);
				CloseUnit2($link,$date);
				echo "<script>alert('Stock  Succesfully closed')</script>";
			echo "<script>location='dashboard.php'</script>";
}else{
	
	echo "<script>alert('Stock Has Not Been Opened Today')</script>";
			echo "<script>location='dashboard.php'</script>";
}
//===========================================================================================================================================================================
function CloseUnit1($link,$date){
	
			$result=mysqli_query($link,"select * from items where BranchCode='001'");
				$prev=0;
				while($reply = mysqli_fetch_array($result)){
					$code=$reply['code'];
					$name=$reply['name'];
					$quan=$reply['quantity'];
					
					$sql =$link->query("update dailystockreport set closing='$quan' where BranchCode='001' and code='$code' and date='$date' ");
					
				}
					
}
//===========================================================================================================================================================================
function CloseUnit2($link,$date){
	
			$result=mysqli_query($link,"select * from items where BranchCode='002'");
				$prev=0;
				while($reply = mysqli_fetch_array($result)){
					$code=$reply['code'];
					$name=$reply['name'];
					$quan=$reply['quantity'];
					
					$sql =$link->query("update dailystockreport set closing='$quan' where BranchCode='002' and code='$code' and date='$date' ");
					
				}
					
}
?>