<?php

	include "library.php";

	$cxn = start_code( true );

	$cxn_common = db_common_login();

	$userid = val( $_SESSION, 'userid' );

    $practice = val( $_SESSION, "logged_in_practice" );
    
    page_header( "Invoices", $cxn );
    
   
	$query = "SELECT * FROM invoice WHERE practice = '".$practice."' ORDER BY due_date DESC";

 	$result = run_query( $query, $cxn );

    if (query_num_rows($result) == 0)
    {
        echo black( "<br><br>You have no invoices to list.<br><br>" );
    }
    else
    {
     	echo "<table width='100%'>";
     	
     	echo "<tr><td width='8%'>".black( "<i>Invoice number</i>&nbsp;&nbsp;" )."</td><td width='8%'>".black( "<i>Invoice date</i>&nbsp;&nbsp;" )."</td><td width='8%'>".black( "<i>Due date</i>&nbsp;&nbsp;" )."</td><td width='8%'>".black( "<i>Amount ex GST</i>" )."</td><td width='8%'>".black( "<i>GST</i>" )."</td><td width='8%'>".black( "<i>Total due</i>" )."</td><td width='8%'>".black( "<i>Amount received</i>" )."</td><td width='8%'>".black( "<i>Status</i>" )."</td><td width='35%'>&nbsp;</td></tr>";
    
        echo "<tr><td>&nbsp;</td></tr>";
        
        
    	for ($i=0; $i < query_num_rows($result); $i++)
    	{
    	    $status = "";

    		$data = get_row( $result );

            echo "<tr><td>".$data['id']."</td>";

            echo "<td>".ad_format_date( $data['effective_date'], "dd/mm/yyyy" )."</td>";

            echo "<td>".ad_format_date( $data['due_date'], "dd/mm/yyyy" )."</td>";
            
            echo "<td>".$data['amount_ex_gst']."</td>";

            echo "<td>".$data['gst']."</td>";

            echo "<td>".$data['total_due']."</td>";

            echo "<td>".$data['amount_received']."</td>";

            if ($data['status'] == "paid_in_full")
                $status = "<span class='ggreen'>Paid in full</span>";
            else
            if ($data['due_date'] >= date( "Y-m-d" ))
                $status = "<span class='ggreen'>Pending</span>";
            else
                $status = "<span class='ggred' style='color: #ff0000; font-weight: bold'>Overdue</span>";

            echo "<td>".$status."</td>";

            echo "</tr>";
            
    	}
    	
    	echo "</table>";
    }	
	
?>