22 private array $activityNames = [];
29 $this->
setType(static::TYPE_FIELD);
38 foreach (
$params[
'items'] as [$item, $joiner])
40 if (!empty($item[
'field']))
43 $this->
addItem($condition, $joiner);
57 $documentId = $documentType;
60 return $this->evaluateByDocument($documentType, $documentId);
69 public function evaluateByDocument(
array $documentType,
array $documentId,
array $document =
null): bool
71 if (empty($this->items))
76 if ($this->
getType() === static::TYPE_MIXED)
81 $documentService = \CBPRuntime::getRuntime(
true)->getDocumentService();
83 if ($document ===
null)
86 foreach ($this->items as $item)
88 $condition = $item[0];
89 $select[] = $condition->getField();
92 $document = $documentService->getDocument($documentId, $documentType,
$select);
97 $joiner = static::JOINER_AND;
99 $this->evaluateResults = [];
101 foreach ($this->items as $item)
104 $condition = $item[0];
105 $conditionField = $condition->getField();
107 $conditionResult =
true;
109 $fld = $document[$conditionField] ??
null;
110 $fieldType = $this->getFieldTypeObject($documentService, $documentType, $conditionField);
112 if (!$condition->checkValue($fld, $fieldType, $documentId))
114 $conditionResult =
false;
117 if ($joiner == static::JOINER_OR)
122 elseif (!$conditionResult)
127 $this->evaluateResults[] = [
128 'condition' => $condition->toArray(),
130 'fieldValue' => $fld ? $fieldType->formatValue($fld) :
null,
131 'result' => $conditionResult ?
'Y' :
'N',
134 $joiner = ($item[1] === static::JOINER_OR) ? static::JOINER_OR : static::
JOINER_AND;
146 if ($type === static::TYPE_FIELD || $type === static::TYPE_MIXED)
168 $this->items[] = [$condition, $joiner];
182 $this->activityNames = [
184 'Branch1' =>
$activity[
'Children'][0][
'Name'],
185 'Branch2' =>
$activity[
'Children'][1][
'Name'],
191 if (isset($this->activityNames))
193 return $this->activityNames;
201 $this->activated = $isActivated;
212 public function toArray()
217 foreach ($this->
getItems() as [$condition, $joiner])
219 $itemsArray[] = [$condition->toArray(), $joiner];
224 'items' => $itemsArray,
235 public function createBizprocActivity(
array $childActivity,
array $documentType, Template
$template)
237 $mixedCondition = [];
241 foreach ($this->
getItems() as [$condition, $joiner])
243 $object = $condition->getObject();
244 $field = $condition->getField();
245 $value = $condition->getValue();
246 $property =
$template->getProperty($object, $field);
248 $operator = $condition->getOperator();
249 $isOperatorWithValue = !in_array(
254 if ($property && $isOperatorWithValue)
256 $fieldInputValueResult = $this->getFieldInputValue($property, $documentType, $condition);
257 if ($fieldInputValueResult->isSuccess())
259 $value = $fieldInputValueResult->getData()[
'value'];
263 $mixedCondition[] = [
266 'operator' => $condition->getOperator(),
267 'value' => self::unConvertExpressions($value, $documentType),
268 'joiner' => $bizprocJoiner,
270 $bizprocJoiner = ($joiner === static::JOINER_OR) ? 1 : 0;
273 $title = Loc::getMessage(
'BIZPROC_AUTOMATION_CONDITION_TITLE');
274 $activated = $childActivity[
'Activated'] ===
'N' ?
'N' :
'Y';
276 'Type' =>
'IfElseActivity',
279 'Properties' => [
'Title' =>
$title],
282 'Type' =>
'IfElseBranchActivity',
286 'mixedcondition' => $mixedCondition
288 'Children' => [$childActivity],
292 'Type' =>
'IfElseBranchActivity',
296 'truecondition' =>
'1',
315 $conditionGroup =
false;
319 &&
$activity[
'Children'][0][
'Type'] ===
'IfElseBranchActivity'
320 &&
$activity[
'Children'][1][
'Type'] ===
'IfElseBranchActivity'
322 !empty(
$activity[
'Children'][0][
'Properties'][
'fieldcondition'])
324 !empty(
$activity[
'Children'][0][
'Properties'][
'mixedcondition'])
326 && !empty(
$activity[
'Children'][1][
'Properties'][
'truecondition'])
330 $conditionGroup =
new static();
331 $conditionGroup->setType(static::TYPE_MIXED);
332 $conditionGroup->setActivityNames(
$activity);
333 $conditionGroup->setActivated(\CBPHelper::getBool(
$activity[
'Activated'] ??
true));
335 $isMixed = isset(
$activity[
'Children'][0][
'Properties'][
'mixedcondition']);
336 $bizprocConditions =
$activity[
'Children'][0][
'Properties'][$isMixed?
'mixedcondition':
'fieldcondition'];
338 foreach ($bizprocConditions as $index => $condition)
342 $condition = self::convertDocumentCondition($condition);
345 $property =
$template->getProperty($condition[
'object'], $condition[
'field']);
346 if ($property && $property[
'Type'] ===
'user')
348 $condition[
'value'] = \CBPHelper::UsersArrayToString(
355 if ($property && $property[
'Type'] ===
'time')
357 $offset = \CTimeZone::GetOffset();
358 $condition[
'value'] = array_map(
359 static fn($value) =>
Bizproc\
BaseType\Value\Time::tryMakeCorrectFormat($value, $offset),
360 (
array)$condition[
'value']
365 'object' => $condition[
'object'],
366 'field' => $condition[
'field'],
367 'operator' => $condition[
'operator'],
368 'value' => self::convertExpressions($condition[
'value'], $documentType),
371 $nextCondition = isset($bizprocConditions[$index + 1]) ? $bizprocConditions[$index + 1] :
null;
372 $joiner = ($nextCondition && (!empty($nextCondition[3]) || !empty($nextCondition[
'joiner'])))
373 ? static::JOINER_OR : static::JOINER_AND;
375 $conditionGroup->addItem($conditionItem, $joiner);
381 return $conditionGroup;
384 private static function convertDocumentCondition(
array $condition):
array
387 'object' =>
'Document',
388 'field' => $condition[0],
389 'operator' => $condition[1],
390 'value' => $condition[2],
391 'joiner' => $condition[3],
400 public function internalizeValues(
array $documentType): self
402 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
403 $documentFields = $documentService->GetDocumentFields($documentType);
406 foreach ($this->
getItems() as [$condition, $joiner])
408 $field = $condition->getField();
409 $value = $condition->getValue();
410 $property = $documentFields[$field] ??
null;
411 if ($property && !in_array($condition->getOperator(), [
'empty',
'!empty']))
413 $condition->setValue(self::unConvertExpressions($value, $documentType));
414 $fieldInputValueResult = $this->getFieldInputValue($property, $documentType, $condition);
416 $condition->setValue(
417 $fieldInputValueResult->isSuccess() ? $fieldInputValueResult->getData()[
'value'] : $value
422 $this->internalized =
true;
437 public function externalizeValues(
array $documentType): self
439 $documentService = \CBPRuntime::GetRuntime(
true)->getDocumentService();
440 $documentFields = $documentService->GetDocumentFields($documentType);
443 foreach ($this->
getItems() as [$condition, $joiner])
445 $field = $condition->getField();
446 $value = $condition->getValue();
447 $property = isset($documentFields[$field]) ? $documentFields[$field] :
null;
448 if ($property && !in_array($condition->getOperator(), [
'empty',
'!empty']))
450 $value = self::convertExpressions($value, $documentType);
451 if ($property[
'Type'] ===
'user')
453 $value = \CBPHelper::UsersArrayToString(
459 if ($property[
'Type'] ===
'time')
461 $offset = \CTimeZone::GetOffset();
463 static fn($value) =>
Bizproc\
BaseType\Value\Time::tryMakeCorrectFormat($value, $offset),
468 $condition->setValue($value);
472 $this->internalized =
false;
477 private static function convertExpressions($value,
array $documentType)
479 if (is_array($value))
481 foreach ($value as
$k => $v)
483 $value[
$k] = self::convertExpressions($v, $documentType);
488 $value = Helper::convertExpressions($value, $documentType);
493 private static function unConvertExpressions($value,
array $documentType)
495 if (is_array($value))
497 foreach ($value as
$k => $v)
499 $value[
$k] = self::unConvertExpressions($v, $documentType);
504 $value = Helper::unConvertExpressions($value, $documentType);
509 private function getFieldTypeObject(\CBPDocumentService $documentService,
array $documentType, $conditionField): ?\Bitrix\Bizproc\FieldType
515 if (isset($documentFields[$conditionField]))
517 $fieldType = $documentService->
getFieldTypeObject($documentType, $documentFields[$conditionField]);
522 $fieldType = $documentService->
getFieldTypeObject($documentType, [
'Type' =>
'string']);
535 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
536 $conditionValue = $condition->
getValue();
546 if ($isBetweenOperator)
549 is_array($conditionValue) && isset($conditionValue[0])
554 is_array($conditionValue) && isset($conditionValue[1])
558 $property[
'Multiple'] =
false;
562 'field_greater_then',
574 $valueInternal = [$valueInternal1 ??
'', $valueInternal2 ??
''];
577 $result = new \Bitrix\Main\Result();
578 $result->setData([
'value' => $valueInternal]);
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)