<?php include_once('database_connection.php');
$userid="";
if(isset($_GET['action']) && $_GET['action']=="checkusersession")
{
	if(isset($_SESSION['userid']))
	$d=true;
	else
	$d=false;

	echo $d;
}
if(isset($_POST['username']))
{
//print_r($_POST);
	$username=$_POST['username'];
	$password=$_POST['password'];
	  $PeriodID=$_POST['Period'];

	//$BRANCHID=$_POST['branchid'];
$upassword="";
	 $sql="select UserName,convert(nvarchar,[Password]) as password,UserID,PassCode,Picture,fullname,phone,gender,SalesCounterID,CompanyID from UserAccounts where UserName='$username'";
	$result=odbc_exec($connection,$sql);
	if(odbc_num_rows($result)>0)
	{
		$upassword=odbc_result($result,2);
		$userid=odbc_result($result,3);
		$PassCode=odbc_result($result,4);
		$Picture=odbc_result($result,5);
		$fullname=odbc_result($result,6);
		$phone=odbc_result($result,7);	
		$gender=odbc_result($result,8);
		$BRANCHID=odbc_result($result,9);							
		$CompanyID=odbc_result($result,10);
	}
	if($password==$upassword)
	{
		$_SESSION['userid']=$userid;
		$_SESSION['BRANCHID']=$BRANCHID;
		$_SESSION['UserName']=strtoupper($username);
		$_SESSION['PassCode']=$PassCode;
		$_SESSION['Picture']=$Picture;
		$_SESSION['fullname']=$fullname;
		$_SESSION['phone']=$phone;
		$_SESSION['gender']=$gender;
		$_SESSION['CompanyID']=$CompanyID;
		$_SESSION['PeriodID']=$PeriodID;									
		// get Running Period
		//
		$sqlsetting="select COAID,'CASHINHAND' as COAName from accounts where COAName like '%cash in hand%' and Degree=5
union
select COAID,'CASHCUSTOMERS' as COAName from accounts where COAName like '%cash customers%' and Degree=5
union
select COAID,'PURCHASES' as COAName from accounts where COAName like '%Purchases%' and Degree=5
union
select COAID,'PURCHASERETURN' as COAName from accounts where COAName like '%Purchase Ret%' and Degree=5
union
select COAID,'VATSALES' as COAName from accounts where COAName like '%VAT-OUTPUT%' and Degree=5
union
select COAID,'VATPURCHASE' as COAName from accounts where COAName like '%VAT-INPUT%' and Degree=5
union
select COAID,'VATPURCHASE' as COAName from accounts where COAName like '%VAT-INPUT%' and Degree=5
union
select COAID,'SALES' as COAName from accounts where COAName like 'sales%' and Degree=5
union
select COAID,'CREDITCARD' as COAName from accounts where COAName like 'credit card%' and Degree=5
union
select COAID,'RTAHEAD' as COAName from accounts where COAName like '%(RTA)%' and Degree=5
";
$sqlsetting="select * from master_values where textboxname='accountsetup'";

		$resultsetting=odbc_exec($connection,$sqlsetting);
		while($row=odbc_fetch_array($resultsetting))
		{
			$COAName=$row['MasterValue'];
			$_SESSION['SETTING'][$COAName]=$row['Code'];
		}
		$_SESSION['SETTING']['OPENINGBALANCE']=true;
		$sqlperiod="select top 1 MonthID
      ,StartDate
      ,EndDate
      ,DaysInMonth
      ,MonthDesc,active from monthmaster where active=1 ";
		$resultperiod=odbc_exec($connection,$sqlperiod);
		if(odbc_num_rows($result)>0)
		{
		$_SESSION['StartDate']=odbc_result($resultperiod,2);
		$_SESSION['EndDate']=odbc_result($resultperiod,3);
		$_SESSION['DaysInMonth']=odbc_result($resultperiod,4);
		$_SESSION['MonthDesc']=odbc_result($resultperiod,5);
						
		}
	$sqlsetting="select * from master_values where textboxname='accountsetup'";

		$resultsetting=odbc_exec($connection,$sqlsetting);
		while($row=odbc_fetch_array($resultsetting))
		{
			$COAName=$row['MasterValue'];
			$_SESSION['SETTING'][$COAName]=$row['Code'];
		}
	if($PeriodID!="")
    {
    $sqlperiod="select * from PeriodMaster where PeriodID=".$PeriodID;
    $resultperiod=odbc_exec($connection,$sqlperiod);
    while($row=odbc_fetch_array($resultperiod))
    {
      //$COAName=$row['MasterValue'];
    $_SESSION['SETTING']['PeriodID']=$row['PeriodID'];
    $_SESSION['SETTING']['OpeningDate']=$row['OpeningDate'];
    $_SESSION['SETTING']['ClosingDate']=$row['ClosingDate'];
    $_SESSION['PeriodStatus']=1;//$row['StatusID'];

      }//end while
    }
		echo 1;
	}
	else
	{
		echo 0;
	}
//	echo $upassword;
}

if(isset($_GET['id']))
{
	$id=$_GET['id'];
$data=array();
	$result=odbc_exec($connection, "select *  FROM VisitMaster where id='$id'");
  while ($row = odbc_fetch_array($result)) {
//$id=$row[0];
  	
    $data []=$row;
    
  }
   echo json_encode($data);

}


?>