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

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

require_once(dirname(__FILE__) . '/EIACustomer.php');

class EIAIdentifyCustomer
{

    private $id_shop;
    private $id_lang;
    private $create_new_customers;
    private $identify;
    private $id_customer;
    private $email;
    private $data;
    private $row;
    private $validateOnly;
    private $id_order;

    const IDENTIFY_CUSTOMER_ID = 1;
    const IDENTIFY_CUSTOMER_EMAIL = 2;

    public function __construct($id_order, $id_shop, $id_lang, $identify, $create_new_customers, $data, $row, $validateOnly)
    {
        $this->id_shop = $id_shop;
        $this->id_lang = $id_lang;
        $this->create_new_customers = isset($create_new_customers) ? (int) $create_new_customers : 0;
        $this->identify = isset($identify) ? (int) $identify : 0;
        $this->id_customer = isset($data['id_customer']) ? (int) $data['id_customer'] : 0;
        $this->email = isset($data['email']) ? pSQL($data['email']) : '';
        $this->data = $data;
        $this->row = $row;
        $this->validateOnly = $validateOnly;
        $this->id_order = $id_order;
    }

    public function identify()
    {
        $id_customer = $customer = false;
        $customer_exists_with_id = false;
        if ($this->id_customer) {
            $customer = $this->getCustomerById();
            if ($customer) {
                $customer_exists_with_id = true;
                if($this->identify == self::IDENTIFY_CUSTOMER_ID) {
                    $id_customer = (int) $customer['id_customer'];
                }
            }
        }

        if ($this->identify == self::IDENTIFY_CUSTOMER_EMAIL && $this->email) {
            $customer = $this->getCustomerByEmail();
            if ($customer) {
                $id_customer = (int) $customer['id_customer'];
            }
        }
        if (!$customer && $this->create_new_customers) {
            $objCustomer = new EIACustomer($this->id_order, $this->id_shop, $this->id_lang, $this->data, $this->row, $this->validateOnly, $customer_exists_with_id);
            $id_customer = $objCustomer->save();
        }
        if ($id_customer) {
            return $id_customer;
        }
        if (!$this->identify && is_numeric($this->id_order) && !$id_customer) {
            $obj = new Order($this->id_order);
            if ($obj->id_customer) {
                return $obj->id_customer;
            }
        }



        return false;
    }

    private function getCustomerById()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "customer as c
                WHERE c.id_customer = " . (int) $this->id_customer . "
               ";

        return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
    }

    private function getCustomerByEmail()
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "customer as c
                WHERE c.email = '" . pSQL($this->email) . "'
               ";
        return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
    }

}