|
@@ -3,6 +3,7 @@
|
|
|
namespace App\Http\Controllers;
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Http\Requests\SaveProductRequest;
|
|
use App\Http\Requests\SaveProductRequest;
|
|
|
|
|
+use App\Jobs\ImportXlsxJob;
|
|
|
use App\Models\Product;
|
|
use App\Models\Product;
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
@@ -51,59 +52,28 @@ class ProductController extends Controller
|
|
|
return view('products.index', $data);
|
|
return view('products.index', $data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // todo вынести в job, чтобы работала в фоне
|
|
|
|
|
|
|
+
|
|
|
public function upload_xls(Request $request)
|
|
public function upload_xls(Request $request)
|
|
|
{
|
|
{
|
|
|
- $xls = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($request->file('file'));
|
|
|
|
|
- $xls->setReadDataOnly(true);
|
|
|
|
|
- $sheet = $xls->load($request->file('file'));
|
|
|
|
|
-
|
|
|
|
|
- // read xls to array
|
|
|
|
|
- $goods = $sheet->setActiveSheetIndex(0)->toArray();
|
|
|
|
|
- unset($sheet, $xls);
|
|
|
|
|
-
|
|
|
|
|
- $series = '';
|
|
|
|
|
- $i = 0;
|
|
|
|
|
- $created = 0;
|
|
|
|
|
- $updated = 0;
|
|
|
|
|
- $no_image = [];
|
|
|
|
|
- foreach ($goods as $good) {
|
|
|
|
|
- // check first line and skip it
|
|
|
|
|
- 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])) {
|
|
|
|
|
- $series = $good[3];
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- $tmp = explode("\n", $good[3]);
|
|
|
|
|
- if (!isset($tmp[1])) {
|
|
|
|
|
- $good[3] = preg_replace('!\s+!', ' ', $good[3]);
|
|
|
|
|
- $tmp = explode(' ', $good[3], 2);
|
|
|
|
|
- }
|
|
|
|
|
- $images = $this->find_images($tmp[0]);
|
|
|
|
|
- $data = [
|
|
|
|
|
-// 'article' => $tmp[0],
|
|
|
|
|
- 'series' => $series,
|
|
|
|
|
- 'name' => (isset($tmp[1])) ? $tmp[1] : 'error',
|
|
|
|
|
- 'name_for_form' => $good[2],
|
|
|
|
|
- 'product_group' => $good[1],
|
|
|
|
|
- 'price' => $good[5],
|
|
|
|
|
- 'characteristics' => $good[4],
|
|
|
|
|
- 'tech_description' => $good[7],
|
|
|
|
|
- 'tech_description_short' => $good[8],
|
|
|
|
|
- 'image_path' => (!empty($images)) ? $images[0] : $images,
|
|
|
|
|
- ];
|
|
|
|
|
-
|
|
|
|
|
- $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];
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $file = $request->file('file');
|
|
|
|
|
+ $path = storage_path('import');
|
|
|
|
|
+ $filename = 'import_' . date('YmdHis') . '.' . $file->extension();
|
|
|
|
|
+ $file->move($path, $filename);
|
|
|
|
|
+
|
|
|
|
|
+ $info = ['count' => 0, 'updated' => 0, 'created' => 0, 'no_image' => []];
|
|
|
|
|
+ $f = fopen(storage_path('import/') . $filename . '.txt', 'w+');
|
|
|
|
|
+ fwrite($f, serialize($info));
|
|
|
|
|
+ fclose($f);
|
|
|
|
|
|
|
|
- return view('products.import_result', ['count' => $i, 'updated' => $updated, 'created' => $created, 'no_image' => $no_image]);
|
|
|
|
|
|
|
|
|
|
|
|
+ ImportXlsxJob::dispatch(storage_path('import/' . $filename));
|
|
|
|
|
+ return redirect()->route('import_result', ['filename' => $filename]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function import_result(Request $request){
|
|
|
|
|
+ $filename = $request->filename . '.txt';
|
|
|
|
|
+ $data = unserialize(file_get_contents(storage_path('import/') . $filename));
|
|
|
|
|
+ return view('products.import_result', $data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private $allfiles; // remember files list
|
|
private $allfiles; // remember files list
|
|
@@ -132,11 +102,8 @@ class ProductController extends Controller
|
|
|
|
|
|
|
|
public function save_product(SaveProductRequest $request)
|
|
public function save_product(SaveProductRequest $request)
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
Product::query()->where('id', $request->validated('id'))->update($request->validated());
|
|
Product::query()->where('id', $request->validated('id'))->update($request->validated());
|
|
|
return redirect($request->prev_url);
|
|
return redirect($request->prev_url);
|
|
|
-// return redirect()->route('index');
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function upload_image(Request $request){
|
|
public function upload_image(Request $request){
|
|
@@ -158,7 +125,19 @@ class ProductController extends Controller
|
|
|
public function select_export(Request $request){
|
|
public function select_export(Request $request){
|
|
|
$data['ids'] = json_decode($request->ids, true);
|
|
$data['ids'] = json_decode($request->ids, true);
|
|
|
$data['products'] = Product::query()->whereIn('id', $data['ids'])->get();
|
|
$data['products'] = Product::query()->whereIn('id', $data['ids'])->get();
|
|
|
|
|
+ $data['templates'] = $this->get_templates();
|
|
|
return view('products.select_export', $data);
|
|
return view('products.select_export', $data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private function get_templates(){
|
|
|
|
|
+ $files = scandir(storage_path('templates'));
|
|
|
|
|
+ $result = [];
|
|
|
|
|
+ foreach ($files as $f){
|
|
|
|
|
+ if(Str::endsWith($f, '.docx'))
|
|
|
|
|
+ $result[] = $f;
|
|
|
|
|
+ }
|
|
|
|
|
+ return$result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|