<?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>
<html>
    <script type="text/javascript">
    function FormatCurrency(ctrl) {
                  //Check if arrow keys are pressed - we want to allow navigation around textbox using arrow keys
                  if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) {
                      return;
                  }

                  var val = ctrl.value;

                  val = val.replace(/,/g, "")
                  ctrl.value = "";
                  val += '';
                  x = val.split('.');
                  x1 = x[0];
                  x2 = x.length > 1 ? '.' + x[1] : '';

                  var rgx = /(\d+)(\d{3})/;

                  while (rgx.test(x1)) {
                      x1 = x1.replace(rgx, '$1' + ',' + '$2');
                  }

                  ctrl.value = x1 + x2;
              }

              function CheckNumeric() {
                  return event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode == 46;
              }
</script>
    <head>

    <?php
    include("../includes/header-links.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['sales_by_table'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
   
    <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        
    </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">Sales Analysis</div>
                            </div>
                            <div class="block-content collapse in">
              
                                <div class="span12">
                                <form method="POST" action="">
                                    <label>Year</label>
                                            <input type="year" value="<?php echo date('Y');?>"  name="year" class="form-control"   required="required" />
                                            
                                            <button type="submit" name="filter" class="btn btn-info"><li class="fa fa-filter">filter</li></button>
                                </form>
<div class="row">
  <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">

  </div>
  <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">

  </div>     
</div>
                                   <div class="table-toolbar">
                                   
                                      

                               </div>     
                                   <?php
  if (isset($_POST['filter'])) {
$mdate=$_POST['year'];
    
}else{
$mdate=date("Y");

}
include '../includes/connection.php';
      //if( $_SESSION['role']=="Super Admin"){ 
  if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
    $sql = mysqli_query($con,"
SELECT distinct DATE_FORMAT(date_time, '%m') as month FROM sales where business_id='$business' order by DATE_FORMAT(date_time, '%m') ");
  }else{
     $sql = mysqli_query($con,"
SELECT distinct DATE_FORMAT(date_time, '%m') as month FROM sales where branch_id='$branch' order by DATE_FORMAT(date_time, '%m') ");
  }


if (mysqli_num_rows($sql) > 0) {
 
    


                                   ?>
       
                                    <div class="datatable-dashv1-list custom-datatable-overright">
                                    <div id="toolbar">
                                        <select class="form-control">
                                                <option value="">Export Basic</option>
                                                <option value="all">Export All</option>
                                                <option value="selected">Export Selected</option>
                                            </select>
                                    </div>
                                    <table class="table-bordered" id="table" data-toggle="table" data-pagination="false" data-search="false" 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>SN</th>
                                                <th>month</th>
                                                <th>date</th>
                                                <th>Total Sales </th>
                                                <th>Expenses  </th>
                                                <th>Balance  </th>
                                                   <?php
                                        // if( $permissions['sales_analysis_details'] == 1){
                                            ?>
                                                <!-- <th> Details </th> -->
                                             
                                                
                                                <?php
                                                // }
                                             ?>
                                                
                                                

                                            </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
                                        $ns=0;
while($row = mysqli_fetch_assoc($sql)) {
  $ns++;
  $mm=$row['month'];
  // $mdate=date("Y");
  $dateObj = DateTime::createFromFormat('!m', $mm); 
     
    $mmname = $dateObj->format('F'); 
 //if( $_SESSION['role']=="Super Admin"){ 
    if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
  $sql1=mysqli_query($con,"
SELECT distinct(DATE_FORMAT(date_time, '%Y-%m-%d')) as date_time from sales where DATE_FORMAT(date_time, '%Y')='$mdate' && DATE_FORMAT(date_time, '%m')='$mm' && business_id='$business'");
 }else{
$sql1=mysqli_query($con,"
SELECT distinct(DATE_FORMAT(date_time, '%Y-%m-%d')) as date_time from sales where DATE_FORMAT(date_time, '%Y')='$mdate' && DATE_FORMAT(date_time, '%m')='$mm' && branch_id='$branch'");
 }
  
  $rowspan=mysqli_num_rows($sql1);
  $nr=$rowspan+1;
        ?>
           <tr>
               <td rowspan="<?php echo $nr?>"><?php echo $ns;?></td>
                                              
                                                
                <td rowspan="<?php echo $nr;?>"> <?php echo$mmname;?></td>
               
                <?php
                while ($row2=mysqli_fetch_array($sql1)) {
                  $date=$row2['date_time'];
                  //if( $_SESSION['role']=="Super Admin"){
                  if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
                     $sale=mysqli_fetch_array(mysqli_query($con,"SELECT id,  sum(quantity*selling_price) as total from sales_details where DATE_FORMAT(date_time, '%Y')='$mdate' && DATE_FORMAT(date_time,'%Y-%m-%d')='$date' && business_id='$business'"));
                     $expense=mysqli_fetch_array(mysqli_query($con,"SELECT sum(Amount) as spent from expenses where date='$date' && business_id='$business'"));

                  }else{
                    $sale=mysqli_fetch_array(mysqli_query($con,"SELECT id,  sum(quantity*selling_price) as total from sales_details where DATE_FORMAT(date_time, '%Y')='$mdate' && DATE_FORMAT(date_time,'%Y-%m-%d')='$date' && branch_id='$branch'"));
                     $expense=mysqli_fetch_array(mysqli_query($con,"SELECT sum(Amount) as spent from expenses where date='$date' && branch_id='$branch'"));
                     
                 
                 
                  }
                  
                 ?>
                 <tr>
                 <td><?php echo $row2['date_time'];?></td>
                 <td>
                 <?php echo $sale['total'];?>
                 </td>
                 <td><?php 
                 $exp=$expense['spent'];
                     if ($exp<1) {
                       echo "0";
                     }else{
                      echo $exp;
                     }
                 ?></td>
                 <td><?php echo $sale['total'] - $exp ;?> </td>

                   <?php
                        if( $permissions['sales_analysis_details'] == 1){
                            ?>
                 <!-- <td>
                 <a href="daily_sales.php?id=<?php echo $sale['id'];?>" ><button class="btn btn-primary"><i class="fa fa-tasks"></i>Detail</button>
                 </a>
                 </td> -->
                 <?php
                }
                ?>


               </tr>
                 <?php
                }
                ?>
             
               
            </tr>
            <!-- <?php
            $mdatet=date("Y");
            if( $_SESSION['branch_id']=="0"){ 
            $salet=mysqli_fetch_array(mysqli_query($con,"SELECT sum(quantity*selling_price) as stotal from sales_details where DATE_FORMAT(date_time,'%m')='$mm' && DATE_FORMAT(date_time, '%Y')='$mdatet' && business_id='$business'"));
          }else{
            $salet=mysqli_fetch_array(mysqli_query($con,"SELECT sum(quantity*selling_price) as stotal from sales_details where DATE_FORMAT(date_time,'%m')='$mm' && DATE_FORMAT(date_time, '%Y')='$mdatet' && branch_id='$branch'"));
          }
            ?> -->
            <!-- <tr>
            
              <td colspan="3"><h5 style="text-align: center; ">Total sales for <?php echo $mmname." ".$mdatet;?> </h5></td>
             
              <td colspan="2"><h5><?php echo number_format($salet['stotal']);?></h5></td>
            </tr> -->
            
            <?php


    }
} else {
    echo "0 results";
}




                                        ?>
                                        <?php
                                         $mdatef=date("Y");
      //if( $_SESSION['role']=="Super Admin"){
                                         if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
$sqlp = mysqli_fetch_array(mysqli_query($con,"SELECT sum(quantity*selling_price) as tprofit FROM sales_details where DATE_FORMAT(date_time, '%Y')='$mdatef' && business_id='$business' "));  
}else{
$sqlp = mysqli_fetch_array(mysqli_query($con,"SELECT sum(quantity*selling_price) as tprofit FROM sales_details where DATE_FORMAT(date_time, '%Y')='$mdatef' && branch_id='$branch' "));  

} 
?>
<!-- <tr ><td colspan="3"><h4>Total Salest for <?php echo $mdatef ?></h4></td><td><h4><?php echo number_format(round($sqlp['tprofit'],0)); ?> /=</h4></td></tr>
 -->
                                        </tbody>
                                    </table>
                                  </div>
                                    
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->

        <!--==========Footer js======-->
    <?php require_once("../includes/footerjs.php");?>
        <!--==========Footer js======-->
       
 


<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>
    </body>
</html>
