public function addResult($resultFields, $resultParameters = array())
{
$this->errors = array();
// get section fields
$sectionFields = array();
$currentSection = '';
$fieldsMap = $this->getFieldsMap();
foreach($fieldsMap as $field)
{
if($field['type'] == 'section')
{
$currentSection = $field['name'];
continue;
}
if(!$currentSection)
{
continue;
}
$sectionFields[$currentSection][] = $field['name'];
}
// format fields by name
$fields = array();
foreach($resultFields as $fieldKey => $field)
{
$fields[$field['name']] = $field;
}
$hiddenFieldNames = array();
// set hidden flag
foreach($resultFields as $fieldKey => $field)
{
if(!isset($field['dependences']))
{
$field['hidden'] = false;
$data['FIELDS'][$fieldKey] = $field;
continue;
}
$isHidden = false;
foreach($field['dependences'] as $dep)
{
if($dep['if']['action'] != 'change')
{
continue;
}
if(!isset($fields[$dep['if']['fieldname']]))
{
continue;
}
$valuesA = $fields[$dep['if']['fieldname']]['values'];
$valueB = $dep['if']['value'];
switch($dep['if']['operation'])
{
case '!=':
$isSuccess = !in_array($valueB, $valuesA);
break;
default:
$isSuccess = in_array($valueB, $valuesA);
break;
}
if(!$isSuccess)
{
continue;
}
$isHidden = $dep['do']['action'] == 'hide';
}
if($field['type'] == 'section')
{
foreach($sectionFields[$field['name']] as $sectionFieldName)
{
$hiddenFieldNames[$sectionFieldName][] = $isHidden;
}
}
else
{
$field['hidden'] = $isHidden;
$resultFields[$fieldKey] = $field;
}
}
$fieldEmailValue = null;
$fieldPhoneValue = null;
$fieldPhoneEntityTypeName = null;
foreach($resultFields as $fieldKey => $field)
{
if(($field['entity_field_name'] == 'EMAIL' || $field['type'] == 'email') && $field['values'][0])
{
$fieldEmailValue = $field['values'][0];
}
if(($field['entity_field_name'] == 'PHONE' || $field['type'] == 'phone') && $field['values'][0])
{
$fieldPhoneEntityTypeName = $field['entity_name'];
$fieldPhoneValue = $field['values'][0];
}
if(!isset($hiddenFieldNames[$field['name']]))
{
continue;
}
$field['hidden'] = end($hiddenFieldNames[$field['name']]);
$resultFields[$fieldKey] = $field;
}
$resultProducts = array();
$activityFields = array();
foreach($resultFields as $fieldKey => $field)
{
if(!isset($field['values'][0]) || (!$field['values'][0] && $field['values'][0] !== '0'))
{
continue;
}
$activityFieldValues = array();
if(is_array($field['items']) && count($field['items']) > 0)
{
if (!empty($field['values'][0]['id']))
{
$fieldValues = array_column($field['values'], 'id');
}
elseif (!empty($field['values'][0]['value']))
{
$fieldValues = array_column($field['values'], 'value');
}
else
{
$fieldValues = $field['values'];
}
foreach($field['items'] as $item)
{
if(!in_array($item['value'], $fieldValues))
{
continue;
}
$activityFieldValues[] = $item;
}
}
else
{
$activityFieldValues = $field['values'];
}
if ($field['type'] === Internals\FieldTable::TYPE_ENUM_DATETIME)
{
$activityFieldValues = array_map(
static function ($fieldValue) {
$dateTimeWithAtomFormat = DateTime::tryParse($fieldValue, DATE_ATOM);
return ($dateTimeWithAtomFormat ?? DateTime::createFromUserTime($fieldValue))->getTimestamp();
},
$activityFieldValues
);
}
$activityFields[] = array(
'type' => $field['type'],
'code' => $field['name'],
'required' => $field['required'],
'caption' => $field['caption'],
'value' => $activityFieldValues,
);
if($field['type'] != 'product')
{
continue;
}
$productValues = [];
foreach ($field['values'] as $value)
{
$productValue = [
'id' => 0,
'quantity' => 1
];
if (is_array($value))
{
$productValue['id'] = $value['id'] ?: 0;
$productValue['quantity'] = $value['quantity'] ?: 1;
$productValue['price'] = $value['price'] ?: null;
}
else
{
$productValue['id'] = $value;
}
$productValues[$productValue['id']] = $productValue;
}
foreach($field['items'] as $item)
{
$productValue = $productValues[$item['value']];
if(!is_array($productValue))
{
continue;
}
$productId = is_numeric($item['value']) ? $item['value'] : 0;
$product = [
'ID' => $productId,
'NAME' => $item['title'],
'PRICE' => $item['changeablePrice'] && $productValue['price']
? $productValue['price']
: $item['price'],
'DISCOUNT' => $item['discount'] ?: 0,
'QUANTITY' => $productValue['quantity'],
];
if ($productId && ($productData = \CCrmProduct::GetByID($productId)))
{
$product['VAT_INCLUDED'] = $productData['VAT_INCLUDED'];
if ($productData['VAT_ID'])
{
$vatData = \CCrmVat::GetByID($productData['VAT_ID']);
if ($vatData && $vatData['RATE'])
{
$product['VAT_RATE'] = floatval($vatData['RATE']) / 100;
}
}
}
$resultProducts[] = $product;
}
}
// set responsible
$responsibleQueue = new ResponsibleQueue($this->id);
$responsibleId = $responsibleQueue->getNextId();
$this->params['ASSIGNED_BY_ID'] = $responsibleId ? $responsibleId : 1;
// add Result
$data = array(
'FORM' => $this->params,
'DUPLICATE_MODE' => $this->params['DUPLICATE_MODE'],
'PRESET_FIELDS' => $this->params['PRESET_FIELDS'],
'COMMON_FIELDS' => isset($resultParameters['COMMON_FIELDS']) ? $resultParameters['COMMON_FIELDS'] : array(),
'AGREEMENTS' => $resultParameters['AGREEMENTS'] ?? [],
'COMMON_DATA' => $resultParameters['COMMON_DATA'],
'PLACEHOLDERS' => isset($resultParameters['PLACEHOLDERS']) ? $resultParameters['PLACEHOLDERS'] : array(),
'DISABLE_FIELD_CHECKING' => $resultParameters['DISABLE_FIELD_CHECKING'] ?? false,
'ORIGIN_ID' => isset($resultParameters['ORIGIN_ID']) ? $resultParameters['ORIGIN_ID'] : null,
'ENTITY_SCHEME' => $this->params['ENTITY_SCHEME'],
'INVOICE_SETTINGS' => $this->params['INVOICE_SETTINGS'],
'FORM_ID' => $this->id,
'FIELDS' => $resultFields,
'ASSIGNED_BY_ID' => $this->params['ASSIGNED_BY_ID'],
'PRODUCTS' => $resultProducts,
'CURRENCY_ID' => $this->getCurrencyId(),
'ACTIVITY_FIELDS' => $activityFields,
'IS_CALLBACK' => $this->isCallback(),
'CALLBACK_PHONE' => $fieldPhoneValue,
'ENTITIES' => isset($resultParameters['ENTITIES']) ? $resultParameters['ENTITIES'] : [],
);
$result = new Result(null, $data);
$result->save();
if($result->hasErrors())
{
$this->errors = $result->getErrors();
}
else
{
if($fieldEmailValue)
{
self::sendEventFormSent(array(
'RESULT_SUCCESS_TEXT' => $this->params['RESULT_SUCCESS_TEXT'],
'RESULT_SUCCESS_URL' => $this->params['RESULT_SUCCESS_URL'],
'RESULT_FAILURE_TEXT' => $this->params['RESULT_FAILURE_TEXT'],
'RESULT_FAILURE_URL' => $this->params['RESULT_FAILURE_URL'],
'FORM_CAPTION' => $this->params['CAPTION'],
'EMAIL_TO' => $fieldEmailValue,
)
);
}
$stopCallBack = false;
if (isset($resultParameters['STOP_CALLBACK']) && $resultParameters['STOP_CALLBACK'])
{
$stopCallBack = true;
}
if($fieldPhoneValue && $this->isCallback())
{
if(Callback::hasPhoneNumbers())
{
Callback::sendCallEvent(array(
'CRM_ENTITY_TYPE' => $fieldPhoneEntityTypeName,
'CRM_ENTITY_ID' => $result->getResultEntity()->getEntityIdByTypeName($fieldPhoneEntityTypeName),
'CALL_FROM' => $this->params['CALL_FROM'],
'CALL_TO' => $fieldPhoneValue,
'TEXT' => $this->params['CALL_TEXT'],
'STOP_CALLBACK' => $stopCallBack,
'CRM_ENTITY_LIST' => $result->getResultEntity()->getResultEntities()
));
}
}
if ($this->isWhatsApp())
{
WhatsApp::sendEvent(array(
'FORM_ID' => $this->id,
'LANG_ID' => $this->getLanguageId(),
'CRM_ENTITY_TYPE' => $fieldPhoneEntityTypeName,
'CRM_ENTITY_ID' => $result->getResultEntity()->getEntityIdByTypeName($fieldPhoneEntityTypeName),
'PHONE_NUMBER' => $fieldPhoneValue,
'CRM_ENTITY_LIST' => $result->getResultEntity()->getResultEntities()
));
}
$redirectUrl = $this->params['RESULT_SUCCESS_URL'];
if($this->isPayable())
{
$resultEntity = $result->getResultEntity();
if ($resultEntity)
{
$resultRedirectUrl = null;
if ($resultEntity->getOrderId())
{
$excludedPaySystems = $this->params['FORM_SETTINGS']['DISABLED_PAY_SYSTEMS'] ?? [];
$urlInfo = SalesCenter\Integration\LandingManager::getInstance()->getUrlInfoByOrderId(
$resultEntity->getOrderId(),
[
'paymentId' => $resultEntity->getPaymentId() ?: 0,
'excludedPS' => $excludedPaySystems
]
);
$resultRedirectUrl = $urlInfo['url'] ?? null;
}
elseif($resultEntity->getInvoiceId())
{
$resultRedirectUrl = \CAllCrmInvoice::getPublicLink($resultEntity->getInvoiceId());
}
if($resultRedirectUrl)
{
$redirectUrl = new \Bitrix\Main\Web\Uri($resultRedirectUrl);
$redirectUrl->addParams(array('form_id' => $this->getId()));
$redirectUrl = $redirectUrl->getLocator();
}
}
}
$result->setUrl($redirectUrl);
}
return $result;
}