
<meta charset="utf-8">

<title>List of Orders</title>
<!doctype html>

<html>

<?php 

include "head.php";
include "dbconfig.php";

?>
  
  <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;">
	  <label><h2> Select a Company</h2></label>
			      <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="company_id" id="company_id" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Select from List </option>
                               <?php
							   
							        $customers_sql="SELECT customers.name,customers.id from customers,invoices
where invoices.customer_id=customers.id group by customers.id order by 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['name']; ?>
    </option>
						<?php
						
						}
						?>
			       	   </select>
			       	   </div>
                       <div class="col-md-1">
                       	  <input type="submit" id="select_jobs" class="btn btn-warning" value="Find Invoices">
                       </div>
                       <!--
                       <div class="col-md-2">
                          <input type="button" name="btn" value="Print Invoices" id="filter_invoice" data-toggle="modal" data-target="#confirm-submit" class="btn btn-success btn-sm pull-right" />
                       </div>
			       	   -->
                    <!-- now retrieve the transactions of this customer -->
                   </div>
                 			
</div>
				  
			    
</form>
<?php
$today= date('Y-m-d');
$from="";
$to="";
$company_id="";
$sql='';
$sql1='';
$sql2='';
$sql3='';

$sql="SELECT invoices.invoice_no,invoices.date, invoices.customer_id, customers.name as company,invoices.nett, invoices.vat,invoices.gross, invoices.amount_paid,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
";

	
if (!empty($_POST['from']) && !empty($_POST['to']))
	{
	if(!empty($_POST['company_id'])){	
	$company=$_POST['company_id'];
	
$sql1=" WHERE 
customers.id=$company 
 
";
	}else{
	$sql1=" WHERE customers.id LIKE '%'";
	}
	  $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));
	 
	   $sql2.=" AND invoices.date BETWEEN '$from' AND '$to'";
	}
	



$sql3.=" ORDER BY invoices.invoice_no DESC
";
$sql=$sql.$sql1.$sql2.$sql3;
$res=mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($res);
if(!$res){echo $sql.'<br>Error is '. mysqli_error($con);}
//echo $sql;


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

//echo $sql;

                
                $nett=0;
				$vat=0;
				$gross=0;
				while($sum=mysqli_fetch_assoc($invoice_sum))
				{
								$nett=$nett+(float)$sum['nett'];
								$vat=$vat+(float)$sum['vat'];
								$gross=$gross+(float)$sum['gross'];
				}
				;?>
                
   <form action="" name="print_invoice" method="post">
     <div class="col-md-12 well" style="font-size:15px; background-color:#64D2E9;">
       <div class="col-md-3"><label style="font-size:20px;">Update Invoice Status</label></div>
       <div class="col-md-3"><select name="status" class="form-control"><option value="1" selected>Paid</option>
     <option value="2">Not Paid</option>
       </select></div>
       <div class="col-md-3">
       <input type="text" name="date" class="form-control date" placeholder="Select Date When Paid">
       
       </div>
     
    <div class="col-md-3">
    <input name="query" id="query" type="hidden" value="<?php echo $sql;?>">
    
    <input type="submit" name="submit" class="btn btn-sm btn-success pull-left" value="Update Invoice"/>
    
    
    
	</div> 
         </div>
         
       <div class="row col-md-12 well container-fluid" style="background-color:#07A8EC; color:#FFFFFF 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 Nett: <?php echo "£".$nett;?></label>
                 </div>  
                 <div class="col-md-3">
                  <label>Total VAT: <?php echo "£".$vat;?></label>
                 </div>   
                 <div class="col-md-3">
                  <label>Total Gross: <?php echo "£".$gross;?></label>
                 </div>               
       	</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>Company</th>
                          
                           <th>Nett</th>
                           <th>VAT</th>
                           <th>Gross</th>
						   <th>Amount Paid</th>
                           <th>Status</th>
                          <th>Update</th>
                           <th>See Details</th>
                           <th></th>
                           
            </tr>

        </thead>

        <tfoot>

            <tr class="btn-primary">
                          <th>Invoice Number</th>
                           <th>Month</th>
                           <th>Company</th>
                           
                           <th>Nett</th>
                           <th>VAT</th>
                           <th>Gross</th>
						   <th>Amount Paid</th>
                           <th>Status</th>
                             <th>Update</th>
                           <th>See Details</th>
                           <th></th>
                           
            </tr>

        </tfoot>

        <tbody >
				
        <?php
                
				while($invoice=mysqli_fetch_assoc($res))
                {?>
				<tr>
				<?php
			      if($invoice['amount_paid']!=$invoice['gross'])	
				  {
				  echo '<td style="background:#FF0000; color:#FFFFFF;">';
				  }
				  else
				  {
				  echo '<td>';
				  }
				  ?>
                           <?php echo $invoice['invoice_no'];?>
                           <input name="invoice_no[]" type="hidden" class="checkbox selected_booking" value=<?php echo $invoice['invoice_no'];?>
                           </td>
                          
						  <?php
			      if($invoice['amount_paid']!=$invoice['gross'])	
				  {
				  echo '<td >';
				  }
				  else
				  {
				  echo '<td>';
				  }
				  ?>
						  
						   <?php $start = date("M", strtotime($invoice['date'])); 
						   
						   echo $start  ;?></td>
                            
							<?php
			      if($invoice['amount_paid']!=$invoice['gross'])	
				  {
				  echo '<td >';
				  }
				  else
				  {
				  echo '<td>';
				  }
				  ?>
							<?php echo $invoice['company'];?></td>
                          
                           <?php
			      if($invoice['amount_paid']!=$invoice['gross'])	
				  {
				  echo '<td >';
				  }
				  else
				  {
				  echo '<td>';
				  }
				  ?>
						   <?php echo "£".$invoice['nett'];?></td>
                           
						   <?php
			      if($invoice['amount_paid']!=$invoice['gross'])	
				  {
				  echo '<td >';
				  }
				  else
				  {
				  echo '<td>';
				  }
				  ?>
						   <?php echo "£".$invoice['vat'];?></td>
                            <?php
			      if($invoice['amount_paid']!=$invoice['gross'])	
				  {
				  echo '<td >';
				  }
				  else
				  {
				  echo '<td>';
				  }
				  ?>
							<?php echo "£".$invoice['gross'];?></td>
							 <?php
			      if($invoice['amount_paid']!=$invoice['gross'])	
				  {
				  echo '<td style=" width:15%;">';
				  }
				  else
				  {
				  echo '<td style="width:15%;">';
				  }
				  ?>
				  <?php echo $invoice['amount_paid']; ?>
							 <!--input name="amount_paid" type="number"  id="amount_paid<?php echo $invoice['invoice_no'];?>" style="width:75%; float:left;" class="form-control"  value="<?php //echo $invoice['amount_paid']; ?>">
							 <a href="update_invoice_amount.php?invoice_no=<?php //echo $invoice['invoice_no'];?>" onClick="window.location=this.href+'&amount_paid='+document.getElementById('amount_paid<?php //echo $invoice['invoice_no'];?>').value;return false;"><button class="form-control" style="width:25%; float:right;">>></button></a-->

							 </td>
                            <?php
                             if ($invoice['status_id']=='1'){?>
                           
							 <td style="background-color:#46E804; color:#0F0F0F;">
                             
                                
						    <?php echo $invoice['status']." on ".date("d/m/Y",strtotime($invoice['pay_date'])); 
						   }else{
								
								echo '<td style="background-color:#F1070B; color:#FFF8F8;">'.$invoice['status'];
								}
							?>
				<?php
			      if($invoice['amount_paid']!=$invoice['gross'])	
				  {
				  echo '<td style=" text-align:right;">';
				  }
				  else
				  {
				  echo '<td style="text-align:right;">';
				  }
				  ?>		  
                   
                 <input name="selected_invoice[]" type="checkbox" class="checkbox selected_invoice" value="<?php echo $invoice['invoice_no'];?>">
                 </td>        
               <?php
			      if($invoice['amount_paid']!=$invoice['gross'])	
				  {
				  echo '<td >';
				  }
				  else
				  {
				  echo '<td>';
				  }
				  ?>
				  <a href="print_invoice.php?invoice_no=<?php echo $invoice['invoice_no'];?>&" onClick="this.href+=new Date().getTime();return true;"><i class="fa fa-print"></i><b>&nbsp;Print</b></a>
                </a>
				&nbsp; &nbsp; &nbsp;
				<a href="print_invoice_ad.php?invoice_no=<?php echo $invoice['invoice_no'];?>&" onClick="this.href+=new Date().getTime();return true;"><i class="fa fa-print"></i><b>&nbsp;Print AD</b></a>
                </a>
				</td>
                 <?php
			      if($invoice['amount_paid']!=$invoice['gross'])	
				  {
				  echo '<td >';
				  }
				  else
				  {
				  echo '<td>';
				  }
				  ?>
				 <a href="update_invoice.php?invoice_no=<?php echo $invoice['invoice_no'];?>&" onClick="this.href+=new Date().getTime();return true;"><i class="fa fa-user"></i><b>&nbsp;Update</b></a></td>
                </a></td>
                
                <!--
               <td col-id="6" class="customers" data-id="<?php //echo $invoice['invoice_no'];?>">See Jobs&nbsp;<i class="fa fa-bars" aria-hidden="true"></i></td>
                </td>
                 -->
            </tr>
      			<?php } ?>
            </tbody>
        </table>
	   </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

 if(isset($_POST['selected_invoice']) && isset($_POST['submit']))
			{
		
		/*	
			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']);
		$date = str_replace("/","-",$date);
	    $date = date("Y-m-d", strtotime($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;
						 }
		}
		echo '<script>alert("Invoice Status is updated.");var url11 = "list_invoice.php?"+new Date().getTime(); window.location.replace(url11)</script>';
	}
?>
<?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, "Limo 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@limochauffeurs.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, "Limo 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 = $row['nett'];
	$vat = $row['vat'];
	
	$gross = $row['gross'];
    $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 =$total_net+$nett;
	$total_vat =$total_vat+$vat;
	$total_gross =$total_gross+$gross;
}
$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>
/* data string to send for printing report
 $('#print_invoices').click(function(){
	 
 var query = $('#query_value').val();
 
 
			  	
                var dataString = 'query=' + query;
               alert(dataString);
				
                $.ajax({
                    type: "POST",
                    url: "ajax/generate_invoice_report.php",
                    data: dataString,
                    success: function(data) {
                    // alert(data);
					 //console.log(data); 
					 
                }
            });
	   
	        
});
*/

</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');
				  }
			  });
	}
});
$('#company_id').select2({width: '100%'});
</script>

</body>
</html>