From c08287d3dc6bb6f203768a71ea0c595ad5c0ebcf Mon Sep 17 00:00:00 2001 From: anton Date: Thu, 31 Oct 2024 15:58:31 +0300 Subject: [PATCH] add agent in updater --- .../general/order/RetailCrmOrder_v5.php | 16 +++---- intaro.retailcrm/options.php | 47 +++++++++++-------- intaro.retailcrm/updater.php | 21 +++++++++ 3 files changed, 57 insertions(+), 27 deletions(-) diff --git a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php index 68a9d667..9b827946 100644 --- a/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php +++ b/intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php @@ -574,10 +574,7 @@ public static function uploadOrders(int $pSize = 50, bool $failed = false, array } } - - $orderIds = array_unique(array_merge($orderIds, $orderUpdateIds)); - - + $orderIds = array_unique(array_merge($orderIds, $orderUpdateIds)); if (count($orderIds) <= 0) { return false; } @@ -609,6 +606,7 @@ public static function uploadOrders(int $pSize = 50, bool $failed = false, array ]; $recOrders = []; + $maxUpdateDate = $dateOfLastUpdate; foreach ($orderIds as $orderId) { $bitrixOrder = Order::load($orderId); @@ -637,7 +635,6 @@ public static function uploadOrders(int $pSize = 50, bool $failed = false, array } $orderCrm = RCrmActions::apiMethod($api, 'ordersGet', __METHOD__, $orderId, $site); - RetailcrmConfigProvider::setLastOrderUpdate(date("Y-m-d H:i:s")); if (isset($orderCrm['order'])) { $methodApi = 'ordersEdit'; @@ -646,18 +643,19 @@ public static function uploadOrders(int $pSize = 50, bool $failed = false, array $methodApi = 'ordersCreate'; } - self::createCustomerForOrder($api, $arCustomer, $arCustomerCorporate,$arParams, $order, $site); - if (isset($order['RESPONSIBLE_ID']) && !empty($order['RESPONSIBLE_ID'])) { $managerService = ManagerService::getInstance(); $arParams['managerId'] = $managerService->getManagerCrmId((int) $order['RESPONSIBLE_ID']); } if ($methodApi === 'ordersEdit') { - $arOrders = self::orderSend($order, $api, $arParams, true, $site, 'ordersEdit'); + $updateDate = $bitrixOrder->getField('DATE_UPDATE')->format("Y-m-d H:i:s"); + $maxUpdateDate = $updateDate > $maxUpdateDate ? $updateDate : $maxUpdateDate; + self::orderSend($order, $api, $arParams, true, $site); continue; } + self::createCustomerForOrder($api, $arCustomer, $arCustomerCorporate,$arParams, $order, $site); $arOrders = self::orderSend($order, $api, $arParams, false, $site, $methodApi); if (!$arCustomer || !$arOrders) { @@ -680,6 +678,8 @@ public static function uploadOrders(int $pSize = 50, bool $failed = false, array $recOrders[] = $orderId; } + RetailcrmConfigProvider::setLastOrderUpdate($maxUpdateDate); + if (count($ordersPack) > 0) { if (false === RetailCrmOrder::uploadCustomersList($resCustomers, $api, $arParams['optionsSitesList'])) { return false; diff --git a/intaro.retailcrm/options.php b/intaro.retailcrm/options.php index 27c93f9d..31f6f447 100644 --- a/intaro.retailcrm/options.php +++ b/intaro.retailcrm/options.php @@ -7,6 +7,7 @@ use Bitrix\Main\SystemException; use Bitrix\Main\UI\Extension; use Bitrix\Sale\Delivery\Services\Manager; +use Bitrix\Sale\Internals\OrderTable; use Intaro\RetailCrm\Component\ApiClient\ClientAdapter; use Intaro\RetailCrm\Component\ConfigProvider; use Intaro\RetailCrm\Component\Constants; @@ -315,7 +316,6 @@ // 0 - agent // 1 - event // 2 - agent without update - $agentUpload = false; $orderDischarge = (int) htmlspecialchars(trim($_POST['order-discharge'])); if (($orderDischarge != $previousDischarge) && ($orderDischarge === 0 || $orderDischarge === 2 )) { @@ -323,23 +323,7 @@ UnRegisterModuleDependences('sale', 'OnSaleOrderDeleted', $mid, 'RetailCrmEvent', "orderDelete"); UnRegisterModuleDependences('sale', 'OnSaleOrderSaved', $mid, 'RetailCrmEvent', "orderSave"); UnRegisterModuleDependences('sale', 'OnOrderUpdate', $mid, 'RetailCrmEvent', "onUpdateOrder"); - $agentUpload = true; - - if ($orderDischarge === 0 && COption::GetOptionString($mid, Constants::LAST_ORDER_UPDATE) === '') { - COption::SetOptionString($mid, Constants::LAST_ORDER_UPDATE, date("Y-m-d H:i:s")); - } else { - COption::RemoveOption($mid, Constants::LAST_ORDER_UPDATE); - } - - } elseif (($orderDischarge != $previousDischarge) && ($orderDischarge === 1)) { - // event dependencies - RegisterModuleDependences('sale', 'OnOrderUpdate', $mid, 'RetailCrmEvent', "onUpdateOrder"); - RegisterModuleDependences('sale', 'OnSaleOrderDeleted', $mid, 'RetailCrmEvent', "orderDelete"); - RegisterModuleDependences('sale', 'OnSaleOrderSaved', $mid, 'RetailCrmEvent', "orderSave"); - COption::RemoveOption($mid, Constants::LAST_ORDER_UPDATE); - } - - if ($agentUpload) { + $dateAgent = new DateTime(); $intAgent = new DateInterval('PT60S'); $dateAgent->add($intAgent); @@ -354,7 +338,32 @@ $dateAgent->format('d.m.Y H:i:s'), 30 ); - } else { + + if ($orderDischarge === 0) { + COption::SetOptionString($mid, Constants::LAST_ORDER_UPDATE, date("Y-m-d H:i:s")); + } else { + COption::RemoveOption($mid, Constants::LAST_ORDER_UPDATE); + } + + $dbOrder = OrderTable::GetList([ + 'order' => ['ID' => 'DESC'], + 'limit' => 1, + 'select' => ['ID'], + ]); + + $arOrder = $dbOrder->fetch(); + if ($dbOrder) { + COption::SetOptionString($mid, Constants::CRM_ORDER_LAST_ID, $arOrder['ID']); + } else { + COption::SetOptionString($mid, Constants::CRM_ORDER_LAST_ID, 0); + } + + } elseif (($orderDischarge != $previousDischarge) && ($orderDischarge === 1)) { + // event dependencies + RegisterModuleDependences('sale', 'OnOrderUpdate', $mid, 'RetailCrmEvent', "onUpdateOrder"); + RegisterModuleDependences('sale', 'OnSaleOrderDeleted', $mid, 'RetailCrmEvent', "orderDelete"); + RegisterModuleDependences('sale', 'OnSaleOrderSaved', $mid, 'RetailCrmEvent', "orderSave"); + COption::RemoveOption($mid, Constants::LAST_ORDER_UPDATE); CAgent::RemoveAgent("RCrmActions::uploadOrdersAgent();", $mid); } diff --git a/intaro.retailcrm/updater.php b/intaro.retailcrm/updater.php index 1bb1eadb..79f0c900 100644 --- a/intaro.retailcrm/updater.php +++ b/intaro.retailcrm/updater.php @@ -1188,6 +1188,27 @@ function update() UnRegisterModuleDependences('sale', 'OnSaleOrderSaved', 'intaro.retailcrm', $loyaltyEventClass, 'OnSaleOrderSavedHandler'); UnRegisterModuleDependences('sale', 'OnSaleComponentOrderResultPrepared', 'intaro.retailcrm', $loyaltyEventClass, 'OnSaleComponentOrderResultPreparedHandler'); } + + $orderDischarge = Option::get('intaro.retailcrm', 'order_discharge'); + + if ($orderDischarge === '0' || $orderDischarge === '2') { + $dateAgent = new DateTime(); + $dateAgent->add('PT60S'); + CAgent::AddAgent( + 'RCrmActions::uploadOrdersAgent();', + 'intaro.retailcrm', + 'N', + 180, + $dateAgent->format('d.m.Y H:i:s'), + 'Y', + $dateAgent->format('d.m.Y H:i:s'), + 30 + ); + + if ($orderDischarge === '0') { + COption::SetOptionString('intaro.retailcrm', 'last_order_update', date("Y-m-d H:i:s")); + } + } } function createCustomPropertyFile()