<?php ob_start(); session_start();  if(!isset($_SESSION['sales'])){ header('location:login.php'); die();}
include 'dbconn.php';
include 'inc/functions.php';
// $results=get_user();
include 'xyz.php';


   ?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>MyOutlet App</title>
   <?php  include 'inc/toplinks.php';  ?>
</head>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

    

            <?php  include 'inc/menus.php';  ?>


        <!-- End of Sidebar -->

        <?php include 'inc/top.php';   ?>
                <!-- End of Topbar -->

                <!-- Begin Page Content -->
                <div class="container-fluid">

                    <!-- Page Heading -->
                   <?php  include 'inc/greet.php';  ?><br>
                   
                    <!-- DataTales Example -->
                    <div class="card shadow mb-4">
                          <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between bg-dark text-white">
                                    <h6 class="m-0 font-weight-bold text-white">#PAYMENT MANAGEMENT</h6>
                                    <div class="dropdown no-arrow">
<button data-toggle="modal" data-target="#filterPay" class="btn btn-warning btn-sm"><i class="fa fa-filter text-dark"> Filter</i></button>&nbsp; &nbsp; <button id="export" class="btn btn-secondary btn-sm"><i class="fa fa-file text-white"> Export</i></button>


                                        
                                    </div>
                                </div>
<div class="card-body"><div class="col-md-12" style="box-shadow:10px 10px 40px gray;">
<?php  include 'process.php';  ?>
<?php 
if(isset($_POST['search'])){ 
$debt_status =1;
extract($_POST);

if(empty($list_month)){$month='';}else{$month='and month='.$_POST['list_month'];}
if(empty($pchs_type)){$pchs_type='';}else{$pchs_type='and type='.$_POST['pchs_type'];}
if(empty($biller)){$biller='';}else{$biller='and onboarded="'.$_POST['biller'].'"';}
if(empty($date_paid)){$date_paid='';}else{
    $date_paid='and date_paid="'.$_POST['date_paid'].'"';}
if(empty($yr)){$yr='';}else{$yr='and year='.$_POST['yr'];}

$query = ("SELECT * FROM payment where id!='' $yr $pchs_type $month $biller $date_paid and pay_status=1 and branchcode='$my_branch_code'   ");

$store_receipt_id = array();
foreach(QueryDB("SELECT * FROM payment where id!='' $yr $pchs_type  $biller  and pay_status=0 and branchcode='$my_branch_code'   ")as $lows){
$store_receipt_id[] =$lows['receipt_id'];
}


$totalPay=QueryDB("SELECT SUM(amt_paid) as tPay FROM payment where id!='' $yr $pchs_type $month $biller $date_paid and pay_status=1 and branchcode='$my_branch_code'   ");
$row = $totalPay->fetch(PDO::FETCH_ASSOC);$endPay =  $row['tPay'];


$msg ='MONTH: <b class="text-danger">'.monthName($_POST['list_month']).'</b> YEAR: <b class="text-danger">'.$_POST['yr'].'</b> Customer Type: <b class="text-danger">'.@get_cus_type($_POST['pchs_type']).'</b> Billed By: <b class="text-danger">'.@get_staff_name($_POST['biller']).'</b> DATE: <b class="text-danger">'.$_POST['date_paid'].'</b>';

}else{
$query = ("SELECT * FROM payment  where  pay_status=1 and branchcode='$my_branch_code' ORDER BY date_paid DESC  ");
$totalPay=QueryDB("SELECT SUM(amt_paid) as tPay FROM payment where pay_status=1 and branchcode='$my_branch_code' ");
$row = $totalPay->fetch(PDO::FETCH_ASSOC);
$endPay =  $row['tPay'];
     $msg ='General';
     $debt_status =1;

$store_receipt_id = array();
foreach(QueryDB("SELECT * FROM payment  where  pay_status=0 and branchcode='$my_branch_code' ORDER BY date_paid DESC  ")as $lows){
$store_receipt_id[] =$lows['receipt_id'];
}


}

    ?>
<table class="table table-bordered table2excel table-responsive" id="dataTable" width="100%" cellspacing="0">
    <h5 class="text-center text-dark"><?php  echo @$msg; ?></h5>
    
    <thead><tr class="small"><th>S/N</th><th>NAME</th><th>ITEM</th><th>SCOST</th><th>TCOST</th><th>TAMT</th><th>PAID</th><th>Pchs Type</th><th>DATE</th><th>BILLER</th><th>DEBT</th><th class="noExl">Print</th></tr></thead>
    
<?php  $count= 1; $getTotalPay = 0; $getTotalDebt=0;
foreach(QueryDB($query) as $rows){
     if ($rows['time_paid'] <= strtotime('-24 hours')) { }else{
echo '<tr class="small"><td>'.$count.'</td>
<td >'.get_cus_Name($rows['cus_id']).' </td><td>';
for($k=0;$k<count(explode(',',$rows['item']));$k++){ 
    $thisP = getProduct(explode(',',$rows['item'])[$k]);
echo $sized_by[$thisP['size']].'-'.getProductName(explode(',',$rows['item'])[$k]).','.$colored_by[$thisP['color']].'
 ('.explode(',',$rows['qty'])[$k].')<br/>';
}echo '</td><td>';for($k=0;$k<count(explode(',',$rows['qty']));$k++){
echo (explode(',',$rows['s_price'])[$k]).'<br/>';
}echo '</td><td>';
for($k=0;$k<count(explode(',',$rows['item']));$k++){
echo (explode(',',$rows['bulk_price'])[$k]).'<br/>';
}echo '</td>';


echo '<td>'.number_format($rows['amt'],2).'</td><td>'.number_format($rows['amt_paid'],2).' ('.pay_MTH($rows['pay_mth']).')</td><td>'.@get_cus_type($rows['type']).'</td><td>'.($rows['date_paid']).'</td><td>'.get_staff_name($rows['onboarded']).'</td><td>'.number_format($rows['out_standing'],2).'</td><td class="noExl"><a href="receeipts?no='.$rows['receipt_id'].'"><i class="fa fa-print"></i></a></td></tr>
'; $count++; $getTotalPay+=$rows['amt_paid'];

if(in_array($rows['receipt_id'],$store_receipt_id)){
    $getTotalDebt+=$rows['out_standing'];
}

  } }
?>
<tr><th></th><th></th><th></th><th></th><th>TOTAL PAYMT</th><th></th><th><?php echo number_format($getTotalPay,2);  ?></th><th></th><th></th><th>DEBT</th><th><?php echo number_format($getTotalDebt,2);  ?></th><th></th></tr>



</table>

<div class="modal fade" id="filterPay" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"><div class="modal-dialog modal-center modal-sm" role="document"><div class="modal-content"><div class="modal-header bg-secondary text-white"><h5 class="modal-title " id="exampleModalLabel">#Filter Payment</h5><button class="close" type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button></div><div class="modal-body">
<form action="" method="POST">
<div class="form-group col-md-12 col-12 mt-3"><select class="form-control" name="pchs_type"><option value="">-Pchs Type-</option>
<?php foreach(QueryDB("SELECT * FROM category where status =1 ") as $rows){
  echo '<option value='.$rows['id'].'>'.$rows['type'].'</option>'; } ?>
</select></div>
<div class="form-group col-md-12 col-12 mt-3"><select class="form-control" name="list_month"><option value="">-Select-</option><option value="1">January</option><option value="2">February</option><option value="3">March</option><option value="4">April</option><option value="5">May</option><option value="6">June</option><option value="7">July</option><option value="8">August</option><option value="9">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option></select></div>
<div class="form-group col-md-12 col-12 mt-3"><input type="text" placeholder="Year" name="yr" class="form-control" maxlength="4" minlength="4" onkeypress="return isNumberKey(event)"></div>
<div class="form-group col-md-12 col-12 mt-3"><input type="date" placeholder="date Paid" name="date_paid" class="form-control" ></div>
<div class="form-group col-md-12 col-12 mt-3"><select class="form-control" name="biller"><option value="">-Biller-</option><?php foreach(QueryDB("SELECT * FROM login ") as $rows){echo '<option value="'.$rows['username'].'">'.$rows['name'].'</option>'; } ?></select></div><div class="form-group col-md-12 col-12 mt-3"><input type="submit" value="Filter" name="search" class="form-control btn btn-primary"></div>
</form>

</div></div></div></div>


                            </div>
                        </div>
                    </div>

                </div>
                <!-- /.container-fluid -->

            </div>
            <!-- End of Main Content -->

           

<?php  include 'inc/foot.php'; include 'inc/bottom.php';   include 'inc/modals.php'; include 'js/foot_links.php';  ?>
<script src="js/jquery.table2excel.min.js"></script>
<script type="text/javascript">
     $(function() {
        var classVal = "payments for <?php echo date('Y-m-d');   ?>.xls";
      $("#export").on("click",function(){
        $(".table2excel").table2excel({
          exclude: ".noExl",
          name: "File Export",
          filename:  classVal,
          fileext: ".xls",
          exclude_img: true,
          exclude_links: true,
          exclude_inputs: true
        });
      });
    });
</script>
</body>

</html>