From 4125565570411840111e8204daf44bb53b18d019 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Fri, 25 Sep 2020 12:17:29 +0300 Subject: [PATCH] Ability to disable payment passthrough (#140) * optional payment total in the order * updater won't fail in case of error --- .../general/RetailcrmConfigProvider.php | 80 +++++++++++++ .../classes/general/RetailcrmConstants.php | 1 + .../classes/general/events/RetailCrmEvent.php | 7 +- .../general/order/RetailCrmOrder_v5.php | 11 +- intaro.retailcrm/lang/ru/options.php | 1 + intaro.retailcrm/options.php | 12 +- intaro.retailcrm/update/updater-5.5.0.php | 8 ++ intaro.retailcrm/updater.php | 29 +++++ .../general/order/RetailCrmOrder_v5.php | 110 ++++++++++++++++++ 9 files changed, 254 insertions(+), 5 deletions(-) create mode 100644 intaro.retailcrm/update/updater-5.5.0.php create mode 100644 intaro.retailcrm/updater.php create mode 100644 tests/classes/general/order/RetailCrmOrder_v5.php diff --git a/intaro.retailcrm/classes/general/RetailcrmConfigProvider.php b/intaro.retailcrm/classes/general/RetailcrmConfigProvider.php index 070887f8..5b750462 100644 --- a/intaro.retailcrm/classes/general/RetailcrmConfigProvider.php +++ b/intaro.retailcrm/classes/general/RetailcrmConfigProvider.php @@ -212,6 +212,16 @@ class RetailcrmConfigProvider return static::$orderTypes; } + /** + * setOrderTypes + * + * @param array $orderTypesArr + */ + public static function setOrderTypes($orderTypesArr) + { + static::setOption(RetailcrmConstants::CRM_ORDER_TYPES_ARR, serialize(RCrmActions::clearArr($orderTypesArr))); + } + /** * getDeliveryTypes * @@ -226,6 +236,16 @@ class RetailcrmConfigProvider return static::$deliveryTypes; } + /** + * setDeliveryTypes + * + * @param array $deliveryTypesArr + */ + public static function setDeliveryTypes($deliveryTypesArr) + { + static::setOption(RetailcrmConstants::CRM_DELIVERY_TYPES_ARR, serialize(RCrmActions::clearArr($deliveryTypesArr))); + } + /** * getPaymentTypes * @@ -240,6 +260,16 @@ class RetailcrmConfigProvider return static::$paymentTypes; } + /** + * setPaymentTypes + * + * @param array $paymentTypesArr + */ + public static function setPaymentTypes($paymentTypesArr) + { + static::setOption(RetailcrmConstants::CRM_PAYMENT_TYPES, serialize(RCrmActions::clearArr($paymentTypesArr))); + } + /** * getPaymentStatuses * @@ -254,6 +284,16 @@ class RetailcrmConfigProvider return static::$paymentStatuses; } + /** + * getPaymentStatuses + * + * @param array $paymentStatusesArr + */ + public static function setPaymentStatuses($paymentStatusesArr) + { + static::setOption(RetailcrmConstants::CRM_PAYMENT_STATUSES, serialize(RCrmActions::clearArr($paymentStatusesArr))); + } + /** * getPayment * @@ -310,6 +350,16 @@ class RetailcrmConfigProvider return static::$contragentTypes; } + /** + * setContragentTypes + * + * @param array $contragentTypeArr + */ + public static function setContragentTypes($contragentTypeArr) + { + static::setOption(RetailcrmConstants::CRM_CONTRAGENT_TYPE, serialize(RCrmActions::clearArr($contragentTypeArr))); + } + /** * getCustomFields * @@ -350,6 +400,36 @@ class RetailcrmConfigProvider return static::getOption(RetailcrmConstants::CRM_ORDER_LAST_ID); } + /** + * getSendPaymentAmount + * + * @return bool|string|null + */ + public static function getSendPaymentAmount() + { + return static::getOption(RetailcrmConstants::SEND_PAYMENT_AMOUNT); + } + + /** + * setSendPaymentAmount + * + * @param string $value + */ + public static function setSendPaymentAmount($value) + { + static::setOption(RetailcrmConstants::SEND_PAYMENT_AMOUNT, $value); + } + + /** + * Returns true if payment amount should be sent from CMS to retailCRM. + * + * @return bool|string|null + */ + public static function shouldSendPaymentAmount() + { + return static::getSendPaymentAmount() === 'Y'; + } + /** * setLastOrderId * diff --git a/intaro.retailcrm/classes/general/RetailcrmConstants.php b/intaro.retailcrm/classes/general/RetailcrmConstants.php index cba861ec..ee25a628 100644 --- a/intaro.retailcrm/classes/general/RetailcrmConstants.php +++ b/intaro.retailcrm/classes/general/RetailcrmConstants.php @@ -77,4 +77,5 @@ class RetailcrmConstants const CANCEL_PROPERTY_CODE = 'INTAROCRM_IS_CANCELED'; const CRM_INTEGRATION_DELIVERY = 'integration_delivery'; const CRM_SHIPMENT_DEDUCTED = 'shipment_deducted'; + const SEND_PAYMENT_AMOUNT = 'send_payment_amount'; } diff --git a/intaro.retailcrm/classes/general/events/RetailCrmEvent.php b/intaro.retailcrm/classes/general/events/RetailCrmEvent.php index 5773406a..4723e3ab 100644 --- a/intaro.retailcrm/classes/general/events/RetailCrmEvent.php +++ b/intaro.retailcrm/classes/general/events/RetailCrmEvent.php @@ -486,8 +486,7 @@ class RetailCrmEvent if (!empty($arPayment['PAY_SYSTEM_ID']) && isset($optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']])) { $paymentToCrm = array( - 'type' => $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']], - 'amount' => $arPayment['SUM'] + 'type' => $optionsPaymentTypes[$arPayment['PAY_SYSTEM_ID']] ); if (!empty($arPayment['ID'])) { @@ -510,6 +509,10 @@ class RetailCrmEvent if (!empty($arPayment['ORDER_ID'])) { $paymentToCrm['order']['externalId'] = $arPayment['ORDER_ID']; } + + if (RetailcrmConfigProvider::shouldSendPaymentAmount()) { + $paymentToCrm['amount'] = $arPayment['SUM']; + } } else { RCrmActions::eventLog('RetailCrmEvent::paymentSave', 'payments', 'OrderID = ' . $arPayment['ID'] . '. Payment not found.'); return false; diff --git a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php index d37e86a8..7b86aa5f 100644 --- a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php +++ b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php @@ -255,18 +255,25 @@ class RetailCrmOrder foreach ($arFields['PAYMENTS'] as $payment) { if (!empty($payment['PAY_SYSTEM_ID']) && isset($arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']])) { $pm = array( - 'type' => $arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']], - 'amount' => $payment['SUM'] + 'type' => $arParams['optionsPayTypes'][$payment['PAY_SYSTEM_ID']] ); + if (!empty($payment['ID'])) { $pm['externalId'] = RCrmActions::generatePaymentExternalId($payment['ID']); } + if (!empty($payment['DATE_PAID'])) { $pm['paidAt'] = new \DateTime($payment['DATE_PAID']); } + if (!empty($arParams['optionsPayment'][$payment['PAID']])) { $pm['status'] = $arParams['optionsPayment'][$payment['PAID']]; } + + if (RetailcrmConfigProvider::shouldSendPaymentAmount()) { + $pm['amount'] = $payment['SUM']; + } + $payments[] = $pm; } else { RCrmActions::eventLog( diff --git a/intaro.retailcrm/lang/ru/options.php b/intaro.retailcrm/lang/ru/options.php index 268eacab..7c99b46d 100644 --- a/intaro.retailcrm/lang/ru/options.php +++ b/intaro.retailcrm/lang/ru/options.php @@ -75,6 +75,7 @@ $MESS ['ORDER_NUMBERS'] = 'Транслировать номера заказо $MESS ['CRM_API_VERSION'] = 'Версия API клиента'; $MESS ['CURRENCY'] = 'Валюта, устанавливаемая в заказе при выгрузке из CRM'; $MESS ['ORDER_DIMENSIONS'] = 'Передавать габариты и вес товаров в заказе'; +$MESS ['SEND_PAYMENT_AMOUNT'] = 'Передавать сумму оплаты в заказе'; $MESS ['INVENTORIES_UPLOAD'] = 'Включить выгрузку остатков в разрезе складов'; $MESS ['INVENTORIES'] = 'Склады'; diff --git a/intaro.retailcrm/options.php b/intaro.retailcrm/options.php index f962b1b2..9780b790 100644 --- a/intaro.retailcrm/options.php +++ b/intaro.retailcrm/options.php @@ -344,8 +344,8 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { } //order numbers $orderNumbers = htmlspecialchars(trim($_POST['order-numbers'])) ? htmlspecialchars(trim($_POST['order-numbers'])) : 'N'; - $orderDimensions = htmlspecialchars(trim($_POST[$CRM_DIMENSIONS])) ? htmlspecialchars(trim($_POST[$CRM_DIMENSIONS])) : 'N'; + $sendPaymentAmount = htmlspecialchars(trim($_POST[RetailcrmConstants::SEND_PAYMENT_AMOUNT])) ? htmlspecialchars(trim($_POST[RetailcrmConstants::SEND_PAYMENT_AMOUNT])) : 'N'; //stores $bitrixStoresArr = array(); @@ -567,6 +567,7 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { COption::SetOptionString($mid, $CRM_UA, $ua); COption::SetOptionString($mid, $CRM_UA_KEYS, serialize(RCrmActions::clearArr($uaKeys))); COption::SetOptionString($mid, $CRM_DIMENSIONS, $orderDimensions); + RetailcrmConfigProvider::setSendPaymentAmount($sendPaymentAmount); COption::SetOptionString($mid, $CRM_DISCOUNT_ROUND, $discount_round); COption::SetOptionString($mid, $CRM_PURCHASE_PRICE_NULL, $purchasePrice_null); @@ -1202,6 +1203,15 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { + + + + + + + diff --git a/intaro.retailcrm/update/updater-5.5.0.php b/intaro.retailcrm/update/updater-5.5.0.php new file mode 100644 index 00000000..2ab0b0ea --- /dev/null +++ b/intaro.retailcrm/update/updater-5.5.0.php @@ -0,0 +1,8 @@ + 'crmType']); + RetailcrmConfigProvider::setContragentTypes(['bitrixType' => 'crmType']); + RetailcrmConfigProvider::setPaymentStatuses([1 => 'paymentStatus']); + RetailcrmConfigProvider::setPaymentTypes(['bitrixPayment' => 'crmPayment']); + RetailcrmConfigProvider::setDeliveryTypes(['test' => 'test']); + RetailcrmConfigProvider::setSendPaymentAmount('N'); + } + + /** + * @param array $arFields + * @param array $arParams + * @param string $methodApi + * @param array $expected + * + * @throws \Bitrix\Main\ArgumentException + * @throws \Bitrix\Main\ObjectPropertyException + * @throws \Bitrix\Main\SystemException + * @dataProvider orderSendProvider + */ + public function testOrderSend($arFields, $arParams, $methodApi, $expected) + { + self::assertEquals($expected, RetailCrmOrder::orderSend( + $arFields, + new stdClass(), + $arParams, + false, + null, + $methodApi + )); + } + + /** + * @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 + ]] + ]; + $arParams = [ + 'optionsOrderTypes' => RetailcrmConfigProvider::getOrderTypes(), + 'optionsPayStatuses' => RetailcrmConfigProvider::getPaymentStatuses(), + 'optionsContragentType' => RetailcrmConfigProvider::getContragentTypes(), + 'optionsDelivTypes' => RetailcrmConfigProvider::getDeliveryTypes(), + 'optionsPayTypes' => RetailcrmConfigProvider::getPaymentTypes(), + 'optionsPayment' => [] + ]; + + return [[ + 'arFields' => $arFields, + 'arParams' => $arParams, + 'methodApi' => 'ordersCreate', + 'expected' => [ + 'number' => $arFields['NUMBER'], + 'externalId' => $arFields['ID'], + 'createdAt' => $arFields['DATE_INSERT'], + 'customer' => ['externalId' => $arFields['USER_ID']], + 'orderType' => $arParams['optionsOrderTypes'][$arFields['PERSON_TYPE_ID']], + 'status' => $arParams['optionsPayStatuses'][$arFields['STATUS_ID']], + 'customerComment' => $arFields['USER_DESCRIPTION'], + 'managerComment' => $arFields['COMMENTS'], + 'delivery' => [ + 'cost' => $arFields['PRICE_DELIVERY'], + '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']) + ]] + ], + ]]; + } +}