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

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

<?php
	ini_set('display_errors', 0);
if(empty($_SESSION['LOGINID']) && empty($_SESSION['LOGINNM']) ){

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


include("user.php");
	$date1 = $_GET["date1"];
	$date2 = $_GET["date2"];
	
?>


						  
	<?php
												  

$settingsSQL = mysqli_query($connection, "SELECT * FROM `settings` where name='address' ");
$settingsSQL = mysqli_fetch_object($settingsSQL);
									
$address = $settingsSQL->value;

$settingsSQL = mysqli_query($connection, "SELECT * FROM `settings` where name='mobile' ");
$settingsSQL = mysqli_fetch_object($settingsSQL);
									
$our_mobile = $settingsSQL->value;

$settingsSQL = mysqli_query($connection, "SELECT * FROM `settings` where name='email' ");
$settingsSQL = mysqli_fetch_object($settingsSQL);
									
$our_email = $settingsSQL->value;
								   
?>	

<html>

<style>
body {
  background: rgb(204,204,204); 
}
page {
  background: white;
  display: block;
  padding-top: 2cm;
  padding-left: 2cm;
  padding-right: 2cm;
  margin: auto;
  margin-bottom: 2.0cm;
}
page[size="A4"] {  
  width: 21cm;
  height: auto; 
}
page[size="A4"][layout="landscape"] {
  width: 29.7cm;
  height: 21cm;  
}

@media print {
  body, page {
    margin: 0;
    box-shadow: 0;
  }
}



</style>

  <head>
    <title>Booking Print</title>
	<meta http-equiv='content-type' content='text/html;charset=utf-8' />
  </head>
  
  <page size="A4">
  
  <body>
<center>
<table style="width:100%;border: 0px solid black;" cellpadding='0' height='' cellspacing='0'  >
 
  <tr align="center" >
    <td  style="width:100%; "  > <img src="assets/images/logo_wd.png" width="150" /> </br>
<?php echo "$address"; ?> </br>
<?php echo "$our_email"; ?> </br>
<?php echo "$our_mobile"; ?>  </br>
<b> Due List </b></br>
Event From Date : <?php echo "$date1"; ?> to  <?php echo "$date2"; ?>

	</td>
  
  </tr>
</table>

</br></br>

<table style="width:100%" cellpadding='' height='' cellspacing='0' border='0' align='center' valign= 'top' >
	<tr align="left" >
	
	
	
	
	  <td style="width:10%;border-bottom: 1px solid black;" > 
	   &nbsp;&nbsp; <b>ID </b>
      </td> 
	  <td style="width:25%;border-bottom: 1px solid black;" > 
	   <b>Client Name</b>
      </td> 
	  <td style="width:15%;border-bottom: 1px solid black;" > 
	   <b>Primary No</b>
      </td> 
	  <td style="width:5%;border-bottom: 1px solid black;" > 
	   <b>No.of.Ev</b>
      </td> 
	    <td style="width:15%;border-bottom: 1px solid black;" > 
	   <b>Total Booking</b>
      </td> 
	    <td style="width:15%;border-bottom: 1px solid black;" > 
	  <b> Paid</b>
      </td> 
	   <td style="width:15%;border-bottom: 1px solid black;" > 
	   <b> Due</b>
      </td>
	</tr> 
	
	
	
	 <?php 
											
											$sl=0;
                                $sqlSelectedLG = "
								select op2.*,op1.total_ev,op1.p_no from (SELECT t1.temp_id,count(t1.id) total_ev,t2.booking_id,t2.cell_1 p_no  FROM `events` as t1 left join bookings as t2 on t1.temp_id=t2.id   WHERE t2.status=1 AND (t1.date BETWEEN '$date1' AND '$date2') GROUP by t1.temp_id) as op1 left join (
								select * from (select booking_id,cell_1,name,ev_total,in_amt,paid,ev_total+in_amt-paid due from (select f1.* ,IFNULL(f4.in_amount,0) in_amt,(IFNULL(f2.ev_value,0)+IFNULL(f3.added_later,0))-((IFNULL(f2.ev_value,0)+IFNULL(f3.added_later,0))*IFNULL(f1.discount,0)/100) ev_total,((IFNULL(f2.ev_value,0)+IFNULL(f3.added_later,0)+IFNULL(f4.in_amount,0))-IFNULL(f1.paid,0)) as due,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,IFNULL(t1.discount,0) discount,t1.status, IFNULL(t2.paid,0) as paid FROM `bookings` as t1 LEFT JOIN (SELECT temp_booking_id, sum(IFNULL(amount,0)) as paid FROM `transactions` 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 left join (select t2.customer_id, IFNULL(t1.inv_amount,0)-IFNULL(t2.discount,0) in_amount from (SELECT invoice_id, sum(total) inv_amount FROM `invoice_items` group by invoice_id order by invoice_id desc) as t1 left join invoices as t2 on t1.invoice_id=t2.inv_id left join bookings as t3 on t2.customer_id=t3.id) as f4 on f4.customer_id=f1.id where f1.status!=0 ) as k1 where k1.due!=0) as p1 where p1.due!=0 
union 
select p2.* from (select l1.booking_id,l1.cell_1,l1.name,0 ev_total,IFNULL(l2.inv_amount,0)-IFNULL(l2.discount,0) in_amt,IFNULL(l2.paid,0) paid,IFNULL(l2.inv_amount,0)-IFNULL(l2.discount,0)-IFNULL(l2.paid,0) due from bookings as l1 left join (select t1.*,IFNULL(t4.paid,0) paid,(t1.inv_amount-t2.discount-IFNULL(t4.paid,0) ) as due, t2.id, t2.date, t2.customer_id,t2.notes,t2.discount, t2.added_at, t2.updated_at,t3.booking_id,t3.name c_name from (SELECT invoice_id, sum(total) inv_amount FROM `invoice_items` group by invoice_id order by invoice_id desc) as t1 left join invoices as t2 on t1.invoice_id=t2.inv_id left join bookings as t3 on t2.customer_id=t3.id left join (SELECT inv_id, sum(amount) paid FROM `transactions` where type=2 GROUP by inv_id) as t4 on t1.invoice_id=t4.inv_id ) as l2 on l1.booking_id=l2.booking_id where l1.booking_id LIKE '9%') as p2 where p2.due!=0				) as op2	on op1.booking_id=op2.booking_id			

";

                               $sqlSelectedLG = mysqli_query($connection, $sqlSelectedLG);

$tdue=0;
$ttotal_charge=0;
$tpaid=0;
                               while ($list = mysqli_fetch_object($sqlSelectedLG)) {

                                 
                                  $idn = $list->booking_id;
								   $cell_1 = $list->p_no;
									$client_name = $list->name;
									$sub_total = $list->ev_value;
									$added_later = $list->added_later;
									$total_charge1 = $list->ev_total;
									$total_ev = $list->total_ev;
									
									$in_amt=$list->in_amt;
									
									
									$total_charge=$total_charge1+$in_amt;
									$paid = $list->paid;
									$tpaid=$tpaid+$paid;
								    
								
									$ev_total = $list->ev_total;  
									
									$due = $list->due;
									
										 $ttotal_charge=$ttotal_charge+$total_charge;	
			                  
								$tdue=$tdue+$due;
									
									?>
	
	
	

	
	
	
	<tr align="left" >
	  <td style="width:10%;border-bottom: 1px solid black;" > 
	   &nbsp;&nbsp; <?php echo $idn; ?>
      </td> 
	  <td style="width:25%;border-bottom: 1px solid black;" > 
	  <?php echo $client_name; ?>
      </td> 
	  <td style="width:15%;border-bottom: 1px solid black;" > 
	<?php echo $cell_1; ?>
      </td> 
	   <td style="width:5%;border-bottom: 1px solid black;" > 
	<?php echo $total_ev; ?>
      </td> 
	    <td style="width:15%;border-bottom: 1px solid black;" > 
	<?php echo $total_charge; ?>
      </td> 
	    <td style="width:15%;border-bottom: 1px solid black;" > 
	  <?php echo $paid; ?> 
      </td> 
	   <td style="width:15%;border-bottom: 1px solid black;" > 
	  <?php echo $due; ?>
      </td>
	</tr> 
	
	
							   <?php } ?>
							   
							   <tr align="left" >
	  <td style="width:10%;border-bottom: 1px solid black;" > 
	   &nbsp;&nbsp; 
      </td> 
	  <td style="width:25%;border-bottom: 1px solid black;" > 
	  
      </td> 
	  <td style="width:15%;border-bottom: 1px solid black;" > 
	 Total = 
      </td> 
	  <td style="width:15%;border-bottom: 1px solid black;" > 
	
      </td> 
	    <td style="width:15%;border-bottom: 1px solid black;" > 
	<?php echo $ttotal_charge; ?>
      </td> 
	    <td style="width:20%;border-bottom: 1px solid black;" > 
	  <?php echo $tpaid; ?> 
      </td> 
	   <td style="width:15%;border-bottom: 1px solid black;" > 
	   <?php echo $tdue; ?>
      </td>
	</tr> 
</table>
</br></br>




 
 
 
 
 
 
 
 
 </center> 
 
 
  </body>
  
  </page>
  
</html>