Skip to content

Commit

Permalink
add agent in updater
Browse files Browse the repository at this point in the history
  • Loading branch information
anton committed Oct 31, 2024
1 parent 8aba1b1 commit c08287d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
16 changes: 8 additions & 8 deletions intaro.retailcrm/classes/general/order/RetailCrmOrder_v5.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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';
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
47 changes: 28 additions & 19 deletions intaro.retailcrm/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -315,31 +316,14 @@
// 0 - agent
// 1 - event
// 2 - agent without update
$agentUpload = false;
$orderDischarge = (int) htmlspecialchars(trim($_POST['order-discharge']));

if (($orderDischarge != $previousDischarge) && ($orderDischarge === 0 || $orderDischarge === 2 )) {
// remove depenedencies
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);
Expand All @@ -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);
}

Expand Down
21 changes: 21 additions & 0 deletions intaro.retailcrm/updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c08287d

Please sign in to comment.