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

<STYLE TYPE='text/css'>
P.pagebreakhere {page-break-after: always}
</STYLE>

<title>Trial Balance Printing</title>
<form>


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




$totcr=0;
$totdr=0;

$enddt = us($enddt);


$query = "delete from pledger";
mysqli_query($conn,$query) or die (mysqli_error());

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

$result=mysqli_query($conn,"select distinct name,account_year from company where compcode = '$compcode' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$compname = $row[0];
$account_year = $row[1];

$result=mysqli_query($conn,"create table tempop as select * from acctmast ") or die(mysqli_error());
$result=mysqli_query($conn,"update acctmast set opening_balance= 0 ") or die(mysqli_error());


?>
</font>
<TABLE  width="780" BORDER=1 CELLPADDING=4 RULES=COLS  >

<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>
<COLGROUP></COLGROUP>

<thead>
<tr> 
<td colspan="100%"  align="center"><div align="center"></div><font size="+2" face="Arial, Helvetica, sans-serif"> <?=$compname?></font></td>  </tr>

<tr> 
<td colspan="100%"  align="center"><font face="Arial, Helvetica, sans-serif">Trial Balance as on <?=british($enddt);?></font></td> </tr>
<tr  valign="baseline"></tr>    <td  colspan="100%"><hr></td>  </tr>
<tr bgcolor="#EBF4CA"> 
<td width="431" ><div align="center"><font face="Arial, Helvetica, sans-serif">Particulars</font></div></td>
<td width="140"><div align="right"><font face="Arial, Helvetica, sans-serif">Credit</font></div></td>
<td width="157"><div align="right"><font face="Arial, Helvetica, sans-serif">Debit</font></div></td>

</tr>

<tr> 
<td><hr></td>
<td><hr></td>
<td><hr></td>
</tr>  
</thead>
<?
$debittotal = 0;
$credittotal=0;
$aresult = mysqli_query($conn,"select acctcode,description from acctmast  where type = '' order by description") or die(mysqli_error());
while($arow = mysqli_fetch_row($aresult))
{
	$cresult = mysqli_query($conn,"select  distinct prodcode from product where prodcode = '$arow[0]' ") or die(mysqli_error());
	$crow = mysqli_fetch_row($cresult);
	if(!is_array($crow))
	{
		$opresult = mysqli_query($conn,"select opening_balance,db_cr from tempop where acctcode = '$arow[0]' ") or die(mysqli_error());
		$oprow = mysqli_fetch_row($opresult);
		$amt = $oprow[0];
		if($oprow[1] == 'DR')
		{
			$amt = $oprow[0];
		}
		elseif($oprow[1] == 'CR')
		{
			$amt = $oprow[0]*-1;
		}	
	}
	$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = '$arow[0]' and date <= '$enddt' ") or die(mysqli_error());
	$ledrow = mysqli_fetch_row($ledresult);	
	$ledgamt = $ledrow[0];
	$ledtotal = $amt + $ledgamt;
	if($ledtotal != 0)
	{
		if($ledtotal > 0)
		{
				$debittotal = $ledtotal;
				$db_cr = 'DR';
				mysqli_query($conn,"update acctmast set opening_balance = '$debittotal',db_cr = 'DR' where acctcode = '$arow[0]' ") or die(mysqli_error());
				
		}
		elseif($ledtotal < 0)
		{
			$credittotal  = $ledtotal*-1;
				$db_cr = 'CR';			
				mysqli_query($conn,"update acctmast set opening_balance = '$credittotal',db_cr = 'CR' where acctcode = '$arow[0]' ") or die(mysqli_error());				
		}
		?>
		<tr> 
		<td><font  face="Arial, Helvetica, sans-serif"><?=$arow[1]?></font></td>
		<td><div align="right"><font  face="Arial, Helvetica, sans-serif"><? if($ledtotal <0) echo(number_format($ledtotal*-1,2,'.',','));?></font></div></td>
		<td><div align="right"><font  face="Arial, Helvetica, sans-serif"><? if($ledtotal >0) echo(number_format($ledtotal,2,'.',','));?></font></div></td></tr>
		<?
		

		
		
	}		
	$amt=0;
	$ledtotal=0;
}	
		$result = mysqli_query($conn,"truncate ledger ") or die(mysqli_error());
				mysqli_query($conn,"update company set account_year = '$new_year' ") or die(mysqli_error());
					
?>

</tbody>

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

		<tr bgcolor="#E3F4B0"> 
		<td><font  face="Arial, Helvetica, sans-serif">Total</font></td>
		<td><div align="right"><font  face="Arial, Helvetica, sans-serif"><?=number_format($credittotal,2,'.',',')?></font></div></td>
		<td><div align="right"><font  face="Arial, Helvetica, sans-serif"><?=number_format($debittotal,2,'.',',')?></font></div></td></tr>
</table>

<?


// STOCK





$result = mysqli_fetch_array(mysqli_query($conn,"select name from company where compcode = '$compcode' ")) or die(mysqli_error());
$startdt = us($startdt);
?>

<table width=658  border=1 rules="all">
<tr><td align ="center" colspan="100%"><h4><font face="Arial, Helvetica, sans-serif"><?=$result['name']?></font></h4></td></tr>
<tr><td align ="center" colspan="100%"><h4><font face="Arial, Helvetica, sans-serif">Closing Stock as on &nbsp;<?=british($startdt)?></font></h4></td></tr>

<tr>
<td width="46"><font face="Arial, Helvetica, sans-serif">Sl No</font></td>
<td width="418"><font face="Arial, Helvetica, sans-serif">Product</font></td>
<td width="83"  align="right"><font face="Arial, Helvetica, sans-serif">Closing Stock</font></td>
<td width="83"  align="right"><font face="Arial, Helvetica, sans-serif">Minimum Stock</font></td>
</tr>


<?
$temp=' ';
$totvalue=0;
$subtot=0;
$qtytot=0;

$qtyonhand = $prow[0];

$sl=1;

$presult = mysqli_query($conn,"select distinct prodcode  from product ");
while($prodcode = mysqli_fetch_row($presult))
{
	$ind=0;
	$result = mysqli_query($conn,"select prodcode,prodname,minimum_qty from product where prodcode = '$prodcode[$ind]'  order by prodcode,prodname");
	$row=mysqli_fetch_row($result);
	$prodname=$row[1];


	$result3 = mysqli_query($conn,"select opening_balance from tempop where  acctcode = '$prodcode[$ind]' ") or die(mysqli_error());
	$row3=mysqli_fetch_row($result3);
	$opening=$row3[0];

	$qresult = mysqli_query($conn,"select sum(inqty) - sum(outqty) from stktrans where  acctcode = '$prodcode[$ind]' and compcode = '$compcode' and date <= '$enddt' ") or die(mysqli_error());
	$qrow = mysqli_fetch_row($qresult);
	if($qrow[0] != 0 or $opening !=0)
	{
	

		?>
		<tr>
		<td width="46"><font face="Arial, Helvetica, sans-serif"><? echo($sl);?>  </font></td>		
		<td width="418"><font face="Arial, Helvetica, sans-serif"><? echo($prodname);?>  </font></td>
		<td width="83" align="right"><font face="Arial, Helvetica, sans-serif"><? echo($qrow[0]+$opening);?></font></td>
		<td width="83" align="right"><font face="Arial, Helvetica, sans-serif"><? echo($minimum_qty);?></font></td>
		</tr>

		<?  
		$qty = $qrow[0]+$opening;
		$sl++;
		mysqli_query($conn,"update acctmast set opening_balance = '$qty' where acctcode = '$prodcode[$ind]' ") or die(mysqli_error());	
	}
}
mysqli_query($conn,"truncate salesreg") or die(mysqli_error());
mysqli_query($conn,"truncate stktrans") or die(mysqli_error());
mysqli_query($conn,"truncate csalesreg") or die(mysqli_error());
mysqli_query($conn,"truncate tranid") or die(mysqli_error());
?>



</table>


