narem

Untitled

Sep 26th, 2025
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.64 KB | None | 0 0
  1. <?php
  2. namespace console\modules\tasks\requests\controllers;
  3.  
  4. use common\components\ManagerAssignment\ManagerAssignment;
  5. use common\models\Customers;
  6. use common\models\RequestTypes;
  7. use console\modules\tasks\TaskController;
  8. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  9. use yii\helpers\FileHelper;
  10. use RuntimeException;
  11. use PhpOffice\PhpSpreadsheet\IOFactory;
  12. use GuzzleHttp\Client;
  13.  
  14. class ImportController extends TaskController
  15. {
  16.     public function rules(): array
  17.     {
  18.         return [];
  19.     }
  20.  
  21.     /**
  22.      * Ипорт заявок их экселя
  23.      *
  24.      * Использование:
  25.      * ./bdctl yii requests/import/import-from-excel /path/to/file.xlsx
  26.      *
  27.      * @param string $filename
  28.      */
  29.     public function actionImportFromExcel(string $filename)
  30.     {
  31.         $filename = FileHelper::normalizePath($filename);
  32.  
  33.         if (!is_readable($filename)) {
  34.             throw new RuntimeException("Не удалось прочитать файл $filename");
  35.         }
  36.  
  37.         $spreadsheet = IOFactory::load($filename);
  38.         $sheet = $spreadsheet->getActiveSheet();
  39.         $header = null;
  40.  
  41.         foreach ($sheet->getRowIterator() as $row) {
  42.             $rowIndex = $row->getRowIndex();
  43.             $cellIterator = $row->getCellIterator();
  44.             $cellIterator->setIterateOnlyExistingCells(false);
  45.  
  46.             $cells = [];
  47.             foreach ($cellIterator as $cell) {
  48.                 $cells[] = $cell->getCalculatedValue();
  49.             }
  50.  
  51.             if (count(array_filter($cells, function ($v) {
  52.                     return $v !== null && $v !== '';
  53.                 })) === 0) {
  54.                 break;
  55.             }
  56.  
  57.             if ($rowIndex == 1) {
  58.                 $header = array_map(function($title) {
  59.                     return $title === null ? '' : trim($title);
  60.                 }, $cells);
  61.                 continue;
  62.             }
  63.  
  64.             if ($header === null) {
  65.                 continue;
  66.             }
  67.  
  68.             $assoc = [];
  69.             foreach ($header as $i => $colName) {
  70.                 $key = $colName;
  71.                 $assoc[$key] = $cells[$i] ?? null;
  72.             }
  73.  
  74.             $a = [
  75.                 'type_id' => RequestTypes::TYPE_SVETDIZAIN_LS_BEZRODINA,
  76.                 'customer_phone' => $assoc['телефон'],
  77.                 'customer_email' => $assoc['почта'],
  78.                 'customer_id' => $assoc['src_ID'],
  79.                 'manager_user_assignment_type' => ManagerAssignment::MANAGER_USER_ASSIGNMENT_TYPE_HARDCODE,
  80.                 'customer_name' => $assoc['имя'],
  81.                 'extra' => [
  82.                     'customer' => [
  83.                         'active_status' => Customers::ACTIVE_STATUS_ON,
  84.                     ]
  85.                 ]
  86.             ];
  87.  
  88.             $client = new Client(['base_uri' => 'htt',
  89.                 'verify'   => false,]);
  90.             try {
  91.                 $rawResponse = $client->post('/v1/crm/requests/create/', [
  92.                     'json' => $a,
  93.                     'headers' => [
  94.                         'X-Token' => '',
  95.                     ],
  96.                     'timeout' => 10,
  97.                 ]);
  98.  
  99.                 $httpCode = $rawResponse->getStatusCode();
  100.                 $body = (string)$rawResponse->getBody();
  101.  
  102.                 $data = \GuzzleHttp\json_decode($body, false);
  103.  
  104.                 $this->stdout("HTTP status: {$httpCode}\n");
  105.                
  106.                 $this->stdout("statusCode: {$data->statusCode}\n");
  107.             } catch (\Throwable $e) {
  108.                 $this->stderr("Ошибка: " . $e->getMessage() . "\n");
  109.                 return 1;
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment