<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_GRN';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

include_once($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 . "/includes/data_checks.inc");
include_once($path_to_root . "/purchasing/includes/db/grn_db.inc");

simple_page_mode(true);

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();

page(_($help_context = "Outstanding Purchase Requistions"
        . ""), false, false, "", $js);



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

$po_no = 0;

if(isset($_GET['PONumber'])){
    $_POST['PONumber'] = $_GET['PONumber'];
}

if(isset($_POST['PONumber'])){
    $po_no = $_POST['PONumber'];
}



//if (isset($_POST['UpdateData'])){
//    mark_selected_i_owe_yous($path_to_root);
//}

start_form();


br();


$Ajax->activate('leaves');

div_start('leaves');

$result = get_outstanding_direct_purchase_requisitions_for_po($po_no);
$pr_count = db_num_rows($result);
    if($pr_count > 0){
    start_table(TABLESTYLE, "width=90%");

    $th = array(_("Pr No"),"Reference", _("Location"), "Project", _("Required By"),
                _("Originator"),_("Created On"), "Status", "PR Complete", "");
    table_header($th);

    $j = 1;
    $k=0; //row colour counter



    $approval_statuses_in = AS_NEW;

    $ious = array();
    //build_iou_array($ious);

    global $document_approval_statuses;

    while ($myrow = db_fetch($result)){

        alt_table_row_color($k);

            //label_cells('',$myrow["id"]);
            label_cell(get_trans_view_str(ST_PURCH_REQ, $myrow["pr_no"]));
            label_cell($myrow["reference"]);
            label_cell($myrow["location_name"]);
            label_cell($myrow["project_description"]);
            label_cell(sql2date($myrow["pr_date"]), "align=center");
            label_cell($myrow["real_name"]);
            label_cell(sql2date($myrow["created_on"]), "align=center");

            label_cell($document_approval_statuses[$myrow["approval_status"]]);
            label_cell(get_yes_no_name($myrow["pr_complete"]), "align=center");

    //        check_cells('', 'selected_'.$iou_line["id"],0);
//            if($myrow["pr_complete"] == 0 && ($myrow["approval_status"] == AS_APPROVED || $myrow["approval_status"] == AS_ISSUED))
            if(($myrow["approval_status"] == AS_APPROVED || $myrow["approval_status"] == AS_ISSUED  || $myrow["approval_status"] == AS_CLOSED))
            {
                $url = "<a href='$path_to_root/purchasing/po_receive_items.php?PONumber=".$po_no."&PRNumber=".$myrow["pr_no"]."'>" . "Receive" . "</a>";
                label_cell($url, "align=center");
            }
            else{
                label_cell('');
            }

            
        end_row();
        $j++;
        If ($j == 12)
        {
            $j = 1;
            table_header($th);
        }
    }
    end_table(1);

    br();
    
    if ($_SESSION["wa_current_user"]->can_access('SA_GRNS_WITHOUT_PRS')){
        display_heading2("Receive Goods without Purchase Requisitions");
        display_note("If you receive goods without Purchase Requisitions, <b>following will be the consequences</b>.");
        display_note("Goods will be received in to the store (stock) and will NOT be issued to the project automatically.");
        display_note("You will need to create a manual Purchase Requisition Issue to issue materials to the project and charge the project on Purchase Order prices.");

        hyperlink_params("$path_to_root/purchasing/po_receive_items.php", _("Recive without Purchase Requisitions"), "PONumber=".$po_no."&PRNumber=-1");
    }
}
else{
    if ($_SESSION["wa_current_user"]->can_access('SA_GRNS_WITHOUT_PRS')){
        display_note("No Direct Purchase Requisitions for selected Purchase Order. Navigating to GRN screen.");
        meta_forward("$path_to_root/purchasing/po_receive_items.php", "PONumber=".$po_no);
    }
    else{
        br(2);
        $m = ("No outstanding Purchase Requisitions for selected Purchase Order. You do not have permission to receive items without a Purchase Requisition.");
        display_note("<font color='red'>".$m."</font>");
    }
}


div_end();
//submit_center('UpdateData', _("Select"), true, false, 'default');

br();
    
if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}
?>
