<?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_issues_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 Issue"), 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;
}
//function view_link($row)
//{
//	$link = get_trans_view_str($row['tran_type'], $row['tran_no']);
//        return $link;
//}

global $inventory_movement_types;

$im_header = view_issue_material_header($trans_no);

//$from_trans = $transfer_items[0];
//$to_trans = $transfer_items[1];

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

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

start_row();
    label_cells(_("Issuing Location"), $im_header['issuing_location_name'], "class='tableheader2'");
    label_cells(_("Issued By"), $im_header['created_user_name'], "class='tableheader2'");
end_row();

start_row();
    label_cells(_("Reference"), $im_header['reference'], "class='tableheader2'");    
    label_cells(_("Employee Company"), $im_header['company_name'], "class='tableheader2'");
    //label_cells(_("Approval Path"), $im_header['approval_path_description'], "class='tableheader2'");
end_row();

start_row();
    label_cells(_("Project"), $im_header['project_description'], "class='tableheader2'");
    if(strlen(($im_header['employee_code'] > 0))){
        label_cells(_("Issued To Employee"), get_group_company_employee_formatted_long_name($im_header['company_code'], $im_header['employee_code']), "class='tableheader2'");
    }
    else{
        label_cells(_("Issued To Employee"), '', "class='tableheader2'");
    }
end_row();

start_row();    
    label_cells(_("MR No"), get_trans_view_str(ST_MATERIAL_REQ, $im_header['source_mr_no']), "class='tableheader2'");    
    label_cells(_("Movement Type"), $im_header['movement_type_name'], "class='tableheader2'");
    
end_row();

start_row();

    label_cells(_("Date"), sql2date($im_header['im_date']), "class='tableheader2'");
    $mt_values = get_movement_type_linked_field_values($im_header['linked_field_type'], $im_header['linked_field_id'], $im_header['group_company_code']);
    label_cells($mt_values['field_name'], $mt_values['value'], "class='tableheader2'");
end_row();

//comments_display_row(ST_LOCTRANSFER, $trans_no);

end_table(2);

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

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

display_heading('Issue Material Items');
table_header($th);
$im_lines = view_issue_material_details($im_header['im_no']);
$total_cost = $k = 0;
while ($item = db_fetch($im_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);
//
//is_voided_display(ST_LOCTRANSFER, $trans_no, _("This transfer has been voided."));

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