<?php session_start(); ob_start(); ?>

<?php  include 'header.php'; ?>

<?php

if(empty($_SESSION['LOGINID']) && empty($_SESSION['LOGINNM']) ){

	header("Location:index.php");
}


include("user.php");
$dayBookDate2=date('Y-m-31');
$dayBookDate1=date('Y-m-01', strtotime($dayBookDate1. ' - 1 years'));


	
if (isset($_POST['day_submit'])) {
	$dayBookDate1=$_POST['date1'];
	$dayBookDate2=$_POST['date2'];
	$sub=2;
	if (isset($_POST['added_later'])) {
		$sub=3;
		$checked="checked";
	}
	
}



?>
  <?php
						  
						   if (isset($_POST['submit'])) {
							    $sub=$_POST['sub'];
								if($sub=='3') {
											  $del_id=$_POST['del_id'];	  
											   $sqlUpdate = "UPDATE bookings SET
                                                  status = '2'

                                            WHERE
                                                  id = '$del_id'           
                     ";
					 
					                           											   
               
                             /* echo $sqlUpdate;
              exit;*/

                                            $sqlUpdated = mysqli_query($connection, $sqlUpdate); 
					                        }
											
											
											$sqlUpdate = "INSERT INTO transactions_deleted
  SELECT * FROM transactions where temp_booking_id = '$del_id'	           
";
$sqlUpdate = mysqli_query($connection, $sqlUpdate); 									  
									  
									  
									  $sqlUpdate = "DELETE FROM `transactions` WHERE `transactions`.`temp_booking_id` = '$del_id'	           
";
$sqlUpdate = mysqli_query($connection, $sqlUpdate); 
echo "<div>
 </br>
 </div>";
 include_once('updated.html');
							   
						   }
						  
						  ?>
<!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">
    <title>Booking List</title>
    <!-- ================= Favicon ================== -->
    <!-- Standard -->
    <link rel="shortcut icon" href="http://placehold.it/64.png/000/fff">
    <!-- Retina iPad Touch Icon-->
    <link rel="apple-touch-icon" sizes="144x144" href="http://placehold.it/144.png/000/fff">
    <!-- Retina iPhone Touch Icon-->
    <link rel="apple-touch-icon" sizes="114x114" href="http://placehold.it/114.png/000/fff">
    <!-- Standard iPad Touch Icon-->
    <link rel="apple-touch-icon" sizes="72x72" href="http://placehold.it/72.png/000/fff">
    <!-- Standard iPhone Touch Icon-->
    <link rel="apple-touch-icon" sizes="57x57" href="http://placehold.it/57.png/000/fff">
    <!-- Styles -->
     <link href="assets/css/lib/data-table/buttons.bootstrap.min.css" rel="stylesheet" />
    <link href="assets/css/lib/font-awesome.min.css" rel="stylesheet">
    <link href="assets/css/lib/themify-icons.css" rel="stylesheet">
    <link href="assets/css/lib/owl.carousel.min.css" rel="stylesheet" />
    <link href="assets/css/lib/owl.theme.default.min.css" rel="stylesheet" />
    <link href="assets/css/lib/weather-icons.css" rel="stylesheet" />
    <link href="assets/css/lib/menubar/sidebar.css" rel="stylesheet">
    <link href="assets/css/lib/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/lib/unix.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
</head>

<body>

    
	
	  <?php include("sidebar.php"); ?>
	
	
    <!-- /# sidebar -->
     <?php include("top.php"); ?>
	<!-- /# topebar --> 
	
	
    <div class="content-wrap">
        <div class="main">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-8 p-r-0 title-margin-right">
                        <div class="page-header">
                            <div class="page-title">
                                <h1>Hello, <span>Welcome Here</span></h1>
                            </div>
                        </div>
                    </div>
                    <!-- /# column -->
                    <div class="col-lg-4 p-l-0 title-margin-left">
                        <div class="page-header">
                            <div class="page-title">
                                <ol class="breadcrumb text-right">
                                    <li><a href="#">Dashboard</a></li>
                                    <li class="active">Home</li>
                                </ol>
                            </div>
                        </div>
                    </div>
                    <!-- /# column -->
                </div>
                <!-- /# row -->
				
				
					<?php if ($user_type==1 OR $user_type==4 OR $user_type==5){ ?>
                <section id="main-content">
				
				
			
				
				
				
				
				
				<div class="row">
				
				        <div class="col-lg-12">
						
						<div class="panel panel-info m-t-15">
                                            <div class="panel-heading">Booking List</div>
                                            <div class="panel-body">
						  	<div class="row-form clearfix">
					       <form action='booking_list.php'  method='POST'> 
                          
                                    <div class="col-sm-12"> 
						   
							<table>
							<tr>
							<td>
							From <input class="date" type="date" value="<?php echo $dayBookDate1; ?>" name="date1" id="datetimepicker2"/>
							</td>
							<td>
							To <input class="date" type="date"  value="<?php echo $dayBookDate2; ?>" name="date2" id="datetimepicker2"/>
							</td>
							<td>
							<input type="checkbox"  name="added_later"  <?php echo $checked; ?> value="3" /> 
							</td>
							
							<td> Filter Additions
							<input class="submit" type="submit"  name="day_submit"  value="Search" /> 
							</td>
							
							</tr>
							</table>
						    
						   </div>
						     
</form>
</div>
                                    <div class="row">
                        <div class="col-lg-12">
                            <div class="card alert">
                                <div class="bootstrap-data-table-panel">
                                    <div class="table-responsive">
                                        <table id="bootstrap-data-table-export" class="table table-striped table-bordered">
                                            <thead>
                                                <tr>
                                                    <th>ID</th>
                                                    <th>Client Name</th>
                                                    <th>Approved Date</th>
													<?php if($sub=="3") { ?>
													<th>Addition Date</th>
													<?php } ?>
													<th>Diary of</th>
													<th>Phone No</th>
													<th>Alt. No</th>
													<th>Sub Total</th>
													<th>Added Later</th>
                                                    <th>Total</th>
													<th>Paid</th>
													<th>Due</th>
													<?php if($sub=="3") { ?>
													<th>Added Event</th>
													<?php } ?>
													
													<th>Total Events</th>
													
													<th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
											
											<?php

						$sqlSelectedLG = "select f1.* , f2.ev_total+IFNULL(f3.ev_total_later,0) ev_total,f2.ev_value,IFNULL(f3.added_later,0) added_later from (SELECT t1.id, t1.booking_id,t1.booking_name,t1.cell_1,t1.cell_2,t1.name,t1.approved_at,t1.added_at,t1.discount,t1.status, t2.paid FROM `bookings` as t1 LEFT JOIN (SELECT temp_booking_id, sum(amount) as paid FROM `transactions` where type=1 GROUP BY temp_booking_id) as t2 on t1.id=t2.temp_booking_id) as f1 left join (SELECT temp_id, count(id) ev_total, sum(ev_value) ev_value FROM `events` where added_later='no' GROUP BY temp_id) as f2 on f1.id=f2.temp_id left join (SELECT temp_id, IFNULL(count(id),0) ev_total_later, sum(ev_value) added_later FROM `events` where added_later='yes' GROUP BY temp_id) as f3 on f1.id=f3.temp_id where f1.status=1 AND (date(approved_at) BETWEEN '$dayBookDate1' AND '$dayBookDate2' ) ";
						
						if($sub=="2") {
						$sqlSelectedLG = "select f1.* , f2.ev_total+IFNULL(f3.ev_total_later,0) ev_total,f2.ev_value,IFNULL(f3.added_later,0) added_later from (SELECT t1.id, t1.booking_id,t1.booking_name,t1.cell_1,t1.cell_2,t1.name,t1.approved_at,t1.added_at,t1.discount,t1.status, t2.paid FROM `bookings` as t1 LEFT JOIN (SELECT temp_booking_id, sum(amount) as paid FROM `transactions` where type=1 GROUP BY temp_booking_id) as t2 on t1.id=t2.temp_booking_id) as f1 left join (SELECT temp_id, count(id) ev_total, sum(ev_value) ev_value FROM `events` where added_later='no' GROUP BY temp_id) as f2 on f1.id=f2.temp_id left join (SELECT temp_id, IFNULL(count(id),0) ev_total_later, sum(ev_value) added_later FROM `events` where added_later='yes' GROUP BY temp_id) as f3 on f1.id=f3.temp_id where f1.status=1 AND (date(approved_at) BETWEEN '$dayBookDate1' AND '$dayBookDate2' ) ";
						}
						if($sub=="3") {
						$sqlSelectedLG = "select g1.* , g2.* from (SELECT temp_id bk_ev_id,added_at ev_added_at, count(id) added_ev_total, sum(ev_value) added_later FROM `events` where added_later='yes' AND (added_at BETWEEN '$dayBookDate1' AND '$dayBookDate2') GROUP BY temp_id) as g1 left join 
(select f1.* , f2.ev_total,f2.ev_value ev_value from (SELECT t1.id, t1.booking_id,t1.booking_name,t1.cell_1,t1.cell_2,t1.name,t1.approved_at,t1.added_at,t1.discount,t1.status, t2.paid FROM `bookings` as t1 LEFT JOIN (SELECT temp_booking_id, sum(amount) as paid FROM `transactions` where type=1 GROUP BY temp_booking_id) as t2 on t1.id=t2.temp_booking_id) as f1 left join (SELECT temp_id, count(id) ev_total, sum(ev_value) ev_value FROM `events` where added_later='no' GROUP BY temp_id) as f2 on f1.id=f2.temp_id  where f1.status=1 ) as g2 on g1.bk_ev_id=g2.id";
						}
						
						
						
						
						

$sqlSelectedLG = mysqli_query($connection, $sqlSelectedLG);
while ($list = mysqli_fetch_object($sqlSelectedLG)) {

                                    
                                   
                                    $idn = $list->id;
									$diary_of = $list->booking_name;
									$ev_added_at = $list->ev_added_at;
									$ev_added_at = date('d/m/Y', strtotime( $ev_added_at));
									$cell_1 = $list->cell_1;
									$cell_2 = $list->cell_2;
								    $date = $list->added_at;
									$date = date('h:i A d/m/Y', strtotime( $date));
									$ap_date = $list->approved_at;
									$ap_date = date('h:i A d/m/Y', strtotime( $ap_date));
									$booking_id = $list->booking_id;
									
									$client_name = $list->name;
									$sub_total = $list->ev_value;
									$added_later = $list->added_later;
									
									$total_charge = $sub_total+$added_later;
									$discount = $list->discount;
									$total_charge=round($total_charge-(($total_charge*$discount)/100));
									
									$paid = $list->paid;
								    $due = $total_charge-$paid;
									$ev_total = $list->ev_total;  
									$ev_total_later = $list->added_ev_total; 
								    $ev_total= $ev_total_later+$ev_total;
										 	
											   
                                  
								
                                    
                                   
                                ?>
											
											
											
											
											
											
											
											
											
											
											
											
                                                <tr>
                                                    <td><?php echo $booking_id;  ?>  

<?php if ($user_type==1){ ?>
													<a href="booking_log.php?id=<?php echo $idn;  ?>" target="popup" onclick="window.open('booking_log.php?id=<?php echo $idn;  ?>','popup','width=800,height=600');   
    return false;">  <font color="red"> <b><i class="ti-menu-alt"></i> </b> </font>
</a> 

<?php } ?>

<a href="booking_print.php?id=<?php echo $idn; ?>" target="blank"> <span class="ti-printer"></span> </a> </td>
                                                    <td><?php echo $client_name;  ?></td>
                                                    <td><?php echo $ap_date;  ?></td>
													<?php if($sub=="3") { ?>
													 <td><?php echo $ev_added_at;  ?></td>
													<?php } ?>
                                                    <td><?php echo $diary_of;  ?></td>
													 <td><?php echo $cell_1;  ?></td>
													  <td><?php echo $cell_2;  ?></td>
													   <td><?php echo $sub_total;  ?></td>
													    <td><?php echo $added_later;  ?></td>
													   <td><?php echo $total_charge;  ?></td>
													<td><?php echo $paid;  ?></td>
													<td><?php echo $due;  ?></td>
													<?php if($sub=="3") { ?>
													 <td><?php echo $ev_total_later;  ?></td>
													<?php } ?>
													<td><?php echo $ev_total;  ?></td>
													
													<td> 
													
															<?php if ($user_type==1){ ?>
													
  <a href="booking_details.php?id=<?php echo $idn;  ?>"> <button class="btn btn-success btn-md" type="submit"><i class="ti-pencil-alt"></i></button> </a>
                                                  <form action='booking_list.php' method='post'>
												
													<input type='hidden' name='sub' value='3'>
													<input type='hidden' name='del_id' value='<?php echo $idn;  ?>'>
													<button class="btn remove btn-danger btn-md" onclick="return confirm('Are you sure you want to Cancel?');" type="submit" name='submit' ><i class="ti-close"></i></button> 
												

												
<?php } ?>	

												</form>
													</td>
                                                </tr>
                                                
<?php } ?>	
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <!-- /# card -->
                        </div>
                        <!-- /# column -->
                    </div>

                                   


							 </div> </div>
							
                        </div>
				
				
                        <!-- /# column -->
                        
                    </div>
					
					
			
				
				
				
				
                    <!-- /# row -->
                   
				   
				   
				           <?php include("footer.php"); ?>
						   
						   
                </section>
				<?php } ?>
				
				
            </div>
        </div>
    </div>

    <div id="search">
        <button type="button" class="close">×</button>
        <form action="search.php">
            <input type="search" name="search" value="" placeholder="type keyword(s) here" />
            <button type="submit" class="btn btn-primary">Search</button>
        </form>
    </div>


    </div>
    <script src="assets/js/lib/jquery.min.js"></script>
    <!-- jquery vendor -->
    <script src="assets/js/lib/jquery.nanoscroller.min.js"></script>
    <!-- nano scroller -->
    <script src="assets/js/lib/menubar/sidebar.js"></script>
    <script src="assets/js/lib/preloader/pace.min.js"></script>
    <!-- sidebar -->
    <script src="assets/js/lib/bootstrap.min.js"></script>
    <!-- bootstrap -->
   
    <script src="assets/js/scripts.js"></script>
	
	 <script src="assets/js/lib/data-table/datatables.min.js"></script>
    <script src="assets/js/lib/data-table/buttons.dataTables.min.js"></script>
    <script src="assets/js/lib/data-table/dataTables.buttons.min.js"></script>
    <script src="assets/js/lib/data-table/buttons.flash.min.js"></script>
    <script src="assets/js/lib/data-table/jszip.min.js"></script>
    <script src="assets/js/lib/data-table/pdfmake.min.js"></script>
    <script src="assets/js/lib/data-table/vfs_fonts.js"></script>
    <script src="assets/js/lib/data-table/buttons.html5.min.js"></script>
    <script src="assets/js/lib/data-table/buttons.print.min.js"></script>
    <script src="assets/js/lib/data-table/datatables-init.js"></script>
</body>

</html>