if ($_POST['me_action'] == 'LOADUNSUBMITTEDORDERS') {
     $orders = $class_payone->newSPorders($jng_sp_id);
     $result = $class_payone->drawListTable($orders);
 } elseif ($_POST['me_action'] == 'AUTHORIZATION') {
     $process_method = 'authorization';
     $order_id = tep_db_prepare_input($_POST['orders_id']);
     $order = $class_jo->retrieveDetail($order_id);
     $items = $class_jo->retrieveItems($order_id);
     $cust = $class_jcust->retrieveDetail(null, $order['jng_sp_id'], $order['customer_billing_id']);
     foreach ($items as $key => $i) {
         if ($class_jo->statusIsCancelled($i['status'])) {
             unset($items[$key]);
         }
     }
     $payi = new payone_invoice();
     $payi->createFromOrderSP($order, $items);
     if (count($order) == 0 || count($items) == 0 || $payi->detail['invoice_complete_status'] != '0') {
         $result = "{$order_id},1";
     } else {
         //--PREPARE PARAMETERS
         $request = array();
         //--STANDARD PARAMETERS
         $request['mid'] = PAYONE_MERCHANT_ID;
         $request['portalid'] = PAYONE_PORTAL_ID;
         $request['key'] = md5(PAYONE_PORTAL_KEY);
         $request['mode'] = PAYONE_MODE;
         $request['request'] = $process_method;
         $request['encoding'] = 'UTF-8';
         //--ORDER INFO
         $request['aid'] = PAYONE_SUBACCOUNT_ID_OTTODE;
         $request['clearingtype'] = PAYONE_PAYMENT_METHOD_INVOICE;
 }
 if (count($items) > 0) {
     $order = $class_jo->retrieveDetail($order_id);
     $display_counter = date('d. M', strtotime($order['order_date'])) . ' - No. ' . sprintf("%003s", $order['daily_count']);
     $shipping_name = $order['customer_shipping_firstname'] . ' ' . $order['customer_shipping_lastname'];
     $shipping_address = $order['customer_shipping_address'];
     $cod_value = 0;
     switch ($order['jng_sp_id']) {
         case '1':
             //NM.DE
             $reference = $order['shipment_id'];
             break;
         case '2':
             //OT.DE
             $invoice = new payone_invoice();
             $invoice->createFromOrderSP($order, $items);
             if ($order['shipment_id'] != $invoice->detail['invoice_no']) {
                 $sda_jo = array('shipment_id' => $invoice->detail['invoice_no']);
                 tep_db_perform('jng_sp_orders', $sda_jo, 'update', "jng_sp_orders_id={$order['jng_sp_orders_id']}");
                 $order['shipment_id'] = $invoice->detail['invoice_no'];
             }
             $reference = $order['shipment_id'];
             $cod_value = $order['payment_method'] == 'CASH_ON_DELIVERY' ? number_format($invoice->detail['invoice_amount'], 2, ',', '') : 0;
             break;
     }
     if ($order['customer_shipping_address2'] != '') {
         $shipping_address .= ' ' . $order['customer_shipping_address2'];
     }
     $cols = array();
     $cols[] = $package['jng_sp_packages_id'];
     $cols[] = $package['package_code'];
         $cust = $class_jcust->retrieveDetail(null, $order['jng_sp_id'], $order['customer_billing_id']);
     }
 } else {
     $order = $class_o->retrieveDetail($order_id, true);
     $items = $class_o->retrieveProducts($order_id, true);
     //            $cust = $class_jcust->retrieveDetail(null, $order['jng_sp_id'], $order['customer_billing_id']);
 }
 //EXCLUDE ITEMS THAT CANCELLED
 foreach ($items as $key => $i) {
     if (!in_array($i['jng_sp_orders_items_id'], $arr_joid)) {
         unset($items[$key]);
     }
 }
 if ($is_sp) {
     if (strtoupper($order["payment_method"]) == strtoupper(PAYMENT_METHOD_PREPAYMENT)) {
         $payi->createFromOrderSP($order, $items, '+1 month');
     } else {
         $payi->createFromOrderSP($order, $items);
     }
 } else {
     if ($order["payment_method"] == PAYMENT_METHOD_BANK_TRANSFER) {
         $payi->createFromOrderJNG($order, $items, '+1 month');
     } else {
         $payi->createFromOrderJNG($order, $items);
     }
 }
 if (count($order) == 0 || count($items) == 0 || $payi->detail['invoice_complete_status'] != 'R') {
     $result = "{$order_id},1";
 } else {
     //--PREPARE PARAMETERS
     $request = array();
}
if (count($orders_array) > 0) {
    foreach ($orders_array as $order_id) {
        $pi_create_counter['sp']++;
        $order = $class_jo->retrieveDetail($order_id);
        $items = $class_jo->retrieveItems($order_id);
        //EXCLUDE CANCELLED ITEMS
        foreach ($items as $key => $item) {
            if ($class_jo->statusIsCancelled($item['status'])) {
                unset($items[$key]);
                break 1;
            }
        }
        //echo "<pre>";var_dump($order);die();
        //Order Items
        $payi->createFromOrderSP($order, $items, $prepayment_due_date, '0', '0');
        if (tep_db_insert_id() != '') {
            $l_invoice_id = tep_db_insert_id();
            $pi_success_create['sp'][] = $order_id;
            $logger->write("SPO{$order_id} invoice created (id = {$l_invoice_id})");
        } else {
            $pi_failed_create['sp'][] = $order_id;
            $logger->write("SPO{$order_id} invoice failed created");
        }
    }
} else {
    $logger->write("no orders ready");
}
//SEND ALL UNSUBMITTED VORKASSE AND PREPAYMENT INVOICE TO PAYONE
//$process_method = 'authorization';    #CHANGED TO PREAUTH, TO PROVIDE VORKASSE WHEN THERE'S VOUCHER USED AFTER ORDER SUBMITTED TO PAYONE
$process_method = 'preauthorization';