$ts_period = date('YmdHis', $timestamp);
 if ($ts_counter_key != $ts_period) {
     $ts_counter_key = $ts_period;
     $ts_counter = 0;
 }
 $ts_counter++;
 $xml_content = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
 $xml_content .= writeXMLopen('ottopartner', 0, 'xmlns="http://www.ottogroupb2b.com/ottopartner"');
 $xml_content .= writeXMLline('LKZ', $temp_sp['jng_id'], 1);
 $xml_content .= writeXMLopen('OrderStates', 1);
 $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');
     $xml_content .= writeXMLline('NECKERMANN_CUSTOMER_ID', $o['customer_billing_id'], 4);
     $xml_content .= writeXMLclose('ORDER_SELLTO', 3);
     //HEADER close
     $xml_content .= writeXMLclose('HEADER', 2);
     $items = $class_jo->retrieveItems($o['jng_sp_orders_id']);
     //XML PER ITEM
     foreach ($items as $i) {
         //ITEMS
         $xml_content .= writeXMLopen('ITEMS', 2);
         //Article Data
         $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;
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($sp_detail, $p, $uselength = false)
{
    global $deleted_eans, $deleted_eans_reported, $jng_sp_id;
    $result = '';
    if (!in_array($p['products_ean'], $deleted_eans_reported)) {
        //only delete product if it is killed
        if ($p['products_active_status'] != '1') {
            $deleted_eans[] = $p['products_ean'];
        } else {
            //kill article if it's deactivated
            if (isset($p['article_status']) && $p['article_status'] == '0') {
                $deleted_eans[] = $p['products_ean'];
            }
        }
        //XML ARTICLES
        $result .= writeXMLopen('SKU', 3);
        if ($p['length'] != '' && $p['length'] != '0') {
            $result .= writeXMLline('Dimension2', textLength($p['length']) . ' Länge', 4);
        }
        $date_inv_start = ottodeDateFormat(date('Y-m-d H:i:s', strtotime($p['date_added'])));
        if ($p['active_status'] == '1' && (!isset($p['article_status']) || $p['article_status'] == '1')) {
            $date_inv_end_time = strtotime('+10 years');
        } else {
            //deactivate products
            $date_inv_end_time = strtotime('+3 days');
        }
        $date_inv_end = ottodeDateFormat(date('Y-m-d H:i:s', $date_inv_end_time));
        $date_updated = ottodeDateFormat(date('Y-m-d H:i:s'));
        $price_old = $p['price_old'] == 0 ? $p['price'] : $p['price_old'];
        $result .= writeXMLline('InvoicingStartDate', $date_inv_start, 4);
        $result .= writeXMLline('InvoicingEndDate', $date_inv_end, 4);
        $result .= writeXMLline('SellingPrice', $p['price'], 4);
        $result .= writeXMLline('SellingPriceOld', $price_old, 4);
        $result .= writeXMLline('BuyingPrice', getPurchasePrice($p['price'], $jng_sp_id, $sp_detail), 4);
        $size = $p['length'] != '' && $p['length'] != '0' ? textLength($p['length'], false) : 'none';
        $result .= writeXMLline('Size', $size, 4);
        $result .= writeXMLline('EAN', $p['products_ean'], 4);
        //In latest Warengruppen it seems that we can only use "neutral"
        //This means we can not use the existing gender information and
        //use a static value "neutral" instad. Updated by sahat on 02.07.2014
        $result .= writeXMLline('Targetgroup', 'neutral', 4);
        //$result .= writeXMLline('ExpressDelivery', $p[''], 4);
        $result .= writeXMLclose('SKU', 3);
    }
    return $result;
}
             }
             fwrite($file, $xml_content);
         }
         $total_products++;
         if ($product_has_stock) {
             $total_products_hasStock++;
         }
     }
 }
 foreach ($articles_manual as $pid => $alist) {
     $xml_content = '';
     foreach ($alist as $a) {
         $xml_content .= writeXMLopen('InventoryUpdate', 1);
         $xml_content .= writeXMLline('ItemNo', $pid, 2);
         $xml_content .= writeXMLline('Size', $a, 2);
         $xml_content .= writeXMLopen('Inventory', 2);
         $xml_content .= writeXMLline('ModificationDate', $export_date, 3);
         $xml_content .= writeXMLline('Count', '0', 3);
         $xml_content .= writeXMLclose('Inventory', 2);
         $xml_content .= writeXMLclose('InventoryUpdate', 1);
     }
     fwrite($file, $xml_content);
 }
 $xml_content = writeXMLclose('Inventories');
 $xml_content .= writeXMLclose('ottopartner');
 $success = fwrite($file, $xml_content);
 fclose($file);
 $logger->write($xml_filename);
 if ($success) {
     $backup_file = SP_OTTODE_PATH . 'cat-backup/inventories.zip';
     if (is_file($backup_file)) {
 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;
 }