Skip to content

Commit

Permalink
Unified address logic (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neur0toxine authored Sep 2, 2020
1 parent 739c277 commit 2dbccd8
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 41 deletions.
14 changes: 8 additions & 6 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 3 additions & 1 deletion .docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions .env-dist
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,5 +14,5 @@ [email protected]
SERVER_PORT=8080
SERVER_URL=http://localhost

TEST_SUITE=
OPENCART=
TEST_SUITE=3.0
OPENCART=3.0.2.0
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v.4.1.1
* Обновлена механика обработки адресов покупателей

## v.4.1.0
* Добавлена возможность подключения Online Consultant

Expand Down
2 changes: 1 addition & 1 deletion README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cp -r opencart-module/* /path/to/site/root

На странице настроек модуля укажите URL retailCRM и ключ авторизации, после сохранения этих данных укажите соответствия справочников типов доставок, оплат и статусов заказа.

#### Получение измений из retailCRM
#### Получение изменений из retailCRM

Для получения изменений и новых данных добавьте в cron следующую запись:

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.0
4.1.1
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand All @@ -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;
Expand All @@ -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'
);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace retailcrm\service;

class CorporateCustomerBuilder {
private $is_main_company = false;
private $company_address_id = null;
private $data = array();
private $company;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 15 additions & 6 deletions tests/system/lib/service/CorporateCustomerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit 2dbccd8

Please sign in to comment.