<?php include("../../include/dbconnect.php");
$filename = 'templedger';
$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 == '')
{
	$todate = $std;
	$startdt=$std;
}

?>
<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><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>



<title>Ledger Printing</title>
<form>
<body bgcolor="#ffffff"  topmargin=0>

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


$delfile = "../../../../tmp/invoice.txt";

if(file_exists($delfile))
 {
  unlink($delfile);
 }




if(!($myfile=fopen("../../../../tmp/invoice.txt","w")))
{
  print("file couldnot be opened");
}


$fname = 'invoice.txt';
$todate = $startdt;
$temp = $startdt;
$startdt = us($startdt);

$totcr=0;
$totdr=0;


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

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




?>
</font>
<TABLE  id="myTable" width="1055" BORDER=1 CELLPADDING=4 RULES=COLS  bgcolor="#DCE8FA" >

<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"><?echo($compname);?></font></h4></td></tr>
  <tr> 
  
    <td colspan="8"  align="center"><font  face="Arial, Helvetica, sans-serif"> <?=$address?></font></td>  </tr>
<tr><td colspan="100%" align="center"><font face="Arial, Helvetica, sans-serif">Ledger From    <? echo($todate);?> To <?echo($edt);?></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="#92B5EF">
<td width="51">&nbsp;</td>
<td width="216" align=center><font face="Arial, Helvetica, sans-serif">GL </font></td>
<td width="275" align=center><font face="Arial, Helvetica, sans-serif">Particulars</font></td>
<td width="60" align=center><div align="right"><font face="Arial, Helvetica, sans-serif">Bill No </font></div></td>
<td width="85" align=center><div align="right"><font face="Arial, Helvetica, sans-serif">Voucher No </font></div></td>
<td width="73" align=center><div align="right"><font face="Arial, Helvetica, sans-serif">Cheque No </font></div></td>


<td width="95" align=center><div align="right"><font face="Arial, Helvetica, sans-serif">Debit </font></div></td>
<td width="100" align=center><div align="right"><font face="Arial, Helvetica, sans-serif">Credit</font></div></td>
<td width="100" align=center><div align="right"><font face="Arial, Helvetica, sans-serif">Edit /Deletion Reason</font></div></td>	
</tr>
<tr><td><hr></td>
<td><hr></td>


<td><hr></td>
<td><hr></td>
<td><hr></td>
<td><hr></td>
<td><hr></td>
<td><hr></td>
<td><hr></td>	
</tr>
</thead>
<tbody>
<?

	
		$totdb = 0;
		$totcr = 0;
		
		
	$tresult = mysqli_query($conn,"truncate table $filename") or die(mysqli_error());

	$query= "insert into  $filename(acctcode,date,narration,cheqno,dbamt,cramt,type,invno,compcode,branch_code,trans_id,account_year,voucher_no,reconciled_by) select acctcode,date,narration,cheqno,dbamt,cramt,type,invno,compcode,branch_code,trans_id,account_year,voucher_no,deleted_reason  from  deleted_ledger where date>='$startdt' and date <='$enddt' and compcode = '$compcode' and branch_code = '$branch'  ";
	mysqli_query($conn,$query) or die('Sorry.. Connectivity lost for few seconds. Please go back and Generate the ledger again.. Thank you...');
	

		$run_bal= 0;


	$result = mysqli_query($conn,"select acctcode,date,narration,dbamt,cramt,type,trans_id,record,date,cheqno,invno,voucher_no,reconciled_by from  $filename ") or die(mysqli_error());
	while($row = mysqli_fetch_row($result))
	{
		$acctcode = $row[0];
		$date = british($row[1]);
		$narration = $row[2];
		$dbamt = $row[3];
		$cramt = $row[4];
		$type = $row[5];
		$trans_id = $row[6];
		$record = $row[7];
		$mdt = british($row[8]);
		$cheqno = $row[9];
		$invno = $row[10];
		$voucher_no = $row[11];
		$deleted_reason = $row[12];
		$netdb = $netdb+$dbamt;
		$netcr = $netcr + $cramt;
	
		$presult=mysqli_query($conn,"select description from acctmast where acctcode = '$acctcode'") or die(mysqli_error());
		$prow = mysqli_fetch_row($presult);
		$pname = $prow[0];
		
			$run_bal=$run_bal+ $dbamt - $cramt;


		
			?>
            
			<tr  >
			<td align=left><font face="Arial, Helvetica, sans-serif"><?echo($mdt);?></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($pname);?>&nbsp;<font color="#0000FF"><? if($amttype=='J') echo('(JV)')?></font> </td>      

     	 	<td align=left><font face="Arial, Helvetica, sans-serif"><?echo($narration);?></font></td>  
     	 	<td align=left><font face="Arial, Helvetica, sans-serif"><?echo($invno);?></font></td>    
     	 	<td align=left><font face="Arial, Helvetica, sans-serif"><?echo($voucher_no);?></font></td>                                
     	 	<td align=left><font face="Arial, Helvetica, sans-serif"><?echo($cheqno);?></font></td>              
			<td align=right><font face="Arial, Helvetica, sans-serif">
		    <? if($dbamt >0) {echo($dbamt);}else {echo("   ");}?> 
		    </font></td>
			<td align=right><font face="Arial, Helvetica, sans-serif">
		    <?if($cramt >0) {echo($cramt);}else {echo("   ");}?>
			</font></td>
		       	 	<td align=left><font face="Arial, Helvetica, sans-serif"><?echo($deleted_reason);?></font></td>
			</tr>
			<?
			strcontrol($myfile,$date,15);
			strcontrol($myfile,substr($prodname['description'],0,45),50);			
			strcontrol($myfile,substr($narration,0,45),50);
			if($dbamt >0)
			{
				numcontrol($myfile,$dbamt,15);
				strcontrol($myfile," ",15);
			}
			elseif($cramt>0)
			{
				fputs($myfile,"              ");
				numcontrol($myfile,$cramt,15);
			}
			if($run_bal >0)
			{
				numcontrol($myfile,number_format($run_bal,2),15);
				fputs($myfile," Dr\n");
			}
			elseif($run_bal < 0)
			{
				numcontrol($myfile,number_format($run_bal*-1,2),15);
				fputs($myfile," Cr\n");
			}
			if($run_bal == 0)
			{
				numcontrol($myfile,number_format(0,2),15);
				fputs($myfile," Dr\n");
			}
			

			$mcnt++;
			$linecnt++;
			if($linecnt > 55)
			{
				line($myfile,110);
				fputs($myfile,"\n");
				fputs($myfile,chr(12));
				fputs($myfile,chr(27).chr(72));//Closes compression					
				ledghead($myfile,$compcode,$todate,$edt,$page_no,$branchname);
				ledgtit($myfile);				
				$page_no++;
				$linecnt= 6;
				$mcnt=1;
			}
		}



	
		
		$query = "truncate table $filename";
		mysqli_query($conn,$query) or die (mysqli_error());

		?>
		
		
		
		
		

		
  <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><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"></b> </font></td>
	
		<td align=right><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">
        <? echo(number_format($netdb,2,'.',''))?>
		</b></font></td>
		<td align=right><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">
        <?  echo(number_format($netcr,2,'.',''))?>
		</b></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>
<td><hr></td>
<td><hr></td>
</tr>		
</tbody>		
		
		<?


	
		fputs($myfile,"                                                               ");
		numcontrol($myfile,number_format($totdr,2),15);
		numcontrol($myfile,number_format($totcr,2),15);
		fputs($myfile,"\n");
		line($myfile,110);

					
		fputs($myfile, "\n\n\n");

		$linecnt=$linecnt+4;
		$mcnt= $mcnt+3;




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

<script type="text/javascript">
addTableRolloverEffect('myTable','tableRollOverEffect1','tableRowClickEffect1','tableRowDoubleClickEffect1');

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

/*  FUNCTION TO PRINT THE HEADING */





function ledghead($myfile,$compcode,$todate,$edt,$page_no,$branchname)
{

$pesult = mysqli_query($conn,"select city from company ");
$row11 = mysqli_fetch_row($pesult);
$city = $row11[0];

$presult = mysqli_query($conn,"select name from company where compcode = '$compcode' ");
$row11 = mysqli_fetch_row($presult);
?>
</font>
<?

fputs($myfile,chr(18));
fputs($myfile,chr(14));
fputs($myfile,"    $row11[0], $city\n");
fputs($myfile,"Ledger From   $todate to $edt                                                 Page No:$page_no\n");
fputs($myfile,"                             Branch :$branchname \n");
fputs($myfile,chr(27).'M'); //Compress and prints
line($myfile,110);


}
?>

<?
function ledgtit($myfile)
 {
?>





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

fputs($myfile," Date                  GL                          Particulars                                       Debit          Credit       \n");
line($myfile,110);

}


function line($myfile,$maxlen)
{
for($sk=1;$sk <$maxlen;$sk++)
{ fputs($myfile,"_"); };
fputs($myfile,"\n");

}


function strcontrol($myfile,$stname,$maxlen)
{
$plen = strlen($stname);
$tlen = $maxlen-$plen;
fputs($myfile,$stname);
for($sk=1;$sk <$tlen;$sk++)
{ fputs($myfile," "); };
}


function numcontrol($myfile,$stname,$maxlen)
{
$plen = strlen($stname);
$tlen = $maxlen-$plen;
for($sk=1;$sk <$tlen;$sk++)
{ fputs($myfile," "); };
fputs($myfile,$stname);
//fputs($myfile,"\n");
}

?>
</font>
