<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_IMPORT_PURCHASE_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/data_checks.inc");
include_once($path_to_root . "/includes/approval_functions.inc"); 
include_once($path_to_root . "/reporting/includes/reporting.inc");

include_once($path_to_root . "/includes/db/approval_functions_db.inc");
include_once($path_to_root . "/imports/includes/db/import_purchase_orders_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");

include_once($path_to_root . "/includes/references.inc");
include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
include_once($path_to_root . "/imports/includes/db/import_payment_terms_db.inc"); 
include_once($path_to_root . "/imports/includes/ui/import_purchase_orders_ui.inc"); 



        
$js = '';
if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}


if(isset($_GET['NewOrder'])){
    $_POST['NewOrder'] = $_GET['NewOrder'];
    hidden('new_order', $_GET['NewOrder']);
}


global $Ajax;

$order_no = -1;
 
if (isset($_GET['SCPO_No']))
{
    $_POST['SCPO_No'] = $_GET['SCPO_No'];
    set_session_variable("ST_PURCH_ORDER_no", $order_no);
}


if (isset($_GET['NewOrder']))
{
    $_POST['SCPO_No'] = -1;
    set_session_variable("ST_PURCH_ORDER_no", $_POST['SCPO_No']);
} 

if (isset($_POST['SCPO_No']) && $_POST['SCPO_No'] > 0){
    $order_no = $_POST['SCPO_No'];
}
else{
    if(!isset($_POST['SCPO_No']) > 0){
        $_POST['SCPO_No'] = -1;
        set_session_variable("ST_PURCH_ORDER_no", $_POST['SCPO_No']);
    }else{
        $order_no = get_session_variable('ST_PURCH_ORDER_no');
    }
}

//---------------------------------------------------------------------------------------------------
$help_context = $order_no > 0 ? "Edit Import Purchase Order ".$order_no : "New Import Purchase Order";
page(_($help_context), false, false, "", $js);

simple_page_mode(true);

check_db_has_suppliers(_("There are no suppliers defined in the system."));

check_db_has_purchasable_items(_("There are no purchasable inventory items defined in the system."));

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



if (isset($_GET['Processed_SCPO_No'])) 
{
	//return;
        
    $trans_no = $_GET['Processed_SCPO_No'];
    $trans_type = ST_UTILITY_BILLS;
    $message = get_session_variable('ST_PURCH_ORDER_messages');
    clear_session_variable('ST_PURCH_ORDER_messages');
    
	display_notification_centered($message);

	hyperlink_no_params("$path_to_root/projects/subcontractor_purchase_orders.php", _("Create another &Subcontractor Purchase Order"));
        
        hyperlink_no_params("$path_to_root/projects/inquiry/subcontractor_purchase_orders_inquiry.php", _("Edit another &Subcontractor Purchase Order"));
        
        hyperlink_no_params("$path_to_root/projects/inquiry/approve_subcontractor_purchase_order_inquiry.php", _("Approve &Subcontractor Purchase Orders"));
       
        display_note(print_document_link($trans_no, _("&Print this Project Purchase Order"), true, $trans_type), 0, 0);

//	hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");

	display_footer_exit();
}




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

function line_start_focus() {
  global $approval_statuses, $Ajax;

  $Ajax->activate('items_table');
  set_focus('_stock_id_edit');
}
//--------------------------------------------------------------------------------------------------

function unset_form_variables() {
    unset($_POST['item_code']);
    unset($_POST['qty']);
    unset($_POST['price']);
    unset($_POST['order_unit_price']);
    unset($_POST['delivery_date']);
    unset($_POST['project_site_id']);
    unset($_POST['order_unit_price_discount']);
    unset($_POST['minimum_price']);
    unset($_POST['minimum_price_discount']);
    unset($_POST['min_price_supplier_id']);
}

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


if(isset($_POST['Update_Release_PO']))
{
    release_import_po_for_approval($order_no);
}


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

    send_import_po_back_to_originator($order_no);        
} 

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

    reject_import_po($order_no);        
}


if(isset($_POST['Save_SC_Purchase_Order_Header'])){
        
    save_import_po_header($order_no, $Mode);

}


if(isset($_POST['AddItem']) || isset($_POST['UpdateItem'])){
        
    $msg = save_import_po($order_no,$selected_id);
    
    if($msg['success'] !== true){
        hidden('selected_id', $selected_id);
        hidden('previous_selected_id', $selected_id);
        display_error($msg['message']);
    }
    else{
        $_POST['SCPO_No'] = $msg['trans_no'];
        display_notification($msg['message']);
        display_notification("Taxes have been recalculated and updated.");
        $Mode = 'RESET';
    }

}


if(isset($_POST['Save_Tax'])){
    
    $spo_msg = save_import_po_order_taxes($order_no);
    if($spo_msg['success'] !== true){
        display_error($spo_msg['message']);
    }
    else{
        $_POST['SCPO_No'] = $order_no;
        display_notification($spo_msg['message']);
        $Mode = 'RESET';
    }
}


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

    $selected_id = -1;
}


if (isset($_POST['cancel'])){
    $Mode = 'RESET';
    $selected_id = -1;
    unset($_POST);
}  



if ($Mode == 'Delete')
{
    if(can_delete_purchase_order_details($selected_id)){
        $recs = delete_purchase_order_details($selected_id);
        if($recs == 1){
//            update_sc_purchase_order_taxes($order_no, $_POST['tax_group_id'], $_POST['tax_included']);
            $_POST['SCPO_No'] = $order_no;

            $Mode = 'RESET';
            display_notification(_("Import purchase order has been deleted $selected_id."));
        }
        else{
            display_error(_("Import purchase order could not be deleted $selected_id."));
        }
    }
    
   $Ajax->activate('po_table');
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
}
    $Ajax->activate('_page_body');
    $Ajax->activate('billing_items');
//--------------------------------------------------------------------------------------------------------------
if (!@$_GET['popup'])
start_form();


display_import_purchase_order_header($order_no);   

if($order_no > 0){
    submit_center_first('Save_SC_Purchase_Order_Header', _("Save"), '', 'default');
//    br(2);
//    submit_center_first('Update_Release_PO', "Release for Approval", '', 'default');
//    submit('Back_To_Originator_PO', 'Back To Originator', true, '', 'default');
//    submit_center_last('Reject_PO', "Reject", '', 'default');	
}

br(1);
     
          
display_purchase_order_details($selected_id, $order_no);

display_import_purchase_order_summary($order_no);
   
//end_table(1);

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

//display_sc_purchase_orders_edit_controls($selected_id, $Mode);


//if($selected_id > 0){
//    submit_center_first('Update_SC_purchase_orders', _("Update"), '', 'default');
//    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
//}
//else{        
//    submit_center_first('Add_SC_purchase_orders', _("ADD"), '', 'default');
//    submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
//}

br();

 
if($order_no > 0){
//    display_tax_edit_controls($order_no);
    
//    submit_center_first('Save_Tax', _("Save Taxes"), '', 'default');
//    
//    br();
}
 

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



?>