• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/shipment.php
  • Класс: BitrixSaleShipment
  • Вызов: Shipment::onFieldModify
protected function onFieldModify($name, $oldValue, $value)
{
	global $USER;

	$result = new Result();

	if ($name === 'DELIVERY_ID')
	{
		if (
			$value > 0
			&& (
				$this->service === null
				|| $this->service->getId() !== (int)$value
			)
		)
		{
			$service = DeliveryServicesManager::getObjectById($value);
			if ($service)
			{
				$this->service = $service;

				$this->setField('DELIVERY_NAME', $this->service->getName());
			}
		}

		$this->getPropertyCollection()->refreshRelated();
	}
	elseif ($name === "MARKED")
	{
		if ($oldValue != "Y")
		{
			$this->setField('DATE_MARKED', new MainTypeDateTime());

			if (is_object($USER))
			{
				$this->setField('EMP_MARKED_ID', $USER->GetID());
			}
		}
		elseif ($value === "N")
		{
			$this->setField('REASON_MARKED', '');
		}
	}
	elseif ($name === "ALLOW_DELIVERY")
	{
		$this->setField('DATE_ALLOW_DELIVERY', new MainTypeDateTime());

		if (is_object($USER))
		{
			$this->setField('EMP_ALLOW_DELIVERY_ID', $USER->GetID());
		}

		if ($oldValue === 'N')
		{
			$shipmentStatus = MainConfigOption::get('sale', 'shipment_status_on_allow_delivery', '');

			$registry = Registry::getInstance(static::getRegistryType());
			/** @var DeliveryStatus $deliveryStatus */
			$deliveryStatusClassName = $registry->getDeliveryStatusClassName();

			if (
				$shipmentStatus !== ''
				&& $this->getField('STATUS_ID') != $deliveryStatusClassName::getFinalStatus()
			)
			{
				$r = $this->setStatus($shipmentStatus);
				if (!$r->isSuccess())
				{
					$result->addErrors($r->getErrors());
				}
			}
		}

		InternalsEventsPool::addEvent(
			's'.$this->getInternalIndex(),
			EventActions::EVENT_ON_SHIPMENT_ALLOW_DELIVERY,
			[
				'ENTITY' => $this,
				'VALUES' => $this->fields->getOriginalValues()
			]
		);
	}
	elseif ($name === "DEDUCTED")
	{
		$this->setField('DATE_DEDUCTED', new MainTypeDateTime());

		if (is_object($USER))
		{
			$this->setField('EMP_DEDUCTED_ID', $USER->GetID());
		}

		if ($oldValue === 'N')
		{
			$shipmentStatus = MainConfigOption::get('sale', 'shipment_status_on_shipped', '');

			$registry = Registry::getInstance(static::getRegistryType());
			/** @var DeliveryStatus $deliveryStatus */
			$deliveryStatusClassName = $registry->getDeliveryStatusClassName();

			if (strval($shipmentStatus) != '' && $this->getField('STATUS_ID') != $deliveryStatusClassName::getFinalStatus())
			{
				$r = $this->setStatus($shipmentStatus);
				if (!$r->isSuccess())
				{
					$result->addErrors($r->getErrors());
				}
			}
		}

		if ($value === 'Y')
		{
			/** @var ShipmentItem $shipmentItem */
			foreach ($this->getShipmentItemCollection() as $shipmentItem)
			{
				$r = $shipmentItem->checkMarkingCodeOnDeducted();
				if (!$r->isSuccess())
				{
					$result->addErrors($r->getErrors());
				}
			}
		}

		InternalsEventsPool::addEvent(
			's'.$this->getInternalIndex(),
			EventActions::EVENT_ON_SHIPMENT_DEDUCTED,
			[
				'ENTITY' => $this,
				'VALUES' => $this->fields->getOriginalValues()
			]
		);

		CashboxInternalsPool::addDoc($this->getOrder()->getInternalId(), $this);
	}
	elseif ($name === "STATUS_ID")
	{
		$event = new MainEvent(
			'sale',
			EventActions::EVENT_ON_BEFORE_SHIPMENT_STATUS_CHANGE,
			[
				'ENTITY' => $this,
				'VALUE' => $value,
				'OLD_VALUE' => $oldValue,
			]
		);
		$event->send();

		InternalsEventsPool::addEvent(
			's'.$this->getInternalIndex(),
			EventActions::EVENT_ON_SHIPMENT_STATUS_CHANGE,
			[
				'ENTITY' => $this,
				'VALUE' => $value,
				'OLD_VALUE' => $oldValue,
			]
		);

		InternalsEventsPool::addEvent(
			's'.$this->getInternalIndex(),
			EventActions::EVENT_ON_SHIPMENT_STATUS_CHANGE_SEND_MAIL,
			[
				'ENTITY' => $this,
				'VALUE' => $value,
				'OLD_VALUE' => $oldValue,
			]
		);
	}
	elseif ($name === 'RESPONSIBLE_ID')
	{
		$this->setField('DATE_RESPONSIBLE_ID', new MainTypeDateTime());
	}
	elseif ($name === 'TRACKING_NUMBER')
	{
		if ($value)
		{
			InternalsEventsPool::addEvent(
				's'.$this->getInternalIndex(),
				EventActions::EVENT_ON_SHIPMENT_TRACKING_NUMBER_CHANGE,
				[
					'ENTITY' => $this,
					'VALUES' => $this->getFields()->getOriginalValues(),
				]
			);
		}
	}

	$r = parent::onFieldModify($name, $oldValue, $value);
	if (!$r->isSuccess())
	{
		return $result->addErrors($r->getErrors());
	}

	if (
		$name === 'BASE_PRICE_DELIVERY'
		&& !$this->isMarkedFieldCustom('PRICE_DELIVERY')
	)
	{
		$value -= $this->getField('DISCOUNT_PRICE');

		$r = $this->setField('PRICE_DELIVERY', $value);
		if (!$r->isSuccess())
		{
			$result->addErrors($r->getErrors());
		}
	}

	if ($r->hasWarnings())
	{
		$result->addWarnings($r->getWarnings());
	}

	$result->addData($r->getData());

	if ($result->isSuccess())
	{
		$this->setFieldNoDemand('DATE_UPDATE', new MainTypeDateTime());
	}

	return $result;
}