<?php
include_once('database_connection.php');


if(isset($_GET['action']))
{

	if($_GET['action']=="symTopUp")
	{
		//print_r($_POST);
		//ApprovalToBePosted
	
		        odbc_autocommit($connection, FALSE);
		$data=array();
		//$data=$_POST['data'];
		//$data=json_decode($data);
	//	print_r($data);
		$date=Date("Y-m-d");
		$CardNumber=isset($_POST['CardNumber'])?$_POST['CardNumber']:0;
		$CustomerID =isset($_POST['CustomerID'])?$_POST['CustomerID']:0;
		$CardNumber=getNameSingle("POS_MemberSetup","CardNumber","AdditionalInfo1",$CustomerID,$connection);
    $InvoiceID =0;//isset($_POST['InvoiceID'])?$_POST['InvoiceID']:0;
    $Reference =isset($_POST['VoucherNo'])?$_POST['VoucherNo']:'';
    $LocationID =1;//isset($_POST['GodownID'])?$_POST['GodownID']:0;
    $SideBarID =42;//isset($_POST['SideBarID'])?$_POST['SideBarID']:0;
   // $InvoiceDate =isset($_POST['InvoiceDate'])?$_POST['InvoiceDate']:0;
   // $VoucherTypeID =isset($_POST['VoucherTypeID'])?$_POST['VoucherTypeID']:0;
    $Debit =isset($_POST['Amount'])?$_POST['Amount']:0;
    $Credit =0;//isset($_POST['CreditAmount'])?$_POST['CreditAmount']:0;
     $Narration="SemNox Advance :Reference : $Reference ".$_POST['Narration'];
		$VoucherID=$_POST['VoucherID'];
		//getNameSingle("VoucherMaster","VoucherID","ReferenceInvoiceID",$InvoiceID,$connection);
		$CardID=getMaxId("CardTransaction","ID",$connection);
		if($CardID==null)$CardID=0+1;
		else
		$CardID=$CardID+1;	
	 $sql="INSERT INTO [dbo].[CardTransaction]
           ([ID]
           ,[CardNumber]
           ,[COAID]
           ,[Debit]
           ,[Credit]
           ,[Narration]
           ,[TDate]
           ,[Reference]
           ,[VoucherID]
           ,[isActive]
           ,[CreateLog]
           ,[InvoiceID]
           ,[LocationID])
     VALUES($CardID,'$CardNumber',$CustomerID,$Debit,$Credit,'$Narration','$date','$Reference',$VoucherID,1,'".$_SESSION['UserName']."',$InvoiceID,$LocationID)";
    // return;
     odbc_exec($connection,$sql) or die(odbc_error().$sql);

     // update vouchermaster 
     $sql="update vouchermaster set ApprovalToBePosted=1 where VoucherID=$VoucherID";
     odbc_exec($connection,$sql) or die(odbc_error().$sql);

     //update invoicemaster
     //$sql="update invoicemaster set symreceived=$Credit,Paid=(isnull(Paid,0)+$Credit) where InvoiceID=$InvoiceID";
//odbc_exec($connection,$sql);     	
	   if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>"ok",'description'=>'Data Saved Sucessfully');
  }
else
{
      odbc_rollback($connection);
      $res=array('status'=>"Error",'description'=>'Record  Not Updated');

}
    echo json_encode($res);

	
	}
	if($_GET['action']=="selectVoucher")
	{
		$data=array();
		$TextBoxName=$_GET['TextBoxName'];
		$fieldid=$_GET['fieldid'];
		$fieldname=$_GET['fieldname'];
		$condition=$_GET['condition'];
		$LedgerID=$_GET['LedgerID'];
	$sql="select $fieldid,$fieldname,Totalamount from VoucherMaster where VoucherID in (select Voucherid from voucherdetails where Creditledgerid=$LedgerID) and $condition";
	$result=odbc_exec($connection,$sql);
		
		while($row=odbc_fetch_array($result))
		{

			foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	

		$data[]=$row;	
		}
	//	getdepreciation.php?action=selectVoucher&TextBoxName=VoucherMaster&fieldid=VoucherID&fieldname=VoucherNo&condition='+condition,
		echo json_encode($data);
	}
	if($_GET['action']=="GenerateSymPayment")
	{
		        odbc_autocommit($connection, FALSE);
		$data=array();
		$data=$_POST['data'];
		$data=json_decode($data);
	//	print_r($data);
		$date=Date("Y-m-d");
		$CardNumber=isset($data->CardNumber)?$data->CardNumber:0;
		$CustomerID =isset($data->CustomerID)?$data->CustomerID:0;
    $InvoiceID =isset($data->InvoiceID)?$data->InvoiceID:0;
    $Reference =isset($data->KeyField)?$data->KeyField:'';
    $LocationID =isset($data->GodownID)?$data->GodownID:0;
    $SideBarID =isset($data->SideBarID)?$data->SideBarID:0;
    $InvoiceDate =isset($data->InvoiceDate)?$data->InvoiceDate:0;
    $VoucherTypeID =isset($data->VoucherTypeID)?$data->VoucherTypeID:0;
    $Debit =isset($data->Debit)?$data->Debit:0;
    $Credit =isset($data->CreditAmount)?$data->CreditAmount:0;
    $data->TransactionNumber =isset($data->TransactionNumber)?$data->TransactionNumber:0;
    $Narration="SemNox Payment :Reference : $Reference";
		$VoucherID=getNameSingle("VoucherMaster","VoucherID","ReferenceInvoiceID",$InvoiceID,$connection);
		if($VoucherID==null)$VoucherID=0;
		$CardID=getMaxId("CardTransaction","ID",$connection);
		if($CardID==null)$CardID=0+1;
		else
		$CardID=$CardID+1;	
	$sql="INSERT INTO [dbo].[CardTransaction]
           ([ID]
           ,[CardNumber]
           ,[COAID]
           ,[Debit]
           ,[Credit]
           ,[Narration]
           ,[TDate]
           ,[Reference]
           ,[VoucherID]
           ,[isActive]
           ,[CreateLog]
           ,[InvoiceID]
           ,[LocationID])
     VALUES($CardID,'$CardNumber',$CustomerID,$Debit,$Credit,'$Narration','$date','$Reference',$VoucherID,1,'".$_SESSION['UserName']."',$InvoiceID,$LocationID)";
     odbc_exec($connection,$sql) or die(odbc_error().$sql);
     //update invoicemaster
     $sql="update invoicemaster set symCardNo='$CardNumber',symreceived=$Credit,Paid=(isnull(Paid,0)+$Credit) where InvoiceID=$InvoiceID";
odbc_exec($connection,$sql);     	
	   if (!odbc_error())
   {
      odbc_commit($connection);
      $res=array('status'=>"ok",'description'=>'Data Saved Sucessfully');
  }
else
{
      odbc_rollback($connection);
      $res=array('status'=>"Error",'description'=>'Record  Not Updated');

}
    echo json_encode($res);

	}
	if($_GET['action']=="GetCardBalance")
	{
		$data=array();
		$CardNumber=$_GET['CardNumber'];
		    $sql="select (sum(Debit)-sum(Credit)) as Balance,(select convert(date,ExpiryDate) from POS_MemberSetup where CardNumber='$CardNumber' and ExpiryDate>='".Date('Y-m-d')."') as ExpiryDate,(select AdditionalInfo1 from POS_MemberSetup where CardNumber='$CardNumber') as COAID from CardTransaction where COAID=(select top 1 AdditionalInfo1 from POS_MemberSetup where CardNumber='$CardNumber')
			
		";
		$result=odbc_exec($connection, $sql) or die(odbc_error().$sql);
		while($row=odbc_fetch_array($result))
		{
			$data[]=$row;
		}
	echo json_encode($data);	
	}
	if($_GET['action']=="GetPaymentHistory3")
	{

			$data=array();
			$sum=0;
			$ReferenceInvoiceID=$_GET['ReferenceInvoiceID'];

	$total=getNameSingle("invoicemaster","NetAmount","InvoiceID",$ReferenceInvoiceID,$connection);
	$Received=getNameSingle("invoicemaster","Paid","InvoiceID",$ReferenceInvoiceID,$connection);
	if($Received=="")$Received=0;
	$Balance=($total-$Received);//=getNameSingle("invoicemaster","Balance","InvoiceID",$ReferenceInvoiceID,$connection);	
$CashID=$_SESSION['SETTING']['CASHINHAND'];
$CreditCardID=$_SESSION['SETTING']['CREDITCARDCUSTOMER'];
			$sql="   select ROW_NUMBER() OVER (ORDER BY [VoucherID])  as row_num,VoucherID,VoucherNo,TotalAmount,VoucherDate,ReferenceInvoiceID,(select Paid from invoicemaster where invoiceid=$ReferenceInvoiceID) as Received,(select Balance from invoicemaster where invoiceid=$ReferenceInvoiceID) as Balance,(select TotalAmount from invoicemaster where invoiceid=$ReferenceInvoiceID) as TotalInvoice,(select sum(Debit) from voucherdetails where Voucherid=VoucherMaster.VoucherID and LedgerID=$CashID) as Cash,(select sum(Debit) from voucherdetails where Voucherid=VoucherMaster.VoucherID and LedgerID=$CreditCardID) as CreditCard from VoucherMaster where VoucherTypeID=7 and ReferenceInvoiceID=".$ReferenceInvoiceID;
			$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{

			foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	

		$sum +=$row['TotalAmount'];
		$data[][]=$row;	
		$data['VoucherID']=$row['VoucherID'];
		$data['VoucherNo']=$row['VoucherNo'];		
		}
		$data['total']=$total;
		$data['Received']=$Received;
		$data['Balance']=$Balance;				
		//$data['description']=$sum;
		//print_r($data);
		echo json_encode($data);

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

			$data=array();
			$sum=0;
			$ReferenceInvoiceID=$_GET['ReferenceInvoiceID'];

	$JobCardNo=getNameSingle("invoicemaster","ParkingNo","InvoiceID",$ReferenceInvoiceID,$connection);
$CashID=$_SESSION['SETTING']['CASHINHAND'];
$CreditCardID=$_SESSION['SETTING']['CREDITCARDCUSTOMER'];

			$sql=" select ROW_NUMBER() OVER (ORDER BY [InvoiceID])  as row_num, Invoiceid,KeyField,InvoiceDate,GodownID,InvoiceStatus, ParkingNo,isNull(Paid,0) as Paid,NetAmount-(isNull(Paid,0)) as Balance,NetAmount,SupplierId,(select sum(Debit) from voucherdetails where ReferenceInvoiceID=invoicemaster.InvoiceID and LedgerID=$CashID) as Cash,(select sum(Debit) from voucherdetails where ReferenceInvoiceID=invoicemaster.InvoiceID and LedgerID=$CreditCardID) as CreditCard from invoicemaster where InvoiceType=5  and ReferenceInvoiceID=".$ReferenceInvoiceID."";
			$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{

		/*	foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	
*/
		$sum +=$row['NetAmount'];
		$data['allinvoice'][]=$row;	
		}
$CashID=$_SESSION['SETTING']['CASHINHAND'];
$CreditCardID=$_SESSION['SETTING']['CREDITCARDCUSTOMER'];
		 $sql="
  select vm.VoucherID as VoucherID,vm.VoucherNo as VoucherNo,  convert(smalldatetime,vm.LastModificationTimeStamp) as VoucherDate,
  sum(vm.TotalAmount) as TotalAmount,im.CustomerName as CustomerName,
  (select sum(Debit) from voucherdetails where Voucherid=vm.VoucherID and LedgerID=$CashID) as Cash,(select sum(Debit) from voucherdetails where Voucherid=vm.VoucherID and LedgerID=$CreditCardID) as CreditCard,InvoiceID
from InvoiceMaster as im  inner join vouchermaster as vm on vm.ReferenceInvoiceID=im.InvoiceID 
where vm.Totalamount>0 and VoucherTypeID=7 
and im.ReferenceInvoiceID=$ReferenceInvoiceID  and vm.active=1
group by convert(smalldatetime,vm.LastModificationTimeStamp),CustomerName,VoucherID,VoucherNo,InvoiceID
";
		$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{

		/*	foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	
*/

		$sum +=$row['TotalAmount'];
		$data['payment'][]=$row;	
		}
		
	//	$data['total']=$total;
		//$data['description']=$sum;
	//	print_r($data);
		echo json_encode($data);

		
	}
		if($_GET['action']=="GetPaymentHistory")
		{
			$data=array();
			$sum=0;
			$ReferenceInvoiceID=$_GET['ReferenceInvoiceID'];


	$total=getNameSingle("invoicemaster","NetAmount","InvoiceID",$ReferenceInvoiceID,$connection);
	$Received=getNameSingle("invoicemaster","Paid","InvoiceID",$ReferenceInvoiceID,$connection);
		$SupplierId=getNameSingle("invoicemaster","SupplierId","InvoiceID",$ReferenceInvoiceID,$connection);
	if($Received=="")$Received=0;
	$Balance=($total-$Received);//=getNameSingle("invoicemaster","Balance","InvoiceID",$ReferenceInvoiceID,$connection);	

			 $sql="   select ROW_NUMBER() OVER (ORDER BY [VoucherID])  as row_num,VoucherID,VoucherNo,TotalAmount,VoucherDate,ReferenceInvoiceID,(select Paid from invoicemaster where invoiceid=$ReferenceInvoiceID) as Received,(select Balance from invoicemaster where invoiceid=$ReferenceInvoiceID) as Balance,(select TotalAmount from invoicemaster where invoiceid=$ReferenceInvoiceID) as TotalInvoice,(select SupplierId from invoicemaster where invoiceid=$ReferenceInvoiceID) as SupplierId from VoucherMaster where VoucherTypeID=7 and ReferenceInvoiceID=".$ReferenceInvoiceID;
			$result=odbc_exec($connection, $sql);
		/*	foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	
*/
			//$SupplierId=96;
			$count=0;
while($row=odbc_fetch_array($result))
		{
			$SupplierId=$row['SupplierId'];
			foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	

		$sum +=$row['TotalAmount'];
		$data['record'][]=$row;
		//$count++;	
		/*$data['VoucherID']=$row['VoucherID'];
		$data['VoucherNo']=$row['VoucherNo'];		
*/		}
		$data['summary']['total']=$total;
		$data['summary']['Received']=$Received;
		$data['summary']['Balance']=$Balance;
		$data['summary']['SupplierId']=$SupplierId;
		//$data['TotalInvoice']=$total;
		//$data['description']=$sum;
	//	print_r($data);
		echo json_encode($data);

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


		$data=array();
		if(isset($_GET['Under']))
		{
			$GLCode=$_GET['Under'];
		$GLCode="  acc.Under=".$GLCode;

		}
		else
		{
		$GLCode=" ";			
		}
		 $sql="select COAName,Hierarchy,Under,CostCenterID,acc.COAID as COAID,GLCode,
(select COAName  from accounts as c where c.COAID=acc.Under) as L3,
(select COAName  from accounts as c where c.COAID=(select Under from accounts as cc where cc.COAID=acc.Under)) as L2,
(select COAName  from accounts as c where c.COAID=(select Under from accounts as cc where cc.COAID=(select Under from accounts as ccc where ccc.COAID=acc.Under))) as COAHead,(select COAName  from accounts as c where c.COAID=(select Under from accounts as cc where cc.COAID=(select Under from accounts as ccc where ccc.COAID=acc.Under))) as L1
,'' as col_Action,Attr1,Attr2,Attr3,Attr4,Attr5,Attr6,Attr7,Attr8,Attr9,Attr10,Attr11,Attr12,Attr13,Attr14,Attr15,Attr16,Attr17,Attr18,Attr19,Attr20 from accounts as acc where  $GLCode";	
			$result=odbc_exec($connection, $sql);
		while($row=odbc_fetch_array($result))
		{

			foreach($row as $key=>$value)
			$row[$key]= utf8_encode($value);	

		$data[]=$row;	
		}
	//	print_r($data);
		echo json_encode($data);

	
	
		}
		if($_GET['action']=="GetDepreciationSchedule")
		{
			$data=array();
			$Under=$_GET['AssetCategoryID'];
			$sql="select vl.*,(vl.DepreciationPerDay*365) as YearlyDepreciation,(vl.DepreciationPerDay*30) as DepreciationAmount  from view_depreciationasset_list as vl
 where under=$Under and NatureID=6";
 				$master=array();
$master['AcquireValue']=0;$master['SalvageValue']=0;$master['DepreciableCost']=0;
$master['DepreciationAmount']=0;$master['DepreciationPerDay']=0;
$master['DepreciationAmount']=0;$master['YearlyDepreciation']=0;
				//$data['master'][]=$master;
			$result=odbc_exec($connection, $sql);
			while($row=odbc_fetch_array($result))
			{

			//	$data[]=$row;
				$data['master'][]=$row;
				$data['Detail'][]=$row;	
		}
	//	print_r($data);
		echo json_encode($data);

		}
}

?>