CModule::IncludeModule("crm");
CModule::IncludeModule("nikaverro.gservice");
$gs = GameService::getInstance();
$lastId= 1140109;
$arResult = $gs->QueryFetch("select id,ls,fio,tel,org from clients where id<".$lastId." order by id desc limit 300;");
$arLs = [];
foreach ($arResult as $key => $arItem) {
$arLs[] = $arItem["ls"];
}
echo "<pre>";
if(!empty($arLs)){
$arFilter = array(
"UF_CID"=>$arLs, //выбираем определенную компанию по ls
"CHECK_PERMISSIONS"=>"N" //не проверять права доступа текущего пользователя
);
$arSelect = array(
"ID",
"UF_CID"
);
$res = CCrmCompany::GetList(Array(), $arFilter, $arSelect);
$arComps = [];
while($row = $res->Fetch()){
$arComps[$row["UF_CID"]] = $row["ID"];
}
$options = array('CURRENT_USER'=>1); //из под админа
$company = new \CCrmCompany( false );
$fieldMulti = new CCrmFieldMulti();
foreach ($arResult as $key => $arItem) {
$lastId = $arItem["id"];
if(isset($arComps[$arItem["ls"]])){
echo "найден";
print_r($arItem);
//break;
}
else{
//print_r($arItem);
$name = $arItem["ls"];
if(!empty($arItem["fio"])){
if(!empty($arItem["org"]) && ($arItem["fio"] != $arItem["org"])){//организация
$name .= " ".$arItem["org"];
}
else{ //частник
$name .= " ".$arItem["fio"];
}
}
//вар1
$arFields = [
"TITLE"=>$name,
"UF_CID" =>$arItem["ls"]
];
$companyId = $company->Add($arFields);
if(($companyId>0) && !empty($arItem["tel"])){
$arTels = explode(" ", $arItem["tel"]);
foreach($arTels as $tel){
//echo $tel;
$fieldMulti->Add([
'ELEMENT_ID' => $companyId,
'ENTITY_ID' => "COMPANY", //"CONTACT","LEAD","DEAL"
'TYPE_ID' => "PHONE", //"EMAIL",
'VALUE' => $tel,
'VALUE_TYPE' => 'WORK',
]);
}
}
/////
}
}
}
echo $lastId;
//print_r($arResult);
echo "</pre>"; |