/**
     * Creating order details of order
     * @param $neteven_order
     * @param $id_order
     * @return mixed
     */
    private function createOrderDetails($neteven_order, $id_order)
    {
        global $cookie;
        $date_now = date('Y-m-d H:i:s');
        if (in_array($neteven_order->Status, $this->getValue('t_list_order_status'))) {
            return;
        }
        // If order detail doesn't exist
        if (!($res = Db::getInstance()->getRow('SELECT * FROM `' . _DB_PREFIX_ . 'orders_gateway` WHERE `id_order_neteven` = ' . (int) $neteven_order->OrderID . ' AND `id_order_detail_neteven` = ' . (int) $neteven_order->OrderLineID))) {
            // If product exist
            $ref_temp = $neteven_order->SKU;
            $type_temp = substr($ref_temp, 0, 1);
            $id_p_temp = str_replace($type_temp, '', $ref_temp);
            $where_req = '';
            if ($type_temp == 'D') {
                $where_req = 'pa.`id_product_attribute` = ' . (int) $id_p_temp;
            }
            if ($type_temp == 'P') {
                $where_req = 'p.`id_product` = ' . (int) $id_p_temp;
            }
            if (self::$type_sku == 'reference') {
                $where_req = ' (p.`reference` = "' . pSQL($ref_temp) . '" OR pa.`reference` = "' . pSQL($ref_temp) . '") ';
            }
            if (empty($where_req)) {
                return;
            }
            $res_product = Db::getInstance()->getRow('
					SELECT pl.`name` as name_product, p.`id_product`, pa.`id_product_attribute`, p.`reference` as product_reference, pa.`reference` as product_attribute_reference, p.`weight` as weight, GROUP_CONCAT(CONCAT(agl.`name`," : ",al.`name`) SEPARATOR ", ") as attribute_name
					FROM `' . _DB_PREFIX_ . 'product` p
					INNER JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON(p.`id_product` = pl.`id_product` AND pl.`id_lang` = ' . (int) $this->getValue('id_lang') . ')
					LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute` pa ON (pa.`id_product` = p.`id_product`)
					LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_combination` pac ON (pac.`id_product_attribute`=pa.`id_product_attribute`)
					LEFT JOIN `' . _DB_PREFIX_ . 'attribute` a ON (a.`id_attribute`=pac.`id_attribute`)
					LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (al.`id_attribute`=a.`id_attribute` AND al.`id_lang`=' . (int) $this->getValue('id_lang') . ')
					LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group_lang` agl ON (agl.`id_attribute_group`=a.`id_attribute_group` AND agl.`id_lang`=' . (int) $this->getValue('id_lang') . ')
					WHERE p.`active` = 1 AND ' . $where_req . '
					GROUP BY pa.`id_product_attribute`, p.`id_product`
				');
            if ($res_product) {
                // Get order detail informations
                $product_reference = $res_product['product_reference'];
                $id_product_attribute = 0;
                $name = $res_product['name_product'];
                $control_attribute_product = false;
                if (!empty($res_product['id_product_attribute'])) {
                    $product_reference = $res_product['product_attribute_reference'];
                    $id_product_attribute = $res_product['id_product_attribute'];
                    if (!empty($res_product['attribute_name'])) {
                        $name .= ' - ' . $res_product['attribute_name'];
                    }
                    $control_attribute_product = true;
                }
                // Add product in cart
                $order = new Order($id_order);
                if (!Db::getInstance()->getRow('SELECT `id_cart` FROM `' . _DB_PREFIX_ . 'cart_product` WHERE `id_cart` = ' . (int) $order->id_cart . ' AND `id_product` = ' . (int) $res_product['id_product'] . ' AND `id_product_attribute` = ' . (int) $id_product_attribute)) {
                    Db::getInstance()->Execute('INSERT INTO `' . _DB_PREFIX_ . 'cart_product` (`id_cart`, `id_product`, `id_product_attribute`, `quantity`, `date_add`) VALUES (' . (int) $order->id_cart . ', ' . (int) $res_product['id_product'] . ', ' . (int) $id_product_attribute . ', ' . (int) $neteven_order->Quantity . ', "' . pSQL($date_now) . '")');
                }
                if ($this->time_analyse) {
                    $this->current_time_0 = time();
                    Toolbox::displayDebugMessage(self::getL('Order information') . ' : ' . ((int) $this->current_time_0 - (int) $this->current_time_2) . 's');
                }
                // Add order detail
                $tax = new Tax(Configuration::get('PS_TAX'), $cookie->id_lang);
                $price_product = ($neteven_order->Price->_ - floatval($neteven_order->VAT->_)) / $neteven_order->Quantity;
                $order_detail = new OrderDetail();
                $order_detail->id_order = $id_order;
                $order_detail->product_id = $res_product['id_product'];
                $order_detail->product_attribute_id = $id_product_attribute;
                $order_detail->product_name = $name;
                $order_detail->product_quantity = $neteven_order->Quantity;
                $order_detail->product_quantity_in_stock = $neteven_order->Quantity;
                $order_detail->product_quantity_refunded = 0;
                $order_detail->product_quantity_return = 0;
                $order_detail->product_quantity_reinjected = 0;
                $order_detail->product_price = number_format((double) $price_product, 4, '.', '');
                $order_detail->total_price_tax_excl = number_format((double) $price_product, 4, '.', '');
                $order_detail->unit_price_tax_incl = number_format((double) $price_product, 4, '.', '');
                $order_detail->unit_price_tax_excl = $tax->rate ? number_format((double) $price_product / ((double) $tax->rate / 100), 4, '.', '') : $price_product;
                $order_detail->reduction_percent = 0;
                $order_detail->reduction_amount = 0;
                $order_detail->group_reduction = 0;
                $order_detail->product_quantity_discount = 0;
                $order_detail->product_ean13 = NULL;
                $order_detail->product_upc = NULL;
                $order_detail->product_reference = $product_reference;
                $order_detail->product_supplier_reference = NULL;
                $order_detail->product_weight = !empty($res_product['weight']) ? (double) $res_product['weight'] : 0;
                $order_detail->tax_name = $tax->name;
                $order_detail->tax_rate = (double) $tax->rate;
                $order_detail->ecotax = 0;
                $order_detail->ecotax_tax_rate = 0;
                $order_detail->discount_quantity_applied = 0;
                $order_detail->download_hash = '';
                $order_detail->download_nb = 0;
                $order_detail->download_deadline = '0000-00-00 00:00:00';
                $order_detail->id_warehouse = 0;
                if (Configuration::get('PS_SHOP_ENABLE')) {
                    $order_detail->id_shop = (int) Configuration::get('PS_SHOP_DEFAULT');
                }
                if (!$order_detail->add()) {
                    Toolbox::addLogLine(self::getL('Failed for creation of order detail / NetEven Order Id') . ' ' . (int) $neteven_order->OrderID . ' ' . self::getL('NetEven order detail id') . ' ' . $neteven_order->OrderLineID);
                } else {
                    if ($this->time_analyse) {
                        $this->current_time_2 = time();
                        Toolbox::displayDebugMessage(self::getL('Order detail') . ' : ' . ((int) $this->current_time_2 - (int) $this->current_time_0) . 's');
                    }
                    $id_order_detail_temp = $order_detail->id;
                    Toolbox::addLogLine(self::getL('Creation of order detail for NetEven order Id') . ' ' . (int) $neteven_order->OrderID . ' ' . self::getL('NetEven order detail id') . ' ' . (int) $neteven_order->OrderLineID);
                    // Update quantity of product
                    if (class_exists('StockAvailable')) {
                        // Update quantity of product
                        if ($control_attribute_product) {
                            StockAvailable::setQuantity($res_product['id_product'], $id_product_attribute, StockAvailable::getQuantityAvailableByProduct($res_product['id_product'], $id_product_attribute) - $neteven_order->Quantity);
                        } else {
                            StockAvailable::setQuantity($res_product['id_product'], 0, StockAvailable::getQuantityAvailableByProduct($res_product['id_product']) - $neteven_order->Quantity);
                        }
                    } else {
                        $t_info_product = array();
                        $t_info_product['id_product'] = $res_product["id_product"];
                        $t_info_product['cart_quantity'] = $neteven_order->Quantity;
                        $t_info_product['id_product_attribute'] = NULL;
                        if ($control_attribute_product) {
                            $t_info_product['id_product_attribute'] = $id_product_attribute;
                        }
                        Product::updateQuantity($t_info_product);
                    }
                    if ($this->time_analyse) {
                        $this->current_time_0 = time();
                        Toolbox::displayDebugMessage(self::getL('Cart product') . ' : ' . ((int) $this->current_time_0 - (int) $this->current_time_2) . 's');
                    }
                    // Insert order in orders_gateway table
                    if (!Db::getInstance()->Execute('INSERT INTO `' . _DB_PREFIX_ . 'orders_gateway` (`id_order_neteven`, `id_order`, `id_order_detail_neteven`, `date_add`, `date_upd`) VALUES (' . (int) $neteven_order->OrderID . ', ' . (int) $id_order . ', ' . (int) $neteven_order->OrderLineID . ', "' . pSQL($date_now) . '", "' . pSQL($date_now) . '")')) {
                        Toolbox::addLogLine(self::getL('Failed for save export NetEven order Id') . ' ' . (int) $neteven_order->OrderID . ' ' . self::getL('NetEven order detail id') . ' ' . (int) $neteven_order->OrderLineID);
                    } else {
                        Toolbox::addLogLine(self::getL('Save export NetEven order Id') . ' ' . (int) $neteven_order->OrderID . ' ' . self::getL('NetEven order detail id') . ' ' . (int) $neteven_order->OrderLineID);
                    }
                }
            }
        } else {
            Toolbox::addLogLine(self::getL('Failed for creation of order detail of NetEven order Id') . $neteven_order->OrderID . ' ' . self::getL('NetEven order detail id') . ' ' . $neteven_order->OrderLineID . ' ' . self::getL('Product not found SKU') . ' ' . $neteven_order->SKU);
        }
        $order = new Order($id_order);
        $products = $order->getProductsDetail();
        if (count($products) == 0 && $this->getValue('mail_active')) {
            $this->sendDebugMail($this->getValue('mail_list_alert'), self::getL('Order imported is empty'), self::getL('Order Id') . ' ' . (int) $order->id);
        }
    }
 /**
  * On supprime tous les produits qui n'ont pas de code EAN13.
  * @param $t_product
  * @param $is_display
  * @return array
  */
 private function delProductWithoutEAN($products, $is_display)
 {
     return $products;
     $products_with_ean = array();
     foreach ($products as $key => $product) {
         if (!empty($product['id_product_attribute']) && !empty($product['ean13_declinaison']) || empty($product['id_product_attribute']) && !empty($product['ean13'])) {
             $products_with_ean[] = $product;
         } else {
             if ($this->getValue('debug')) {
                 Toolbox::addLogLine(self::getL('No EAN13 for product') . ' : ' . $product['id_product'] . '/' . $product['id_produt_attribute']);
                 Toolbox::displayDebugMessage(self::getL('No EAN13 for product') . ' : ' . $product['id_product'] . '/' . $product['id_produt_attribute']);
             }
         }
     }
     return $products_with_ean;
 }
 public function sendDebugMail($emails, $subject, $message, $classic_mail = false)
 {
     if (!$emails) {
         return;
     }
     foreach ($emails as $email) {
         if (Validate::isEmail($email)) {
             if (!$classic_mail) {
                 $id_lang = $this->id_lang ? (int) $this->id_lang : Configuration::get('PS_LANG_DEFAULT');
                 $shop_email = Configuration::get('PS_SHOP_EMAIL');
                 $shop_name = Configuration::get('PS_SHOP_NAME');
                 Mail::Send($id_lang, 'debug', $subject, array('{message}' => $message), $email, NULL, $shop_email, $shop_name, NULL, NULL, dirname(__FILE__) . '/../mails/');
             } else {
                 mail($email, $subject, $message);
             }
             if ($this->getValue('debug')) {
                 Toolbox::displayDebugMessage(self::getL('Send email to') . ' : ' . $email);
             }
         }
     }
 }