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

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

include($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");
include_once($path_to_root . "/inventory/includes/db/material_requisitions_db.inc");
include_once($path_to_root . "/inventory/includes/db/issue_materials_db.inc");
include_once($path_to_root . "/inventory/includes/db/material_return_inquiry_db.inc");

include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/includes/common_functions.inc");


$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();

page(_($help_context = "View Material Return"), true, false, "", $js);

if (isset($_GET["trans_no"]))
{
	$trans_no = $_GET["trans_no"];
}

function view_link($row)
{
    $link = get_trans_view_str(ST_MATERIAL_REQ, $row);
    return $link;
}


$rm_header = view_materials_return($trans_no);

display_heading('Material Return' . " #$trans_no");

echo "<br>";
start_table(TABLESTYLE2, "width=90%");

start_row();
    label_cells(_("Received Location"), $rm_header['receiving_location_name'], "class='tableheader2'");
    label_cells(_("Issued By"), $rm_header['created_user_name'], "class='tableheader2'");
end_row();

start_row();
    label_cells(_("Reference"), $rm_header['reference'], "class='tableheader2'");
    label_cells(_("Issued Date"), sql2date($rm_header['rm_date']), "class='tableheader2'");
    //label_cells(_("Approval Path"), $im_header['approval_path_description'], "class='tableheader2'");
end_row();

start_row();
    label_cells(_("Project"), $rm_header['project_description'], "class='tableheader2'");
    label_cells(_("Company"), $rm_header['company_name'], "class='tableheader2'");
end_row();

start_row();    
    label_cells(_("MR No"), get_trans_view_str(ST_MATERIAL_REQ, $rm_header['source_mr_no']), "class='tableheader2'");
    
    if(strlen(($rm_header['employee_code'] > 0))){
        label_cells(_("Employee"), get_group_company_employee_formatted_long_name($rm_header['company_code'], $rm_header['employee_code']), "class='tableheader2'");
    }
    else{
        label_cells(_("Employee"), '', "class='tableheader2'");
    }
end_row();

start_row();
    label_cells(_("Comment"), nl2br($rm_header['comment']), "class='tableheader2'", "colspan=3"); 
end_row();

//comments_display_row(ST_LOCTRANSFER, $trans_no);

end_table(2);

start_table(TABLESTYLE, "width=90%");

$th = array(_("Item Code"), _("Item Description"), _("Unit"),_("Returned Qty"), _("Unit Cost"), _("Line Cost"), _("Budget"), _("Job"));

display_heading('Material Return Details');
table_header($th);

$rmd_lines = view_material_return_details($rm_header['rm_no']);
$total_cost = $k = 0;
while ($item = db_fetch($rmd_lines))
{
    label_cell($item['item_code']);   
    label_cell($item['item_description']);
    label_cell($item['unit_name']);
    
    label_cell(number_format($item['tran_qty'], $item['decimals']), "align=right");
    
    $tran_unit_cost = $item['unit_cost'] * $item['conversion_factor'];
    $line_cost = $tran_unit_cost * $item['tran_qty'];
    
    if ($_SESSION["wa_current_user"]->can_access("SA_VIEW_ITEM_COST")){
        label_cell(number_format($tran_unit_cost, 2), "align=right");        
        label_cell(number_format($line_cost, 2), "align=right");  
    }
    else{
        label_cell("");
        label_cell("");
    }

    label_cell($item["task_description"]);
    label_cell($item['site_description']);
    
    end_row();
    
	$total_cost += $line_cost;
}

start_row();
label_cell("Total", "align=right colspan=5");
label_cell("<b>".number_format($total_cost, 2)."</b>", "align=right");
label_cell("", "colspan=2");
end_row();

end_table(1);

end_page(true, false, false, ST_MATERIAL_REQ, $trans_no);


?>