CCrmInvoiceRestService::update

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmInvoiceRestService
  4. update
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/restservice_invoice.php
  • Класс: \CCrmInvoiceRestService
  • Вызов: CCrmInvoiceRestService::update
static function update($params)
{
	/** @global CMain $APPLICATION*/
	global $APPLICATION, $DB;

	$ID = CCrmInvoiceRestUtil::getParamScalar($params, 'id', 0);
	if($ID <= 0)
		throw new RestException('Invalid identifier.');

	$invoice = new CCrmInvoice();
	if(!CCrmInvoice::CheckUpdatePermission($ID))
		throw new RestException('Access denied.');

	$fields = CCrmInvoiceRestUtil::getParamArray($params, 'fields');
	$fields = self::filterFields($fields, 'update');

	// sanitize
	$updateComments = isset($fields['COMMENTS']);
	$updateUserDescription = isset($fields['USER_DESCRIPTION']);
	$comments = $updateComments ? trim($fields['COMMENTS']) : '';
	$userDescription = $updateUserDescription ? trim($fields['USER_DESCRIPTION']) : '';
	$bSanitizeComments = ($comments !== '' && mb_strpos($comments, '<') !== false);
	$bSanitizeUserDescription = ($userDescription !== '' && mb_strpos($userDescription, '<') !== false);
	if ($bSanitizeComments || $bSanitizeUserDescription)
	{
		$sanitizer = new CBXSanitizer();
		$sanitizer->ApplyDoubleEncode(false);
		$sanitizer->SetLevel(CBXSanitizer::SECURE_LEVEL_MIDDLE);
		//Crutch for for Chrome line break behaviour in HTML editor.
		$sanitizer->AddTags(array('div' => array()));
		if ($bSanitizeComments)
			$fields['COMMENTS'] = $sanitizer->SanitizeHtml($fields['COMMENTS']);
		if ($bSanitizeUserDescription)
			$fields['USER_DESCRIPTION'] = $sanitizer->SanitizeHtml($fields['USER_DESCRIPTION']);
		unset($sanitizer);
	}
	unset($bSanitizeComments, $bSanitizeUserDescription);
	if ($updateComments)
		$fields['COMMENTS'] = $comments;
	if ($updateUserDescription)
		$fields['USER_DESCRIPTION'] = $userDescription;
	unset($updateComments, $updateUserDescription, $comments, $userDescription);

	if (!is_array($fields) || count($fields) === 0)
		throw new RestException('Invalid parameters.');

	$updateProps = is_array($fields['INVOICE_PROPERTIES']) ? $fields['INVOICE_PROPERTIES'] : array();

	$origFields = self::getInvoiceDataByID($ID);
	$origFields = self::filterFields($origFields, 'update', false);
	foreach ($origFields as $fName => $fValue)
	{
		if (!array_key_exists($fName, $fields) && $fName !== 'DATE_INSERT' && $fName !== 'DATE_UPDATE')
			$fields[$fName] = $fValue;
	}

	self::internalizeUserFields(
		$fields,
		array(
			'IGNORED_ATTRS' => array(
				CCrmFieldInfoAttr::Immutable,
				CCrmFieldInfoAttr::UserPKey
			)
		)
	);

	$bStatusSuccess = CCrmStatusInvoice::isStatusSuccess($fields['STATUS_ID']);
	if ($bStatusSuccess)
		$bStatusFailed = false;
	else
		$bStatusFailed = CCrmStatusInvoice::isStatusFailed($fields['STATUS_ID']);

	// person type
	$arPersonTypes = CCrmPaySystem::getPersonTypeIDs();
	if (!isset($arPersonTypes['COMPANY']) || !isset($arPersonTypes['CONTACT']))
		throw new RestException('Incorrect values in the peson type settings.');
	$personTypeId = isset($fields['PERSON_TYPE_ID']) ? (int)$fields['PERSON_TYPE_ID'] : 0;
	if (isset($fields['UF_COMPANY_ID']) && intval($fields['UF_COMPANY_ID']) > 0)
		$personTypeId = (int)$arPersonTypes['COMPANY'];
	else if (isset($fields['UF_CONTACT_ID']) && intval($fields['UF_CONTACT_ID']) > 0)
		$personTypeId = (int)$arPersonTypes['CONTACT'];
	if ($personTypeId !== intval($arPersonTypes['COMPANY']) && $personTypeId !== intval($arPersonTypes['CONTACT']))
	{
		throw new RestException('Incorrect value of PERSON_TYPE_ID field ('.
			$arPersonTypes['CONTACT'].' - Contact, '.$arPersonTypes['CONTACT'].' - Company)');
	}
	$fields['PERSON_TYPE_ID'] = $personTypeId;

	if (!is_array($fields['INVOICE_PROPERTIES']))
	{
		$fields['INVOICE_PROPERTIES'] = array();
	}
	if (isset($fields['PR_LOCATION']) && is_array($fields['INVOICE_PROPERTIES']))
	{
		$fields['INVOICE_PROPERTIES']['LOCATION'] = $updateProps['LOCATION'] = $fields['PR_LOCATION'];
	}

	$options = array();
	if(!self::isRequiredUserFieldCheckEnabled())
	{
		$options['DISABLE_REQUIRED_USER_FIELD_CHECK'] = true;
	}

	if (!$invoice->CheckFields($fields, $ID, $bStatusSuccess, $bStatusFailed, $options))
	{
		if (!empty($invoice->LAST_ERROR))
			throw new RestException($invoice->LAST_ERROR);
		else
			throw new RestException('Error on check fields.');
	}

	$propsInfo = CCrmInvoice::GetPropertiesInfo($fields['PERSON_TYPE_ID']);
	$propsInfo = is_array($propsInfo[$fields['PERSON_TYPE_ID']]) ? $propsInfo[$fields['PERSON_TYPE_ID']] : array();
	$invoiceProperties = array();
	foreach ($propsInfo as $propCode => $arProp)
	{
		if (array_key_exists($propCode, $fields['INVOICE_PROPERTIES']))
		{
			$invoiceProperties[$arProp['ID']] = $fields['INVOICE_PROPERTIES'][$propCode];
		}
		else if ($propCode === 'COMPANY_NAME' && array_key_exists('COMPANY', $fields['INVOICE_PROPERTIES']))    // ua company name hack
		{
			$invoiceProperties[$arProp['ID']] = $fields['INVOICE_PROPERTIES']['COMPANY'];
		}
		else if (is_array($origFields['INVOICE_PROPERTIES']))
		{
			if (array_key_exists($propCode, $origFields['INVOICE_PROPERTIES']))
			{
				$invoiceProperties[$arProp['ID']] = $origFields['INVOICE_PROPERTIES'][$propCode];
			}
			else if ($propCode === 'COMPANY_NAME'
				&& array_key_exists('COMPANY', $fields['INVOICE_PROPERTIES']))    // ua company name hack
			{
				$invoiceProperties[$arProp['ID']] = $origFields['INVOICE_PROPERTIES']['COMPANY'];
			}
		}
	}
	$fields['INVOICE_PROPERTIES'] = $invoiceProperties;
	unset($propCode, $arProp);
	$invoiceProperties = array();
	foreach ($updateProps as $code => $value)
	{
		if (array_key_exists($code, $propsInfo))
		{
			$invoiceProperties[$propsInfo[$code]['ID']] = $value;
		}
		else if ($code === 'COMPANY' && array_key_exists('COMPANY_NAME', $propsInfo))    // ua company name hack
		{
			$invoiceProperties[$propsInfo['COMPANY_NAME']['ID']] = $value;
		}
	}
	$updateProps = $invoiceProperties;
	unset($propsInfo, $invoiceProperties, $code, $value);

	$defRqLinkParams = Requisite\EntityLink::determineRequisiteLinkBeforeSave(
		CCrmOwnerType::Invoice, $ID, Requisite\EntityLink::ENTITY_OPERATION_UPDATE, $fields
	);

	//region Autocomplete property values
	$companyId = 0;
	$contactId = 0;
	$requisiteIdLinked = 0;

	if (isset($defRqLinkParams['CLIENT_ENTITY_TYPE_ID']) && isset($defRqLinkParams['CLIENT_ENTITY_ID'])
		&& $defRqLinkParams['CLIENT_ENTITY_ID'] > 0)
	{
		if ($defRqLinkParams['CLIENT_ENTITY_TYPE_ID'] === CCrmOwnerType::Company)
		{
			$companyId = (int)$defRqLinkParams['CLIENT_ENTITY_ID'];
		}
		else if ($defRqLinkParams['CLIENT_ENTITY_TYPE_ID'] === CCrmOwnerType::Contact)
		{
			$contactId = (int)$defRqLinkParams['CLIENT_ENTITY_ID'];
		}
	}
	if ($contactId <= 0 && isset($fields['UF_CONTACT_ID']) && $fields['UF_CONTACT_ID'] > 0)
	{
		$contactId = (int)$fields['UF_CONTACT_ID'];
	}
	if (isset($defRqLinkParams['REQUISITE_ID']) && $defRqLinkParams['REQUISITE_ID'] > 0)
	{
		$requisiteIdLinked = $defRqLinkParams['REQUISITE_ID'];
	}
	$props = $invoice->GetProperties($ID, $personTypeId);
	CCrmInvoice::__RewritePayerInfo($companyId, $contactId, $props);
	CCrmInvoice::rewritePropsFromRequisite($personTypeId, $requisiteIdLinked, $props);
	$formProps = array();
	$propsValues = $invoice->ParsePropertiesValuesFromPost($personTypeId, $formProps, $props);
	if (isset($propsValues['PROPS_VALUES']) && is_array($propsValues['PROPS_VALUES']))
	{
		foreach($propsValues['PROPS_VALUES'] as $propertyId => $propertyValue)
		{
			if (!isset($updateProps[$propertyId]) || $updateProps[$propertyId] === '')
			{
				$fields['INVOICE_PROPERTIES'][$propertyId] = $propertyValue;
			}
		}
		unset($propertyId, $propertyValue);
	}
	unset($companyId, $contactId, $requisiteIdLinked, $props, $propsValues, $formProps);
	//endregion Autocomplete property values

	$DB->StartTransaction();
	$ID = $invoice->Update($ID, $fields, array('UPDATE_SEARCH' => true));
	if(!is_int($ID) || $ID <= 0)
	{
		$DB->Rollback();

		$errMsg = '';
		if (!empty($invoice->LAST_ERROR))
		{
			$errMsg = $invoice->LAST_ERROR;
		}
		else
		{
			$ex = $APPLICATION->GetException();
			if ($ex)
			{
				$APPLICATION->ResetException();
				if ($errMsg == '')
					$errMsg = $ex->GetString();
			}
		}
		throw new RestException((!empty($errMsg) ? $errMsg : 'Unknown error during invoice updating.')."
\n"); } else { Requisite\EntityLink::register( CCrmOwnerType::Invoice, $ID, $defRqLinkParams['REQUISITE_ID'], $defRqLinkParams['BANK_DETAIL_ID'], $defRqLinkParams['MC_REQUISITE_ID'], $defRqLinkParams['MC_BANK_DETAIL_ID'] ); $DB->Commit(); } return $ID; }

Добавить комментарий