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

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

class EIACustomer
{
    public $id;
    public $id_order;
    public $id_lang;
    public $id_shop;
    public $email;
    public $passwd;
    public $firstname;
    public $lastname;
    public $birthday;
    public $company;
    public $note;
    public $is_guest;
    public $website;
    public $newsletter;
    public $default_group;
    public $groups;
    public $row;
    public $validateOnly;
    public $customer_exists_with_id;

    public function __construct($id_order, $id_shop = null, $id_lang = null, $customerData = null, $row = null, $validateOnly = true, $customer_exists_with_id = false)
    {
        $this->id_order = (int) $id_order;
        $this->id_shop = (int) $id_shop;
        $this->id_lang = (int) $id_lang;
        $this->row = $row;
        $this->validateOnly = $validateOnly;
        $this->customer_exists_with_id = $customer_exists_with_id;
        $this->id = isset($customerData['id_customer']) ? (int) trim($customerData['id_customer']) : null;
        $this->email = isset($customerData['email']) ? ($customerData['email']) : '';
//        $this->passwd = md5(Tools::passwdGen(8, 'RANDOM'));
        $this->passwd = !empty($customerData['passwd']) ? ($customerData['passwd']) : '';
        $this->firstname = isset($customerData['firstname']) ? ($customerData['firstname']) : '';
        $this->lastname = isset($customerData['lastname']) ? ($customerData['lastname']) : '';
        $this->birthday = isset($customerData['birthday']) ? ($customerData['birthday']) : '';
        $this->company = isset($customerData['company']) ? ($customerData['company']) : '';
        $this->note = isset($customerData['note']) ? ($customerData['note']) : '';
        $this->is_guest = isset($customerData['is_guest']) ? $customerData['is_guest'] : 0;
        $this->website = isset($customerData['website']) ? ($customerData['website']) : '';
        $this->newsletter = isset($customerData['newsletter']) ? $customerData['newsletter'] : 0;
        $this->default_group = isset($customerData['default_group']) ? ($customerData['default_group']) : '';
        $this->groups = isset($customerData['groups']) ? $customerData['groups'] : array();
    }

    public function save()
    {
        $customer = new Customer();
        if ($this->id && !$this->customer_exists_with_id) {
            $customer->id = $this->id;
            $customer->force_id = true;
        }
        $customer->id_shop = $this->id_shop;
        $customer->id_lang = $this->id_lang;
        $customer->passwd = $this->passwd;
        $customer->email = $this->email;
        $customer->firstname = $this->firstname;
        $customer->lastname = $this->lastname;
        $customer->birthday = $this->birthday;
        $customer->company = $this->company;
        $customer->note = $this->note;
        $customer->is_guest = $this->is_guest;
        $customer->website = $this->website;
        $customer->newsletter = $this->newsletter;
        $customer->id_default_group = $this->getIdGroup($this->default_group);
        if (($error = $customer->validateFields(false, true)) !== true) {
            throw new PrestaShopException("Error: $error. ID: $this->id_order. Row in file: $this->row.");
        }
        if (!$this->validateOnly) {
            $customer->add();
            $this->addGroups();
            $this->id = $customer->id;
            return (int) $this->id;
        } else {
            return 1;
        }
    }

    private function addGroups()
    {
        $ids = array();
        foreach ($this->groups as $group) {
            if (($id_group = $this->getIdGroup($group))) {
                $ids[] = $id_group;
            }
        }
        if ($ids) {
            $customer = new Customer($this->id);
            $customer->addGroups($ids);
        }
    }

    public function getIdGroup($group)
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "group_lang as g
                WHERE g.name = '" . pSQL($group) . "'
                AND g.id_lang = " . (int) $this->id_lang . "
               ";
        $group = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);

        if (isset($group['id_group']) && $group['id_group']) {
            return $group['id_group'];
        }
        return false;
    }

}