<?php include_once('database_connection.php');
$userid="";
if(isset($_GET['action']))
{
if($_GET['action']=="UpdateSyncTable")
{
//	print_r($_REQUEST);
$json = file_get_contents('php://input');	
//$data=json_decode($_REQUEST['data']);	
$data=json_decode($json);
//print_r($data);
$PrimaryKeyID=isset($data->PrimaryKeyID)?$data->PrimaryKeyID:0;
$TableName=isset($data->TableName)?$data->TableName:'';

$GodownID=isset($_GET['GodownID'])?$_GET['GodownID']:0;
if($GodownID!="" && $GodownID>0)
{
$value=getnamesingle("Godowns","Additionalinfo5","GodownID",$GodownID,$connection);
$value=getnamesingle("master.dbo.sysdatabases","name","dbid",$value,$connection);

$sqlupdate="update [".$value."].dbo.SyncTableStatus set datamode=1 where GodownID=$GodownID and PrimaryKeyID=$PrimaryKeyID and TableName='$TableName'";
odbc_exec($connection,$sqlupdate) or die(odbc_error().$sqlupdate);
if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>1,'Message'=>'Record Updated Sucessfully');
      	//unset($_SESSION['InvoiceID']);
  }
  else
  {
      odbc_rollback($connection);
      $res=array('status'=>0,'Message'=>'ERROR !: Record NOT Updated');

  }
 echo json_encode($res); 
}//end if 
else
{
  $res=array('status'=>0,'Message'=>'ERROR !: Record NOT Updated');	
  echo json_encode($res); 
}

}

if($_GET['action']=="getSyncListUnPost")
{
	$data=array();
$GodownID=$_GET['GodownID'];
$value=getnamesingle("Godowns","Additionalinfo5","GodownID",$GodownID,$connection);
$value=getnamesingle("master.dbo.sysdatabases","name","dbid",$value,$connection);

$sql="select TableName,PrimaryKeyID,PrimaryFieldName,datamode,GodownID from [".$value."].dbo.SyncTableStatus where GodownID=".$GodownID." and datamode=0";
$result=odbc_exec($connection,$sql) or die(odbc_error().$sql);
while($row=odbc_fetch_array($result))
{
		foreach ($row as $key => $value) {
					# code...
					$row[$key]=utf8_encode($value);
				}
$row['PrimaryKeyID']=(int)$row['PrimaryKeyID'];
$row['datamode']=(int)$row['datamode'];
$data[]=$row;
}
echo json_encode($data);
}

if($_GET['action']=="SaveOrder2")
	{
		odbc_autocommit($connection,FALSE);
//print_r($_REQUEST);
//return;
$key=array_keys($_POST);
print_r($_POST);
//$record=json_encode($_POST['Records']);
$record=json_decode($_POST['Records']);
$data=json_decode($_POST['data']);
//print_r($data);
$CustomerName=$data->CustomerName;
//print_r(count($record));//[0]->CustomerID);
if($record==null || $record=="")
{
	$res=array('status'=>0,'Message'=>'Invalid Format Kindly Check Your Data','TotalRecordsAffected'=>0);
}
else
{
	$record[0]->UserID=$_SESSION['userid'];
$GlobalID=isset($record[0]->GlobalID)?$record[0]->GlobalID:0;	
$InvoiceID=isset($record[0]->InvoiceID)?$record[0]->InvoiceID:0;
$Unitid=1;
$CustomerID=isset($record[0]->CustomerID)?$record[0]->CustomerID:96;//$record->UserID;
if(!isset($record[0]->UserID))
{
$res=array('Status'=>0,'Message'=>'Missing USERID Invalid Format Kindly Check Your Data','TotalRecordsAffected'=>0);
echo json_encode($res);
return;	
}
//&& (!isset($_SESSION['customerIP']))
	 $sql="select * from salesinvoicemaster where BillNo='$GlobalID' and  CustomerID=".$CustomerID." and InvoiceStatusID=1";
	$result=odbc_exec($connection,$sql);
	if(odbc_num_rows($result)>0)
	{
		$InvoiceID=odbc_result($result,'InvoiceID');
	}
		else
		{
if($InvoiceID==0)
{


	$_SESSION['customerIP']=$_SERVER['REMOTE_ADDR'];
	$_SESSION['UserName']=$_SERVER['REMOTE_ADDR'];
	$GodownID=1;
	$InvoiceDate=Date("Y-m-d");
	$Active=1;
	$InvoiceType=12;
	$TotalTaxAmount=0;
	$DiningTableID=1;
	$Posted=1;
	$KeyField='';
	$where='';
	$NetAmount=0;
	$KeyField=generateCode("SalesInvoiceMaster","InvoiceID",$where,$connection);
	$KeyField=generateCode("SalesInvoiceMaster","KeyField","1=1",$connection);
    $KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
    $KeyField="CUA-".$KeyField2;
	$InvoiceID=getMaxId("SalesInvoiceMaster","InvoiceID",$connection)+1;
	$_SESSION['InvoiceID']=$InvoiceID;
	//$NetAmount=0;
//insert into SalesInvoiceMaster 
/*$_SESSION['UserID']=$_SERVER['Remote']*/
$UserName="admin";
  $sql="insert into SalesInvoiceMaster(InvoiceID,InvoiceDate,GodownID,KeyField,InvoiceType,Active,
Posted,TotalTaxAmount,NetAmount,CreateLog,InvoiceStatusID,CustomerID,OrderStatusID,BillNo) values($InvoiceID,'$InvoiceDate',$GodownID,'$KeyField',$InvoiceType,$Active,
$Posted,$TotalTaxAmount,$NetAmount,'".$UserName."',1,".$CustomerID.",1,'$GlobalID')";
odbc_exec($connection,$sql);
}
}
//if($InvoiceID==0)$InvoiceID=$_SESSION['InvoiceID'];

for($a=0;$a<count($record);$a++) {
		# code...
		
$ItemID=isset($record[$a]->Item_ItemID)?$record[$a]->Item_ItemID:0;
$vatpert=getnamesingle("inventory","vatpercent","ID",$ItemID,$connection);
if($vatpert==null || $vatpert=="")$vatpert=0;
//[{"CustomerID",32,"InvoiceID":0,ProductID":1,"Qty",1,"Price":50,"SubTotal":52.5,"TaxAmount":2.5,"NetAmount":52.5}]
$Qty=isset($record[$a]->Qty)?$record[$a]->Qty:0;
$UnitPrice=isset($record[$a]->Rate)?str_replace('_','.',$record[$a]->Rate):0;
$Amount=isset($record[$a]->Amount)?str_replace('_','.',$record[$a]->Amount):0;
$Amount=$Qty*$UnitPrice;
$TaxAmount=$Amount*($vatpert/100);
//$TaxAmount=isset($record->TaxAmount)?str_replace('_','.',$record->TaxAmount):0;
//$NetAmount=isset($record->NetAmount)?str_replace('_','.',$record->NetAmount):0;
$NetAmount=$Amount+$TaxAmount;
//$InvoiceID=isset($record[$a]->InvoiceID)?$record[$a]->InvoiceID:0;
$Unitid=1;
$CustomerID=isset($record[$a]->CustomerID)?$record[$a]->CustomerID:0;//$record->UserID;
$_SESSION['userid']=isset($record[$a]->UserID)?$record[$a]->UserID:0;

  $sql="insert into SalesInvoiceDetail (ItemID,Qty,UnitPrice,Amount,NetAmount,InvoiceID,Unitid,TaxAmount) values($ItemID,$Qty,$UnitPrice,$Amount,$NetAmount,$InvoiceID,$Unitid,$TaxAmount)";
odbc_exec($connection,$sql);
$sql="update SalesInvoiceMaster set NetAmount=(select sum(NetAmount) from SalesInvoiceDetail where InvoiceID=$InvoiceID),CustomerName='$CustomerName',BillNo='$GlobalID',InvoiceStatusID=4 where InvoiceID=$InvoiceID";
}
//insert into SalesInvoiceDetail 
if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>1,'Message'=>'Invoice Generated Sucessfully','TotalRecordsAffected'=>$InvoiceID);
      	//unset($_SESSION['InvoiceID']);
  }
  else
  {
      odbc_rollback($connection);
      $res=array('status'=>0,'Message'=>'Invoice Can Not BE Generated','TotalRecordsAffected'=>0);

  }
}
		echo json_encode($res);

	}
	if($_GET['action']=="deletecart")
	{
		$data=array();
		$transactionid=$_GET['TransactionID'];
		$sql="delete from salesinvoicedetail where transactionid=".$transactionid;
		odbc_exec($connection,$sql);
		$data=array('status'=>'ok','description'=>'Item Deleted Sucessfully');
				echo json_encode($data);


	}
	if($_GET['action']=="cartlist")
	{
		$InvoiceID=0;
		$data=array();
		$sql="select * from salesinvoicemaster where customerid=".$_SESSION['userid'];
		$result=odbc_exec($connection,$sql);
			while($row=odbc_fetch_array($result))
			{
								foreach ($row as $key => $value) {
					# code...
					$row[$key]=utf8_encode($value);
				}
				
					$data['master'][]=$row;
					$InvoiceID=$row['InvoiceID'];
			}
		//code for salesinvoicedetail	
		$sql="select *,(select Name from inventory where id=ItemID) as Name  from SalesInvoiceDetail where InvoiceID=".$InvoiceID;
		$result=odbc_exec($connection,$sql);
			while($row1=odbc_fetch_array($result))
			{
					$data['details'][]=$row1;
			}
		echo json_encode($data);

	}
	if($_GET['action']=="getproductdata")
	{

$under=$_GET['ProductID'];
		$date=date("Y-m-d");
		$data=array();
		/*$sql="
select isnull(sum(TotalCreditCard),0) as CreditCard,isnull(sum(TotalCashReceived),0) as Cash,(isnull(sum(TotalCreditCard),0)+isnull(sum(TotalCashReceived),0)) as Sales,GodownID,
(select Name from Godowns where Godowns.GodownID=salesinvoicemaster.GodownID) as Branch from   salesinvoicemaster where InvoiceDate='$date  00:00:00'
group by GodownID
";*/
 $sql="select top 100 ID,Name,Under,StandardPrice2 as Price,PictureLocation,
(select Name from inventory  i where i.ID=inventory.Under) as Category 
from inventory where Degree=2 and Active=1 and under=$under and [group]=0 and HasIngredients=0 and StandardPrice2>0 ";
			$result=odbc_exec($connection,$sql);
			while($row=odbc_fetch_array($result))
			{
								foreach ($row as $key => $value) {
					# code...
					$row[$key]=utf8_encode($value);
				}
					$data[]=$row;
			}
		echo json_encode($data);
	
		
	
	}
	if($_GET['action']=="showsubcategory")
	{
		$data=array();
		$under=$_GET['under'];
		$sql="select top 6 * from inventory where ProductBaseType='Service' and under=$under and  active=1";
		$result=odbc_exec($connection,$sql);
			while($row=odbc_fetch_array($result))
			{
				foreach ($row as $key => $value) {
					# code...
					$row[$key]=utf8_encode($value);
				}
					$data[]=$row;
			}
		echo json_encode($data);

	
	}
	if($_GET['action']=="showcategory")
	{
		$sql="select top 6 * from inventory where ProductBaseType='Service' and Degree=1 and under=0 and [group]=1 and active=1";
		$result=odbc_exec($connection,$sql);
			while($row=odbc_fetch_array($result))
			{
				foreach ($row as $key => $value) {
					# code...
					$row[$key]=utf8_encode($value);
				}
					$data[]=$row;
			}
		echo json_encode($data);

	}
	if($_GET['action']=="addtocart")
	{
		odbc_autocommit($connection,FALSE);
//print_r($_REQUEST);
//return;
$key=array_keys($_POST);
//print_r($_POST);
//$record=json_encode($_POST['Records']);
$record=json_decode($_POST['Records']);
//print_r(count($record));//[0]->CustomerID);
if($record==null || $record=="")
{
	$res=array('status'=>0,'Message'=>'Invalid Format Kindly Check Your Data','TotalRecordsAffected'=>0);
}
else
{
$GlobalID=isset($record[0]->GlobalID)?$record[0]->GlobalID:0;	
$InvoiceID=isset($record[0]->InvoiceID)?$record[0]->InvoiceID:0;
$Unitid=1;
$CustomerID=isset($record[0]->CustomerID)?$record[0]->CustomerID:0;//$record->UserID;
if(!isset($record[0]->UserID))
{
$res=array('Status'=>0,'Message'=>'Missing USERID Invalid Format Kindly Check Your Data','TotalRecordsAffected'=>0);
echo json_encode($res);
return;	
}
//&& (!isset($_SESSION['customerIP']))
	 $sql="select * from salesinvoicemaster where BillNo='$GlobalID' and  CustomerID=".$CustomerID;
	$result=odbc_exec($connection,$sql);
	if(odbc_num_rows($result)>0)
	{
		$InvoiceID=odbc_result($result,'InvoiceID');
	}
		else
		{
if($InvoiceID==0)
{


	$_SESSION['customerIP']=$_SERVER['REMOTE_ADDR'];
	$_SESSION['UserName']=$_SERVER['REMOTE_ADDR'];
	$GodownID=1;
	$InvoiceDate=Date("Y-m-d");
	$Active=1;
	$InvoiceType=12;
	$TotalTaxAmount=0;
	$DiningTableID=1;
	$Posted=1;
	$KeyField='';
	$where='';
	$NetAmount=0;
	$KeyField=generateCode("SalesInvoiceMaster","InvoiceID",$where,$connection);
	$KeyField=generateCode("SalesInvoiceMaster","KeyField","1=1",$connection);
    $KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
    $KeyField="CUA-".$KeyField2;
	$InvoiceID=getMaxId("SalesInvoiceMaster","InvoiceID",$connection)+1;
	$_SESSION['InvoiceID']=$InvoiceID;
	//$NetAmount=0;
//insert into SalesInvoiceMaster 
/*$_SESSION['UserID']=$_SERVER['Remote']*/
$UserName="admin";
  $sql="insert into SalesInvoiceMaster(InvoiceID,InvoiceDate,GodownID,KeyField,InvoiceType,Active,
Posted,TotalTaxAmount,NetAmount,CreateLog,InvoiceStatusID,CustomerID,OrderStatusID,BillNo) values($InvoiceID,'$InvoiceDate',$GodownID,'$KeyField',$InvoiceType,$Active,
$Posted,$TotalTaxAmount,$NetAmount,'".$UserName."',1,".$CustomerID.",1,'$GlobalID')";
odbc_exec($connection,$sql);
}
}
//if($InvoiceID==0)$InvoiceID=$_SESSION['InvoiceID'];

for($a=0;$a<count($record);$a++) {
		# code...
		
$ItemID=isset($record[$a]->ProductID)?$record[$a]->ProductID:0;
$vatpert=getnamesingle("inventory","vatpercent","ID",$ItemID,$connection);
if($vatpert==null || $vatpert=="")$vatpert=0;
//[{"CustomerID",32,"InvoiceID":0,ProductID":1,"Qty",1,"Price":50,"SubTotal":52.5,"TaxAmount":2.5,"NetAmount":52.5}]
$Qty=isset($record[$a]->Qty)?$record[$a]->Qty:0;
$UnitPrice=isset($record[$a]->Price)?str_replace('_','.',$record[$a]->Price):0;
$Amount=isset($record[$a]->SubTotal)?str_replace('_','.',$record[$a]->SubTotal):0;
$Amount=$Qty*$UnitPrice;
$TaxAmount=$Amount*($vatpert/100);
//$TaxAmount=isset($record->TaxAmount)?str_replace('_','.',$record->TaxAmount):0;
//$NetAmount=isset($record->NetAmount)?str_replace('_','.',$record->NetAmount):0;
$NetAmount=$Amount+$TaxAmount;
//$InvoiceID=isset($record[$a]->InvoiceID)?$record[$a]->InvoiceID:0;
$Unitid=1;
$CustomerID=isset($record[$a]->CustomerID)?$record[$a]->CustomerID:0;//$record->UserID;
$_SESSION['userid']=isset($record[$a]->UserID)?$record[$a]->UserID:0;

  $sql="insert into SalesInvoiceDetail (ItemID,Qty,UnitPrice,Amount,NetAmount,InvoiceID,Unitid,TaxAmount) values($ItemID,$Qty,$UnitPrice,$Amount,$NetAmount,$InvoiceID,$Unitid,$TaxAmount)";
odbc_exec($connection,$sql);
$sql="update SalesInvoiceMaster set NetAmount=(select sum(NetAmount) from SalesInvoiceDetail where InvoiceID=$InvoiceID) where InvoiceID=$InvoiceID";
}
//insert into SalesInvoiceDetail 
if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>1,'Message'=>'Invoice Generated Sucessfully','InvoiceID'=>$InvoiceID);
      	//unset($_SESSION['InvoiceID']);
  }
  else
  {
      odbc_rollback($connection);
      $res=array('status'=>0,'Message'=>'Invoice Can Not BE Generated','InvoiceID'=>0);

  }
}
		echo json_encode($res);

	}//end addtocart

if($_GET['action']=="SaveOrder")
{
	$data=$_POST;
	$data=array_keys($data);

	$data1=$data[0];
	$data1="[".$data1."]";
//	print_r($data1);
	$data2=json_decode($data1);
	$data3=$data2[0];
//	return;
//$SalesInvoiceID=$_SESSION['InvoiceID'];
	$sql="select * from salesinvoicemaster where CustomerID=".$_SESSION['userid'];
	$result=odbc_exec($connection,$sql);
	if(odbc_num_rows($result)>0)
	{
		$SalesInvoiceID=odbc_result($result,'InvoiceID');
	}
$where="InvoiceType=12";
	$KeyField=generateCode("InvoiceMaster","InvoiceID",$where,$connection);
	$InvoiceID=getMaxId("InvoiceMaster","InvoiceID",$connection)+1;
            $KeyField2=str_pad($KeyField,5,"0",STR_PAD_LEFT);
            $KeyField="INQ-".$KeyField2;

$sql="insert into SalesInvoiceMaster (InvoiceID,InvoiceDate,GodownID,KeyField,InvoiceType,Active,
Posted,TotalTaxAmount,NetAmount,CreateLog,InvoiceStatusID) select $InvoiceID,InvoiceDate,GodownID,'$KeyField',InvoiceType,Active,
Posted,TotalTaxAmount,NetAmount,CreateLog,InvoiceStatusID from SalesInvoiceMaster where InvoiceID=$SalesInvoiceID";
 odbc_exec($connection,$sql);
// update customer Name 
$CustomerName=isset($data3->CustomerName)?$data3->CustomerName:"-";
$Mobile=isset($data3->Mobile)?$data3->Mobile:"-";
$Email=isset($data3->Email)?$data3->Email:"-";
$Narration=isset($data3->Narration)?$data3->Narration:"-";
$sqlupdate="update InvoiceMaster set Narration='$Narration',CustomerName='$CustomerName',MobileNumber='$Mobile',Email='$Email',InvoiceStatus=1 where InvoiceID=$InvoiceID";

odbc_exec($connection,$sqlupdate);

//insert invoicedetail 
$sql="insert into SalesInvoiceDetail (ItemID,Qty,UnitPrice,Amount,NetAmount,InvoiceID,Unitid) select ItemID,Qty,UnitPrice,Amount,NetAmount,$InvoiceID,Unitid from SalesInvoiceDetail where InvoiceID=$SalesInvoiceID";
odbc_exec($connection,$sql);
      
      $sql="delete from salesinvoicemaster where InvoiceID=".$SalesInvoiceID;
      odbc_exec($connection,$sql);

      $sql="delete from SalesInvoiceDetail where InvoiceID=".$SalesInvoiceID;
      odbc_exec($connection,$sql);

if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>"ok",'description'=>'Inquiry Generated Sucessfully','InvoiceID'=>$InvoiceID);
      session_destroy();
  }
  else
  {
      odbc_rollback($connection);
      $res=array('status'=>"Error",'description'=>'Invoice Can Not BE Generated');

  }
echo json_encode($res);
}	
	if($_GET['action']=="getsalesdetails")
	{
		$data=array();
		$GodownID=$_GET['GodownID'];
$InvoiceID=0;
$date=date("Y-m-d");
		$sql="select DiningTableID,(TotalAmount+TotalTaxAmount) as TotalAmount,StatusID,InvoiceID,GodownID,(select Name from Godowns where Godowns.GodownID=SalesInvoiceMaster.GodownID) as Company from SalesInvoiceMaster where StatusID=1 and InvoiceType=7 and InvoiceDate='$date 00:00:00'
and DiningTableID is not null order by GodownID";
			$result=odbc_exec($connection,$sql);
			while($row=odbc_fetch_array($result))
			{
					$data['master'][]=$row;
					$InvoiceID=$row['InvoiceID'];
			
					$sql="
select InvoiceID,ItemID,UnitPrice,QTY,Amount,NetAmount,TaxPercent,TaxAmount,Narration ,Unitid,
(select Name from inventory where ID=ItemID)as ItemName from SalesInvoiceDetail  where InvoiceID=$InvoiceID";
			$result1=odbc_exec($connection,$sql);
			while($row1=odbc_fetch_array($result1))
			{
							$data[$InvoiceID][]=$row1;

			}
		}

		echo json_encode($data);

	}
	if($_GET['action']=="getsales")
	{
		$date=date("Y-m-d");
		$data=array();
		/*$sql="
select isnull(sum(TotalCreditCard),0) as CreditCard,isnull(sum(TotalCashReceived),0) as Cash,(isnull(sum(TotalCreditCard),0)+isnull(sum(TotalCashReceived),0)) as Sales,GodownID,
(select Name from Godowns where Godowns.GodownID=salesinvoicemaster.GodownID) as Branch from   salesinvoicemaster where InvoiceDate='$date  00:00:00'
group by GodownID
";*/
$sql="select isnull(sum(TotalCreditCard),0) as CreditCard,
(isnull(sum(TotalCashReceived),0)-sum(TotalCreditCard)-sum(CashRefund)) as Cash,
(isnull(sum(TotalCashReceived),0)-isnull(sum(CashRefund),0)) as Sales,GodownID,
(select Name from Godowns where Godowns.GodownID=salesinvoicemaster.GodownID) as Branch 
from   salesinvoicemaster where InvoiceDate='$date  00:00:00' and StatusID=2
group by GodownID
";
			$result=odbc_exec($connection,$sql);
			while($row=odbc_fetch_array($result))
			{
				foreach ($row as $key => $value) {
					# code...
					$row[$key]=utf8_encode($value);
				}
					$data[]=$row;
			}
		echo json_encode($data);
	
	}
	if($_GET['action']=="getproducts")
	{

		$date=date("Y-m-d");
		$data=array();
		/*$sql="
select isnull(sum(TotalCreditCard),0) as CreditCard,isnull(sum(TotalCashReceived),0) as Cash,(isnull(sum(TotalCreditCard),0)+isnull(sum(TotalCashReceived),0)) as Sales,GodownID,
(select Name from Godowns where Godowns.GodownID=salesinvoicemaster.GodownID) as Branch from   salesinvoicemaster where InvoiceDate='$date  00:00:00'
group by GodownID
";*/
 $sql="select top 100 ID,Name,Under,StandardPrice2 as Price,PictureLocation,
(select Name from inventory  i where i.ID=inventory.Under) as Category 
from   inventory where Degree=2 and Active=1 and under>0 and [group]=0 and HasIngredients=0 and StandardPrice2>0 ";
			$result=odbc_exec($connection,$sql);
			while($row=odbc_fetch_array($result))
			{
								foreach ($row as $key => $value) {
					# code...
					$row[$key]=utf8_encode($value);
				}
					$data[]=$row;
			}
		echo json_encode($data);
	
		
	}
}
if(isset($_POST['username']))
{
//print_r($_POST);
	$username=$_POST['username'];
	$password=$_POST['password'];
$upassword="";
	 $sql="select UserName,convert(nvarchar,[Password]) as password,UserID,PassCode,Picture,fullname,phone,gender 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);							
	}
	if($password==$upassword)
	{
		$_SESSION['userid']=$userid;
		$_SESSION['UserName']=$username;
		$_SESSION['PassCode']=$PassCode;
		$_SESSION['Picture']=$Picture;
		$_SESSION['fullname']=$fullname;
		$_SESSION['phone']=$phone;
		$_SESSION['gender']=$gender;									
		// 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
";
		$resultsetting=odbc_exec($connection,$sqlsetting);
		while($row=odbc_fetch_array($resultsetting))
		{
			$COAName=$row['COAName'];
			$_SESSION['SETTING'][$COAName]=$row['COAID'];
		}
		$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);
						
		}
		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);

}


?>