<?php include("../../include/dbconnect.php");?>
<title>supplier ledger Printing</title>
<body bgcolor="#ffffff"  topmargin=0>
<?

$delfile = "/tmp/print.txt";
if(file_exists($delfile))
 {
  unlink($delfile);
 }

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

$fname = 'print.txt';
$todate = $startdt;
$temp = $startdt;
$startdt = substr($temp,6,4).substr($temp,3,2).substr($temp,0,2);
$edt = $enddt;
$temp=$enddt;
$enddt = substr($temp,6,4).substr($temp,3,2).substr($temp,0,2);

$query = "delete from pledger";
mysql_query($query) or die (mysql_error());



$linecnt=0;
$pgbrk=5;
for ($buyind=0;$buyind < count($bcode);$buyind++)
{
		$purchk= 'F';
		$clbal=0;
		$salesbal=0;
		$netbal=0;
		$opbal=0;
		
		
		//  Buyers tsupledger
		$result2 = mysql_query("select sum(dbamt)-sum(cramt) from supledger where bcode = '$bcode[$buyind]' and date >='$startdt' and date <='$enddt'  and compcode ='$compcode' order by date,record ");
		$rows=mysql_fetch_row($result2);
		$amt=$rows[0];
		$tdbamt = 0;
		$tcramt = 0;
	//	if($amt!=0) // Check for 0 balance tsupledgers
	//	{
			$bresult=mysql_query("select name from supplier where bcode = '$bcode[$buyind]'");
			$brow=mysql_fetch_row($bresult);
			$bname = $brow[0];
			
			
						
			$result = mysql_query("select date,rate,cramt,dbamt,narration,qty from supledger where bcode = '$bcode[$buyind]' and compcode='$compcode' order by date ");


			$tempdate=' ';
			while($row=mysql_fetch_row($result))
			{
  			  	   if($tempdate != $row[0])
				   {
					   $date = substr($row[0],8,2).'/'.substr($row[0],5,2).'/'.substr($row[0],0,4);
					   $tempdate=$row[0];
				   }
				   else
				   {
				  		$date='          ';
				   }
				   
				   $rate = $row[1];
				   $narration = addslashes($row[4]);
				   $qty=$row[5];
				   $dbamt = $row[3];
				   $cramt = $row[2];
				   $temp=$row[0];
				   $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','$qty','$rate',$cramt,$dbamt)";
					mysql_query($query) or die(mysql_error());
				   }
	

			}
			$opbal = $tdbamt - $tcramt;
			if($linecnt==0)
			{
				if($pgbrk==0)
				{
					fputs($myfile,"\n");
					fputs($myfile,chr(12));
				}
	
			}
	
			?>
	
			<table width=800>
			<tr><td align="center"><font color="#CC0000"><b><? echo($bname);?></b></font></td></tr>

			</table>
			<?
			$bname = substr($bname,0,30);
			fputs($myfile,chr(18).chr(14));
			fputs($myfile,"   $bname\n");
			$linecnt++;
			$purchk= 'T';
			
			ledghead($myfile,$branch,$todate,$edt);
			ledgtit2($myfile);
			$pgbrk=0;
			$linecnt= $linecnt+5;

			?>
			<table width=800>
			<td width="532" align=center><font color="#0000CC"><b>Opening Balance </b></td>
			<td width="123" align=right><font color="#0000CC"><b><?if($opbal <0) { echo(number_format($opbal*-1,2,'.',''));}else {echo("   ");}?></b> </td>
			<td width="129" height="20" align=right><font color="#0000CC"><b><?if($opbal>=0) {echo(number_format($opbal,2,'.',''));}else {echo("   ");}?></b></td>
			</table>
			
<?
			if($opbal <0)
			{
				fputs($myfile,"                   Opening Balance  :                 ");
				numcontrol($myfile,number_format($opbal*-1,2,'.',''),12);
				fputs($myfile,"\n");
			}
			elseif($opbal >= 0)
			{
				fputs($myfile,"                   Opening Balance  :                 ");
				numcontrol($myfile,number_format($opbal,2,'.',''),12);
				fputs($myfile,"\n");
			}
			$linecnt++;
			if($linecnt > 55)
			{
				fputs($myfile,"\n");
				fputs($myfile,chr(12));
				ledghead($myfile,$branch,$todate,$edt);
				ledgtit2($myfile);
				$linecnt= 5;
			}
	
	
			$credit=0;
			$debit=0;
			$result3 = mysql_query("select * from pledger");
			while($row3 = mysql_fetch_row($result3))
			{
				$credit=$credit+$row3[4];
				$debit =$debit+$row3[5];
				?>
				<table width=800>
				<td width="84" align=left><?echo($row3[0]);?></td>
				<td width="255" align=left><?echo($row3[1]);?></td>
				<td width="86" align=right><? if ($row3[2] >0){echo($row3[2]);}else {echo("   ");}?></td>
				<td width="94" align=right><? if($row3[3] >0) {echo($row3[3]);}else {echo("   ");}?></td>
				<td width="124" align=right><? if($row3[4] >0) {echo($row3[4]);}else {echo("   ");}?></td>
				<td width="129" align=right><? if($row3[5] >0) {echo($row3[5]);}else {echo("   ");}?></td>				
				</table>
				<?
				
				strcontrol($myfile,$row3[0],12);
				strcontrol($myfile,substr($row3[1],0,25),30);
				if($row3[2] >0)
				{
					numcontrol($myfile,$row3[2],10);
					fputs($myfile,'     ');
				}
				else
				{
					strcontrol($myfile,' ',15);
				}
				if($row3[4] >0)
				{
					numcontrol($myfile,$row3[4],12);
					strcontrol($myfile," ",15);
				}
				elseif($row3[5]>0)
				{
					fputs($myfile,"              ");
					numcontrol($myfile,$row3[5],12);
				}
				fputs($myfile,"\n");
				$linecnt++;
				if($linecnt > 55)
				{
					line($myfile,82);
					fputs($myfile,"\n");
					fputs($myfile,chr(12));
					ledghead($myfile,$branch,$todate,$edt);
					ledgtit2($myfile);
					$linecnt= 5;
				}
			} // END OF WHILE LOOP
			$clbal = ($tdbamt+$debit)-($tcramt+$credit);
			$query = "delete from pledger";
			mysql_query($query) or die (mysql_error());
			?>
			<table width=800>
			<td width="532" align=center><font color="#0000CC"><b>Closing Balance </b></td>
			<td width="123" align=right><font color="#0000CC"><b><?if($clbal <0) { echo(number_format($clbal*-1,2,'.',''));}else {echo("   ");}?></b> </td>
			<td width="129" height="20" align=right><font color="#0000CC"><b><?if($clbal >= 0) {echo(number_format($clbal,2,'.',''));}else {echo("   ");}?></b></td>
			</table>
			<hr align = left width = 800 ></hr>
			<br><br><br>
			<?
			if($clbal <0)
			{
				fputs($myfile,"                   Closing Balance  :                 ");
				numcontrol($myfile,number_format($clbal*-1,2,'.',''),12);
				fputs($myfile,"\n");
			}
			elseif($clbal >= 0)
			{
				fputs($myfile,"                   Closing Balance  :                 ");
				fputs($myfile,"              ");
				numcontrol($myfile,number_format($clbal,2,'.',''),12);
				fputs($myfile,"\n");
			}
			fputs($myfile, "\n\n\n");
			$linecnt=$linecnt+4;
			if($linecnt > 55)
			{
				line($myfile,82);
				fputs($myfile,"\n");
				fputs($myfile,chr(12));
				ledghead($myfile,$branch,$todate,$edt);
				ledgtit2($myfile);
				$linecnt= 5;
			}
			$salesbal=$clbal;
	 //} // end of amt if loop
	

	 
	 
	 
	 
	 
} // END OF FOR LOOP

?>

<a href = "http://192.168.1.2/stt/dload.php?dfile=<?echo($fname);?>">Download Text file</a>

<center><font color="#000080"><font size="+1"><a href= "index.html">Return to Index </a></font></center>

<br>
<br>
<br>
<br>
<br>
<?

/*  FUNCTION TO PRINT THE HEADING */





function ledghead($myfile,$branch,$todate,$edt)
{
	?>
	<table width=800 border=0>
	<tr><td align="center">Supplier Ledger From    <? echo($todate);?> To <? echo($edt);?> </td></tr>
	</table>
	<hr align = left width = 800 ></hr>

	<?

	fputs($myfile,"                              Supplier Ledger From   $todate to $edt    \n");
	fputs($myfile,chr(27)."M");
	line($myfile,82);
}

function ledgtit($myfile)
 {
	?>
	<table width=800>
	<td width="100">Date </td>
	<td  width= 209>Particulars</td>
	<td width="94" align=right>Qty </td>
	<td width="110" align=right>Rate </td>
	<td width="135" align=right>Credit </td>
	<td width="124" align=right>Debit</td></table>
	<hr align = left width = 800 ></hr>
	<?
	fputs($myfile,"  Date                Particulars                    Credit   Pmt Due to Supplier \n");
	fputs($myfile,"_________________________________________________________________________________\n");
}

function ledgtit2($myfile)
 {
	?>
	<table width=800>
	<td width="100">Date </td>
	<td  width= 209>Particulars</td>
	<td width="94" align=right> </td>
	<td width="110" align=right> </td>
	<td width="135" align=right>Credit </td>
	<td width="124" align=right>Debit</td></table>
	<hr align = left width = 800 ></hr>
	<?
	fputs($myfile,"  Date                Particulars                        Collected Due from Buyer  \n");
	fputs($myfile,"_________________________________________________________________________________\n");
}





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");
}

?>





