ProductController.php 5.9 KB

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