<?php include("../../include/dbconnect.php");?>
<script language="javascript">
function OpenWindow(url)
{
	window.open(url,'ledg','width=900,height=400,scrollbars=yes,resizable=0');
}
</script>
<STYLE TYPE='text/css'>

	a {text-decoration:none}
	a { color: black; }
P.pagebreakhere {page-break-after: always}
</STYLE>

<title>Profit & Loss  Printing</title>
<form>


<span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >
<?


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

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

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


$fname = 'invoice.txt';

$totcr=0;
$totdr=0;
$enddt = actdate($enddt);
$enddt = us($enddt);
date_belong($enddt,$account_year);

$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,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];






?>
</span>
<table  width="914" border="0" align="center" cellpadding="0" cellspacing="0"    style="padding:5px; border:1px solid #666666">

    <tr>
      <td height="80" colspan="4"  align="center" bgcolor="#095F92">
      <div style="padding:10px;"><img src="../../images/hapbco_logo.png" alt="logo" width="82" height="87" style="float:left"/></div>
      <span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#FFFFFF; font-weight:bold;" > <?=$compname?> </span><br />
      <span style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#FFFFFF; font-weight:bold;" > <?=$address?> </span><br />      
</td>
    </tr>

    <tr>
      <td height="30" colspan="4"  align="center" bgcolor="#EAEAEA"><span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#000; font-weight:bold;" >Profit   &amp; Loss Account as on    <?=british($enddt);?></span></td>
    </tr>
    <tr>
    <tr>
      <td height="30" colspan="4"  align="center"  >
      
      <table  width="100%" border="0" cellpadding="4" rules="cols"  >
              <colgroup>
          </colgroup>
        <colgroup>
          </colgroup>
        <colgroup>
          </colgroup>
        <colgroup>
          </colgroup>          
 
</THEAD>
<tbody>

  <tr   bgcolor="#095F92"> 
    <td><div align="center"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Expense</span></div></td>
        <td><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Debit</span></div></td>
    <td  ><div align="center"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Income</span></div></td>
        <td  ><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Credit</span></div></td>
  </tr>
  <tr  bgcolor="#095F92"> 
    <td ><div align="center"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Description</span></div></td>
    <td ><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Rs</span></div></td>
    <td ><div align="center"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Description</span></div></td>
    <td ><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Rs</span></div></td>
  </tr>

  <tr><td><hr></td>
<td><hr></td>
<td><hr></td>
<td><hr></td>
</tr> 
  <?
$expensetotal = 0;
$x=0;
$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='22' and type ='15' order by sequence ") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{

	$subtotal = 0;
	$aresult = mysqli_query($conn,"select distinct ledger.acctcode,acctmast.description from acctmast,ledger where acctgroup= '$row[0]' and ledger.acctcode = acctmast.acctcode and ledger.branch_code = '$branch'   union select distinct openbalance.acctcode,acctmast.description from acctmast,openbalance where acctgroup= '$row[0]' and openbalance.acctcode = acctmast.acctcode and openbalance.branch_code = '$branch'  order by description") or die(mysqli_error());
	while($arow = mysqli_fetch_row($aresult))
	{

			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] 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')
			{
				$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' and branch_code = '$branch' and compcode = '$compcode'    and account_year = '$account_year'") or die(mysqli_error());		

		$ledrow = mysqli_fetch_row($ledresult);	
		$ledgamt = $ledrow[0];
		$ledtotal = $amt + $ledgamt;
		$expensetotal = $expensetotal + $ledtotal;

		if($ledtotal != 0)
		{
			if($ledtotal > 0)
			{
				$type = '';
	
			}
			elseif($ledtotal < 0)
			{
				$type = '(-)';
				$ledtotal = $ledtotal*-1;
		
			}
 			$expense_head[$x]=$arow[0];
			$expense_des[$x]=$arow[1];
			$expense_amt[$x]=$ledtotal;
			$expense_type[$x]=$type;
			$x++;

		}		
	}	
	
}



//  INCOME PART OF STATEMENTS.


$incometotal = 0;


$m=0;
$trade_account = trading($rawmaterial,$packingmaterial,$finishedgoods,$enddt,$account_year,$branch,$compcode);
$incometotal = $trade_account;


if($trade_account > 0)
{
	$type = 'CR';
}
elseif($trade_account < 0)
{
	$type = 'DR';
	$trade_account = $trade_account*-1;
}

$income_head[$m]='Profit transfered..';			
$income_des[$m]='Profit From Trade A/c.';
$income_amt[$m]=$trade_account;
$income_type[$m]='';

$m=1;

$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='22' and type ='14' ") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{

	$subtotal = 0;
			$aresult = mysqli_query($conn,"select distinct ledger.acctcode,acctmast.description from acctmast,ledger where acctgroup= '$row[0]' and ledger.acctcode = acctmast.acctcode and ledger.branch_code = '$branch'   union select distinct openbalance.acctcode,acctmast.description from acctmast,openbalance where acctgroup= '$row[0]' and openbalance.acctcode = acctmast.acctcode and openbalance.branch_code = '$branch'  order by description") or die(mysqli_error());
	while($arow = mysqli_fetch_row($aresult))
	{
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] 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')
			{
				$amt = $oprow[0];
			}
			elseif($oprow[1] == 'CR')
			{
				$amt = $oprow[0]*-1;
			}	
		
		
			$ledresult = mysqli_query($conn,"select sum(cramt)-sum(dbamt) from ledger where acctcode = $arow[0] and date <= '$enddt' and branch_code = '$branch' and compcode = '$compcode'   and account_year = '$account_year' ") or die(mysqli_error());		
		$ledrow = mysqli_fetch_row($ledresult);
		$ledgamt = $ledrow[0];
		$ledtotal = $amt + $ledgamt;

	    $incometotal = $incometotal + $ledtotal;

		if($ledtotal != 0)
		{
			if($ledtotal > 0)
			{
							$type = '';

			
			}
			elseif($ledtotal < 0)
			{
				$type = '(-)';
				$ledtotal = $ledtotal*-1;
			
			}
					
			$income_head[$m]=$arow[0];			
			$income_des[$m]=$arow[1];
			$income_amt[$m]=$ledtotal;
			$income_type[$m]=$type;
			$m++;			

		}		
	}	
	
}
$netprofit = $incometotal - $expensetotal;





if($netprofit >=0)
{
		$expense_des[$x]='Net Profit & Loss A/c (Profit)';
		$expense_amt[$x]=$netprofit;
		$expense_type[$x]=$type;
}
elseif($netprofit <0)
{
		$income_des[$m]= 'Net Profit & Loss A/c(Loss)';
		$income_amt[$m]=$netprofit*-1;
		$income_type[$m]=$type;	
}
if(count($expense_amt)>count($income_amt))
{
	$max= count($expense_amt);
}
else
{
	$max = count($income_amt);
}

for($ind = 0;$ind <=$max;$ind++)
{
?>
  <tr>
  	<?
	if($branch !='ALL')
	{
		?> 
    	<td ><div align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><a onClick="javascript:OpenWindow('../ledgers/ledg_print.php?acctcode[0]=<?echo($expense_head[$ind]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>&enddt=<?=british($enddt)?>&prnt=All')" href ="#"><?  echo ucwords(strtolower($expense_des[$ind]));?></span></div></td>
   		<?
	}
	else
	{
		?> 
    	<td ><div align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><a onClick="javascript:OpenWindow('../ledgers/allbr_ledg_print.php?acctcode[0]=<?echo($expense_head[$ind]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>&enddt=<?=british($enddt)?>&prnt=All')" href ="#"><?  echo ucwords(strtolower($expense_des[$ind]));?></span></div></td>
   		<?

	}
     ?>  
    <td ><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><? if($expense_amt[$ind]>0) { echo($expense_type[$ind]);echo number_format($expense_amt[$ind],2);}?></span></div></td>  
   
   	<?
	if($branch !='ALL')
	{	
		?>
 	   <td ><div align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><a onClick="javascript:OpenWindow('../ledgers/ledg_print.php?acctcode[0]=<?echo($income_head[$ind]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>&enddt=<?=british($enddt)?>&prnt=All')" href ="#"><?   echo ucwords(strtolower($income_des[$ind]));?></span></div></td>
		<?
	}	
	else
	{
		?>
 	   <td ><div align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><a onClick="javascript:OpenWindow('../ledgers/allbr_ledg_print.php?acctcode[0]=<?echo($income_head[$ind]);?>&compcode=<?=$compcode?>&branch=<?=$branch?>&account_year=<?=$account_year?>&enddt=<?=british($enddt)?>&prnt=All')" href ="#"><?   echo ucwords(strtolower($income_des[$ind]));?></span></div></td>
		<?
	}
	?>
    <td ><div align="right"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;  font-weight:bold;" ><? if($income_amt[$ind]>0) {echo($income_type[$ind]); echo number_format($income_amt[$ind],2);}?></span></div></td>

  </tr>
<?

}


	
	
	
	
	

		if($incometotal >= $expensetotal)
		{
			$netamt = $incometotal;
		}
		else
		{
			$netamt = $expensetotal;		
		}
		
?>
  <tr><td><hr></td>
<td><hr></td>
<td><hr></td>
<td><hr></td>
</tr>



	<tr bgcolor="#095F92"> 
	<td  align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Grant Total </span></td>

      
		<td align=right><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" ><b><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >
        <?=number_format($netamt,2)?>
		</b></span></td>
	<td  align="left"><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >Grant Total </span></td>	
		<td align=right><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" ><b><span style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#FFFFFF;  font-weight:bold;" >
        <?=number_format($netamt,2)?>
		</b></span></td>
	     

	</tbody>
	


</table>




<?

function trading($rawmaterial,$packingmaterial,$finishedgoods,$enddt,$account_year,$branch,$compcode)
{
    global $conn;
	$expensetotal = 0;
	$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='33' and type ='15' ") or die(mysqli_error());
	while($row = mysqli_fetch_row($result))
	{
		$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' order by description ") or die(mysqli_error());
		while($arow = mysqli_fetch_row($aresult))
		{
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] 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')
			{
				$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' and account_year = '$account_year'  and branch_code = '$branch' and compcode = '$compcode'") or die(mysqli_error());
			$ledrow = mysqli_fetch_row($ledresult);	
			$ledgamt = $ledrow[0];
			$ledtotal = $amt + $ledgamt;
			$expensetotal = $expensetotal + $ledtotal;
	
		}	
	}
	//  INCOME PART OF STATEMENTS.
	
	$incometotal = 0;
	$closingstock = (float)$rawmaterial+(float)$packingmaterial+(float)$finishedgoods;
	
	if($closingstock>0)
	{
		$incometotal = $closingstock;
	}
	$result = mysqli_query($conn,"select acctcode,groupname from acctgroup where appear_in='33' and type ='14' ") or die(mysqli_error());
	while($row = mysqli_fetch_row($result))
	{
		$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' ") or die(mysqli_error());
		while($arow = mysqli_fetch_row($aresult))
		{
		
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0] 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')
			{
				$amt = $oprow[0];
			}
			elseif($oprow[1] == 'CR')
			{
				$amt = $oprow[0]*-1;
			}
	
			$ledresult = mysqli_query($conn,"select sum(cramt)-sum(dbamt) from ledger where acctcode = $arow[0] and date <= '$enddt' and account_year = '$account_year' and branch_code = '$branch' and compcode = '$compcode' ") or die(mysqli_error());
			$ledrow = mysqli_fetch_row($ledresult);	
			$ledgamt = $ledrow[0];
			$ledtotal = $amt + $ledgamt;
			$incometotal = $incometotal + $ledtotal;
		}	
	}

	$netprofit = $incometotal - $expensetotal;
	return $netprofit;
}


?>




