ImportXlsxJob.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Product;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Str;
  12. class ImportXlsxJob implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @return void
  19. */
  20. protected $filename;
  21. public function __construct($filename)
  22. {
  23. $this->filename = $filename;
  24. }
  25. /**
  26. * Execute the job.
  27. *
  28. * @return void
  29. */
  30. public function handle()
  31. {
  32. $xls = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($this->filename);
  33. $xls->setReadDataOnly(true);
  34. $sheet = $xls->load($this->filename);
  35. $tmpfile = $this->filename . '.txt';
  36. // read xls to array
  37. $goods = $sheet->setActiveSheetIndex(0)->toArray();
  38. unset($sheet, $xls);
  39. $series = '';
  40. $i = $created = $updated = 0;
  41. $no_image = [];
  42. foreach ($goods as $good) {
  43. // check first line and skip it
  44. if ($good[0] === '№п/п' && $good['3'] === 'Наименование') continue;
  45. // check the line is name of series
  46. if ($good[0] == NULL && $good[1] == NULL && $good[2] == NULL && is_string($good[3])) {
  47. $series = $good[3];
  48. continue;
  49. }
  50. // get article from 3rd column
  51. $tmp = explode("\n", $good[3]);
  52. if (!isset($tmp[1])) {
  53. $good[3] = preg_replace('!\s+!', ' ', $good[3]);
  54. $tmp = explode(' ', $good[3], 2);
  55. }
  56. $images = $this->find_images($tmp[0]);
  57. $data = [
  58. 'series' => $series,
  59. 'name' => (isset($tmp[1])) ? $tmp[1] : 'error',
  60. 'name_for_form' => $good[2],
  61. 'product_group' => $good[1],
  62. 'price' => $good[5],
  63. 'characteristics' => $good[4],
  64. 'tech_description' => $good[7],
  65. 'tech_description_short' => $good[8],
  66. 'image_path' => (!empty($images)) ? $images[0] : $images,
  67. ];
  68. $a = Product::query()->updateOrCreate(['article' => $tmp[0]], $data);
  69. if ($a->wasRecentlyCreated) $created++; else $updated++;
  70. //echo $i++ . '. Серия: ' . $series . ', артикул: ' . $tmp[0] . '<br>';
  71. $i++;
  72. if ($data['image_path'] == '') $no_image[] = $tmp[0];
  73. $info = ['count' => $i, 'updated' => $updated, 'created' => $created, 'no_image' => $no_image];
  74. $f = fopen($tmpfile, 'w+');
  75. fwrite($f, serialize($info));
  76. fclose($f);
  77. }
  78. $info['finished'] = true;
  79. $f = fopen($tmpfile, 'w+');
  80. fwrite($f, serialize($info));
  81. fclose($f);
  82. }
  83. private $allfiles; // remember files list
  84. private function find_images($article)
  85. {
  86. $path = base_path('www/') . env('IMAGES_PATH', '---') . '/';
  87. if (!isset($this->allfiles) || empty($this->allfiles)) {
  88. $this->allfiles = scandir($path);
  89. }
  90. $images = [];
  91. foreach ($this->allfiles as $filename) {
  92. if ((mb_strpos($filename, $article) === 0) || (
  93. mb_strpos(Str::lower($filename), Str::slug($article)) === 0))
  94. $images[] = $filename;
  95. }
  96. return (!empty($images)) ? $images : '';
  97. }
  98. }