<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ITEMSTRANSVIEW';
$path_to_root = "../..";

include($path_to_root . "/includes/session.inc");

//page(_($help_context = "View Inventory Transfer"), true);

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 . "/purchasing/includes/db/purchase_requisitions_db.inc");
include_once($path_to_root . "/inventory/includes/db/material_requisitions_inquiry_db.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 Requisitions"), true, false, "", $js);


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

$mr_header = view_material_requisition_header($trans_no);

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

echo "<br>";
    start_outer_table(TABLESTYLE2, "width=60%");
    
        table_section(1, '50%');

            label_row(_("Issuing Location:"), $mr_header['issuing_location_name'], "class='tableheader2'");
            label_row(_("Project:"), $mr_header['project_description'], "class='tableheader2'");
            label_row(_("Requestor:"), $mr_header['requestor_name'], "class='tableheader2'");
            label_row(_("Reference:"), $mr_header['reference'], "class='tableheader2'");
            label_row(_("Required Date:"), sql2date($mr_header['mr_required_date']), "class='tableheader2'");


        table_section(2, '50%');
            $s = get_project_trans_view_str(ST_APPROVAL_LOG, $trans_no."&source_type_id=".ST_MATERIAL_REQ, "Approval Status");
            label_row("$s:", $approval_statuses[$mr_header['approval_status']], "class='tableheader2'");

            if($mr_header['mr_complete'] == 0)
            {
                label_row(_("Requisition Complete:"), "No", "class='tableheader2'");
            }
            else 
            {
                label_row(_("Requisition Complete:"), "Yes", "class='tableheader2'");
            }

            $prs_link = get_source_pr_ids_string(ST_MATERIAL_REQ, $trans_no);
            label_row(_("Purchase Requisitions:"), $prs_link, "class='tableheader2'");

            global $inventory_movement_type_linked_fields;
            label_row(_("Movement Type:"), $mr_header['movement_type_name'], "class='tableheader2'");

            if ($mr_header['linked_field_type'] != IMLF_NOT_LINKED) {
                $link_field = get_movement_type_linked_field_values($mr_header['linked_field_type'], $mr_header['linked_field_id']);
                label_row(_($inventory_movement_type_linked_fields[$mr_header['linked_field_type']].":"), $link_field['value'], "class='tableheader2'");
            }

    end_outer_table(1);
    
    if (strlen($mr_header['comments']) > 0) {
        start_table(TABLESTYLE);
            label_row('Comment:', nl2br($mr_header['comments']), "class='tableheader2'");
        end_table(1);
    }

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

$th = array(_("Id"),_("Item Code"), _("Item Description"), _("Req. Unit"),_("Req. Qty"),
                    "Unit Cost", "Line Cost",
                    _("Issued Qty"), _("Required Date"), _("Budget"), "Comment");

display_heading('Material Requisition Items');
table_header($th);
$mr_lines = view_material_requisition_details($trans_no);
$total_cost = $k = 0;
while ($item = db_fetch($mr_lines))
{
    label_cell($item['id']);
    label_cell($item['stock_id']);
    label_cell($item['description']);
    
    label_cell($item['requested_unit_name']);
    label_cell(number_format($item['requested_unit_qty'], $item['decimals']), "align=right");
    
    $tran_unit_cost = $item['price'] * $item['req_unit_conv_factor'];
    $line_cost = $tran_unit_cost * $item['requested_unit_qty'];
    
//    $line_cost = $item['requested_qty'] * $item['price'] ;
    
    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("");
    }
      
    
    if($item['issued_qty'] > 0){
        label_cell(number_format(($item['issued_qty'] / $item['req_unit_conv_factor']), $item['decimals']), "align=right");
    }
    else{
        label_cell('');
    }

    label_cell(sql2date($item['required_date']));
    label_cell($item['task_description']);
    
    label_cell($item['line_comment']);
    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);

br();

display_heading2('Material Issues');

$im_header_data = get_material_issues_for_mr($trans_no);
if(db_num_rows($im_header_data) > 0){

    

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

    $th = array(_("MI#"), _("Project"), 
                         _("Issue Location"), _("Issued By"), _("Issued On"));


    table_header($th);
//    $im_header_data = get_material_issue_head_from_mrf($trans_no);
    $k = 0;
    while ($mr_line = db_fetch($im_header_data))
    {
        label_cell(get_trans_view_str(ST_MATERIAL_ISSUE, $mr_line['im_no']));
        label_cell($mr_line['project_description']);
        label_cell($mr_line['issuing_location_name']);
        label_cell($mr_line['created_user_name']);
        label_cell(substr($mr_line['created_on'],0,16), "align=center");
        end_row();
    }
	
    end_table(1);
}
else{
    br();
    display_note("No Material Issues for this requisition.");
}

//
//is_voided_display(ST_LOCTRANSFER, $trans_no, _("This transfer has been voided."));

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