<?php include("../../include/dbconnect.php");

$result=mysqli_query($conn,"select distinct name,address1,address2,city,state from company where compcode = '$compcode' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$compname = $row[0];
$address = $row[1].'&nbsp;'.$row[2].'&nbsp;'.$row[3].'&nbsp;'.$row[4];


$result=mysqli_query($conn,"select distinct name,start_date from branches where code = '$branch' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$branchname = $row[0];
$std = british($row[1]);


if($startdt == '')
{
	$yr = substr($account_year,0,4);
	$startdt = '01-04-'.$yr;

}

?>

<script type="text/javascript">
	var arrayOfRolloverClasses = new Array();
	var arrayOfClickClasses = new Array();
	var arrayOfDoubleClickClasses = new Array();	
	var activeRow = false;
	var activeRowClickArray = new Array();
	var activeRowDoubleClickArray = new Array();	
	
	function highlightTableRow()
	{
		var tableObj = this.parentNode;
		if(tableObj.tagName!='TABLE')tableObj = tableObj.parentNode;

		if(this!=activeRow){
			this.setAttribute('origCl',this.className);
			this.origCl = this.className;
		}
		this.className = arrayOfRolloverClasses[tableObj.id];
		
		activeRow = this;
		
	}
	
	function clickOnTableRow()
	{
		var tableObj = this.parentNode;
		if(tableObj.tagName!='TABLE')tableObj = tableObj.parentNode;		
		
		//if(activeRowClickArray[tableObj.id] && this!=activeRowClickArray[tableObj.id]){
		//	activeRowClickArray[tableObj.id].className='';
		//}
		this.className = arrayOfClickClasses[tableObj.id];
		
		activeRowClickArray[tableObj.id] = this;
				
	}
	function DoubleclickOnTableRow()
	{
		var tableObj = this.parentNode;
		if(tableObj.tagName!='TABLE')tableObj = tableObj.parentNode;		
		

		this.className = arrayOfDoubleClickClasses[tableObj.id];
		
		activeRowDoubleClickArray[tableObj.id] = this;
				
	}		
	
	
	function resetRowStyle()
	{
		var tableObj = this.parentNode;
		if(tableObj.tagName!='TABLE')tableObj = tableObj.parentNode;

		if(activeRowClickArray[tableObj.id] && this==activeRowClickArray[tableObj.id]){
			this.className = arrayOfClickClasses[tableObj.id];
			return;	
		}
		
		var origCl = this.getAttribute('origCl');
		if(!origCl)origCl = this.origCl;
		this.className=origCl;
		
	}
		
	function addTableRolloverEffect(tableId,whichClass,whichClassOnClick,whichClassOnDoubleClick)
	{
		arrayOfRolloverClasses[tableId] = whichClass;
		arrayOfClickClasses[tableId] = whichClassOnClick;
		arrayOfDoubleClickClasses[tableId] = whichClassOnDoubleClick;		
		
		var tableObj = document.getElementById(tableId);
		var tBody = tableObj.getElementsByTagName('TBODY');
		if(tBody){
			var rows = tBody[0].getElementsByTagName('TR');
		}else{
			var rows = tableObj.getElementsByTagName('TR');
		}
		for(var no=0;no<rows.length;no++){
			//rows[no].onmouseover = highlightTableRow;
			//rows[no].onmouseout = resetRowStyle;
			
			if(whichClassOnClick){
				rows[no].onclick = clickOnTableRow;	
				
			}
			if(whichClassOnDoubleClick){
				rows[no].ondblclick = DoubleclickOnTableRow;	

			
			}			
		}
		
	}	</script>	

</script>



<script language="javascript">
function OpenWindow(url)
{
	window.open(url,'win','width=900,height=400,scrollbars=yes,resizable=0');
}
</script>

<title>Ledger Printing</title>
<form>
<head>
<style type="text/css">

	a {text-decoration:none}
	
	body{
		font-size:0.8em;
		font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;
		margin:0px;
		padding:0px;
	}
	img{
		border:0px;
	}
	thead td{
		font-weight:bold;
		color:#000;
		background-color:#E2EBED;
	}
	td{
		padding:2px;
	}
	table{
		border:1px solid #000;
		border-collapse: collapse;
	}
	h1{
		font-size:1.3em;
		margin-bottom:0px;
	}
	table,h1,p,#ads{
		margin-left:10px;
	}
	#ads{
		margin-top:20px;
	}
	
	/* These classes are used by the script as rollover effect for table 1 and 2 */
	
	.tableRollOverEffect1{
		background-color:#317082;
		color:#FFF;
	}


	
	.tableRowClickEffect1{
		background-color:#F00;
		color:#FFF;
	}


</style>

</head>
<body bgcolor="#ffffff"  topmargin=0>

<font face="Arial, Helvetica, sans-serif">
<?


$delfile = "d:/temp/invoice.txt";





$todate = $startdt;
$temp = $startdt;
$startdt = substr($temp,6,4).substr($temp,3,2).substr($temp,0,2);

$totcr=0;
$totdr=0;

$edt = us($enddt);
$edt = british($edt);

$temp=$enddt;
$enddt = substr($temp,6,4).substr($temp,3,2).substr($temp,0,2);



$linecnt=0;
$pgbrk=5;
$mcnt=1;




?>
</font>
<table   id="myTable" width="1104" border=1 cellpadding=4 rules=COLS  bgcolor="#E3ECFB" >
  <colgroup>
  </colgroup>
  <colgroup>
  </colgroup>
  <colgroup>
  </colgroup>
  <colgroup>
  </colgroup>
  <colgroup>
  </colgroup>
  <colgroup>
  </colgroup>
  <colgroup>
  </colgroup>  
  <colgroup>
  </colgroup>
  <colgroup>
  </colgroup>
  <colgroup>
  </colgroup>      
  <thead>
    <tr>
      <td align ="center" colspan="100%"><h4><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?echo($compname);?></font></font></h4></td>
    </tr>
    <tr>
      <td colspan="9"  align="center"><font  face="Arial, Helvetica, sans-serif">
        <?=$address?>
      </font></td>
    </tr>
    <tr>
      <td colspan="100%" align="center"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Ledger From <? echo($todate);?> To <?echo($edt);?></font></font></td>
    </tr>
    <tr>
      <td colspan="100%"  align="center">
          <font size="+1" face="Arial, Helvetica, sans-serif" color="#0000CC"> Profit Centre:
            <?=$branchname?>
          </font></td>
    </tr>
    <?
	/*
    <tr  bgcolor="#B1B8F3">
      <td width="54">&nbsp;</td>
            <td width="266" align=center><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">GL</font></font></td>
      <td width="316" align=center><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Particulars</font></font></td>
      <td width="135" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Debit </font></font></div></td>
      <td width="97" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Credit</font></font></div></td>
      <td width="48">&nbsp;</td>
    </tr>
	
	*/
	?>
    <tr>
      <td><hr></td>    
      <td><hr></td>
      <td><hr></td>
      <td><hr></td>
  
      <td><hr></td>
      <td><hr></td>
      <td><hr></td>       
      
                     
    </tr>
  </thead>
<tbody>
    <?
	
	for ($accind=0;$accind < count($acctcode);$accind++)
  	{
		
		$query = "delete from pledger ";
		mysqli_query($conn,$query) or die (mysqli_error());

	
		$tdbamt = 0;
		$tcramt = 0;



		$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = '$acctcode[$accind]' and branch_code = '$branch' and compcode = '$compcode' and account_year = '$account_year' ") or die(mysqli_error());
		$oprow = mysqli_fetch_row($opresult);

		$amt = $oprow[0];
		
	
		if($oprow[1] =='DR')
		{
			$tdbamt = $amt;
		}
		elseif($oprow[1] == 'CR')
		{

			$tcramt = $amt;
		}
		$msresult = mysqli_query($conn,"select sum(cramt+dbamt) from ledger where compcode = '$compcode' && acctcode = '$acctcode[$accind]' and date >= '$startdt' and date <= '$enddt'  and branch_code = '$branch' and account_year = '$account_year' order by date ");
		$msrow = mysqli_fetch_row($msresult);
		$chck_amt = $msrow[0]+$amt;
		if($chck_amt>0)
		{				
		$bresult=mysqli_query($conn,"select description from acctmast where acctcode = '$acctcode[$accind]'");
		$brow=mysqli_fetch_row($bresult);
		$bname = $brow[0];
		?>
    <tr>
      <td colspan="9" align="center"><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif" color="#0000FF"  ><? echo($bname);?></font></b></font></td>
      

    </tr>
    <tr  bgcolor="#B1B8F3">
      <td width="28">&nbsp;</td>
      <td width="186" align=center><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">GL</font></font></td>
      <td width="281" align=center><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Particulars</font></font></td>
      <td width="64" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Bill No </font></font></div></td>
              
      <td width="79" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Debit </font></font></div></td>
      <td width="63" align=center><div align="right"><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">Credit</font></font></div></td>
      <td width="85">&nbsp;</td>
    </tr>    
    <?
		$mcnt++;
		$bname = substr($bname,0,30);


		$result = mysqli_query($conn,"select date,invno,cramt,dbamt,narration,trans_id,record,amttype,account_year,cheqno,voucher_no,opp_gl from ledger where compcode = '$compcode' and acctcode = '$acctcode[$accind]'  and branch_code = '$branch'  and account_year = '$account_year' order by date ");
		while($row=mysqli_fetch_row($result))
		{
			$date = substr($row[0],8,2).'/'.substr($row[0],5,2).'/'.substr($row[0],0,4);
			
			$invno = $row[1];
			$narration = addslashes($row[4]);
			$dbamt = $row[3];
			$cramt = $row[2];
			$temp=$row[0];
			$trans_id = $row[5];
			$record = $row[6];
			$amttype = $row[7];
			$account_year = $row[8];
			$cheqno = $row[9];
			$voucher_no = $row[10];
			$opp_gl = $row[11];
			
			
			$cdate = substr($temp,0,4).substr($temp,5,2).substr($temp,8,2);
			if($cdate < "$startdt")
			{
				if ($dbamt > 0)
				{
					$tdbamt = $tdbamt+$dbamt;

				}
				elseif($cramt > 0)
				{
					$tcramt = $tcramt+$cramt;

				}
			}
			elseif ($cdate >= "$startdt"  && $cdate <= "$enddt" )
			{
				
				$query = "insert into  pledger values('$date','$narration','$invno',$dbamt,$cramt,'$trans_id','$record','$amttype','$branch','$account_year','$cheqno','$voucher_no','$opp_gl')";

				mysqli_query($conn,$query) or die(mysqli_error());
			}
		}
			
	  	$opbal = $tdbamt - $tcramt;

		?>
    <tr>
         <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
              <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
          <td align="center"><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">Opening Balance </font></b></font></td>
               <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>

      <td align=right><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">
        <?if($opbal >=0) {echo(number_format($opbal,2,'.',''));}else {echo("   ");}?>
      </font></b> </font></td>
      <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
        <?if($opbal< 0) {echo(number_format($opbal*-1,2,'.',''));}else {echo("   ");}?>
      </font></font></td>
      <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
    </tr>
    <?
		$mcnt++;


		$credit=0;
		$debit=0;
		$run_bal= $opbal;
		$result3 = mysqli_query($conn,"select * from pledger where branch_code = '$branch' ");
		while($row3 = mysqli_fetch_row($result3))
		{
			$credit=$credit+$row3[4];
			$debit =$debit+$row3[3];
			$run_bal=$opbal+$debit - $credit;

			$totcr=$totcr+$row3[4];
			$totdr=$totdr+$row3[3];
			$trans_id = $row3[5];
			$record = $row3[6];
			$amttype = $row3[7];
			$account_year = $row3[9];
			$invno = $row3[2];
			$cheqno = $row3[10];
			$voucher_no = $row3[11];
			$opp_gl = $row3[12];

		$opresult = mysqli_query($conn,"select acctcode from ledger where compcode = '$compcode'   and branch_code = '$branch'  and trans_id = '$trans_id'  and record != '$record' and account_year = '$account_year' ") or die(mysqli_error());
		$oprow = mysqli_fetch_row($opresult);
		if($opp_gl >0)
		{
			$oprow[0] = $opp_gl;
		}
		$gresult = mysqli_query($conn,"select description from acctmast where acctcode = '$oprow[0]' ") or die(mysqli_error());
		$grow = mysqli_fetch_row($gresult);
		$gl = $grow[0];
			?>
    <tr>

      <td align=left><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?echo($row3[0]);?></font></font></td>
      <td align=left><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><a onClick="javascript:OpenWindow('editview.php?record=<?echo($record);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&trans_id=<?=$trans_id?>&account_year=<?=$account_year?>')" href ="#"><? echo($gl);?>&nbsp;<font color="#0000FF"><? if($amttype=='J') echo('(JV)')?></font> </td>      
      <td align=left><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?echo($row3[1]);?></font></font></td>
      <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif"><?echo($row3[2]);?></font></font></td>

             
      <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
        <? if($row3[3] >0) {echo(number_format($row3[3],2));}else {echo("   ");}?>
      </font></font></td>
      <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
        <? if($row3[4] >0) {echo(number_format($row3[4],2));}else {echo("   ");}?>
      </font></font></td>
      <td align=right><font face="Arial, Helvetica, sans-serif"><font face="Arial, Helvetica, sans-serif">
        <? if($run_bal >0) {echo(number_format($run_bal,2));echo("&nbsp;Dr");}elseif($run_bal < 0) {echo(number_format($run_bal*-1,2));echo("&nbsp;Cr");}?>

      </font></font></td>
    </tr>
 <?
		}
		$clbal = ($tdbamt+$debit)-($tcramt+$credit);
		$totdb = $tdbamt +$debit;
		$totcr = $tcramt +$credit;
		if($totdb >= $totcr)
		{
			$netamt = $totdb;
		}
		else
		{
			$netamt = $totcr;		
		}
		


    ?>
	
    <tr>
          <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
               <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
      <td align=center ><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">Closing Balance</font></b><font face="Arial, Helvetica, sans-serif"> c/d </font></font></td>
      <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>

                 
      <td align=right><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">
        <? if($clbal< 0) {echo(number_format($clbal*-1,2));}else {echo("   ");}?>
      </font></b></font></td>
      <td align=right><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">
        <? if($clbal >=0) {echo(number_format($clbal,2));}else {echo("   ");}?>
      </font></b></font></td>
      <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
    </tr>   
    <?
    /*
    <tr>
          <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
      <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
      <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
                  
      <td align=center><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">&nbsp;</font></b><font face="Arial, Helvetica, sans-serif"> </font></font></td>
      <td align=right><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">
        <?=number_format($netamt,2)?>
      </font></b></font></td>
      <td align=right><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">
        <?=number_format($netamt,2)?>
      </font></b></font></td>
      <td><font face="Arial, Helvetica, sans-serif">&nbsp;</font></td>
    </tr>      
    */
    ?>
    <tr>
      <td><hr></td>
      <td><hr></td>      
      <td><hr></td>
      <td><hr></td>

      <td><hr></td>
      <td><hr></td>                  
      <td><hr></td>
    </tr>

  <?
		$datedb=0;
		$datecr=0;




	}  // end of check amount if loop
	} // Buyer For loop end


?>
  <tr>
    <td colspan="100%"><font face="Arial, Helvetica, sans-serif"><a href = "dload.php?dfile=<?echo($fname);?>">Download Text file</a></font></td>
  </tr>
  </tbody>
</table>
<font face="Arial, Helvetica, sans-serif">
<?

?>
</font>