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

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

class EIAIdentifyOrder
{

    private $method;
    private $identify;
    private $id_order;
    private $reference;

    const IDENTIFY_ORDER_ID = 1;
    const IDENTIFY_ORDER_REFERENCE = 2;

    public function __construct($method, $identify, $data)
    {
        $this->method = $method;
        $this->identify = isset($identify) ? (int) $identify : 0;
        $this->id_order = isset($data['id_order']) ? (int) $data['id_order'] : 0;
        $this->reference = isset($data['reference']) ? pSQL($data['reference']) : '';
    }

    public function identify()
    {
        $order = false;

        if ($this->identify == self::IDENTIFY_ORDER_ID && $this->id_order) {
            $order = $this->getOrderById();
        }

        if ($this->identify == self::IDENTIFY_ORDER_REFERENCE && $this->reference) {
            $order = $this->getOrderByReference();
        }

        if ($this->identify == self::IDENTIFY_ORDER_ID && !$this->id_order && $this->method == EIAConfigurationValidator::ONLY_UPDATE_ORDERS) {
            return false;
        }

        if ($this->identify == self::IDENTIFY_ORDER_REFERENCE && !$this->reference && $this->method == EIAConfigurationValidator::ONLY_UPDATE_ORDERS) {
            return false;
        }

        if ($this->identify == self::IDENTIFY_ORDER_ID && !$this->id_order && $this->method !== EIAConfigurationValidator::ONLY_UPDATE_ORDERS) {
            return true;
        }

        if ($this->identify == self::IDENTIFY_ORDER_REFERENCE && !$this->reference && $this->method !== EIAConfigurationValidator::ONLY_UPDATE_ORDERS) {
            return true;
        }

        if (!$this->identify && $this->method == EIAConfigurationValidator::UPDATE_INSERT_ORDERS) {
            return true;
        }

        if ($order && $this->method == EIAConfigurationValidator::ONLY_INSERT_ORDERS) {
            $this->id_order = (int) $order['id_order'];
            return false;
        }

        if (!$order && $this->method == EIAConfigurationValidator::ONLY_UPDATE_ORDERS) {
            return false;
        }

        if ($order && $this->method !== EIAConfigurationValidator::ONLY_INSERT_ORDERS) {
            return (int) $order['id_order'];
        }

        if (!$order && $this->method == EIAConfigurationValidator::UPDATE_INSERT_ORDERS) {
            return true;
        }

        if (!$order && $this->method == EIAConfigurationValidator::ONLY_INSERT_ORDERS) {
            return true;
        }
    }
    
    public function getOrderId()
    {
        return $this->id_order;
    }
    
    private function getOrderById()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "orders as o
                WHERE o.id_order = " . $this->id_order . "
               ";
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
    }

    private function getOrderByReference()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "orders as o
                WHERE o.reference = '" . $this->reference . "'
               ";
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
    }

}