Example #1
0
if (isset($_REQUEST['copy'])) {
    $arParams['TYPE'] = 'copy';
}
$arResult['TYPE'] = $arParams['TYPE'];
$arResult['BUTTONS'] = array();
$arFields = array();
$arParams['ELEMENT_ID'] = intval($arParams['ELEMENT_ID']);
if ($arParams['TYPE'] == 'list') {
    $bRead = !$CrmPerms->HavePerm('INVOICE', BX_CRM_PERM_NONE, 'READ');
    $bExport = !$CrmPerms->HavePerm('INVOICE', BX_CRM_PERM_NONE, 'EXPORT');
    $bImport = !$CrmPerms->HavePerm('INVOICE', BX_CRM_PERM_NONE, 'IMPORT');
    $bAdd = !$CrmPerms->HavePerm('INVOICE', BX_CRM_PERM_NONE, 'ADD');
    $bWrite = !$CrmPerms->HavePerm('INVOICE', BX_CRM_PERM_NONE, 'WRITE');
    $bDelete = false;
} else {
    $arFields = CCrmInvoice::GetByID($arParams['ELEMENT_ID']);
    $arEntityAttr[$arParams['ELEMENT_ID']] = array();
    if ($arFields !== false) {
        $arEntityAttr = $CrmPerms->GetEntityAttr('INVOICE', array($arParams['ELEMENT_ID']));
    }
    $bRead = $arFields !== false;
    $bExport = false;
    $bImport = false;
    $bAdd = !$CrmPerms->HavePerm('INVOICE', BX_CRM_PERM_NONE, 'ADD');
    $bWrite = $CrmPerms->CheckEnityAccess('INVOICE', 'WRITE', $arEntityAttr[$arParams['ELEMENT_ID']]);
    $bDelete = $CrmPerms->CheckEnityAccess('INVOICE', 'DELETE', $arEntityAttr[$arParams['ELEMENT_ID']]);
}
if (!$bRead && !$bAdd && !$bWrite) {
    return false;
}
if ($arParams['TYPE'] === 'list') {
Example #2
0
 public static function OnSendMentionGetEntityFields($arCommentFields)
 {
     if (!in_array($arCommentFields["ENTITY_TYPE"], CCrmLiveFeedEntity::GetAll())) {
         return false;
     }
     if (!CModule::IncludeModule("socialnetwork")) {
         return true;
     }
     $dbLog = CSocNetLog::GetList(array(), array("ID" => $arCommentFields["LOG_ID"]), false, false, array("ID", "ENTITY_ID", "EVENT_ID"));
     if ($arLog = $dbLog->Fetch()) {
         $genderSuffix = "";
         $dbUser = CUser::GetByID($arCommentFields["USER_ID"]);
         if ($arUser = $dbUser->Fetch()) {
             $genderSuffix = $arUser["PERSONAL_GENDER"];
         }
         switch ($arLog["EVENT_ID"]) {
             case "crm_company_add":
                 $entityName = GetMessage("CRM_LF_COMPANY_ADD_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Company, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_COMPANY_ADD|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_contact_add":
                 $entityName = GetMessage("CRM_LF_CONTACT_ADD_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Contact, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_CONTACT_ADD|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_lead_add":
                 $entityName = GetMessage("CRM_LF_LEAD_ADD_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Lead, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_LEAD_ADD|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_deal_add":
                 $entityName = GetMessage("CRM_LF_DEAL_ADD_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Deal, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_DEAL_ADD|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_company_responsible":
                 $entityName = GetMessage("CRM_LF_COMPANY_RESPONSIBLE_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Company, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_COMPANY_RESPONSIBLE|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_contact_responsible":
                 $entityName = GetMessage("CRM_LF_CONTACT_RESPONSIBLE_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Contact, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_CONTACT_RESPONSIBLE|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_lead_responsible":
                 $entityName = GetMessage("CRM_LF_LEAD_RESPONSIBLE_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Lead, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_LEAD_RESPONSIBLE|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_deal_responsible":
                 $entityName = GetMessage("CRM_LF_DEAL_RESPONSIBLE_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Deal, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_DEAL_RESPONSIBLE|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_company_message":
                 $entityName = GetMessage("CRM_LF_COMPANY_MESSAGE_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Company, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_COMPANY_MESSAGE|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_contact_message":
                 $entityName = GetMessage("CRM_LF_CONTACT_MESSAGE_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Contact, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_CONTACT_MESSAGE|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_lead_message":
                 $entityName = GetMessage("CRM_LF_LEAD_MESSAGE_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Lead, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_LEAD_MESSAGE|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_deal_message":
                 $entityName = GetMessage("CRM_LF_DEAL_MESSAGE_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption(CCrmOwnerType::Deal, $arLog["ENTITY_ID"], false))));
                 $notifyTag = "CRM_DEAL_MESSAGE|COMMENT_MENTION|" . $arCommentFields["ID"];
                 break;
             case "crm_activity_add":
                 if ($arActivity = CCrmActivity::GetByID($arLog["ENTITY_ID"])) {
                     switch ($arActivity["OWNER_TYPE_ID"]) {
                         case CCrmOwnerType::Company:
                             $ownerType = "COMPANY";
                             break;
                         case CCrmOwnerType::Contact:
                             $ownerType = "CONTACT";
                             break;
                         case CCrmOwnerType::Lead:
                             $ownerType = "LEAD";
                             break;
                         case CCrmOwnerType::Deal:
                             $ownerType = "DEAL";
                             break;
                     }
                     switch ($arActivity["TYPE_ID"]) {
                         case CCrmActivityType::Meeting:
                             $activityType = "MEETING";
                             break;
                         case CCrmActivityType::Call:
                             $activityType = "CALL";
                             break;
                         case CCrmActivityType::Email:
                             $activityType = "EMAIL";
                             break;
                     }
                     if ($ownerType && $activityType) {
                         $entityName = GetMessage("CRM_LF_ACTIVITY_" . $activityType . "_" . $ownerType . "_COMMENT_MENTION_TITLE", array("#title#" => htmlspecialcharsbx(CCrmOwnerType::GetCaption($arActivity["OWNER_TYPE_ID"], $arActivity["OWNER_ID"], false))));
                         $notifyTag = "CRM_ACTIVITY_ADD|COMMENT_MENTION|" . $arCommentFields["ID"];
                     }
                 }
                 break;
             case "crm_invoice_add":
                 if ($arInvoice = CCrmInvoice::GetByID($arLog["ENTITY_ID"])) {
                     $entityName = GetMessage("CRM_LF_INVOICE_ADD_COMMENT_MENTION_TITLE", array("#id#" => $arInvoice["ID"]));
                     $notifyTag = "CRM_INVOICE_ADD|COMMENT_MENTION|" . $arCommentFields["ID"];
                 }
                 break;
         }
         if ($entityName) {
             $notifyMessage = GetMessage("CRM_LF_COMMENT_MENTION" . (strlen($genderSuffix) > 0 ? "_" . $genderSuffix : ""), array("#title#" => "<a href=\"#url#\" class=\"bx-notifier-item-action\">" . $entityName . "</a>"));
             $notifyMessageOut = GetMessage("CRM_LF_COMMENT_MENTION" . (strlen($genderSuffix) > 0 ? "_" . $genderSuffix : ""), array("#title#" => $entityName)) . " (" . "#server_name##url#)";
             $strPathToLogCrmEntry = str_replace("#log_id#", $arLog["ID"], "/crm/stream/?log_id=#log_id#");
             $strPathToLogCrmEntryComment = $strPathToLogCrmEntry . (strpos($strPathToLogCrmEntry, "?") !== false ? "&" : "?") . "commentID=" . $arCommentFields["ID"] . "#com" . $arCommentFields["ID"];
             if (in_array($arLog["EVENT_ID"], array("crm_company_message", "crm_contact_message", "crm_deal_message", "crm_lead_message"))) {
                 $strPathToLogEntry = str_replace("#log_id#", $arLog["ID"], COption::GetOptionString("socialnetwork", "log_entry_page", "/company/personal/log/#log_id#/", SITE_ID));
                 $strPathToLogEntryComment = $strPathToLogEntry . (strpos($strPathToLogEntry, "?") !== false ? "&" : "?") . "commentID=" . $arCommentFields["ID"] . "#com" . $arCommentFields["ID"];
             }
             $arReturn = array("IS_CRM" => "Y", "URL" => $strPathToLogEntryComment, "CRM_URL" => $strPathToLogCrmEntryComment, "NOTIFY_MODULE" => "crm", "NOTIFY_TAG" => $notifyTag, "NOTIFY_MESSAGE" => $notifyMessage, "NOTIFY_MESSAGE_OUT" => $notifyMessageOut);
             return $arReturn;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
Example #3
0
             continue;
         }
         $item = array('OWNER_ID' => $entityID, 'OWNER_TYPE_ID' => $entityTypeID, 'OWNER_TYPE_NAME' => $entityTypeName, 'TITLE' => $result['TITLE'], 'DESCRIPTION' => $result['DESCRIPTION'], 'IMAGE_URL' => '', 'COMMUNICATIONS' => array());
         if ($result['TYPE'] !== '' && $result['VALUE'] !== '') {
             $item['COMMUNICATIONS'][] = array('TYPE' => $result['TYPE'], 'VALUE' => $result['VALUE']);
         }
         if (!isset($imageless[$entityTypeName])) {
             $imageless[$entityTypeName] = array();
         }
         $imageless[$entityTypeName][] = $entityID;
         $items["{$entityTypeName}_{$entityID}"] =& $item;
         unset($item);
     }
     unset($result);
 } else {
     $entity = CCrmInvoice::GetByID($ownerID, false);
     if (!is_array($entity)) {
         ShowError(GetMessage('M_CRM_COMM_SELECTOR_OWNER_NOT_FOUND'));
         return;
     }
     $companyID = isset($entity['UF_COMPANY_ID']) ? intval($entity['UF_COMPANY_ID']) : 0;
     $company = $companyID > 0 ? CCrmCompany::GetByID($companyID, true) : null;
     if (is_array($company)) {
         $info = array('ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $companyID);
         if (CCrmActivity::PrepareCommunicationInfo($info, $company)) {
             $companyKey = "COMPANY_{$companyID}";
             $item = array('OWNER_ID' => $companyID, 'OWNER_TYPE_ID' => CCrmOwnerType::Company, 'TITLE' => $info['TITLE'], 'DESCRIPTION' => $info['DESCRIPTION'], 'IMAGE_URL' => CCrmMobileHelper::PrepareCompanyImageUrl($company, array('WIDTH' => 40, 'HEIGHT' => 40)), 'COMMUNICATIONS' => array());
             if ($commType === 'PERSON') {
                 $items[$companyKey] =& $item;
                 unset($item);
             } else {
Example #4
0
 public function Add($arFields, &$arRecalculated = false, $siteId = SITE_ID, $options = array())
 {
     /** @global \CDatabase $DB */
     global $DB;
     if (!CModule::IncludeModule('sale')) {
         return false;
     }
     if (!is_array($options)) {
         $options = array();
     }
     $bRecalculate = is_array($arRecalculated);
     $orderID = false;
     $tmpOrderId = intval($arFields['ID']) <= 0 ? 0 : $arFields['ID'];
     if (isset($arFields['ID'])) {
         unset($arFields['ID']);
     }
     $arPrevOrder = $tmpOrderId !== 0 ? CCrmInvoice::GetByID($tmpOrderId) : null;
     $userId = CCrmSecurityHelper::GetCurrentUserID();
     if (!isset($arFields['RESPONSIBLE_ID']) || (int) $arFields['RESPONSIBLE_ID'] <= 0) {
         if (is_array($arPrevOrder) && isset($arPrevOrder['RESPONSIBLE_ID']) && intval($arPrevOrder['RESPONSIBLE_ID']) > 0) {
             $arFields['RESPONSIBLE_ID'] = $arPrevOrder['RESPONSIBLE_ID'];
         } else {
             $arFields['RESPONSIBLE_ID'] = $userId;
         }
     }
     $orderStatus = '';
     if (isset($arFields['STATUS_ID'])) {
         $orderStatus = $arFields['STATUS_ID'];
         unset($arFields['STATUS_ID']);
     }
     // prepare entity permissions
     $arAttr = array();
     if (!empty($arFields['OPENED'])) {
         $arAttr['OPENED'] = $arFields['OPENED'];
     }
     $sPermission = $tmpOrderId > 0 ? 'WRITE' : 'ADD';
     if ($this->bCheckPermission) {
         $arEntityAttr = self::BuildEntityAttr($userId, $arAttr);
         $userPerms = $userId == CCrmPerms::GetCurrentUserID() ? $this->cPerms : CCrmPerms::GetUserPermissions($userId);
         $sEntityPerm = $userPerms->GetPermType('INVOICE', $sPermission, $arEntityAttr);
         if ($sEntityPerm == BX_CRM_PERM_NONE) {
             $this->LAST_ERROR = GetMessage('CRM_PERMISSION_DENIED');
             $GLOBALS['APPLICATION']->ThrowException($this->LAST_ERROR);
             return false;
         }
         $responsibleID = intval($arFields['RESPONSIBLE_ID']);
         if ($sEntityPerm == BX_CRM_PERM_SELF && $responsibleID != $userId) {
             $arFields['RESPONSIBLE_ID'] = $userId;
         }
         if ($sEntityPerm == BX_CRM_PERM_OPEN && $userId == $responsibleID) {
             $arFields['OPENED'] = 'Y';
         }
     }
     $responsibleID = intval($arFields['RESPONSIBLE_ID']);
     $arEntityAttr = self::BuildEntityAttr($responsibleID, $arAttr);
     $userPerms = $responsibleID == CCrmPerms::GetCurrentUserID() ? $this->cPerms : CCrmPerms::GetUserPermissions($responsibleID);
     $sEntityPerm = $userPerms->GetPermType('INVOICE', $sPermission, $arEntityAttr);
     $this->PrepareEntityAttrs($arEntityAttr, $sEntityPerm);
     // date fields
     if ($tmpOrderId === 0) {
         $arFields['~DATE_BILL'] = $DB->CharToDateFunction(isset($arFields['DATE_BILL']) && $arFields['DATE_BILL'] !== '' ? $arFields['DATE_BILL'] : ConvertTimeStamp(time(), 'SHORT', SITE_ID), 'SHORT', false);
     } else {
         if (isset($arFields['DATE_BILL']) && $arFields['DATE_BILL'] !== '') {
             $arFields['~DATE_BILL'] = $DB->CharToDateFunction($arFields['DATE_BILL'], 'SHORT', false);
         }
     }
     unset($arFields['DATE_BILL']);
     if (isset($arFields['DATE_PAY_BEFORE']) && $arFields['DATE_PAY_BEFORE'] !== '') {
         $arFields['~DATE_PAY_BEFORE'] = $DB->CharToDateFunction($arFields['DATE_PAY_BEFORE'], 'SHORT', false);
     }
     unset($arFields['DATE_PAY_BEFORE']);
     if ($tmpOrderId !== 0 && !isset($arFields['PRODUCT_ROWS']) && !isset($arFields['INVOICE_PROPERTIES'])) {
         if (!is_array($arPrevOrder)) {
             return false;
         }
         $prevResponsibleID = isset($arPrevOrder['RESPONSIBLE_ID']) ? intval($arPrevOrder['RESPONSIBLE_ID']) : 0;
         $responsibleID = isset($arFields['RESPONSIBLE_ID']) ? intval($arFields['RESPONSIBLE_ID']) : 0;
         $prevStatusID = isset($arPrevOrder['STATUS_ID']) ? $arPrevOrder['STATUS_ID'] : '';
         // simple update order fields
         $CSaleOrder = new CSaleOrder();
         $orderID = $CSaleOrder->Update($tmpOrderId, $arFields);
         CCrmEntityHelper::NormalizeUserFields($arFields, self::$sUFEntityID, $GLOBALS['USER_FIELD_MANAGER'], array('IS_NEW' => false));
         $GLOBALS['USER_FIELD_MANAGER']->Update(self::$sUFEntityID, $tmpOrderId, $arFields);
         $registerSonetEvent = isset($options['REGISTER_SONET_EVENT']) && $options['REGISTER_SONET_EVENT'] === true;
         if (is_int($orderID) && $orderID > 0) {
             if ($registerSonetEvent) {
                 $newDealID = isset($arFields['UF_DEAL_ID']) ? intval($arFields['UF_DEAL_ID']) : 0;
                 $oldDealID = isset($arPrevOrder['UF_DEAL_ID']) ? intval($arPrevOrder['UF_DEAL_ID']) : 0;
                 $newCompanyID = isset($arFields['UF_COMPANY_ID']) ? intval($arFields['UF_COMPANY_ID']) : 0;
                 $oldCompanyID = isset($arPrevOrder['UF_COMPANY_ID']) ? intval($arPrevOrder['UF_COMPANY_ID']) : 0;
                 $newContactID = isset($arFields['UF_CONTACT_ID']) ? intval($arFields['UF_CONTACT_ID']) : 0;
                 $oldContactID = isset($arPrevOrder['UF_CONTACT_ID']) ? intval($arPrevOrder['UF_CONTACT_ID']) : 0;
                 $parents = array();
                 $parentsChanged = $newDealID !== $oldDealID || $newCompanyID !== $oldCompanyID || $newContactID !== $oldContactID;
                 if ($parentsChanged) {
                     if ($newDealID > 0) {
                         $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Deal, 'ENTITY_ID' => $newDealID);
                     }
                     if ($newCompanyID > 0) {
                         $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $newCompanyID);
                     }
                     if ($newContactID > 0) {
                         $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Contact, 'ENTITY_ID' => $newContactID);
                     }
                 }
                 $oldOrderStatus = isset($arPrevOrder['STATUS_ID']) ? $arPrevOrder['STATUS_ID'] : '';
                 self::SynchronizeLiveFeedEvent($orderID, array('PROCESS_PARENTS' => $parentsChanged, 'PARENTS' => $parents, 'REFRESH_DATE' => $orderStatus !== $oldOrderStatus, 'START_RESPONSIBLE_ID' => $prevResponsibleID, 'FINAL_RESPONSIBLE_ID' => $responsibleID, 'TOPIC' => isset($arPrevOrder['ORDER_TOPIC']) ? $arPrevOrder['ORDER_TOPIC'] : $orderID));
             }
             if ($responsibleID !== $prevResponsibleID) {
                 CCrmSonetSubscription::ReplaceSubscriptionByEntity(CCrmOwnerType::Invoice, $orderID, CCrmSonetSubscriptionType::Responsibility, $responsibleID, $prevResponsibleID, $registerSonetEvent);
             }
         }
     } else {
         // check product rows
         if (!isset($arFields['PRODUCT_ROWS']) || !is_array($arFields['PRODUCT_ROWS']) || count($arFields['PRODUCT_ROWS']) <= 0) {
             return false;
         }
         $arProduct = $arFields['PRODUCT_ROWS'];
         // prepare shopping cart data
         // <editor-fold defaultstate="collapsed" desc="prepare shopping cart data ...">
         // get xml_id fields
         $catalogXmlId = CCrmCatalog::GetDefaultCatalogXmlId();
         $arNewProducts = array();
         $bGetBasketXmlIds = false;
         foreach ($arProduct as &$productRow) {
             if (isset($productRow['ID']) && intval($productRow['ID']) === 0 && isset($productRow['PRODUCT_ID'])) {
                 $arNewProducts[] = $productRow['PRODUCT_ID'];
             } else {
                 $bGetBasketXmlIds = true;
             }
         }
         unset($productRow);
         $arXmlIds = array();
         $oldProductRows = null;
         if ($bGetBasketXmlIds && intval($tmpOrderId) > 0) {
             $oldProductRows = CCrmInvoice::GetProductRows($tmpOrderId);
             if (count($oldProductRows) > 0) {
                 foreach ($oldProductRows as $row) {
                     $arXmlIds[intval($row['ID'])][$row['PRODUCT_ID']] = array('CATALOG_XML_ID' => $row['CATALOG_XML_ID'], 'PRODUCT_XML_ID' => $row['PRODUCT_XML_ID']);
                 }
                 unset($row);
             }
         }
         unset($bGetBasketXmlIds);
         if (count($arNewProducts) > 0) {
             $dbRes = CCrmProduct::GetList(array(), array('ID' => $arNewProducts), array('ID', 'XML_ID'));
             while ($row = $dbRes->Fetch()) {
                 $arXmlIds[0][$row['ID']] = array('CATALOG_XML_ID' => $catalogXmlId, 'PRODUCT_XML_ID' => $row['XML_ID']);
             }
             unset($dbRes, $row);
         }
         unset($arNewProducts, $arOldProducts);
         // products without measures
         $productMeasures = array();
         $productId = 0;
         $productIds = array();
         foreach ($arProduct as $productRow) {
             $productId = intval($productRow['PRODUCT_ID']);
             if ($productId > 0 && (!array_key_exists('MEASURE_CODE', $productRow) || intval($productRow['MEASURE_CODE']) <= 0)) {
                 $productIds[] = $productId;
             }
         }
         unset($productId, $productRow);
         if (count($productIds) > 0) {
             $productMeasures = \Bitrix\Crm\Measure::getProductMeasures($productIds);
         }
         unset($productIds);
         $currencyId = CCrmInvoice::GetCurrencyID($siteId);
         $i = 0;
         $defaultMeasure = null;
         $oldProductRowsById = null;
         foreach ($arProduct as &$productRow) {
             $productXmlId = $catalogXmlId = null;
             $rowIndex = intval($productRow['ID']);
             $productId = $productRow['PRODUCT_ID'];
             $isCustomized = isset($productRow['CUSTOMIZED']) && $productRow['CUSTOMIZED'] === 'Y';
             $productRow['MODULE'] = $productRow['PRODUCT_PROVIDER_CLASS'] = '';
             if ($productId > 0) {
                 if (!$isCustomized) {
                     $productRow['MODULE'] = 'catalog';
                     $productRow['PRODUCT_PROVIDER_CLASS'] = 'CCatalogProductProvider';
                 }
                 if (is_array($arXmlIds[$rowIndex]) && isset($arXmlIds[$rowIndex][$productId])) {
                     $catalogXmlId = $arXmlIds[$rowIndex][$productId]['CATALOG_XML_ID'];
                     $productXmlId = $arXmlIds[$rowIndex][$productId]['PRODUCT_XML_ID'];
                 }
                 $productRow['CATALOG_XML_ID'] = $catalogXmlId;
                 $productRow['PRODUCT_XML_ID'] = $productXmlId;
             } else {
                 $productRow["PRODUCT_XML_ID"] = "CRM-" . randString(8);
                 $ri = new \Bitrix\Main\Type\RandomSequence($productRow["PRODUCT_XML_ID"]);
                 $productRow["PRODUCT_ID"] = $ri->rand(1000000, 9999999);
                 $productRow['CATALOG_XML_ID'] = '';
             }
             if ($isCustomized) {
                 $productRow['CUSTOM_PRICE'] = 'Y';
             }
             if (isset($productRow['PRODUCT_NAME'])) {
                 $productRow['NAME'] = $productRow['PRODUCT_NAME'];
                 unset($productRow['PRODUCT_NAME']);
             }
             if (isset($productRow['PRICE'])) {
                 $productRow['PRICE_DEFAULT'] = $productRow['PRICE'];
             }
             if (!isset($productRow['CURRENCY'])) {
                 $productRow['CURRENCY'] = $currencyId;
             }
             // measures
             $bRefreshMeasureName = false;
             if (!array_key_exists('MEASURE_CODE', $productRow) || intval($productRow['MEASURE_CODE'] <= 0)) {
                 if ($oldProductRows === null && $tmpOrderId > 0) {
                     $oldProductRows = CCrmInvoice::GetProductRows($tmpOrderId);
                 }
                 if (is_array($oldProductRows) && count($oldProductRows) > 0 && $oldProductRowsById === null) {
                     $oldProductRowsById = array();
                     foreach ($oldProductRows as $row) {
                         $oldProductRowsById[intval($row['ID'])] = $row;
                     }
                     unset($row);
                 }
                 if (is_array($oldProductRowsById) && isset($oldProductRowsById[$rowIndex])) {
                     $row = $oldProductRowsById[$rowIndex];
                     if (intval($productId) === intval($row['PRODUCT_ID'])) {
                         if (isset($row['MEASURE_CODE'])) {
                             $productRow['MEASURE_CODE'] = $row['MEASURE_CODE'];
                         }
                         if (isset($row['MEASURE_NAME'])) {
                             $productRow['MEASURE_NAME'] = $row['MEASURE_NAME'];
                         } else {
                             $bRefreshMeasureName = true;
                         }
                         unset($row);
                     }
                 }
             }
             if (!isset($productRow['MEASURE_CODE']) || intval($productRow['MEASURE_CODE']) <= 0) {
                 if ($productId > 0 && isset($productMeasures[$productId])) {
                     $measure = is_array($productMeasures[$productId][0]) ? $productMeasures[$productId][0] : null;
                     if (is_array($measure)) {
                         if (isset($measure['CODE'])) {
                             $productRow['MEASURE_CODE'] = $measure['CODE'];
                         }
                         if (isset($measure['SYMBOL'])) {
                             $productRow['MEASURE_NAME'] = $measure['SYMBOL'];
                         }
                     }
                     unset($measure);
                 }
             }
             if (!isset($productRow['MEASURE_CODE']) || intval($productRow['MEASURE_CODE']) <= 0) {
                 if ($defaultMeasure === null) {
                     $defaultMeasure = \Bitrix\Crm\Measure::getDefaultMeasure();
                 }
                 if (is_array($defaultMeasure)) {
                     $productRow['MEASURE_CODE'] = $defaultMeasure['CODE'];
                     $productRow['MEASURE_NAME'] = $defaultMeasure['SYMBOL'];
                 }
             }
             if (isset($productRow['MEASURE_CODE']) && intval($productRow['MEASURE_CODE']) > 0 && ($bRefreshMeasureName || !array_key_exists('MEASURE_NAME', $productRow) || empty($productRow['MEASURE_NAME']))) {
                 $measure = \Bitrix\Crm\Measure::getMeasureByCode($productRow['MEASURE_CODE']);
                 if (is_array($measure) && isset($measure['SYMBOL'])) {
                     $productRow['MEASURE_NAME'] = $measure['SYMBOL'];
                 }
                 unset($measure);
             }
             $i++;
         }
         unset($productRow, $productMeasures, $catalogXmlId, $productXmlId);
         $arOrderProductPrice = self::__fGetUserShoppingCart($arProduct, $siteId, 'N');
         foreach ($arOrderProductPrice as &$arItem) {
             $arItem["ID_TMP"] = $arItem["ID"];
             $arItem["NAME_TMP"] = $arItem["NAME"];
             unset($arItem["ID"]);
         }
         unset($arItem);
         // user id for order
         $saleUserId = intval(CSaleUser::GetAnonymousUserID());
         if ($saleUserId <= 0) {
             return false;
         }
         $arErrors = array();
         $arShoppingCart = CSaleBasket::DoGetUserShoppingCart($siteId, $saleUserId, $arOrderProductPrice, $arErrors, array(), $tmpOrderId);
         if (!is_array($arShoppingCart) || count($arShoppingCart) === 0) {
             $GLOBALS['APPLICATION']->ThrowException(GetMessage('CRM_ERROR_EMPTY_INVOICE_SPEC'));
             return false;
         }
         foreach ($arShoppingCart as $key => &$arItem) {
             $arItem["ID"] = $arItem["ID_TMP"];
             $arItem["NAME"] = $arItem["NAME_TMP"];
             unset($arItem["NAME_TMP"], $arItem["ID_TMP"]);
             //$arShoppingCart[$key]["ID"] = $arItem["ID"];
         }
         unset($key, $arItem);
         // </editor-fold>
         // person type
         $personTypeId = 0;
         if (!isset($arFields['PERSON_TYPE_ID']) || intval($arFields['PERSON_TYPE_ID']) <= 0) {
             $arPersonTypes = CCrmPaySystem::getPersonTypeIDs();
             if (isset($arPersonTypes['CONTACT'])) {
                 $personTypeId = intval($arPersonTypes['CONTACT']);
             }
         } else {
             $personTypeId = $arFields['PERSON_TYPE_ID'];
         }
         if ($personTypeId <= 0) {
             return false;
         }
         // preparing order to save
         // <editor-fold defaultstate="collapsed" desc="preparing order to save ...">
         $arOrderPropsValues = array();
         if (isset($arFields['INVOICE_PROPERTIES']) && is_array($arFields['INVOICE_PROPERTIES']) && count($arFields['INVOICE_PROPERTIES']) > 0) {
             $arOrderPropsValues = $arFields['INVOICE_PROPERTIES'];
         }
         if (isset($arFields['INVOICE_PROPERTIES'])) {
             unset($arFields['INVOICE_PROPERTIES']);
         }
         if (count($arOrderPropsValues) <= 0) {
             return false;
         }
         $deliveryId = null;
         $paySystemId = $arFields['PAY_SYSTEM_ID'];
         $arOptions = array('LOCATION_IN_CODES' => true);
         // let DoCalculateOrder know we send location in CODEs
         $arErrors = $arWarnings = array();
         $CSaleOrder = new CSaleOrder();
         $arOrder = $CSaleOrder->DoCalculateOrder($siteId, $saleUserId, $arShoppingCart, $personTypeId, $arOrderPropsValues, $deliveryId, $paySystemId, $arOptions, $arErrors, $arWarnings);
         if (count($arOrder) <= 0) {
             return false;
         }
         // </editor-fold>
         if ($bRecalculate) {
             foreach ($arOrder as $k => $v) {
                 $arRecalculated[$k] = $v;
             }
             return true;
         }
         // merge order fields
         $arAdditionalFields = array();
         foreach ($arFields as $k => $v) {
             if ($k === 'PRODUCT_ROWS') {
                 continue;
             }
             $arAdditionalFields[$k] = $v;
         }
         $arOrder['LOCATION_IN_CODES'] = true;
         // let DoSaveOrder know we send location in IDs
         // saving order
         $arErrors = array();
         $orderID = $CSaleOrder->DoSaveOrder($arOrder, $arAdditionalFields, $tmpOrderId, $arErrors);
         if (is_int($orderID) && $orderID > 0) {
             CCrmEntityHelper::NormalizeUserFields($arFields, self::$sUFEntityID, $GLOBALS['USER_FIELD_MANAGER'], array('IS_NEW' => $tmpOrderId === 0));
             $GLOBALS['USER_FIELD_MANAGER']->Update(self::$sUFEntityID, $orderID, $arFields);
         }
         if (is_int($orderID) && $orderID > 0 && isset($options['REGISTER_SONET_EVENT']) && $options['REGISTER_SONET_EVENT'] === true) {
             $prevResponsibleID = is_array($arPrevOrder) && isset($arPrevOrder['RESPONSIBLE_ID']) ? intval($arPrevOrder['RESPONSIBLE_ID']) : 0;
             $responsibleID = isset($arFields['RESPONSIBLE_ID']) ? intval($arFields['RESPONSIBLE_ID']) : 0;
             if ($tmpOrderId <= 0) {
                 self::RegisterLiveFeedEvent($arFields, $orderID, $userId);
                 if ($responsibleID > 0) {
                     CCrmSonetSubscription::RegisterSubscription(CCrmOwnerType::Invoice, $orderID, CCrmSonetSubscriptionType::Responsibility, $responsibleID);
                 }
             } else {
                 $newDealID = isset($arFields['UF_DEAL_ID']) ? intval($arFields['UF_DEAL_ID']) : 0;
                 $oldDealID = isset($arPrevOrder['UF_DEAL_ID']) ? intval($arPrevOrder['UF_DEAL_ID']) : 0;
                 $newCompanyID = isset($arFields['UF_COMPANY_ID']) ? intval($arFields['UF_COMPANY_ID']) : 0;
                 $oldCompanyID = isset($arPrevOrder['UF_COMPANY_ID']) ? intval($arPrevOrder['UF_COMPANY_ID']) : 0;
                 $newContactID = isset($arFields['UF_CONTACT_ID']) ? intval($arFields['UF_CONTACT_ID']) : 0;
                 $oldContactID = isset($arPrevOrder['UF_CONTACT_ID']) ? intval($arPrevOrder['UF_CONTACT_ID']) : 0;
                 $parents = array();
                 $parentsChanged = $newDealID !== $oldDealID || $newCompanyID !== $oldCompanyID || $newContactID !== $oldContactID;
                 if ($parentsChanged) {
                     if ($newDealID > 0) {
                         $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Deal, 'ENTITY_ID' => $newDealID);
                     }
                     if ($newCompanyID > 0) {
                         $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Company, 'ENTITY_ID' => $newCompanyID);
                     }
                     if ($newContactID > 0) {
                         $parents[] = array('ENTITY_TYPE_ID' => CCrmOwnerType::Contact, 'ENTITY_ID' => $newContactID);
                     }
                 }
                 $oldOrderStatus = isset($arPrevOrder['STATUS_ID']) ? $arPrevOrder['STATUS_ID'] : '';
                 self::SynchronizeLiveFeedEvent($orderID, array('PROCESS_PARENTS' => $parentsChanged, 'PARENTS' => $parents, 'REFRESH_DATE' => $orderStatus !== $oldOrderStatus, 'START_RESPONSIBLE_ID' => $prevResponsibleID, 'FINAL_RESPONSIBLE_ID' => $responsibleID, 'TOPIC' => isset($arPrevOrder['ORDER_TOPIC']) ? $arPrevOrder['ORDER_TOPIC'] : $orderID));
                 if ($responsibleID !== $prevResponsibleID) {
                     CCrmSonetSubscription::ReplaceSubscriptionByEntity(CCrmOwnerType::Invoice, $orderID, CCrmSonetSubscriptionType::Responsibility, $responsibleID, $prevResponsibleID, true);
                 }
             }
         }
     }
     if (intval($orderID) > 0 && !empty($orderStatus)) {
         // set status
         $this->SetStatus($orderID, $orderStatus);
         // update entity permissions
         CCrmPerms::UpdateEntityAttr('INVOICE', $orderID, $arEntityAttr);
         $newDealID = isset($arFields['UF_DEAL_ID']) ? (int) $arFields['UF_DEAL_ID'] : 0;
         $oldDealID = is_array($arPrevOrder) && isset($arPrevOrder['UF_DEAL_ID']) ? (int) $arPrevOrder['UF_DEAL_ID'] : 0;
         if ($newDealID) {
             Bitrix\Crm\Statistics\DealInvoiceStatisticEntry::register($newDealID);
         }
         if ($oldDealID > 0 && $oldDealID !== $newDealID) {
             Bitrix\Crm\Statistics\DealInvoiceStatisticEntry::register($oldDealID);
         }
         if (isset($options['UPDATE_SEARCH']) && $options['UPDATE_SEARCH'] === true) {
             $arFilterTmp = array('ID' => $orderID);
             if (!$this->bCheckPermission) {
                 $arFilterTmp["CHECK_PERMISSIONS"] = "N";
             }
             CCrmSearch::UpdateSearch($arFilterTmp, 'INVOICE', true);
         }
     }
     return $orderID;
 }
Example #5
0
        } else {
            $fieldNames[] = $_POST['FIELD_NAME'];
        }
    }
    if (count($fieldNames) == 0) {
        __CrmInvoiceShowEndJsonResonse(array('ERROR' => 'FIELD_NAME IS NOT DEFINED!'));
    }
    $fieldValues = array();
    if (isset($_POST['FIELD_VALUE'])) {
        if (is_array($_POST['FIELD_VALUE'])) {
            $fieldValues = $_POST['FIELD_VALUE'];
        } else {
            $fieldValues[] = $_POST['FIELD_VALUE'];
        }
    }
    $arFields = CCrmInvoice::GetByID($ID);
    if (is_array($arFields)) {
        CCrmInstantEditorHelper::PrepareUpdate(CCrmOwnerType::Invoice, $arFields, $fieldNames, $fieldValues);
        // check fields
        if (!$CCrmInvoice->CheckFieldsUpdate($arFields, $ID)) {
            $errMsg = '';
            if (!empty($CCrmInvoice->LAST_ERROR)) {
                $errMsg .= $CCrmInvoice->LAST_ERROR;
            } else {
                $errMsg .= GetMessage('UNKNOWN_ERROR');
            }
            __CrmInvoiceShowEndJsonResonse(array('ERROR' => $errMsg));
        }
        $CCrmInvoice->Update($ID, $arFields, array('REGISTER_SONET_EVENT' => true, 'UPDATE_SEARCH' => true));
    }
}
 private static function getInvoiceDataByID($ID)
 {
     $arInvoice = CCrmInvoice::GetByID($ID);
     if (!is_array($arInvoice)) {
         throw new RestException('Not found.');
     }
     $arProperties = CCrmInvoice::GetProperties($ID, $arInvoice['PERSON_TYPE_ID']);
     $arAllowedProperties = CCrmInvoice::GetPropertiesInfo($arInvoice['PERSON_TYPE_ID'], true);
     $arAllowedProperties = is_array($arAllowedProperties[$arInvoice['PERSON_TYPE_ID']]) ? array_keys($arAllowedProperties[$arInvoice['PERSON_TYPE_ID']]) : array();
     $arPropertiesResult = array();
     foreach ($arProperties as $k => $v) {
         if ($k !== 'PR_LOCATION') {
             if (in_array($v['FIELDS']['CODE'], $arAllowedProperties)) {
                 $arPropertiesResult[$v['FIELDS']['CODE']] = $v['VALUE'];
             }
         } else {
             $arInvoice['PR_LOCATION'] = $v['VALUE'];
         }
     }
     $arProducts = CCrmInvoice::GetProductRows($ID);
     $result = $arInvoice;
     if (count($arPropertiesResult) > 0) {
         // ua company name hack
         if (!isset($arPropertiesResult['COMPANY']) && isset($arPropertiesResult['COMPANY_NAME'])) {
             $arPropertiesResult['COMPANY'] = $arPropertiesResult['COMPANY_NAME'];
             unset($arPropertiesResult['COMPANY_NAME']);
         }
         $result['INVOICE_PROPERTIES'] = $arPropertiesResult;
     }
     if (count($arProducts) > 0) {
         $result['PRODUCT_ROWS'] = $arProducts;
     }
     return $result;
 }