$xml_content .= writeXMLopen('OrderState', 2);
 $xml_content .= writeXMLline('order-no', $o['order_id'], 3);
 $xml_content .= writeXMLopen('product-line-items', 3);
 $items = $class_jo->retrieveItems($o['jng_sp_orders_id']);
 foreach ($items as $i) {
     $xml_content .= writeXMLopen('product-line-item', 4);
     $xml_content .= writeXMLline('position', $i['position'], 5);
     $xml_content .= writeXMLline('order-status', 'entered', 5);
     $xml_content .= writeXMLline('quantity', intval($i['order_quantity']), 5);
     $xml_content .= writeXMLline('ModificationDate', ottodeDateFormat($timestamp), 5);
     $xml_content .= writeXMLclose('product-line-item', 4);
 }
 $xml_content .= writeXMLclose('product-line-items', 3);
 $xml_content .= writeXMLclose('OrderState', 2);
 $xml_content .= writeXMLclose('OrderStates', 1);
 $xml_content .= writeXMLclose('ottopartner', 0);
 //CREATE XML ON SERVER
 $fn_counter = str_pad($ts_counter, 2, '0', STR_PAD_LEFT);
 $xml_filename = $temp_sp['jng_id'] . '_orderstatus_' . $ts_period . $fn_counter . '.xml';
 $xmlfullpath = SP_OTTODE_UPLOAD_PATH_STATES . $xml_filename;
 $file = fopen($xmlfullpath, 'w');
 $success = fwrite($file, $xml_content) !== false;
 fclose($file);
 if ($success) {
     $orders_confirmed++;
     $class_jo->confirmOrders($o['jng_sp_orders_id'], $timestamp);
     $archive_path_raw = createBackupDirectory(SP_OTTODE_ARCHIVE_PATH . 'confirm/');
     $archive_fullpath = $archive_path_raw . $xml_filename;
     while (file_exists($archive_fullpath)) {
         $archive_fullpath .= '_copy';
     }
         $xml_content .= writeXMLopen('ARTICLE_DATA', 3);
         $xml_content .= writeXMLline('A_SUP_ARTNO', $i['article_number'], 4);
         $xml_content .= writeXMLline('NECKERMANN_POS_REF', $i['article_reference_number'], 4);
         $xml_content .= writeXMLclose('ARTICLE_DATA', 3);
         //Quantity
         $xml_content .= writeXMLopen('QUANTITY', 3);
         $xml_content .= writeXMLline('ORDER_QUANTITY', $i['order_quantity'], 4);
         $xml_content .= writeXMLclose('QUANTITY', 3);
         //ITEMS close
         $xml_content .= writeXMLclose('ITEMS', 2);
     }
     //BDATA close
     $xml_content .= writeXMLclose('BDATA', 1);
 }
 //END ORDER LOOPING
 $xml_content .= writeXMLclose('NECKERMANN_BDATA', 0);
 //CREATE XML ON SERVER
 $file = fopen(SP_NECKERMANN_TEMP_FOLDER . $xml_filename, 'w');
 $success = fwrite($file, $xml_content) !== false;
 fclose($file);
 $success_upload = false;
 if ($success) {
     //ENCRYPT XML FILE ON SERVER
     //initialize encryption options n paths
     putenv('GNUPGHOME=' . GNUPGHOME);
     $sp_gpg_id = NECKERMANNGPGID;
     $unencrypted_file = SP_NECKERMANN_TEMP_FOLDER . $xml_filename;
     $encrypted_file = SP_NECKERMANN_TEMP_FOLDER . $xml_filename_encrypted;
     if ($runinlocal) {
         //fake encryption for staging local:
         copy($unencrypted_file, $encrypted_file);
function createXMLCloser($bundle_exist)
{
    //XML CLOSER
    $xml_content = writeXMLclose('Styles', 0);
    if ($bundle_exist) {
        $xml_content .= writeXMLclose('Bundles', 0);
    }
    $xml_content .= '</ottopartner>';
    return $xml_content;
}
function generateXMLproductsLink($product_links, $type)
{
    $result = '';
    $total_links = count($product_links);
    if ($total_links > 0) {
        $pl_sort = 0;
        foreach ($product_links as $plt) {
            $result .= writeXMLopen('P_PRODUCT_LINK', 1, 'type="' . $type . '"');
            $result .= writeXMLline('P_ID', $plt['links_id'], 2);
            if ($total_links > 1) {
                $pl_sort++;
                $result .= writeXMLline('LINK_SORT', $pl_sort, 2);
            }
            $result .= writeXMLclose('P_PRODUCT_LINK', 1);
        }
    }
    return $result;
}
 function generateXMLarticle($article_number, $p)
 {
     global $sp_detail;
     global $products_stock, $hide_extreme_flag, $hide_hard_flag, $hide_nostock_flag;
     $actual_stock = $products_stock[$p['products_id'] . '-' . $p['articles_id']];
     $stock_info = actualStockReport($sp_detail, $p['reported_stock'], $actual_stock, $p['delivery_days'], $p['is_bestselling'], $p['complexity'], $hide_extreme_flag, $hide_hard_flag, $hide_nostock_flag);
     $reported_stock = $p['active_status'] == '0' && $stock_info['stock_isreal'] == '0' ? '0' : $stock_info['stock'];
     //TODO: Remove 4 lines below to reactive bestselling in NM.DE
     //      It is currently deactivate to wait final confirmation
     //      on future partnership with NM.DE (sahat - 20.07.2012)
     //if($p['is_bestselling']=='1') {
     $reported_stock = '0';
     $stock_info['delivery_time'] = '14';
     //}
     //------------------------------------------------------------
     $result = '';
     //XML ARTICLES
     $result .= writeXMLopen('ARTICLEDATA', 1);
     $result .= writeLine();
     //XML ARTICLE DETAIL
     $article_number_used = $article_number;
     if ($p['length'] != '' && $p['length'] != '0') {
         $plength = textLength($p['length'], false);
         $plength = intval($plength);
         if ($plength != 0) {
             $article_number_used .= '_' . $plength;
         }
     }
     if ($p['text'] != '' && $p['text_code'] != '') {
         $article_number_used .= '_' . $p['text_code'];
     }
     $result .= writeXMLline('A_SUP_ARTNO', $article_number_used, 2);
     $result .= writeXMLline('A_EAN', $p['products_ean'], 2);
     $valid_date = date('Y-m-d', strtotime($p['date_added']));
     $result .= writeXMLline('A_VALID_DATE', $valid_date, 2);
     $result .= writeLine();
     //XML ARTICLE STOCK
     //below additional stock is to handle neckermann error about hiding resourcable products when stock is 0
     //if($p['reported_stock']>0) $p['reported_stock'] += 49;
     //thile line above should be deleted once this error is fixed
     $result .= writeXMLline('A_STOCK', $reported_stock, 2);
     $result .= writeLine();
     //XML ARTICLE PRICING
     $result .= writeXMLopen('A_PRICE', 2);
     $result .= writeXMLline('PRICE_VK', $p['price'], 3);
     if ($p['price_old'] > 0 && $p['price_old'] > $p['price']) {
         $result .= writeXMLline('PRICE_VK_OLD', $p['price_old'], 3);
     }
     //$result .= writeXMLline('PRICE_TAX', '2', 3);
     $result .= writeXMLline('PRICE_PROVISION_TYPE', $sp_detail['provision'], 3);
     $result .= writeXMLclose('A_PRICE', 2);
     $result .= writeLine();
     //XML DELIVERY
     //$delivery_time = ($p['delivery_days']==0) ? $sp_detail['delivery_time'] : $p['delivery_days'];
     $delivery_time = $stock_info['delivery_time'];
     if ($reported_stock == 0 || $p['reported_stock'] == 0) {
         $replacement = 'N';
         $delivery_replacement_time = '';
     } else {
         $replacement = 'J';
         $delivery_replacement_time = $delivery_time + 2;
     }
     $result .= writeXMLopen('A_DELIVERY', 2);
     $result .= writeXMLline('DELIVERY_TIME', $delivery_time, 3);
     $result .= writeXMLline('DELIVERY_REPLACEMENT', $replacement, 3);
     $result .= writeXMLline('DELIVERY_REPLACEMENT_TIME', $delivery_replacement_time, 3);
     $result .= writeXMLclose('A_DELIVERY', 2);
     $result .= writeLine();
     $result .= writeXMLclose('ARTICLEDATA', 1);
     $result .= writeLine();
     return $result;
 }