<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_TOOL_REQUEST_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 . "/assets/includes/db/tool_requests_inquiry_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");     
include_once($path_to_root . "/includes/db/group_companies_db.inc");   


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

//-------------------------------------------------------------------------------------------------
$woid = 0;
if ($_GET['trans_no'] != "")
{
    $tool_request_id = $_GET['trans_no'];
}

display_heading("Tool Request No: " . $tool_request_id);


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

$myrow = view_tool_request($tool_request_id);

start_row();

label_cells("Reference", $myrow['reference'], "class='tableheader2'");
end_row();

label_cells(_("Project"), $myrow['project_description'], "class='tableheader2'");
end_row();

label_cells(_("Employee"), $myrow['employee_code']." - ".$myrow['initials']."  ".$myrow['employee_last_name'], "class='tableheader2'");
end_row();

label_cells(_("Location"), $myrow['location_name'], "class='tableheader2'");
end_row();

label_cells(_("Date"), sql2date($myrow['date']), "class='tableheader2'");
end_row();

label_cells(_("Total"), number_format($myrow['total'], 2), "class='tableheader2'");
end_row();

label_cells(_("Comment"), $myrow['comment'], "class='tableheader2'");
end_row();


$s = get_project_trans_view_str(ST_APPROVAL_LOG, $myrow['id']."&source_type_id=".ST_TOOL_REQUEST, "Approval Status");
        label_cells($s,$approval_statuses[$myrow['approval_status']], "class='tableheader2'");  
        
end_table(1);

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

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

display_heading("Details");

$result = view_tool_request_details($tool_request_id);

$th = array(_("ID"),_("Tool"), _('Rate Category'),_('Budget'),
            _('Site'), _("From Date"),_("To Date"),_('Project Asset'),
            _("Req. Qty"),_("Iss. Qty"),_('Rate'), _('Days'), _('Cost'));  
                          

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

$line_total =0;
while($row_b = db_fetch($result)){
    
    alt_table_row_color($k);
     
    label_cell($row_b['id']);
    label_cell($row_b['asset_code']."-".$row_b['asset_description']."-"."(".$row_b['asset_id'].")");
    label_cell($row_b['rate_description']);
    label_cell($row_b["task_description"]."(".($row_b["task_code"]).")");
    label_cell($row_b['site_name']);
    label_cell(sql2date($row_b['from_date']), "align=center");
    label_cell(sql2date($row_b['to_date']), "align=center");
    label_cell(get_yes_no_name($row_b["project_asset"]), "align=center"); 
    
    
    label_cell($row_b['quantity'], "align=right");
    label_cell($row_b['issued_qty'], "align=right");
    label_cell($row_b['rate'], "align=right");
    label_cell($row_b['no_of_days'], "align=right");
    
       if($row_b["no_of_days"] == 0){
            $days = date_diff2(sql2date($row_b["to_date"]), sql2date($row_b["from_date"]), 'd');
            $line_cost = ($row_b["rate"] * $row_b["quantity"]) * $days;
        }else{
            $line_cost = $row_b["rate"] * $row_b["quantity"] * $row_b["no_of_days"];
        }
        
    label_cell(number_format($line_cost, 2), "align=right");
    
    if(strlen($row_b['line_comment']) > 0){
        start_row();
        $k +=1;
//        alt_table_row_color($k);
        label_cell('');
        label_cell('<b>Comment :.</b>'.$row_b['line_comment'], 'colspan=12');
        end_row();
    }
    
    
    $line_total += $line_cost;
    
    $k++;
}

start_row();
    
    label_cell("Total", "colspan=12 align=right");
    amount_cell($line_total, true);
        
end_row();

end_table(1);

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

    display_heading("Tool Issues");

    $th = array(_("ID"),_("Reference"),_("Date"),_("Location"),_("Employee"));

    table_header($th);

    $asset_issue_details = view_tool_issues_for_tool_request($tool_request_id);
    
    while($ai_row = db_fetch($asset_issue_details)){
        alt_table_row_color($k);
        
        $id = get_asset_trans_view_str(ST_TOOL_ISSUE, $ai_row['id']);
        label_cell($id);
        label_cell($ai_row['reference']);
        label_cell(sql2date($ai_row['tran_date']), "align=center");
        label_cell($ai_row['location_name']);
        $name = get_group_company_employee_formatted_long_name($ai_row['company_code'], $ai_row['employee_code']);
        label_cell($name) ;  
    }

end_table();

end_page(true, false, false, ST_TOOL_REQUEST, $tool_request_id);

?>
