Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions local/php_interface/include/classes/Otus/Event/DealHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Otus\Event;

use Bitrix\Main\ArgumentException;
use Bitrix\Main\Loader;
use Bitrix\Crm\Service\Container;
use Bitrix\Iblock\Elements;
use Bitrix\Main\LoaderException;

class DealHandler
{
/**
* Обработчик после обновления сделки
*
* @param $arFields
* @return void
* @throws ArgumentException
* @throws LoaderException
*/
public static function onAfterDealUpdate(&$arFields)
{
Loader::IncludeModule("iblock");


$item = Elements\ElementApplicationsTable::query()
->setSelect(['ID', 'DEAL' => 'DEAL', 'RESPONSIBLE' => 'RESPONSIBLE', 'SUM' => 'SUM'])
->where('DEAL.VALUE', $arFields['ID'])
->fetch();


if($item){

$sum = 0;
$currency = '';

if (str_contains($item['SUMVALUE'], '|')) {
list($sum, $currency) = explode('|', $item['SUMVALUE']);
}

// $arFields['OPPORTUNITY']
// $arFields['CURRENCY_ID']
// $arFields['ASSIGNED_BY_ID']


$dealFactory = Container::getInstance()->getFactory(\CCrmOwnerType::Deal);
$dealItem = $dealFactory->getItem($arFields['ID']);
$currentCurrency = $dealItem->get('CURRENCY_ID');
$currentSum = (float)$dealItem->get('OPPORTUNITY');
$currentResponsible = $dealItem->get('ASSIGNED_BY_ID');

$properties = [];

if((float)$sum != $currentSum || $currency != $currentCurrency){
$properties['SUM'] = $currentSum.'|'.$currency;
}

if($currentResponsible != $item['RESPONSIBLEIBLOCK_GENERIC_VALUE']){
$properties['ASSIGNED_BY_ID'] = $currentResponsible;
}

if($properties){

\CIBlockElement::SetPropertyValuesEx(
$item['ID'],
false,
$properties
);

}

}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Otus\Event;

use Bitrix\Iblock\IblockTable;
use Bitrix\Main\ArgumentException;
use Bitrix\Main\Loader;
use Bitrix\Crm\Service\Container;
use Bitrix\Iblock\Elements;
use Bitrix\Main\LoaderException;
use Bitrix\Main\ObjectPropertyException;
use Bitrix\Main\SystemException;

class IblockElementHandler
{
const IBLOCK_APPLICATIONS_CODE = 'applications';

/**
* Возвращает ID инфоблока заявки
*
* @return int
* @throws ArgumentException
* @throws LoaderException
* @throws ObjectPropertyException
* @throws SystemException
*/
public static function getApplicationsBlockId(): int
{

Loader::IncludeModule("iblock");

$iblockApp = IblockTable::query()
->addSelect('ID')
->where('CODE', self::IBLOCK_APPLICATIONS_CODE)
->setLimit(1)
->setCacheTtl(3600)
->fetch();

return $iblockApp['ID'] ?? 0;
}

/**
* Обработчик после обновления элемента инфоблока
*
* @param $arFields
* @return void
* @throws ArgumentException
* @throws LoaderException
* @throws ObjectPropertyException
* @throws SystemException
*/
public static function onElementAfterUpdate(&$arFields)
{


$iblockApp = self::getApplicationsBlockId();

if($iblockApp > 0 && $iblockApp == $arFields['IBLOCK_ID']){

if (!Loader::includeModule('crm')) {
return;
}

$dealFactory =
Container::getInstance()->getFactory(\CCrmOwnerType::Deal);

$item = Elements\ElementApplicationsTable::query()
->setSelect(['DEAL' => 'DEAL', 'RESPONSIBLE' => 'RESPONSIBLE', 'SUM' => 'SUM'])
->where('ID', $arFields['ID'])
->fetch();

$sum = 0;
$currency = '';

if (str_contains($item['SUMVALUE'], '|')) {
list($sum, $currency) = explode('|', $item['SUMVALUE']);
}

$existedDealId = $item['DEALVALUE'];
$dealItem = $dealFactory->getItem($existedDealId);

if($dealItem){

$update = false;

$currentSum = (float)$dealItem->get('OPPORTUNITY');
$newSum = (float)$sum;

if($currentSum !== $newSum){
$dealItem->set('OPPORTUNITY', $sum);
$update = true;
}

$currentCurrency = $dealItem->get('CURRENCY_ID');
if($currentCurrency !== $currency){
$dealItem->set('CURRENCY_ID', $currency);
$update = true;
}

$currentResponsible = (int)$dealItem->get('ASSIGNED_BY_ID');
$newResponsible = (int)$item['RESPONSIBLEIBLOCK_GENERIC_VALUE'];

if ($currentResponsible !== $newResponsible) {
$dealItem->set('ASSIGNED_BY_ID', $item['RESPONSIBLEIBLOCK_GENERIC_VALUE']);
$update = true;
}

if($update){
$dealUpdateOperation = $dealFactory->getUpdateOperation($dealItem);
$updateResult = $dealUpdateOperation->launch();
}

}


}
}

}
28 changes: 26 additions & 2 deletions local/php_interface/include/events.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

use Otus\Event\DealHandler;
use Otus\Event\IblockElementEvent;
use Otus\Event\IblockElementHandler;

$eventManager = \Bitrix\Main\EventManager::getInstance();

$eventManager->addEventHandlerCompatible('main', 'OnPageStart', '\Otus\Event\Main::handlerPageStart');
//$eventManager->addEventHandlerCompatible('main', 'OnPageStart', '\Otus\Event\Main::handlerPageStart');

$eventManager->AddEventHandler(
'iblock',
Expand All @@ -10,4 +15,23 @@
'Otus\UserType\OnlineRecord',
'GetUserTypeDescription'
]
);
);


$eventManager->addEventHandler(
"iblock",
"OnAfterIBlockElementUpdate",
[
IblockElementHandler::class,
'onElementAfterUpdate'
]
);

$eventManager->addEventHandlerCompatible(
"crm",
"OnAfterCrmDealUpdate",
[
DealHandler::class,
'onAfterDealUpdate'
]
);
Loading