<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SUPPTRANSVIEW';
$path_to_root = "../..";
include($path_to_root . "/purchasing/includes/po_class.inc");

include($path_to_root . "/includes/session.inc");
include($path_to_root . "/purchasing/includes/ui/purchasing_ui.inc");
include($path_to_root . "/purchasing/includes/db/purchase_requisitions_db.inc");
include($path_to_root . "/purchasing/includes/db/purchase_requisitions_inquiry_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");


$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(1100, 500);
page(_($help_context = "View Purchase Requition"), true, false, "", $js);


if ($_GET['trans_no'] != "")
{
	$purch_req_id = $_GET['trans_no'];
}



function get_pr_allocation_ids_string_of_pr($pr_line_id)
{
    $pr_ids_info = get_pr_allocation_ids($pr_line_id);
    $pr_ids_string = "";
    while($pr_ids=db_fetch($pr_ids_info))
    {
        $pr_ids = array_unique($pr_ids);
        foreach ($pr_ids as $pr_id)
        {
           
            $pr_ids_string .= get_supplier_trans_view_str(ST_PR_LINE_ALLOCATIONS,$pr_line_id, $pr_id)." ";
        }
    }
    $pr_ids_string = trim($pr_ids_string);
    return $pr_ids_string;
}

//$purch_data= get_pr_info($purch_req_id);
//$requisition_id=$purch_data['requisition_id'];

display_heading(_("Purchase Requisition ") . " #" .$purch_req_id);


echo "<br>";
start_table(TABLESTYLE, "width=80%");
//echo "<br>";
$purch_requition_info= view_purchase_requitition_header($purch_req_id);

$project_id = $purch_requition_info['project_id'];
$user_id = $_SESSION['wa_current_user']->user;
$cap = can_access_project($project_id, $user_id);
if($cap['success'] !== true) 
{
    display_error($cap["message"]);
    set_focus('project_id');
    display_footer_exit();
    exit;
}

global $systypes_array, $stock_types;

start_row();
    display_view_cell('Reference',$purch_requition_info['reference']);
    
    display_view_cell('Project',$purch_requition_info['project']);
    
    $source_type_name = $source_order_id = '';
    if($purch_requition_info['source_type'] > 0){
        $source_type_name = $systypes_array[$purch_requition_info['source_type']];
        $source_order_id = $purch_requition_info['source_order_id'];
    }
    display_view_cell('Source Type',$source_type_name);
    
end_row();
    
start_row();
    display_view_cell('Location',$purch_requition_info['location_name']);
    display_view_cell('Estimated Total', number_format($purch_requition_info['total'],2));
    $s = get_trans_view_str($purch_requition_info['source_type'], $source_order_id, $source_order_id);
    display_view_cell("Source No",$s);
//    display_view_cell('Source No',$source_order_id);

end_row();

start_row();
    display_view_cell('Required Date',sql2date($purch_requition_info['pr_date']));
    display_view_cell('Originator',$purch_requition_info['originator_name']);
    $s = get_project_trans_view_str(ST_APPROVAL_LOG, $purch_req_id."&source_type_id=".ST_PURCH_REQ, "Approval Status");
    display_view_cell($s,$approval_statuses[$purch_requition_info['approval_status']], "class='tableheader2'");
end_row();

start_row();
    display_view_cell('PR Completed', get_yes_no_name($purch_requition_info['pr_complete'])); 
end_row();

end_table();
//-------------------------------------------------------------------------------------------------------------


echo "<br>";
display_heading(_("Purchase Requisition Details") );
echo "<br>";
start_table(TABLESTYLE, "width=98%", 6);
//echo "<br>";

$th = array( "Id", _("Budget"), _("Item code"), _("Item Description"), _("Item Type"),_("Req. Unit"),
	_("Req. Qty"),  _("Est. Price"),_("Req. Date"),
        _("Job"), "Line Cost", ("GRN Qty."), _("Iss. Qty"),_("Rtn. Qty"), ("PR Allo."), "Line Status");
table_header($th);
$result= view_pr_details($purch_req_id);
$k = $pr_total = 0;

while($purch_data=db_fetch($result))
{  
    label_cell($purch_data['id']);
    label_cell($purch_data['task_description']);
    label_cell($purch_data['item_code']);
    $item_description = $purch_data['item_pr_description'];
    if(isset($purch_data['item_pr_description']) && strlen($purch_data['item_pr_description']) > 0){
            $item_description  = $purch_data['item_pr_description'];
    }
    else{
        $item_description = $purch_data['description'];
    }
    label_cell($item_description);
    label_cell($stock_types[$purch_data['mb_flag']]);
    label_cell($purch_data['req_unit_name']);
    amount_decimal_cell($purch_data['requested_unit_qty']);
    
    //label_cell($purch_data['req_unit']);
    amount_decimal_cell($purch_data['estimate_price']);
    label_cell(sql2date($purch_data['required_date']));   
    label_cell($purch_data['project_site_description']);
    
    
    
    $line_total = $purch_data['requested_unit_qty'] * $purch_data['estimate_price'];
    qty_cell($line_total, false, 2);

    $total_base_unit_qty = get_grn_qty_by_prd_no($purch_data['id']);
    $total_req_unit_qty = $total_base_unit_qty / $purch_data['req_unit_conv_factor'];
    qty_cell($total_req_unit_qty, false, 3);
    
    $req_unit_issued_qty = $purch_data['issued_qty'] / $purch_data['req_unit_conv_factor'];
    qty_cell($req_unit_issued_qty, false, 3);
    
    $req_unit_issued_qty = $purch_data['returned_qty'] / $purch_data['req_unit_conv_factor'];
    qty_cell($req_unit_issued_qty, false, 3);
    
    $allocation_ids=get_pr_allocation_ids_string_of_pr($purch_data['id']);

    label_cell($allocation_ids, "align=center");
    
    switch ($purch_data["line_complete"]) {
        case 150:
            global $approval_statuses;
            $line_status = $approval_statuses[$purch_data["line_complete"]];
            break;
        case 0: 
            $line_status = "New / Ordering";
            break;
        case 1: 
            $line_status = "Closed / Completed";
            break;
        default:
            $line_status = "Unknown";
            break;
    }
    label_cell($line_status, "align=center");

    end_row();
    
    if(strlen($purch_data['line_comment']) > 0){
        start_row();
        label_cell(text_bold("Comment").": ".$purch_data['line_comment'], "colspan=12");
        end_row();
    }

    
    
    $pr_total += $line_total;
    $k++;
}
start_row();
label_cell("Total Cost", "colspan=9 align=right");
qty_cell($pr_total, true, 2);
label_cell('');
end_table();


//----------------------------------------------------------------------------------------------------

br(2);
start_outer_table(TABLESTYLE_NOBORDER, "width=80%");

    $result= get_pr_issues_for_pr($purch_req_id);
    if(db_num_rows($result) > 0){
        display_heading(_("Purchase Requisition Issues") );
        br();
        start_table(TABLESTYLE, "width=80%", 6);
        //echo "<br>";

        $th = array( _("PRI#"), _("Location"), "Issued By", ("Issued On"));
        table_header($th);

        $k = 0;

        while($purch_data=db_fetch($result))
        {  
            $pri_link = get_supplier_trans_view_str(ST_PR_ISSUE,$purch_data['pri_no']);
            label_cell($pri_link);
            label_cell($purch_data['location']);
            label_cell($purch_data['originator_name']);
            label_cell(sql2date($purch_data['created_on']), "align=center");



            end_row();

            $k++;
        }

        end_table();
    }


    $returns_result= get_pr_returns_for_pr($purch_req_id);
    if (db_num_rows($returns_result) > 0){
        echo "</td><td valign=top>"; // outer table
        if(db_num_rows($returns_result) > 0){
            display_heading(_("Purchase Requisition Returns") );
            br();
            start_table(TABLESTYLE, "width=80%", 6);
            //echo "<br>";

            $th = array( _("PRR#"), _("Location"), "Returned By", ("Returned On"));
            table_header($th);

            $k = 0;

            while($prr=db_fetch($returns_result))
            {  
                $prr_link = get_supplier_trans_view_str(ST_PR_RETURN,$prr['prr_no']);
                label_cell($prr_link);
                label_cell($prr['location']);
                label_cell($prr['originator_name']);
                label_cell(sql2date($prr['created_on']), "align=center");



                end_row();

                $k++;
            }

            end_table();
        }
    }

end_outer_table();

end_page(true, false, false, ST_PURCHORDER, $_GET['trans_no']);

?>
