<? 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');
	require_once 'lib/audit-log.php';

	$action=$_REQUEST['action'];
	if($action=='delete'){

		$cid=$_REQUEST['cid'];

		$getb=f(q("SELECT * FROM obill WHERE blid='$cid'"));
		$custid=$getb['bl_cid'];

		$getmaxbill=f(q("SELECT MAX(blid) as maxid FROM obill WHERE bl_cid='$custid'"));
		$maxid=$getmaxbill['maxid'];

		if($maxid==$cid){

		// Log the delete action before performing it
		$additionalData = array(
			'bill_id' => $cid,
			'customer_id' => $custid,
			'amount' => $getb['bl_amt']
		);
		$actionDetails = "Other bill deleted for Customer ID: $custid, Bill Amount: " . $getb['bl_amt'] . ", Month: " . $getb['bl_month'] . " " . $getb['bl_year'];
		logAuditAction('OTHER_BILL_DELETE', $actionDetails, $additionalData);


		$res=mysqli_query($con,"DELETE FROM obill WHERE blid='$cid'");

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

			}else{?>


			 <script type="text/javascript">
            Swal.fire('Bill 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>



				// $res=mysqli_query($con,"INSERT INTO tbl_lane SET lnname='$lnname',lnarid='$lnarid'");
		<?	}

			



	}

	


	?>
	<? include "ajax.php"; 
		

	?>

	 <style type="text/css">
         th{
         font-size: 15px !important;
         }
         td{
         font-size: 14px !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>Bill 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">Bill 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>Due</th>
									<th>Action</th>	
								</tr>
							</thead>
							<tbody>
								<?	
									$stdt=$_POST['stdt'];
									$enddt=$_POST['enddt'];

									if($stdt==''){

										$stdt=date("Y-m-d");
										$enddt=date("Y-m-d");
									}

									$csadded=$_SESSION['murgibhai'];
									//echo "SELECT * FROM tbl_login WHERE l_uid='$csadded'";	
									$getlog=f(q("SELECT * FROM tbl_login WHERE l_uid='$csadded'"));
									$ltype=$getlog['l_type'];
									$uidd=$getlog['user_id'];

									if($ltype=='2'){

										$query="SELECT * FROM obill WHERE bl_user='$getlog[l_uid]' AND bl_dt BETWEEN '$stdt' AND '$enddt' ORDER BY blid DESC";
									}elseif($ltype=='1'){

										$query="SELECT * FROM obill WHERE bl_dt BETWEEN '$stdt' AND '$enddt' ORDER BY blid DESC";
									}else{

										//$getu=f(q("SELECT * FROM adduser WHERE custid='$getlog[l_read]'"));
										$query="SELECT * FROM obill WHERE bl_cid='$uidd'  AND bl_dt BETWEEN '$stdt' AND '$enddt' ORDER BY blid DESC";
										// $getu=f(q("SELECT * FROM adduser WHERE custid=''"));
									}

									//echo $query;


									$getc=mysqli_query($con,$query);
									while($arr=mysqli_fetch_array($getc)){

										$getar=f(q("SELECT * FROM adduser WHERE id='$arr[bl_cid]'"));

										$sumpay=f(q("SELECT SUM(pay_amt) as sums FROM tbl_opay WHERE pay_bill='$arr[blid]'"));
										$amtpay=$arr['bl_amt'];
										$paid=$sumpay['sums'];
										$due=floatval($amtpay)-floatval($paid);




										if($arr['bl_attch']==''){   
                                                            $longurl="https://cable.biswascable.in/getpdf.php?bookids=".$arr['bl_trn'];
                                                            $timestamp = time();
                                                            // actual value: $time = 1628625734
                                                            $signature = md5( $timestamp . '268d2a5320' );
                                                            // actual value: $signature = "1d5710a81760ddf64f2a3d3941002798"
                                                                $username = 'admin';
                                                                $password = 'PaSs_$#@!56';
                                                                //$api_url =  'http://uss.e-marketing.ooo/yourls-api.php?timestamp=$timestamp&signature=$signature';

                                                                $api_url =  'http://uss.e-marketing.ooo/yourls-api.php';

                                                                // Init the CURL session
                                                                $ch = curl_init();
                                                                curl_setopt($ch, CURLOPT_URL, $api_url);
                                                                curl_setopt($ch, CURLOPT_HEADER, 0);            // No header in the result
                                                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
                                                                curl_setopt($ch, CURLOPT_POST, 1);              // This is a POST request
                                                                curl_setopt($ch, CURLOPT_POSTFIELDS, array(     // Data to POST
                                                                        'url' => $longurl,
                                                                        'format'   => 'json',
                                                                        'action'   => 'shorturl',
                                                                        'username' => $username,
                                                                        'password' => $password
                                                                    ));

                                                                // Fetch and return content
                                                                $data = curl_exec($ch);
                                                                curl_close($ch);

// Do something with the result. Here, we echo the long URL
$data = json_decode( $data );
$urls=$data->shorturl;  

$upurl=mysqli_query($con,"UPDATE obill SET bl_attch='$urls' WHERE blid='$arr[blid]'");

}else{


    $urls=$arr['bl_attch'];
}

                                                            $textval=htmlentities("Your bill amount for the month ".$arr['bl_month']." is Rs.".$arr['bl_amt'].". Download bill from ".$urls);

                                                           


                                                            $wmsg="https://api.whatsapp.com/send?phone=91".$arr['bl_phone']."&text=".$textval;




								?>
								<tr>
									<td class="table-plus"><?=$arr['bl_no']?><br><br>
									<?php if($arr['bl_stat']==1){?> <small style="color:white;padding:5px;background-color: green;border-radius: 5px;">Paid </small> <?php }else{?> <small style="color:white;padding:5px;background-color: red;border-radius: 5px;">Not Paid</small> <?php } ?>
								
								</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>


						                  <? } ?> 
																
						                  <b>For: </b><?php echo $arr['bl_payuid'];?>

									</td>
									<td><?php echo $arr['bl_amt']?></td>	
									<td><?php echo $due?></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">
												
												
												<? if($ltype=='1'){?><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>
												
											<? } ?>
											<?php if($arr['bl_stat']==0){?>

											<a class="dropdown-item" href="payother.php?blid=<?php echo $arr['bl_trn']?>"><i class="fa fa-check"></i> Add Payment</a>


											<?php } ?>
											<a class="dropdown-item" target="_blank" href="otherprint.php?bookids=<?php echo $arr['bl_trn']?>"><i class="fa fa-print"></i> Print Bill</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>
				</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>