
<meta charset="utf-8">

<title>Summary</title>
<!doctype html>

<html>

<?php 

include "head.php";
include "dbconfig.php";
error_reporting(0);
?>
  
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

   <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
   <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

<body style="font-family:Montserrat; font-size:13px;"> 
<form action="" method="post">
	<div class="container-fluid" style="margin-top:200x;">
	 
			      <div class="well col-md-12" style="background-color:#e9e9e9; box-shadow: 5px 5px 5px;">
			       	  
                        
                       <div class="col-md-2">
 						<input type="text" name="from" class="from form-control" placeholder="Date From" id="from">
                     </div>
                       <div class="col-md-2">
                        <input type="text"  name="to" class="to form-control" placeholder="Date To" id="to">
             		 </div>
         <div class="form-group col-sm-3">
			       	   <select name="driver_id" id="driver_id" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Select Driver </option>
                               <?php
							   
							        $customers_sql="SELECT employees.first_name,employees.id from employees,driver_invoices
where driver_invoices.driver_id=employees.id group by employees.id order by first_name ASC";
									$t_result=mysqli_query($con,$customers_sql);
									while($customer=mysqli_fetch_assoc($t_result))
									{
									
									?>
	<option style="padding:20px; background-color:#2977C9; color:#F9F0F1; cursor:pointer;" value="<?php echo $customer['id']; ?>">
	   <?php echo $customer['first_name']; ?>
    </option>
						<?php
						
						}
						?>
			       	   </select>
			       	   </div>
                       <div class="col-md-1">
                       	  <input type="submit" id="select_jobs" class="btn btn-warning" value=" Find ">
                       </div>
                     
                   
                    <!-- now retrieve the transactions of this customer -->
                   </div>
                 			
</div>
				  
			    
</form>
<?php
if (isset($_POST['driver_id']) && !empty($_POST['driver_id'])) {
$sql='';
$sql="SELECT driver_invoices.invoice_no,driver_invoices.driver_id,driver_invoices.date,driver_invoices.vat_amount, employees.first_name,employees.address,employees.post_code,driver_invoices.nett, driver_invoices.gross FROM driver_invoices 
LEFT JOIN invoice_status ON driver_invoices.status = invoice_status.id 
LEFT JOIN employees ON driver_invoices.driver_id = employees.id 
WHERE 1=1
";
$today= date('Y-m-d');
$from="";
$to="";
	if (!empty($_POST['driver_id']))
	{
	  $driver_id=$con->real_escape_string($_POST['driver_id']);
	$sql.=" AND driver_invoices.driver_id=".$driver_id; 
	}
if (!empty($_POST['from']) && !empty($_POST['to']))// && empty($_POST['driver_id']))
	{
	  $from=$con->real_escape_string($_POST['from']);
	  $from = str_replace("/","-",$from);
	  $from = date("Y-m-d", strtotime($from));
	
	  $to=$con->real_escape_string($_POST['to']);
	  $to = str_replace("/","-",$to);
	  $to = date("Y-m-d", strtotime($to));
	$sql.=" AND 
    driver_invoices.date BETWEEN '$from' AND '$to'"; 
	}
/*	elseif (!empty($_POST['from']) && !empty($_POST['to']) && !empty($_POST['driver_id']))
	{
	  $from=$con->real_escape_string($_POST['from']);
	  $from = str_replace("/","-",$from);
	  $from = date("Y-m-d", strtotime($from));
	
	  $to=$con->real_escape_string($_POST['to']);
	  $to = str_replace("/","-",$to);
	  $to = date("Y-m-d", strtotime($to));
	$sql.=" AND driver_invoices.driver_id=".$driver_id." AND 
    driver_invoices.date BETWEEN '$from' AND '$to'"; 
	}
	*/
	
	
	


$sql.=" ORDER BY driver_invoices.invoice_no DESC
";
//echo $sql;
// $customer_id=$_GET['company_id'];
//WHERE customers.id=$customer_id GROUP BY bookings.id
$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);

if (!mysqli_query($con,$sql)) {echo $sql."Error is ". mysqli_error($con);}


?>
	  
<?php
$invoice_sum=mysqli_query($con,$sql);
$total_invoices=mysqli_num_rows($invoice_sum);
$res=mysqli_query($con,$sql);

//echo $sql;

                
                $nett='';
				$vat='';
				$gross='';
				$address='';
				$post_code='';
				while($sum=mysqli_fetch_assoc($invoice_sum))
				{
								$nett=$nett+(float)$sum['nett'];
								$gross=$gross+(float)$sum['gross'];
								$vat=$vat+(float)$sum['vat_amount'];
								$name=ucfirst($sum['first_name']);
								$address=ucfirst($sum['address']);
								$post_code=$sum['post_code'];
				}
				;?>
                
   <form action="" name="print_invoice" method="post">
   
         
       <div class="row col-md-12 well container-fluid" style="background-color:#9BF04E; font-size:18px; margin-left:5px; margin-bottom:10px; padding:5px;">
                <!--div class="col-md-3">
                  <label>Total Invoices : <?php echo $total_invoices;?></label>
                 </div-->  
             
                 <div class="col-md-3">
                  <label>Total : <?php echo "£".$gross;?></label>
                 </div> 
				  <!--div class="col-md-3">
                  <label>VAT: <?php echo "£".$vat;?></label>
                 </div-->        
				 <div class="col-md-2">
                       <input type="button" class="btn btn-primary btn-sm" onClick="printDiv('invoice')" value="Print" /> 
                    </div>       
       	</div>	
       
      <div id="invoices">
	  
	   <?php 
	  
	   $invoice_html=
					 '   
					
					 
					  <div id="invoice" style="margin-left:15%;  width:50%; position:absolute; float:left;">';
					   
					 
					 
					 
					 
					 
					  $invoice_html.=
					 '
					 <div id="invoice" style="margin-left:15%; width:80%; position:absolute; float:left;">
					
						
						  <b>
							 <!--p style="font-size:20px">Wings Chauffeurs Ltd.</p-->
							 
							  <address>
							  <img src="wings_logo.png" style="margin-left:0%; width:122px; height:80px;">
							 <!--p>Office 2.25, 450 Bath Rd, </p>
							 <p>West Drayton,Middlesex,London,</p>
							 <p>UB7 0EB</p-->
							 <p>'.$address.'</p>
							 <p>'.$post_code.'</p>
							  <!--p>E:info@wingschauffeurs.com </p>
							 <p>Ph. +44(0)208077925</p-->
							<div align="right">
							 <p>M. +44(0)07792560326</p>
							
							 <!--p>Website: www.wingschauffeurs.com</p-->
							 </div>
							 </address>
					     ';
						   if($from!="" && $to!="")
					  {
					   $invoice_html.=
					 '
					 <br><br>
					<label> Date &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.date("d/m/Y", strtotime($from)).'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; To &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.date("d/m/Y", strtotime($to)).'</label>';
					 }
						  $invoice_html.=
					 '
						  <br><br><br>
						  <p>Driver  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '.$name.'</p>
						  <br><br>
						  <label>Total  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; £'.$gross.'</label>
						  <br><br><br>
						  <!--label>VAT  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; £'.$vat.'</label>
						  <br><br><br-->					 
				<label>Amount Payable  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; £'.$gross.'</label>
						  <br><br><br>					 
			I accept I am an independent operator and I am self-employed.I am liable for 
				my own National Insurance and TAX contribution to Inland Revenue.
				<br><br>
				This is to confirm that I agree to work with wings chauffeurs limited, as a subcontracted chauffeur
				And as the subcontracted chauffer I am responsible for my own expenses and taxes and accountability
				To the Inland Revenue and all the relevant bodies.
				<br><br><br><br><br><br>				
				
				Signed:
				<br><br><br>		
				Date:
					 </div>
					 
					 ';
					  ?>
         <!--table style="font-family:Montserrat; font-size:13px; cursor:pointer;" id="invoices" class="table invoices table-striped table-bordered table-hover bordered" cellspacing="0">

        <thead>

            <tr class="btn-primary">
              <th>Invoice Number</th>
                           <th>Month</th>
                           <th>Driver</th>
                          
                       
                           <th>Gross</th>
                          <th>Vat</th>
                           <th>See Details</th>
                           
            </tr>

        </thead>

        <tfoot>

            <tr class="btn-primary">
                          <th>Invoice Number</th>
                           <th>Month</th>
                           <th>Driver</th>
                           
                           <th>Gross</th>
                          
                            <th>Vat</th>
                           <th>See Details</th>
                           
            </tr>

        </tfoot>

        <tbody >
				
        <?php
                
				while($invoice=mysqli_fetch_assoc($res))
                {?><td><?php echo $invoice['invoice_no'];?>
                          <td>
						   <?php $start = date("M", strtotime($invoice['date'])); 
						   
						   echo $start  ;?></td>
                            <td><?php echo $invoice['first_name'];?></td>
                           
                        
                            <td><?php echo "£".$invoice['gross'];?></td>
                             <td><?php echo "£".$invoice['vat_amount'];?></td>
						  
               <td><a href="print_driver_invoice.php?invoice_no=<?php echo $invoice['invoice_no'];?>&driver_id=<?php echo $invoice['driver_id'];?>&" onClick="this.href+=new Date().getTime();return true;"><i class="fa fa-print"></i><b>&nbsp;Print</b></a></td>
                </a></td>
               
            </tr>
      			<?php } ?>
            </tbody>
        </table-->
      </div>
	   </div>
      </div>
<div class="modal fade bd-example-modal-lg custom-class" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true" id="jobs_modal">

</div><!-- /.modal -->
</form>

<?php
 echo 
				   '<div class="col-md-11" id="invoice_div" style="margin-left:20px;">'.$invoice_html.'</div>';
}
 if(isset($_POST['selected_invoice']))
			{
		
		/*	
			echo "<pre>";
  				 print_r($_POST);
			echo "</pre>";
		*/	
		$bookingIds = $_POST;
		
		$count=count($_POST['selected_invoice']);
		
		for ($i=0;$i<$count;$i++){
			$counter=1+$i;
			$index = array_search($_POST['selected_invoice'][$i] ,$bookingIds['invoice_no']);
			//echo "index=".$index;
		$status=$con->real_escape_string($_POST['status']);
		$date=$con->real_escape_string($_POST['date']);
		$invoice_no=$bookingIds['invoice_no'][$index];
		
		$update_booking="UPDATE invoices SET
					  status=$status,
					  pay_date='$date'
				  
					  WHERE invoice_no=$invoice_no";
					  //echo $update_booking;
					  if (!mysqli_query($con,$update_booking)) {
							echo $update_booking;
							die('update 3 INTO Bookings -Error: ' . mysqli_error($con));
							exit;
						 }
		}
	}
?>
<?php
if(isset($_POST['print_invoices']))
{
$sql="SELECT invoices.invoice_no,invoices.date, invoices.customer_id, customers.name,invoices.nett, invoices.vat,invoices.gross, invoices.status status_id,
invoice_status.name as status,invoices.pay_date
FROM invoices
LEFT JOIN invoice_status ON invoices.status = invoice_status.id
LEFT JOIN customers ON invoices.customer_id = customers.id 
WHERE invoices.date BETWEEN '$from' AND '$to'";
if(!empty($filter_invoice_no)){
	
	$sql.=" AND invoice_no=$filter_invoice_no";
	}
$sql.=" order by invoices.invoice_no DESC";

include "pdf_demo/fpdf.php";
class PDF extends FPDF
{
function Header()
{
	$this->SetY(5);  
    $this->SetFont('Arial', 'B', 20); 
    $this->SetFillColor(0,0,125); 
    $this->SetTextColor(225); 
    $this->Cell(0, 10, "List of Invoices", 0, 0, 'L', true); 
	
    $this->SetTextColor(0);
    $this->SetFont('Arial', '', 12); 
	$this->Ln(10);
	$this->Cell(100, 10, "Wings Chauffers Ltd.,",0,1);
	$this->SetY(20);
	$this->Cell(100, 10, "12 Lampton Park Road,",0,1);
    $this->SetY(25);;
	$this->Cell(100, 10, "Hounslow,",0,1);
    $this->SetY(30);
	$this->Cell(100, 10, "Middlesex",0,1);
	$this->SetY(35);
	$this->Cell(100, 10, "TW3 4HS ",0,1);
	 $this->SetY(40);
	$this->Cell(100, 10, "United Kingdom ",0,1); 

	$this->SetXY(115,15);   
    $this->SetTextColor(0); 
 	$this->SetFont('Arial', '', 10); 
	$this->Cell(100, 10, "Phone: (0)2082304594",0,1);
	$this->Ln(8);
	$this->SetXY(115,20);   
	$this->Cell(100, 10, "Mobile:(0)7808939220,",0,1);
	$this->Ln(8);
	$this->SetXY(115,25);
	$this->Cell(100, 10, "Email: info@wingschauffeurs.com",0,1);
	$this->Ln(3);
	$this->SetXY(115,30);
	
	$this->Cell(100, 10, "Website: www.limochauffeurs.com");
	$this->SetFont('Arial', 'B', 11); 
	$this->Ln(3);
	$this->SetXY(115,35);
	
	$this->Cell(100, 10, "VAT No: 1234569898");
	// Set up the Invoice To Data
}
 //footer
 function Footer() { 
    $this->SetFont('Arial', '', 10); 
    $this->SetTextColor(0); 
    $this->SetY(-15);
	$this->Cell(0, 20, "Wings Chauffeurs Ltd", 'T', 1, 'C');
    
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); 
	
	//$this->Image('footer.JPG',0,750,620,50); 
}
}
	  
	  $pdf=new PDF();
	  define('£',chr(163));
	  //Add first page
	  $pdf->AddPage('L');
	  $pdf->SetFont( 'Arial', '', 20 );
	 $y_axis_initial = 50;

$pdf->SetFillColor(232, 232, 232);
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(10);
		$pdf->Cell(20, 6, 'Sr.No.', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Date', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Invoice No.', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Status', 1, 0, 'R', 1);
        
        $pdf->Cell(60, 6, 'Company', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Nett', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'VAT', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Gross', 1, 0, 'L', 1);
        

$row_height = 6;
$y_axis = 50;
$y_axis = $y_axis + $row_height;
//echo $sql;
//exit;
$report=mysqli_query($con,$sql);

//initialize counter
$i = 0;

//Set maximum rows per page
$max = 25;

//Set Row Height

 $No = 0;
while($row = mysqli_fetch_array($report))
{
    
	//If the current row is the last one, create new page and print column title
    if ($i == $max)
    {
        $pdf->AddPage('L');

        //print column titles for the current page
        $pdf->SetY($y_axis_initial);
        $pdf->SetX(25);
		$pdf->Cell(20, 6, 'Sr.No.', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Date', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Invoice No.', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Status', 1, 0, 'R', 1);
		
        $pdf->Cell(60, 6, 'Company', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'Nett', 1, 0, 'L', 1);
		$pdf->Cell(30, 6, 'VAT', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'Gross', 1, 0, 'L', 1);
        

        //Go to next row
        $y_axis = $y_axis + $row_height;

        //Set $i variable to 0 (first row)
        $i = 0;
    }
     $No = $No +1;
    $invoice_no = $row['invoice_no'];
	$date = $row['date'];
	$date =date("d/m/Y",strtotime($date));
	$company = $row['name'];
    $nett = number_format($row['nett'],2);
	$vat = number_format($row['vat'],2);
	
	$gross = number_format($row['gross'],2);
    $status = $row['status'];

    $pdf->SetY($y_axis);
    $pdf->SetX(10);
	$pdf->Cell(20, 6, $No, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $date, 1, 0, 'L', 1);
    $pdf->Cell(30, 6, $invoice_no, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, $status, 1, 0, 'R', 1);
	
	$pdf->Cell(60, 6, $company, 1, 0, 'L', 1);
	$pdf->Cell(30, 6, £.$nett, 1, 0, 'R', 1);
	$pdf->Cell(30, 6, £.$vat, 1, 0, 'R', 1);
    $pdf->Cell(30, 6, £.$gross, 1, 0, 'R', 1);
    

    //Go to next row
    $y_axis = $y_axis + $row_height;
    $i = $i + 1;
	$total_net =number_format($total_net+$nett,2);
	$total_vat =number_format($total_vat+$vat,2);
	$total_gross =number_format($total_gross+$gross,2);
}
$pdf->SetFillColor(0,0,255);
$pdf->SetTextColor(255,255,255);
$pdf->SetFont( 'Arial', '', 15);
$pdf->SetXY(150,$y_axis);
$pdf->Cell(30, 6, "Totals", 1, 0, 'R', 1);
$pdf->SetXY(180,$y_axis);
$pdf->Cell(30, 6, £.$total_net, 1, 0, 'R', 1);
$pdf->SetXY(210,$y_axis);
$pdf->Cell(30, 6, £.$total_vat, 1, 0, 'R', 1);
$pdf->SetXY(240,$y_axis);
$pdf->Cell(30, 6, £.$total_gross, 1, 0, 'R', 1);

//Create file
$file_name="Invoice-Report-on ".date("d-m-Y").".pdf";
ob_clean();
$pdf->Output($file_name,'D');

}
?>
<script>
$(document).ready(function() { 
        $('#driver_id').select2({ width: 'resolve' });
	});

</script>


<script type="text/jscript">
$('.date').datepicker({
	
    	onSelect: function() {
			
                $( ".date" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
        });
$('.from').datepicker({
	
    	onSelect: function() {
			
                $( ".from" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
        });
$('.to').datepicker({
	
    	onSelect: function() {
			
                $( ".to" ).datepicker( "option", "dateFormat","dd/mm/yy");
		
        }
        });
		
		
$(".invoices").find('td[data-id]').on('click', function () {
	
	////alert("Demo");
	var selected_col=$(this).attr('col-id');
	
	if(selected_col=='6')
	{
			var invoice_no=$(this).attr('data-id');
			var dataString = 'invoice_no='+invoice_no;
			alert(dataString);
			var url11 = 'ajax/list_invoice_jobs.php?'+new Date().getTime();
			  $.ajax({
				  type: "POST",
			  url: url11,
			  data: dataString,
			  success: function(data) {
				  //alert(data);
				  $('#jobs_modal').html(data);
				  
				  $('#jobs_modal').modal('show');
				  }
			  });
	}
});
</script>
<script type="text/javascript">
$('#driver_id').select2({width: '100%'});
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}
                   </script>
</body>
</html>