Compare commits

...
This repository has been archived on 2024-10-16. You can view files and clone it, but cannot push or open issues or pull requests.

2 Commits

2 changed files with 126 additions and 61 deletions

View File

@ -9,15 +9,10 @@ use mirzaev\arangodb\connection,
use ArangoDBClient\Document as _document; use ArangoDBClient\Document as _document;
// Фреймворк для Google Sheets // Фреймворк для Google Sheets
use Flow\ETL\Adapter\GoogleSheet\GoogleSheetRange, use Flow\ETL\Adapter\GoogleSheet\GoogleSheetExtractor,
Flow\ETL\Adapter\GoogleSheet\GoogleSheetExtractor,
Flow\ETL\Adapter\GoogleSheet\Columns, Flow\ETL\Adapter\GoogleSheet\Columns,
Flow\ETL\Flow, Flow\ETL\Flow,
Flow\ETL\Config,
Flow\ETL\FlowContext,
Flow\ETL\Row\Entry,
Flow\ETL\Row, Flow\ETL\Row,
Flow\ETL\DSL\To,
Flow\ETL\DSL\From; Flow\ETL\DSL\From;
// Фреймворк для Google API // Фреймворк для Google API
@ -148,7 +143,7 @@ function connectAll(_document $worker): void
} }
function sync(Row &$row, string $city = 'Красноярск'): void function sync(Row &$row, string $city = 'Красноярск', array $formulas = []): void
{ {
global $arangodb; global $arangodb;
@ -200,6 +195,33 @@ function sync(Row &$row, string $city = 'Красноярск'): void
// Конвертация // Конвертация
$worker->{$key} = $_row[$key] ?? $value; $worker->{$key} = $_row[$key] ?? $value;
} }
if (strlen($formulas[2]) < 12) {
// Не конвертирован номер
// Инициализация номера
$number = convertNumber($_row['phone'] ?? '');
// Реинициализация строки с новыми данными по ссылке (приоритет из Google Sheets)
$row = $row->set((new Flow())->read(From::array([init([
'id' => $_row['id'] ?? '',
'name' => $_row['name'] ?? '',
'phone' => "=HYPERLINK(\"https://call.ctrlq.org/+$number\"; \"$number\")",
'birth' => $_row['birth'] ?? '',
'address' => $_row['address'] ?? '',
'commentary' => $_row['commentary'] ?? '',
'activity' => $_row['activity'] ?? '',
'passport' => $_row['passport'] ?? '',
'issued' => $_row['issued'] ?? '',
'department' => $_row['department'] ?? '',
'hiring' => $_row['hiring'] ?? '',
'district' => $_row['district'] ?? '',
'requisites' => $_row['requisites'] ?? '',
'fired' => $_row['fired'] ?? '',
'payment' => $_row['payment'] ?? '',
'tax' => $_row['tax'] ?? '',
], true)]))->fetch(1)[0]->get('row'));
}
} }
// Обновление инстанции документа в базе данных // Обновление инстанции документа в базе данных
@ -275,18 +297,38 @@ $sheets = require(__DIR__ . '/../settings/workers/sheets.php');
$client = new Client(); $client = new Client();
$client->setScopes(Sheets::SPREADSHEETS); $client->setScopes(Sheets::SPREADSHEETS);
$client->setAuthConfig($settings); $client->setAuthConfig($settings);
$api = new Sheets($client);
foreach ($sheets as $sheet) { foreach ($sheets as $sheet) {
$rows = (new Flow())->read(new GoogleSheetExtractor($api, $document, new Columns($sheet, 'A', 'P'), true, 1000, 'row')); // Перебор таблиц
// Инициализация обработчика таблиц
$sheets = new Sheets($client);
// Инициализация инстанции Flow для Google Sheet API
$rows = (new Flow())->read(new GoogleSheetExtractor($sheets, $document, new Columns($sheet, 'A', 'P'), true, 1000, 'row'));
// Инициализация счётчика итераций
$i = 1; $i = 1;
$formulas = $sheets->spreadsheets_values->get($document, "$sheet!A:P", ['valueRenderOption' => 'FORMULA']) ?? null;
if ($formulas === null) continue;
foreach ($rows->fetch(5000) as $row) { foreach ($rows->fetch(5000) as $row) {
// Перебор строк
// Запись счётчика
++$i; ++$i;
// Инициализация буфера строки
$buffer = $row; $buffer = $row;
sync($row, $sheet);
// Синхронизация с базой данных
sync($row, $sheet, $formulas[$i - 1]);
// Запись изменений строки в Google Sheet
if ($buffer !== $row) { if ($buffer !== $row) {
$api->spreadsheets_values->update( $sheets->spreadsheets_values->update(
$document, $document,
"$sheet!A$i:P$i", "$sheet!A$i:P$i",
new ValueRange(['values' => [array_values($row->entries()->toArray()['row'])]]), new ValueRange(['values' => [array_values($row->entries()->toArray()['row'])]]),

View File

@ -95,8 +95,9 @@ function filterWorker(?string $worker): string
{ {
global $arangodb; global $arangodb;
return match ((int) $worker) { return match ((int) $worker) {
'', 'Отмена', 'отмена', 0, 00, 000, 0000, 00000, 000000, 0000000, 00000000, 000000000, 0000000000 => '', 'Отмена', 'отмена', 'ОТМЕНА' => 'Отмена',
'', 0, 00, 000, 0000, 00000, 000000, 0000000, 00000000, 000000000, 0000000000 => '',
default => (function () use ($worker, $arangodb) { default => (function () use ($worker, $arangodb) {
if ( if (
collection::init($arangodb->session, 'workers') collection::init($arangodb->session, 'workers')
@ -123,7 +124,7 @@ function init(array $row, bool $reverse = false): array
} }
function sync(int $_i, Row &$row, ?array $raw = null): void function sync(int $_i, Row &$row, array $raw = []): void
{ {
global $arangodb; global $arangodb;
@ -143,24 +144,24 @@ function sync(int $_i, Row &$row, ?array $raw = null): void
// Инициализация данных для записи в таблицу // Инициализация данных для записи в таблицу
$new = [ $new = [
'imported_created_in_sheets' => $work->imported_created_in_sheets['converted'], 'imported_created_in_sheets' => $work->imported_created_in_sheets,
'imported_date' => $work->imported_date['converted'], 'imported_date' => $work->imported_date,
'imported_market' => $work->imported_market, 'imported_market' => $work->imported_market,
'imported_worker' => $work->imported_worker, 'imported_worker' => $work->imported_worker,
'imported_work' => $work->imported_work, 'imported_work' => $work->imported_work,
'imported_start' => $work->imported_start['converted'], 'imported_start' => $work->imported_start,
'imported_end' => $work->imported_end['converted'], 'imported_end' => $work->imported_end,
'imported_hours' => $work->imported_hours, 'imported_hours' => $work->imported_hours,
'created_in_sheets' => $work->created_in_sheets['converted'], 'created_in_sheets' => $work->created_in_sheets,
'date' => $work->date['converted'], 'date' => $work->date,
'market' => $work->market, 'market' => $work->market,
'type' => $work->type, 'type' => $work->type,
'address' => $work->address, 'address' => $work->address,
'worker' => $work->worker, 'worker' => $work->worker,
'name' => $work->name, 'name' => $work->name,
'work' => $work->work, 'work' => $work->work,
'start' => $work->start['converted'], 'start' => $work->start,
'end' => $work->end['converted'], 'end' => $work->end,
'hours' => $work->hours, 'hours' => $work->hours,
'tax' => $work->tax, 'tax' => $work->tax,
'confirmed' => $work->confirmed, 'confirmed' => $work->confirmed,
@ -244,7 +245,6 @@ function sync(int $_i, Row &$row, ?array $raw = null): void
} }
} }
if ( if (
collection::init($arangodb->session, 'requests', true) && collection::init($arangodb->session, 'markets') collection::init($arangodb->session, 'requests', true) && collection::init($arangodb->session, 'markets')
&& ($market = collection::search( && ($market = collection::search(
@ -275,7 +275,7 @@ function sync(int $_i, Row &$row, ?array $raw = null): void
$_row['market'] $_row['market']
) )
)) { )) {
// Инициализирована инстанция документа в базе данных нового мазагина // Инициализирована инстанция документа в базе данных нового магазина
// Реинициализация магазина // Реинициализация магазина
$request->_from = $_market->getId(); $request->_from = $_market->getId();
@ -306,32 +306,32 @@ function sync(int $_i, Row &$row, ?array $raw = null): void
} else if ( } else if (
(!empty($_row['imported_market']) || !empty($_row['market'])) (!empty($_row['imported_market']) || !empty($_row['market']))
&& collection::init($arangodb->session, 'requests', true) && collection::init($arangodb->session, 'markets') && collection::init($arangodb->session, 'requests', true) && collection::init($arangodb->session, 'markets')
&& ($market = collection::search($arangodb->session, sprintf("FOR d IN markets FILTER d.id == '%s' RETURN d", $_row['imported_market'] ?? $_row['market']))) && ($market = collection::search($arangodb->session, sprintf("FOR d IN markets FILTER d.id == '%s' RETURN d", $raw[2] ?? $_row[10])))
&& $work = collection::search( && $work = collection::search(
$arangodb->session, $arangodb->session,
sprintf( sprintf(
"FOR d IN works FILTER d._id == '%s' RETURN d", "FOR d IN works FILTER d._id == '%s' RETURN d",
document::write($arangodb->session, 'works', [ document::write($arangodb->session, 'works', [
'imported_created_in_sheets' => ['number' => $_row['imported_created_in_sheets'] ?? '', 'converted' => $raw[0] ?? ''], 'imported_created_in_sheets' => $raw[0],
'imported_date' => ['number' => $_row['imported_date'] ?? '', 'converted' => $raw[1] ?? ''], 'imported_date' => $raw[1],
'imported_market' => $_row['imported_market'] ?? '', 'imported_market' => $raw[2],
'imported_worker' => $_row['imported_worker'] ?? '', 'imported_worker' => $raw[3],
'imported_work' => $_row['imported_work'] ?? '', 'imported_work' => $raw[4],
'imported_start' => ['number' => $_row['imported_start'] ?? '', 'converted' => $raw[5] ?? ''], 'imported_start' => $raw[5],
'imported_end' => ['number' => $_row['imported_end'] ?? '', 'converted' => $raw[6] ?? ''], 'imported_end' => $raw[6],
'imported_hours' => $_row['imported_hours'] ?? '', 'imported_hours' => $raw[7],
'created_in_sheets' => ['number' => $_row['imported_created_in_sheets'] ?? '', 'converted' => $raw[0] ?? ''], 'created_in_sheets' => $raw[0] ?? $raw[8],
'date' => ['number' => $_row['imported_date'] ?? '', 'converted' => $raw[1] ?? ''], 'date' => $raw[1] ?? $raw[9],
'market' => $_row['imported_market'] ?? '', 'market' => $raw[2] ?? $raw[10],
'type' => empty($_row['type']) ? "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;2;);\"Нет в базе\"))" : $_row['type'], 'type' => "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;2;);\"Нет в базе\"))",
'address' => empty($_row['address']) ? "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;4;);\"Нет в базе\"))" : $_row['address'], 'address' => "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;4;);\"Нет в базе\"))",
'worker' => $_row['imported_worker'] ?? '', 'worker' => filterWorker($raw[13] ?? $raw[3]),
'name' => empty($_row['name']) ? "=ЕСЛИ(СОВПАД(\$I$_i;\"\");\"\"; ЕСЛИ( НЕ(СОВПАД(IFNA(ВПР(\$N$_i;part_import_KRSK!\$R$2:\$R$4999;1;);\"\");\$N$_i)); ЕСЛИ((СОВПАД(IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$L\$4999;4);\"\");\"\")); IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$J\$4999;2;); \"Сотрудник не назначен\"); \"УВОЛЕН (В списке работающих)\"); \"УВОЛЕН (В списке уволенных)\"))" : $_row['name'], 'name' => "=ЕСЛИ(СОВПАД(\$I$_i;\"\");\"\"; ЕСЛИ( НЕ(СОВПАД(IFNA(ВПР(\$N$_i;part_import_KRSK!\$R$2:\$R$4999;1;);\"\");\$N$_i)); ЕСЛИ((СОВПАД(IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$L\$4999;4);\"\");\"\")); IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$J\$4999;2;); \"Сотрудник не назначен\"); \"УВОЛЕН (В списке работающих)\"); \"УВОЛЕН (В списке уволенных)\"))",
'work' => $_row['imported_work'] ?? '', 'work' => $raw[4] ?? $raw[15],
'start' => ['number' => $_row['imported_start'] ?? '', 'converted' => $raw[5] ?? ''], 'start' => $raw[5] ?? $raw[16],
'end' => ['number' => $_row['imported_end'] ?? '', 'converted' => $raw[6] ?? ''], 'end' => $raw[6] ?? $raw[17],
'hours' => $_row['imported_hours'] ?? '', 'hours' => $raw[7] ?? $raw[18],
'tax' => empty($_row['tax']) ? "=ЕСЛИ(СОВПАД(\$I$_i;\"\");\"\"; IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$K\$5000;3;); IFNA(ВПР(\$N$_i;part_import_KRSK!\$R\$2:\$T\$5000;3;);\"000000000000\")))" : $_row['tax'], 'tax' => "=ЕСЛИ(СОВПАД(\$I$_i;\"\");\"\"; IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$K\$5000;3;); IFNA(ВПР(\$N$_i;part_import_KRSK!\$R\$2:\$T\$5000;3;);\"000000000000\")))",
'confirmed' => $_row['confirmed'] ?? '', 'confirmed' => $_row['confirmed'] ?? '',
'commentary' => $_row['commentary'] ?? '', 'commentary' => $_row['commentary'] ?? '',
'response' => $_row['response'] ?? '', 'response' => $_row['response'] ?? '',
@ -381,18 +381,18 @@ function sync(int $_i, Row &$row, ?array $raw = null): void
'imported_start' => $raw[5] ?? '', 'imported_start' => $raw[5] ?? '',
'imported_end' => $raw[6] ?? '', 'imported_end' => $raw[6] ?? '',
'imported_hours' => $_row['imported_hours'] ?? '', 'imported_hours' => $_row['imported_hours'] ?? '',
'created_in_sheets' => $raw[0] ?? $raw[8] ?? '', 'created_in_sheets' => $raw[0] ?? $raw[8],
'date' => $raw[1] ?? $raw[9] ?? '', 'date' => $raw[1] ?? $raw[9],
'market' => $_row['imported_market'] ?? $_row['market'] ?? '', 'market' => $raw[2] ?? $raw[10],
'type' => empty($_row['type']) ? "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;2;);\"Нет в базе\"))" : $_row['type'], 'type' => "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;2;);\"Нет в базе\"))",
'address' => empty($_row['address']) ? "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;4;);\"Нет в базе\"))" : $_row['address'], 'address' => "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;4;);\"Нет в базе\"))",
'worker' => filterWorker($_row['imported_worker'] ?? $_row['worker'] ?? ''), 'worker' => filterWorker($raw[13] ?? $raw[3]),
'name' => empty($_row['name']) ? "=ЕСЛИ(СОВПАД(\$I$_i;\"\");\"\"; ЕСЛИ( НЕ(СОВПАД(IFNA(ВПР(\$N$_i;part_import_KRSK!\$R$2:\$R$4999;1;);\"\");\$N$_i)); ЕСЛИ((СОВПАД(IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$L\$4999;4);\"\");\"\")); IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$J\$4999;2;); \"Сотрудник не назначен\"); \"УВОЛЕН (В списке работающих)\"); \"УВОЛЕН (В списке уволенных)\"))" : $_row['name'], 'name' => "=ЕСЛИ(СОВПАД(\$I$_i;\"\");\"\"; ЕСЛИ( НЕ(СОВПАД(IFNA(ВПР(\$N$_i;part_import_KRSK!\$R$2:\$R$4999;1;);\"\");\$N$_i)); ЕСЛИ((СОВПАД(IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$L\$4999;4);\"\");\"\")); IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$J\$4999;2;); \"Сотрудник не назначен\"); \"УВОЛЕН (В списке работающих)\"); \"УВОЛЕН (В списке уволенных)\"))",
'work' => $_row['imported_work'] ?? $_row['work'] ?? '', 'work' => $raw[4] ?? $raw[15],
'start' => $raw[5] ?? $raw[16] ?? '', 'start' => $raw[5] ?? $raw[16],
'end' => $raw[6] ?? $raw[17] ?? '', 'end' => $raw[6] ?? $raw[17],
'hours' => $_row['imported_hours'] ?? $_row['hours'] ?? '', 'hours' => $raw[7] ?? $raw[18],
'tax' => empty($_row['tax']) ? "=ЕСЛИ(СОВПАД(\$I$_i;\"\");\"\"; IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$K\$5000;3;); IFNA(ВПР(\$N$_i;part_import_KRSK!\$R\$2:\$T\$5000;3;);\"000000000000\")))" : $_row['tax'], 'tax' => "=ЕСЛИ(СОВПАД(\$I$_i;\"\");\"\"; IFNA(ВПР(\$N$_i;part_import_KRSK!\$I\$2:\$K\$5000;3;); IFNA(ВПР(\$N$_i;part_import_KRSK!\$R\$2:\$T\$5000;3;);\"000000000000\")))",
'confirmed' => $_row['confirmed'] ?? '', 'confirmed' => $_row['confirmed'] ?? '',
'commentary' => $_row['commentary'] ?? '', 'commentary' => $_row['commentary'] ?? '',
'response' => $_row['response'] ?? '', 'response' => $_row['response'] ?? '',
@ -417,11 +417,15 @@ foreach ($sheets as $sheet) {
$sheets = new Sheets($client); $sheets = new Sheets($client);
// Инициализация инстанции Flow для Google Sheet API // Инициализация инстанции Flow для Google Sheet API
$rows = (new Flow())->read(new GoogleSheetExtractor($sheets, $document, new Columns($sheet, 'A', 'X'), true, 1000, 'row', ['valueRenderOption' => 'FORMULA'])); $rows = (new Flow())->read(new GoogleSheetExtractor($sheets, $document, new Columns($sheet, 'A', 'X'), true, 1000, 'row'));
// Инициализация счётчика итераций // Инициализация счётчика итераций
$i = 1; $i = 1;
$raws = $sheets->spreadsheets_values->get($document, "$sheet!A:X") ?? null;
if ($raws === null) continue;
foreach ($rows->fetch(10000) as $row) { foreach ($rows->fetch(10000) as $row) {
// Перебор строк // Перебор строк
@ -432,14 +436,33 @@ foreach ($sheets as $sheet) {
$buffer = $row; $buffer = $row;
// Синхронизация с базой данных // Синхронизация с базой данных
sync($i, $row, $sheets->spreadsheets_values->get($document, "$sheet!A$i:X$i")[0] ?? null); sync($i, $row, $raws[$i - 1]);
// Запись изменений строки в Google Sheet // Запись изменений строки в Google Sheet
if ($buffer !== $row) { if ($buffer !== $row) {
$row = init($row->toArray()['row']);
$sheets->spreadsheets_values->update( $sheets->spreadsheets_values->update(
$document, $document,
"$sheet!A$i:X$i", "$sheet!I$i:X$i",
new ValueRange(['values' => [array_values($row->entries()->toArray()['row'])]]), new ValueRange(['values' => [array_values(init([
'created_in_sheets' => $row['created_in_sheets'] ?? '',
'date' => $row['date'] ?? '',
'market' => $row['market'] ?? '',
'type' => $row['type'] ?? '',
'address' => $row['address'] ?? '',
'worker' => $row['worker'] ?? '',
'name' => $row['name'] ?? '',
'work' => $row['work'] ?? '',
'start' => $row['start'] ?? '',
'end' => $row['end'] ?? '',
'hours' => $row['hours'] ?? '',
'tax' => $row['tax'] ?? '',
'confirmed' => $row['confirmed'] ?? '',
'commentary' => $row['commentary'] ?? '',
'response' => $row['response'] ?? '',
'_id' => $row['_id'] ?? '',
], true))]]),
['valueInputOption' => 'USER_ENTERED'] ['valueInputOption' => 'USER_ENTERED']
); );