Files
crm.clientright.ru/modules/Contacts/tasks/GetCourt.php

98 lines
3.9 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
function GetCourt($ws_entity) {
require_once 'include/Webservices/Utils.php';
include_once 'modules/Users/Users.php';
require_once 'include/Webservices/Revise.php';
require_once 'includes/Loader.php';
vimport ('includes.runtime.Globals');
vimport ('includes.runtime.BaseModel');
vimport ('includes.runtime.LanguageHandler');
global $adb;
$user = Users::getActiveAdminUser(); // Получаем пользователя, под которым будем создавать записи
//$logstring = date('Y-m-d H:i:s').' - вошли в кастомный обработчик'.PHP_EOL;
//file_put_contents('wfGetCourt.log', $logstring, FILE_APPEND);
// WS id
$ws_id = $ws_entity->getId();
$crmid = vtws_getCRMEntityId($ws_id);
//$logstring = date('Y-m-d H:i:s').' - получили ID проекта: '.$crmid.PHP_EOL;
//file_put_contents('wfGetCourt.log', $logstring, FILE_APPEND);
$query = 'select a.mailingstreet
from vtiger_contactaddress a
left join vtiger_crmentity e on e.crmid = a.contactaddressid
where e.deleted = 0 and a.contactaddressid = '.$crmid;
$result = $adb->pquery($query);
if ($adb->num_rows($result) == 0) {
$logstring = date('Y-m-d H:i:s').' - очень странно, но Контакта с там ID нет...'.PHP_EOL;
file_put_contents('wfGetCourt.log', $logstring, FILE_APPEND);
} else {
$address = $adb->query_result($result, 0, "mailingstreet");
if (empty($address)) {
$logstring = date('Y-m-d H:i:s').' - Найден Контакт: '.$crmid.'; но в нем не указан адрес - видимо удалили'.PHP_EOL;
file_put_contents('wfGetCourt.log', $logstring, FILE_APPEND);
} else {
$enddate = $startdate;
$endtime= date('H:i:s', strtotime($starttime) + 3600);
$logstring = date('Y-m-d H:i:s').' - Найден Контакт: '.$crmid.'; адрес: '.$address.PHP_EOL;
file_put_contents('wfGetCourt.log', $logstring, FILE_APPEND);
$host = "https://api.xn----7sbarabva2auedgdkhac2adbeqt1tna3e.xn--p1ai/api"; // адрес сервера api
$token = "y4kNd3Li2NDxyOgaG37ZDuGTWg5CfF2A2dERwbFUjas"; // указать полученный токен
// запрос по адресу
$url = $host."?token=".$token."&address=".urlencode($address);
// запрос по координатам
//$coords = "49.099524 55.794340"; // координаты точки, разделитель - пробел
//$url = $host . "?token=" . $token . "&coords=" . urlencode($coords);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
curl_close($curl);
$logstring = date('Y-m-d H:i:s').' - Ответ от API-сервера: '.$data.PHP_EOL;
file_put_contents('wfGetCourt.log', $logstring, FILE_APPEND);
$court = json_decode($data, true);
$court = $court['request']['court_fs'];
$logstring = date('Y-m-d H:i:s').' - Даные суда: '.json_encode($court).PHP_EOL;
file_put_contents('wfGetCourt.log', $logstring, FILE_APPEND);
preg_match('/\((.*?)\)/', $court['title'], $matches);
$region = $matches[1];
try {
$params = array (
'id' => $ws_id,
'cf_1782' => $court['title'],
'cf_1784' => $court['address'],
'cf_1851' => $court['site'],
'cf_1857' => $court['email'],
'cf_1853' => $court['tel'],
'cf_1992' => $court['code'],
'mailingstate' => $region
);
$contact = vtws_revise($params, $user);
$logstring = date("Y-m-d H:i:s").' Контакт обновлен: '.$contact['id'];
file_put_contents('wfGetCourt.log', $logstring.PHP_EOL, FILE_APPEND);
$contactid = substr($contact['id'], 3);
} catch (WebServiceException $ex) {
$logstring = date('Y-m-d H:i:s').' '.$ex->getMessage().PHP_EOL;
file_put_contents('wfGetCourt.log', $logstring, FILE_APPEND);
}
}
}
return;
}