<?php include("../../include/dbconnect.php");?>
<script language="javascript">
function OpenWindow(url)
{
	window.open(url,'win','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>


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


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




?>
</font>
<TABLE  width="904" BORDER=1 CELLPADDING=4 RULES=COLS  bgcolor="#F4E795" >

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

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

  <tr> 
  <tr> 
  
    <td colspan="4"  align="center"><font  face="Arial, Helvetica, sans-serif"> <?=$address?></font></td>  </tr>

  <tr>   
  
    <td colspan="4"  align="center"><font size="+1" face="Arial, Helvetica, sans-serif" color="#0000CC"> Profit Centre:<?=$branchname?></font></td>  </tr>

  <tr> 
    <td colspan="4"  align="center"><font face="Arial, Helvetica, sans-serif">Profit   &amp; Loss Account as on    <?=british($enddt);?>      </font></td>
  </tr>
  <tr><td width="360"><hr></td>
<td width="78"><hr></td>
<td width="328"><hr></td>
<td width="86"><hr></td>

</tr>
</THEAD>
<tbody>

  <tr   bgcolor="#DFBC31"> 
    <td><div align="center"><font face="Arial, Helvetica, sans-serif">Expense</font></div></td>
        <td><div align="right"><font face="Arial, Helvetica, sans-serif">Debit</font></div></td>
    <td  ><div align="center"><font face="Arial, Helvetica, sans-serif">Income</font></div></td>
        <td  ><div align="right"><font face="Arial, Helvetica, sans-serif">Credit</font></div></td>
  </tr>
  <tr   bgcolor="#EDE770"> 
    <td ><div align="center"><font face="Arial, Helvetica, sans-serif">Description</font></div></td>
    <td ><div align="right"><font face="Arial, Helvetica, sans-serif">Rs</font></div></td>
    <td ><div align="center"><font face="Arial, Helvetica, sans-serif">Description</font></div></td>
    <td ><div align="right"><font face="Arial, Helvetica, sans-serif">Rs</font></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]' ") 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]' ") 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 = $rawmaterial+$packingmaterial+$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 Profit & Loss Rs. ';
		$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> 
    <td ><div align="left"><font face="Arial, Helvetica, sans-serif"><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)?>')" href ="#"><?  echo $expense_des[$ind];?></font></div></td>
    <td ><div align="right"><font face="Arial, Helvetica, sans-serif"><? if($expense_amt[$ind]>0) { echo($expense_type[$ind]);echo number_format($expense_amt[$ind],2);}?></font></div></td>  
    <td ><div align="left"><font face="Arial, Helvetica, sans-serif"><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)?>')" href ="#"><?   echo $income_des[$ind];?></font></div></td>
    <td ><div align="right"><font face="Arial, Helvetica, sans-serif"><? if($income_amt[$ind]>0) {echo($income_type[$ind]); echo number_format($income_amt[$ind],2);}?></font></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> 
	<td  align="left"><font  face="Arial, Helvetica, sans-serif">Grant Total </font></td>

      
		<td align=right><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">
        <?=number_format($netamt,2)?>
		</b></font></td>
	<td  align="left"><font  face="Arial, Helvetica, sans-serif">Grant Total </font></td>	
		<td align=right><font face="Arial, Helvetica, sans-serif"><b><font face="Arial, Helvetica, sans-serif">
        <?=number_format($netamt,2)?>
		</b></font></td>
	     

</tr>

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


?>




