• Модуль: tasks
  • Путь к файлу: ~/bitrix/modules/tasks/lib/scrum/service/sprintservice.php
  • Класс: BitrixTasksScrumServiceSprintService
  • Вызов: SprintService::completeSprint
public function completeSprint(
	EntityForm $sprint,
	EntityService $entityService,
	TaskService $taskService,
	KanbanService $kanbanService,
	ItemService $itemService,
	int $targetEntityId = 0,
	PushService $pushService = null
): EntityForm
{
	try
	{
		$sprint->setDateEnd(DateTime::createFromTimestamp(time()));
		$sprint->setStatus(EntityForm::SPRINT_COMPLETED);
		$sprint->setSort(0);

		$finishedTaskIds = $kanbanService->getFinishedTaskIdsInSprint($sprint->getId());
		$unFinishedTaskIds = $kanbanService->getUnfinishedTaskIdsInSprint($sprint->getId());

		$taskIdsToComplete = [];
		foreach ($finishedTaskIds as $finishedTaskId)
		{
			$isCompletedTask = $taskService->isCompletedTask($finishedTaskId);
			if ($taskService->getErrors())
			{
				$this->errorCollection->add($taskService->getErrors());

				return $sprint;
			}

			if (
				!$isCompletedTask
				&& TaskAccessController::can(
					$this->userId,
					ActionDictionary::ACTION_TASK_COMPLETE,
					$finishedTaskId
				)
			)
			{
				$taskIdsToComplete[] = $finishedTaskId;
			}
		}

		$taskService->completeTasks($taskIdsToComplete);
		if ($taskService->getErrors())
		{
			$this->errorCollection->add($taskService->getErrors());

			return $sprint;
		}

		foreach ($unFinishedTaskIds as $key => $unFinishedTaskId)
		{
			$isCompletedTask = $taskService->isCompletedTask($unFinishedTaskId);
			if ($taskService->getErrors())
			{
				$this->errorCollection->add($taskService->getErrors());

				return $sprint;
			}

			if ($isCompletedTask)
			{
				$kanbanService->addTaskToFinishStatus($sprint->getId(), $unFinishedTaskId);
				unset($unFinishedTaskIds[$key]);
			}
		}

		if ($kanbanService->getErrors())
		{
			$this->errorCollection->add($kanbanService->getErrors());

			return $sprint;
		}

		if ($targetEntityId)
		{
			$entity = $entityService->getEntityById($targetEntityId);
		}
		else
		{
			$group = Workgroup::getById($sprint->getGroupId());

			$countSprints = count($this->getSprintsByGroupId($sprint->getGroupId()));

			$newSprint = new EntityForm();

			$newSprint->setGroupId($sprint->getGroupId());
			$newSprint->setName(Loc::getMessage('TASKS_SCRUM_SPRINT_NAME', ['%s' => $countSprints + 1]));
			$newSprint->setSort(0);
			$newSprint->setCreatedBy($taskService->getUserId());
			$newSprint->setDateStart(DateTime::createFromTimestamp(time()));
			$newSprint->setDateEnd(DateTime::createFromTimestamp(time() + $group->getDefaultSprintDuration()));

			$entity = $this->createSprint($newSprint, $pushService);
		}

		foreach ($taskIdsToComplete as $taskIdToComplete)
		{
			if (!$taskService->isCompletedTask($taskIdToComplete))
			{
				$unFinishedTaskIds[] = $taskIdToComplete;
			}
		}

		$itemIds = $itemService->getItemIdsBySourceIds($unFinishedTaskIds, [$sprint->getId()]);

		if (!$itemService->getErrors() && !$this->getErrors())
		{
			$itemService->moveItemsToEntity($itemIds, $entity->getId(), $pushService);
		}

		if ($itemService->getErrors())
		{
			$this->errorCollection->add($itemService->getErrors());

			return $sprint;
		}

		$result = EntityTable::update($sprint->getId(), $sprint->getFieldsToUpdateEntity());

		if ($pushService)
		{
			$pushService->sendUpdateSprintEvent($sprint);
		}

		if (!$result->isSuccess())
		{
			$this->setErrors($result, self::ERROR_COULD_NOT_COMPLETE_SPRINT);
		}

		(new CacheService($sprint->getGroupId(), CacheService::STATS))->clean();
		(new CacheService($sprint->getGroupId(), CacheService::TEAM_STATS))->cleanRoot();
	}
	catch (Exception $exception)
	{
		$this->errorCollection->setError(
			new Error($exception->getMessage(), self::ERROR_COULD_NOT_COMPLETE_SPRINT)
		);
	}

	return $sprint;
}