ProductController.php 5.8 KB

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