evoleasing-public/local/ilsa_update.php
2022-11-30 08:07:39 +03:00

728 lines
21 KiB
PHP
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.

<?
//$_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');
$arrContextOptions = [
"ssl" => [
"verify_peer"=>false,
"verify_peer_name"=>false,
],
];
$bitrix_existed_brands = [];
$bitrix_existed_models = [];
$api_brands = [];
$api_models = [];
$bitrix_existed_modifications = [];
$bitrix_existed_cars = [];
$all_brands_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => IBLOCK_ID_BRANDS, "ACTIVE" => "Y" ], false, false, [] );
while($all_brands_search_ob = $all_brands_search_res->GetNextElement())
{
$brand_search = $all_brands_search_ob->GetFields();
$brand_search['PROPERTIES'] = $all_brands_search_ob->GetProperties();
array_push( $bitrix_existed_brands, $brand_search['PROPERTIES']['UID']['VALUE'] );
}
$all_models_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => IBLOCK_ID_MODELS, "ACTIVE" => "Y" ], false, false, [] );
while($all_models_search_ob = $all_models_search_res->GetNextElement())
{
$model_search = $all_models_search_ob->GetFields();
$model_search['PROPERTIES'] = $all_models_search_ob->GetProperties();
array_push( $bitrix_existed_models, $model_search['PROPERTIES']['UID']['VALUE'] );
}
$all_modifications_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => 10, "ACTIVE" => "Y" ], false, false, [] );
while($all_modifications_search_ob = $all_modifications_search_res->GetNextElement())
{
$modification_search = $all_modifications_search_ob->GetFields();
array_push( $bitrix_existed_modifications, $modification_search['CODE'] );
}
$all_cars_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => 1, ], false, false, [] );
while($all_cars_search_ob = $all_cars_search_res->GetNextElement())
{
$car_search = $all_cars_search_ob->GetFields();
$car_search['PROPERTIES'] = $all_cars_search_ob->GetProperties();
array_push( $bitrix_existed_cars, $car_search['PROPERTIES']['UID']['VALUE'] );
}
print "bitrix_existed_brands = ".count($bitrix_existed_brands)."\n";
print "bitrix_existed_models = ".count($bitrix_existed_models)."\n";
print "bitrix_existed_modifications = ".count($bitrix_existed_modifications)."\n";
print "bitrix_existed_cars = ".count($bitrix_existed_cars)."\n";
$api_modifications_url = API_HOST."/site/Vehicle/GetModifications";
$content = file_get_contents($api_modifications_url, false, stream_context_create($arrContextOptions));
print $api_modifications_url."\n\n";
file_put_contents("./api_modifications.json", $content);
$json = json_decode($content, true);
$brands_and_models = [];
//print "---------------\n\n";
//print_r();
//print "---------------\n\n";
$c = 0;
$api_data_titles = [];
$api_data = [];
$api_brands = [];
$api_models = [];
$api_modifications = [];
$api_cars = [];
//prepare brands list from API
foreach($json AS $entry)
{
if(!isset($api_data[ $entry['brand']['id'] ]))
{
$api_data[ $entry['brand']['id'] ] = [
"models" => []
];
}
if(!isset($api_data_titles[ $entry['brand']['name'] ]))
{
$api_data_titles[ $entry['brand']['name'] ] = [
"models" => []
];
}
if(!in_array($entry['brand']['id'], $api_brands))
{
array_push($api_brands, $entry['brand']['id']);
}
}
//prepare models list from API
foreach($json AS $entry)
{
if(!isset($api_data[ $entry['brand']['id'] ]['models'][ $entry['model']['id'] ]))
{
$api_data[ $entry['brand']['id'] ]['models'][ $entry['model']['id'] ] = [
"modifications" => []
];
}
if(!isset($api_data_titles[ $entry['brand']['name'] ]['models'][ $entry['model']['name'] ]))
{
$api_data_titles[ $entry['brand']['name'] ]['models'][ $entry['model']['name'] ] = [
"modifications" => []
];
}
if(!in_array($entry['model']['id'], $api_models))
{
array_push($api_models, $entry['model']['id']);
}
}
//prepare modifications list from API
foreach($json AS $entry)
{
if(!isset($api_data[ $entry['brand']['id'] ]['models'][ $entry['model']['id'] ]['modifications'][ $entry['modification'] ]))
{
$api_data[ $entry['brand']['id'] ]['models'][ $entry['model']['id'] ]['modifications'][ $entry['modification'] ] = [
"cars" => []
];
}
if(!isset($api_data_titles[ $entry['brand']['name'] ]['models'][ $entry['model']['name'] ]['modifications'][ $entry['modification'] ]))
{
$api_data_titles[ $entry['brand']['name'] ]['models'][ $entry['model']['name'] ]['modifications'][ $entry['modification'] ] = [
"cars" => []
];
}
if(!in_array($entry['modification'], $api_modifications))
{
array_push($api_modifications, $entry['modification']);
}
}
//prepare cars list from API
foreach($json AS $entry)
{
if(!isset($api_data[ $entry['brand']['id'] ]['models'][ $entry['model']['id'] ]['modifications'][ $entry['modification'] ]['cars'][ $entry['uid'] ]))
{
array_push($api_data[ $entry['brand']['id'] ]['models'][ $entry['model']['id'] ]['modifications'][ $entry['modification'] ]['cars'], $entry['uid']);
}
if(!isset($api_data_titles[ $entry['brand']['name'] ]['models'][ $entry['model']['name'] ]['modifications'][ $entry['modification'] ]['cars'][ $entry['uid'] ]))
{
array_push($api_data_titles[ $entry['brand']['name'] ]['models'][ $entry['model']['name'] ]['modifications'][ $entry['modification'] ]['cars'], $entry['uid']);
}
if(!in_array($entry['uid'], $api_cars))
{
array_push($api_cars, $entry['uid']);
}
}
ksort($api_data_titles);
foreach($api_data_titles AS $k => $v)
{
$models = $v['models'];
ksort($models);
foreach($models AS $kk => $vv)
{
$modifications = $vv['modifications'];
ksort($modifications);
$models[$kk]['modifications'] = $modifications;
}
$api_data_titles[$k]['models'] = $models;
}
file_put_contents("./api_data_titles.json", json_encode($api_data_titles));
//print_r($bitrix_existed_models);
foreach($bitrix_existed_models AS $existed_model)
{
//print $existed_model." ?\n";
if(!in_array($existed_model, $api_models))
{
print "HIDE MODEL ".$existed_model."\n";
}
}
////print_r($api_data);
////die();
foreach($json AS $entry)
{
if(!isset($brands_and_models[ $entry['brand']['id'] ]))
{
$brands_and_models[ $entry['brand']['id'] ] = [
"name" => $entry['brand']['name'],
"models" => [
$entry['model']['id'] => [
"name" => $entry['model']['name'],
"modifications" => [ $entry['modification'] ],
]
],
];
}
else
{
if(!isset($brands_and_models[ $entry['brand']['id'] ][ 'models' ][ $entry['model']['id'] ]))
{
$brands_and_models[ $entry['brand']['id'] ][ 'models' ][ $entry['model']['id'] ] = [
"name" => $entry['model']['name'],
"modifications" => [ $entry['modification'] ],
];
}
if(!in_array($entry['modification'], $brands_and_models[ $entry['brand']['id'] ][ 'models' ][ $entry['model']['id'] ][ 'modifications' ]))
{
array_push($brands_and_models[ $entry['brand']['id'] ][ 'models' ][ $entry['model']['id'] ][ 'modifications' ], $entry['modification']);
}
}
if($c === 0)
{
print_r($entry);
}
$c++;
}
//print_r($brands_and_models);
//die();
$c = 0;
$bitrix_brands = [];
$bitrix_models = [];
$bitrix_modifications = [];
$bitrix_modifications_codes = [];
foreach($brands_and_models AS $brand_uid => $brand)
{
//print $uid." - ".$brand['name']."\n";
//if($c < 2)
//{
//добавляем новую марку
$brand_existed = null;
$brand_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => IBLOCK_ID_BRANDS, "PROPERTY_UID" => $brand_uid ], false, ["nPageSize" => 1], [] );
while($brand_search_ob = $brand_search_res->GetNextElement())
{
$brand_existed = $brand_search_ob->GetFields();
$brand_existed['PROPERTIES'] = $brand_search_ob->GetProperties();
$bitrix_brands[ $brand_uid ] = $brand_existed['ID'];
}
$code = CUtil::translit( $brand['name'], "ru", [ "max_len" => 255, "change_case" => "L", "replace_space" => "-", "replace_other" => "-", ] );
if($brand_existed !== null)
{
$brand_update_payload = [
"ACTIVE" => "Y",
"CODE" => $code,
"NAME" => $brand['name'],
];
$update_element = new CIBlockElement;
$update_result = $update_element->Update($brand_existed['ID'], $brand_update_payload);
$result = models_check($bitrix_models, $bitrix_modifications, $bitrix_modifications_codes, $brand_uid, $brand);
$bitrix_models = $result['bitrix_models'];
$bitrix_modifications = $result['bitrix_modifications'];
$bitrix_modifications_codes = $result['bitrix_modifications_codes'];
}
else
{
$brand_element = new CIBlockElement;
$brand_payload = [
"IBLOCK_SECTION_ID" => false,
"IBLOCK_ID" => IBLOCK_ID_BRANDS,
"PROPERTY_VALUES" => [
"UID" => $brand_uid,
"TRANSCRIPTION" => $brand['name'],
],
"NAME" => $brand['name'],
"CODE" => $code,
"ACTIVE" => "Y",
];
if($new_brand_id = $brand_element->Add($brand_payload))
{
$bitrix_brands[$brand_uid] = $new_brand_id;
}
else
{
echo "brand error: ".$brand_element->LAST_ERROR;
}
$result = models_check($bitrix_models, $bitrix_modifications, $bitrix_modifications_codes, $brand_uid, $brand);
$bitrix_models = $result['bitrix_models'];
$bitrix_modifications = $result['bitrix_modifications'];
$bitrix_modifications_codes = $result['bitrix_modifications_codes'];
}
//print "\n------------------------------\n";
//}
$c++;
}
function set_brand_activity($brand_uid, $active = "N")
{
$brand_id = null;
$brand_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => IBLOCK_ID_BRANDS, "PROPERTY_UID" => $brand_uid ], false, false, [] );
while($brand_search_ob = $brand_search_res->GetNextElement())
{
$brand_search = $brand_search_ob->GetFields();
$brand_id = $brand_search['ID'];
}
if($brand_id !== null)
{
$update_element = new CIBlockElement;
$update_result = $update_element->Update($brand_id, [ "ACTIVE" => $active, ]);
}
}
function set_model_activity($model_uid, $active = "N")
{
$model_id = null;
$model_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => IBLOCK_ID_MODELS, "PROPERTY_UID" => $model_uid ], false, false, [] );
while($model_search_ob = $model_search_res->GetNextElement())
{
$model_search = $model_search_ob->GetFields();
$model_id = $model_search['ID'];
}
if($model_id !== null)
{
$update_element = new CIBlockElement;
$update_result = $update_element->Update($model_id, [ "ACTIVE" => $active, ]);
}
}
function set_modification_activity($modification_code, $active = "N")
{
$modification_id = null;
$modification_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => 10, "CODE" => $modification_code ], false, false, [] );
while($modification_search_ob = $modification_search_res->GetNextElement())
{
$modification_search = $modification_search_ob->GetFields();
$modification_id = $modification_search['ID'];
}
if($modification_id !== null)
{
$update_element = new CIBlockElement;
$update_result = $update_element->Update($modification_id, [ "ACTIVE" => $active, ]);
}
}
function set_car_activity($car_uid, $active = "N")
{
//print "SET ACTIVE ".$car_uid." | ".$active."\n";
$car_id = null;
$car_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => IBLOCK_ID_CATALOG_CARS_NEW, "PROPERTY_UID" => $car_uid ], false, false, [] );
while($car_search_ob = $car_search_res->GetNextElement())
{
$car_search = $car_search_ob->GetFields();
$car_id = $car_search['ID'];
}
if($car_id !== null)
{
$update_element = new CIBlockElement;
$update_result = $update_element->Update($car_id, [ "ACTIVE" => $active, ]);
}
}
function models_check($bitrix_models, $bitrix_modifications, $bitrix_modifications_codes, $brand_uid, $brand)
{
foreach($brand['models'] AS $model_uid => $model)
{
$model_name = $model['name'];
$model_existed = null;
$model_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => IBLOCK_ID_MODELS, "PROPERTY_UID" => $model_uid ], false, ["nPageSize" => 1], [] );
while($model_search_ob = $model_search_res->GetNextElement())
{
$model_existed = $model_search_ob->GetFields();
$model_existed['PROPERTIES'] = $model_search_ob->GetProperties();
$bitrix_models[$model_uid] = $model_existed['ID'];
}
$code = CUtil::translit( $model_name, "ru", [ "max_len" => 255, "change_case" => "L", "replace_space" => "-", "replace_other" => "-", ] );
if($model_existed !== null)
{
//print "MODEL ".$model_uid." EXISTED";
$model_update_payload = [
"ACTIVE" => "Y",
"NAME" => $model_name,
"CODE" => $code,
];
$update_element = new CIBlockElement;
$update_result = $update_element->Update($model_existed['ID'], $model_update_payload);
}
else
{
$model_element = new CIBlockElement;
$model_payload = [
"IBLOCK_SECTION_ID" => false,
"IBLOCK_ID" => IBLOCK_ID_MODELS,
"PROPERTY_VALUES" => [
"BRAND_UID" => $brand_uid,
"UID" => $model_uid,
],
"NAME" => $model_name,
"CODE" => $code,
"ACTIVE" => "Y",
];
if($new_model_id = $model_element->Add($model_payload))
{
$bitrix_models[$model_uid] = $new_model_id;
//echo "new_model_id: ".$new_model_id;
}
else
{
echo "model error: ".$model_element->LAST_ERROR;
}
}
foreach($model['modifications'] AS $modification)
{
print "MOD ".$modification."\n";
$modification_existed = null;
$NAME = $brand['name']." ".$model_name." ".$modification;
$CODE = CUtil::translit( $NAME, "ru", [ "max_len" => 255, "change_case" => "L", "replace_space" => "-", "replace_other" => "-", ] );
//"ID"
$modification_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => 10, "CODE" => $CODE ], false, ["nPageSize" => 1], [] );
while($modification_search_ob = $modification_search_res->GetNextElement())
{
$m_res = $modification_search_ob->GetFields();
if(isset($m_res['ID']))
{
$modification_existed = $m_res['ID'];
$bitrix_modifications[md5($brand_uid.$model_uid.$modification)] = $m_res['ID'];
array_push($bitrix_modifications_codes, $CODE);
}
}
if($modification_existed === null)
{
$modification_element = new CIBlockElement;
$modification_payload = [ "IBLOCK_SECTION_ID" => false, "IBLOCK_ID" => 10, "PROPERTY_VALUES" => [ "MODEL_UID" => $model_uid, "TITLE" => $modification ], "NAME" => $NAME, "CODE" => $CODE, "ACTIVE" => "Y", ];
if($new_modification_id = $modification_element->Add($modification_payload))
{
$bitrix_modifications[md5($brand_uid.$model_uid.$modification)] = $new_modification_id;
array_push($bitrix_modifications_codes, $CODE);
}
else
{
echo "modification error: ".$modification_element->LAST_ERROR;
}
}
}
}
return [
"bitrix_models" => $bitrix_models,
"bitrix_modifications" => $bitrix_modifications,
"bitrix_modifications_codes" => $bitrix_modifications_codes
];
}
print "bitrix brands count: ".count($bitrix_brands)."\n";
//print_r($bitrix_brands);
print "bitrix models count: ".count($bitrix_models)."\n";
//print_r($bitrix_models);
print "bitrix modifications count: ".count($bitrix_modifications)."\n";
//print_r($bitrix_modifications);
//кешируем новые UID ТС
$json_new_cars = [];
foreach($json AS $car)
{
array_push($json_new_cars, $car['uid']);
}
$car_codes = [];
$c = 0;
foreach($json AS $car)
{
if(!in_array($car['uid'], $bitrix_existed_cars))
{
$c++;
}
//обновляем список ТС в битриксе
$modification_content = file_get_contents(API_HOST."/site/Vehicle/GetCatalog?id=". $car['uid'] ."", false, stream_context_create($arrContextOptions));
$modification_json = json_decode($modification_content, true);
$name = "".$car['brand']['name']." ".$car['model']['name']." ".$car['modification']."";
$title = $name." ".$car['modificationYear']."";
$code = CUtil::translit( str_replace(["&quot;", " ", "+"], ['"', "-", "-plus-"], $title), "ru", [ "max_len" => 255, "change_case" => "L", "replace_space" => "-", "replace_other" => "-", "safe_chars" => "-" ] );
//print "new car --- uid ".$car['uid']." | code ".$code."\n";
$PROPERTY_VALUES = [
"UID" => $car['uid'],
"BRAND" => $bitrix_brands[ $car['brand']['id'] ],
"MODEL" => $bitrix_models[ $car['model']['id'] ],
"MODIFICATION" => $bitrix_modifications[ md5($car['brand']['id'].$car['model']['id'].$car['modification']) ],
"MODIFICATION_YEAR" => $car['modificationYear'],
"GEAR" => $car['gear'],
"DRIVE" => $car['drive'],
"BODY" => $car['body'],
"ENGINE_POWER" => $car['engine']['power'],
"ENGINE_VOLUME" => $car['engine']['volume'],
"ENGINE_FUEL" => $car['engine']['fuel'],
"PRICE" => $car['price'],
"IMAGE_URL" => $car['url'],
"COMPLECTATION_BODY" => json_encode($modification_json['body']),
"COMPLECTATION_INTERIOR" => json_encode($modification_json['interior']),
"COMPLECTATION_SECURITY" => json_encode($modification_json['security']),
"COMPLECTATION_GUARANTEE" => json_encode($modification_json['guarantee']),
"COMPLECTATION_TECHNICAL_DESCRIPTION" => json_encode($modification_json['technicalDescription']),
"COMPLECTATION_FUNCTIONAL_EQUIPMENT" => json_encode($modification_json['functionalEquipment']),
"COMPLECTATION_TOTAL_INFORMATION" => json_encode($modification_json['totalInformation']),
"LEASING_PROGRAMS" => $car['programms'],
];
//"ID"
$car_existed = null;
$car_search_res = CIBlockElement::GetList( [], [ "IBLOCK_ID" => IBLOCK_ID_CATALOG_CARS_NEW, "PROPERTY_UID" => $car['uid'] ], false, ["nPageSize" => 1], [] );
while($car_search_ob = $car_search_res->GetNextElement())
{
$car_existed = $car_search_ob->GetFields();
//print "CAR FOUND\n\n";
//print_r($car_existed);
}
$car_update_payload = [ "IBLOCK_SECTION" => false, "NAME" => $name, "CODE" => $code, "ACTIVE" => "Y", ];
if($car_existed !== null)
{
$car_update_element = new CIBlockElement;
$car_update_result = $car_update_element->Update($car_existed['ID'], $car_update_payload);
CIBlockElement::SetPropertyValuesEx($car_existed['ID'], IBLOCK_ID_CATALOG_CARS_NEW, $PROPERTY_VALUES, []);
}
else
{
//if($c === 0)
//{
$car_element = new CIBlockElement;
$car_payload = [ "IBLOCK_SECTION_ID" => false, "IBLOCK_ID" => 1, "NAME" => $name, "CODE" => $code, "PROPERTY_VALUES" => $PROPERTY_VALUES, "ACTIVE" => "Y", ];
if($new_car_id = $car_element->Add($car_payload))
{
//$bitrix_brands[$brand_uid] = $new_brand_id;
//echo "new car id: ".$new_car_id;
}
else
{
print "CAR ERROR: ".$car_element->LAST_ERROR." | ".$car['uid']." | ".$code."\n";
if($car_element->LAST_ERROR == "Элемент с таким символьным кодом уже существует.<br>")
{
$car_with_problem = null;
$car_with_problem_res = CIBlockElement::GetList( [ "ID" => "ASC" ], [ "IBLOCK_ID" => IBLOCK_ID_CATALOG_CARS_NEW, "CODE" => $code ], false, ["nPageSize" => 1], [] );
while($car_with_problem_ob = $car_with_problem_res->GetNextElement())
{
$car_with_problem = $car_with_problem_ob->GetFields();
}
if($car_with_problem !== null)
{
print "REPLACE FOR ".$car_with_problem['ID']." uid => ".$car['uid']."\n\n";
print_r($car_with_problem);
print "\n\n\n";
CIBlockElement::SetPropertyValuesEx($car_with_problem['ID'], IBLOCK_ID_CATALOG_CARS_NEW, $PROPERTY_VALUES, []);
}
}
}
//}
}
}
//print "car_codes?\n";
/*
foreach($car_codes AS $cc => $count)
{
if($count > 1)
{
print "cc ".$cc." - ".$count."\n";
}
}
*/
print "\n\nновых ТС: ".$c."\n\n";
//print "bitrix_existed_cars = ".count($bitrix_existed_cars)."\n";
//print "json_new_cars = ".count($json_new_cars)."\n";
$h = 0;
foreach($bitrix_existed_cars AS $existed_car_uid)
{
if(!in_array($existed_car_uid, $json_new_cars))
{
$h++;
set_car_activity($existed_car_uid, "N");
}
}
print "\n\nснято с публикации ТС: ".$h."\n\n";
print "bitrix_existed_brands\n";
$c = 0;
foreach($bitrix_existed_brands AS $k => $b)
{
if($c < 10)
{
//print $k." => ".$b."\n";
$c++;
}
}
print "bitrix_brands\n";
$c = 0;
foreach($bitrix_brands AS $k => $b)
{
if($c < 10)
{
//print $k." => ".$b."\n";
$c++;
}
}
$md = 0;
foreach($bitrix_existed_modifications AS $existed_modifications_code)
{
if(!in_array($existed_modifications_code, $bitrix_modifications_codes))
{
set_modification_activity($existed_modifications_code, "N");
$md++;
}
}
//print "API_MODELS:\n";
//print_r($api_models);
print "\n\nснято с публикации модификаций: ".$md."\n";
$m = 0;
foreach($bitrix_existed_models AS $existed_model_uid)
{
if($bitrix_models[$existed_model_uid] !== null)
{}
else
{
if(!in_array($existed_model_uid, $api_models))
{
set_model_activity($existed_model_uid, "N");
$m++;
}
}
}
//print "API_BRANDS:\n";
//print_r($api_brands);
print "\n\nснято с публикации моделей: ".$m."\n";
$b = 0;
foreach($bitrix_existed_brands AS $existed_brand_uid)
{
if($bitrix_brands[$existed_brand_uid] !== null)
{}
else
{
if(!in_array($existed_brand_uid, $api_brands))
{
set_brand_activity($existed_brand_uid, "N");
$b++;
}
}
}
print "\n\nснято с публикации марок: ".$b."\n";
die();
?>