Current File : /var/www/vinorea/modules/ipexportimport/classes/import/order/EIAProduct.php
<?php
/**
 *
 * NOTICE OF LICENSE
 *
 *  @author    SmartPresta <tehran.alishov@gmail.com>
 *  @copyright 2024 SmartPresta
 *  @license   Commercial License
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

class EIAProduct
{

    public $id_shop;
    public $id_lang;
    public $id_order;
    public $identify_product;
    public $identify_combination;
    public $id_product;
    public $id_product_attribute;
    public $reference;
    public $ean13;
    public $upc;
    public $mpn;
    public $combination_reference;
    public $combination_ean;
    public $combination_upc;
    public $combination_mpn;
    public $combination_isbn;
    public $name;
    public $quantity;
    public $quantity_in_stock;
    public $quantity_refunded;
    public $quantity_return;
    public $quantity_reinjected;
    public $unit_price_tax_excl;
    public $unit_price_tax_incl;
    public $total_price_tax_excl;
    public $total_price_tax_incl;
    public $weight;
    public $supplier_reference;
    public $ecotax;
    public $ecotax_tax_rate;
    public $tax_rate;
    public $tax_name;
    public $reduction_percent;
    public $reduction_amount_tax_incl;
    public $reduction_amount_tax_excl;
    public $isbn_exists;
    public $mpn_exists;
    public $id_order_invoice;
    public $outOfStock;
    public $isbn;
    public $row;
    public $id_process;
    public $validateOnly; 

    public function __construct($id_shop = null, $id_lang = null, $id_order = null, $identify_product = null, $identify_combination = null, $row = null, $id_process = null, $validateOnly = false)
    {
        $this->id_process = $id_process;
        $this->row = $row;
        $this->validateOnly = $validateOnly;
        $this->id_shop = (int) $id_shop;
        $this->id_lang = (int) $id_lang;
        $this->id_order = (int) $id_order;
        $this->identify_product = $identify_product;
        $this->identify_combination = $identify_combination;
        $this->isbn_exists = !empty(Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS("SHOW COLUMNS FROM `" . _DB_PREFIX_ . "product` LIKE 'isbn'"));
        $this->mpn_exists = !empty(Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS("SHOW COLUMNS FROM `" . _DB_PREFIX_ . "product` LIKE 'mpn'"));
    }

    public function run()
    {
        $product = false;
        $id_order_detail = $this->getOrderDetailId();
        if (!$id_order_detail) {
            $product = $this->getProduct();
        }
        $obj = new OrderDetail($id_order_detail);
        $this->setOrderDetailField($obj, $product, $id_order_detail);
        $oldQuantity = $obj->product_quantity;
        $obj->id_order = (int) $this->id_order;
        $obj->id_shop = (int) $this->id_shop;
        $obj->id_warehouse = 0;
        $obj->product_id = $this->id_product;
        $obj->id_order_invoice = $this->id_order_invoice;
        $obj->product_attribute_id = $this->id_product_attribute;
        $obj->product_name = $this->name;
        $obj->product_reference = $this->reference;
        $obj->product_ean13 = $this->ean13;
        $obj->product_upc = $this->upc;
        $obj->product_mpn = $this->mpn;
        $obj->product_quantity = (int) $this->quantity;
        $obj->product_quantity_in_stock = (int) $this->quantity_in_stock;
        $obj->product_quantity_return = (int) $this->quantity_return;
        $obj->product_quantity_refunded = (int) $this->quantity_refunded;
        $obj->product_quantity_reinjected = (int) $this->quantity_reinjected;
        $obj->product_weight = EIATools::preparePrice($this->weight);
        $obj->product_supplier_reference = $this->supplier_reference;
        $obj->tax_rate = $this->tax_rate;
        $obj->tax_name = $this->tax_name;
        $obj->ecotax = EIATools::preparePrice($this->ecotax);
        $obj->ecotax_tax_rate = EIATools::preparePrice($this->ecotax_tax_rate);
        $obj->reduction_percent = EIATools::preparePrice($this->reduction_percent);
        $obj->reduction_amount_tax_incl = EIATools::preparePrice($this->reduction_amount_tax_incl);
        $obj->reduction_amount_tax_excl = EIATools::preparePrice($this->reduction_amount_tax_excl);
        $obj->total_price_tax_excl = EIATools::preparePrice($this->total_price_tax_excl);
        $obj->total_price_tax_incl = EIATools::preparePrice($this->total_price_tax_incl);
        $obj->unit_price_tax_excl = EIATools::preparePrice($this->unit_price_tax_excl);
        $obj->unit_price_tax_incl = EIATools::preparePrice($this->unit_price_tax_incl);
        $obj->product_price = EIATools::preparePrice($this->unit_price_tax_excl);

        if (($error = $obj->validateFields(false, true)) !== true) {
            throw new PrestaShopException("Error: $error. ID: $this->id_order. Row in file: $this->row.");
        }
        
        if (!$this->validateOnly) {
            $res = $obj->save();
            $cart = new EIACart();
            $cart->update($obj);

            // Caused error because of the "product_sqlstock" UNIQUE KEY
//            if ($oldQuantity < $this->quantity) {
//                $this->checkProductStock($this->quantity - $oldQuantity);
//            }
            if ($res) {
                $this->saveOrderDetailTax($obj->id);
            }
        }
    }
    
    public function saveOrderDetailTax($id_order_detail)
    {
        if (!empty($this->unit_amount_tax)) {
            Db::getInstance()->execute('DELETE FROM `' . _DB_PREFIX_ . 'order_detail_tax` WHERE id_order_detail=' . (int) $id_order_detail);
            $values = '';
            $id_tax = 0;
            foreach ($this->unit_amount_tax as $key => $val) {
                if (!empty($this->tax_name_new[$key])) {
                    $id_tax = (int) EIAOrdersImport::getTaxIdByName($this->tax_name_new[$key], 0, $this->id_lang);
                } elseif (!empty($this->id_tax[$key])) {
                    $id_tax = (int) $this->id_tax[$key];
                } else {
                    $id_tax = 0;
                }
                $unit_amount = (float) EIATools::preparePrice($val);
                $total_amount = !empty($this->unit_amount_tax[$key]) ? (float) EIATools::preparePrice($this->unit_amount_tax[$key]) : 0;
                $values .= '(' . (int) $id_order_detail . ',' . $id_tax . ',' . $unit_amount . ',' . $total_amount . '),';
            }
            if ($id_tax == 0) {
                return;
            }
            $values = rtrim($values, ',');
            $sql = 'INSERT INTO `' . _DB_PREFIX_ . 'order_detail_tax` (id_order_detail, id_tax, unit_amount, total_amount)
				VALUES ' . $values;

            Db::getInstance()->execute($sql);
        }
    }

    public function getStock($id_product = 0, $id_product_attribute = 0)
    {
        if (!(int) $id_product || !is_int($id_product_attribute)) {
            return false;
        }

        $results = Warehouse::getWarehousesByProductId($id_product, $id_product_attribute);
        $stock_quantity = 0;

        if ($results) {
            foreach ($results as $result) {
                $stock_quantity += Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT SUM(s.`usable_quantity`) as quantity
						FROM ' . _DB_PREFIX_ . 'stock s
						WHERE s.`id_product` = ' . (int) $id_product . ' AND s.`id_product_attribute` = ' . (int) $id_product_attribute . ' AND s.`id_warehouse` = ' . $result['id_warehouse'] . ' GROUP BY s.`id_product`');
            }
        } else {
            $stock_quantity += Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT s.quantity
						FROM ' . _DB_PREFIX_ . 'stock_available s
						WHERE s.`id_product` = ' . (int) $id_product . ' AND s.`id_product_attribute` = ' . (int) $id_product_attribute . '  AND s.`id_shop` = ' . (int) $this->id_shop);
        }

        return $stock_quantity;
    }

    protected function checkProductStock($quantity)
    {
        $update_quantity = true;
        if (!StockAvailable::dependsOnStock($this->id_product)) {
            $update_quantity = StockAvailable::updateQuantity($this->id_product, $this->id_product_attribute, -(int) $quantity, $this->id_shop, true);
        }
        $stock_quantity = $this->getStock((int) $this->id_product, (int) $this->id_product_attribute);

        if ($update_quantity) {
            $stock_quantity -= $quantity;
        }

        if ($stock_quantity < 0 && Configuration::get('PS_STOCK_MANAGEMENT')) {
            $this->outOfStock = true;
        }
        Product::updateDefaultAttribute($this->id_product);
    }

    private function setOrderDetailField($obj, $product, $id_order_detail)
    {
        $id_product_attribute = 0;
        $id_product = 0;

        if (isset($product['id_product_attribute']) && $product['id_product_attribute']) {
            $this->id_product_attribute = $id_product_attribute = $product['id_product_attribute'];
        } else {
            $this->id_product_attribute = $obj->product_attribute_id;
        }
        if (!$this->id_product_attribute) {
            $this->id_product_attribute = 0;
        }

        if (isset($product['id_product']) && $product['id_product']) {
            $this->id_product = $id_product = $product['id_product'];
        } else {
            $this->id_product = $obj->product_id;
        }
        if (!$this->id_product) {
            $this->id_product = 0;
        }
        if (empty($this->name) && $id_order_detail) {
            $this->name = $obj->product_name;
        } elseif (empty($this->name) && !$id_order_detail && $id_product) {
            $this->name = $this->getFullProductName($id_product, $id_product_attribute);
        }
        if (empty($this->name)) {
            $this->name = "";
        }
        if (empty($this->reference) && $id_order_detail) {
            $this->reference = $obj->product_reference;
        } elseif (empty($this->reference) && !$id_order_detail && $id_product) {
            $this->reference = $this->getFieldData('reference', $id_product, $id_product_attribute);
        }
        if (empty($this->reference)) {
            $this->reference = "";
        }
        if (empty($this->ean13) && $id_order_detail) {
            $this->ean13 = $obj->product_ean13;
        } elseif (empty($this->ean13) && !$id_order_detail && $id_product) {
            $this->ean13 = $this->getFieldData('ean13', $id_product, $id_product_attribute);
        }
        if (empty($this->ean13)) {
            $this->ean13 = "";
        }
        if (empty($this->upc) && $id_order_detail) {
            $this->upc = $obj->product_upc;
        } elseif (empty($this->ean13) && !$id_order_detail && $id_product) {
            $this->upc = $this->getFieldData('upc', $id_product, $id_product_attribute);
        }
        if (empty($this->upc)) {
            $this->upc = "";
        }
        if (empty($this->mpn) && $id_order_detail && isset($obj->product_mpn)) {
            $this->mpn = $obj->product_mpn;
        } elseif (empty($this->mpn) && !$id_order_detail && $id_product && $this->mpn_exists) {
            $this->mpn = $this->getFieldData('mpn', $id_product, $id_product_attribute);
        }
        if (empty($this->mpn)) {
            $this->mpn = "";
        }
        if (empty($this->isbn) && $id_order_detail && isset($obj->product_isbn)) {
            $this->isbn = $obj->product_isbn;
        } elseif (empty($this->isbn) && !$id_order_detail && $id_product && $this->isbn_exists) {
            $this->isbn = $this->getFieldData('isbn', $id_product, $id_product_attribute);
        }
        if (empty($this->isbn)) {
            $this->isbn = "";
        }
        if (empty($this->quantity) && $id_order_detail) {
            $this->quantity = $obj->product_quantity;
        } elseif (empty($this->quantity) && !$id_order_detail) {
            $this->quantity = 0;
        }
        if (empty($this->quantity_return) && $id_order_detail) {
            $this->quantity_return = $obj->product_quantity_return;
        } elseif (empty($this->quantity_return) && !$id_order_detail) {
            $this->quantity_return = 0;
        }
        if (empty($this->quantity_refunded) && $id_order_detail) {
            $this->quantity_refunded = $obj->product_quantity_refunded;
        } elseif (empty($this->quantity_refunded) && !$id_order_detail) {
            $this->quantity_refunded = 0;
        }
        if (empty($this->quantity_reinjected) && $id_order_detail) {
            $this->quantity_reinjected = $obj->product_quantity_reinjected;
        } elseif (empty($this->quantity_refunded) && !$id_order_detail) {
            $this->quantity_reinjected = 0;
        }
        if (empty($this->weight) && $id_order_detail) {
            $this->weight = $obj->product_weight;
        } elseif (empty($this->mpn) && !$id_order_detail && $id_product) {
            $this->weight = $this->getFieldData('weight', $id_product, $id_product_attribute);
        }
        if (empty($this->weight)) {
            $this->weight = 0;
        }
        if (empty($this->supplier_reference) && $id_order_detail) {
            $this->supplier_reference = $obj->product_supplier_reference;
        } elseif (empty($this->supplier_reference) && !$id_order_detail && $id_product) {
            $this->supplier_reference = $this->getFieldData('supplier_reference', $id_product, $id_product_attribute);
        }
        if (empty($this->supplier_reference)) {
            $this->supplier_reference = '';
        }
        if (empty($this->tax_rate) && $id_order_detail) {
            $this->tax_rate = $obj->tax_rate;
        } elseif (empty($this->tax_rate) && !$id_order_detail) {
            $this->tax_rate = '';
        }
        if (empty($this->tax_name) && $id_order_detail) {
            $this->tax_name = $obj->tax_name;
        } elseif (empty($this->tax_name) && !$id_order_detail) {
            $this->tax_name = '';
        }
        if (empty($this->ecotax) && $id_order_detail) {
            $this->ecotax = $obj->ecotax;
        } elseif (empty($this->ecotax) && !$id_order_detail) {
            $this->ecotax = '';
        }
        if (empty($this->ecotax_tax_rate) && $id_order_detail) {
            $this->ecotax_tax_rate = $obj->ecotax_tax_rate;
        } elseif (empty($this->ecotax_tax_rate) && !$id_order_detail) {
            $this->ecotax_tax_rate = '';
        }
        if (empty($this->reduction_percent) && $id_order_detail) {
            $this->reduction_percent = $obj->reduction_percent;
        } elseif (empty($this->reduction_percent) && !$id_order_detail) {
            $this->reduction_percent = 0;
        }
        if (empty($this->reduction_amount_tax_incl) && $id_order_detail) {
            $this->reduction_amount_tax_incl = $obj->reduction_amount_tax_incl;
        } elseif (empty($this->reduction_amount_tax_incl) && !$id_order_detail) {
            $this->reduction_amount_tax_incl = 0;
        }
        if (empty($this->reduction_amount_tax_excl) && $id_order_detail) {
            $this->reduction_amount_tax_excl = $obj->reduction_amount_tax_excl;
        } elseif (empty($this->reduction_amount_tax_excl) && !$id_order_detail) {
            $this->reduction_amount_tax_excl = 0;
        }
        if (empty($this->total_price_tax_excl) && $id_order_detail) {
            $this->total_price_tax_excl = $obj->total_price_tax_excl;
        } elseif (empty($this->total_price_tax_excl) && !$id_order_detail) {
            $this->total_price_tax_excl = 0;
        }
        if (empty($this->total_price_tax_incl) && $id_order_detail) {
            $this->total_price_tax_incl = $obj->total_price_tax_incl;
        } elseif (empty($this->total_price_tax_incl) && !$id_order_detail) {
            $this->total_price_tax_incl = 0;
        }
        if (empty($this->unit_price_tax_excl) && $id_order_detail) {
            $this->unit_price_tax_excl = $obj->unit_price_tax_excl;
        } elseif (empty($this->unit_price_tax_excl) && !$id_order_detail) {
            $this->unit_price_tax_excl = 0;
        }
        if (empty($this->unit_price_tax_incl) && $id_order_detail) {
            $this->unit_price_tax_incl = $obj->unit_price_tax_incl;
        } elseif (empty($this->unit_price_tax_incl) && !$id_order_detail) {
            $this->unit_price_tax_incl = 0;
        }
    }

    private function getFieldData($field, $id_product, $id_product_attribute)
    {
        $where = "";
        $table = _DB_PREFIX_ . 'product';
        if ($id_product_attribute) {
            $table = _DB_PREFIX_ . 'product_attribute';
            $where = " AND t.id_product_attribute = " . (int) $id_product_attribute;
        }
        $sql = "
                SELECT t." . $field . "
                FROM " . $table . " as t
                WHERE t.id_product = " . (int) $id_product . "
                " . $where . "
               ";
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
    }

    private function getProduct()
    {
        $id_product_attribute = false;
        $id_product = false;

        if ($this->identify_combination == EIAOrderDetail::IDENTIFY_PRODUCT_ID && $this->id_product_attribute) {
            $product = $this->getProductByIdCombination();
            $id_product_attribute = $this->id_product_attribute;
            if ($product) {
                $id_product = $product['id_product'];
            }
        }
        if ($this->identify_combination == EIAOrderDetail::IDENTIFY_PRODUCT_REFERENCE && $this->combination_reference) {
            $product = $this->getProductByReferenceCombination();
            if ($product) {
                $id_product_attribute = $product['id_product_attribute'];
                $id_product = $product['id_product'];
            }
        }
        if ($this->identify_combination == EIAOrderDetail::IDENTIFY_PRODUCT_EAN && $this->combination_ean) {
            $product = $this->getProductByEanCombination();
            if ($product) {
                $id_product_attribute = $product['id_product_attribute'];
                $id_product = $product['id_product'];
            }
        }
        if ($this->identify_combination == EIAOrderDetail::IDENTIFY_PRODUCT_UPC && $this->combination_upc) {
            $product = $this->getProductByUpcCombination();
            if ($product) {
                $id_product_attribute = $product['id_product_attribute'];
                $id_product = $product['id_product'];
            }
        }
        if ($this->identify_combination == EIAOrderDetail::IDENTIFY_PRODUCT_MPN && $this->combination_mpn) {
            $product = $this->getProductByMpnCombination();
            if ($product) {
                $id_product_attribute = $product['id_product_attribute'];
                $id_product = $product['id_product'];
            }
        }

        if (!$id_product) {
            if ($this->identify_product == EIAOrderDetail::IDENTIFY_PRODUCT_ID && $this->id_product) {
                $id_product = $this->id_product;
            }
            if ($this->identify_product == EIAOrderDetail::IDENTIFY_PRODUCT_REFERENCE && $this->reference) {
                $id_product = $this->getProductIdByReference();
            }
            if ($this->identify_product == EIAOrderDetail::IDENTIFY_PRODUCT_EAN && $this->ean13) {
                $id_product = $this->getProductIdByEan();
            }
            if ($this->identify_product == EIAOrderDetail::IDENTIFY_PRODUCT_UPC && $this->upc) {
                $id_product = $this->getProductIdByUpc();
            }
            if ($this->identify_product == EIAOrderDetail::IDENTIFY_PRODUCT_MPN && $this->mpn) {
                $id_product = $this->getProductIdByMpn();
            }
        }
        return array(
            'id_product_attribute' => $id_product_attribute,
            'id_product' => $id_product,
        );
    }

    private function getAttributeGroupLangTableCollation()
    {
        $collation = 'utf8_general_ci';
        $attribute_group_lang_table_info = Db::getInstance()->executeS("SHOW TABLE STATUS WHERE name = '" . _DB_PREFIX_ . "attribute_group_lang'");
        if (!empty($attribute_group_lang_table_info)) {
            $collation = $attribute_group_lang_table_info[0]['Collation'];
        }
        return $collation;
    }

    private function getFullProductName($id_product, $id_product_attribute)
    {
        $query = new DbQuery();
        $table_collation = $this->getAttributeGroupLangTableCollation();

        if ($id_product_attribute) {
            $query->select('IFNULL(CONCAT(pl.name COLLATE ' . pSQL($table_collation) . ', \' : \', GROUP_CONCAT(DISTINCT agl.`name`, \' - \', al.name SEPARATOR \', \')),pl.name) as name');
        } else {
            $query->select('DISTINCT pl.name as name');
        }

        if ($id_product_attribute) {
            $query->from('product_attribute', 'pa');
            $query->join(\Shop::addSqlAssociation('product_attribute', 'pa'));
            $query->innerJoin('product_lang', 'pl',
                    'pl.id_product = pa.id_product AND pl.id_lang = ' . (int) $this->id_lang . Shop::addSqlRestrictionOnLang('pl', $this->id_shop));
            $query->leftJoin('product_attribute_combination', 'pac',
                    'pac.id_product_attribute = pa.id_product_attribute');
            $query->leftJoin('attribute', 'atr', 'atr.id_attribute = pac.id_attribute');
            $query->leftJoin('attribute_lang', 'al',
                    'al.id_attribute = atr.id_attribute AND al.id_lang = ' . (int) $this->id_lang);
            $query->leftJoin('attribute_group_lang', 'agl',
                    'agl.id_attribute_group = atr.id_attribute_group AND agl.id_lang = ' . (int) $this->id_lang);
            $query->where('pa.id_product = ' . (int) $id_product . ' AND pa.id_product_attribute = ' . (int) $id_product_attribute);
        } else {
            $query->from('product_lang', 'pl');
            $query->where('pl.id_product = ' . (int) $id_product);
            $query->where('pl.id_lang = ' . (int) $this->id_lang . Shop::addSqlRestrictionOnLang('pl', $this->id_shop));
        }
        return Db::getInstance()->getValue($query);
    }

    private function getOrderDetailId()
    {
        $id_order_detail = false;

        if ($this->identify_combination == EIAOrderDetail::IDENTIFY_PRODUCT_ID && $this->id_product_attribute) {
            $id_order_detail = $this->getOrderDetailByIdCombination();
        }
        if ($this->identify_combination == EIAOrderDetail::IDENTIFY_PRODUCT_REFERENCE && $this->combination_reference) {
            $id_order_detail = $this->getOrderDetailByReference($this->combination_reference);
        }
        if ($this->identify_combination == EIAOrderDetail::IDENTIFY_PRODUCT_EAN && $this->combination_ean) {
            $id_order_detail = $this->getOrderDetailByEan($this->combination_ean);
        }
        if ($this->identify_combination == EIAOrderDetail::IDENTIFY_PRODUCT_UPC && $this->combination_upc) {
            $id_order_detail = $this->getOrderDetailByUpc($this->combination_upc);
        }
        if ($this->identify_combination == EIAOrderDetail::IDENTIFY_PRODUCT_MPN && $this->combination_mpn) {
            $id_order_detail = $this->getOrderDetailByMpn($this->combination_mpn);
        }

        if (!$id_order_detail) {
            if ($this->identify_product == EIAOrderDetail::IDENTIFY_PRODUCT_ID && $this->id_product) {
                $id_order_detail = $this->getOrderDetailByIdProduct();
            }
            if ($this->identify_product == EIAOrderDetail::IDENTIFY_PRODUCT_REFERENCE && $this->reference) {
                $id_order_detail = $this->getOrderDetailByReference($this->reference);
            }
            if ($this->identify_product == EIAOrderDetail::IDENTIFY_PRODUCT_EAN && $this->ean13) {
                $id_order_detail = $this->getOrderDetailByEan($this->ean13);
            }
            if ($this->identify_product == EIAOrderDetail::IDENTIFY_PRODUCT_UPC && $this->upc) {
                $id_order_detail = $this->getOrderDetailByUpc($this->upc);
            }
            if ($this->identify_product == EIAOrderDetail::IDENTIFY_PRODUCT_MPN && $this->mpn) {
                $id_order_detail = $this->getOrderDetailByMpn($this->mpn);
            }
        }

        return $id_order_detail;
    }

    private function getOrderDetailByMpn($mpn)
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "order_detail as od
                WHERE od.product_mpn = '" . pSQL($mpn) . "'
                AND od.id_order = " . (int) $this->id_order . "
               ";
        $order_detail = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($order_detail['id_order_detail']) && $order_detail['id_order_detail']) {
            return $order_detail['id_order_detail'];
        }
        return false;
    }

    private function getOrderDetailByUpc($upc)
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "order_detail as od
                WHERE od.product_upc = '" . pSQL($upc) . "'
                AND od.id_order = " . (int) $this->id_order . "
               ";
        $order_detail = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($order_detail['id_order_detail']) && $order_detail['id_order_detail']) {
            return $order_detail['id_order_detail'];
        }
        return false;
    }

    private function getOrderDetailByEan($ean)
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "order_detail as od
                WHERE od.product_ean13 = '" . pSQL($ean) . "'
                AND od.id_order = " . (int) $this->id_order . "
               ";
        $order_detail = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($order_detail['id_order_detail']) && $order_detail['id_order_detail']) {
            return $order_detail['id_order_detail'];
        }
        return false;
    }

    private function getOrderDetailByReference($reference)
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "order_detail as od
                WHERE od.product_reference = '" . pSQL($reference) . "'
                AND od.id_order = " . (int) $this->id_order . "
               ";
        $order_detail = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($order_detail['id_order_detail']) && $order_detail['id_order_detail']) {
            return $order_detail['id_order_detail'];
        }
        return false;
    }

    private function getOrderDetailByIdCombination()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "order_detail as od
                WHERE od.product_attribute_id = " . (int) $this->id_product_attribute . "
                AND od.id_order = " . (int) $this->id_order . "
               ";
        $order_detail = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($order_detail['id_order_detail']) && $order_detail['id_order_detail']) {
            return $order_detail['id_order_detail'];
        }
        return false;
    }

    private function getOrderDetailByIdProduct()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "order_detail as od
                WHERE od.product_id = " . (int) $this->id_product . "
                AND od.id_order = " . (int) $this->id_order . "
               ";
        $order_detail = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($order_detail['id_order_detail']) && $order_detail['id_order_detail']) {
            return $order_detail['id_order_detail'];
        }
        return false;
    }

    private function getProductIdByMpn()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "product as pa
                WHERE pa.mpn = '" . pSQL($this->mpn) . "'
    
               ";
        $product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($product['id_product']) && $product['id_product']) {
            return $product['id_product'];
        }
        return false;
    }

    private function getProductIdByUpc()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "product as pa
                WHERE pa.upc = '" . pSQL($this->upc) . "'
    
               ";
        $product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($product['id_product']) && $product['id_product']) {
            return $product['id_product'];
        }
        return false;
    }

    private function getProductIdByEan()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "product as pa
                WHERE pa.ean13 = '" . pSQL($this->ean13) . "'
    
               ";
        $product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($product['id_product']) && $product['id_product']) {
            return $product['id_product'];
        }
        return false;
    }

    private function getProductIdByReference()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "product as pa
                WHERE pa.reference = '" . pSQL($this->reference) . "'
    
               ";
        $product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($product['id_product']) && $product['id_product']) {
            return $product['id_product'];
        }
        return false;
    }

    private function getProductByMpnCombination()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "product_attribute as pa
                WHERE pa.mpn = '" . pSQL($this->combination_mpn) . "'
    
               ";
        $product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($product['id_product_attribute']) && $product) {
            return $product;
        }
        return false;
    }

    private function getProductByUpcCombination()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "product_attribute as pa
                WHERE pa.upc = '" . pSQL($this->combination_upc) . "'
    
               ";
        $product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($product['id_product_attribute']) && $product) {
            return $product;
        }
        return false;
    }

    private function getProductByEanCombination()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "product_attribute as pa
                WHERE pa.ean13 = '" . pSQL($this->combination_ean) . "'
    
               ";
        $product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($product['id_product_attribute']) && $product) {
            return $product;
        }
        return false;
    }

    private function getProductByReferenceCombination()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "product_attribute as pa
                WHERE pa.reference = '" . pSQL($this->combination_reference) . "'
    
               ";
        $product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($product['id_product_attribute']) && $product) {
            return $product;
        }
        return false;
    }

    private function getProductByIdCombination()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "product_attribute as pa
                WHERE pa.id_product_attribute = " . (int) $this->id_product_attribute . "
    
               ";
        $product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($product['id_product_attribute']) && $product) {
            return $product;
        }
        return false;
    }

}