|
|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
+use App\Http\Requests\SaveProductRequest;
|
|
|
use App\Models\Product;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Str;
|
|
|
@@ -9,14 +10,46 @@ use Illuminate\Support\Str;
|
|
|
|
|
|
class ProductController extends Controller
|
|
|
{
|
|
|
- public function index(Request $request){
|
|
|
+ public function index(Request $request)
|
|
|
+ {
|
|
|
+ $data = $request->validate([
|
|
|
+ 's' => 'nullable|string',
|
|
|
+ 's_series' => 'nullable|string',
|
|
|
+ 's_price_min' => 'nullable|integer',
|
|
|
+ 's_price_max' => 'nullable|integer',
|
|
|
+ 'perpage' => 'nullable|string',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $query = Product::query();
|
|
|
+ if (!empty($data['s'])) {
|
|
|
+ $query->whereRaw(" (`article` LIKE '%{$data['s']}%' OR `name` LIKE '%{$data['s']}%' OR
|
|
|
+ `name_for_form` LIKE '%{$data['s']}%' OR `product_group` LIKE '%{$data['s']}%' OR
|
|
|
+ `characteristics` LIKE '%{$data['s']}%' OR `tech_description` LIKE '%{$data['s']}%'
|
|
|
+ OR `tech_description_short` LIKE '%{$data['s']}%') ");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($data['s_series'])) {
|
|
|
+ $query->where('series', '=', $data['s_series']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($data['s_price_min'])) {
|
|
|
+ $query->where('price', '>=', $data['s_price_min']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($data['s_price_max'])) {
|
|
|
+ $query->where('price', '<=', $data['s_price_max']);
|
|
|
+ }
|
|
|
|
|
|
- $data['products'] = Product::query()->paginate(20);
|
|
|
+ $data['products'] = $query->paginate($data['perpage'] ?? 20)->withQueryString();
|
|
|
+
|
|
|
+ // get all series for select field
|
|
|
$data['series'] = Product::query()->select('series')->distinct()->OrderBy('series')->get();
|
|
|
return view('products.index', $data);
|
|
|
}
|
|
|
|
|
|
- public function upload(Request $request){
|
|
|
+ // todo вынести в job, чтобы работала в фоне
|
|
|
+ public function upload(Request $request)
|
|
|
+ {
|
|
|
$xls = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($request->file('file'));
|
|
|
$xls->setReadDataOnly(true);
|
|
|
$sheet = $xls->load($request->file('file'));
|
|
|
@@ -26,21 +59,21 @@ class ProductController extends Controller
|
|
|
unset($sheet, $xls);
|
|
|
|
|
|
$series = '';
|
|
|
- $i = 1;
|
|
|
+ $i = 0;
|
|
|
$created = 0;
|
|
|
$updated = 0;
|
|
|
$no_image = [];
|
|
|
- foreach ($goods as $good){
|
|
|
+ foreach ($goods as $good) {
|
|
|
// check first line and skip it
|
|
|
- if($good[0] === '№п/п' && $good['3'] === 'Наименование') continue;
|
|
|
+ if ($good[0] === '№п/п' && $good['3'] === 'Наименование') continue;
|
|
|
|
|
|
// check the line is name of series
|
|
|
- if($good[0] == NULL && $good[1] == NULL && $good[2] == NULL && is_string($good[3])){
|
|
|
+ if ($good[0] == NULL && $good[1] == NULL && $good[2] == NULL && is_string($good[3])) {
|
|
|
$series = $good[3];
|
|
|
continue;
|
|
|
}
|
|
|
$tmp = explode("\n", $good[3]);
|
|
|
- if(!isset($tmp[1])){
|
|
|
+ if (!isset($tmp[1])) {
|
|
|
$good[3] = preg_replace('!\s+!', ' ', $good[3]);
|
|
|
$tmp = explode(' ', $good[3], 2);
|
|
|
}
|
|
|
@@ -58,32 +91,45 @@ class ProductController extends Controller
|
|
|
'image_path' => $this->find_image($tmp[0]),
|
|
|
];
|
|
|
|
|
|
- $a= Product::query()->updateOrCreate(['article' => $tmp[0]], $data);
|
|
|
- if($a->wasRecentlyCreated) $created++; else $updated++;
|
|
|
+ $a = Product::query()->updateOrCreate(['article' => $tmp[0]], $data);
|
|
|
+ if ($a->wasRecentlyCreated) $created++; else $updated++;
|
|
|
//echo $i++ . '. Серия: ' . $series . ', артикул: ' . $tmp[0] . '<br>';
|
|
|
$i++;
|
|
|
- if($data['image_path'] == '') $no_image[] = $tmp[0];
|
|
|
+ if ($data['image_path'] == '') $no_image[] = $tmp[0];
|
|
|
}
|
|
|
|
|
|
return view('products.import_result', ['count' => $i, 'updated' => $updated, 'created' => $created, 'no_image' => $no_image]);
|
|
|
|
|
|
}
|
|
|
|
|
|
- private $allfiles; // rememer files list
|
|
|
+ private $allfiles; // remember files list
|
|
|
|
|
|
- private function find_image($article){
|
|
|
+ private function find_image($article)
|
|
|
+ {
|
|
|
$path_to_files = './' . env('IMAGES_PATH', '---') . '/';
|
|
|
- if(!isset($this->allfiles) || empty($this->allfiles)){
|
|
|
+ if (!isset($this->allfiles) || empty($this->allfiles)) {
|
|
|
$this->allfiles = scandir($path_to_files);
|
|
|
}
|
|
|
- foreach ($this->allfiles as $filename){
|
|
|
- if((mb_strpos($filename, $article) !== false) || (
|
|
|
- mb_strpos(Str::lower($filename), Str::slug($article)) !== false
|
|
|
- ))
|
|
|
+ foreach ($this->allfiles as $filename) {
|
|
|
+ if ((mb_strpos($filename, $article) === 0) || (
|
|
|
+ mb_strpos(Str::lower($filename), Str::slug($article)) === 0))
|
|
|
return $filename;
|
|
|
}
|
|
|
|
|
|
return '';
|
|
|
}
|
|
|
|
|
|
+ public function product(Request $request, $id)
|
|
|
+ {
|
|
|
+ $data['product'] = Product::query()->findOrFail($id);
|
|
|
+ return view('products.product', $data);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function save_product(SaveProductRequest $request)
|
|
|
+ {
|
|
|
+ $p = Product::query()->where('id', $request->validated('id'))->update($request->validated());
|
|
|
+
|
|
|
+ return redirect()->route('index');
|
|
|
+ }
|
|
|
+
|
|
|
}
|