<?php

	include "library.php";

	$cxn = start_code();
	
    $send_it = val( $_GET, "sendit" );
    
    $invoice_id = val( $_POST, "invoice_id" );
    

    page_header( "", $cxn );			


    if ($invoice_id != "")
        $query = "SELECT * FROM invoice WHERE id = ".$invoice_id;
    else
        $query = "SELECT * FROM invoice WHERE status = 'not_sent'";


    $result = run_query( $query, $cxn );
    
    
    for ($i=0; $i < query_num_rows( $result ); $i++)
    {    
        $data = get_row( $result );

        $invoice_id = $data['id'];
        $practice = $data['practice'];
        
        $ok = false;
        
        if ($data['practice'] != "")
        {
            $query2 = "SELECT * FROM practice WHERE display_name = '".$data['practice']."'";

            $result2 = run_query( $query2, $cxn );
            
            if (query_num_rows( $result2 ) == 0)
            {
                echo red( "Error on invoice ".$data['id'].", practice not found.<br>" );
            }
            else
            {
                $ok = true;
                
                $data2 = get_row( $result2 );
                
                $contact_first_name = $data2['accounts_contact_first_name'];

                $email_address = $data2['accounts_email_address'];
            }
        }

        if ($ok)
        {
            if ($contact_first_name =="")
                echo red( "Error on invoice ".$data['id'].", no contact name.<br>" );
            else
            if ($email_address =="")
                echo red( "Error on invoice ".$data['id'].", no email address.<br>" );
            else
            {
                
            	$subject = "Invoice ".$data['id']." from Melbourne Paraplanning";
            
            	$text_body = "Hi ".name_case( $contact_first_name ).",\n\n";

//            	$text_body .= "We have assessed your task and would like to quote a price of ".$data['amount']." plus GST.\n\n";
//            	$text_body .= "To appove this quote please reply to this email.\n\n\n\n";
//            	$text_body .= "Kind Regards\n\n";
//            	$text_body .= "Melbourne Paraplanning\n\n";
    

            	$html_body = "<img src='https://melbourneparaplanning.com.au/images/MPL_Logo.jpg' alt='MPL logo' height='48'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            	$html_body .= "<span valign='top' style='font-family: verdana; font-size:18pt; font-weight: bold;'>Invoice</span><br><br>";

            	$html_body .= "<span style='font-family: verdana; font-size:12pt; font-weight: bold;'>".$practice."</span><br><br>";

                $html_body .= "<table>";

            	$html_body .= "<tr><td style='font-family: verdana; font-size:10pt; '>Issue Date:</td><td style='font-family: verdana; font-size:10pt; '>".format_date_of_birth( $data['effective_date'], "dd/mm/yyyy" )."</td></tr>";
            	$html_body .= "<tr><td style='font-family: verdana; font-size:10pt; '>Due Date:</td><td style='font-family: verdana; font-size:10pt; '>".format_date_of_birth( $data['due_date'], "dd/mm/yyyy" )."</td></tr>";

                $html_body .= "</table>";

            	$html_body .= "<br><br><br>";
  
            	$html_body .= "<span style='font-family: verdana; font-size:11pt;'>Hi ".name_case( $contact_first_name ).",</span><br><br>";
            	$html_body .= "<span style='font-family: verdana; font-size:11pt;'>This message is invoice number ".$invoice_id." from Melbroune Paraplanning.</span><br><br><br><br><br>";

                $html_body .= "<table>";
                
                $query2 = "SELECT * FROM invoice_line WHERE invoice_id = ".$invoice_id;
                
                $result2 = run_query( $query2, $cxn );

                for ($j=0; $j < query_num_rows( $result2 ); $j++)
                {
                    $data2 = get_row( $result2 );

            	    $text_body .= ($j+1)." ".$data2['description']." $".$data2['amount']."\n\n";

                    $html_body .= "<tr><td style='font-family: verdana; font-size:11pt;'>".($j+1)."</td>";
                    $html_body .= "<td style='font-family: verdana; font-size:11pt;'>".$data2['description']."</td>";
                    $html_body .= "<td align='right' style='font-family: verdana; font-size:11pt;'>$".$data2['amount']."</td></tr>";
                }


                $text_body .= "GST: $".$data['gst']."\n\n";
                $text_body .= "Total Due: $".$data['total_due']."\n\n";

                if ($data['amount_received'] != 0)
                {
                    $text_body .= "Amount Received: $".$data['amount_received']."\n\n";
                    $text_body .= "Net  Due: $".($data['total_due']-$data['amount_received'])."\n\n";
                }

                $html_body .= "<tr><td>&nbsp;</td></tr>";
                
                $html_body .= "<tr><td style='font-family: verdana; font-size:11pt;'>GST</td><td></td><td align='right' style='font-family: verdana; font-size:11pt;'>$".number_format( $data['gst'], 2 )."</td></tr>";
                $html_body .= "<tr><td style='font-family: verdana; font-size:11pt;'>Total Due</td><td></td><td align='right' style='font-family: verdana; font-size:11pt;'>$".number_format( $data['total_due'], 2)."</td></tr>";

                if ($data['amount_received'] != 0)
                {
                    $html_body .= "<tr><td style='font-family: verdana; font-size:11pt;'>Amount Received</td><td></td><td align='right' style='font-family: verdana; font-size:11pt;'>$".number_format( $data['total_due'], 2)."</td></tr>";
                    $html_body .= "<tr><td style='font-family: verdana; font-size:11pt;'>Net Due</td><td></td><td align='right' style='font-family: verdana; font-size:11pt;'>$".number_format( $data['total_due'] - $data['amount_received'], 2)."</td></tr>";
                }
                

                $html_body .= "</table>";
                
                if ($data['total_due'] == 0)
                {
                	$html_body .= "<br><br><span style='font-family: verdana; font-size:11pt; color: #ff0000'>Payment not required</span><br><br>";
                	$text_body .= "\n\n\nPayment not required\n\n";
                }
                else
                {
                	$text_body .= "Our invoice terms are 14 days. Please make full payment by the Due Date.\n\n";
                	$text_body .= "We offer a 5% pay-on-time discount. If full payment is received by the due date, 5% of the invoice total will be added as a credit to your next invoice.\n\n";
    
                	$text_body .= "Our invoice terms are 14 days.\n";
                	$text_body .= "We offer a 5% pay-on-time discount. If full payment is received by the due date, 5% of the invoice total will be added as a credit to your next invoice.\n\n";
    
                	$text_body .= "Payment should be made by direct credit to our bank account as listed below.\n\n\n";
    
                    $text_body .= "Bank name: Westpac\n";
                    $text_body .= "Account name: Melbourne Paraplanning\n";
                    $text_body .= "BSB: 033 129\n";
                    $text_body .= "Account number: 02 9059\n";
    
    
    
                	$html_body .= "<br><br><br><br><span style='font-family: verdana; font-size:11pt;'>";
    
                	$html_body .= "Our invoice terms are 14 days.<br><br>";
                	$html_body .= "We offer a 5% pay-on-time discount. If full payment is received by the due date, 5% of the invoice total will be added as a credit to your next invoice.<br><br>";
    
                	$html_body .= "Payment should be made by direct credit to our bank account as listed below.<br><br></span>";
    
                    $html_body .= "<table>";
                    
                    $html_body .= "<tr><td style='font-family: verdana; font-size:11pt;'>Bank name:</td><td style='font-family: verdana; font-size:11pt;'>Westpac</td><tr>";
                    $html_body .= "<tr><td style='font-family: verdana; font-size:11pt;'>Account name:</td><td style='font-family: verdana; font-size:11pt;'>Aitken Creek Ventures Pty Ltd</td><tr>";
                    $html_body .= "<tr><td style='font-family: verdana; font-size:11pt;'>BSB:</td><td style='font-family: verdana; font-size:11pt;'>033 129</td><tr>";
                    $html_body .= "<tr><td style='font-family: verdana; font-size:11pt;'>Account number:</td><td style='font-family: verdana; font-size:11pt;'>02 9059</td><tr>";
    
                    $html_body .= "</table>";
                }

            	if ($send_it == "true")
            	{
            		send_external( $email_address, $subject, $text_body, $html_body, $cxn, "live" );

            		send_external( manager_email_address, $subject, $text_body, $html_body, $cxn, "live" );

                    if ($data['total_due'] == 0)
                        $query3 = "UPDATE invoice SET status = 'paid_in_full' WHERE id = ".$data['id'];
                    else
                        $query3 = "UPDATE invoice SET status = 'sent' WHERE id = ".$data['id'];
                        
                    $result3 = run_query( $query3, $cxn );
            		
            		
                    echo black( "Invoice ".$data['id']." sent to &nbsp;&nbsp;&nbsp;&nbsp;".$practice.",&nbsp;&nbsp;&nbsp;&nbsp;".$email_address."<br>" );
            	}
            	else
            	{
                    echo black( "Invoice ".$data['id']." &nbsp;&nbsp;&nbsp;&nbsp; to ".$practice.",&nbsp;&nbsp;&nbsp;&nbsp;".$email_address."<br>" );
                    
                    echo black( $html_body."<br><br>" );
            	}
            }
        }
    }

    echo black( "<br>Finished<br>" );

?>