<?php
session_start();
if($_SESSION["username"]=="")
{
 	echo "You are not Logged in Please <a href='index.html'>Sign in </a> First ";
}
else
{


?>
<!doctype html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8" lang=""> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9" lang=""> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en">
<!--<![endif]-->

<head>

</head>

<body>


    <!-- Left Panel -->

   <!-- /#left-panel -->

    <!-- Left Panel -->

    <!-- Right Panel -->

    <div id="right-panel" class="right-panel">

        <!-- Header-->
        
        <!-- Header-->

        

       
						<center> <img src="logo.jpg" align="middle" /></center></strong>
                        <h1><center><font color=black size=8pt face=Impact><? echo "" . $_SESSION["userid"]; ?></font></center></h1>
                   
                                <strong class="card-title"><center>Income Statement Report</center> </strong>
                          
							<?php

include 'dbc.php';


$userid=$_SESSION["userid"];
$from_date=$_POST["from_date"];
$to_date=$_POST["to_date"];


?>
                               
								<h4><center><? echo "For the Period of From:" . $from_date . "To:" . $to_date; ?></center></h4>
                                  
									<?php
include 'dbc.php';


$userid=$_SESSION["userid"];
  $sqlinactives = "SELECT * FROM invoice where date between '$from_date' and '$to_date' and  user_id='$userid'";
$resultinactives=$conn->query($sqlinactives);
   while($rowinactives= $resultinactives->fetch_assoc())
 
  {
  
  $tfeeinactives=$tfeeinactives + $rowinactives['discount'];

  }
  $totaldiscount=$tfeeinactivest + $tfeeinactives;
   			 $sqlinactives1 = "SELECT * FROM qoute_details where date between '$from_date' and '$to_date' and  user_id='$userid'";
$resultinactives1=$conn->query($sqlinactives1);
   while($rowinactives1= $resultinactives1->fetch_assoc())
 
  {
  
  $tfeeinactivestret=$tfeeinactivestret + $rowinactives1['amount'];

  }
  
   $sqlinactive1 = "SELECT * FROM invoice_details where date between '$from_date' and '$to_date' and  user_id='$userid'";
$resultinactive1=$conn->query($sqlinactive1);
while($rowinactive1 = $resultinactive1->fetch_assoc())
 
  {
  $item_name1 = $rowinactive1['item_name'];
   $qty1 = $rowinactive1['qty'];
  $sql101 = "SELECT * FROM items where item_name = '$item_name1' and  user_id = '$userid'";
$result101=$conn->query($sql101);
$row101 = $result101->fetch_assoc();
$crate1=$row101['cost_price'] ;
$costamount1 = $crate1 * $qty1;
						$costamount1qtyret = $crate1 * $qtyret;
  $tfeeinactive1=$tfeeinactive1 + $rowinactive1['amount'];
  $tcostamount1 = $tcostamount1 + $costamount1;
  
 					 
  }
 
					 
					 $tcostamount1ret = $tcostamount1ret + $costamount1qtyret;
  $totalsale= ($tfeeinactive + $tfeeinactive1) - ($tfeeinactivestret );
  
  
 						 $totalcostamount=$tcostamount1 - $tcostamount1ret;
  $netitemprofit1 = ($totalsale) - ($totaldiscount);
    $netitemprofit = ($netitemprofit1) - ($totalcostamount);
	 
  $amounts1=0;
  $amounts2=0;
  $amounts3=0;
  $amounts4=0;
  $amounts5=0;
  $amounts6=0;
  $amounts7=0;
  $amounts8=0;
 

$netincome=$netitemprofit;
  echo "<table border=10 width=100% align=center bgcolor=white>";

 echo "<tr height=50>";
echo "<th width=80%  bgcolor=#0099FF>  Item & Fee Discription Details   </th>";
echo "<th width=20%  bgcolor=#0099FF>  Amount   </th>";
 
   echo "</tr>";
    echo "<tr>";
echo "<td width=80%>" . "Cost of Items Sold" .   "</td>";
echo "<td width=20%>" . $tfeeinactive1 .   "</td>";
   echo "</tr>";
    echo "<tr>";
echo "<td width=80%>" . "Cost of Items Return" .   "</td>";
echo "<td width=20%>" . $tfeeinactivestret .   "</td>";
   echo "</tr>";
    echo "<tr height=50>";
echo "<th width=80%>Net Sales</th>";
echo "<th width=20%>  $totalsale </th>";
   echo "</tr>";
    echo "<tr>";
echo "<td width=80%>" . "Less Discount on Items" .   "</td>";
echo "<td width=20%>" . $totaldiscount .   "</td>";
   echo "</tr>";
   
   echo "<tr height=50>";
echo "<th width=80%>Net Cost of Items Sold</th>";
echo "<th width=20%>  $netitemprofit1 </th>";
   echo "</tr>";
   
    echo "<tr>";
echo "<td width=80%>" . "Cost of Items" .   "</td>";
echo "<td width=20%>" . $totalcostamount .   "</td>";
   echo "</tr>";
   
    echo "<tr height=50>";
echo "<th width=80%>Net Items Profit</th>";
echo "<th width=20%>  $netitemprofit </th>";
   echo "</tr>";

  
echo "</table>";
    echo "<table border=10 width=100% align=center bgcolor=white>";

echo "<tr height=50>";
echo "<th width=80%  bgcolor=#0099FF>  Others Income Details   </th>";
echo "<th width=20%  bgcolor=#0099FF>  Amount   </th>";
 
   echo "</tr>";
   $sqlcoa1 = "select * from chartofaccounts where acc_type='Income' and user_id='$userid'";
//$sql = "select * from chartofaccounts";
  $resultcoa1=$conn->query($sqlcoa1);
                                       while($rowcoa1 = $resultcoa1->fetch_assoc())

  {
 $acc_name1= $rowcoa1["acc_name"];
 $net_bal1=0;
 $amounts1 =0;
  $total_cr_amount1=0;

$sql1 = "select * from expense_voucher where date between '$from_date' and '$to_date' and acc_name_cr='$acc_name1' and mtype='Income' and user_id='$userid'";
   $result1=$conn->query($sql1);
                                       while($row1 = $result1->fetch_assoc())
 
  {
 $amounts1=$amounts1 + $row1["amount"];
  }
  echo "<tr>";
echo "<td width=80%>" . $acc_name1 .   "</td>";
echo "<td width=20%>" . $amounts1 .   "</td>";
 
   echo "</tr>";
  
   $netexp1=$netexp1 + $amounts1;
  }
  $grossprofit=$netexp1 + $netincome;
  echo "<tr height=50>";
echo "<th width=80%>  Total Other Income   </th>";
echo "<th width=20%> $netexp1  </th>";
 
   echo "</tr>";
    echo "<tr height=50>";
echo "<th width=80%><right> Gross Profit </right>  </th>";
echo "<th width=20%> $grossprofit  </th>";
 
   echo "</tr>";
   echo "</table>";

   
   
   
 
 echo "<table border=10 width=100% align=center bgcolor=white>";

echo "<tr height=50>";
echo "<th width=80%  bgcolor=#0099FF> Expense Details   </th>";
echo "<th width=20%  bgcolor=#0099FF>  Amount   </th>";
 
   echo "</tr>";
$sqlcoa = "select * from chartofaccounts where acc_type='Expense' and user_id='$userid'";
//$sql = "select * from chartofaccounts";
  $resultcoa=$conn->query($sqlcoa);
                                       while($rowcoa = $resultcoa->fetch_assoc())

  {
 $acc_name= $rowcoa["acc_name"];
 $net_bal=0;
 $amounts =0;
  $total_cr_amount=0;

$sql = "select * from expense_voucher where date between '$from_date' and '$to_date' and acc_name_dr='$acc_name' and mtype='Expense' and user_id='$userid'";
   $result=$conn->query($sql);
                                       while($row = $result->fetch_assoc())
 
  {
 $amounts=$amounts + $row["amount"];
  }
  echo "<tr>";
echo "<td width=80%>" . $acc_name .   "</td>";
echo "<td width=20%>" . $amounts .   "</td>";
 
   echo "</tr>";
  
   $netexp=$netexp + $amounts;
  }
  echo "<tr height=50>";
echo "<th width=80%>  Net Total Expeses   </th>";
echo "<th width=20%> $netexp  </th>";
 
   echo "</tr>";
   echo "</table>";

$profit=$grossprofit - $netexp;


echo "<table border=10 width=100% align=center >";
  echo "<tr height=60>";
echo "<th width=80%  bgcolor=#0099FF> <right> Net Income   </right></th>";
echo "<th width=20%  bgcolor=#0099FF> $profit  </th>";
 
   echo "</tr>";
echo "</table>";
?> 
                                       
                                 
                          
</body>

</html>
<?php
}
?>