

 <!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");
		?>
    <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">Profit Analysis</div>
                            </div>
                            <div class="block-content collapse in">
							
                                <div class="span12">
<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>
<form method="POST" action="">
  <?php
                        if( $permissions['filter_profit_analysis'] == 1){
                            ?>
   <div class="span12">
                                           
        <div class="span4">
            <label>Date from</label>
            <input type="date" name="datefrom" id="dfrom" required="required">
        </div>
        <div class="span4">
            <label>Date To</label>
            <input type="date" name="dateto"  id="dto" required="required">
        </div>
            <div class="span4">
              <label>&nbsp &nbsp &nbsp &nbsp</label>
           <button class="btn btn-info" type="submit" name="filter"><li class="icon-filter icon-white"></li>filter</button>
        </div>                                 
      </div>
      <?php
                         }
                         ?>
</form>
                                 
    <div class="table-toolbar">
                                  
                                      

                                   
                                   <?php
include '../includes/connection.php';
if (isset($_POST['filter'])) {
  $dfrom=$_POST['datefrom'];
  $dto=$_POST['dateto'];
  //if( $_SESSION['role']=="Super Admin"){
  if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
  $sql = mysqli_query($con,"SELECT DATE_FORMAT(date_time, '%Y-%m-%d') as date_time,(select product_name from products where id =sales_details.product_id)as product,quantity,buying_price,selling_price,(quantity*selling_price) as tsell,(quantity*buying_price) as tbuy, ((quantity*selling_price)-(quantity*buying_price)) as profit FROM sales_details where  DATE_FORMAT(date_time, '%Y-%m-%d')>='$dfrom' &&  DATE_FORMAT(date_time, '%Y-%m-%d')<='$dto' && business_id='$business'  ");

                 $sqlp = mysqli_fetch_array(mysqli_query($con,"
SELECT sum(((quantity*selling_price)-(quantity*buying_price))) as tprofit FROM sales_details where  DATE_FORMAT(date_time, '%Y-%m-%d')>='$dfrom' &&  DATE_FORMAT(date_time, '%Y-%m-%d')<='$dto' && business_id='$business' "));  
 }else{
   $sql = mysqli_query($con,"SELECT DATE_FORMAT(date_time, '%Y-%m-%d') as date_time,(select product_name from products where id =sales_details.product_id)as product,quantity,buying_price,selling_price,(quantity*selling_price) as tsell,(quantity*buying_price) as tbuy, ((quantity*selling_price)-(quantity*buying_price)) as profit FROM sales_details where  DATE_FORMAT(date_time, '%Y-%m-%d')>='$dfrom' &&  DATE_FORMAT(date_time, '%Y-%m-%d')<='$dto' && branch_id='$branch'  ");

                 $sqlp = mysqli_fetch_array(mysqli_query($con,"
SELECT sum(((quantity*selling_price)-(quantity*buying_price))) as tprofit FROM sales_details where  DATE_FORMAT(date_time, '%Y-%m-%d')>='$dfrom' &&  DATE_FORMAT(date_time, '%Y-%m-%d')<='$dto' && branch_id='$branch' ")); 
 }
$preriod="FROM ".$dfrom." TO ".$dto;
}else{
  //if( $_SESSION['role']=="Super Admin"){
  if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
  $sql = mysqli_query($con,"SELECT DATE_FORMAT(date_time, '%Y-%m-%d') as date_time,(select product_name from products where id =sales_details.product_id)as product,quantity,buying_price,selling_price,(quantity*selling_price) as tsell,(quantity*buying_price) as tbuy, ((quantity*selling_price)-(quantity*buying_price)) as profit FROM sales_details where business_id='$business' ");


  $sqlp = mysqli_fetch_array(mysqli_query($con,"
SELECT sum(((quantity*selling_price)-(quantity*buying_price))) as tprofit FROM sales_details where business_id='$business'")); 
$preriod="IN ".date('Y');  
}else{
   
  $sql = mysqli_query($con,"SELECT DATE_FORMAT(date_time, '%Y-%m-%d') as date_time,(select product_name from products where id =sales_details.product_id)as product,quantity,buying_price,selling_price,(quantity*selling_price) as tsell,(quantity*buying_price) as tbuy, ((quantity*selling_price)-(quantity*buying_price)) as profit FROM sales_details where branch_id='$branch' ");


  $sqlp = mysqli_fetch_array(mysqli_query($con,"
SELECT sum(((quantity*selling_price)-(quantity*buying_price))) as tprofit FROM sales_details where branch_id='$branch'   ")); 
$preriod="IN ".date('Y');  
}


                                   // Create connection

//$result = mysqli_query($con, $sql);

if (mysqli_num_rows($sql) > 0) {
    // output data of each row
    


                                   ?>
        </div>
                                    <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  id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" 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 colspan="9"><h5 style="text-align: center;">PROFITS MADE <?php echo $preriod;?></h5></th>
                                          </tr>
                                            <tr>
                                                <th>SN</th>
                                                <th>date</th>
                                                <th>Product Name</th>
                                               
                                                <th>Quantity</th>
                                                <th>Buying Price</th>
                                                <th>Selling Price</th>
                                                <th>Total Selling Price</th>
                                                <th>Total buying Price</th>
                                                <th>Profit</th>
                                                

                                            </tr>
                                        </thead>
                                        <tbody>
                                        
                                        <?php
                                        $ns=0;
while($row = mysqli_fetch_assoc($sql)) {
	$ns++;
        ?>
<tr class="gradeU">
                                                <td><?php echo $ns; ?></td>
                                              
                                                
                                                <td><?php echo $row['date_time']; ?></td>
                                                <td><?php echo $row['product']; ?></td>
                                                <td><?php echo $row['quantity']; ?></td>
                                               
                                                <td><?php echo $row['buying_price']; ?></td>
                                                <td class="center"><?php echo round($row['selling_price'],3); ?></td>
                                                
                                                <td class="center"><?php echo round($row['tsell'],3); ?></td>
                                                <td class="center"><?php echo round($row['tbuy'],0); ?></td>
                                                <td class="center"><?php echo round($row['profit'],0); ?></td>
                                               
                                               
                                            </tr>
                                            
                                            <?php


    }
} else {
    echo "0 results";
}
}




                                        ?>
                                        
<tr ><td colspan="8"><h4>Total Profits</h4></td><td><h4><?php echo 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>
