<?
@set_time_limit(0);
@ignore_user_abort(true);
$path_parts = pathinfo($_SERVER['SCRIPT_FILENAME']);
chdir($path_parts['dirname']);
require_once (__DIR__.'/include/crest.php');

$_REQUEST['DATE TIME'] = date('d.m.Y H:i:s');

file_put_contents(
	$path_parts['dirname']."/onCompanyAdd_all.log", 
	print_r($_REQUEST, true), 
	FILE_APPEND
);

if($_REQUEST['event'] == 'ONCRMCOMPANYADD' && $_REQUEST['data']['FIELDS']['ID'] !== ''){
	$company_id = $_REQUEST['data']['FIELDS']['ID'];
	$arNewCompany_data = CRest::call("crm.company.get", array(
		"id" => $company_id
	));
	$arTests['arNewCompany_data'] = $arNewCompany_data;
	if($arNewCompany_data['result']){
		$company = $arNewCompany_data['result'];
		$arTests['company'] = $company;

		$arListCompanyAddress_data = CRest::call("crm.address.list", array(
			"order" => ["TYPE_ID" => "ASC"],
			"filter" => [
				"ENTITY_TYPE_ID" => 4,
				"ENTITY_ID" => $company_id,
				"TYPE_ID" => 1
			],
			"select" => ["ENTITY_TYPE_ID", "ENTITY_ID", "TYPE_ID", "ADDRESS_1", "ADDRESS_2", 'CITY', 'POSTAL_CODE', 'PROVINCE', 'COUNTRY'],
		));

		$arTests['$arListCompanyAddress_data'] = $arListCompanyAddress_data;
		if(count($arListCompanyAddress_data['result']) == 0) {
			$new_address = [];
			if($company['ADDRESS'] && trim($company['ADDRESS']) == ''){
				$new_address['ADDRESS_1'] = $company['UF_CRM_COMPANY_1657534985446'];
			}
			if($company['ADDRESS_2'] && trim($company['ADDRESS_2']) == ''){
				$new_address['ADDRESS_2'] = $company['UF_CRM_COMPANY_1657535002882'];
			}
			if($company['ADDRESS_CITY'] && trim($company['ADDRESS_CITY']) == ''){
				$new_address['CITY'] = $company['UF_CRM_COMPANY_1657534926084'];
			}
			if($company['ADDRESS_POSTAL_CODE'] && trim($company['ADDRESS_POSTAL_CODE']) == ''){
				$new_address['POSTAL_CODE'] = $company['UF_CRM_1660281609'];
			}
			if($company['ADDRESS_PROVINCE'] && trim($company['ADDRESS_PROVINCE']) == ''){
				$new_address['PROVINCE'] = $company['UF_CRM_COMPANY_1657534877352'];
			}
			if($company['ADDRESS_COUNTRY'] && trim($company['ADDRESS_COUNTRY']) == ''){
				$new_address['COUNTRY'] = $company['UF_CRM_COMPANY_1657288466255'];
			}

			if(count($new_address)>0){
				$new_address["TYPE_ID"] = 1;
				$new_address["ENTITY_TYPE_ID"] = 4;
				$new_address["ENTITY_ID"] = $company_id;
				sleep(2);
				$arNewCompanyAddress_data = CRest::call("crm.address.add", array(
					"fields" => $new_address
				));
				$arTests['arNewCompanyAddress_data'] = $arNewCompanyAddress_data;
			}
		}else{
			$new_address = [];

			if(trim($arListCompanyAddress_data['result'][0]['ADDRESS_1']) == ''){
				$new_address['ADDRESS_1'] = $company['UF_CRM_COMPANY_1657534985446'];
			}
			if(trim($arListCompanyAddress_data['result'][0]['ADDRESS_2']) == ''){
				$new_address['ADDRESS_2'] = $company['UF_CRM_COMPANY_1657535002882'];
			}
			if(trim($arListCompanyAddress_data['result'][0]['CITY']) == ''){
				$new_address['CITY'] = $company['UF_CRM_COMPANY_1657534926084'];
			}
			if(trim($arListCompanyAddress_data['result'][0]['POSTAL_CODE']) == ''){
				$new_address['POSTAL_CODE'] = $company['UF_CRM_1660281609'];
			}
			if(trim($arListCompanyAddress_data['result'][0]['PROVINCE']) == ''){
				$new_address['PROVINCE'] = $company['UF_CRM_COMPANY_1657534877352'];
			}
			if(trim($arListCompanyAddress_data['result'][0]['COUNTRY']) == ''){
				$new_address['COUNTRY'] = $company['UF_CRM_COMPANY_1657288466255'];
			}

			if(count($new_address)>0){
				$new_address["TYPE_ID"] = '1';
				$new_address["ENTITY_TYPE_ID"] = $arListCompanyAddress_data['result'][0]['ENTITY_TYPE_ID'];
				$new_address["ENTITY_ID"] = $arListCompanyAddress_data['result'][0]['ENTITY_ID'];
				
				sleep(1);
				$arUpdateCompanyAddress_data = CRest::call("crm.address.update", array(
					"fields" => $new_address
				));
				$arTests['arUpdateCompanyAddress_data'] = $arUpdateCompanyAddress_data;
			}
		}
	}
	file_put_contents(
		$path_parts['dirname']."/onCompanyAdd.log", 
		print_r($arTests, true), 
		FILE_APPEND
	);
}
?>