<?php
$PageSecurity = 1;
 $title = _('Invoice');
 include('Trial_function.php');
 	//include('config_crm.php');
	//include('includes/header.inc');
 global $db;	echo '<link href="'.$rootpath. '/css/'. $_SESSION['Theme'] .'/default.css" rel="stylesheet" type="text/css" />';
function GetClient($ClientId, $CBranch)
{
 global $db; 
 $arr=array();
 $explode_client=explode(',',$ClientId);
	 for($i=0;$i<count($explode_client);$i++)
	 {
			$GetRecord2="select client_id  from invoice A   where A.branch_name='".$CBranch."'  and A.client_id  ='".$explode_client[$i]."' and CompanyId='".$_SESSION['ACGCompanyId']."'";
	
		$ResultData2 = DB_query($GetRecord2,$db);
		 if(DB_num_rows($ResultData2)>0)
		{
			 $RowDataInvoice = DB_fetch_array($ResultData2);
			 $arr[]=$RowDataInvoice['client_id']  ;
		}
	 }
	return($arr);
}


 
function GetClient_InvoiceAmount($ClientId, $CBranch)
{
 global $db;
    $ResultInvoice=DB_query("select sum(grand_total) as grand_total  from invoice A   where A.branch_name='".$CBranch."'  and A.client_id in ($ClientId) and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
    if(DB_num_rows($ResultInvoice)>0)
	{
	   	$RowDataInvoice = DB_fetch_array($ResultInvoice);
 	    return($RowDataInvoice['grand_total']) ;
 	}
  
}
	
function GetClient_InvoiceCRAmount($ClientId, $CBranch)
{
 global $db;
      $GetRecord2 = "Select sum(A.Amount) as Amount from cashbankvoucher_master A,cashbankvoucher B where B.TransId=A.voucherMasterId and   B.clientid='".$ClientId."'  and BranchID='".$CBranch."'";
   $ResultInvoice = DB_query($GetRecord2,$db);
     if(DB_num_rows($ResultInvoice)>0)
	{
	   	$RowDataInvoice = DB_fetch_array($ResultInvoice);
		 
 	    return($RowDataInvoice['Amount']) ;
 	}
  
}
 if($_REQUEST['BtnClient'])
{
 if($_REQUEST['ClientId']!='')
 $url="main.php?route=TrialVoucher&CashBankId=". $_REQUEST['CashBankId']."&Lid=".$_REQUEST['Lid']."&ClientId=".$_REQUEST['companyclient']."&CBranch=".$_REQUEST['CBranch'];
else
 $url="main.php?route=TrialVoucher&CashBankId=". $_REQUEST['CashBankId']."&Lid=".$_REQUEST['Lid']."&CBranch=".$_REQUEST['CBranch'];
   $s->pageLocation($url);
  }
   
$ClientId=  $_REQUEST['ClientId'];
$ClientIDS=GetClient($_REQUEST['ClientId'], $_REQUEST['CBranch']);
if(count($ClientIDS)>0)
{
   $CIL1=implode(",",$ClientIDS);
   	 $CIL=SeperateByCommes($CIL1);
	 
    $Result_branch=DB_query("select A.id,A.c_name,A.ledger_id  from clientmaster A  where A.id in ($CIL) and A.CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	 
}
else{
    $Result_branch=DB_query("select A.id,A.c_name,A.ledger_id from clientmaster A   where  A.id in ($ClientId) and A.CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
 	}
  if(!$Result_branch) 
  die("Error".DB_error());
 
  ?>
<form action="" method="post">
<h2> <center> Branch Details</center></h2>
  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="maintable" style="margin-top:30px;">
  <tr>
  <td></td><td></td>
    <td><strong>DR</strong></td>
	  <td><strong>CR</strong></td>
  </tr>
<?php 
while($row_branch=DB_fetch_array($Result_branch))
{
 ?>
 <tr><td width="36%">
	 <input id="companyclient" name="companyclient" type="radio"     value="<?php echo $row_branch['id'];?>" /><?php echo $row_branch['c_name'] ;?>
	 	 <input name="CashBankId" id="CashBankId" type="hidden" value="<?php echo $_REQUEST['CashBankId']?>" />
	     <input name="ClientId" id="ClientId" type="hidden"  value="<?php echo $_REQUEST['ClientId']?>" />
		   <input name="Lid" id="Lid" type="hidden"  value="<?php echo $_REQUEST['Lid']?>" />
		      <input name="CBranch" id="CBranch" type="hidden"  value="<?php echo $_REQUEST['CBranch']?>" />

 </td>
 <td width="4%"><?php // echo  $row_branch['id'];?></td>
  <td width="20%"><?php  echo GetClient_InvoiceAmount($row_branch['id'], $_REQUEST['CBranch']);?> </td>

  <td width="20%"><?php  echo GetClient_InvoiceCRAmount($row_branch['id'], $_REQUEST['CBranch']);?> </td>

 </tr>
<?php } ?>		
<tr ><td colspan="4">&nbsp;</td></tr>
		<td colspan="4"><center><input name="BtnClient" type="submit" value="View" /></center></td>
 </tr>
 </table>
</form>