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

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
//include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/purchasing/includes/db/purchase_requisitions_db.inc");


$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 = "Reopen Purchase Requisition Line"), false, false, "", $js);
    
if (user_use_date_picker()) {
    
        $js .= get_js_date_picker();
}


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

check_db_has_projects(_("There are no projects defined in the system."));

simple_page_mode(true);
//---------------------------------------------------------------------------------------------------
$input_error = 0;

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


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

//set_global_stock_item($_POST['project_id']);
function clear_site_data()
{
	unset($_POST['site_name']);
	unset($_POST['project_site_status_code']);
	unset($_POST['contact_no']);
	unset($_POST['comment']);
	unset($_POST['inactive']);
	
}

function can_commit($Mode)
{
//    if($Mode=='ADD_ITEM'){
//        if(!(get_post('project_id') > 0)){
//            display_error("Please select a project.");
//            set_focus('project_id');
//            return false;
//        }
//    }
    

    return true;
    
}
//----------------------------------------------------------------------------------------------------


if (isset($_POST['reopen_pr_line'])) 
{
    if(can_commit($Mode)){
        reopen_pr_line($selected_id);
        clear_site_data();
        $msg = _("Purchase Requisition line $selected_id has been reopened for ordering.");
        
        display_notification($msg);
        $Mode = 'RESET';
    }
}
//------------------------------------------------------------------------------------------------------

//if ($Mode == 'Delete')
//{
//	//the link to delete a selected record was clicked
//	delete_site($_POST['project_id'], $selected_id);
//	display_notification(_("The selected task has been deleted."));
//	$Mode = 'RESET';
//}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

$Ajax->activate("pr_details");
//--------------------------------------------------------------------------------------------------------
//$tasks = get_task($_POST['project_id']);

if (!@$_GET['popup'])
	start_form();

start_table(TABLESTYLE_NOBORDER);
start_row();
ref_cells(_("PR No:"), 'pr_no', '',null, '', false);
submit_cells('SearchOrders', _("Search"),'',_('Select documents'), 'default');
end_row();
end_table();


br();

$requisition_number = 0;
if (isset($_POST['pr_no']))
{
	$requisition_number = $_POST['pr_no'];
}

$req_date_from = INFINITE_START_DATE;
$req_date_to = INFINITE_END_DATE;
$project = 0;
$supplier = 0;
$po_no = 0;

$pra_q = get_pr_allocation_lines_of_pr($requisition_number);

div_start('pr_details');

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

$th = array(_("PR#"), "PR Line Id", _("Project"), _("Location"), _("Item"), _("Unit"),
                _("Req. Qty"), _("Required Date"), _("Supplier"), _("Ord. Location"),
                _("Ord. Unit"), _("Ord. Qty"), _("Ord. Price"), _("Ord. Dis."),
                _("PO No."), "Alloc. Id","Line Status", "");

table_header($th);

$k = 0; //row colour counter

$calculated = false;

while ($myrow = db_fetch($pra_q))
{
    alt_table_row_color($k);

    label_cell(get_supplier_trans_view_str(ST_PURCH_REQ, $myrow["pr_no"]));
    label_cell($myrow["pr_line_id"]);
    label_cell($myrow["project_description"]);
    label_cell($myrow["loc_name_req"]);
    label_cell($myrow["item_name"]);
    label_cell($myrow["unit_name"]);
    label_cell($myrow["requested_unit_qty"]);
    label_cell($myrow["required_date"]);
    label_cell($myrow["supplier_name"]);
    label_cell($myrow["location_name"]);
    label_cell($myrow["order_unit"]);
    label_cell($myrow["order_quantity"]);
    label_cell($myrow["order_unit_price"]);
    label_cell($myrow["order_unit_price_discount"]);
    label_cell($myrow["order_no"]);
    label_cell($myrow["pr_po_line_id"]);
    
    switch ($myrow["line_complete"]) {
        case 150:
            global $approval_statuses;
            $line_status = $approval_statuses[$myrow["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");
    

    edit_button_cell("Edit".$myrow['pr_line_id'], _("Edit"));

   // delete_button_cell("Delete".$myrow['id'], _("Delete"));
    end_row();
}

end_table();

div_end();
//------------------------------------------------------------------------------------------------

echo "<br>";

hidden('selected_id', $selected_id);

div_start('price_details');

br();


if ($Mode =='Edit' && $selected_id > 0){
	
    $myrow = get_pr_detail_info($selected_id);
    start_table(TABLESTYLE2);
    
    label_row("PR Line Id:", $myrow['id']);
    label_row("Itemcode:", $myrow['item_code']);
    
    if(strlen($myrow['item_pr_description'] > 0)){
        label_row("PR Item Description:", $myrow['item_pr_description']);
    }
    else{
        label_row("Item Description:", $myrow['item_description']);
    }
    label_row(_("Requested Unit:"), $myrow['unit_name']);
    label_row(_("Requested Qty:"), $myrow['requested_unit_qty']);
    label_row(_("Required Date:"), $myrow['required_date']);
    label_row(_("Total Qty Ordered:"), $myrow['quantity_ordered']. " (".$myrow['base_unit_name'].")");

    end_table(1);
    
    start_table();

        submit("reopen_pr_line", "Reopen PR Line" );

    end_table(1);
    br();
   // submit_add_or_update_center($selected_id == -1, '', 'both');
}

div_end();

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