Files
crm.clientright.ru/include/Webservices/CreateContact.php

97 lines
4.2 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*********************************************************************************
* API-интерфейс для создания Контакта
* All Rights Reserved.
* Contributor(s): Илья Руденко itsaturn@yandex.ru
********************************************************************************/
include_once 'include/Webservices/Query.php';
include_once 'modules/Users/Users.php';
require_once('include/Webservices/Utils.php');
require_once 'include/Webservices/Create.php';
require_once 'include/Webservices/Revise.php';
require_once 'includes/Loader.php';
vimport ('includes.runtime.Globals');
vimport ('includes.runtime.BaseModel');
vimport ('includes.runtime.LanguageHandler');
function vtws_createcontact($lastname, $firstname, $secondname, $email, $mobile, $tgid, $birthday, $birthplace, $mailingstreet, $inn, $requisites, $code, $user = false) {
$logstring = date("Y-m-d H:i:s").' '.json_encode($_REQUEST);
file_put_contents('logs/CreateContact.log', $logstring.PHP_EOL, FILE_APPEND);
if(empty($firstname) or empty($lastname) or empty($birthday) or empty($mobile) or empty($inn)){
$logstring = date("Y-m-d H:i:s").' Не указано одно из обязательных полей: имя, фамилия, день рождения, мобила, ИНН';
file_put_contents('logs/CreateContact.log', $logstring.PHP_EOL, FILE_APPEND);
throw new WebServiceException(WebServiceErrorCode::$INVALIDID, "Не заполнены обязательные поля");
}
$mobile = preg_replace('/[^0-9]/', '', $mobile); //Удаляем всё, что не цифры из телефона
if (strlen($mobile) == 11) {
$mobile = "7".substr($mobile, 1);
} else {
if (strlen($mobile) == 10) {
$mobile = "7".$mobile;
} else {
$logstring = date("Y-m-d H:i:s").' Некорректный номер телефона: '.$mobile;
file_put_contents('logs/CreateContact.log', $logstring.PHP_EOL, FILE_APPEND);
throw new WebServiceException(WebServiceErrorCode::$INVALIDID, "Некорректный номер телефона");
}
}
$output = 'Внутрення ошибка CRM, данные не сохранены';
$params = array (
'firstname' => $firstname,
'cf_1157' => $secondname,
'lastname' => $lastname,
'mobile' => $mobile,
'email' => $email,
'phone' => $tgid,
'birthday' => $birthday,
'cf_1263' => $birthplace,
'mailingstreet' => $mailingstreet,
'cf_1257' => $inn,
'cf_1849' => $requisites,
'cf_1580' => $code
);
global $adb, $current_user;
$query = "select c.contactid
from vtiger_contactdetails c
left join vtiger_crmentity e on e.crmid = c.contactid
where e.deleted = 0 and c.mobile = ?
limit 1";
$result = $adb->pquery($query, array($mobile));
if ($adb->num_rows($result) > 0) {
$params['id'] = '12x'.$adb->query_result($result, 0, 'contactid');
$logstring = date('Y-m-d H:i:s').' Массив для обновления Контакта: '.json_encode($params).PHP_EOL;
file_put_contents('logs/CreateContact.log', $logstring, FILE_APPEND);
try {
$contact = vtws_revise($params, $current_user);
$output = substr($contact['id'], 3);
$logstring = date('Y-m-d H:i:s').' обновлен Контакт с id '.$output.PHP_EOL;
file_put_contents('logs/CreateContact.log', $logstring, FILE_APPEND);
} catch (WebServiceException $ex) {
$logstring = date('Y-m-d H:i:s').' '.$ex->getMessage().PHP_EOL;
file_put_contents('logs/CreateContact.log', $logstring, FILE_APPEND);
}
} else {
$params['assigned_user_id'] = vtws_getWebserviceEntityId('Users', $current_user->id);
$logstring = date('Y-m-d H:i:s').' Массив для создания Контакта: '.json_encode($params).PHP_EOL;
file_put_contents('logs/CreateContact.log', $logstring, FILE_APPEND);
try {
$contact = vtws_create('Contacts', $params, $current_user);
$output = substr($contact['id'], 3);
$logstring = date('Y-m-d H:i:s').' создан Контакт с id '.$output.PHP_EOL;
file_put_contents('logs/CreateContact.log', $logstring, FILE_APPEND);
} catch (WebServiceException $ex) {
$logstring = date('Y-m-d H:i:s').' '.$ex->getMessage().PHP_EOL;
file_put_contents('logs/CreateContact.log', $logstring, FILE_APPEND);
}
}
return $output;
}