<!DOCTYPE html>
 <?php include '../includes/connection.php'; 
 
 ?>
<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>
        <title>Credit Report</title>
        <!-- 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">
        <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <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="span9" id="content">


                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Credit</div>
                                <div class="muted pull-right">Filter By Date</div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                   <div class="table-toolbar">
                                      
                                      <div class="btn-group pull-right">
                                         <button data-toggle="dropdown" class="btn dropdown-toggle">Tools <span class="caret"></span></button>
                                         <ul class="dropdown-menu">
                                            <li><a href="#">Print</a></li>
                                            <li><a href="#">Save as PDF</a></li>
                                            <li><a href="#">Export to Excel</a></li>
                                         </ul>
                                      </div>
                                   </div>
                                    
                                    <table cellpadding="0" cellspacing="0" border="0" class="table  table-bordered" id="example2">
                                    <?php
include '../includes/connection.php';
                                   // Create connection


$sql= mysqli_query($con,"SELECT id, total_price, (select count(id) from sales_details where order_id=purchase_totals.id)as count_orders, AddedOn,(select  distinct supplier from purchases where purchase_id=purchase_totals.id) as supplier,paid from purchase_totals where total_price > paid order by id desc");
//$result = mysqli_query($con, $sql);

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


                                   ?>
                                        <thead>
                                            <tr>
                                                <th  width="10%">S/NO</th>
                                                
                                                  <th width="50%">
                                                    <table background="white" class="table table-bordered">
                                                    <th width="40%">Product</th>
                                                    <th width="20%">Quantity</th>
                                                    <th width="20%">Unit Price</th>
                                                    <th width="20%">Total Price</th>
                                                    
                                                    
                                                </table>
                                              </th>
                                                <th  width="10%">Purchase Total</th>
                                                <th  width="10%">Credit Balance</th>
                                                <th width="20%">Date</th>
                                                <th width="20%">Supplier</th>
                                                <th width="10%">Action</th>
                                                
                                            </tr>
                                        </thead>
                                        <tbody>
                <?php
              $nid=0;
              while($row = mysqli_fetch_array($sql)) {
                $iddx=$row['id'];
                $nid++;
                        
                    ?>
                                            <tr>
                                                <td><?php echo $nid; ?></td>
                                                
                                                <td>
                                                    <?php include 'credit_details.php'; ?>
                                                </td>
                                                <td><?php echo $row['total_price']; ?></td>
                                                <td><?php echo $row['total_price'] -$row['paid']; ?></td>
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['supplier']; ?></td>
                                                <td><a href="#edit<?php echo $iddx; ?>" data-toggle="modal" ><button class="btn btn-success"><li class=" icon icon-check icon-white"></li>Pay</button>
                                                </a>
                                            </td>
                         <div id="edit<?php echo $iddx; ?>" class="modal hide">
                      <div class="modal-header">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3> Pay Credit</h3>
                      </div>
                      <div class="modal-body">
                        
                      </div>
                    </div>
                                                
                                            </tr>
               <?php                             
    }
} else {
    echo "0 results";
}

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

if(isset($_POST['pay'])){
  include '../includes/connection.php';
$sup=mysqli_real_escape_string($con,$_POST['supplier']);
$upid=mysqli_real_escape_string($con,$_POST['editid']);
$cbal=mysqli_real_escape_string($con,$_POST['bal']);
$ampaid=str_replace(",","",  mysqli_real_escape_string($con,$_POST['cpaid']));
if ($ampaid>$cbal) {
    echo "<script> alert('Amount paid(shs $ampaid) cannot be greater than the credit balance (shs &cbal)'),history.go(-1)</script>";
}else{
  

$sqlstm = mysqli_query($con,"update purchase_totals set paid=paid+$ampaid where id='$upid' ");


if($sqlstm) {
 $id_user=$_SESSION['id'];
$action="Paid   shs  $ampaid to  $sup";
 $log=mysqli_query($con,"INSERT INTO logs(user_id, activity) VALUES 
('$id_user','$action')");
 
 
 echo "<script>
    
    window.location='credit_voucher.php?voucher=<?php echo $voucher_no;?>';
    </script>";
 
} else {
    echo "Error ".mysqli_error($con);
}

  
}


}

?>
                                            
                                        </tbody>
                                    </table>
                                    <?php
                        $sqledit=mysqli_fetch_array(mysqli_query($con,"SELECT id, total_price,paid,(select product_name from products where id=(select product_id from purchases where purchase_id=purchase_totals.id)) as pdname,(select supplier from purchases where purchase_id=purchase_totals.id) as supplier from purchase_totals where id='$iddx'"));
                          $editid=$sqledit['id'];
                         $prod=$sqledit['pdname'];
                         $tprice=$sqledit['total_price'];
                          $ppaid=$sqledit['paid'];
                          $supp=$sqledit['supplier'];
                         $cbal= $tprice-$ppaid;
                             
                        ?>
                        <form action="" method="POST" enctype="multipart/form-data">
                        <input type="hidden" value="<?php echo $editid; ?>" name="editid">
                        <input type="hidden" value="<?php echo $cbal; ?>" name="bal">
                        <label>Supplier </label>
                        <div class="controls">
                          <input type="text" value="<?php echo  $supp; ?>" name="supplier" class="input-xlarge focused" readonly="readonly" style="width: 95%; height: 30px;">
                     </div>
                    
                        <label>Credit Amount</label>
                        <div class="controls">
                          <input type="number" value="<?php echo  $tprice-$ppaid;; ?>" name="bprice" 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 Paid </label>
                         <div class="controls">
                        <input type="text"  onkeypress="return CheckNumeric()" onkeyup="FormatCurrency(this)" value="<?php echo $cbal ?>" name="cpaid" class=" input-xlarge focused " max="<?php echo $cbal;  ?>" style="width: 95%; height: 30px;">
                      </div>
                  </div>
                        <div class="form-actions">
                                          <button type="submit" name="pay" class="btn btn-primary">Pay </button>
                                          
                                        </div>  
                        </form>
                                </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="../assets/scripts.js"></script>
        <script src="../assets/DT_bootstrap.js"></script>
        <script>
        $(function() {
            
        });
        </script>
    </body>

</html>