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

include($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/purchasing/includes/db/pr_returns_inquiry_db.inc");
include_once($path_to_root . "/purchasing/includes/db/pr_issues_db.inc"); 

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
page(_($help_context = "View Payment to Supplier"), true, false, "", $js);

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


echo "<center>";

display_heading(_("Purchase Requisition Return") . " #$trans_no");

echo "<br>";

    $pr_issue_result = view_pr_return($trans_no);
        while($pr_issue_row = db_fetch($pr_issue_result)){
            
            start_outer_table(TABLESTYLE2, "width=70%");

                table_section(1, "50%");
                    label_row(_("Reference:"), $pr_issue_row['reference'], "class='tableheader2'");
                    label_row(_("Porject:"), $pr_issue_row['project_description'], "class='tableheader2'");
                    if($pr_issue_row["cn_no"] == 0){
                        $cn_no = '';
                    }else{
                        $cn_no = $pr_issue_row["cn_no"];
                    }
                    label_row('CN #:', get_trans_view_str(ST_SUPPCREDIT, $cn_no), "class='tableheader2'");
                    
                    
                    $date = $pr_issue_row['created_on'];
                    $real_date = substr($date, 0, 11);
                    label_row(_("Issued Date:"), sql2date($real_date), "class='tableheader2'");

                    label_row(_("PR #:"), get_trans_view_str(ST_PURCH_REQ, $pr_issue_row['pr_no']), "class='tableheader2'");
                    label_row(_("PR Issue #:"), get_trans_view_str(ST_PR_ISSUE, $pr_issue_row['pri_no']), "class='tableheader2'");

                table_section(2, "50%");
                    label_row(_("Location:"), $pr_issue_row['location_name'], "class='tableheader2'");
                    label_row(_("Originator:"), $pr_issue_row['originator_name'], "class='tableheader2'");
                    
                    
                    $pri_row = get_pri_record_by_id($pr_issue_row['pri_no']);

                    $mv_tp = get_movement_type($pri_row['movement_type']);
                    label_row(_("Movement Type:"), $mv_tp['name'], "class='tableheader2'");

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

            end_outer_table(2);

            if (strlen($pr_issue_row['comment']) > 0) {
                start_table(TABLESTYLE2);
                    label_row(_("Comment:"), nl2br($pr_issue_row['comment']), "class='tableheader2'");
                end_table(2);
            }
            
        }


display_heading(_("Purchase Requisition Return Details"));

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

$th = array(_("Item Code"),_("Description"), _("Site"), _("Unit"), _("Qty"),  
                _("Unit Cost"), _("Total"), "Budeget", "Stock/<br>Purchase<br>(GL) Unit Cost");               
	
table_header($th);

$pr_issue_detail_result = view_pr_returns_details($trans_no);

$k = $pri_total = 0;
global $stock_types;
while($pr_issue_detail = db_fetch($pr_issue_detail_result))
{  
    label_cells(null, $pr_issue_detail['item_code'], "class='tableheader2'");
    label_cells(null, $pr_issue_detail['description'], "class='tableheader2'");
    label_cells(null, $pr_issue_detail['site_name'], "class='tableheader2'", 'align=center');  
    label_cells(null, $pr_issue_detail['tran_unit'], "class='tableheader2'");
    label_cells(null, number_format($pr_issue_detail['tran_qty'], 3), "class='tableheader2'", 'align=right');
    
    $tran_unit_cost = $pr_issue_detail['unit_cost'] * $pr_issue_detail['conversion_factor'];
    label_cells(null, number_format($tran_unit_cost, 2), "class='tableheader2'", 'align=right');
    
    $line_total = $pr_issue_detail['tran_qty'] * $tran_unit_cost;
    label_cells(null, number_format($line_total, 2), "class='tableheader2'", 'align=right');
     
    label_cells(null, $pr_issue_detail['task_description'], "class='tableheader2'"); 
    
    if(in_array($pr_issue_detail['mb_flag'], array("B","M"))){
        $tran_unit_ave_cost = $pr_issue_detail['standard_cost'] *  $pr_issue_detail['conversion_factor'];
        $tran_unit_ave_cost_text = number_format($tran_unit_ave_cost,2);
    }
    else{
//        $tran_unit_ave_cost_text = $stock_types[$pr_issue_detail['mb_flag']];
        $tran_unit_purchase_unit_cost = $pr_issue_detail['unit_cost'] *  $pr_issue_detail['conversion_factor']; 
        $tran_unit_ave_cost_text = number_format($tran_unit_purchase_unit_cost,2);
    }
//    label_cells(null, number_format($tran_unit_ave_cost, 2), "align=right"); 
    label_cell($tran_unit_ave_cost_text,'align=right');
    end_row();
    $k++;
    $pri_total += $line_total;
}

start_row();
label_cell(text_bold('Total'), 'colspan=6 align=right');
label_cell(text_bold(number_format($pri_total, 2)), 'align=right');
label_cell('', 'colspan=2');

end_row();
end_table(2);

$voided = is_voided_display(ST_SUPPAYMENT, $trans_no, _("This payment has been voided."));


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



?>