Выбрать курс
0  /  8

Поиск компаний и контактов CRM 1С-Битрикс24 по e-mail, телефону или ИНН реквизиту. Запись реквизитов компаний или контактов по ИНН

1. Поиск компаний и контактов CRM.

Class CScoderTools
{	

	public static function __check_crm($query, $type = "inn", $param = "SHORT")
    {
		$result = false;
		
		if ($query!="")
		{
			switch ($type) {
				case "phone":
				case "email":
					
					$query = trim($query);
					$ar_query = array($query);
					if ($type == "email" && check_email($query))
					{
						//$query = trim($query);	//todo
						
					}
					elseif ($type == "phone" && $query!="")
					{
						$num_query = preg_replace('/[^0-9]/', '', $query);
						$ar_query[] = "+" . $num_query;
						$ar_query[] = $num_query;
						
						if ($query[0] == 8)
						{
							$query[0] = 7;
							$ar_query[] = $query;
							$ar_query[] = "+" . $query;
							$num_query = preg_replace('/[^0-9]/', '', $query);
							$ar_query[] = "+" . $num_query;
							$ar_query[] = $num_query;
							
						}						
					}
					
					if ($query != "")
					{
						$bcompany = $bcontact = false;
						$companies = $contacts = array();
						foreach ($ar_query as $search_query)
						{
							$filter = array(
								'ENTITY_ID' => array('CONTACT','COMPANY'), 
								'TYPE_ID' => ToUpper($type),
								'VALUE' => $search_query,
							);
							$dbFieldMulti = CCrmFieldMulti::GetListEx(
								array("ID" => "DESC"),
								$filter,
								false,
								false	//array("nTopCount" => 10)
							);
							 
							while ($arFieldMulti = $dbFieldMulti->Fetch()) 
							{
								if ($arFieldMulti["ENTITY_ID"] == "COMPANY"
									&& !in_array($arFieldMulti["ELEMENT_ID"],$companies)
								)
								{
									$bcompany = true;
									$data["COMPANY"][] = $arFieldMulti;
									$companies[] = $arFieldMulti["ELEMENT_ID"];
								}
								elseif ($arFieldMulti["ENTITY_ID"] == "CONTACT"
									&& !in_array($arFieldMulti["ELEMENT_ID"],$contacts)
								)
								{
									$bcontact = true;
									$data["CONTACT"][] = $arFieldMulti;
									$contacts[] = $arFieldMulti["ELEMENT_ID"];
								}
							}
						}
						$companies = $contacts = array();
						
						//если нашли компании
						if ($bcompany)
						{
							foreach ($data["COMPANY"] as $multi)
							{
								//поиск по ИД по компаниям
								$filter = array(
								   "ID" => $multi["ELEMENT_ID"],
								);
								$company = self::__get_data($filter, $param, "COMPANY");
								
								if (!in_array($company["ID"],$companies))
								{
									$result["COMPANY"][] = $company;
									$companies[] = $company["ID"];
								}
							}
						}
						if ($bcontact)	//если контакт
						{
							foreach ($data["CONTACT"] as $multi)
							{
								$filter = array(
									"ID" => $multi["ELEMENT_ID"],
								);
								$filter["CHECK_PERMISSIONS"] = "N";
		
								$rsItems =  CCrmContact::GetListEx(
									array("ID" => "DESC"),
									$filter,
									false,
									array("nTopCount" => 1),
									array("ID","COMPANY_ID")
								);
								
								if ($item = $rsItems->Fetch()) 
								{
									if ($item["COMPANY_ID"]>0)
									{
										$filter = array(
										   "ID" => $item["COMPANY_ID"],
										);
										$company = self::__get_data($filter, $param, "COMPANY");
										
										if (!in_array($company["ID"],$companies))
										{
											$result["COMPANY"][] = $company;
											$companies[] = $company["ID"];
										}
									}
									else
									{
										$filter = array(
										   "ID" => $item["ID"],
										);
										
										$contact = self::__get_data($filter, $param, "CONTACT");
										
										if (!in_array($company["ID"],$contacts))
										{
											$result["CONTACT"][] = $contact;
											$contacts[] = $contact["ID"];
										}
									}
								}
							}
						}
						
					}
					break;
				default:	//поиск по ИНН
					$query = preg_replace('/[^0-9]/', '', $query);
					if (strlen($query)==12 || strlen($query)==10)
					{
						//поиск по ИНН по реквизитам
						$filter = array(
							"RQ_INN" => $query,
							"ENTITY_TYPE_ID"=>CCrmOwnerType::Company,
							//'PRESET_ID'=>1
						);
						
						$req= new \Bitrix\Crm\EntityRequisite(false);
						$dbRes = $req->getList(
							array(
								"filter"=>$filter,
								"limit" => 1,
							)
						);
						if ($requisite = $dbRes->fetch())
						{
							//поиск по ИД по компаниям
							$filter = array(
							   "ID" => $requisite["ENTITY_ID"],
							);
							
							if ($company = self::__get_data($filter, $param, "COMPANY"))
							{
								
								if (!in_array($company["ID"],$companies))
								{
									$company["REQUISITES"] = $requisite;
									$result["COMPANY"][] = $company;
									$companies[] = $company["ID"];
								}
							}
							
						}
					}
			}
		}
		
		if ($param != "FULL"
			&& $result !== false
		)
		{
			$result = true;
		}
		return $result;
	}
	//вспомогательная функция: возвращает информацию по компании
	function __get_data($filter = array(), $param = "SHORT", $object_type = "COMPANY")
	{
		$result = false;
		$filter["CHECK_PERMISSIONS"] = "N";
		
		if ($object_type == "CONTACT")
		{
			$rsItems =  CCrmContact::GetListEx(
				array("ID" => "DESC"),
				$filter,
				false,
				array("nTopCount" => 1),
				array("ID","LAST_NAME","NAME","SECOND_NAME","ASSIGNED_BY","ASSIGNED_BY_LAST_NAME","ASSIGNED_BY_NAME","ASSIGNED_BY_SECOND_NAME","ASSIGNED_BY_LOGIN","DATE_CREATE")
			);
		}
		else
		{
			$rsItems =  CCrmCompany::GetListEx(
				array("ID" => "DESC"),
				$filter,
				false,
				array("nTopCount" => 1),
				array("ID","TITLE","ASSIGNED_BY","ASSIGNED_BY_LAST_NAME","ASSIGNED_BY_NAME","ASSIGNED_BY_SECOND_NAME","ASSIGNED_BY_LOGIN","DATE_CREATE")
			);
		}
		
		if ($item = $rsItems->Fetch()) 
		{
			$item["ASSIGNED_FULL_NAME"] = trim($item["ASSIGNED_BY_LAST_NAME"] . " " . $item["ASSIGNED_BY_NAME"] . " " . $item["ASSIGNED_BY_SECOND_NAME"]);
			if ($item["ASSIGNED_FULL_NAME"]=="")
				$item["ASSIGNED_FULL_NAME"] = $item["ASSIGNED_BY_LOGIN"];
			
			if (!isset($item["TITLE"]))
				$item["TITLE"] = trim($item["LAST_NAME"] . " " . $item["NAME"] . " " . $item["SECOND_NAME"]);
			
			if ($param == "FULL")
			{
				$ar = array();
				if ($object_type == "CONTACT")
				{
					$ar[] = array(
						'OWNER_TYPE_ID' => CCrmOwnerType::Contact, 
						'OWNER_ID' => $item["ID"],
					);
				}
				else
				{
					$ar[] = array(
						'OWNER_TYPE_ID' => CCrmOwnerType::Company, 
						'OWNER_ID' => $item["ID"],
					);
				}
				
				//смотрим все контакты компании
				$filter = array(
					"COMPANY_ID" => $item["ID"],
				);
				$filter["CHECK_PERMISSIONS"] = "N";
				
				$rsContacts =  CCrmContact::GetListEx(
					array("ID" => "DESC"),
					$filter,
					false,
					false,
					array("ID")
				);
				
				while ($contact = $rsContacts->Fetch()) 
				{
					$ar[] = array(
						'OWNER_TYPE_ID' => self::OWNER_TYPE_ID_CONTACT, 
						'OWNER_ID' => $contact["ID"],
					);
				}
				//смотрим последнее дело компаний и связанных контактов
				$filter = array(
					'CHECK_PERMISSIONS' => 'N'
				);
				
				$filter['BINDINGS'] = $ar;
				
				$dbRes = CCrmActivity::GetList(
					array("ID" => "DESC"), 
					$filter, 
					false, 
					array("nTopCount" => 1), 
					Array("ID","CREATED","OWNER_ID","OWNER_TYPE_ID")
				);
				if ($activity = $dbRes->Fetch()) 
				{
					$item["ACTIVITY_LAST"] = $activity;
				}
				
				//смотрим все сделки компании
				$filter = array(
					"COMPANY_ID" => $item["ID"],
				);
				$filter["CHECK_PERMISSIONS"] = "N";
				$filter["STAGE_SEMANTIC_ID"] = array(Bitrix\Crm\PhaseSemantics::PROCESS);		//Группа стадий "Сделка в работе"
				//Bitrix\Crm\PhaseSemantics::SUCCESS Сделка заключена
				//Bitrix\Crm\PhaseSemantics::FAILURE Сделка провалена
				
				$rsDeals =  CCrmDeal::GetListEx(
					array("ID" => "DESC"),
					$filter,
					false,
					false,
					array("ID","TITLE","DATE_CREATE","STAGE_ID","CATEGORY_ID")
				);
				
				while ($deal = $rsDeals->Fetch()) 
				{
					$item["DEALS"][] = $deal;
				}
			}
			
			$result = $item;
			$result["OBJECT_TYPE"] = $object_type;
		}
		
		return $result;
	}
}

2. Сохранение реквизитов компании или контакта по ИНН и ИД объекта.

Class CScoderTools
{	
	function __sc_set_requisites($object_id = 0, $query = 0, $object_type = "COMPANY")
	{
		$btrue = false;
		
		if ($query > 0 
			&& $object_id >0 
				&& \Bitrix\Main\Loader::includeModule("crm") 
					&& \Bitrix\Main\Loader::includeModule('socialservices')
					)
		{
			//по ИНН получим реквизиты
			$client = new \Bitrix\socialservices\properties\Client;
			$arRequisite = $client->getByInn($query);
			
			if (is_array($arRequisite))
			{
				switch ($object_type) {
					case "CONTACT":
						$entity_type_id = CCrmOwnerType::Contact;
						break;
					default:
						$entity_type_id = CCrmOwnerType::Company;
						
				}
				//добавим реквизиты 
				$entityRequisite = new \Bitrix\Crm\EntityRequisite;
				if(strlen($query)==10)	//юр лицо
				{
					$load = array(
						'ENTITY_ID' => $object_id,
						"ENTITY_TYPE_ID" => $entity_type_id,
						'PRESET_ID' => 1,	//организация                              
						'NAME' => $arRequisite['NAME_SHORT'],
						'SORT' => 500,
						'ACTIVE' => 'Y',
						'RQ_COMPANY_NAME' => $arRequisite['NAME_SHORT'],
						'RQ_COMPANY_FULL_NAME' => $arRequisite['NAME'],                              
						'RQ_INN' => $arRequisite["INN"], //инн
						'RQ_KPP' => $arRequisite["KPP"],  //кпп
						'RQ_OGRN' => $arRequisite["OGRN"], //огрн
						'RQ_COMPANY_REG_DATE' => $arRequisite["CREATION_REGISTRATION_DATE"],
						'RQ_OKVED' => $arRequisite["OKVED_CODE"],                              
					);

					if(isset($arRequisite["OFFICIALS"][0]["LAST_NAME"]))
					{
						$load["RQ_DIRECTOR"] = $arRequisite["OFFICIALS"][0]["LAST_NAME"]." ".$arRequisite["OFFICIALS"][0]["NAME"]." ".$arRequisite["OFFICIALS"][0]["SECOND_NAME"];
					}
				}
				else	//ИП
				{
					$load = array(
						'ENTITY_ID' => $object_id,
						"ENTITY_TYPE_ID" => $entity_type_id,
						'PRESET_ID' => 2,	//ИП                              
						'NAME' => $arRequisite["TYPE_NAME"]." ".$arRequisite["LAST_NAME"]." ".$arRequisite["NAME"]." ".$arRequisite["SECOND_NAME"],
						'SORT' => 500,
						'ACTIVE' => 'Y',
						'RQ_LAST_NAME' => $arRequisite['LAST_NAME'],
						'RQ_FIRST_NAME' => $arRequisite["NAME"],
						'RQ_SECOND_NAME' => $arRequisite["SECOND_NAME"],                              
						'RQ_COMPANY_FULL_NAME' => $arRequisite["TYPE_NAME"]." ".$arRequisite["LAST_NAME"]." ".$arRequisite["NAME"]." ".$arRequisite["SECOND_NAME"],                              
						'RQ_INN' => $arRequisite["INN"], //инн                              
						'RQ_OGRNIP' => $arRequisite["OGRNIP"], //огрн
						'RQ_OKVED' => $arRequisite["OKVED_CODE"],                           
					);
				}
				$bank   = new \Bitrix\Crm\EntityBankDetail();
				$bank_load = array(
					"RQ_BANK_NAME" => $arRequisite["RQ_BANK_NAME"],
					"RQ_BIK" => $arRequisite["RQ_BIK"],
					"RQ_ACC_NUM" => $arRequisite["RQ_ACC_NUM"],
					"RQ_COR_ACC_NUM" => $arRequisite["RQ_COR_ACC_NUM"],
					"RQ_BANK_ADDR" => $arRequisite["RQ_BANK_ADDR"],
				);
				$address = new \Bitrix\Crm\EntityAddress();
				$address_load = array(                                                            
					"ADDRESS_1" =>$arRequisite["ADDRESS_STREET_NAME"]." ".$arRequisite["ADDRESS_STREET_TYPE"]." ".$arRequisite["ADDRESS_HOUSE"]." ".$arRequisite["ADDRESS_BUILDING"],
					"ADDRESS_2" => $arRequisite["ADDRESS_FLAT"],
					"PROVINCE" => $arRequisite["ADDRESS_REGION_NAME"],
					"CITY" => $arRequisite["ADDRESS_CITY_NAME"],
					"POSTAL_CODE" => $arRequisite["ADDRESS_INDEX"],
					"COUNTRY" => "РФ"
				);
				//поиск по ИНН по реквизитам
				$filter = array(
					"RQ_INN" => $query,
					"ENTITY_TYPE_ID"=> $entity_type_id,
					//'PRESET_ID'=>1
				);
				$req = new \Bitrix\Crm\EntityRequisite(false);
				$dbRes = $req->getList(
					array(
						"filter"=>$filter,
						"limit" => 1,
					)
				);
				if ($requisite = $dbRes->fetch())
				{
					//$res = $entityRequisite->Update(intval($requisite['ID']),$load);		//todo если надо
					if ($bank_load["RQ_BIK"] > 0)
					{
						if ($arRequisite["ADDRESS_STREET_NAME"] != ""
							&& $arRequisite["ADDRESS_REGION_NAME"] != "")
						{
							//адрес добавляем
							//$address->deleteByEntity(2, $requisite['ID']);
							//$address->deleteByEntity(6, $requisite['ID']);
							//$address->register(8, $res->getId(), 6, $address_load);	//todo если надо
						}
						
						$dbRes  = $bank->getList(array(
							'filter' => array('ENTITY_ID' => $requisite['ID'])
						));
						$arBankOld =  $dbRes->Fetch();
						if($arBankOld["ID"] > 0)
						{
							//$bank->Update(intval($arBankOld["ID"]),$bank_load);		//todo если надо
						}
						else
						{
							//$bank->Add($bank_load);		//todo если надо
						}
					}
					$btrue = true;
				}
				else
				{
					$res = $entityRequisite->add($load);
					
					if($res->getId() > 0)
					{
						$btrue = true;
						if ($arRequisite["ADDRESS_STREET_NAME"] != ""
							&& $arRequisite["ADDRESS_REGION_NAME"] != "")
						{
							//адрес добавляем
							$address->register(8, $res->getId(), 6, $address_load);
						}
						if ($bank_load["RQ_BIK"] > 0)
						{
							$bank->Add($bank_load);
						}
					}
				}
			}
		}
		return $btrue;
	}
}