From d06cb3c6e693b7d52fb8d2cfd3b56007959ee265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=A7=D0=B0=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2?= <45812598+Chazovs@users.noreply.github.com> Date: Wed, 8 Sep 2021 13:00:50 +0300 Subject: [PATCH] Remove date for integration payment (#214) --- .docker/php.ini | 1 - .gitignore | 3 +- .../classes/general/events/RetailCrmEvent.php | 12 ++- .../general/order/RetailCrmOrder_v5.php | 10 +-- .../general/services/RetailCrmService.php | 13 +++ tests/BitrixTestCase.php | 33 ++++++++ tests/bootstrap.php | 1 + .../general/order/RetailCrmOrderTest.php | 0 ...Order_v5.php => RetailCrmOrder_v5Test.php} | 79 +++++++++++-------- 9 files changed, 107 insertions(+), 45 deletions(-) delete mode 100644 tests/classes/general/order/RetailCrmOrderTest.php rename tests/classes/general/order/{RetailCrmOrder_v5.php => RetailCrmOrder_v5Test.php} (68%) diff --git a/.docker/php.ini b/.docker/php.ini index fcb51624..c3eddbf9 100755 --- a/.docker/php.ini +++ b/.docker/php.ini @@ -7,7 +7,6 @@ log_errors = On display_startup_errors = On cgi.fix_pathinfo = 0 date.timezone = "Europe/Moscow" -mbstring.func_overload = 2 mbstring.internal_encoding = "UTF-8" default_charset = utf-8 max_input_vars = 10000 diff --git a/.gitignore b/.gitignore index 73dc1cb0..d1d5219f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ /vendor/ .env .phpunit.result.cache -/release/ \ No newline at end of file +/release/ +coverage.xml \ No newline at end of file diff --git a/intaro.retailcrm/classes/general/events/RetailCrmEvent.php b/intaro.retailcrm/classes/general/events/RetailCrmEvent.php index 15b5e74b..c91df52b 100644 --- a/intaro.retailcrm/classes/general/events/RetailCrmEvent.php +++ b/intaro.retailcrm/classes/general/events/RetailCrmEvent.php @@ -1,5 +1,6 @@ 'YYYY-MM-DD HH:MI:SS']); + $culture = new Culture(['FORMAT_DATETIME' => 'YYYY-MM-DD HH:MI:SS']); $paymentToCrm['paidAt'] = $arPayment['DATE_PAID']->toString($culture); } elseif (is_string($arPayment['DATE_PAID'])) { $paymentToCrm['paidAt'] = $arPayment['DATE_PAID']; } } - if (!empty($optionsPayStatuses[$arPayment['PAID']])) { + if (!empty($optionsPayStatuses[$arPayment['PAID']]) && !$isIntegrationPayment) { $paymentToCrm['status'] = $optionsPayStatuses[$arPayment['PAID']]; } diff --git a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php index d7ba19f7..ad6ef492 100644 --- a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php +++ b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php @@ -274,11 +274,12 @@ class RetailCrmOrder $order['weight'] = $weight; } - $integrationPayment = RetailcrmConfigProvider::getIntegrationPaymentTypes(); //payments $payments = []; foreach ($arOrder['PAYMENTS'] as $payment) { + $isIntegrationPayment = RetailCrmService::isIntegrationPayment($payment['PAY_SYSTEM_ID'] ?? null); + if (!empty($payment['PAY_SYSTEM_ID']) && isset($arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']])) { $pm = [ 'type' => $arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']] @@ -288,14 +289,11 @@ class RetailCrmOrder $pm['externalId'] = RCrmActions::generatePaymentExternalId($payment['ID']); } - if (!empty($payment['DATE_PAID'])) { + if (!empty($payment['DATE_PAID']) && !$isIntegrationPayment) { $pm['paidAt'] = new \DateTime($payment['DATE_PAID']); } - if ( - !empty($arParams['optionsPayment'][$payment['PAID']]) - && !in_array($arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']], $integrationPayment) - ) { + if (!empty($arParams['optionsPayment'][$payment['PAID']]) && !$isIntegrationPayment) { $pm['status'] = $arParams['optionsPayment'][$payment['PAID']]; } diff --git a/intaro.retailcrm/classes/general/services/RetailCrmService.php b/intaro.retailcrm/classes/general/services/RetailCrmService.php index c6e08aac..22f8d973 100644 --- a/intaro.retailcrm/classes/general/services/RetailCrmService.php +++ b/intaro.retailcrm/classes/general/services/RetailCrmService.php @@ -110,4 +110,17 @@ class RetailCrmService return $result; } + + /** + * @param int|null $paySystemId + * + * @return bool + */ + public static function isIntegrationPayment(?int $paySystemId): bool { + return in_array( + RetailcrmConfigProvider::getPaymentTypes()[$paySystemId] ?? null, + RetailcrmConfigProvider::getIntegrationPaymentTypes(), + true + ); + } } diff --git a/tests/BitrixTestCase.php b/tests/BitrixTestCase.php index ae7809af..0e758295 100644 --- a/tests/BitrixTestCase.php +++ b/tests/BitrixTestCase.php @@ -33,4 +33,37 @@ class BitrixTestCase extends \PHPUnit\Framework\TestCase // без этого вызова Mockery не будет работать \Mockery::close(); } + + public function getArFields(): array + { + return [ + 'ID' => 1, + 'NUMBER' => "1", + 'USER_ID' => "1", + 'STATUS_ID' => "1", + 'PERSON_TYPE_ID' => 'bitrixType', + 'DATE_INSERT' => '2015-02-22 00:00:00', + 'USER_DESCRIPTION' => 'userComment', + 'COMMENTS' => 'managerComment', + 'PRICE_DELIVERY' => '100', + 'PROPS' => ['properties' => []], + 'DELIVERYS' => [[ + 'id' => 'test', + 'service' => 'service' + ]], + 'BASKET' => [], + 'PAYMENTS' => [[ + 'ID' => 1, + 'PAY_SYSTEM_ID' => 1, + 'SUM' => 1000, + 'DATE_PAID' => $this->getDateTime(), + 'PAID' => 'Y' + ]] + ]; + } + + public function getDateTime(): \Bitrix\Main\Type\DateTime + { + return \Bitrix\Main\Type\DateTime::createFromPhp(new DateTime('2000-01-01')); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9ed17302..fedb49ed 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -38,3 +38,4 @@ VALUES ('2020-05-08 19:04:03', 'iblock', '500', '500', '23791', 'image/jpeg', 'i $DB->Query($strSql); require_once 'BitrixTestCase.php'; + diff --git a/tests/classes/general/order/RetailCrmOrderTest.php b/tests/classes/general/order/RetailCrmOrderTest.php deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/classes/general/order/RetailCrmOrder_v5.php b/tests/classes/general/order/RetailCrmOrder_v5Test.php similarity index 68% rename from tests/classes/general/order/RetailCrmOrder_v5.php rename to tests/classes/general/order/RetailCrmOrder_v5Test.php index 7617ec45..796c9244 100644 --- a/tests/classes/general/order/RetailCrmOrder_v5.php +++ b/tests/classes/general/order/RetailCrmOrder_v5Test.php @@ -3,7 +3,8 @@ /** * Class RetailCrmOrder_v5Test */ -class RetailCrmOrder_v5Test extends \PHPUnit_Framework_TestCase { +class RetailCrmOrder_v5Test extends BitrixTestCase { + /** * setUp method */ @@ -13,12 +14,6 @@ class RetailCrmOrder_v5Test extends \PHPUnit_Framework_TestCase { COption::SetOptionString('intaro.retailcrm', 'api_version', 'v5'); CModule::IncludeModule('intaro.retailcrm'); - RetailcrmConfigProvider::setOrderTypes(['bitrixType' => 'crmType']); - RetailcrmConfigProvider::setContragentTypes(['bitrixType' => 'crmType']); - RetailcrmConfigProvider::setPaymentStatuses([1 => 'paymentStatus']); - RetailcrmConfigProvider::setPaymentTypes(['bitrixPayment' => 'crmPayment']); - RetailcrmConfigProvider::setDeliveryTypes(['test' => 'test']); - RetailcrmConfigProvider::setSendPaymentAmount('N'); } /** @@ -44,40 +39,55 @@ class RetailCrmOrder_v5Test extends \PHPUnit_Framework_TestCase { )); } + /** + * @dataProvider orderSendProvider + */ + public function testOrderSendWitIntegrationPayment( + array $arFields, + array $arParams, + string $methodApi, + array $expected + ): void { + RetailcrmConfigProvider::setIntegrationPaymentTypes(['testPayment']); + + $orderSend = RetailCrmOrder::orderSend( + $arFields, + new stdClass(), + $arParams, + false, + null, + $methodApi + ); + + unset($expected['payments'][0]['paidAt'], $expected['payments'][0]['status']); + static::assertEquals($expected['payments'][0], $orderSend['payments'][0]); + } + + public function initSystemData(): void + { + RetailcrmConfigProvider::setOrderTypes(['bitrixType' => 'crmType']); + RetailcrmConfigProvider::setContragentTypes(['bitrixType' => 'individual']); + RetailcrmConfigProvider::setPaymentStatuses([1 => 'paymentStatus']); + RetailcrmConfigProvider::setPaymentTypes([1 => 'testPayment']); + RetailcrmConfigProvider::setDeliveryTypes(['test' => 'test']); + RetailcrmConfigProvider::setSendPaymentAmount('N'); + } + /** * @return array[] */ public function orderSendProvider() { - $arFields = [ - 'ID' => 1, - 'NUMBER' => 1, - 'USER_ID' => 1, - 'STATUS_ID' => 1, - 'PERSON_TYPE_ID' => 'bitrixType', - 'DATE_INSERT' => '2015-02-22 00:00:00', - 'USER_DESCRIPTION' => 'userComment', - 'COMMENTS' => 'managerComment', - 'PRICE_DELIVERY' => '100', - 'PROPS' => ['properties' => []], - 'DELIVERYS' => [[ - 'id' => 'test', - 'service' => 'service' - ]], - 'BASKET' => [], - 'PAYMENTS' => [[ - 'ID' => 1, - 'PAY_SYSTEM_ID' => 'bitrixPayment', - 'SUM' => 1000 - ]] - ]; + $arFields = $this->getArFields(); + $this->initSystemData(); + $arParams = [ 'optionsOrderTypes' => RetailcrmConfigProvider::getOrderTypes(), 'optionsPayStatuses' => RetailcrmConfigProvider::getPaymentStatuses(), 'optionsContragentType' => RetailcrmConfigProvider::getContragentTypes(), 'optionsDelivTypes' => RetailcrmConfigProvider::getDeliveryTypes(), 'optionsPayTypes' => RetailcrmConfigProvider::getPaymentTypes(), - 'optionsPayment' => [] + 'optionsPayment' => ['Y' => 'paid'] ]; return [[ @@ -86,7 +96,7 @@ class RetailCrmOrder_v5Test extends \PHPUnit_Framework_TestCase { 'methodApi' => 'ordersCreate', 'expected' => [ 'number' => $arFields['NUMBER'], - 'externalId' => $arFields['ID'], + 'externalId' => (string) $arFields['ID'], 'createdAt' => $arFields['DATE_INSERT'], 'customer' => ['externalId' => $arFields['USER_ID']], 'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']], @@ -95,14 +105,17 @@ class RetailCrmOrder_v5Test extends \PHPUnit_Framework_TestCase { 'managerComment' => $arFields['COMMENTS'], 'delivery' => [ 'cost' => $arFields['PRICE_DELIVERY'], - 'code' => $arFields['DELIVERYS'][0]['service'], + 'code' => $arFields['DELIVERYS'][0]['id'], + 'service' => ['code' => $arFields['DELIVERYS'][0]['service']] ], 'contragent' => [ 'contragentType' => $arParams['optionsContragentType'][$arFields['PERSON_TYPE_ID']] ], 'payments' => [[ 'type' => $arParams['optionsPayTypes'][$arFields['PAYMENTS'][0]['PAY_SYSTEM_ID']], - 'externalId' => RCrmActions::generatePaymentExternalId($arFields['PAYMENTS'][0]['ID']) + 'externalId' => RCrmActions::generatePaymentExternalId($arFields['PAYMENTS'][0]['ID']), + 'status' => 'paid', + 'paidAt' => $this->getDateTime()->format('Y-m-d H:i:s') ]] ], ]];