<?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<1){
		$result=mysqli_query($link,"select * from items where BranchCode='$bcode'");
		$sq =$link->query("update login set status='YES' where usertype='Sales'");
				$prev=0;
				while($reply = mysqli_fetch_array($result)){
					$code=$reply['code'];
					$name=$reply['name'];
					$quan=$reply['quantity'];
					$retail=$reply['nprice'];
					$lar=$reply['lar'];
					$vsm=$reply['vsm'];
					$psn=$reply['psn'];
					$cost=$reply['cost'];
					
					$sql =$link->query("insert into dailystockreport values(0, '$bcode','$date','$code','$name','$quan','0','0',$mon,$yr,'$officer')");
					$sql =$link->query("insert into summary values(0, '$bcode','$date','$code','$name','0','$retail','0','$lar','0','$vsm','0','$psn',$mon,$yr,'$officer')");
					
				}
			OpenUnit1($link,$date);
			OpenUnit2($link,$date);	
			echo "<script>alert('Stock Opened Succesfully')</script>";
			echo "<script>location='dashboard.php'</script>";
				
}else{
	
	echo "<script>alert('Stock Has Been Opened Today')</script>";
			echo "<script>location='dashboard.php'</script>";
}
//================================================================POINT 1==========================================================================================================

function OpenUnit1($link,$date){
	$date=date('Y-m-d');
$mon=date('m');
$yr=date('Y');

			$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'];
						$retail=$reply['price1'];
						$cost=$reply['cost'];
						
						$sql =$link->query("insert into dailystockreport values(0, '001','$date','$code','$name','$quan','0','0',$mon,$yr,'')");
						$sql =$link->query("insert into summary values('NULL', '001','$date','$code','$name','0','$retail','0','$cost',$mon,$yr,'')");
						
					}
					
}

//================================================================POINT 1==========================================================================================================

function OpenUnit2($link,$date){
	$date=date('Y-m-d');
$mon=date('m');
$yr=date('Y');

			$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'];
						$retail=$reply['price1'];
						$cost=$reply['cost'];
						
						$sql =$link->query("insert into dailystockreport values(0, '002','$date','$code','$name','$quan','0','0',$mon,$yr,'')");
						$sql =$link->query("insert into summary values('NULL', '002','$date','$code','$name','0','$retail','0','$cost',$mon,$yr,'')");
						
					}
					
}
?>