} $arResult['HEAD_TITLE'] = isset($entityFields['TITLE']) ? $entityFields['TITLE'] : ''; $arResult['HEAD_TITLE_FIELD_ID'] = 'TITLE'; } elseif ($entityTypeID === CCrmOwnerType::Invoice) { if ($enableDefaultConfig) { $config['left'] = 'DATE_BILL,DATE_PAY_BEFORE,PAY_VOUCHER_DATE,UF_DEAL_ID,UF_QUOTE_ID'; $config['center'] = 'CLIENT_ID'; $config['right'] = 'RESPONSIBLE_ID'; $config['bottom'] = 'COMMENTS'; } $ufEntityID = CCrmInvoice::$sUFEntityID; $fieldKeys = array('ACCOUNT_NUMBER' => true, 'ORDER_TOPIC' => true, 'STATUS_ID' => true, 'PAY_VOUCHER_DATE' => true, 'PAY_VOUCHER_NUM' => true, 'DATE_BILL' => true, 'DATE_PAY_BEFORE' => true, 'REASON_MARKED_SUCCESS' => true, 'DATE_MARKED' => true, 'REASON_MARKED' => true, 'RESPONSIBLE_ID' => true, 'CURRENCY' => true, 'PRICE' => true, 'UF_CONTACT_ID' => true, 'UF_COMPANY_ID' => true, 'UF_DEAL_ID' => true, 'UF_QUOTE_ID' => true, 'PR_LOCATION' => true, 'PAYER_INFO' => true, 'PAY_SYSTEM_ID' => true, 'COMMENTS' => true); $arResult['HEAD_PROGRESS_LEGEND'] = isset($entityFields['STATUS_TEXT']) ? $entityFields['STATUS_TEXT'] : ''; $statusText = htmlspecialcharsbx($arResult['HEAD_PROGRESS_LEGEND']); $progressHtml = $arResult['HEAD_PROGRESS_BAR'] = CCrmViewHelper::RenderProgressControl(array('ENTITY_TYPE_NAME' => CCrmOwnerType::InvoiceName, 'REGISTER_SETTINGS' => true, 'CONTROL_ID' => $arResult['GUID'], 'ENTITY_ID' => $entityFields['ID'], 'CURRENT_ID' => $entityFields['STATUS_ID'], 'SERVICE_URL' => '/bitrix/components/bitrix/crm.invoice.list/list.ajax.php', 'READ_ONLY' => !$canEdit, 'DISPLAY_LEGEND' => false)); $currencyID = isset($entityFields['CURRENCY']) ? $entityFields['CURRENCY'] : CCrmInvoice::GetCurrencyID(); $arResult['HEAD_FORMATTED_SUM'] = CCrmCurrency::MoneyToString(isset($entityFields['PRICE']) ? $entityFields['PRICE'] : 0.0, $currencyID); $arResult['HEAD_SUM_FIELD_ID'] = 'PRICE'; __CrmQuickPanelViewPrepareContactInfo($entityFields, $entityContext, 'UF_CONTACT', false); __CrmQuickPanelViewPrepareCompanyInfo($entityFields, $entityContext, 'UF_COMPANY', false); $isSuccessfullStatus = isset($entityFields['STATUS_SUCCESS']) ? strtoupper($entityFields['STATUS_SUCCESS']) === 'Y' : false; $isFailedStatus = isset($entityFields['STATUS_FAILED']) ? strtoupper($entityFields['STATUS_FAILED']) === 'Y' : false; foreach ($entityFields as $k => $v) { if (!isset($fieldKeys[$k])) { continue; } if ($k === 'STATUS_ID') { $entityData[$k] = array('type' => 'custom', 'data' => array('html' => "<div class=\"crm-detail-stage\"><div class=\"crm-detail-stage-name\">{$statusText}</div>{$progressHtml}</div>")); } elseif ($k === 'CURRENCY') { //HACK: EDIT FORM REFERS BY 'CURRENCY_ID' $k = 'CURRENCY_ID';
public function map(EntityConversionMap $map, array $options = null) { $srcFields = $this->getSourceFields(); if (empty($srcFields)) { return array(); } $dstFields = array(); $dstEntityTypeID = $map->getDestinationEntityTypeID(); foreach ($map->getItems() as $item) { $dstFieldID = $item->getDestinationField(); //Skip empty binding if ($dstFieldID === '-') { continue; } $srcFieldID = $item->getSourceField(); if ((!isset($srcFields[$srcFieldID]) || $srcFields[$srcFieldID] === '') && $srcFieldID !== 'PRODUCT_ROWS') { $altSrcFieldID = ''; foreach ($item->getAlternativeSourceFields() as $fieldID) { if (isset($srcFields[$fieldID])) { $altSrcFieldID = $fieldID; break; } } if ($altSrcFieldID !== '') { $srcFieldID = $altSrcFieldID; } } if ($dstFieldID === '') { $dstFieldID = $srcFieldID; } if (strpos($srcFieldID, 'UF_') === 0 && strpos($dstFieldID, 'UF_') === 0) { self::mapUserField(\CCrmOwnerType::Deal, $srcFieldID, $srcFields, $dstEntityTypeID, $dstFieldID, $dstFields, $options); } elseif ($srcFieldID === 'PRODUCT_ROWS') { $productRows = \CCrmDeal::LoadProductRows($this->srcEntityID); if (count($productRows) > 0) { if ($dstEntityTypeID === \CCrmOwnerType::Invoice) { $currencyID = isset($srcFields['CURRENCY_ID']) ? $srcFields['CURRENCY_ID'] : ''; if ($currencyID === '' || !\CCrmCurrency::IsExists($currencyID)) { $currencyID = \CCrmCurrency::GetBaseCurrencyID(); } $actualRows = \CCrmInvoice::ProductRows2BasketItems($productRows, $currencyID, \CCrmInvoice::GetCurrencyID()); if (count($actualRows) > 0) { foreach ($actualRows as &$productRow) { unset($productRow['ID']); } unset($productRow); $dstFields[$dstFieldID] = $actualRows; } } else { foreach ($productRows as &$productRow) { unset($productRow['ID']); } unset($productRow); $dstFields[$dstFieldID] = $productRows; } } } elseif (isset($srcFields[$srcFieldID])) { $dstFields[$dstFieldID] = $srcFields[$srcFieldID]; } } if (!empty($dstFields)) { if ($dstEntityTypeID === \CCrmOwnerType::Invoice) { $dstFields['UF_DEAL_ID'] = $this->srcEntityID; //region Prepare Invoice Properties $dstFields['INVOICE_PROPERTIES'] = array(); $invoiceEntity = new \CCrmInvoice(false); $companyID = isset($dstFields['UF_COMPANY_ID']) ? (int) $dstFields['UF_COMPANY_ID'] : 0; $contactID = isset($dstFields['UF_CONTACT_ID']) ? (int) $dstFields['UF_CONTACT_ID'] : 0; $personTypeID = 0; $personTypes = \CCrmPaySystem::getPersonTypeIDs(); if ($companyID > 0 && isset($personTypes['COMPANY'])) { $personTypeID = $personTypes['COMPANY']; } elseif (isset($personTypes['CONTACT'])) { $personTypeID = $personTypes['CONTACT']; } $properties = $invoiceEntity->GetProperties(0, $personTypeID); if (is_array($properties)) { \CCrmInvoice::__RewritePayerInfo($companyID, $contactID, $invoiceProperties); foreach ($properties as $property) { $dstFields['INVOICE_PROPERTIES'][$property['FIELDS']['ID']] = $property['VALUE']; } } //endregion } elseif ($dstEntityTypeID === \CCrmOwnerType::Quote) { $dstFields['DEAL_ID'] = $this->srcEntityID; } } return $dstFields; }
$arResult['CONTEXT_ID'] = $arParams['CONTEXT_ID'] = $contextID; //<-- CONTEXT_ID $arResult['STATUS_LIST'] = CCrmStatus::GetStatus('INVOICE_STATUS'); $personTypes = $arResult['PERSON_TYPES'] = CCrmPaySystem::getPersonTypeIDs(); // '' - NO TAXES // 'VAT' - VAT ONLY // 'EXT' - EXTENDED MODE WITH CUSTOM TAXES $taxMode = $arResult['TAX_MODE'] = CCrmTax::isVatMode() ? 'VAT' : (CCrmTax::isTaxMode() ? 'EXT' : ''); $companyID = 0; $contactID = 0; $dealID = 0; $dealCurrencyID = ''; $personTypeID = 0; $paySystemID = 0; $properties = array(); $currencyID = CCrmInvoice::GetCurrencyID(); $arFields = null; if ($entityID === 0) { $arResult['MODE'] = 'CREATE'; $now = time() + CTimeZone::GetOffset(); $nowDate = ConvertTimeStamp($now, 'SHORT', SITE_ID); $arFields = array('~CURRENCY' => $currencyID, 'CURRENCY' => htmlspecialcharsbx($currencyID)); $arFields['~STATUS_ID'] = !empty($arResult['STATUS_LIST']) ? array_shift(array_keys($arResult['STATUS_LIST'])) : ''; $arFields['STATUS_ID'] = htmlspecialcharsbx($arFields['~STATUS_ID']); $arFields['DATE_PAY_BEFORE_STAMP'] = 0; $arFields['~DATE_PAY_BEFORE'] = $arFields['DATE_PAY_BEFORE'] = ''; $arFields['DATE_BILL_STAMP'] = $now; $arFields['~DATE_BILL'] = $arFields['~DATE_INSERT'] = $nowDate; $arFields['DATE_BILL'] = $arFields['DATE_INSERT'] = htmlspecialcharsbx($nowDate); if (isset($_REQUEST['deal_id'])) { $dealID = max(intval($_REQUEST['deal_id']), 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; }
$bStatusFailed = false; if (isset($arResult['ELEMENT']['STATUS_ID']) && $arResult['ELEMENT']['STATUS_ID'] !== '') { $bStatusSuccess = CCrmStatusInvoice::isStatusSuccess($arResult['ELEMENT']['STATUS_ID']); if ($bStatusSuccess) { $bStatusFailed = false; } else { $bStatusFailed = CCrmStatusInvoice::isStatusFailed($arResult['ELEMENT']['STATUS_ID']); } } $arResult['STATUS_SUCCESS'] = $arResult['ELEMENT']['STATUS_SUCCESS'] = $bStatusSuccess ? 'Y' : 'N'; $arResult['STATUS_FAILED'] = $arResult['ELEMENT']['STATUS_FAILED'] = $bStatusFailed ? 'Y' : 'N'; $arResult['ELEMENT']['REASON_MARKED_SUCCESS'] = $bStatusSuccess ? $arResult['ELEMENT']['REASON_MARKED'] : ''; if (!$bStatusFailed) { $arResult['ELEMENT']['REASON_MARKED'] = ''; } $currencyID = isset($arResult['ELEMENT']['CURRENCY']) ? $arResult['ELEMENT']['CURRENCY'] : CCrmInvoice::GetCurrencyID(); $dealID = isset($arResult['ELEMENT']['UF_DEAL_ID']) ? $arResult['ELEMENT']['UF_DEAL_ID'] : 0; $arResult['PATH_TO_DEAL_SHOW'] = $arResult['ELEMENT']['UF_DEAL_SHOW_URL'] = $dealID > 0 ? CComponentEngine::MakePathFromTemplate($arParams['PATH_TO_DEAL_SHOW'], array('deal_id' => $dealID)) : ''; if ($dealID > 0) { $arResult['ELEMENT']['UF_DEAL_TITLE'] = CCrmOwnerType::GetCaption(CCrmOwnerType::Deal, $dealID, false); } $quoteID = isset($arResult['ELEMENT']['UF_QUOTE_ID']) ? $arResult['ELEMENT']['UF_QUOTE_ID'] : 0; $arResult['PATH_TO_QUOTE_SHOW'] = $arResult['ELEMENT']['UF_QUOTE_SHOW_URL'] = $quoteID > 0 ? CComponentEngine::MakePathFromTemplate($arParams['PATH_TO_QUOTE_SHOW'], array('quote_id' => $quoteID)) : ''; if ($quoteID > 0) { $arResult['ELEMENT']['UF_QUOTE_TITLE'] = CCrmOwnerType::GetCaption(CCrmOwnerType::Quote, $quoteID, false); } $companyID = isset($arResult['ELEMENT']['UF_COMPANY_ID']) ? $arResult['ELEMENT']['UF_COMPANY_ID'] : 0; $arResult['PATH_TO_COMPANY_SHOW'] = $arResult['ELEMENT']['UF_COMPANY_SHOW_URL'] = $companyID > 0 ? CComponentEngine::MakePathFromTemplate($arParams['PATH_TO_COMPANY_SHOW'], array('company_id' => $companyID)) : ''; if ($companyID > 0) { $dbResult = CCrmCompany::GetListEx(array(), array('=ID' => $companyID, 'CHECK_PERMISSIONS' => 'N'), false, false, array('ID', 'TITLE', 'LOGO')); $entityInfo = $dbResult ? $dbResult->Fetch() : null;
public function map(EntityConversionMap $map, array $options = null) { $srcFields = $this->getSourceFields(); if (empty($srcFields)) { return array(); } $dstFields = array(); $dstEntityTypeID = $map->getDestinationEntityTypeID(); foreach ($map->getItems() as $item) { $dstFieldID = $item->getDestinationField(); //Skip empty binding if ($dstFieldID === '-') { continue; } $srcFieldID = $item->getSourceField(); if ((!isset($srcFields[$srcFieldID]) || $srcFields[$srcFieldID] === '') && $srcFieldID !== 'PRODUCT_ROWS') { $altSrcFieldID = ''; foreach ($item->getAlternativeSourceFields() as $fieldID) { if (isset($srcFields[$fieldID])) { $altSrcFieldID = $fieldID; break; } } if ($altSrcFieldID !== '') { $srcFieldID = $altSrcFieldID; } } if ($dstFieldID === '') { $dstFieldID = $srcFieldID; } if (strpos($srcFieldID, 'UF_') === 0 && strpos($dstFieldID, 'UF_') === 0) { self::mapUserField(\CCrmOwnerType::Quote, $srcFieldID, $srcFields, $dstEntityTypeID, $dstFieldID, $dstFields, $options); } elseif ($srcFieldID === 'PRODUCT_ROWS') { $productRows = \CCrmQuote::LoadProductRows($this->srcEntityID); if (count($productRows) > 0) { if ($dstEntityTypeID === \CCrmOwnerType::Invoice) { $currencyID = isset($srcFields['CURRENCY_ID']) ? $srcFields['CURRENCY_ID'] : ''; if ($currencyID === '' || !\CCrmCurrency::IsExists($currencyID)) { $currencyID = \CCrmCurrency::GetBaseCurrencyID(); } $actualRows = \CCrmInvoice::ProductRows2BasketItems($productRows, $currencyID, \CCrmInvoice::GetCurrencyID()); if (count($actualRows) > 0) { foreach ($actualRows as &$productRow) { unset($productRow['ID']); } unset($productRow); $dstFields[$dstFieldID] = $actualRows; } } else { foreach ($productRows as &$productRow) { unset($productRow['ID']); } unset($productRow); $dstFields[$dstFieldID] = $productRows; } } } elseif (isset($srcFields[$srcFieldID])) { $dstFields[$dstFieldID] = $srcFields[$srcFieldID]; } } if (!empty($dstFields) && $dstEntityTypeID === \CCrmOwnerType::Invoice) { $dstFields['UF_QUOTE_ID'] = $this->srcEntityID; } return $dstFields; }