Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace console\modules\tasks\requests\controllers;
- use common\components\ManagerAssignment\ManagerAssignment;
- use common\models\Customers;
- use common\models\RequestTypes;
- use console\modules\tasks\TaskController;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- use yii\helpers\FileHelper;
- use RuntimeException;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use GuzzleHttp\Client;
- class ImportController extends TaskController
- {
- public function rules(): array
- {
- return [];
- }
- /**
- * Ипорт заявок их экселя
- *
- * Использование:
- * ./bdctl yii requests/import/import-from-excel /path/to/file.xlsx
- *
- * @param string $filename
- */
- public function actionImportFromExcel(string $filename)
- {
- $filename = FileHelper::normalizePath($filename);
- if (!is_readable($filename)) {
- throw new RuntimeException("Не удалось прочитать файл $filename");
- }
- $spreadsheet = IOFactory::load($filename);
- $sheet = $spreadsheet->getActiveSheet();
- $header = null;
- foreach ($sheet->getRowIterator() as $row) {
- $rowIndex = $row->getRowIndex();
- $cellIterator = $row->getCellIterator();
- $cellIterator->setIterateOnlyExistingCells(false);
- $cells = [];
- foreach ($cellIterator as $cell) {
- $cells[] = $cell->getCalculatedValue();
- }
- if (count(array_filter($cells, function ($v) {
- return $v !== null && $v !== '';
- })) === 0) {
- break;
- }
- if ($rowIndex == 1) {
- $header = array_map(function($title) {
- return $title === null ? '' : trim($title);
- }, $cells);
- continue;
- }
- if ($header === null) {
- continue;
- }
- $assoc = [];
- foreach ($header as $i => $colName) {
- $key = $colName;
- $assoc[$key] = $cells[$i] ?? null;
- }
- $a = [
- 'type_id' => RequestTypes::TYPE_SVETDIZAIN_LS_BEZRODINA,
- 'customer_phone' => $assoc['телефон'],
- 'customer_email' => $assoc['почта'],
- 'customer_id' => $assoc['src_ID'],
- 'manager_user_assignment_type' => ManagerAssignment::MANAGER_USER_ASSIGNMENT_TYPE_HARDCODE,
- 'customer_name' => $assoc['имя'],
- 'extra' => [
- 'customer' => [
- 'active_status' => Customers::ACTIVE_STATUS_ON,
- ]
- ]
- ];
- $client = new Client(['base_uri' => 'htt',
- 'verify' => false,]);
- try {
- $rawResponse = $client->post('/v1/crm/requests/create/', [
- 'json' => $a,
- 'headers' => [
- 'X-Token' => '',
- ],
- 'timeout' => 10,
- ]);
- $httpCode = $rawResponse->getStatusCode();
- $body = (string)$rawResponse->getBody();
- $data = \GuzzleHttp\json_decode($body, false);
- $this->stdout("HTTP status: {$httpCode}\n");
- $this->stdout("statusCode: {$data->statusCode}\n");
- } catch (\Throwable $e) {
- $this->stderr("Ошибка: " . $e->getMessage() . "\n");
- return 1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment