<!DOCTYPE html>
<?php 
 ?>
<html>
    
    <head>
        <?php include("../includes/header-links.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">Sales Reports</div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                   <div class="table-toolbar">
                           
                                        
                                   </div>
                                    
                                    <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>S/NO</th>
                                                
                                                <?php if( $business_type==1){
                                    ?>
                                                <th>Sale Type</th>
                                                <?php
                                              }
                                                ?>
                                                    <th width="50%">
                                                    <table background="white" class="table table-bordered">
                                                    <td width="40%">Product</td>
                                                    <td width="20%">Quantity</td>
                                                    <td width="20%">Unit Price</td>
                                                    <td width="20%">Total Price</td>
                                                    
                                                    
                                                </table></th>
                                                <th  width="10%">Staff</th>
                                                <th  width="10%">customer</th>
                                                <th  width="10%">Total</th>
                                                <th  width="10%">Paid</th>
                                                <th  width="10%">Balance</th>
                                                <th width="20%">Date</th>
                                                <th width="10%">Action</th>
                                                
                                            </tr>
                                        </thead>
                                        <tbody id="fulldata">
                                        <?php
include '../includes/connection.php';
 
$sql= mysqli_query($con,"SELECT id, total_price,paid,changebal,saletype,customer_name, (select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id))as usernames from sales where branch_id='$branch' && changebal>0 order by id desc ");
                      // Create connection

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

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


   

$totalprice=0;

                                        $cou=1;
              while($row = mysqli_fetch_assoc($sql)) {
        $dpprice=$row['changebal'];
                 
                $iddx=$row['id'];
                       
                       $totalprice=$totalprice+$dpprice;
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $cou; ?></td>
                                    
                                    <?php if( $business_type==1){
                                      $saletype=$row['saletype'];
                                      if ($saletype==1) {
                                       $sst="Wholesale";
                                      }else{
                                        $sst="Retail";
                                      }
                                    ?>
                                                <td><?php echo $sst; ?></td>
                                                <?php
                                              }
                                                ?>
                                                <td>
                                                    <?php include 'sales_details.php'; ?>
                                                </td>
                                                <td><?php echo $row['staffname']; ?></td>
                                                <td><?php echo $row['usernames']; ?></td>
                                                <td><?php echo $row['total_price']; ?></td>
                                                <td><?php echo $row['paid']+$row['changebal']; ?></td>
                                                <td><?php echo $row['changebal']; ?></td>
                                                <td><?php echo $row['date_time']; ?></td>
                                               <td><a href="#edit<?php echo $row['id']; ?>" data-toggle="modal" ><button class="btn btn-success"><li class=" icon icon-check icon-white"></li>Payout</button>
                                                </a>
                                            </td>
                         <div id="edit<?php echo $row['id']; ?>" class="modal hide">
                      <div class="modal-header">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3> Give Out Balance</h3>
                      </div>
                      <div class="modal-body">
                        
                        <form action="" method="POST" enctype="multipart/form-data">
                        <input type="hidden" value="<?php echo $iddx; ?>" name="editid">
                        <label>Balance</label>
                        <div class="controls">
                          <input type="number" value="<?php echo  $row['changebal'] ?>" name="qtyremain" class="input-xlarge focused" readonly="readonly" style="width: 95%; height: 30px;">
                                          </div>
                    
                         <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        
                        <label>Amount Given Out </label>
                         <div class="controls">
                        <input type="number"   value="<?php echo $row['changebal'] ?>" name="amount" class=" input-xlarge focused " max="<?php echo $row['changebal'] ?>" style="width: 95%; height: 30px;">
                      </div>
                  </div>
                        <div class="form-actions">
                                          <button type="submit" name="pay" class="btn btn-primary">Sumbit </button>
                                          
                                        </div>  
                        </form>
                      </div>
                    </div>
                                                
                                                
                                            </tr>
               <?php 
               $cou++;                             
    }
    ?>
    
    <tr>
      <td <?php if( $_SESSION['role']=="Super Admin"){
                                    ?>
                                    colspan="5"
                                   <?php 
                                  }else{
                                    ?>
                   colspan="6"
                                    <?php } ?>
                                  
                                    ><h4>Total Balances</h4></td>
                                  
      <td colspan="3"><h4> <?php echo number_format(abs($totalprice));?></h4></td>
    </tr>
    <?php
} else {
    echo " ".mysqli_error($con);
}

//mysqli_close($conn);
?>
<?php

if(isset($_POST['pay'])){
  include '../includes/connection.php';
$amount=mysqli_real_escape_string($con,$_POST['amount']);
$editid=mysqli_real_escape_string($con,$_POST['editid']);


$sqlstm = mysqli_query($con,"UPDATE sales set changebal=changebal-$amount where id='$editid' ");


if($sqlstm) {

 echo "<script>
    
    window.location='customer_balances.php';
    </script>";
 
} else {
    echo "Error ".mysqli_error($con);
}

  
}




?>
                                            
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
             <script type = "text/javascript">
    function confirmationChange(anchor){
        var conf = confirm("Are you sure you want to Change the Sale?");
        if(conf){
            window.location = anchor.attr("href");
        }
    }    
    
</script>
            <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>