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

$page_security = 'SA_SUBCONTRACTOR_INVOICE_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/subcontractor_invoices_db.inc");
include_once($path_to_root . "/projects/includes/db/subcontractor_invoice_inquiry_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");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");



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

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


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

function display_subcontractor_invoice_view_header($pi_id)
{
    $myrow = view_subcontractor_invoice_header($pi_id);
    
    $project_id = $myrow['project_id'];
    $user_id = $_SESSION['wa_current_user']->user;
    $cap = can_access_project($project_id, $user_id);
    if($cap['success'] !== true) 
    {
        display_error($cap["message"]);
        set_focus('project_id');
        display_footer_exit();
        exit;
    }
        
    display_heading("Subcontractor Invoice # ".$pi_id);

    br();
    
    start_table(TABLESTYLE2, "width=80%");

    
    start_row();

    //display_heading("Manual Project Invoice Header");
    label_cells(_("Reference"), $myrow['reference'], "class='tableheader2'");
    label_cells(_("Project"), $myrow['project_description'], "class='tableheader2'");
    
    end_row();
    label_cells(_("Supplier"), $myrow['supplier_name'], "class='tableheader2'");
    label_cells(_("P/A Reference"), $myrow['payment_application_ref'], "class='tableheader2'");
    
    end_row();
    label_cells(_("Order Date"), sql2date($myrow['ord_date']), "class='tableheader2'");
    label_cells(_("P/A Amount"), number_format($myrow['payment_application_amount'],2), "class='tableheader2'");
    
    end_row();
    label_cells(_("Payment Terms"), $myrow['payment_terms'], "class='tableheader2'");
    label_cells(_("P/C Reference"), $myrow['payment_certificate_ref'], "class='tableheader2'");
    
    end_row();
    label_cells(_("Supp. Inv. Ref"), $myrow['supp_inv_ref'], "class='tableheader2'");
    label_cells(_("P/C Amount"), number_format($myrow['payment_certificate_amount'],2), "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']);
//    }

    
    if($myrow['po_no'] > 0){
        $s = get_project_trans_view_str(ST_SUBCONTRACTOR_PURCH_ORDER, $myrow['po_no']);
        label_cells(_("PO No"), $s, "class='tableheader2'");
    }
    else{
        label_cells("PO No:", "(Manual Invoice)", "class='tableheader2'");
    }
            
            
    global $approval_statuses;
    $s = get_project_trans_view_str(ST_APPROVAL_LOG, $pi_id."&source_type_id=".ST_SUBCONTRACTOR_INVOICE, "Approval Status");
    label_cells($s,$approval_statuses[$myrow['approval_status']], "class='tableheader2'");
    end_row();

    end_table();

    br(1);
    
    display_heading2("Invoice Summary");
    br(1);

    start_table(TABLESTYLE2, "width=40%");

    start_row();
    label_cells(_("Total Amount"), number_format($myrow['total'], 2), "class='tableheader2'");
    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_subcontractor_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'");
            end_row();
        }
    }

    end_table();

}



function display_subcontractor_invoice_view_details($sci_no)
{  
    global $project_invoice_line_types;    
          
    $result = view_subcontractor_invoice_details($sci_no);  
    
    display_heading2("Invoice Details");
                
    start_table(TABLESTYLE, "width=98%");
    $th = array(_('Seq.'),_('Details'), _('Date'),
                _('Unit'), _('Quantity'),_('Rate'), _('Amount'), _('Category'));              

    table_header($th);
    $k = 0; //row colour counter
   // $total = 0;
    
    while ($myrow = db_fetch($result))
    {
        alt_table_row_color($k);


        label_cell($myrow["sequence"], "align=right");
        
//***********************************
//           Details
//***********************************        
        switch ($myrow['line_type']) {
            case PILT_MAIN_HEADER:
            case PILT_MAIN_SUMMARY:
                label_cell("<b>".$myrow["description"]."</b>");
                break;
            case PILT_MAIN_ITEM:
                if($myrow["item_id"] > 0){
                    label_cell(spaces(10).$myrow["boq_item_code"]." - ".$myrow["description"]);
                }
                else{
                    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:
                if($myrow["item_id"] > 0){
                    label_cell(spaces(20).$myrow["boq_item_code"]." - ".$myrow["description"]);
                }
                else{
                    label_cell(spaces(20).$myrow["description"]);
                }
                break;
            case PILT_DETAIL:
            default:
                if($myrow["item_id"] > 0){
                    label_cell(spaces(30).$myrow["boq_item_code"]." - ".$myrow["description"]);
                }
                else{
                    label_cell(spaces(30).$myrow["description"]);
                }
                break;
            case PILT_FINAL_TOTAL:
                label_cell("<b>".$myrow["description"]."</b>");
                break;
        }

//***********************************
//           Unit, 
//           Quantity, 
//           Rate, 
//           Amount
//***********************************
        
        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($myrow["unit"] == '' || $myrow["unit"] == '0'){
                    label_cell("");
                    label_cell("");
                }
                else{                    
                    label_cell(number_format($myrow["quantity"],3), "align=right");
                    label_cell(number_format($myrow["rate"],2), "align=right");
                }
                label_cell(number_format($myrow["amount"],2). spaces(10), "align=right");
                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>".number_format($myrow["amount"],2)."</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"]);
                label_cell(number_format($myrow["quantity"],3), "align=right");
                label_cell(number_format($myrow["rate"],2), "align=right");
                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($myrow["unit"] == '' || $myrow["unit"] == '0'){
                    label_cell("");
                    label_cell("");
                }
                else{                    
                    label_cell(number_format($myrow["quantity"],3), "align=right");
                    label_cell(number_format($myrow["rate"],2), "align=right");
                }
                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"]);
        
        if($myrow['task_id'] > 0){
            $details = "<b>Budget:</b> ".$myrow["task_code"]." - ".$myrow["task_description"];
        }
        else{
            if(strlen($myrow["debit_account"]) > 0 || strlen($myrow["credit_account"]) > 0){
                $details = "<b>Debit A/C:</b> ".$myrow["debit_account"]." - ".$myrow["debit_account_name"];
                $details .= spaces(5)."<b>Credit A/C:</b> ".$myrow["credit_account"]." - ".$myrow["credit_account_name"];
            }
            else{
                $details = "";
            }
        }
        
        if(strlen($details) > 0){
            alt_table_row_color($k);
            label_cell('');
            label_cell($details, "colspan=6 align=right");
            label_cell('');
            end_row();
        }
        
        
//        label_cell("[".$subcontractor_invoice_line_types[$myrow['line_type']]."]"); 
        
        end_row();  
        
        if(strlen($myrow["line_comment"]) > 0){
            alt_table_row_color($k);
            label_cell('');
            label_cell("<b>Comment:</b> ".$myrow["line_comment"], 'colspan=6 align=right');
            label_cell('');
            end_row();
        }
      
       }       
      
    

}

display_subcontractor_invoice_view_header($pi_id);


br(1);

display_subcontractor_invoice_view_details($pi_id);

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


?>
