<?php session_start();
require_once ("connection_sql.php");
session_start(); 
$sql = "select * from  invpara where  COMCODE='".$_SESSION['company']."' "; 
$sql = $conn->query($sql);
if (!$row1 = $sql->fetch()) {
    exit('Network Error Please Login Again......!!!');
}
if($_SESSION["CURRENT_USER"]==""){ 
exit("Invalid Login");
}
if($_SESSION["company"]==""){
exit("Invalid Login");
}

?>
<?php date_default_timezone_set('Asia/Colombo'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Profit</title>
        <style>
            body{
                font-family:Arial, Helvetica, sans-serif;
                font-size:16px;
            }
            table
            {
                table table-bordered;
            }
            table, td, th
            {
                border:1px solid black;
                font-family:Arial, Helvetica, sans-serif;
                padding:5px;
            }
            th
            {
                font-weight:bold;
                font-size:15px;

            }
            td
            {
                font-size:15px; 
                border-left:none;
                border-right:none;


            }
        </style>
        <style type="text/css">
            <!--
            .style1 {
                color: #0000FF;
                font-weight: bold;
                font-size: 24px;
            }
            -->
        </style>
    </head>

    <body>
        <!-- Progress bar holder -->
        
        <?php
        
         require_once("connection_sql.php");
        $sinvpa = "select   * from invpara   ";
 $result_invpa = $conn->query($sinvpa);
    $row_invpa = $result_invpa->fetch(); 
          
            $sdep = "select   * from s_stomas where    CODE ='" . $_GET["dep"] . "'  ";
 $result_dep = $conn->query($sdep);
    $row_dep = $result_dep->fetch(); 
     if($_GET["dep"]!="ALL"){
         $dep=$row_dep["DESCRIPTION"];
     }else{
          $dep="ALL";
     }
        ?>

<table class="table table-bordered" > 
  <tr>
    
    <td ><strong><b>  <?php echo  $row_invpa["COMPANY"]   ?> </b> </strong></td>
    
  </tr>
  <tr>
    
    <td ><strong><b>  Profit Report  </b> </strong></td>
    
  </tr>
   <tr>
    
    <td width="220" align="left"><strong><b>  Department - <?php    echo  $dep;   ?></b> </strong></td>
     
  </tr>
  <tr>
    
    <td width="220" align="left"><strong><b>  <?php echo  $_GET["dtfrom"]   ?> To <?php echo  $_GET["dtto"]    ?></b> </strong></td>
      
  </tr>
  
  </table>
        <?php
        require_once("connection_sql.php");
 
        
     
          
  
    $sql2 = "select * from s_salma where CANCELL='0' and  SDATE>='" . $_GET["dtfrom"] . "' and SDATE<='" . $_GET["dtto"] . "' ";
    if($_GET["dep"]!="ALL"){
        $sql2 .= " and DEPARTMENT='".$_GET["dep"]."' ";
    }  
    echo "<table id='example'  border='1' class='table table-bordered' style='font-size: 14px;'>";
 
    echo "<thead><tr>";
    echo "<th>INVOICE NO</th>";
    echo "<th>CUSTOMER</th>";
    echo "<th>DATE</th>"; 
     echo "<th>AMOUNT</th>"; 
     echo "<th>AMOUNT</th>"; 

    echo "</tr></thead><tbody>";

    foreach ($conn->query($sql2) as $row) {  
        echo "<tr>               
        <td  >" . $row['REF_NO'] . " </td>
        <td >" . $row['CUS_NAME'] . " </td>
        <td >" . $row['SDATE'] . " </td>
        <td >" . $row['GRAND_TOT'] . " </td>
        <td >  </td>
        </tr>";
    }
           
         
        //  =======================================
        
   $scbal = "select * from c_bal where CANCELL='0' and trn_type='grn' and SDATE>='" . $_GET["dtfrom"] . "' and SDATE<='" . $_GET["dtto"] . "'";
if($_GET["dep"]!="ALL"){
    $scbal .= " and DEP='".$_GET["dep"]."' ";
}     
     
 
    echo "<thead><tr>";
    echo "<th>GRN NO</th>";
    echo "<th>CUSTOMER</th>";
    echo "<th>DATE</th>"; 
     echo "<th>AMOUNT</th>";
     echo "<th>AMOUNT</th>"; 

    echo "</tr></thead><tbody>";

    foreach ($conn->query($scbal) as $row) {  
        echo "<tr>               
        <td  >" . $row['REFNO'] . " </td>
        <td >" . $row['CUSCODE'] . " </td>
        <td >" . $row['SDATE'] . " </td>
        <td >  </td>
        <td >" . $row['AMOUNT'] . " </td>
        
        </tr>";
    }
        
//   =================================


 $sexp = "select * from expense where  	  cancel='0'	 and sdate>='" . $_GET["dtfrom"] . "' and sdate<='" . $_GET["dtto"] . "'";
if($_GET["dep"]!="ALL"){
    $sexp .= " and depcode='".$_GET["dep"]."' ";
}    
     
 
    echo "<thead><tr>";
    echo "<th>EXPENSE NO</th>";
    echo "<th>NAME</th>";
    echo "<th>DATE</th>"; 
     echo "<th>AMOUNT</th>"; 
    echo "<th>AMOUNT</th>"; 
    echo "</tr></thead><tbody>";

    foreach ($conn->query($sexp) as $row) {  
        echo "<tr>               
        <td  >" . $row['refno'] . " </td>
        <td >" . $row['name'] . " </td>
        <td >" . $row['sdate'] . " </td>
        <td >  </td>
        <td >" . number_format($row['amount'],2)   . " </td>
        
        </tr>";
    }
         echo " </table> </div>";    
         
           

  
       
 
       
$ssalma = "select sum(GRAND_TOT)  as GRAND_TOT  from s_salma where CANCELL='0' and  SDATE>='" . $_GET["dtfrom"] . "' and SDATE<='" . $_GET["dtto"] . "'";
if($_GET["dep"]!="ALL"){
    $ssalma .= " and DEPARTMENT='".$_GET["dep"]."' ";
} 

 $result_sal = $conn->query($ssalma);
  $row_sal = $result_sal->fetch(); 

$ssalma1 = "select   sum(COST)  as costtot from s_trn where   SDATE>='" . $_GET["dtfrom"] . "' and SDATE<='" . $_GET["dtto"] . "' and LEDINDI='INV'";
if($_GET["dep"]!="ALL"){
    $ssalma1 .= " and DEPARTMENT='".$_GET["dep"]."' ";
} 
 $result_sal1 = $conn->query($ssalma1);
    $row_sal1 = $result_sal1->fetch(); 
     

$ssalma2 = "select   sum(COST)  as costtot from s_trn where   SDATE>='" . $_GET["dtfrom"] . "' and SDATE<='" . $_GET["dtto"] . "' and LEDINDI='GRN'";
if($_GET["dep"]!="ALL"){
    $ssalma2 .= " and DEPARTMENT='".$_GET["dep"]."' ";
}
 $result_sal2 = $conn->query($ssalma2);
    $row_sal2 = $result_sal2->fetch(); 
     

$scbal = "select sum(AMOUNT)  as GRAND_TOT from c_bal where CANCELL='0' and trn_type='grn' and SDATE>='" . $_GET["dtfrom"] . "' and SDATE<='" . $_GET["dtto"] . "'";
if($_GET["dep"]!="ALL"){
    $scbal .= " and DEP='".$_GET["dep"]."' ";
}
  $result_cbal = $conn->query($scbal);
    $row_cbal = $result_cbal->fetch(); 

$sexp = "select sum(amount) as GRAND_TOT from expense where  	  cancel='0'	 and sdate>='" . $_GET["dtfrom"] . "' and sdate<='" . $_GET["dtto"] . "'";
if($_GET["dep"]!="ALL"){
    $sexp .= " and depcode='".$_GET["dep"]."' ";
}
 $result_exp = $conn->query($sexp);
    $row_exp = $result_exp->fetch();  
?>
<table class="table table-bordered" border="0" > 
  
  <tr>
    
    <td width="20" align="left"><strong>Total Sales</strong></td>
    <td width="20" align="left"><strong>:</strong></td>
    <td width="20" align="right"><strong><?php echo number_format($row_sal['GRAND_TOT'],2) ;?></strong></td>
  </tr>
   <tr>
    
    <td width="20" align="left"><strong>Total Return</strong></td>
    <td width="20" align="left"><strong>:</strong></td>
    <td width="20" align="right"><strong><?php echo  number_format($row_cbal['GRAND_TOT'],2);?></strong></td>
  </tr>
  <tr>
    
    <td width="20" align="left"><strong>Total Expenses</strong></td>
    <td width="20" align="left"><strong>:</strong></td>
    <td width="20" align="right"><strong><?php echo number_format($row_exp['GRAND_TOT'],2);?></strong></td>
  </tr>
  <tr>
    
    <td width="20" align="left"><strong>Total Cost</strong></td>
    <td width="20" align="left"><strong>:</strong></td>
    <td width="20" align="right"><strong><?php echo number_format($row_sal1['costtot']-$row_sal2['costtot'],2);?></strong></td>
  </tr>
  <tr>
    
    <td width="20" align="left"><strong>Total Profit</strong></td>
    <td width="20" align="left"><strong>:</strong></td>
    <td width="20" align="right"><strong><?php echo number_format($row_sal['GRAND_TOT']-$row_cbal['GRAND_TOT']-$row_exp['GRAND_TOT']-($row_sal1['costtot']-$row_sal2['costtot']),2);?></strong></td>
  </tr>
  </table>
  

           
    </body>
</html>
