<?php
session_start();
error_reporting(0);
@ini_set('display_errors', 0);
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
?>

<!DOCTYPE html>
 <?php include '../includes/connection.php';
   include("../includes/header-links.php");
  ?>
<html>
    
    <head>
       <?php 
$permissions = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM  
  permissions,users WHERE  permissions.role_id = users.role 
  AND users.id='$id_user' AND users.business_id ='$business' AND users.branch_id ='$branch'"));

if($permissions['view_payment_report'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
       
        <!-- Bootstrap -->
        <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="../bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="../assets/styles.css" rel="stylesheet" media="screen">
        <link href="../assets/DT_bootstrap.css" rel="stylesheet" media="screen">
        <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        
        <script src="../vendors/modernizr-2.6.2-respond-1.1.0.min.js"></script>
    </head>
    
    <body>
        <?php include '../includes/header.php'; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span10" id="content">


                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Closing Stock</div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                
                                    
                                    <div class="datatable-dashv1-list custom-datatable-overright">
                                         <div class="span12">
                                        <?php 
                                         // if( $_SESSION['role']=="admin"){
                                         if( $permissions['filter_payment_report'] == 1){
                                         // if( $_SESSION['branch_id']!="0"){

                                          ?>
                                              
<!-- <script type="text/javascript">
    function filterPayment1(sup){
        document.getElementById("dto").disabled=false;
        document.getElementById("testing").innerHTML=sup;
        var dto=document.getElementById("dto").value;

        var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("filtered").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "stock_value_filters.php?dfrom="+sup+"&dto="+dto, true);
  xhttp.send();
    }
</script>
<script type="text/javascript">
    function filterPayment2(sup){
        document.getElementById("dto").disabled=false;
        document.getElementById("testing").innerHTML=sup;
        var dfrom=document.getElementById("dfrom").value;

        var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("filtered").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "stock_value_filters.php?dfrom="+dfrom+"&dto="+sup, true);
  xhttp.send();
    }
</script>  -->
                          <p id="testing"></p>
                                            <form method="POST">
                                            <div class="span4">
                                                <label>Date from</label>
                                                <input type="date" name="datefrom" id="dfrom" onchange="filterPayment1(this.value)" required="">
                                            </div>
                                            <div class="span4">
                                                <label>Date To</label>
                                                <input type="date" name="dateto" id="dto" onchange="filterPayment2(this.value)" required="">
                                            </div>
                                            <div class="span4">
                                                <label>&nbsp;<br></label>
                                                <input type="submit" name="submit" value="submit" class="btn btn-primary">
                                            </div>
                                          </form>
                                            
                                            <?php
                                         
                                      }
                                            ?>
            
                                           
                                        </div>
                                        
        <p id="text"></p>
                                        <div>&nbsp;</div>

                               <?php 
                        // if($_SESSION['role']=="Admin"){
                               
include '../includes/connection.php';
  //if( $_SESSION['role']=="Super Admin"){ 
  if($permissions['business_id'] != 0 && $permissions['branch_id']==0){   
                             
}else{
  if (isset($_POST['submit'])) {
    $datefrom=$_POST['datefrom'];
    $dateto=$_POST['dateto'];
    $tittle=" PROFIT STATEMENT FROM ".$datefrom."  TO ".$dateto;
    $stockdate=date('Y-m-d', strtotime($datefrom. ' - 1 day'));
    $sql=mysqli_fetch_array(mysqli_query($con,"SELECT  sum(total_price) as sales,
      (SELECT sum(total_price) from purchases where branch_id='$branch' && DATE_FORMAT(date_time, '%Y-%m-%d')>='$datefrom' && DATE_FORMAT(date_time, '%Y-%m-%d')<='$dateto') as purchases, 
      (SELECT stock_value FROM stock_values where DATE_FORMAT(date, '%Y-%m-%d')='$stockdate' && branch_id='$branch') as openingstock,
      (SELECT stock_value FROM stock_values where DATE_FORMAT(date, '%Y-%m-%d')='$dateto' && branch_id='$branch') as closingstock,
      (SELECT sum(amount) FROM expenses WHERE branch_id='$branch' && DATE_FORMAT(date, '%Y-%m-%d')>='$datefrom' && DATE_FORMAT(date, '%Y-%m-%d')<='$dateto') as expenses 
      from sales where branch_id='$branch' && DATE_FORMAT(date_time, '%Y-%m-%d')>='$datefrom' && DATE_FORMAT(date_time, '%Y-%m-%d')<='$dateto'"));
      $openingstock=$sql['openingstock'];
      if(DATE_FORMAT($dateto, '%Y-%m-%d')==DATE_FORMAT(date('Y-m-d'), '%Y-%m-%d')){
         $stkclose=mysqli_fetch_array(mysqli_query($con,"SELECT sum(buying_price*quantity) stock_value FROM stock WHERE branch_id='$branch'"));
      }else{
        $stkclose=mysqli_fetch_array(mysqli_query($con,"SELECT stock_value FROM stock_values where DATE_FORMAT(date, '%Y-%m-%d')='$dateto' && branch_id='$branch'"));
      }
      $closingstock=$stkclose['stock_value'];

  }else{
    $tittle=" PROFIT STATEMENT";

    $sql=mysqli_fetch_array(mysqli_query($con,"SELECT  sum(total_price) as sales,(SELECT sum(total_price) from purchases where branch_id='$branch') as purchases, (SELECT sum(buying_price*quantity) FROM stock WHERE branch_id='$branch') as closingstock,(SELECT sum(amount) FROM expenses WHERE branch_id='$branch') as expenses from sales where   branch_id='$branch'"));
      $closingstock=$sql['closingstock'];
       $openingstock=0;
      
  }
}
$sales=$sql['sales'];
$purchases=$sql['purchases'];
$costofsales=($openingstock+$purchases)-$closingstock;
$grossprofit=$sales-$costofsales;
$expenses=$sql['expenses'];
$operatingprofit=$grossprofit-$expenses;
$vat=(30*$operatingprofit)/100;
$net=$operatingprofit-$vat;

                        
                            ?>
                            <h3 style="text-align: center; color: #08C;" ><?php echo $tittle; ?></h3>
                                    
                                    <table  id="table" data-toggle="table" data-pagination="false" data-search="true" data-show-columns="true" data-show-pagination-switch="false" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true"
                                        data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar" data-export-options='{"fileName": "Testing_filename"}'>

                                        <thead>
                                            <tr>
                                                    <th  width="10%">particular</th>
                                                
                                                    <th><h4 style="text-align: right; margin-right: 20px;">shs</h4></th>
                                                    <th><h4 style="text-align: right; margin-right: 20px;">shs</h4></th>
                                                    
                                                    
                                            </tr>
                                        </thead>
                                        <tbody id="filtered">
                                        
                                            <tr>
                                                <td>Sales</td>
                                                <td></td>
                                                <td><p style="text-align: right; margin-right: 20px;"><strong><?php echo number_format($sales,3,'.',', '); ?></strong></p></td>
                                            </tr>
                                            <tr>
                                              <td>Less Cost of Sales</td>
                                                <td></td>
                                              <td></td>
                                            </tr>
                                            <tr>
                                              <td>Purchases</td>
                                              <td><p style="text-align: right; margin-right: 20px;"><?php echo number_format($purchases,3,'.',', '); ?></p></td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                              <td>Add Opening Stock</td>
                                              <td><p style="text-align: right; margin-right: 20px;"><?php echo number_format($openingstock,3,'.',', '); ?></p></td>
                                                <td></td>
                                            </tr>
                                            </tr>
                                            <tr>
                                              <td>Less Closing Stock</td>
                                              <td><p style="text-align: right; margin-right: 20px;">(<?php echo number_format($closingstock,3,'.',', '); ?>)</p></td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                              <td> Cost of Sales</td>
                                                <td></td>
                                              <td><p style="text-align: right; margin-right: 20px;">(<?php echo number_format($costofsales,3,'.',', '); ?>)</p></td>
                                            </tr>
                                            <tr>
                                              <td>Gross profit</td>
                                                <td></td>
                                              <td><strong><p style="text-align: right; margin-right: 20px;"><?php echo number_format($grossprofit,3,'.',', '); ?></p></strong></td>
                                            </tr>
                                            <tr>
                                              <td>Less Operating Expenses</td>
                                                <td></td>
                                              <td><p style="text-align: right; margin-right: 20px;">(<?php echo number_format($expenses,3,'.',', '); ?>)</p></td>
                                            </tr>
                                            <tr>
                                              <td>Operating Profit</td>
                                                <td></td>
                                              <td><p style="text-align: right; margin-right: 20px;"><?php echo number_format($operatingprofit,3,'.',', '); ?></p></td>
                                            </tr>
                                            <tr>
                                              <td>Less VAT (30 %)</td>
                                                <td></td>
                                              <td><p style="text-align: right; margin-right: 20px;"><?php echo number_format($vat,3,'.',', '); ?></p></td>
                                            </tr>
                                            <tr>
                                              <td>Net profit/loss</td>
                                                <td></td>
                                              <td><strong><p style="text-align: right; margin-right: 20px;"><?php echo number_format($net,3,'.',', '); ?></strong></p></td>
                                            </tr>
                                            
                                        </tbody>
                                    </table>
                                </div>
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->

        <script src="../vendors/jquery-1.9.1.js"></script>
        <script src="../bootstrap/js/bootstrap.min.js"></script>
        <script src="../vendors/datatables/js/jquery.dataTables.min.js"></script>
        <script src="../data-table-js/bootstrap-table.js"></script>
    <script src="../data-table-js/tableExport.js"></script>
    <script src="../data-table-js/data-table-active.js"></script>
    <script src="../data-table-js/bootstrap-table-editable.js"></script>
    <script src="../data-table-js/bootstrap-editable.js"></script>
    <script src="../data-table-js/bootstrap-table-resizable.js"></script>
    <script src="../data-table-js/colResizable-1.5.source.js"></script>
    <script src="../data-table-js/bootstrap-table-export.js"></script>


        <script src="../assets/scripts.js"></script>
        <script src="../assets/DT_bootstrap.js"></script>
        <script>
        $(function() {
            
        });
        </script>
    </body>

</html>