<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_PROJECT_BILLING_INQ';
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/projects/includes/db/manual_project_invoice_db.inc");
include_once($path_to_root . "/projects/includes/db/project_payment_application_db.inc");
include_once($path_to_root . "/projects/includes/db/project_billing_inquiry_db.inc");



$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
page(_($help_context = "View Manual Project Invoice"), true, false, "", $js);

//-------------------------------------------------------------------------------------------------


if ($_GET['trans_no'] != "")
{
    $pi_id = $_GET['trans_no'];
}

function display_project_invoice_header($myrow)
{
    $pi_id = $myrow['order_no'];
    
    start_table(TABLESTYLE2, "width=100%");


    start_row();

    //display_heading("Manual Project Invoice Header");

    label_cells(_("Project"), $myrow['project_description'], "class='tableheader2'");
    end_row();
    label_cells(_("Customer"), $myrow['customer_name'], "class='tableheader2'");
    end_row();
    label_cells(_("Branch"), $myrow['branch_name'], "class='tableheader2'");
    end_row();
    label_cells(_("Reference"), $myrow['reference'], "class='tableheader2'");
    end_row();
    label_cells(_("Order Date"), sql2date($myrow['ord_date']), "class='tableheader2'");
    end_row();
    label_cells(_("Payment Terms"), $myrow['payment_terms'], "class='tableheader2'");
    end_row();
    label_cells(_("Sales Person"), $myrow['salesman_name'], "class='tableheader2'");
    end_row();

    $project_sales_order_link ='';
    if($myrow['project_sales_order_id'] > 0){
        $project_sales_order_link = get_project_trans_view_str(ST_PROJECT_SALES_ORDERS, $myrow['project_sales_order_id']);
    }
    label_cells(_("Customer PO:"), $project_sales_order_link, "class='tableheader2'");
    end_row();

    $payment_application_link = '';
    if($myrow['payment_application_id'] > 0){
        $payment_application_link = get_project_trans_view_str(ST_PROJECT_PAYMENT_APPLICATION, $myrow['payment_application_id']);
    }
    label_cells(_("Payment Application"), $payment_application_link, "class='tableheader2'");

    end_row();


    label_cells(_("Payment Certificate Reference"), $myrow['payment_certificate_ref'], "class='tableheader2'");
    end_row();

    $payment_certificate_amount = '';
    if($myrow['payment_certificate_amount'] > 0){
        $payment_certificate_amount = number_format($myrow['payment_certificate_amount'], 2);
    }
    label_cells(_("Payment Certificate Amount"), $payment_certificate_amount, "class='tableheader2'");
    end_row();

    global $approval_statuses;
    $s = get_project_trans_view_str(ST_APPROVAL_LOG, $pi_id."&source_type_id=".ST_PROJECT_INVOICE, "Approval Status");
    label_cells($s,$approval_statuses[$myrow['approval_status']], "class='tableheader2'");
    end_row();

    end_table();

    //----------------------------------------------------------------------------------------------

}


function display_project_invoice_summary($myrow)
{
//    br();

//    display_heading("Invoice Summary");

    $pi_id = $myrow['order_no'];
    
    start_table(TABLESTYLE2, "width=100%");

    start_row();
    label_cells(_("Total Amount (Without Taxes)"), number_format($myrow['total'], 2), "class='tableheader2'", "align=right");
    end_row();
    start_row();
    label_cells(_("Price Include Taxes"), get_yes_no_name($myrow['rate_include_taxes']), "class='tableheader2'");
    end_row();
    start_row();
    label_cells(_("Tax Group"), $myrow['tax_group_name'], "class='tableheader2'");
    end_row();



    $tax_group_items = get_tax_group_items_as_array($myrow['tax_group_id']);
    foreach ($tax_group_items as $tax_group_item) {
        if($tax_group_item['rate'] > 0){
            $tax_type_amount = get_manual_project_invoice_tax_type_amount($pi_id, $tax_group_item['tax_type_id']);
            start_row();
            label_cells($tax_group_item['tax_type_name'], number_format($tax_type_amount, 2), "class='tableheader2'", "align=right");
            end_row();
        }
    }

    start_row();
    label_cells(_("Total Amount (With Taxes)"), text_bold(number_format($myrow['total'] + $myrow['taxes'], 2)), "class='tableheader2'", "align=right");
    end_row();
    
    end_table();


    echo "<br>";

}


function display_project_invoice_details($pi_id)
{
    global $project_invoice_line_types;    

    $result = get_view_manual_project_invoice_details($pi_id);

    start_table(TABLESTYLE, "width=95%");
    $th = array(_('Seq.'),_('Details'), _('Date'),
                    _('Unit'), _('Quantity'),_('Rate'), _('Amount'), _('Category'));            

    table_header($th);
    $k = 0; //row colour counter

    display_heading("Project Invoice Details");

     while ($myrow = db_fetch($result))
     {
        alt_table_row_color($k);

            label_cell($myrow["sequence"], "align=right");

            switch ($myrow["line_type"]) {
                case PILT_MAIN_HEADER:
                case PILT_MAIN_SUMMARY:
                    label_cell("<b>".$myrow["description"]."</b>");
                    break;
                case PILT_MAIN_ITEM:
                    label_cell(spaces(10).$myrow["description"]);
                    break;
                case PILT_SUB_HEADER:
                case PILT_SUB_SUMMARY:
                    label_cell(spaces(10)."<b>".$myrow["description"]."</b>");
                    break;
                case PILT_SUB_ITEM:
                    label_cell(spaces(20).$myrow["description"]);
                    break;
                case PILT_DETAIL:
                default:
                    label_cell(spaces(30).$myrow["description"]);
                    break;
                case PILT_FINAL_TOTAL:
                    label_cell("<b>".$myrow["description"]."</b>");
                    break;
            }

    //      label_cell($myrow["unit"]);  

          switch ($myrow["line_type"]) {
                case PILT_MAIN_HEADER:
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    break;
                case PILT_MAIN_SUMMARY:
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    amount_cell($myrow["amount"], true);
                    break;
                case PILT_MAIN_ITEM:
                    if(is_date(sql2date($myrow["completed_date"]))){
                        label_cell(sql2date($myrow["completed_date"]), "align=center");
                    }
                    else{
                        label_cell('');
                    }
                    label_cell($myrow["unit_name"]);
                    if(strlen($myrow["unit_name"]) > 0){
                        label_cell(number_format($myrow["quantity"],3), "align=right");
                        label_cell(number_format($myrow["rate"],2), "align=right");
                    }
                    else{
                        label_cell('');
                        label_cell('');
                    }
                    label_cell(number_format($myrow["amount"],2). spaces(10), "align=right");
    //                amount_cell($myrow["amount"]);
                    break;
                case PILT_SUB_HEADER:
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    break;
                case PILT_SUB_SUMMARY:
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("<b>".$myrow["amount"]."</b>". spaces(10), "align=right");
                    break;
                case PILT_SUB_ITEM:
                    if(is_date(sql2date($myrow["completed_date"]))){
                        label_cell(sql2date($myrow["completed_date"]), "align=center");
                    }
                    else{
                        label_cell('');
                    }
                    label_cell($myrow["unit_name"]);
                    if(strlen($myrow["unit_name"]) > 0){
                        label_cell(number_format($myrow["quantity"],3), "align=right");
                        label_cell(number_format($myrow["rate"],2), "align=right");
                    }
                    else{
                        label_cell('');
                        label_cell('');
                    }
                    label_cell(number_format($myrow["amount"],2). spaces(10), "align=right");
                    break;
                case PILT_DETAIL:
                default:
                    if(is_date(sql2date($myrow["completed_date"]))){
                        label_cell(sql2date($myrow["completed_date"]), "align=center");
                    }
                    else{
                        label_cell('');
                    }
                    label_cell($myrow["unit_name"]);
                    if(strlen($myrow["unit_name"]) > 0){
                        label_cell(number_format($myrow["quantity"],3), "align=right");
                        label_cell(number_format($myrow["rate"],2), "align=right");
                    }
                    else{
                        label_cell('');
                        label_cell('');
                    }
                    label_cell(number_format($myrow["amount"],2). spaces(10), "align=right");
                    break;
                case PILT_FINAL_TOTAL:
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    label_cell("");
                    amount_cell($myrow["amount"], true);
                    break;
            }

            label_cell($myrow["category_name"]);
    //        label_cell($myrow["debit_account"]." - ".$myrow["debit_account_name"]);
    //        label_cell($myrow["credit_account"]." - ".$myrow["credit_account_name"]);    

    //        label_cell("[".$project_invoice_line_types[$myrow['line_type']]."]"); 

            end_row();  

            if(strlen($myrow["line_comment"]) > 0){
                alt_table_row_color($k);
                label_cell($myrow["line_comment"], 'colspan=8');
                end_row();
            }
     }


    end_table();
}

display_heading("Project Invoice #: " . $pi_id);

br();

$invh = view_project_invoice($pi_id);

start_outer_table(TABLESTYLE2, "width=60%");
    table_section(1);        
    echo "<td>";    
        display_project_invoice_header($invh);
    echo "</td>";
    table_section(2);
    start_row();
    echo "<td>";
        display_project_invoice_summary($invh);
    echo "</td>";
    
end_outer_table();
    
    
br(2);

display_project_invoice_details($pi_id);

br(2);

display_allocations_to(PT_CUSTOMER, $invh['debtor_no'], ST_PROJECT_INVOICE, $pi_id, $invh['total'] + $invh['taxes']);

end_page(true, false, false, ST_PROJECT_INVOICE, $pi_id);


?>
