<?php  $PageSecurity = 10;
include('includes/session.inc');
 $title = _('Branch');
 include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');
include('Trial_function.php');
 include('includes/header.inc');

$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
   $rurl="&modid=".$modid."&frmid=".$frmid;

function BranchAmount($CBranch,$led)
{
global $db;
     $ResultInvoice=DB_query("select  sum(value_of_goods)  as value_of_goods  from invoice where ledger_id='".$led."' and branch_name='".$CBranch."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	 if(!$ResultInvoice)
		{
		   die('Error 101'.DB_error());
		}
   $RowDataInvoice = DB_fetch_array($ResultInvoice);
     return($RowDataInvoice['value_of_goods']) ;

}

function GetVatAmount($CBranch,$Lid)
{
 global $db;
		$ResultInvoice=DB_query("select sum(excise_duty)  as excise_duty from invoice A    where   A.excise_legder='".$Lid."' and  A.branch_name='".$CBranch."' and A.CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
		if(!$ResultInvoice)
		{
		   die('Error 101'.DB_error());
		}
   $RowDataInvoice = DB_fetch_array($ResultInvoice);
   
     return($RowDataInvoice['excise_duty']) ;
 }
 function GetEDU_CessAmount($CBranch,$Lid)
{
  global $db;
      	$ResultInvoice=DB_query("select sum(edu_cess) as edu_cess  from invoice A    where   A.edu_cess_ledger ='".$Lid."'   and  A.branch_name='".$CBranch."' and A.CompanyId='".$_SESSION['ACGCompanyId']."'   ",$db);
		if(!$ResultInvoice)
		{
		   die('Error 101'.DB_error());
		}
   $RowDataInvoice = DB_fetch_array($ResultInvoice);
  
   return($RowDataInvoice['edu_cess']) ;
 }

  function GetSHE_CessAmount($CBranch,$Lid)
{
  global $db;
      	$ResultInvoice=DB_query("select sum(she_cess) as she_cess   from invoice A    where  (A.she_cess_ledger='".$Lid."'   ) and  A.branch_name='".$CBranch."' and A.CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
		if(!$ResultInvoice)
		{
		   die('Error 101'.DB_error());
		}
   $RowDataInvoice = DB_fetch_array($ResultInvoice);
  
   return($RowDataInvoice['she_cess']) ;
 }
	     
function GetCaseBank_Amount($CBranch,$CaseBankid)
{	 
   global $db;
   $GetCashBankPayment =DB_query("Select sum(A.Amount ) as Amount from cashbankvoucher_master A,cashbankvoucher B where B.TransId=A.voucherMasterId    and A.CaseBankId='".$CaseBankid."' and  A.BranchId='".$CBranch."' ",$db);
   if(!$GetCashBankPayment)
		{
		   die('Error 101'.DB_error());
		}
   $RowDataInvoice = DB_fetch_array($GetCashBankPayment);
     return($RowDataInvoice['Amount']) ;

}


function VatAmount($CBranch,$Lid)
{
 global $db;
//e//cho  "select total_taxable ,  vat_cst from invoice A    where  (A.vat_cst_ledger='".$Lid."') and  A.branch_name='".$CBranch."' and A.CompanyId='".$_SESSION['ACGCompanyId']."' ";
 $ResultInvoice=DB_query("select sum(total_taxable * vat_cst * .01) as VatAmount  from invoice A    where  (A.vat_cst_ledger='".$Lid."') and  A.branch_name='".$CBranch."' and A.CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
  if(!$ResultInvoice)
		{
		   die('Error 101'.DB_error());
		}
	if(DB_num_rows($ResultInvoice)>0)
	{
	   $RowDataInvoice = DB_fetch_array($ResultInvoice);
	     $t= $RowDataInvoice['VatAmount'];
		 return($t) ;
	}
}
function GetClientAmount($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 in($ClientId) or  A.client_id in($ClientId))  and  A.BranchId='".$CBranch."'  ";
  	$ResultData2 = DB_query($GetRecord2,$db);
	 if(!$ResultData2)
		{
		   die('Error 101'.DB_error());
		}
    if(DB_num_rows($ResultData2)>0)
	{
	   	$RowDataInvoice = DB_fetch_array($ResultData2);
 		 
		  return($RowDataInvoice['Amount']) ;
	}

}

function GetClient_InvoiceAmount($ClientId, $CBranch)
{
 global $db;
 //echo "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']."'";
   $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(!$ResultInvoice)
		{
		   die('Error 101'.DB_error());
		}
    if(DB_num_rows($ResultInvoice)>0)
	{
	   	$RowDataInvoice = DB_fetch_array($ResultInvoice);
 	    return($RowDataInvoice['grand_total']) ;
 	}
  
}

	echo '<link href="'.$rootpath. '/css/'. $_SESSION['Theme'] .'/default.css" rel="stylesheet" type="text/css" />';
	
 if($_REQUEST['BtnBranch'])
{
 if($_REQUEST['ClientId']!='')
 $url="index.php?route=TrialVoucherByClientId&CashBankId=". $_REQUEST['CashBankId']."&Lid=".$_REQUEST['Lid']."&ClientId=".$_REQUEST['ClientId']."&CBranch=".$_REQUEST['companybranch']."&Ledgername=".$_REQUEST['Ledgername'].$rurl;
else
 $url="index.php?route=TrialVoucher&CashBankId=". $_REQUEST['CashBankId']."&Lid=".$_REQUEST['Lid']."&CBranch=".$_REQUEST['companybranch']."&Ledgername=".$_REQUEST['Ledgername'].$rurl;
 $s->pageLocation($url);
  }
 if(isset($_REQUEST['CBranch'])){
   $CBranch1=SeperateByCommes($_REQUEST['CBranch']);
  // echo "select *  from locations  where (id in ($CBranch1) or prefix in ($CBranch1)) and CompanyId='".$_SESSION['ACGCompanyId']."'";
 $Result_branch=DB_query("select *  from locations  where (id in ($CBranch1) or prefix in ($CBranch1)) and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
 }
  else {
 $Result_branch=DB_query("select *  from locations  where  CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
// echo "select *  from locations  where  CompanyId='".$_SESSION['ACGCompanyId']."'";
 }
 ?>
 <div class="contentmain"> 

<center> <h2>Branch</h2></center>

<form action="<?php echo $url;?>" method="post">
  <table width="100%" border="0" cellspacing="0" cellpadding="0" class="maintable" style="margin-top:30px;">
  <tr >
 <th></th>
   <th><strong>DR</strong></th> <th><strong>CR</strong></th>
 </tr>
<?php
 $GDR=0;$GCR=0;
while($row_branch=DB_fetch_array($Result_branch))
{
 $TDR=0;$TCR=0;
?>
 <tr>
 <td>
	 <input id="companybranch" name="companybranch" type="radio"   checked="checked" value="<?php echo $row_branch['prefix'];?>" /><?php echo $row_branch['locationname']."&nbsp;(&nbsp;".$row_branch['prefix']."&nbsp;)&nbsp;";?>
	 <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="Ledgername" id="Ledgername" type="hidden"  value="<?php echo $_REQUEST['Ledgername']?>" /></td>
<td><?php  $TDR= GetVatAmount($row_branch['prefix'],$_REQUEST['Lid']);
$TDR+= GetSHE_CessAmount($row_branch['prefix'],$_REQUEST['Lid']);
$TDR+= GetEDU_CessAmount($row_branch['prefix'],$_REQUEST['Lid']);
$TDR+= GetCaseBank_Amount($row_branch['prefix'],$_REQUEST['CashBankId']);
$TDR+=VatAmount($row_branch['prefix'],$_REQUEST['Lid']);
if($_REQUEST['ClientId']!='')
$TDR+= GetClient_InvoiceAmount($_REQUEST['ClientId'],$row_branch['prefix']);

if($TDR==0)
echo '0';
else
echo $TDR;

 $GDR+=$TDR;
?> </td>

<td><?php $TCR= BranchAmount($row_branch['prefix'],$_REQUEST['Lid']);
if($_REQUEST['ClientId']!='')
$TCR+= GetClientAmount($_REQUEST['ClientId'],$row_branch['prefix']);
if($TCR==0)
echo '0';
else
echo $TCR;
$GCR+=$TCR;

?> </td>
 </tr>
 
<?php } ?>		
 <tr><td align="right"><div align="right"> <strong>Total:</strong></div></td>
		<td  ><strong> <?php  echo $GDR;?></strong></td>
		<td  ><strong> <?php  echo $GCR;?></strong></td>
 </tr>       <tr>
          <td colspan="2"><center><input name="BtnBranch" type="submit" value="View" /></center></td>
        </tr>
 </table>
</form>
</div>