function importOttoB2BOrderFromArray($order, $xml_filename, $import_time)
{
    global $obj_sp, $logger;
    $orders_created = array();
    $jng_id = $order['OttoB2B:SupplierCode']['value'];
    if ($jng_id == $obj_sp->getExternalID()) {
        $order_buyer = $order['OttoB2B:BuyerAddress'];
        $order_billing = $order['OttoB2B:InvoiceAddress'];
        $order_delivery = $order['OttoB2B:DeliveryAddress'];
        //generate shipping name
        $full_shipping_name = $order_delivery['OttoB2B:Name']['value'];
        $name_limit = 32;
        //check if shipping name is too long
        if (strlen($full_shipping_name) <= $name_limit) {
            $shipping_firstname_text = $full_shipping_name;
            $shipping_lastname_text = '';
        } else {
            $logger->write('Shipping name is too long, run auto split');
            $shipping_name_split = explode(' ', $full_shipping_name);
            $shipping_firstname = array();
            $shipping_lastname = array();
            $firstname_full = false;
            $total_name_chars = 0;
            $name_word_counter = 0;
            foreach ($shipping_name_split as $name) {
                $name_word_counter++;
                $name_length = strlen($name);
                //consider space between word as part of $name_length
                if ($name_word_counter > 1) {
                    $name_length += 1;
                }
                //check if first name is already reach the limit
                if ($firstname_full || $total_name_chars + $name_length > $name_limit) {
                    //limit reached, use lastname
                    if (!$firstname_full) {
                        $firstname_full = true;
                    }
                    $shipping_lastname[] = $name;
                } else {
                    //all ok, use firstname
                    $total_name_chars += $name_length;
                    $shipping_firstname[] = $name;
                }
            }
            $shipping_firstname_text = implode(' ', $shipping_firstname);
            $shipping_lastname_text = implode(' ', $shipping_lastname);
            $logger->write('Firstname: ' . $shipping_firstname_text);
            $logger->write('Lastname: ' . $shipping_lastname_text);
        }
        $order_date = translateToMySqlDate($order['OttoB2B:OrderDate']['value']);
        $order_no = $order['OttoB2B:OrderNumber']['value'];
        $customer_billing_id = $order_billing['OttoB2B:Gln']['value'];
        $customer_billing_title = '';
        $customer_billing_firstname = $order_billing['OttoB2B:Name']['value'];
        $customer_billing_lastname = '';
        $customer_billing_address = $order_billing['OttoB2B:Street']['value'];
        $customer_billing_address2 = '';
        $customer_billing_city = $order_billing['OttoB2B:City']['value'];
        $customer_billing_postcode = $order_billing['OttoB2B:Zip']['value'];
        $customer_billing_country = $order_billing['OttoB2B:Country']['value'];
        $customer_billing_phone = $order_buyer['OttoB2B:ContactPhone']['value'];
        $customer_billing_phone2 = '';
        //we decided no need to grab the email address
        //$customer_billing_email = $order_buyer['OttoB2B:ContactEmail']['value'];
        $customer_billing_email = '';
        $customer_shipping_id = $order_delivery['OttoB2B:Gln']['value'];
        $customer_shipping_title = '';
        $customer_shipping_firstname = $shipping_firstname_text;
        $customer_shipping_lastname = $shipping_lastname_text;
        $customer_shipping_address = $order_delivery['OttoB2B:Street']['value'];
        $customer_shipping_address2 = '';
        $customer_shipping_city = $order_delivery['OttoB2B:City']['value'];
        $customer_shipping_postcode = $order_delivery['OttoB2B:Zip']['value'];
        $customer_shipping_country = $order_delivery['OttoB2B:Country']['value'];
        $customer_shipping_phone = '';
        $customer_shipping_phone2 = '';
        $customer_shipping_email = '';
        $expected_delivery_date = translateToMySqlDate($order['OttoB2B:DeliveryDate']['value']);
        $expected_delivery_date_from = 'null';
        $expected_delivery_date_until = 'null';
        $shipping_window_close = $expected_delivery_date;
        $shipping_window_open = date('Y-m-d', strtotime($shipping_window_close . ' -1 week'));
        $total_price = $order['OttoB2B:TotalPrice']['OttoB2B:Value']['value'];
        $shipping_cost = 0;
        if ($order['OttoB2B:TermsOfPayment']['value'] == Order::PAYMENT_METHOD_OTTO_B2B_1) {
            $payment_method = 'ottob2b1';
        } else {
            die('NEW UNKNOWN PAYMENT METHOD! Please report to IT Team');
        }
        $payment_status = 0;
        $item_counter = 0;
        $products_not_found = array();
        $prev_ean = '';
        foreach ($order['OttoB2B:OrderItems']['OttoB2B:Item'] as $item) {
            $item_counter++;
            $item_sizes = array();
            //There are ItemSize with 1 size and multiple sizes
            //Since the array returned by the function xmlToArray is different
            //We have to rebuild them in a new array again below
            if (isset($item['OttoB2B:ItemSizes']['OttoB2B:ItemSize']['OttoB2B:EAN'])) {
                $item_size = $item['OttoB2B:ItemSizes']['OttoB2B:ItemSize'];
                $item_sizes[] = $item_size;
            } else {
                foreach ($item['OttoB2B:ItemSizes']['OttoB2B:ItemSize'] as $item_size) {
                    $item_sizes[] = $item_size;
                }
            }
            //Loop through the multiple sizes
            foreach ($item_sizes as $item_size) {
                $ean = $item_size['OttoB2B:EAN']['value'];
                if ($ean != $prev_ean) {
                    $prev_ean = $ean;
                    $obj_product = Product::loadObjectFromEAN($ean);
                    if (is_null($obj_product->id)) {
                        $products_not_found[] = $ean;
                        $logger->write('PRODUCT NOT FOUND: ' . $ean);
                        continue;
                    }
                    $product_id = $obj_product->id;
                    $article_id = $obj_product->getArticleIDFromEAN($ean);
                }
                $external_item_id = $item['OttoB2B:ItemNumber']['value'];
                $billing_product_code = $item['OttoB2B:SupplierItemDescription']['value'];
                $billing_text = $item['OttoB2B:StyleNumber']['value'];
                $ordered_quantity = $item_size['OttoB2B:Quantity']['OttoB2B:Value']['value'];
                $price = $item_size['OttoB2B:Price']['OttoB2B:Value']['value'];
                $price_after_provision = $price;
                $item_count = 1;
                $item_total = 1;
                $external_item_count = $item_counter;
                $items_created = array();
                for ($qty = 1; $qty <= $ordered_quantity; $qty++) {
                    $shipment_id = $order_no . substr($ean, -6) . str_pad($qty, 4, '0', STR_PAD_LEFT);
                    $order_id = Order::createNewSalesPartnerOrder($obj_sp->id, basename($xml_filename), $jng_id, $order_date, $import_time, $shipment_id, $order_no, $customer_billing_id, $customer_billing_title, $customer_billing_firstname, $customer_billing_lastname, $customer_billing_address, $customer_billing_address2, $customer_billing_city, $customer_billing_postcode, $customer_billing_country, $customer_billing_phone, $customer_billing_phone2, $customer_billing_email, $customer_shipping_id, $customer_shipping_title, $customer_shipping_firstname, $customer_shipping_lastname, $customer_shipping_address, $customer_shipping_address2, $customer_shipping_city, $customer_shipping_postcode, $customer_shipping_country, $customer_shipping_phone, $customer_shipping_phone2, $customer_shipping_email, $expected_delivery_date, $expected_delivery_date_from, $expected_delivery_date_until, $total_price, $shipping_cost, $payment_method, $payment_status);
                    if ($order_id !== false) {
                        $item_id = Order::createNewSalesPartnerOrderItem($order_id, $product_id, $article_id, $ean, $external_item_id, $billing_product_code, $billing_text, 1, $price, $price_after_provision, $item_count, $item_total, $external_item_count, $shipping_window_open, $shipping_window_close);
                        $orders_created[] = $order_id;
                        $items_created[] = $item_id;
                        Order::getOldOrderFunctionSP()->leadtimeSetStart($import_time, null, $order_id);
                        $logger->write("Order {$order_no} EAN {$ean} Qty {$qty} created:" . " SP-{$item_id}");
                    } else {
                        $logger->write("Order {$order_no} EAN {$ean} Qty {$qty} Exist!");
                    }
                }
            }
        }
        if ($order_id !== false) {
            Order::getOldOrderFunctionSPCustomers()->getFromOrder($order_id);
        }
        if (count($orders_created) > 0) {
            Order::getOldOrderFunctionSP()->confirmOrders($orders_created, $import_time);
        }
        $products_not_found_total = count($products_not_found);
        if ($products_not_found_total > 0) {
            echo "{$products_not_found_total} EAN not found:<br />";
            foreach ($products_not_found as $ean) {
                echo "{$ean}<br />";
            }
        }
    } else {
        $logger->write('Order is not for J&G ID, import is aborted!');
    }
    return count($orders_created);
}
         $obj_ws->setCellValue('K' . $row_used, $row['total_qty']);
     }
 }
 //Block 4 - Returned products
 $logger->write('Looping returned orders');
 $q = "SELECT jo.order_id, jo.order_date, joi.article_number" . ", joi.products_ean, SUM(joir.return_quantity) AS total_qty, joi.price" . ", joir.confirm_return_time AS confirm_return" . ", joi.jng_sp_orders_items_id" . " FROM jng_sp_orders jo" . " INNER JOIN jng_sp_orders_items joi" . " ON joi.jng_sp_orders_id = jo.jng_sp_orders_id" . " INNER JOIN jng_sp_orders_items_return joir" . " ON joir.jng_sp_orders_items_id=joi.jng_sp_orders_items_id" . " {$filter_returns}" . " GROUP BY joi.jng_sp_orders_items_id";
 $r = tep_db_query($q);
 $returned_products = array();
 while ($row = tep_db_fetch_array($r)) {
     //$logger->write($row['jng_sp_orders_items_id']);
     if (!isset($rows_item_id[$row['jng_sp_orders_items_id']])) {
         //this row is not exist yet, need to create one
         $row_counter++;
         $rows_item_id[$row['jng_sp_orders_items_id']] = $row_counter;
         $row_used = $row_counter;
         $p = Product::loadObjectFromEAN($row['products_ean']);
         $aid = $p->getArticleIDFromEAN($row['products_ean']);
         addNewRowInOttoCommisionTemplate($obj_ws, $row_used, $row['order_id'], $row['article_number'], $p->getLengthOrSizeAsText($aid), 0, 0, 0, 0, 0, 0, 0, 0, $row['total_qty'] * $row['price'], $row['total_qty']);
     } else {
         //row already exist, just need to update the return values
         $row_used = $rows_item_id[$row['jng_sp_orders_items_id']];
         $obj_ws->setCellValue('N' . $row_used, $row['total_qty'] * $row['price']);
         $obj_ws->setCellValue('O' . $row_used, $row['total_qty']);
     }
 }
 /*
 //Block 5 - Payment Status
 $logger->write('Query payment status');
 $q = "SELECT jo.order_id, jo.order_date, jo.shipment_date" .
     ", pi.invoice_amount AS original_invoice_amount" .
     ", SUM(receipt_amount) AS total_payment" .
use_class('ProductAttribute');
define('ERROR_MESSAGE_NOIMAGE', 'NOIMAGEFOUND');
if (isset($_POST['me_action'])) {
    if ($_POST['me_action'] == 'LOADPACKAGEIMAGE') {
        $product_entry = tep_db_prepare_input($_POST['product_entry']);
        if (USERAGENTISMANOBOCLIENT && $hidemenuscript != '') {
            //limited size in manobo client
            $imgwidth = '380';
            $imgheight = '';
        } else {
            //unlimited size in manobo
            $imgwidth = '';
            $imgheight = '';
        }
        if (strlen($product_entry) == 13) {
            $obj_product = Product::loadObjectFromEAN($product_entry);
        } else {
            $obj_product = Product::loadObjectFromProductCode($product_entry);
            if ($obj_product === false) {
                $obj_product = new Product($product_entry);
            }
        }
        $result = '';
        if ($obj_product === false) {
            $result .= '<h3 class="red">Product <strong>' . $product_entry . '</strong> not found!</h3>';
        } else {
            $result .= '<div style="float:left;">' . $obj_product->displayImage(IMAGE_SIZE_THUMBNAIL_1, IMAGE_SIZE_THUMBNAIL_1_PORTRAIT) . '</div><div style="margin-left:120px;">';
            $attributes = ProductAttribute::getOldStylesFunction()->getProductStyles($obj_product->id);
            $total_images = 0;
            foreach ($attributes[ProductAttribute::GROUP_ID_PACKAGING] as $attribute_id => $attribute_name) {
                $obj_pa = new ProductAttribute($attribute_id);
 function generateLabelHSE($product_id, $article_id, $ean = '')
 {
     $result = array();
     if ($ean != '') {
         $obj_product = Product::loadObjectFromEAN($ean);
         $article_id = $obj_product->getArticleIDFromEAN($ean);
     } else {
         $obj_product = new Product($product_id);
         $ean = $obj_product->getEAN($article_id);
     }
     $sales_partner_id = '12';
     //Use product code from SP (HSE)
     //SP is not yet created, maybe will have a different ID
     $code = $obj_product->getSalesPartnerProductCode($sales_partner_id);
     if ($code == '') {
         $code = 'N/A';
     }
     $name = $obj_product->getSalesPartnerProductName($sales_partner_id);
     if ($name == '') {
         $name = 'No SP Name';
     }
     $length = $obj_product->getLengthOrSizeAsText($article_id);
     //Set label size (in dots)
     $label_width = '340';
     $text_block_width = '300';
     //Set default x and y position
     $posx_start = 20;
     $posy_start = 35;
     //Start setting x and y position to be used and changed dynamically
     //can be set to default size again when needed
     $posx = $posx_start;
     $posy = $posy_start;
     //CONSTRUCT label
     //==> START COMMAND ^XA
     $label = "^XA";
     //==> Change International Font/Encoding ^CI
     //6 = German Character Set
     $label .= "^CI6";
     //==> Media Type
     //^MT
     //D for Direct Thermal (need special media)
     //T for Thermal Transfer (need ribbon)
     $label .= "^MTD";
     //==> Media Darkness
     //^MD
     //0 : 0 (-30 to 30)
     $label .= "^MD0";
     //==> Print Mode
     //^MM
     //T = Tear-off
     $label .= "^MMT";
     //==> Measurement Unit
     //^MU
     //d = dots
     //300 = dpi format
     //300 = dpi conversion
     $label .= "^MUd,300,300";
     //==> Label Home
     //^LH
     //0,0 = x,y axis position (0 - 32000)
     $label .= "^LH0,0";
     //==> Media Tracking
     //^MN
     //Y = non-continues web sensing
     //N = continues
     $label .= "^MNY";
     //$label .= "^MNN";
     //==> Label Length
     //^LL
     //only needed for continues media
     //$label .= "^LL384.5";
     //==> Print Width
     //^PW
     //Specify label width in dots
     $label .= "^PW" . $label_width;
     //==> Set Dots per Milimiter
     //^JM
     //will affect sizes of result that will be printed on label
     //A = 24 dots/mm, 12 dots/mm, 8 dots/mm or 6 dots/mm
     //B = 12 dots/mm,  6 dots/mm, 4 dots/mm or 3 dots/mm
     $label .= "^JMA";
     //==> Field Origin
     //^FO
     //x axis in dots
     //y axis in dots
     //z = justification (0 = left, 1 = right, 2 = auto)
     $label .= "^FO{$posx},{$posy}";
     //==> Set Scalable/Bitmapped Font
     //^A
     //font name : A-Z, 0-9 (saved in printer memory)
     //orientation : N = normal, R = 90CW, I = 180, B = 270
     //character height in dots
     //width in dots
     $label .= "^A0N,35,35";
     //==> Field Block
     //^FB
     //set field data to be printed as a block which can be aligned
     //width in dots
     //maximum lines : 1 to 9999
     //line spaces : -9999 to 9999
     //alignment : L = Left, C = Center, R = Right, J = Justified
     //hanging indent : 0 to 9999
     $label .= '^FB' . $text_block_width . ',1,0,C,0';
     //==> Field Data (set barcode value)
     //^FD ... ^FS
     //Data to print (in this case Product Code)
     $label .= '^FD' . 'HSE 24' . '^FS';
     $posy += 38;
     //==> Field Origin
     //^FO
     //x axis in dots
     //y axis in dots
     //z = justification (0 = left, 1 = right, 2 = auto)
     $label .= "^FO{$posx},{$posy}";
     //==> Set Scalable/Bitmapped Font
     //^A
     //font name : A-Z, 0-9 (saved in printer memory)
     //orientation : N = normal, R = 90CW, I = 180, B = 270
     //character height in dots
     //width in dots
     $label .= "^A0N,60,60";
     //==> Field Block
     //^FB
     //set field data to be printed as a block which can be aligned
     //width in dots
     //maximum lines : 1 to 9999
     //line spaces : -9999 to 9999
     //alignment : L = Left, C = Center, R = Right, J = Justified
     //hanging indent : 0 to 9999
     $label .= '^FB' . $text_block_width . ',1,0,C,0';
     //==> Field Data (set barcode value)
     //^FD ... ^FS
     //Data to print (in this case Product Code)
     $label .= '^FD' . $code . '^FS';
     $posy += 50;
     $posx_barcode = 35;
     //==> Bar Code Field Default
     //^BY
     //could make barcode on smallest size
     //width : 1 - 10
     //ratio : 2.0 - 3.0
     //height : in dots
     $label .= "^BY2";
     //==> Field Origin
     //^FO
     //x axis in dots
     //y axis in dots
     //z = justification (0 = left, 1 = right, 2 = auto)
     $label .= "^FO\${$posx_barcode},{$posy}";
     //==> Set Barcode Type and Format
     //^B
     //C = Code 128
     //orientation : N = normal, R = 90CW, I = 180, B = 270
     //height in dots
     //print interpretation : Y = Yes, N = No
     //print interpretation above : Y = Yes, N = No (print below)
     $label .= "^BCN,80,N,N";
     //==> Field Data (set barcode value)
     //^FD ... ^FS
     //Data to print (in this case EAN Code)
     $label .= '^FD' . $code . '^FS';
     $posy += 85;
     //==> Field Origin
     //^FO
     //x axis in dots
     //y axis in dots
     //z = justification (0 = left, 1 = right, 2 = auto)
     $label .= "^FO{$posx},{$posy}";
     //==> Set Scalable/Bitmapped Font
     //^A
     //font name : A-Z, 0-9 (saved in printer memory)
     //orientation : N = normal, R = 90CW, I = 180, B = 270
     //character height in dots
     //width in dots
     $label .= "^A0N,30,30";
     //==> Field Block
     //^FB
     //set field data to be printed as a block which can be aligned
     //width in dots
     //maximum lines : 1 to 9999
     //line spaces : -9999 to 9999
     //alignment : L = Left, C = Center, R = Right, J = Justified
     //hanging indent : 0 to 9999
     $label .= '^FB' . $text_block_width . ',2,0,C,0';
     //==> Field Data (set barcode value)
     //^FD ... ^FS
     //Data to print (in this case the Article Length)
     $label .= '^FH^FD' . $this->convertToGermanHex($name) . '^FS';
     //$label .= '^FD' . $name . ' ' . $length . '^FS';
     //END COMMAND ^XZ
     $label .= "^XZ";
     $result['barcode_type'] = 'label_product_hse';
     $result['label'] = $label;
     $result['label_name'] = "{$ean}.data";
     return $result;
 }