diff --git a/mirzaev/spetsresurs/google_sheets/parser/system/public/workers.php b/mirzaev/spetsresurs/google_sheets/parser/system/public/workers.php index 7fe376e..d290822 100644 --- a/mirzaev/spetsresurs/google_sheets/parser/system/public/workers.php +++ b/mirzaev/spetsresurs/google_sheets/parser/system/public/workers.php @@ -9,15 +9,10 @@ use mirzaev\arangodb\connection, use ArangoDBClient\Document as _document; // Фреймворк для Google Sheets -use Flow\ETL\Adapter\GoogleSheet\GoogleSheetRange, - Flow\ETL\Adapter\GoogleSheet\GoogleSheetExtractor, +use Flow\ETL\Adapter\GoogleSheet\GoogleSheetExtractor, Flow\ETL\Adapter\GoogleSheet\Columns, Flow\ETL\Flow, - Flow\ETL\Config, - Flow\ETL\FlowContext, - Flow\ETL\Row\Entry, Flow\ETL\Row, - Flow\ETL\DSL\To, Flow\ETL\DSL\From; // Фреймворк для 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; @@ -200,6 +195,33 @@ function sync(Row &$row, string $city = 'Красноярск'): void // Конвертация $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->setScopes(Sheets::SPREADSHEETS); $client->setAuthConfig($settings); -$api = new Sheets($client); 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; + + $formulas = $sheets->spreadsheets_values->get($document, "$sheet!A:P", ['valueRenderOption' => 'FORMULA']) ?? null; + + if ($formulas === null) continue; + foreach ($rows->fetch(5000) as $row) { + // Перебор строк + + // Запись счётчика ++$i; + + // Инициализация буфера строки $buffer = $row; - sync($row, $sheet); + + // Синхронизация с базой данных + sync($row, $sheet, $formulas[$i - 1]); + + // Запись изменений строки в Google Sheet if ($buffer !== $row) { - $api->spreadsheets_values->update( + $sheets->spreadsheets_values->update( $document, "$sheet!A$i:P$i", new ValueRange(['values' => [array_values($row->entries()->toArray()['row'])]]), diff --git a/mirzaev/spetsresurs/google_sheets/parser/system/public/works.php b/mirzaev/spetsresurs/google_sheets/parser/system/public/works.php index fb20d2d..1a70441 100644 --- a/mirzaev/spetsresurs/google_sheets/parser/system/public/works.php +++ b/mirzaev/spetsresurs/google_sheets/parser/system/public/works.php @@ -123,7 +123,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; @@ -143,24 +143,24 @@ function sync(int $_i, Row &$row, ?array $raw = null): void // Инициализация данных для записи в таблицу $new = [ - 'imported_created_in_sheets' => $work->imported_created_in_sheets['converted'], - 'imported_date' => $work->imported_date['converted'], + 'imported_created_in_sheets' => $work->imported_created_in_sheets, + 'imported_date' => $work->imported_date, 'imported_market' => $work->imported_market, 'imported_worker' => $work->imported_worker, 'imported_work' => $work->imported_work, - 'imported_start' => $work->imported_start['converted'], - 'imported_end' => $work->imported_end['converted'], + 'imported_start' => $work->imported_start, + 'imported_end' => $work->imported_end, 'imported_hours' => $work->imported_hours, - 'created_in_sheets' => $work->created_in_sheets['converted'], - 'date' => $work->date['converted'], + 'created_in_sheets' => $work->created_in_sheets, + 'date' => $work->date, 'market' => $work->market, 'type' => $work->type, 'address' => $work->address, 'worker' => $work->worker, 'name' => $work->name, 'work' => $work->work, - 'start' => $work->start['converted'], - 'end' => $work->end['converted'], + 'start' => $work->start, + 'end' => $work->end, 'hours' => $work->hours, 'tax' => $work->tax, 'confirmed' => $work->confirmed, @@ -244,7 +244,6 @@ function sync(int $_i, Row &$row, ?array $raw = null): void } } - if ( collection::init($arangodb->session, 'requests', true) && collection::init($arangodb->session, 'markets') && ($market = collection::search( @@ -275,7 +274,7 @@ function sync(int $_i, Row &$row, ?array $raw = null): void $_row['market'] ) )) { - // Инициализирована инстанция документа в базе данных нового мазагина + // Инициализирована инстанция документа в базе данных нового магазина // Реинициализация магазина $request->_from = $_market->getId(); @@ -306,32 +305,32 @@ function sync(int $_i, Row &$row, ?array $raw = null): void } else if ( (!empty($_row['imported_market']) || !empty($_row['market'])) && 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( $arangodb->session, sprintf( "FOR d IN works FILTER d._id == '%s' RETURN d", document::write($arangodb->session, 'works', [ - 'imported_created_in_sheets' => ['number' => $_row['imported_created_in_sheets'] ?? '', 'converted' => $raw[0] ?? ''], - 'imported_date' => ['number' => $_row['imported_date'] ?? '', 'converted' => $raw[1] ?? ''], - 'imported_market' => $_row['imported_market'] ?? '', - 'imported_worker' => $_row['imported_worker'] ?? '', - 'imported_work' => $_row['imported_work'] ?? '', - 'imported_start' => ['number' => $_row['imported_start'] ?? '', 'converted' => $raw[5] ?? ''], - 'imported_end' => ['number' => $_row['imported_end'] ?? '', 'converted' => $raw[6] ?? ''], - 'imported_hours' => $_row['imported_hours'] ?? '', - 'created_in_sheets' => ['number' => $_row['imported_created_in_sheets'] ?? '', 'converted' => $raw[0] ?? ''], - 'date' => ['number' => $_row['imported_date'] ?? '', 'converted' => $raw[1] ?? ''], - 'market' => $_row['imported_market'] ?? '', - 'type' => empty($_row['type']) ? "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;2;);\"Нет в базе\"))" : $_row['type'], - 'address' => empty($_row['address']) ? "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;4;);\"Нет в базе\"))" : $_row['address'], - 'worker' => $_row['imported_worker'] ?? '', - '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'], - 'work' => $_row['imported_work'] ?? '', - 'start' => ['number' => $_row['imported_start'] ?? '', 'converted' => $raw[5] ?? ''], - 'end' => ['number' => $_row['imported_end'] ?? '', 'converted' => $raw[6] ?? ''], - 'hours' => $_row['imported_hours'] ?? '', - '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'], + 'imported_created_in_sheets' => $raw[0], + 'imported_date' => $raw[1], + 'imported_market' => $raw[2], + 'imported_worker' => $raw[3], + 'imported_work' => $raw[4], + 'imported_start' => $raw[5], + 'imported_end' => $raw[6], + 'imported_hours' => $raw[7], + 'created_in_sheets' => $raw[0] ?? $raw[8], + 'date' => $raw[1] ?? $raw[9], + 'market' => $raw[2] ?? $raw[10], + 'type' => "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;2;);\"Нет в базе\"))", + 'address' => "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;4;);\"Нет в базе\"))", + 'worker' => filterWorker($raw[13] ?? $raw[3]), + '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' => $raw[4] ?? $raw[15], + 'start' => $raw[5] ?? $raw[16], + 'end' => $raw[6] ?? $raw[17], + 'hours' => $raw[7] ?? $raw[18], + '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'] ?? '', 'commentary' => $_row['commentary'] ?? '', 'response' => $_row['response'] ?? '', @@ -381,18 +380,18 @@ function sync(int $_i, Row &$row, ?array $raw = null): void 'imported_start' => $raw[5] ?? '', 'imported_end' => $raw[6] ?? '', 'imported_hours' => $_row['imported_hours'] ?? '', - 'created_in_sheets' => $raw[0] ?? $raw[8] ?? '', - 'date' => $raw[1] ?? $raw[9] ?? '', - 'market' => $_row['imported_market'] ?? $_row['market'] ?? '', - 'type' => empty($_row['type']) ? "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;2;);\"Нет в базе\"))" : $_row['type'], - 'address' => empty($_row['address']) ? "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;4;);\"Нет в базе\"))" : $_row['address'], - 'worker' => filterWorker($_row['imported_worker'] ?? $_row['worker'] ?? ''), - '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'], - 'work' => $_row['imported_work'] ?? $_row['work'] ?? '', - 'start' => $raw[5] ?? $raw[16] ?? '', - 'end' => $raw[6] ?? $raw[17] ?? '', - 'hours' => $_row['imported_hours'] ?? $_row['hours'] ?? '', - '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'], + 'created_in_sheets' => $raw[0] ?? $raw[8], + 'date' => $raw[1] ?? $raw[9], + 'market' => $raw[2] ?? $raw[10], + 'type' => "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;2;);\"Нет в базе\"))", + 'address' => "=ЕСЛИ(СОВПАД(I$_i;\"\");\"\"; IFNA(ВПР(K$_i;part_import_KRSK!\$B\$2:\$E\$15603;4;);\"Нет в базе\"))", + 'worker' => filterWorker($raw[13] ?? $raw[3]), + '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' => $raw[4] ?? $raw[15], + 'start' => $raw[5] ?? $raw[16], + 'end' => $raw[6] ?? $raw[17], + 'hours' => $raw[7] ?? $raw[18], + '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'] ?? '', 'commentary' => $_row['commentary'] ?? '', 'response' => $_row['response'] ?? '', @@ -417,11 +416,15 @@ foreach ($sheets as $sheet) { $sheets = new Sheets($client); // Инициализация инстанции 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; + $raws = $sheets->spreadsheets_values->get($document, "$sheet!A:X") ?? null; + + if ($raws === null) continue; + foreach ($rows->fetch(10000) as $row) { // Перебор строк @@ -432,14 +435,33 @@ foreach ($sheets as $sheet) { $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 if ($buffer !== $row) { + $row = init($row->toArray()['row']); + $sheets->spreadsheets_values->update( $document, - "$sheet!A$i:X$i", - new ValueRange(['values' => [array_values($row->entries()->toArray()['row'])]]), + "$sheet!I$i:X$i", + 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'] );