<?php

	include "library.php";

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

    $quote_id = val( $_POST, "quote_id" );
    

    page_header( "", $cxn );			


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


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

        $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 quote ".$data['id'].", practice not found.<br>" );
            }
            else
            {
                $ok = true;
                
                $data2 = get_row( $result2 );
                
                $recipient_name = $data['practice'];
                
                $contact_first_name = $data2['contact_first_name'];

                $email_address = $data2['email_address'];
            }
        }
        else
        if ($data['adviser'] != "")
        {
            $query2 = "SELECT * FROM adviser WHERE display_name = '".$data['adviser']."'";

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

                $data2 = get_row( $result2 );

                $recipient_name = $data['adviser'];
                
                $contact_first_name = $data2['first_name'];

                $email_address = $data2['email_address'];
            }
        }
        else
            echo red( "Error on quote ".$data['id'].", no practice or adviser.<br>" );

        if ($ok)
        {
            if ($contact_first_name =="")
                echo red( "Error on quote ".$data['id'].", no contact name.<br>" );
            else
            if ($email_address =="")
                echo red( "Error on quote ".$data['id'].", no email address.<br>" );
            else
            {
                
            	$subject = "Quote ".$data['id']." from Melbourne Paraplanning for ".$data['client_names'];
            
            	$text_body = "Hi ".name_case( $contact_first_name ).",\n\n";
            	
            	if ($data['task_type'] == "soa")
                	$text_body .= "Thanks for your Paraplanning Request for clients ".$data['client_names']." for a Statement of Advice.\n\n";
                else
            	if ($data['task_type'] == "roa")
                	$text_body .= "Thank you for your Paraplanning Request for clients ".$data['client_names']." for a Record of Advice.\n\n";
            	else
            	    echo red( "Error on quote ".$data['id'].", task type not recognised.<br>" );
            	    
            	$text_body .= "We have assessed your task and would like to quote a price of ".$data['amount_ex_gst']." 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 = "<span style='font-family: verdana; font_size: 10pt;'> Hi ".name_case( $contact_first_name ).",<br><br>";
            	
            	if ($data['task_type'] == "soa")
                	$html_body .= "Thanks for your Paraplanning Request for clients ".$data['client_names']." for a Statement of Advice.\n\n";
                else
            	if ($data['task_type'] == "roa")
                	$html_body .= "Thanks for your Paraplanning Request for clients ".$data['client_names']." for a Record of Advice.\n\n";
            	else
            	    echo red( "Error on quote ".$data['id'].", task type not recognised.<br>" );
            	
            	$html_body .= "We have assessed your task and would like to quote a price of $".$data['amount_ex_gst']." plus GST.<br><br>";
            	$html_body .= "To accept this quote please click on the link below.<br><br><br>";
            	
            	$html_body .= "</span><a style='font-family: verdana; font_size: 18pt; color: #009500; text-decoration: none;' href='https://clients.melbourneparaplanning.com/accept_quote.php?id=".$data['id']."'>Accept Quote</a>";
    
            	$html_body .= "<span style='font-family: verdana; font_size: 10pt;'><br><br><br><br>Kind Regards<br><br>";
            	$html_body .= "Melbourne Paraplanning<br><br><br></span>";
    
    
            	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" );


                    $query3 = "UPDATE quote SET status = 'sent' WHERE id = ".$data['id'];

                    $result3 = run_query( $query3, $cxn );
            		
            		
                    echo black( "Sent quote ".$data['id']." for clients ".$data['client_names']."&nbsp;&nbsp;&nbsp;&nbsp;for $".$data['amount_ex_gst']."&nbsp;&nbsp;&nbsp;&nbsp;to ".$recipient_name.",&nbsp;&nbsp;&nbsp;&nbsp;".$email_address."<br>" );
            	}
            	else
                    echo black( "Quote ".$data['id']." for clients ".$data['client_names']."&nbsp;&nbsp;&nbsp;&nbsp;for $".$data['amount_ex_gst'].",&nbsp;&nbsp;&nbsp;&nbsp;".$recipient_name.",&nbsp;&nbsp;&nbsp;&nbsp;".$email_address."<br>" );
            }
        }
    }

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

?>