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

$page_security = 'SA_SUBCONTRACTOR_PURCHASE_ORDER_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_purchase_orders_inquiry_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/projects/includes/db/subcontractor_purchase_orders_db.inc");



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

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


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

$sc_poh = view_sc_purchase_orders_header($sc_po_id);

$project_id = $sc_poh['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 Purchase Order #" . $sc_po_id);
br();

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


start_row();


label_cells(_("Reference"), $sc_poh['reference'], "class='tableheader2'");
end_row();
label_cells(_("Project"), $sc_poh['project_description'], "class='tableheader2'");
end_row();
label_cells(_("Supplier"), $sc_poh['supp_name'], "class='tableheader2'");
end_row();
label_cells(_("Date"), sql2date($sc_poh['date']), "class='tableheader2'");
end_row();

if($sc_poh['rate_include_taxes'] == 1){
    $rate_include_taxes = "Yes";
}else{
    $rate_include_taxes = "No";
}
label_cells(_("Rate Include Taxes"), $rate_include_taxes, "class='tableheader2'");
end_row();
label_cells(_("Tax Group"), $sc_poh['tax_group_name'], "class='tableheader2'");
end_row();
label_cells(_("Agreement Reference"), $sc_poh['agreement_reference'], "class='tableheader2'");
end_row();
label_cells(_("Comment"), $sc_poh['comment'], "class='tableheader2'");
end_row();

 $s = get_project_trans_view_str(ST_APPROVAL_LOG, $sc_po_id."&source_type_id=".ST_SUBCONTRACTOR_PURCH_ORDER, "Approval Status");
        label_cells($s,$approval_statuses[$sc_poh['approval_status']], "class='tableheader2'");

end_table(1);


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

br();

display_heading2("Order Details");

$result = view_sc_purchase_order_details($sc_po_id);  

$th = array(_('Item Type'),_("BOQ Item"), _("Units"), _('Quantity'), _('Rate'),
                _('Period From'),_('Period To'),"Invced Qty", _("Amount"));  
 
start_table(TABLESTYLE2, "width=95%");

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

        if($myrow["item_type"] == 0){
            $item_type = "Description";
        }else{
            $item_type = "BOQ Item";
        }
        label_cell($item_type);
        
        if($myrow["item_type"] == 1){
            
            if($myrow['boq_item_id'] > 0){
                label_cell($myrow['boq_item_code']." - ".$myrow['boq_item_description']);
            }else{
                label_cell("");
            }
          
        }else{
            label_cell($myrow['description']);
        }
       
        if($myrow["item_type"] == 1){
            if($myrow['boq_item_id'] > 0){
                $unit_name = $myrow['unit'];
            }else{
                $unit_name = "";
            }
          
        }else{
            $unit_name = $myrow['unit_name'];
        }
        
        label_cell($unit_name);
        label_cell(number_format($myrow["quantity"], 3), "align=right");
        label_cell(number_format($myrow["rate"], 3), "align=right");
        label_cell(sql2date($myrow["period_from"]), "align=center");
        label_cell(sql2date($myrow["period_to"]), "align=center");
        label_cell(number_format($myrow["invoiced_qty"], 3), "align=right");
        $amount = $myrow["quantity"] * $myrow["rate"];
        label_cell(number_format($amount, 2), "align=right");
//        label_cell($myrow['budget_task_id']." - ".$myrow['budget_code']." - ".$myrow["budget_description"], "align=center");      

        end_row(); 
            
        if(strlen($myrow["budget_code"]) > 0){
            alt_table_row_color($k);
            label_cell('');
            label_cell("<b>Budget: </b>".$myrow['budget_code']." - ".$myrow["budget_description"], "colspan= 10");
            end_row(); 
        }
        
        
        if(strlen($myrow["comment"]) > 0){
            alt_table_row_color($k);
            label_cell('');
            label_cell("<b>Comment:</b>".$myrow["comment"], "colspan= 10");
            end_row(); 
        }
                    
        $total_scpo += $amount;      
       
    }
    
    start_row();
    label_cell("Total", "align=right colspan=8");
    label_cell("<b>".number_format($total_scpo, 2)."</b>", "align=right");
    end_row();
    
    end_table();
  
//----------------------------------------------------------------------------------------------

    br();
    
display_heading2("Order Summary");

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

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



    $tax_group_items = get_tax_group_items_as_array($sc_poh['tax_group_id']);
    foreach ($tax_group_items as $tax_group_item) {
        if($tax_group_item['rate'] > 0){
            $tax_type_amount = get_sc_purchase_orders_tax_type_amount($sc_po_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();

 

end_page(true, false, false, ST_SUBCONTRACTOR_PURCH_ORDER, $sc_po_id);




?>
