Browse Source

Added accepts filters and search on export

Alexander Musikhin 9 months ago
parent
commit
5baf558421

+ 6 - 5
app/Http/Controllers/ProductController.php

@@ -11,7 +11,6 @@ use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Str;
 use Illuminate\Support\Str;
-use function PHPUnit\Framework\isString;
 
 
 class ProductController extends Controller
 class ProductController extends Controller
 {
 {
@@ -112,12 +111,12 @@ class ProductController extends Controller
                 }
                 }
 
 
                 if(Str::endsWith($filterName, '_from')) {
                 if(Str::endsWith($filterName, '_from')) {
-                    if(isString($filterValue) && DateHelper::isDate($filterValue)) {
+                    if(is_string($filterValue) && DateHelper::isDate($filterValue)) {
                         $filterValue .= ' 00:00:00';
                         $filterValue .= ' 00:00:00';
                     }
                     }
                     $q->where(Str::replace('_from', '', $filterName), '>=', $filterValue);
                     $q->where(Str::replace('_from', '', $filterName), '>=', $filterValue);
                 } elseif(Str::endsWith($filterName, '_to')) {
                 } elseif(Str::endsWith($filterName, '_to')) {
-                    if(isString($filterValue) && DateHelper::isDate($filterValue)) {
+                    if(is_string($filterValue) && DateHelper::isDate($filterValue)) {
                         $filterValue .= ' 23:59:59';
                         $filterValue .= ' 23:59:59';
                     }
                     }
                     $q->where(Str::replace('_to', '', $filterName), '<=', $filterValue);
                     $q->where(Str::replace('_to', '', $filterName), '<=', $filterValue);
@@ -188,13 +187,15 @@ class ProductController extends Controller
     public function export(Request $request)
     public function export(Request $request)
     {
     {
         $request->validate([
         $request->validate([
-            'withFilter' => 'nullable',
+            'withFilter'    => 'nullable',
+            'filters'       => 'nullable|array',
         ]);
         ]);
 
 
         // load and save file
         // load and save file
+        $filters = ($request->withFilter) ? $request->filters : [];
 
 
         // dispatch job
         // dispatch job
-        ExportCatalog::dispatch([], $request->user()->id);
+        ExportCatalog::dispatch($filters, $request->user()->id);
         Log::info('ImportCatalog job created!');
         Log::info('ImportCatalog job created!');
 
 
 
 

+ 40 - 1
app/Services/ExportService.php

@@ -2,7 +2,10 @@
 
 
 namespace App\Services;
 namespace App\Services;
 
 
+use App\Helpers\DateHelper;
 use App\Models\Product;
 use App\Models\Product;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Str;
 use PhpOffice\PhpSpreadsheet\IOFactory;
 use PhpOffice\PhpSpreadsheet\IOFactory;
 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
 
 
@@ -12,7 +15,43 @@ class ExportService
 
 
     public function handle(array $filters = []): string
     public function handle(array $filters = []): string
     {
     {
-        $products = Product::cursor();
+        $q = Product::query();
+
+        foreach ($filters as $filterName => $filterValue) {
+            if(!$filterValue) continue;
+
+            if($filterName === 's') {
+                // accept search
+                $searchFields = ['nomenclature_number', 'article', 'name_tz', 'manufacturer_name', 'note'];
+                $q->where(function ($query) use ($searchFields, $filterValue) {
+                    foreach ($searchFields as $searchField) {
+                        $query->orWhere($searchField, 'LIKE', '%' . $filterValue . '%');
+                    }
+                });
+
+                continue;
+            }
+
+            if(Str::contains($filterName, 'price')) {
+                $filterValue = $filterValue * 100;
+            }
+
+            if(Str::endsWith($filterName, '_from')) {
+                if(is_string($filterValue) && DateHelper::isDate($filterValue)) {
+                    $filterValue .= ' 00:00:00';
+                }
+                $q->where(Str::replace('_from', '', $filterName), '>=', $filterValue);
+            } elseif(Str::endsWith($filterName, '_to')) {
+                if(is_string($filterValue) && DateHelper::isDate($filterValue)) {
+                    $filterValue .= ' 23:59:59';
+                }
+                $q->where(Str::replace('_to', '', $filterName), '<=', $filterValue);
+            } else {
+                $q->where($filterName, '=', $filterValue);
+            }
+        }
+
+        $products = $q->cursor();
         $inputFileType = 'Xlsx'; // Xlsx - Xml - Ods - Slk - Gnumeric - Csv
         $inputFileType = 'Xlsx'; // Xlsx - Xml - Ods - Slk - Gnumeric - Csv
         $inputFileName = './templates/ExportCatalogTemplate.xlsx';
         $inputFileName = './templates/ExportCatalogTemplate.xlsx';
 
 

+ 11 - 1
resources/views/catalog/index.blade.php

@@ -51,7 +51,7 @@
         </div>
         </div>
     </div>
     </div>
 
 
-    <!-- Модальное окно импорта-->
+    <!-- Модальное окно экспорта-->
     <div class="modal fade" id="exportModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
     <div class="modal fade" id="exportModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
         <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
         <div class="modal-dialog modal-fullscreen-sm-down modal-lg">
             <div class="modal-content">
             <div class="modal-content">
@@ -63,6 +63,16 @@
                     <form action="{{ route('catalog.export') }}" method="post" enctype="multipart/form-data">
                     <form action="{{ route('catalog.export') }}" method="post" enctype="multipart/form-data">
                         @csrf
                         @csrf
                         @include('partials.checkbox', ['title' => 'С учётом текущего фильтра и поиска', 'name' => 'withFilter', 'type' => 'checkbox', 'value' => 'yes', 'checked' => false])
                         @include('partials.checkbox', ['title' => 'С учётом текущего фильтра и поиска', 'name' => 'withFilter', 'type' => 'checkbox', 'value' => 'yes', 'checked' => false])
+                        <div class="d-none">
+                            @if(request()->s)
+                                @include('partials.input', ['name' => 'filters[s]', 'title' => 'поиск', 'value' => request()->s])
+                            @endif
+                            @if(request()->filters)
+                                @foreach(request()->filters as $filterName => $filterValue)
+                                    @include('partials.input', ['name' => 'filters[' . $filterName .']', 'title' => $filterName, 'value' => $filterValue])
+                                @endforeach
+                            @endif
+                        </div>
                         @include('partials.submit', ['name' => 'Экспорт'])
                         @include('partials.submit', ['name' => 'Экспорт'])
                     </form>
                     </form>
                 </div>
                 </div>