<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_REOPEN_PURCH_ORDERS';
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/inventory.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/payroll/includes/payroll_db.inc");

include_once($path_to_root . "/purchasing/includes/db/po_reopen_db.inc"); 
include_once($path_to_root . "/purchasing/includes/db/suppalloc_db.inc"); 
include_once($path_to_root . "/jobq/includes/jobq_requests.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 Orders"), false, false, "", $js);


//----------------------------------------------------------------------------------------
simple_page_mode(true);


if (isset($_GET['AddedID'])) 
{
	$trans_no = $_GET['AddedID'];
	$trans_type = ST_PURCHORDER;
        
    display_notification_centered("Purchase order $trans_no has been reopened");
        
//	display_note(get_trans_view_str($trans_type, $trans_no, _("&View this payroll")), 0, 1);
//
//	display_note(print_document_link($trans_no, _("&Print This Payslips"), true, $trans_type), 0, 0);

	display_footer_exit();
}

function can_reopen_po($order_no)
{
    $result = get_pos_for_reopen($order_no);
    $po = db_fetch($result);
    if(!$po){
        $msg = "Purchase order $order_no has been reopened or goods recieving started by another user";
        display_error($msg);
        return false;
    }
    
    $msg = check_transaction_allocated(0, 0, ST_PURCHORDER, $order_no);
    if($msg["success"] === true){
        $msg = "Purchase order $order_no has payment allocations. Please remove the payment allocations first.";
        display_error($msg);
        return false;
    }
    
    return true;
}


function reopen_approved_po($po_no){
        
    $po_header = get_po_header($po_no);
    
    if(!can_reopen_po($po_header['order_no'])){
        return;
    }
    
    $current_user_id = $_SESSION['wa_current_user']->user;
    
    $rad_msg = reopen_approved_document(ST_PURCHORDER, $po_no, $po_header['project_id'], 
                        $po_header['next_approval_group_id'], $current_user_id, $po_header['row_state'],
                        $po_header['created_by']);
    
    if($rad_msg['success'] !== true){
        display_error($rad_msg['message']);
    }
    else{
        meta_forward($_SERVER['PHP_SELF'], "AddedID=$po_no");
    }
}

if(isset($_POST['reopen_po'])){

    reopen_approved_po($selected_id);
    
    $selected_id = -1;
}
//--------------------------------------------------------------------------------------------------

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


br();	

   // $result = get_payroll_runs();
    $result = get_pos_for_reopen(); 
    div_start('price_table');
    
    if (db_num_rows($result) == 0)
    {
    	display_note(_("There are no payroll runs defined."));
    }
    else
    {
        start_table(TABLESTYLE, "width=80%");

		$th = array(_("Id"), _("Reference"), _("Supplier"),
                        _("Location"), _("Supplier's Reference"),
                        _("Order Date"),_("Currency"),
                        _("Order Total"),"");

        table_header($th);

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

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

            label_cell(get_supplier_trans_view_str(ST_PURCHORDER, $myrow["order_no"]));
            label_cell(get_supplier_trans_view_str(ST_PURCHORDER, $myrow["reference"])); 
            label_cell($myrow["supp_name"]);
            label_cell($myrow["location_name"], "align=center");
            label_cell($myrow["requisition_no"], "align=center");
            label_cell(sql2date($myrow["ord_date"]), "align=center");
            label_cell($myrow["curr_code"], "align=center");
 
            label_cell(number_format($myrow["OrderValue"], 2), "align=right");

            //label_cell("&nbsp"."&nbsp"."&nbsp"."&nbsp"."&nbsp");

            select_button_cell("Edit".$myrow['order_no'], _("Select"), _("Select this PO to reopen"));

            //label_cell("&nbsp"."&nbsp"."&nbsp"."&nbsp"."&nbsp");
                                

            end_row();

            $j++;
            If ($j == 25)
            {
            	$j = 1;
        	table_header($th);
            } //end of page full new headings
        } //end of while loop

        end_table();
    }
    div_end();

//-----------------------------------------------------------------------------------------------
    $_POST['order_no'] = 0;
    $_POST['reference'] = "";
    $_POST['supp_name'] = "";
    $_POST['location_name'] = "";
    $_POST['requisition_no'] = 0;
    $_POST['ord_date'] = 0;
    $_POST['curr_code'] = 0;
    
$dec2 = 6;
if ($Mode =='Edit' && $selected_id > 0)
{
    $myrow = get_po_header($selected_id);

    $_POST['order_no'] = $myrow["order_no"];
    $_POST['reference'] = $myrow["reference"];
    $_POST['supp_name'] = $myrow["supp_name"];
    $_POST['location_name'] = $myrow["location_name"];
    $_POST['requisition_no'] = $myrow["requisition_no"];
    $_POST['ord_date'] = sql2date($myrow["ord_date"]);
    $_POST['curr_code'] = $myrow["curr_code"];
    
    hidden('id', $_POST['order_no']); 
    
}

br();

hidden('selected_id', $selected_id);

if($selected_id > 0){
    start_table(TABLESTYLE2);
    
    label_row("Id:", get_supplier_trans_view_str(ST_PURCHORDER, $_POST['order_no']));
    label_row("Reference:", get_supplier_trans_view_str(ST_PURCHORDER, $_POST['reference'])); 
    label_row(_("Supplier Name:"), $_POST['supp_name']);
    label_row(_("Location:"), $_POST['location_name']);
    label_row(_("Order Date:"), $_POST['ord_date']);
    label_row(_("Currency:"), $_POST['curr_code']);

    end_table(1);
    
    submit("reopen_po", "Re Open Purchase Order" );


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


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