SHARE
TWEET

Untitled

a guest Apr 24th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace TbsSemi\DealReport\Handler;
  2.  
  3.  
  4. use Bitrix\Crm\DealTable;
  5. use Bitrix\Main\Config\Option;
  6. use Bitrix\Main\Loader;
  7.  
  8. class Deal
  9. {
  10.     public static function addDealRelations($arFields)
  11.     {
  12.         if (!Loader::includeModule('crm')) {
  13.             return;
  14.         }
  15.  
  16.         $currentDealId = $arFields['ID'];
  17.  
  18.         $relatedDealsField = Option::get('tbssemi.dealreport', 'DEAL_SETTINGS_FIELD_RELATED_DEALS');
  19.         $dealFields = DealTable::getList(array(
  20.             'filter' => array(
  21.                 'ID' => $currentDealId,
  22.             ),
  23.             'select' => array('ID', $relatedDealsField),
  24.         ))->fetch();
  25.  
  26.         if (empty($dealFields)) {
  27.             return;
  28.         }
  29.  
  30.         foreach ($dealFields[$relatedDealsField] as $relatedDeal) {
  31.             $matches = array();
  32.             preg_match(
  33.                 '/'. \CCrmOwnerTypeAbbr::Deal .'_(\d+)/i',
  34.                 $relatedDeal,
  35.                 $matches
  36.             );
  37.  
  38.             $relatedDealId = $matches[1];
  39.             if (empty($relatedDealId)) {
  40.                 continue;
  41.             }
  42.  
  43.             $relatedDealFields = DealTable::getList(array(
  44.                 'filter' => array(
  45.                     'ID' => $relatedDealId,
  46.                 ),
  47.                 'select' => array('ID', $relatedDealsField),
  48.             ))->fetch();
  49.  
  50.             if (empty($relatedDealFields)) {
  51.                 continue;
  52.             }
  53.  
  54.             $updateRelatedDeals = $relatedDealFields[$relatedDealsField];
  55.             if (!is_array($updateRelatedDeals)) {
  56.                 $updateRelatedDeals = array();
  57.             }
  58.  
  59.             $updateRelatedDeals[] = $currentDealId;
  60.  
  61.             DealTable::update($relatedDealId, array(
  62.                 $relatedDealsField => $updateRelatedDeals,
  63.             ));
  64.         }
  65.     }
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top