<?php

	include "library.php";

	$cxn = start_code();
	
    $cxn_common = db_common_login();
	
//	$start_date = val( $_POST, "start_date" );

//	$end_date = val( $_POST, "end_date" );

	
		/* start the page  */


//	$report_start_date = convert_date_of_birth( $start_date, "dd/mm/yyyy", $cxn );
	
//	$report_end_date = convert_date_of_birth( $end_date, "dd/mm/yyyy", $cxn );

	
	page_header( "Available Credit Notes", $cxn, $cxn_common );


    $query = "SELECT * FROM credit_note WHERE amount_paid < amount ORDER BY practice, effective_date";
    


	$result = run_query( $query, $cxn );
	
    if (query_num_rows( $result ) == 0)
    {
    	echo black( "There are no available credit notes.<br><br>" );
    	
    	echo show_home_link();
    	
    	exit();
    }
    else
    {
    	echo "<br><br>";
    
    	echo "<style type='text/css'>\n";
    	echo "td ";
    	echo " { ";
    	echo " padding:0 8px 0 8px; ";
    	echo " } ";
    	echo "</style>";
    	
    	echo "<table>";
    	
    	$total_income = 0;
    	$total_for_practice = 0;
    	$last_practice = "";
    	
    	echo "<tr>";

    	echo "<td valign='top' align='left'>".black( "<b>Practice</b>" )."</td>";
    	echo "<td valign='top' align='center'>".black( "<b>Effective Date</b>" )."</td>";
    	echo "<td valign='top' align='center'>".black( "<b>Notes</b>" )."</td>";
    	echo "<td valign='top' align='center'>".black( "<b>Amount</b>" )."</td>";

    	echo "</tr>";
    	
    	for ($i=0; $i < query_num_rows( $result ); $i++)
    	{
    		$data = get_row( $result );

            if ($i > 0)    		
            {
                if ($last_practice != $data['practice'])
                {
            		echo "<tr>";
            		echo "<td>".black( "Total ".$last_practice )."</td>";
            		
            		echo "<td></td><td></td>";

            		echo "<td>".black( "$".$total_for_practice )."</td>";
            		echo "</tr>";
            		
                	echo "<tr><td>&nbsp;</td></tr>";
                	
                	$total_for_practice = 0;
                }
            }

    		echo "<tr>";
    		
    		echo "<td>".black( $data['practice'] )."</td>";
    		echo "<td>".black( format_date_of_birth( substr($data['effective_date'], 0, 10), "dd/mm/yyyy" ) )."</td>";
    		echo "<td>".black( $data['comments'] )."</td>";
    		echo "<td>".black( "$".$data['amount'] )."</td>";

            echo "<td>".show_link( "Edit", "edit_all_data.php", "formname", "mpl_credit_note", "tabletitle", "Credit Note", "newrecord", "false", "id", $data['id'] )."</td";

            $total_for_practice += $data['amount'];
            
            $last_practice = $data['practice'];

    		echo "</tr>";
    	}

		echo "<tr>";
		echo "<td>".black( "Total ".$data['practice'] )."</td>";

		echo "<td></td><td></td>";

		echo "<td>".black( "$".$total_for_practice )."</td>";
		echo "</tr>";
    	
    	echo "<tr><td>&nbsp;</td></tr>";

    	echo "</table>";
    }			

    echo "<br><br><br>";

    echo show_home_link();

?>

