<?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_transfer_requisitions_db.inc");
include_once($path_to_root . "/inventory/includes/material_transfer_receipts_ui.inc");
include_once($path_to_root . "/inventory/includes/db/material_transfer_receipts_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc"); 

$js = "";
if ($SysPrefs->use_popup_windows) {
    $js .= get_js_open_window(900, 600);
}

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

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

function view_material_transfers($mtr_no){
    $mt_details = get_material_transfers_for_requisition($mtr_no);
        $db_num_rows = db_num_rows($mt_details);
            if ($db_num_rows == 0) {
                return;
            }
    
    display_heading2(_(text_bold("Material Requisition Transfers")));

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

    $th = array(_("#"), _("Reference"), _("Issuing Location"), _("Receiving Location"), _("Issued By"),
                _("Issued on"));
    table_header($th);

    $k = 0;
        while ($mts = db_fetch($mt_details)) {
            alt_table_row_color($k);

                label_cell(get_trans_view_str(ST_REQTRANSFER, $mts['mrt_no']));
                label_cell($mts['reference']);
                label_cell($mts['issuing_location_name']);
                label_cell($mts['receiving_location_name']);
                label_cell($mts['issuer_name']);
                label_cell(sql2date($mts['tran_date']));
                
            end_row();
        }

    end_table(1);
    
}

$mtr_items = get_material_transfer_requisition($trans_no);


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

echo "<br>";
start_outer_table(TABLESTYLE2, "width=60%");
    
    table_section(1, '50%');
        $req_location_name = get_location_name($mtr_items['requesting_location']);
        label_row(_("Receiving Location:"), $req_location_name, "class='tableheader2'");
        label_row(_("Requested By:"), $mtr_items['requestor_name'], "class='tableheader2'");
        
        $iss_location_name = get_location_name($mtr_items['issuing_location']);
        label_row(_("Issuing Location:"), $iss_location_name, "class='tableheader2'");
        label_row(_("Date:"), sql2date($mtr_items['mtr_required_date']), "class='tableheader2'");
    
        $project = get_project($mtr_items['project_id']);
        label_row(_("Project:"), $project['description'], "class='tableheader2'");

        label_row(_("Requested On:"), sql2date($mtr_items['created_on']), "class='tableheader2'");

    table_section(2, '50%');
        label_row(_("Reference:"), $mtr_items['reference'], "class='tableheader2'");

        $s = get_project_trans_view_str(ST_APPROVAL_LOG, $trans_no."&source_type_id=".ST_TRANSFERREQ, "Approval Status");
        label_row("$s:", $approval_statuses[$mtr_items['approval_status']], "class='tableheader2'");
        $mtr_complete = $mtr_items['mtr_complete'] == 1 ? "Yes" : "No";
        
        label_row(_("MTR Complete:"), $mtr_complete, "class='tableheader2'");
    
        global $inventory_movement_type_linked_fields;
        label_row(_("Movement Type:"), $mtr_items['movement_type_name'], "class='tableheader2'");

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

end_outer_table(2);

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

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

        $th = array(_("Line Id"), _("Item"), _("Description"), _("Req. Date"), _("Req. Qty"), _("Unit"), _("Tranferred Qty"), _("Received Qty"), 
                    _("Comment"));
        table_header($th);

            $mtr_details = get_mtr_details($trans_no);
            $k = 0;
                while ($item = db_fetch($mtr_details)) {
                    alt_table_row_color($k);
                        label_cell($item['id'], 'align=center');
                        label_cell($item['stock_id']);
                        label_cell($item['description']);
                        label_cell(sql2date($item['required_date']));

                        $dec = get_qty_dec($item['stock_id']);
                        qty_cell($item['requested_qty'] / $item['req_unit_conv_factor'], false, $dec);
                        label_cell($item['requested_unit']);
                        qty_cell($item['issued_qty'] / $item['req_unit_conv_factor'], false, $dec);
                        qty_cell($item['received_qty'] / $item['req_unit_conv_factor'], false, $dec);
//                        label_cell($item['task_description']);
                        label_cell($item['line_comment']);
                    end_row();
                }

    end_table(2);


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

br();

view_material_transfers($trans_no);

br(3);
view_material_transfers_receipt_headers($trans_no, true, $show_data_has=true);

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