Fix comparing address in back sync

This commit is contained in:
max-baranikov 2022-04-22 18:34:27 +03:00 committed by GitHub
parent 730000b47b
commit 06ca38be56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 17 deletions

View File

@ -191,11 +191,11 @@ class RetailcrmCustomerAddressBuilder extends RetailcrmAbstractBuilder implement
$this->buildAddressLine(); $this->buildAddressLine();
if (isset($this->dataCrm['notes'])) { if (array_key_exists('notes', $this->dataCrm)) {
$this->setAddressField('other', $this->dataCrm['notes'], ''); $this->setAddressField('other', $this->dataCrm['notes'], '');
} }
if (isset($this->dataCrm['countryIso'])) { if (array_key_exists('countryIso', $this->dataCrm)) {
$countryIso = null; $countryIso = null;
if (Validate::isLanguageIsoCode($this->dataCrm['countryIso'])) { if (Validate::isLanguageIsoCode($this->dataCrm['countryIso'])) {
$countryIso = Country::getByIso($this->dataCrm['countryIso']); $countryIso = Country::getByIso($this->dataCrm['countryIso']);
@ -204,13 +204,13 @@ class RetailcrmCustomerAddressBuilder extends RetailcrmAbstractBuilder implement
$this->setAddressField('id_country', $countryIso, Configuration::get('PS_COUNTRY_DEFAULT')); $this->setAddressField('id_country', $countryIso, Configuration::get('PS_COUNTRY_DEFAULT'));
} }
if (isset($this->dataCrm['city'])) { if (array_key_exists('city', $this->dataCrm)) {
$this->setAddressField('city', $this->dataCrm['city'], '--'); $this->setAddressField('city', $this->dataCrm['city'], '--');
} }
if (isset($this->dataCrm['index'])) { if (array_key_exists('index', $this->dataCrm)) {
$this->setAddressField('postcode', $this->dataCrm['index'], ''); $this->setAddressField('postcode', $this->dataCrm['index'], '');
} }
if (isset($this->dataCrm['region'])) { if (array_key_exists('region', $this->dataCrm)) {
$this->setAddressField('id_state', (int) State::getIdByName($this->dataCrm['region'])); $this->setAddressField('id_state', (int) State::getIdByName($this->dataCrm['region']));
} }
@ -242,10 +242,12 @@ class RetailcrmCustomerAddressBuilder extends RetailcrmAbstractBuilder implement
{ {
if (isset($this->dataCrm['text'])) { if (isset($this->dataCrm['text'])) {
$text = $this->dataCrm['text']; $text = $this->dataCrm['text'];
if (isset($this->dataCrm['notes'])) { if (isset($this->dataCrm['notes'])) {
$text = str_replace($this->dataCrm['notes'], '', $text); $text = str_replace($this->dataCrm['notes'], '', $text);
} }
$text = rtrim($text, ', ');
$addressLine = explode(RetailcrmAddressBuilder::ADDRESS_LINE_DIVIDER, $text, 2); $addressLine = explode(RetailcrmAddressBuilder::ADDRESS_LINE_DIVIDER, $text, 2);
$this->setAddressField('address1', $addressLine[0], '--'); $this->setAddressField('address1', $addressLine[0], '--');

View File

@ -772,15 +772,14 @@ class RetailcrmTools
/** /**
* Returns true if mapped fields in address are equal. Returns false otherwise. * Returns true if mapped fields in address are equal. Returns false otherwise.
* *
* @param \Address $first * @param \Address $address1
* @param \Address $second * @param \Address $address2
* *
* @return bool * @return bool
*/ */
protected static function isAddressesEqualByFields($first, $second) protected static function isAddressesEqualByFields($address1, $address2)
{ {
$checkMapping = [ $fieldsToCompare = [
'alias',
'id_country', 'id_country',
'lastname', 'lastname',
'firstname', 'firstname',
@ -795,16 +794,26 @@ class RetailcrmTools
'vat_number', 'vat_number',
]; ];
foreach ($checkMapping as $field) { try {
if ($first->$field != $second->$field) { // getting fields in the same format (normalized)
$address1Fields = $address1->getFields();
$address2Fields = $address2->getFields();
} catch (PrestaShopException $e) {
RetailcrmLogger::writeDebug(__METHOD__, $e->getMessage());
return false;
}
foreach ($fieldsToCompare as $field) {
if ($address1Fields[$field] !== $address2Fields[$field]) {
RetailcrmLogger::writeDebug(__METHOD__, json_encode([ RetailcrmLogger::writeDebug(__METHOD__, json_encode([
'first' => [ 'first' => [
'id' => $first->id, 'id' => $address1->id,
$field => $first->$field, $field => $address1Fields[$field],
], ],
'second' => [ 'second' => [
'id' => $second->id, 'id' => $address2->id,
$field => $second->$field, $field => $address2Fields[$field],
], ],
])); ]));

View File

@ -661,7 +661,7 @@ class RetailCRM extends Module
$zipname = _PS_DOWNLOAD_DIR_ . '/retailcrm_logs_' . date('Y-m-d H-i-s') . '.zip'; $zipname = _PS_DOWNLOAD_DIR_ . '/retailcrm_logs_' . date('Y-m-d H-i-s') . '.zip';
$zipFile = new ZipArchive(); $zipFile = new ZipArchive();
$zipFile->open($zipname, ZIPARCHIVE::CREATE); $zipFile->open($zipname, ZipArchive::CREATE);
foreach (RetailcrmLogger::getLogFilesInfo() as $logFile) { foreach (RetailcrmLogger::getLogFilesInfo() as $logFile) {
$zipFile->addFile($logFile['path'], $logFile['name']); $zipFile->addFile($logFile['path'], $logFile['name']);