From 2dbccd8a02b05cf84f4681e4f518fa0db214c720 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Wed, 2 Sep 2020 12:21:33 +0300 Subject: [PATCH] Unified address logic (#193) --- .docker/Dockerfile | 14 +++--- .docker/entrypoint.sh | 4 +- .env-dist | 12 ++--- CHANGELOG.md | 3 ++ README.ru.md | 2 +- VERSION | 2 +- composer.json | 4 +- .../lib/service/CorporateCustomer.php | 44 ++++++++++++------- .../lib/service/CorporateCustomerBuilder.php | 37 +++++++++++++++- .../service/CorporateCustomerBuilderTest.php | 21 ++++++--- 10 files changed, 102 insertions(+), 41 deletions(-) diff --git a/.docker/Dockerfile b/.docker/Dockerfile index a638c72..086f7c3 100644 --- a/.docker/Dockerfile +++ b/.docker/Dockerfile @@ -23,9 +23,9 @@ RUN wget -O /usr/bin/phpunit https://phar.phpunit.de/phpunit-7.phar && chmod +x RUN curl --insecure https://getcomposer.org/download/1.9.3/composer.phar -o /usr/bin/composer && chmod +x /usr/bin/composer # Set timezone -RUN rm /etc/localtime -RUN ln -s /usr/share/zoneinfo/Europe/Moscow /etc/localtime -RUN "date" +RUN rm /etc/localtime && \ + ln -s /usr/share/zoneinfo/Europe/Moscow /etc/localtime && \ + "date" ARG TEST_SUITE ARG OPENCART @@ -51,6 +51,8 @@ ENV OC_EMAIL=${OC_EMAIL} ADD .docker/entrypoint.sh /usr/local/bin/docker-php-entrypoint -RUN sed -i "s/80/$PORT/g" /etc/apache2/sites-enabled/000-default.conf /etc/apache2/ports.conf -RUN sed -i 's/var\/www\/html/opencart-module\/www/g' /etc/apache2/sites-enabled/000-default.conf -RUN sed -i 's/var\/www/opencart-module/g' /etc/apache2/apache2.conf +RUN chmod +x /usr/local/bin/docker-php-entrypoint + +RUN sed -i "s/80/$PORT/g" /etc/apache2/sites-enabled/000-default.conf /etc/apache2/ports.conf && \ + sed -i 's/var\/www\/html/opencart-module\/www/g' /etc/apache2/sites-enabled/000-default.conf && \ + sed -i 's/var\/www/opencart-module/g' /etc/apache2/apache2.conf diff --git a/.docker/entrypoint.sh b/.docker/entrypoint.sh index 9383e96..9b281e9 100644 --- a/.docker/entrypoint.sh +++ b/.docker/entrypoint.sh @@ -8,8 +8,10 @@ done make -C /opencart-module run -mkdir /opencart-module/www/system/storage/session +mkdir -p /opencart-module/www/system/storage/session +sed -i 's/\?>//g' /opencart-module/www/config.php +sed -i 's/\?>//g' /opencart-module/www/admin/config.php echo '\nini_set("session.save_path", DIR_SYSTEM . "storage/session");' >> /opencart-module/www/config.php echo '\nini_set("session.save_path", DIR_SYSTEM . "storage/session");' >> /opencart-module/www/admin/config.php diff --git a/.env-dist b/.env-dist index 21b845c..ab49aff 100644 --- a/.env-dist +++ b/.env-dist @@ -1,8 +1,8 @@ # OpenCart Database connection values -OC_DB_HOSTNAME=host -OC_DB_USERNAME=user -OC_DB_PASSWORD=pass -OC_DB_DATABASE=database +OC_DB_HOSTNAME=mysql +OC_DB_USERNAME=opencart +OC_DB_PASSWORD=opencart +OC_DB_DATABASE=opencart OC_DB_DRIVER=mysqli # OpenCart Administration user @@ -14,5 +14,5 @@ OC_EMAIL=you@example.com SERVER_PORT=8080 SERVER_URL=http://localhost -TEST_SUITE= -OPENCART= +TEST_SUITE=3.0 +OPENCART=3.0.2.0 diff --git a/CHANGELOG.md b/CHANGELOG.md index c893757..a7f6f5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v.4.1.1 +* Обновлена механика обработки адресов покупателей + ## v.4.1.0 * Добавлена возможность подключения Online Consultant diff --git a/README.ru.md b/README.ru.md index f93deb4..491d92f 100644 --- a/README.ru.md +++ b/README.ru.md @@ -38,7 +38,7 @@ cp -r opencart-module/* /path/to/site/root На странице настроек модуля укажите URL retailCRM и ключ авторизации, после сохранения этих данных укажите соответствия справочников типов доставок, оплат и статусов заказа. -#### Получение измений из retailCRM +#### Получение изменений из retailCRM Для получения изменений и новых данных добавьте в cron следующую запись: diff --git a/VERSION b/VERSION index ee74734..627a3f4 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.1.0 +4.1.1 diff --git a/composer.json b/composer.json index 97132b6..581e825 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,8 @@ "ext-json": "*" }, "require-dev": { - "consolidation/robo": "~1.0" + "consolidation/robo": "~1.0", + "beyondit/opencart-test-suite": "~3.0", + "opencart/opencart": "3.0.2.0" } } diff --git a/src/upload/system/library/retailcrm/lib/service/CorporateCustomer.php b/src/upload/system/library/retailcrm/lib/service/CorporateCustomer.php index fba574f..696197b 100644 --- a/src/upload/system/library/retailcrm/lib/service/CorporateCustomer.php +++ b/src/upload/system/library/retailcrm/lib/service/CorporateCustomer.php @@ -163,12 +163,9 @@ private function createAddressAndCompany($order_data, $corp_client_id) { if ($address_response && $address_response->isSuccessful()) { $company = CorporateCustomerBuilder::create(false) ->setCompany($order_data['payment_company']) + ->setCompanyAddressId($address_response['id']) ->buildCompany($order_data); - $company['address'] = array( - 'id' => $address_response['id'] - ); - $this->api->customersCorporateCompaniesCreate($corp_client_id, $company, 'id'); } } @@ -180,11 +177,14 @@ private function createAddressAndCompany($order_data, $corp_client_id) { * @return void */ private function updateOrCreateAddress($order_data, $corp_client) { + $address_id = null; $addresses_response = $this->api->customersCorporateAddresses($corp_client['id'], array(), null, null, 'id'); $corp_address = CorporateCustomerBuilder::create(false)->buildAddress($order_data); + if ($addresses_response && $addresses_response->isSuccessful() && !empty($addresses_response['addresses'])) { foreach ($addresses_response['addresses'] as $address) { if (Utils::addressEquals($corp_address, $address)) { + $address_id = $address['id']; $exist_address = $address; break; @@ -193,25 +193,35 @@ private function updateOrCreateAddress($order_data, $corp_client) { } if (!isset($exist_address)) { - $this->api->customersCorporateAddressesCreate( + $response = $this->api->customersCorporateAddressesCreate( $corp_client['id'], $corp_address, 'id' ); - } - if (!empty($corp_client['mainCompany'])) { - $company = CorporateCustomerBuilder::create(false) - ->setCompany($order_data['payment_company']) - ->buildCompany($order_data); + if ($response && $response->isSuccessful() && isset($response['id'])) { + $address_id = $response['id']; + } + } - $this->api->customersCorporateCompaniesEdit( - $corp_client['id'], - $corp_client['mainCompany']['id'], - $company, - 'id', - 'id' - ); + $company = CorporateCustomerBuilder::create(false) + ->setCompany($order_data['payment_company']) + ->setCompanyAddressId($address_id) + ->buildCompany($order_data); + $companies = $this->api->customersCorporateCompanies($corp_client['id'], array(), null, null, 'id'); + + if ($companies && $companies->isSuccessful() && !empty($companies['companies'])) { + foreach ($companies['companies'] as $crm_company) { + if ($crm_company['name'] === $order_data['payment_company']) { + $this->api->customersCorporateCompaniesEdit( + $corp_client['id'], + $crm_company['id'], + $company, + 'id', + 'id' + ); + } + } } } } diff --git a/src/upload/system/library/retailcrm/lib/service/CorporateCustomerBuilder.php b/src/upload/system/library/retailcrm/lib/service/CorporateCustomerBuilder.php index abbe299..effcdb1 100644 --- a/src/upload/system/library/retailcrm/lib/service/CorporateCustomerBuilder.php +++ b/src/upload/system/library/retailcrm/lib/service/CorporateCustomerBuilder.php @@ -3,6 +3,8 @@ namespace retailcrm\service; class CorporateCustomerBuilder { + private $is_main_company = false; + private $company_address_id = null; private $data = array(); private $company; @@ -36,6 +38,28 @@ public function build() { return $this->data; } + /** + * @param bool $isMainCompany + * + * @return CorporateCustomerBuilder + */ + public function setIsMainCompany($isMainCompany) + { + $this->is_main_company = $isMainCompany; + return $this; + } + + /** + * @param int $companyAddressId + * + * @return CorporateCustomerBuilder + */ + public function setCompanyAddressId($companyAddressId) + { + $this->company_address_id = $companyAddressId; + return $this; + } + public function setCompany($company) { $this->company = $company; @@ -83,14 +107,23 @@ public function buildLegalAddress($data) { } public function buildCompany($data) { - return array( - 'isMain' => true, + $company = array( 'name' => $this->company, 'contragent' => array( 'legalAddress' => $this->buildLegalAddress($data), 'contragentType' => 'legal-entity', ) ); + + if ($this->is_main_company) { + $company['isMain'] = true; + } + + if (null !== $this->company_address_id) { + $company['address'] = array('id' => $this->company_address_id); + } + + return $company; } public function addCompany($data) { diff --git a/tests/system/lib/service/CorporateCustomerBuilderTest.php b/tests/system/lib/service/CorporateCustomerBuilderTest.php index 3328bca..51f52a4 100644 --- a/tests/system/lib/service/CorporateCustomerBuilderTest.php +++ b/tests/system/lib/service/CorporateCustomerBuilderTest.php @@ -21,17 +21,26 @@ public function testBuild() { 'shipping_address_2' => '' ); - $corp = \retailcrm\service\CorporateCustomerBuilder::create() + $builder = \retailcrm\service\CorporateCustomerBuilder::create(); + $corp = $builder ->setCustomerExternalId(1) ->setCompany('Company') ->addCompany($data) ->addAddress($data) ->build(); + $company_with_is_main = $builder->setIsMainCompany(true)->buildCompany($data); + $company_with_address = $builder->setCompanyAddressId(12)->buildCompany($data); - $this->assertNotEmpty($corp); - $this->assertNotEmpty($corp['addresses']); - $this->assertNotEmpty($corp['companies']); - $this->assertNotEmpty($corp['customerContacts'][0]['customer']); - $this->assertEquals(1, $corp['customerContacts'][0]['customer']['externalId']); + self::assertNotEmpty($corp); + self::assertNotEmpty($corp['addresses']); + self::assertNotEmpty($corp['companies']); + self::assertFalse(isset($corp['companies'][0]['isMain'])); + self::assertArrayHasKey('isMain', $company_with_is_main); + self::assertTrue($company_with_is_main['isMain']); + self::assertArrayHasKey('address', $company_with_address); + self::assertNotEmpty($company_with_address['address']); + self::assertEquals(12, $company_with_address['address']['id']); + self::assertNotEmpty($corp['customerContacts'][0]['customer']); + self::assertEquals(1, $corp['customerContacts'][0]['customer']['externalId']); } }