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



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




?>
</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 />
</td>
    </tr>
    <tr>
      <td height="35" colspan="4"  align="center" bgcolor="#F0F0F0"><span style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#000; font-weight:bold;" > Profit Centre:<?=$branchname?></span></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='33' and type ='15' order by sequence ") or die(mysqli_error());
while($row = mysqli_fetch_row($result))
{

	$subtotal = 0;
	//$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' order by description ") or die(mysqli_error());
		if($branch=='ALL')
		{	
			$aresult = mysqli_query($conn,"select acctcode,description,subgroup from acctmast where acctgroup = '$row[0]'   order by description") or die(mysqli_error());
		}
		else
		{
		
			$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))
	{

		if($branch=='ALL')
		{
			$amt=0;	
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0]  and compcode = '$compcode'   and account_year = '$account_year' ") or die(mysqli_error());
		}
		else
		{
			$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;
			}	
		
		
		if($branch=='ALL')
		{
			$ledresult = mysqli_query($conn,"select sum(dbamt)-sum(cramt) from ledger where acctcode = $arow[0] and date <= '$enddt' and compcode = '$compcode'   and account_year = '$account_year' ") or die(mysqli_error());
		}
		else
		{
			$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;

$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))
{

	$subtotal = 0;
	//$aresult = mysqli_query($conn,"select acctcode,description from acctmast where acctgroup = '$row[0]' order by description ") or die(mysqli_error());
		if($branch=='ALL')
		{	
			$aresult = mysqli_query($conn,"select acctcode,description,subgroup from acctmast where acctgroup = '$row[0]'   order by description") or die(mysqli_error());
		}
		else
		{
			$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))
	{

		if($branch=='ALL')
		{
			$amt=0;	
			$opresult = mysqli_query($conn,"select opening_balance,db_cr from openbalance where acctcode = $arow[0]  and compcode = '$compcode'   and account_year = '$account_year'") or die(mysqli_error());
		}
		else
		{
			$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;
			}	
		
		
		if($branch=='ALL')
		{
			$ledresult = mysqli_query($conn,"select sum(cramt)-sum(dbamt) from ledger where acctcode = $arow[0] and date <= '$enddt' and compcode = '$compcode'  and account_year = '$account_year' ") or die(mysqli_error());
		}
		else
		{
			$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++;			

		}		
	}	
	
}

$closingstock = (float)$rawmaterial+(float)$packingmaterial+(float)$finishedgoods;
if($closingstock>0)
{
	$incometotal = $incometotal+$closingstock;
	
			$income_head[$m]='CLOSING STOCK';			
			$income_des[$m]='';
			$income_amt[$m]=0;
			$income_type[$m]='';
			$m++;			
	
}
	if($rawmaterial >0)
	{
				$income_head[$m]='CLOSING STOCK';			
			$income_des[$m]='Raw Material';
			$income_amt[$m]=$rawmaterial;
			$income_type[$m]='';
			$m++;			
	
	}
	if($packingmaterial >0)
	{
			$income_head[$m]='CLOSING STOCK';			
			$income_des[$m]='Packing Material';
			$income_amt[$m]=$packingmaterial;
			$income_type[$m]='';
			$m++;			
	}
	if($finishedgoods >0)
	{
			$income_head[$m]='CLOSING STOCK';			
			$income_des[$m]='Finished Goods';
			$income_amt[$m]=$finishedgoods;
			$income_type[$m]='';
			$m++;			

	}



$netprofit = $incometotal - $expensetotal;





if($netprofit >=0)
{
		$expense_des[$x]='Gross Profit/ Loss (Profit) transfered to P&L Rs. ';
		$expense_amt[$x]=$netprofit;
		$expense_type[$x]=$type;
}
elseif($netprofit <0)
{
		$income_des[$m]= 'Gross Profit/ Loss (Loss) transfered to P&L Rs. ';
		$income_amt[$m]=$netprofit*-1;
		$income_type[$m]=$type;	
}
if(count((array)$expense_amt)>count((array)$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;		
		}
if($work_order_value>0)
{		
	$profit_percentage = ($work_order_value - $expensetotal) / $work_order_value * 100;		
}		
?>
  <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)
{
	$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 substring(acctcode,1,6) = '$row[0]' 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 acctmast 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;
			$expensetotal = $expensetotal + $ledtotal;
	
		}	
	}
	//  INCOME PART OF STATEMENTS.
	
	$incometotal = 0;
	$closingstock = $rawmaterial+$packingmaterial+$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 substring(acctcode,1,6) = '$row[0]' ") 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 acctmast 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(cramt)-sum(dbamt) from ledger where acctcode = $arow[0] and date <= '$enddt' ") or die(mysqli_error());
			$ledrow = mysqli_fetch_row($ledresult);	
			$ledgamt = $ledrow[0];
			$ledtotal = $amt + $ledgamt;
			$incometotal = $incometotal + $ledtotal;
		}	
	}

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


?>




