<? include "top.php"; ?>
<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); ?>
	<title>Bill List</title>
	<link rel="stylesheet" type="text/css" href="src/plugins/datatables/media/css/jquery.dataTables.css">
	<link rel="stylesheet" type="text/css" href="src/plugins/datatables/media/css/dataTables.bootstrap4.css">
	<link rel="stylesheet" type="text/css" href="src/plugins/datatables/media/css/responsive.dataTables.css">
	<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
</head>
<body>
	<?php include('include/header.php'); ?>
	<?php include('include/sidebar.php'); 

	$action=$_REQUEST['action'];
	$custac=$_POST['custac'];
	if($action=='delete'){
		if($pdelete==1){
		$bilid=$_REQUEST['cid'];
		
		$getb=f(q("SELECT * FROM tbl_trans WHERE trn_billid='$bilid'"));
		$custid=$getb['trn_ac'];
		$cid=$getb['trnid'];

		$getmaxbill=f(q("SELECT MAX(trnid) as maxid FROM tbl_trans WHERE trn_ac='$custid'"));
		$maxid=$getmaxbill['maxid'];
		echo "CID".$cid;

		if($maxid==$cid){

		$payamt=$getb['trn_amt'];
		$paybill=$getb['trn_billid'];
		
		
			
		$res=mysqli_query($con,"DELETE FROM tbl_trans WHERE trnid='$cid'");
		if($res){
				$updatebillpay=mysqli_query($con,"DELETE FROM tbl_billpay WHERE pay_amt='$payamt' AND pay_bill='$paybill'");
				
				$getbill=f(q("SELECT * FROM cbill WHERE blid=(SELECT MAX(blid) FROM cbill WHERE bl_cid='$custid')"));
		$res1=mysqli_query($con,"UPDATE cbill SET bl_stat=0,bl_payamt='0',bl_due='$getbill[bl_amt]' WHERE blid=(SELECT MAX(blid) FROM cbill WHERE bl_cid='$custid')");

			
		}	
		

		if($res){
			?>		
				 <script type="text/javascript">
	            Swal.fire('Payment Deleted','','success');
	             </script>
			<?	

			}else{?>


			 <script type="text/javascript">
            Swal.fire('Payment Cannot be deleted','','error');
             </script>


		<?	}

			}else{ ?>


				 <script type="text/javascript">
	            Swal.fire('This bill cannot be deleted, please delete bills after this bill first','','error');
	             </script>



				
		<?	}

			
			}


	}

	


	?>
	<? include "ajax.php"; 
		

	?>

	 <style type="text/css">
         th{
         font-size: 12px !important;
         }
         td{
         font-size: 10px !important;
         }
      </style>
	
	<div class="main-container">
		<div class="pd-ltr-20 customscroll customscroll-10-p height-100-p xs-pd-20-10">
			<div class="min-height-200px">
				<div class="page-header">
					<div class="row">
						<div class="col-md-6 col-sm-12">
							<div class="title">
								<h4>Payment List</h4>
							</div>
							<nav aria-label="breadcrumb" role="navigation">
								<ol class="breadcrumb">
									<li class="breadcrumb-item"><a href="index.php">Home</a></li>
									<li class="breadcrumb-item active" aria-current="page">Bill List</li>
								</ol>
							</nav>
						</div>
						<div class="col-md-6 col-sm-12 text-right">
							<!-- <div class="dropdown">
								<a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-toggle="dropdown">
									January 2018
								</a>
								<div class="dropdown-menu dropdown-menu-right">
									<a class="dropdown-item" href="#">Export List</a>
									<a class="dropdown-item" href="#">Policies</a>
									<a class="dropdown-item" href="#">View Assets</a>
								</div>
							</div> -->
						</div>
					</div>
				</div>
				<!-- Default Basic Forms Start -->
				
				<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<div class="clearfix mb-20">
						<div class="pull-left">
							<h5 class="text-blue">Payment List</h5>
						</div>
					</div>
					<div class="row">
						<table class="stripe hover multiple-select-row data-table-export nowrap">
							<thead>
								<tr>
									<th class="table-plus datatable-nosort">Bill No</th>
									<th>Date</th>	
									<th>Customer</th>	
									<th>Bill Amt</th>
									<th>Amt Paid</th>
									<th>Action</th>	
								</tr>
							</thead>
							<tbody>
								<?	
									$stdt=$_POST['stdt'];
									$enddt=$_POST['enddt'];
									$totalpay=0;
									
									$csadded=$_SESSION['murgibhai'];	
									$getlog=f(q("SELECT * FROM tbl_login WHERE l_uid='$csadded'"));
									$ltype=$getlog['l_type'];
									

									if($ltype=='2'){

										$getu=f(q("SELECT * FROM adduser WHERE id='$getlog[user_id]' AND area_id='$getlog[user_id]'"));
									}elseif($ltype=='1'){

										$getu=f(q("SELECT * FROM adduser WHERE id='$getlog[user_id]'"));
									}else{
										$getu=f(q("SELECT * FROM adduser WHERE id='$getlog[user_id]'"));
										// $getu=f(q("SELECT * FROM adduser WHERE custid=''"));
									}


									if($custac==''){

									$qw="SELECT * FROM cbill WHERE bl_paydt BETWEEN '$stdt' AND '$enddt' ORDER BY blid DESC";
									}else{

									$qw="SELECT * FROM cbill WHERE bl_cid='$getu[id]' AND bl_paydt BETWEEN '$stdt' AND '$enddt' ORDER BY blid DESC";
									}	

									//echo $qw;
									$getc=mysqli_query($con,$qw);
									while($arr=mysqli_fetch_array($getc)){
										
										if(floatval($arr['bl_payamt'])>0){
										$getar=f(q("SELECT * FROM adduser WHERE id='$arr[bl_cid]'"));
										$totalpay=floatval($totalpay)+floatval($arr['bl_payamt']);
								?>
								<tr>
									<td class="table-plus"><?=$arr['bl_no']?></td>
									<td><?=date("d-m-Y", strtotime($arr['bl_dt']))?></td>
									<td>
										<b><?=$getar['name']?> </b><br>
						                  <? if($getar['is_cable']=='1'){ ?>
						                     <b>VC NO: </b><?=$getar['vc']?><br>
						                     <b>STB NO: </b><?=$getar['stb']?><br>
						                     <b>A/C No: </b><?=$getar['custid']?><br>
						                  <?}else{ ?>
						                  
						                     <b>IP: </b><?=$getar['vc']?><br>
						                     <b>Userid: </b><?=$getar['stb']?><br>


						                  <? } ?> 
																


									</td>
									<td><?=$arr['bl_amt']?></td>	
									<td><?=$arr['bl_payamt']?></td>	
									<td>
										
										<div class="dropdown">
											<a class="btn btn-outline-primary dropdown-toggle" href="#" role="button" data-toggle="dropdown" style="color: black;">
												<i class="fa fa-ellipsis-h"></i>
											</a>
											<div class="dropdown-menu dropdown-menu-right">
												
												<a class="dropdown-item" href="getbills.php?bookids=<?=$arr['bl_trn']?>" target="_blank"><i class="fa fa-print"></i> Print Bill</a>
												<? if($ltype!='3'){?>
												<!-- <a class="dropdown-item" href="<?=$_SERVER['PHP_SELF']?>?action=delete&cid=<?=$arr['blid']?>" onclick="return confirm('Are you sure you want to delete this item?');"><i class="fa fa-trash"></i> Delete</a>-->
												
												<? } ?>
											</div>
										</div>


									<!-- <a href="getpdf.php?bookids=<?=$arr['bl_trn']?>" target="_blank">Get PDF</a><br>
									<a href="<?=$_SERVER['PHP_SELF']?>?action=delete&cid=<?=$arr['blid']?>" onclick="return confirm('Are you sure you want to delete this item?');">DELETE</a><br> -->
									

									</td>
								</tr>
								<? } }?>	
							</tbody>
						</table>
					</div><br><br>
					<h5>Total Payment Collection:&#8377;<?=$totalpay?></h5>
				</div>
				

			</div>
			<?php include('include/footer.php'); ?>
		</div>
	</div>
	<?php include('include/script.php'); ?>

	<script src="src/plugins/datatables/media/js/jquery.dataTables.min.js"></script>
	<script src="src/plugins/datatables/media/js/dataTables.bootstrap4.js"></script>
	<script src="src/plugins/datatables/media/js/dataTables.responsive.js"></script>
	<script src="src/plugins/datatables/media/js/responsive.bootstrap4.js"></script>
	<!-- buttons for Export datatable -->
	<script src="src/plugins/datatables/media/js/button/dataTables.buttons.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.bootstrap4.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.print.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.html5.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.flash.js"></script>
	<script src="src/plugins/datatables/media/js/button/pdfmake.min.js"></script>
	<script src="src/plugins/datatables/media/js/button/vfs_fonts.js"></script>
	<script>
		$('document').ready(function(){
			$('.data-table').DataTable({
				scrollCollapse: true,
				autoWidth: false,
				responsive: true,
				columnDefs: [{
					targets: "datatable-nosort",
					orderable: false,
				}],
				"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
				"language": {
					"info": "_START_-_END_ of _TOTAL_ entries",
					searchPlaceholder: "Search"
				},
			});
			$('.data-table-export').DataTable({
				scrollCollapse: true,
				autoWidth: false,
				responsive: true,
				columnDefs: [{
					targets: "datatable-nosort",
					orderable: false,
				}],
				"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
				"language": {
					"info": "_START_-_END_ of _TOTAL_ entries",
					searchPlaceholder: "Search"
				},
				dom: 'Bfrtip',
				buttons: [
				'copy', 'csv', 'pdf', 'print'
				]
			});
			var table = $('.select-row').DataTable();
			$('.select-row tbody').on('click', 'tr', function () {
				if ($(this).hasClass('selected')) {
					$(this).removeClass('selected');
				}
				else {
					table.$('tr.selected').removeClass('selected');
					$(this).addClass('selected');
				}
			});
			var multipletable = $('.multiple-select-row').DataTable();
			$('.multiple-select-row tbody').on('click', 'tr', function () {
				$(this).toggleClass('selected');
			});
		});
	</script>
</body>
</html>