• Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/lib/order/shipment.php
  • Класс: Bitrix\Crm\Order\Shipment
  • Вызов: Shipment::onAfterSave
protected function onAfterSave($isNew)
{
	if (!$this->isSystem())
	{
		if ($isNew)
		{
			$this->addTimelineEntryOnCreate();

			$deliveryService = $this->getDelivery();
			$deliveryRequestHandler = $deliveryService ? $deliveryService->getDeliveryRequestHandler() : null;
			if ($deliveryRequestHandler && $deliveryRequestHandler->hasCallbackTrackingSupport())
			{
				Delivery::addActivity($this);
			}
		}
		else
		{
			if ($this->fields->isChanged('STATUS_ID'))
			{
				$this->addTimelineEntryOnStatusModify();
			}

			if ($this->fields->isChanged('PRICE_DELIVERY') || $this->fields->isChanged('CURRENCY') )
			{
				$this->updateTimelineCreationEntity();
			}
		}
	}

	$automationAvailable = Crm\Automation\Factory::isAutomationAvailable(\CCrmOwnerType::Order);

	if ($automationAvailable)
	{
		if (!$this->isSystem() && !$isNew && $this->isChanged())
		{
			Crm\Automation\Trigger\ShipmentChangedTrigger::execute(
				[['OWNER_TYPE_ID' => \CCrmOwnerType::Order, 'OWNER_ID' => $this->getField('ORDER_ID')]],
				['SHIPMENT' => $this]
			);
		}

		if ($this->fields->isChanged('ALLOW_DELIVERY') && $this->isAllowDelivery())
		{
			Crm\Automation\Trigger\AllowDeliveryTrigger::execute(
				[['OWNER_TYPE_ID' => \CCrmOwnerType::Order, 'OWNER_ID' => $this->getField('ORDER_ID')]],
				['SHIPMENT' => $this]
			);
		}

		if (
			$this->fields->isChanged('STATUS_ID')
			&& $this->getField('STATUS_ID') === DeliveryStatus::getFinalStatus()
		)
		{
			Crm\Automation\Trigger\ShipmentChangedTrigger::execute(
				[['OWNER_TYPE_ID' => \CCrmOwnerType::Order, 'OWNER_ID' => $this->getField('ORDER_ID')]],
				['SHIPMENT' => $this]
			);
		}

		if ($this->fields->isChanged('TRACKING_NUMBER') && !empty($this->getField('TRACKING_NUMBER')))
		{
			Crm\Automation\Trigger\FillTrackingNumberTrigger::execute(
				[['OWNER_TYPE_ID' => \CCrmOwnerType::Order, 'OWNER_ID' => $this->getField('ORDER_ID')]],
				['SHIPMENT' => $this]
			);
		}
	}

	if ($this->fields->isChanged('DEDUCTED'))
	{
		if ($automationAvailable && $this->getField('DEDUCTED') == "Y")
		{
			Crm\Automation\Trigger\DeductedTrigger::execute(
				[['OWNER_TYPE_ID' => \CCrmOwnerType::Order, 'OWNER_ID' => $this->getField('ORDER_ID')]],
				['SHIPMENT' => $this]
			);
		}

		if (!$isNew && !$this->isSystem() && !$this->getOrder()->isNew())
		{
			$timelineBindingsOptions = [];
			if (\Bitrix\Main\Config\Option::get('catalog', 'default_use_store_control', 'N') === 'Y')
			{
				$timelineBindingsOptions['withDeal'] = false;
			}

			$timelineParams = [
				'FIELDS' => $this->getFieldValues(),
				'SETTINGS' => [
					'CHANGED_ENTITY' => \CCrmOwnerType::OrderShipmentName,
					'FIELDS' => [
						'ORDER_DEDUCTED' => $this->getField('DEDUCTED'),
						'ORDER_DONE' => 'N'
					],
				],
				'BINDINGS' => BindingsMaker\TimelineBindingsMaker::makeByShipment($this, $timelineBindingsOptions)
			];

			Crm\Timeline\OrderShipmentController::getInstance()->onDeducted($this->getId(), $timelineParams);
		}
	}

	if(Main\Loader::includeModule('pull'))
	{
		\CPullWatch::AddToStack(
			'CRM_ENTITY_ORDER_SHIPMENT',
			[
				'module_id' => 'crm',
				'command' => 'onOrderShipmentSave',
				'params' => [
					'FIELDS' => $this->getFieldValues()
				]
			]
		);
	}
}