176 lines
8.1 KiB
PHP
176 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use A7kz\Platform\Models\UniModel;
|
|
use A7kz\Platform\Modules\Platform\Segment\Facades\Segment;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ApiController extends Controller
|
|
{
|
|
public function syncData(Request $request, $modelName, $validationRules, $extraProcessing = null)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$validatedData = $request->validate($validationRules);
|
|
$segments = Segment::listActive();
|
|
|
|
foreach ($segments as $segment) {
|
|
foreach ($validatedData as $value) {
|
|
UniModel::model($modelName, $segment->connector)->updateOrCreate(
|
|
['code' => $value['Код']],
|
|
array_merge($this->mapAttributes($value), $extraProcessing ? $extraProcessing($value, $segment) : [])
|
|
);
|
|
}
|
|
}
|
|
|
|
if (isset($extraProcessing)) {
|
|
foreach ($validatedData as $value) {
|
|
$this->processParentRelation($modelName, $value);
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
return response()->json(['message' => ucfirst($modelName) . ' data synchronized successfully'], 200);
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('Error syncing ' . $modelName . ' data: ' . $e->getMessage());
|
|
return response()->json(['error' => 'Failed to synchronize ' . $modelName . ' data'], 500);
|
|
}
|
|
}
|
|
|
|
private function mapAttributes($data)
|
|
{
|
|
return [
|
|
'is_group' => (bool)($data['ЭтоГруппа'] ?? false),
|
|
'name' => $data['Наименование'] ?? null,
|
|
'comments' => $data['Комментарий'] ?? null,
|
|
'iin' => $data['ИдентификационныйКодЛичности'] ?? null,
|
|
'kbe' => $data['КБЕ'] ?? null,
|
|
'okpo_code' => $data['КодПоОКПО'] ?? null,
|
|
'main_contact' => $data['ОсновноеКонтактноеЛицо'] ?? null,
|
|
'bank_account' => $data['ОсновнойБанковскийСчет'] ?? null,
|
|
'address' => $data['Адрес'] ?? null,
|
|
'phone' => $data['Телефон'] ?? null,
|
|
'mail' => $data['Почта'] ?? null,
|
|
'id_number' => $data['УдНомер'] ?? null,
|
|
'id_date' => $data['УдДата'] ?? null,
|
|
'issued' => $data['УдВыдан'] ?? null,
|
|
'contract_id' => $data['ОсновнойДоговор'] ?? null,
|
|
'deleted_at' => $data['ПометкаУдаления'] ? now() : null,
|
|
];
|
|
}
|
|
|
|
private function processParentRelation($modelName, $data)
|
|
{
|
|
$model = UniModel::model($modelName)->where('name', $data['Наименование'])->first();
|
|
|
|
if ($model && !empty($data['Родитель'])) {
|
|
$parent = UniModel::model($modelName)->where('name', $data['Родитель'])->first();
|
|
if ($parent) {
|
|
$model->parent_id = $parent->id;
|
|
$model->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
public function syncAutoData(Request $request)
|
|
{
|
|
$validationRules = [
|
|
'*.Код' => 'required|string',
|
|
'*.Наименование' => 'required|string',
|
|
'*.Тип' => 'required|string',
|
|
'*.Класс' => 'required|string',
|
|
'*.Марка' => 'required|string',
|
|
'*.Модель' => 'required|string',
|
|
'*.Цвет' => 'required|string',
|
|
'*.СерийныйНомер' => 'nullable|string',
|
|
'*.ГосНомер' => 'nullable|string',
|
|
'*.ГодПроизводства' => 'nullable|integer',
|
|
'*.НомерТехПаспорта' => 'nullable|string',
|
|
'*.ДатаТехпаспорта' => 'nullable|date',
|
|
'*.ОценочнаяСтоимость' => 'nullable|numeric',
|
|
'*.ВладелецИмущества' => 'required|string',
|
|
'*.ВДоверенномУправлении' => 'required|boolean',
|
|
'*.ПроцентВладельца' => 'nullable|numeric',
|
|
'*.НеИспользовать' => 'required|boolean',
|
|
'*.Предопределенный' => 'required|boolean',
|
|
'*.ИмяПредопределенныхДанных' => 'nullable|string',
|
|
'*.ПометкаУдаления' => 'required|boolean',
|
|
];
|
|
|
|
$extraProcessing = function ($value, $segment) {
|
|
return [
|
|
'type_id' => UniModel::model('pipi_auto_types', $segment->connector)
|
|
->firstOrCreate(['name' => $value['Тип']])->id,
|
|
'class_id' => UniModel::model('pipi_auto_classes', $segment->connector)
|
|
->firstOrCreate(['name' => $value['Класс']])->id,
|
|
'brand_id' => UniModel::model('pipi_auto_brands', $segment->connector)
|
|
->firstOrCreate(['name' => $value['Марка']])->id,
|
|
'model_id' => UniModel::model('pipi_brand_models', $segment->connector)
|
|
->firstOrCreate([
|
|
'name' => $value['Модель'],
|
|
'brand_id' => UniModel::model('pipi_auto_brands', $segment->connector)
|
|
->firstOrCreate(['name' => $value['Марка']])->id,
|
|
])->id,
|
|
'color_id' => UniModel::model('pipi_auto_colors', $segment->connector)
|
|
->firstOrCreate(['name' => $value['Цвет']])->id,
|
|
'owner_id' => UniModel::model('pipi_owners', $segment->connector)
|
|
->firstOrCreate(['name' => $value['ВладелецИмущества']])->id,
|
|
];
|
|
};
|
|
|
|
return $this->syncData($request, 'pipi_auto', $validationRules, $extraProcessing);
|
|
}
|
|
|
|
public function syncOwners(Request $request)
|
|
{
|
|
$validationRules = [
|
|
'*.Код' => 'required|string',
|
|
'*.Наименование' => 'required|string',
|
|
'*.Комментарий' => 'nullable|string',
|
|
'*.ИдентификационныйКодЛичности' => 'nullable|string',
|
|
'*.КБЕ' => 'nullable|string',
|
|
'*.КодПоОКПО' => 'nullable|string',
|
|
'*.ОсновноеКонтактноеЛицо' => 'nullable|string',
|
|
'*.ОсновнойБанковскийСчет' => 'nullable|string',
|
|
'*.Адрес' => 'nullable|string',
|
|
'*.Телефон' => 'nullable|string',
|
|
'*.Почта' => 'nullable|string',
|
|
'*.УдНомер' => 'nullable|string',
|
|
'*.УдДата' => 'nullable|date',
|
|
'*.УдВыдан' => 'nullable|string',
|
|
'*.ОсновнойДоговор' => 'nullable|string',
|
|
'*.ЭтоГруппа' => 'required|boolean',
|
|
'*.ПометкаУдаления' => 'required|boolean',
|
|
'*.Родитель' => 'nullable|string',
|
|
];
|
|
|
|
return $this->syncData($request, 'pipi_owners', $validationRules);
|
|
}
|
|
|
|
public function syncOwnerContracts(Request $request)
|
|
{
|
|
$validationRules = [
|
|
'*.Код' => 'required|string',
|
|
'*.Наименование' => 'required|string',
|
|
'*.Комментарий' => 'nullable|string',
|
|
'*.Организация' => 'nullable|string',
|
|
'*.НомерДоговора' => 'required|string',
|
|
'*.ДатаДоговора' => 'nullable|date',
|
|
'*.ДатаНачалаДействияДоговора' => 'nullable|date',
|
|
'*.ДатаОкончанияДействияДоговора' => 'nullable|date',
|
|
'*.УсловияОплаты' => 'nullable|string',
|
|
'*.ЭтоГруппа' => 'required|boolean',
|
|
'*.ПометкаУдаления' => 'required|boolean',
|
|
'*.Родитель' => 'nullable|string',
|
|
];
|
|
|
|
return $this->syncData($request, 'pipi_owner_contracts', $validationRules);
|
|
}
|
|
}
|