<?
//$_SERVER["DOCUMENT_ROOT"] = __DIR__;
define('STOP_STATISTICS', true);
define('NO_KEEP_STATISTIC', 'Y');
define('NO_AGENT_STATISTIC', 'Y');
define('DisableEventsCheck', true);
define('BX_SECURITY_SHOW_MESSAGE', true);
define('NOT_CHECK_PERMISSIONS', true);

$_SERVER['DOCUMENT_ROOT'] = dirname(__DIR__, 1);
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php'); 
CModule::IncludeModule('iblock');

$users_res = CUser::GetList([ 'sort' => 'asc' ], 'desc', [], [ "SELECT" => [ "UF_*" ] ]);
while($user = $users_res->Fetch())
{
	print "user_id = ".$user['ID']."\n";

	if($user['ID'] != 1)
	{
		$user_properties = [
			'COMPANY' => $user['UF_ORG_TITLE'], 
			'INN' => $user['UF_INN'], 
			'KPP' => $user['UF_KPP'], 
			'OGRN' => $user['UF_UF_OGRN'], 
			'ADMINS' => [ $user['ID'] ], 
			'USERS' => [ $user['ID'] ], 
		];

		$ar_new_client = [
			'IBLOCK_ID' => IBLOCK_ID_CLIENTS, 
			'NAME' => $user['UF_ORG_TITLE'], 
			'CODE' => $user['XML_ID'], 
			'PROPERTY_VALUES' => $user_properties, 
			'ACTIVE' => 'Y', // активен
		];

		$new_client = new CIBlockElement;
		if($new_client_id = $new_client->Add($ar_new_client))
		{
			print "New ID: ".$new_client_id."\n\n";
		}
		else 
		{
			print "Error: ".$new_client->LAST_ERROR."\n\n";
			print "EXISTED USER: \n\n";

			$existed_client_res = CIBlockElement::GetList([ 'id' => 'desc' ], [ 'IBLOCK_ID' => IBLOCK_ID_CLIENTS, 'CODE' => $user['XML_ID'] ], false, []);
			while ($existed_client_element = $existed_client_res->GetNextElement())
			{
				$existed_client_record = $existed_client_element->GetFields();
				$existed_client_record['PROPERTIES'] = $existed_client_element->GetProperties();

				$admins = $existed_client_record['PROPERTIES']['ADMINS']['VALUE'];
				$users = $existed_client_record['PROPERTIES']['USERS']['VALUE'];

				array_push($admins, $user['ID']);
				array_push($users, $user['ID']);

				$admins = array_unique($admins);
				$users = array_unique($users);

				$updated_fields = [
					'PROPERTY_VALUES' => [
						'ADMINS' => $admins, 
						'USERS' => $users, 
					]
				];

				CIBlockElement::SetPropertyValuesEx($existed_client_record['ID'], IBLOCK_ID_CLIENTS, [ 'ADMINS' => $admins, 'USERS' => $users, ]);
				print $existed_client_record['CODE']." UPDATED\n\n";

				/*
					if($ai = array_search(20, $admins))
					{
						array_splice($admins, $ai, 1);
					}

					if($ai = array_search(20, $users))
					{
						array_splice($users, $ai, 1);
					}
				*/
				/*
				$updated_client = new CIBlockElement;
				if($updated_client_result = $updated_client->Update($existed_client_record['ID'], $updated_fields))
				{
					print "UPDATED\n\n";
				}
				else
				{
					print "UPDATE ERROR: ".$updated_client->LAST_ERROR."\n\n";
				}
				*/

				//print_r($existed_client_record);
			}
		}

		/*
		$rules_res = CIBlockElement::GetList([ 'id' => 'desc' ], [ 'PROPERTY_USER.' => '' ], false, $options);
		while ($rules_ob_element = $rules_res->GetNextElement())
		{
			$rules_ar_res = $rules_ob_element->GetFields();
			$rules_ar_res['PROPERTIES'] = $rules_ob_element->GetProperties();
		}
		*/
	}
}

die();

?>