Current File : /var/www/vinorea/modules/ipexportimport/classes/import/order/EIAIdentifyAddress.php |
<?php
/**
*
* NOTICE OF LICENSE
*
* @author SmartPresta <tehran.alishov@gmail.com>
* @copyright 2024 SmartPresta
* @license Commercial License
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class EIAIdentifyAddress
{
private $create_new_address;
private $id_shop;
private $id_lang;
private $id;
private $address_type;
private $identify;
private $dataAddress;
private $id_customer;
private $address1;
private $address2;
private $firstname;
private $lastname;
private $dni;
private $alias;
private $postcode;
private $city;
private $other;
private $phone;
private $phone_mobile;
private $company;
private $vat_number;
private $id_country;
private $country_name;
private $country_iso_code;
private $id_state;
private $state_name;
private $state_iso_code;
private $row;
private $validateOnly;
private $id_order;
const IDENTIFY_ADDRESS_ID = 1;
const IDENTIFY_ADDRESS_ALIAS = 2;
const IDENTIFY_ADDRESS_COMPANY = 3;
const IDENTIFY_ADDRESS_LASTNAME = 4;
const IDENTIFY_ADDRESS_FIRSTNAME = 5;
const IDENTIFY_ADDRESS_1 = 6;
const IDENTIFY_ADDRESS_2 = 7;
const IDENTIFY_ADDRESS_POSTAL = 8;
public function __construct($id_order, $id_shop, $id_lang, $identify, $address_type, $create_new_address, $dataAddress, $id_customer, $row, $validateOnly)
{
$this->row = $row;
$this->address_type = $address_type;
$this->id_shop = $id_shop;
$this->id_lang = $id_lang;
$this->create_new_address = $create_new_address;
$this->identify = isset($identify) ? $identify : array();
$this->dataAddress = $dataAddress;
$this->id_customer = $id_customer;
$this->validateOnly = $validateOnly;
$this->id_order = $id_order;
$this->id = isset($dataAddress['id_address']) ? (int) $dataAddress['id_address'] : 0;
}
private function getObjectVariables($dataAddress, $obj)
{
if (isset($dataAddress['address1'])) {
$this->address1 = ($dataAddress['address1']) ? $dataAddress['address1'] : '';
} else {
$this->address1 = $obj->address1;
}
if (isset($dataAddress['address2'])) {
$this->address2 = ($dataAddress['address2']) ? $dataAddress['address2'] : '';
} else {
$this->address2 = $obj->address2;
}
if (isset($dataAddress['firstname'])) {
$this->firstname = ($dataAddress['firstname']) ? $dataAddress['firstname'] : '';
} else {
$this->firstname = $obj->firstname;
}
if (isset($dataAddress['lastname'])) {
$this->lastname = ($dataAddress['lastname']) ? $dataAddress['lastname'] : '';
} else {
$this->lastname = $obj->lastname;
}
if (isset($dataAddress['alias'])) {
$this->alias = ($dataAddress['alias']) ? $dataAddress['alias'] : '';
} else {
$this->alias = $obj->alias;
}
if (isset($dataAddress['company'])) {
$this->company = ($dataAddress['company']) ? $dataAddress['company'] : '';
} else {
$this->company = $obj->company;
}
if (isset($dataAddress['postcode'])) {
$this->postcode = ($dataAddress['postcode']) ? $dataAddress['postcode'] : '';
} else {
$this->postcode = $obj->postcode;
}
if (isset($dataAddress['id_country'])) {
$this->id_country = ($dataAddress['id_country']) ? (int) $dataAddress['id_country'] : 0;
} else {
$this->id_country = $obj->id_country;
}
if (isset($dataAddress['id_state'])) {
$this->id_state = ($dataAddress['id_state']) ? (int) $dataAddress['id_state'] : 0;
} else {
$this->id_state = $obj->id_state;
}
if (isset($dataAddress['other'])) {
$this->other = ($dataAddress['other']) ? $dataAddress['other'] : '';
}
if (isset($dataAddress['city'])) {
$this->city = ($dataAddress['city']) ? $dataAddress['city'] : '';
} else {
$this->city = $obj->city;
}
if (isset($dataAddress['phone'])) {
$this->phone = ($dataAddress['phone']) ? $dataAddress['phone'] : '';
} else {
$this->phone = $obj->phone;
}
if (isset($dataAddress['phone_mobile'])) {
$this->phone_mobile = ($dataAddress['phone_mobile']) ? $dataAddress['phone_mobile'] : '';
} else {
$this->phone_mobile = $obj->phone_mobile;
}
if (isset($dataAddress['vat_number'])) {
$this->vat_number = ($dataAddress['vat_number']) ? $dataAddress['vat_number'] : '';
} else {
$this->vat_number = $obj->vat_number;
}
if (isset($dataAddress['dni'])) {
$this->dni = ($dataAddress['dni']) ? $dataAddress['dni'] : '';
} else {
$this->dni = $obj->dni;
}
if (isset($dataAddress['country_name'])) {
$this->country_name = ($dataAddress['country_name']) ? $dataAddress['country_name'] : '';
} else {
$this->country_name = '';
}
if (isset($dataAddress['country_iso_code'])) {
$this->country_iso_code = ($dataAddress['country_iso_code']) ? $dataAddress['country_iso_code'] : '';
} else {
$this->country_iso_code = '';
}
if (isset($dataAddress['state_name'])) {
$this->state_name = ($dataAddress['state_name']) ? $dataAddress['state_name'] : '';
} else {
$this->state_name = '';
}
if (isset($dataAddress['state_iso_code'])) {
$this->state_iso_code = ($dataAddress['state_iso_code']) ? $dataAddress['state_iso_code'] : '';
} else {
$this->state_iso_code = '';
}
}
public function identify()
{
$id_address = $address = false;
if ($this->identify) {
$address = $this->getAddress();
}
if ($address) {
$id_address = $this->id = $address['id_address'];
}
if (!$address && $this->create_new_address) {
$obj = new Address();
if ($this->id && !$this->getAddressById($this->id)) {
$obj->id = $this->id;
$obj->force_id = true;
}
$this->getObjectVariables($this->dataAddress, $obj);
if ($this->id_country) {
$obj->id_country = $this->id_country;
}
if ($this->id_state) {
$obj->id_state = $this->id_state;
}
if (!$this->id_country && ((isset($this->country_name) && $this->country_name) || (isset($this->country_iso_code) && $this->country_iso_code))) {
$obj->id_country = $this->getIdCountry($this->country_name, $this->country_iso_code);
}
if (!$this->id_state && ($this->state_name || $this->state_iso_code)) {
$obj->id_state = $this->getIdState($this->state_name, $this->state_iso_code);
}
$obj->alias = $this->alias;
$obj->id_customer = $this->id_customer;
$obj->address1 = $this->address1;
$obj->address1 = $this->address1;
$obj->address2 = $this->address2;
$obj->lastname = $this->lastname;
$obj->firstname = $this->firstname;
$obj->dni = $this->dni;
$obj->postcode = $this->postcode;
$obj->city = $this->city;
$obj->other = $this->other;
$obj->phone = $this->phone;
$obj->phone_mobile = $this->phone_mobile;
$obj->company = $this->company;
$obj->vat_number = $this->vat_number;
if (($error = $obj->validateFields(false, true)) !== true) {
throw new PrestaShopException("Error: $error. ID: $this->id_order. Row in file: $this->row.");
}
if (!$this->validateOnly) {
$obj->add();
$id_address = $obj->id;
} else {
return 1;
}
}
if ($id_address) {
return $id_address;
}
if (!$id_address && is_numeric($this->id_order)) {
$obj = new Order($this->id_order);
if ($this->address_type == 'shipping') {
$field = 'id_address_delivery';
} else {
$field = 'id_address_invoice';
}
if ($obj->$field) {
return $obj->$field;
}
}
return false;
}
private function getIdCountry($name, $iso)
{
$where = "";
if ($name) {
$where = " AND cl.name = '" . pSQL($name) . "'";
}
if ($iso) {
$where = " AND c.iso_code = '" . pSQL($iso) . "'";
}
$sql = "
SELECT *
FROM " . _DB_PREFIX_ . "country as c
LEFT JOIN " . _DB_PREFIX_ . "country_lang as cl
ON c.id_country = cl.id_country
WHERE cl.id_lang = " . (int) $this->id_lang . "
" . $where . "
";
$country = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
if (isset($country['id_country']) && $country['id_country']) {
return $country['id_country'];
}
return false;
}
private function getIdState($name, $iso)
{
$where = "";
if ($name) {
$where = " AND s.name = '" . pSQL($name) . "'";
}
if ($iso) {
$where = " AND s.iso_code = '" . pSQL($iso) . "'";
}
$sql = "
SELECT *
FROM " . _DB_PREFIX_ . "state as s
WHERE 1
" . $where . "
";
$state = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
if (isset($state['id_state']) && $state['id_state']) {
return $state['id_state'];
}
return false;
}
private function getAddressById($id_address)
{
$sql = "
SELECT *
FROM " . _DB_PREFIX_ . "address as a
WHERE id_address = " . (int) $id_address;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
}
private function getAddress()
{
if ($where = $this->getSelectOptions()) {
$sql = "
SELECT *
FROM " . _DB_PREFIX_ . "address as a
WHERE 1
" . $where . "
";
return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
}
return false;
}
private function getSelectOptions()
{
$where = "";
if ($this->identify && in_array(self::IDENTIFY_ADDRESS_ID, $this->identify) && (isset($this->dataAddress['id_address']) && $this->dataAddress['id_address'])) {
$where .= " AND a.id_address = " . (int) $this->dataAddress['id_address'];
}
if ($this->identify && in_array(self::IDENTIFY_ADDRESS_ALIAS, $this->identify) && (isset($this->dataAddress['alias']) && $this->dataAddress['alias'])) {
$where .= " AND a.alias = '" . pSQL($this->dataAddress['alias']) . "' ";
}
if ($this->identify && in_array(self::IDENTIFY_ADDRESS_COMPANY, $this->identify) && (isset($this->dataAddress['company']) && $this->dataAddress['company'])) {
$where .= " AND a.company = '" . pSQL($this->dataAddress['company']) . "' ";
}
if ($this->identify && in_array(self::IDENTIFY_ADDRESS_LASTNAME, $this->identify) && (isset($this->dataAddress['lastname']) && $this->dataAddress['lastname'])) {
$where .= " AND a.lastname = '" . pSQL($this->dataAddress['lastname']) . "' ";
}
if ($this->identify && in_array(self::IDENTIFY_ADDRESS_FIRSTNAME, $this->identify) && (isset($this->dataAddress['firstname']) && $this->dataAddress['firstname'])) {
$where .= " AND a.firstname = '" . pSQL($this->dataAddress['firstname']) . "' ";
}
if ($this->identify && in_array(self::IDENTIFY_ADDRESS_1, $this->identify) && (isset($this->dataAddress['address1']) && $this->dataAddress['address1'])) {
$where .= " AND a.address1 = '" . pSQL($this->dataAddress['address1']) . "' ";
}
if ($this->identify && in_array(self::IDENTIFY_ADDRESS_2, $this->identify) && (isset($this->dataAddress['address2']) && $this->dataAddress['address2'])) {
$where .= " AND a.address2 = '" . pSQL($this->dataAddress['address2']) . "' ";
}
if ($this->identify && in_array(self::IDENTIFY_ADDRESS_POSTAL, $this->identify) && (isset($this->dataAddress['postcode']) && $this->dataAddress['postcode'])) {
$where .= " AND a.postcode = '" . pSQL($this->dataAddress['postcode']) . "' ";
}
if (!$this->identify) {
return false;
}
return $where;
}
}