<?
session_start();
$brcode = $_SESSION['brcode'];
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
?>
<style>
body,td 
{
    margin: 0 auto;
	padding: 0;
	font-family: Arial, Verdana, Sans-Serif;
	font-size: 0.8em;
	background: #E7E7E7;
}

// td{font-family:verdana;font-size:14;color:black;}
 th{font-family:verdana;font-size:14;color:white;background-color:#5b5858}
 body{background-color:ivory}
</style>
<?
 $d = $_REQUEST['d'];
 $m = $_REQUEST['m'];
 $y = $_REQUEST['y'];
 $d1 = $_REQUEST['d1'];
 $m1 = $_REQUEST['m1'];
 $y1 = $_REQUEST['y1'];
 $df=$y."-".$m."-".$d;
 $dt=$y1."-".$m1."-".$d1;
?>
<?
include("../opendb.php");
include("../test.php");
include("dwstock.php");
include("dwstock2.php");
$r=mysqli_query($db,"select brname from branch where brcode='$brcode'");
$br_row=mysqli_fetch_row($r);
$branchname=strtoupper($br_row[0]);

?>
<head>
<title>PSN - Profit & Loss </title>
<script>
function sh()
{
 window.open("prnpl.php");
}
</script>
</head>

<center>
<table border=0 width=100%>
<tr><td align=center><b>
<?print $branchname?><BR>

TRADING AND  PROFIT AND LOSS ACCOUNT FROM<? print datetohtml($df).' to '.datetohtml($dt)?>
</b></td>
<td width=30><a href='javascript:sh()' title='Print'><img src='../Type001.gif' border=0></a></td></tr>
</table>
<hr>
</center>
<table border=1 width=100%>
<tr><td valign=top>
<table border =1 width=100%>
<?$ops=mysqli_query($db,"select sum(pvalue) from vehtmp");
$stocks=mysqli_fetch_row($ops);
$ostock=$stocks[0];


$cls=mysqli_query($db,"select sum(pvalue) from vehtmpcls");
$stockscls=mysqli_fetch_row($cls);
$cstock=$stockscls[0];



?>
<tr><td colspan=2><font color=brown>To Opening Stock </td><td align=right><font color=brown><?print putComa($ostock);?></td></tr>
<tr><td colspan=2><font color=brown>To Purchase A/c</td></tr>
<?
$sumleft=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='purchase'") or die(mysqli_error());
while($prows=mysqli_fetch_array($rrr))
{ //while1
 $result=mysqli_query($db,"select * from ledger where grp='$prows[gname]' and brcode='$brcode' order by led");
 while($row1=mysqli_fetch_array($result))
 {
  $sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt' ";
   $re2=mysqli_query($db,$sq);
  $re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  $opb=mysqli_fetch_array($re3);
$tr=0; $tp=0;
if($opb[drcr]=="Dr")
{
$tr+=$opb[op];
}
else if($opb[drcr]=="Cr")
{
$tp+=$opb[op];
}
$a1=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
$a1row=mysqli_fetch_row($a1);
$tr=$a1row[0];
$tp=$a1row[1];

$bal=$tr-$tp;
$tot+=$bal;
//if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
} //end while 1	
$sumleft+=$tot;
print "<td align=right>".putComa($tot)."</td></tr>";

	//**********************888 PURCHASE RETURNS
print "<tr><td><font color=brown> Purchase Returns</td></tr>";
$acg_pret=mysqli_query($db,"select gname from acgroups where headgroup='purchase returns' and gname!='-'")or die(mysqli_error());
while($acg_pret_row=mysqli_fetch_row($acg_pret))
{
 $sq1="select * from ledger where grp='$acg_pret_row[0]' and brcode='$brcode'"; 
 $pret_led=mysqli_query($db,$sq1) or die(mysqli_error()); 
  while($pret_led_row=mysqli_fetch_row($pret_led))
  {
   $sq="select sum(dramt) ,sum(cramt)from journal2 where partic='$pret_led_row[0]' and brcode='$brcode' and date>='$df' and date<='$dt'";
    $amt_ret=mysqli_query($db,$sq) or die(mysqli_error());
     $amt_ret_row=mysqli_fetch_row($amt_ret);
     $preturn_value=($amt_ret_row[0]-$amt_ret_row[1]);
	$preturn_value*=-1;
	print "<tr><td colspan=2><blockquote>$pret_led_row[0]</td><td align=right>".putComa($preturn_value)."</td></tr>";
  }
}

//*********************************************************************************************//
print "<tr><td colspan=2><font color=brown>To Direct Expenses</td></tr>";
$bal=0; $tot=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='DIRECT EXPENSE'") or die(mysqli_error());
while($dexrow=mysqli_fetch_array($rrr))
{ //while 1
 $sq="select * from ledger where grp='$dexrow[0]' and brcode='$brcode' order by led";

 $result=mysqli_query($db,$sq);
 while($row1=mysqli_fetch_array($result))
 {
  $sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'";

   $re2=mysqli_query($db,$sq);
  $re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  $opb=mysqli_fetch_array($re3);
$tr=0; $tp=0;
if($opb[drcr]=="Dr")
{
$tr+=$opb[op];
}
else if($opb[drcr]=="Cr")
{
$tp+=$opb[op];
}
    $ded=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[0]' and brcode='$brcode' and date>='$df' and date<='$dt'") or die(mysqli_error());
     $dex_row=mysqli_fetch_row($ded);
 $tr+=$dex_row[0];
 $tp+=$dex_row[1];

/*while($r1=mysqli_fetch_array($re2))
{
if($r1[cr]==$row1[led])
{
//  $tp+=$r1[payment];
  $tp+=$r1[cramt];
}
else if($r1[dr]==$row1[led])
{
//  $tr+=$r1[payment];
  $tr+=$r1[cramt];
}
else
{
//$tr+=$r1[payment];
//$tp+=$r1[receipt];
}
}
*/
$bal=$tr-$tp;
$tot+=$bal;
if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
} // end while 1
$sumleft+=$tot;
$sumleft+=$ostock;
	// less purchase returns
$sumleft+=$preturn_value;
print "<td align=right>".putComa($tot)."</td></tr>";
print "<tr><td colspan=3 align=right>".putComa($sumleft)."</td>";
?>
</table>
</td>
<td width=50% valign=top>
<table border=1 width=100%>
<?
//*********************************************************************************************//
print "<tr><td colspan=2><font color=brown>By Sales</td></tr>";
$bal=0; $tot=0;
$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='sales' order by ghlevel")  or die(mysqli_error());
while($sarow=mysqli_fetch_array($rrr))
{ // while 1
 $sq="select * from ledger where grp='$sarow[0]' and brcode='$brcode'";
 $result=mysqli_query($db,$sq);
 while($row1=mysqli_fetch_array($result))
 {
  $sq="select * from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'";

   $re2=mysqli_query($db,$sq);
  $re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  $opb=mysqli_fetch_array($re3);
$tr=0; $tp=0;
if($opb[drcr]=="Dr")
{
$tr+=$opb[op];
}
else if($opb[drcr]=="Cr")
{
$tp+=$opb[op];
}
$saleres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
$salerow=mysqli_fetch_row($saleres);
$tr+=$salerow[0];
$tp+=$salerow[1];
$bal=$tr-$tp;
	//******************************88888888888888888
		$bal*=-1;
	//******************************88888888888888888

$tot+=$bal;
//if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
}// end while 1
$sumright+=$tot;
print "<td align=right>".putComa($tot)."</td></tr>";

		//********************* SALES RETURNS
print "<tr><td colspan=3><font color=brown>Sales Returns</td></tr>";

$acg_pret=mysqli_query($db,"select gname from acgroups where headgroup='sales returns' and gname!='-' and brcode='$brcode'");
while($acg_pret_row=mysqli_fetch_row($acg_pret))
{
 $sq1="select * from ledger where grp='$acg_pret_row[0]' and brcode='$brcode'";
print $sq1.'<br>';
 $pret_led=mysqli_query($db,$sq1) or die(mysqli_error()); 
  while($pret_led_row=mysqli_fetch_row($pret_led))
  {
    $amt_ret=mysqli_query($db,"select sum(cramt) from journal2 where partic='$pret_led_row[0]' and brcode='$brcode' and date>='$df' and date<='$dt'") or die(mysqli_error());
     $amt_ret_row=mysqli_fetch_row($amt_ret);
     $sreturn_value=$amt_ret_row[0];
	$sreturn_value*=-1;
	print "<tr><td colspan=2><blockquote>$pret_led_row[0]</td><td align=right>".putComa($sreturn_value)."</td></tr>";
  }
}




//print "<tr><td colspan=3 align=right>".putComa($sumright)."</td>";
//*********************************************************************************************//
print "<tr><td colspan=2><font color=brown><font color=brown>By Direct Income</td></tr>";
$bal=0; 
$tot=0;
//$sumright=0;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='Direct Income' order by ghlevel")  or die(mysqli_error());
while($dirow=mysqli_fetch_array($rrr))
{ // while 1
 $result=mysqli_query($db,"select * from ledger where grp='$dirow[0]'");
 while($row1=mysqli_fetch_array($result))
 {
  $sq="select * from journal2 where partic='$row1[led]'and brcode='$brcode' and date>='$df' and date<='$dt'";
   $re2=mysqli_query($db,$sq);
  $re3=mysqli_query($db,"select * from ledger where led='$row1[led]'"); 
  $opb=mysqli_fetch_array($re3);
$tr=0; $tp=0;
if($opb[drcr]=="Dr")
{
$tr+=$opb[op];
}
else if($opb[drcr]=="Cr")
{
$tp+=$opb[op];
}

$dinres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
$drinrow=mysqli_fetch_row($dinres);
$tr+=$drinrow[0];
$tp+=$drinrow[1];

$bal=$tr-$tp;


		//**************************8888888
			$bal=$bal*-1;
		//**************************8888888

$tot+=$bal;

//if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
} // end while 1
$sumright+=$tot; 
//$sumright=abs($sumright)+$cstock;           // CHECK


	// minus sales returns also
$sumright=$sumright+$cstock+$sreturn_value;  
         
print "<td align=right>".putComa($tot)."</td></tr>";

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
print "<tr><td colspan=2><font color=brown>By Closing Stock</td><td align=right><font color=brown>".putComa($cstock)."</td></tr>";

print "<tr><td colspan=3 align=right><b><u>".putComa($sumright)."</td>";
print "</tr></table>";
//xxxxxxxxxxxxxxxxxxxxxxxxxxx//
//$diff=abs($sumright)-$sumleft;
$diff=$sumright-$sumleft;             // CHECK
//$grossprofit=0;$grossloss=0;
  //$diff=number_format($diff,2,'','');
  //$sumright=number_format($sumright,2,'','');
if($diff>=0)
{
$trading_gross_profit=$diff;
 print "<tr><td><table border=1 width=100%><tr><td><font color=green>To Gross Profit</td><td align=right><font color=green>".putComa($diff)."</td></tr>";
print "<tr><td colspan=2 align=right><b><u>".putComa($sumright)."</tr></td></table>";
}
else
{
$trading_gross_loss=$sumleft;
 print "<table border=1 width=100%><tr><td><font color=brown>By Gross Loss</td><td align=right><font color=red>".putComa(abs($diff))."</td></tr>";
print "<tr><td colspan=2 align=right><font color=black>".putComa($sumleft)."</tr></td></table>";
}
?>
</table></table>

<hr>
<table border=2 width=100%>
<tr><td width=50%>
<table border=1 width=100%>

<?if($diff<0)
{
$grossloss=$diff;
?>
<td colspan=2>By Gross Loss b/d</td><td align=right><?print putComa(abs($diff))?></td></tr>
<?
}
?>

<tr><td colspan=2><font color=brown>To Indirect Expenses</td></tr>
<?
$bal=0; $tot=0;
//$sumright=0;
$sumleft2=0;

//********************************* all other expenses are treated as indirect expenses **//
$rrr=mysqli_query($db,"select * from acgroups where headgroup='Indirect Expense' order by ghlevel")  or die(mysqli_error());
while($indirow=mysqli_fetch_array($rrr))
{ // while 1

 $result=mysqli_query($db,"select * from ledger where grp='$indirow[0]' and brcode='$brcode' order by led");
 while($row1=mysqli_fetch_array($result))
 {
  $re3=mysqli_query($db,"select * from ledger where led='$row1[led]' and brcode='$brcode'"); 
  $opb=mysqli_fetch_array($re3);
$tr=0; $tp=0;
if($opb[drcr]=="Dr")
{
$tr+=$opb[op];
}
else if($opb[drcr]=="Cr")
{
$tp+=$opb[op];
}

// sum
$indexres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
$indrow=mysqli_fetch_row($indexres);
$tr+=$indrow[0];
$tp+=$indrow[1];

//********
$bal=$tr-$tp;
$tot+=$bal;
if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";
}
} // end while 1
//$sumright+=$tot;
$sumleft2+=$tot;
print "<td align=right>".putComa($tot)."</td></tr>";
$sumleft2=$tot+abs($grossloss);
	
	//*8888888888888
		$ind_exp_tot=$sumleft2;
	//*8888888888888

//print "<tr><td colspan=3 align=right>X".putComa($sumleft2)."</td>";
print "</tr></table>";
?>
<td valign=top>

<!-- *************right indirect income-->
<!--<tr><td>-->
<table border=1 width=100%>
<?if($diff>0)
{
$grossprofit=$diff;
?>
<td colspan=2><font color=green>By Gross Profit b/d</td><td align=right><font color=green><?print putComa($diff)?></td></tr>
<?
$gp=$diff;
}
?>

<tr><td colspan=2><font color=brown>By Indirect Income</td></tr>
<?
$bal=0; $tot=0;
$sumright=0;
$sumright+=$diff;
$rrr=mysqli_query($db,"select * from acgroups where headgroup='Indirect Income' order by ghlevel")  or die(mysqli_error());
while($indincrow=mysqli_fetch_array($rrr))
{ // while 1
 $result=mysqli_query($db,"select * from ledger where grp='$indincrow[0]' and brcode='$brcode' order by led");
 while($row1=mysqli_fetch_array($result))
 {

  $re3=mysqli_query($db,"select * from ledger where led='$row1[led]'"); 
  $opb=mysqli_fetch_array($re3);
$tr=0; $tp=0;
if($opb[drcr]=="Dr")
{
$tr+=$opb[op];
}
else if($opb[drcr]=="Cr")
{
$tp+=$opb[op];
}

//*** sum
$indincres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where partic='$row1[led]' and brcode='$brcode' and date>='$df' and date<='$dt'");
$indrow=mysqli_fetch_row($indincres);
$tr+=$indrow[0];
$tp+=$indrow[1];


//*****************
$bal=$tr-$tp;

//*******************888
 $bal=$bal*-1;
//*****************88888888
//$tot=$tot+abs($bal);
$tot=$tot+$bal;
if($bal!=0)
 print "<tr><td><blockquote>$row1[led]</td><td align=right>".putComa($bal)."</td>";

}
} // end while 1
//$gtot=$tot+$diff;
$gtot=$tot+$grossprofit;
//$sumright=abs($sumright)+$tot;
		$sumright=$sumright+$tot;
$sumright+=$grossprofit;

//$porloss1=$sumleft2-$tot;//+abs($diff));


print "<td align=right>".putComa($tot)."</td></tr>";
print "<tr><td colspan=3 align=right><b><u>".putComa($gtot)."</td></tr></table>";
	//*8888888888888
	$pl_credit_tot=$tot;
	//*8888888888888

//print "left".$sumleft2. " right".$gtot;
//  $sumleft2=number_format($sumleft2,2,'','');
//  $gtot=number_format($gtot,2,'','');

if($sumleft2>$gtot)//summright)
{
//$porloss=abs($sumleft2)-(abs($gtot));//+abs($diff));

$porloss=($sumleft2-$gtot);//+abs($diff));
  //$porloss=number_format($porloss,2,'','');
$gtot=$sumleft2;
print "<tr><td></td><td><table border=2 width=100%><tr><td colspan=2><font color=darkbrown><b>Net Loss</td><td align=right><font color=darkbrown>".putComa($porloss)."</td></tr>";
Print "<tr><td colspan=3 align=right>".putComa($gtot)."</td></tr>";
print "</tr></table>";
}
else 
{
//$porloss=abs($sumleft2)-(abs($gtot));//+abs($diff));
//$porloss=($sumleft2-$gtot);//+abs($diff));

//  $porloss=number_format($porloss,2,'','');
//  $gtot=number_format($gtot,2,'','');

$porloss=($gtot-$sumleft2);//+abs($diff));
  //$porloss=number_format($porloss,2,'','');

print "<tr><td><table border=2 width=100%><tr><td colspan=2><font color=green><b>Net Profit</td><td align=right><font color=green>".putComa($porloss)."</td></tr>";
Print "<tr><td colspan=3 align=right><b><u>".putComa($gtot)."</td></tr>";
print "</tr></table>";
}

//mysqli_query($db,"update pl set npl='$porloss'") or die(mysqli_error());
?>

</table>
</table>
</tr></td>
</table>
<?
/*
print '<br>trading_g_profit='.$trading_gross_profit;
print '<br>trading_g_loss='.$trading_gross_loss;

print '<br>p&l credit='.$pl_credit_tot;
print '<br>ind exp tot='.$ind_exp_tot;

$tot1=$trading_gross_loss+$ind_exp_tot;
$tot2=$trading_gross_profit+$pl_credit_tot;

  $tot1=number_format($tot1,2,'','');
  $tot2=number_format($tot2,2,'','');


if($tot1<=$tot2)
{
print "<br>tot1=". $tot1." tot 2=".$tot2;
  $netprofit=($tot2-$tot1);
  $netprofit=number_format($netprofit,2,'','');
 print "<b>Net Profit=".$netprofit;
}
else
{
  $netloss=($tot1-$tot2);
  $netloss=number_format($netloss,2,'','');
 print "<b>Net Loss=".$netloss;
}


//print "NPL=$porloss";*/
?>
