• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/Service/Factory/SmartInvoice.php
  • Класс: Bitrix\Crm\Service\Factory\SmartInvoice
  • Вызов: SmartInvoice::createTypeIfNotExists
static function createTypeIfNotExists(): void
{
	$type = TypeTable::getByEntityTypeId(\CCrmOwnerType::SmartInvoice)->fetchObject();
	if ($type)
	{
		return;
	}

	Container::getInstance()->getLocalization()->loadMessages();

	$type =
		TypeTable::createObject()
		->setName('SmartInvoice')
		->setEntityTypeId(\CCrmOwnerType::SmartInvoice)
		->setTitle('Smart Invoice')
		->setCode('BX_SMART_INVOICE')
		->setCreatedBy(0)
		->setIsCategoriesEnabled(false)
		->setIsStagesEnabled(true)
		->setIsBeginCloseDatesEnabled(true)
		->setIsClientEnabled(true)
		->setIsUseInUserfieldEnabled(true)
		->setIsLinkWithProductsEnabled(true)
		->setIsCrmTrackingEnabled(true)
		->setIsMycompanyEnabled(true)
		->setIsDocumentsEnabled(true)
		->setIsSourceEnabled(true)
		->setIsObserversEnabled(true)
		->setIsRecyclebinEnabled(true)
		->setIsAutomationEnabled(true)
		->setIsBizProcEnabled(true)
		->setIsPaymentsEnabled(true)
		->setIsCountersEnabled(true)
	;

	/** @var AddResult $result */
	$result = $type->save();
	if ($result->isSuccess())
	{
		$userFieldsMap = UserFieldManager::getLinkedUserFieldsMap();
		foreach ($userFieldsMap as $ufName => $description)
		{
			$isSuccess = UserFieldManager::enableEntityInUserField($description, \CCrmOwnerType::SmartInvoiceName, true);
			if (!$isSuccess)
			{
				$result->addError(new Error("Attempt to enable SmartInvoice in user field {$ufName} was unsuccessful"));
			}
		}

		// add parent fields
		$conversionSettings =
			(new Relation\Settings())
				->setRelationType(Relation\RelationType::CONVERSION)
				->setIsChildrenListEnabled(true)
		;

		$relationManager = Container::getInstance()->getRelationManager();

		$bindResult = $relationManager->bindTypes(
			new Relation(
				new RelationIdentifier(\CCrmOwnerType::Deal, \CCrmOwnerType::SmartInvoice),
				clone $conversionSettings,
			)
		);
		if (!$bindResult->isSuccess())
		{
			$result->addErrors($bindResult->getErrors());
		}

		$bindResult = $relationManager->bindTypes(
			new Relation(
				new RelationIdentifier(\CCrmOwnerType::Quote, \CCrmOwnerType::SmartInvoice),
				clone $conversionSettings,
			)
		);
		if (!$bindResult->isSuccess())
		{
			$result->addErrors($bindResult->getErrors());
		}

		$factory = Container::getInstance()->getFactory(\CCrmOwnerType::SmartInvoice);
		if ($factory)
		{
			// copy permissions
			$defaultCategoryId = $factory->createDefaultCategoryIfNotExist()->getId();
			$userPermissions = Container::getInstance()->getUserPermissions();
			$smartInvoicePermissionEntity = $userPermissions::getPermissionEntityType(
				\CCrmOwnerType::SmartInvoice,
				$defaultCategoryId
			);
			$oldInvoicePermissionEntity = $userPermissions::getPermissionEntityType(\CCrmOwnerType::Invoice);
			$permissions = RolePermission::getByEntityId($oldInvoicePermissionEntity);
			$permissionsCopingResult = RolePermission::setByEntityId($smartInvoicePermissionEntity, $permissions);
			if (!$permissionsCopingResult->isSuccess())
			{
				$result->addErrors($permissionsCopingResult->getErrors());
			}

			// bind templates
			$documentGeneratorManager = DocumentGeneratorManager::getInstance();
			if ($documentGeneratorManager->isEnabled())
			{
				$invoicesProvider = DataProvider\Invoice::class;

				$smartInvoiceProviderCode = DataProvider\SmartInvoice::getProviderCode(
					\CCrmOwnerType::SmartInvoice,
					$factory->getDefaultCategory()->getId()
				);
				$copyTemplatesResult = $documentGeneratorManager->copyTemplatesProviders($invoicesProvider, $smartInvoiceProviderCode);
				if (!$copyTemplatesResult->isSuccess())
				{
					$result->addErrors($copyTemplatesResult->getErrors());
				}
			}

			// hide old invoices if there are no invoices created during last month
			$lastMonthCreatedInvoicesCount = InvoiceTable::getCount([
				'>DATE_INSERT' => DateTime::createFromTimestamp(time() - 30 * 24 * 3600),
			]);
			InvoiceSettings::getCurrent()->setOldInvoicesEnabled($lastMonthCreatedInvoicesCount > 0);

			// if there is at least one old invoice - show notification where to find them
			if ($lastMonthCreatedInvoicesCount <= 0)
			{
				$lastSixMonthgsCreatedInvoicesCount = InvoiceTable::getCount([
					'>DATE_INSERT' => DateTime::createFromTimestamp(time() - 183 * 24 * 3600),
				]);
				InvoiceSettings::getCurrent()->setShowInvoiceTransitionNotice($lastSixMonthgsCreatedInvoicesCount > 10);
			}
		}
		else
		{
			$result->addError(new Error('Factory for SmartInvoice was not found'));
		}
	}

	if (!$result->isSuccess())
	{
		AddMessage2Log(
			'Error while trying to create SmartInvoice type: ' . implode(', ', $result->getErrorMessages()),
			'crm',
		);
	}
}