ProductController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\SaveProductRequest;
  4. use App\Models\Product;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Str;
  7. class ProductController extends Controller
  8. {
  9. public function index(Request $request)
  10. {
  11. $data = $request->validate([
  12. 's' => 'nullable|string',
  13. 's_series' => 'nullable|string',
  14. 's_price_min' => 'nullable|integer',
  15. 's_price_max' => 'nullable|integer',
  16. 'perpage' => 'nullable|string',
  17. ]);
  18. $query = Product::query();
  19. if (!empty($data['s'])) {
  20. $query->whereRaw(" (`article` LIKE '%{$data['s']}%' OR `name` LIKE '%{$data['s']}%' OR
  21. `name_for_form` LIKE '%{$data['s']}%' OR `product_group` LIKE '%{$data['s']}%' OR
  22. `characteristics` LIKE '%{$data['s']}%' OR `tech_description` LIKE '%{$data['s']}%'
  23. OR `tech_description_short` LIKE '%{$data['s']}%') ");
  24. }
  25. if (!empty($data['s_series'])) {
  26. $query->where('series', '=', $data['s_series']);
  27. }
  28. if (!empty($data['s_price_min'])) {
  29. $query->where('price', '>=', $data['s_price_min']);
  30. }
  31. if (!empty($data['s_price_max'])) {
  32. $query->where('price', '<=', $data['s_price_max']);
  33. }
  34. $data['products'] = $query->paginate($data['perpage'] ?? 20)->withQueryString();
  35. // get all series for select field
  36. $data['series'] = Product::query()->select('series')->distinct()->OrderBy('series')->get();
  37. return view('products.index', $data);
  38. }
  39. // todo вынести в job, чтобы работала в фоне
  40. public function upload(Request $request)
  41. {
  42. $xls = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($request->file('file'));
  43. $xls->setReadDataOnly(true);
  44. $sheet = $xls->load($request->file('file'));
  45. // read xls to array
  46. $goods = $sheet->setActiveSheetIndex(0)->toArray();
  47. unset($sheet, $xls);
  48. $series = '';
  49. $i = 0;
  50. $created = 0;
  51. $updated = 0;
  52. $no_image = [];
  53. foreach ($goods as $good) {
  54. // check first line and skip it
  55. if ($good[0] === '№п/п' && $good['3'] === 'Наименование') continue;
  56. // check the line is name of series
  57. if ($good[0] == NULL && $good[1] == NULL && $good[2] == NULL && is_string($good[3])) {
  58. $series = $good[3];
  59. continue;
  60. }
  61. $tmp = explode("\n", $good[3]);
  62. if (!isset($tmp[1])) {
  63. $good[3] = preg_replace('!\s+!', ' ', $good[3]);
  64. $tmp = explode(' ', $good[3], 2);
  65. }
  66. $data = [
  67. // 'article' => $tmp[0],
  68. 'series' => $series,
  69. 'name' => (isset($tmp[1])) ? $tmp[1] : 'error',
  70. 'name_for_form' => $good[2],
  71. 'product_group' => $good[1],
  72. 'price' => $good[5],
  73. 'characteristics' => $good[4],
  74. 'tech_description' => $good[7],
  75. 'tech_description_short' => $good[8],
  76. 'image_path' => $this->find_image($tmp[0]),
  77. ];
  78. $a = Product::query()->updateOrCreate(['article' => $tmp[0]], $data);
  79. if ($a->wasRecentlyCreated) $created++; else $updated++;
  80. //echo $i++ . '. Серия: ' . $series . ', артикул: ' . $tmp[0] . '<br>';
  81. $i++;
  82. if ($data['image_path'] == '') $no_image[] = $tmp[0];
  83. }
  84. return view('products.import_result', ['count' => $i, 'updated' => $updated, 'created' => $created, 'no_image' => $no_image]);
  85. }
  86. private $allfiles; // remember files list
  87. private function find_image($article)
  88. {
  89. $path_to_files = './' . env('IMAGES_PATH', '---') . '/';
  90. if (!isset($this->allfiles) || empty($this->allfiles)) {
  91. $this->allfiles = scandir($path_to_files);
  92. }
  93. foreach ($this->allfiles as $filename) {
  94. if ((mb_strpos($filename, $article) === 0) || (
  95. mb_strpos(Str::lower($filename), Str::slug($article)) === 0))
  96. return $filename;
  97. }
  98. return '';
  99. }
  100. public function product(Request $request, $id)
  101. {
  102. $data['product'] = Product::query()->findOrFail($id);
  103. return view('products.product', $data);
  104. }
  105. public function save_product(SaveProductRequest $request)
  106. {
  107. $p = Product::query()->where('id', $request->validated('id'))->update($request->validated());
  108. return redirect()->route('index');
  109. }
  110. }