Parcourir la source

+ Добавлены копейки
+ изменена верстка главной
+ выгрузка в фоне через job

Alexander Musikhin il y a 2 ans
Parent
commit
b6296f03a0

+ 21 - 3
app/Http/Controllers/ExportController.php

@@ -41,7 +41,7 @@ class ExportController extends Controller
                 'num#' . $i           => $i,
                 'name#' . $i          => $product->name,
                 'name_for_form#' . $i          => $product->name_for_form,
-                'price#' . $i         => number_format($product->price, 0, '', ' '),
+                'price#' . $i         => number_format($product->price, 2, '.', ' '),
                 'image#' . $i         => $product->image_path,
                 'description#' . $i   => str_replace("\n", '</w:t><w:br/><w:t xml:space="preserve">', $descr),
             ]);
@@ -51,12 +51,30 @@ class ExportController extends Controller
         // prepared vars - run job
 
         $filename = 'export_' . date('YmdHis') . '.docx';
-        $job = GenerateDocxJob::dispatch($vars_for_template, count($products), $filename);
+        GenerateDocxJob::dispatch($vars_for_template, count($products), $filename);
 
         Log::notice('Created job. Execution time: ' . microtime(true) - LARAVEL_START);
 
         $data['filename'] = $filename;
-        return view('products.wait', $data);
+        return redirect()->route('wait_export', $data);
+
+    }
+
+    public function wait_export(Request $request, $filename){
+        $fn = public_path('exported/docx/' . $filename . '.txt');
+        if(file_exists($fn)){
+            $data['percent'] = file_get_contents($fn);
+        } else {
+            $data['percent'] = 0;
+        }
 
+        $fn = public_path('exported/docx/' . $filename);
+        if(file_exists($fn)) {
+            $data['percent'] = 100;
+        }
+
+        $data['filename'] = $filename;
+
+        return view('products.wait', $data);
     }
 }

+ 2 - 2
app/Jobs/GenerateDocxJob.php

@@ -54,9 +54,9 @@ class GenerateDocxJob implements ShouldQueue
                 $my_template->setValue($k, $v);
             }
 
-            if(($i++ % 10) == 0) {
+            if(($i++ % 50) == 0) {
                 $f = fopen($tmpfile, 'w+');
-                $percent = $i / ($total / 100);
+                $percent = round($i / ($total / 100), 0);
                 fwrite($f, $percent);
                 fclose($f);
             }

+ 1 - 1
database/migrations/2023_01_26_032600_create_products_table.php

@@ -20,7 +20,7 @@ return new class extends Migration
             $table->string('name');
             $table->string('name_for_form')->default('');
             $table->string('product_group')->default('');
-            $table->unsignedBigInteger('price')->default(0);
+            $table->decimal('price', 15, 2, true)->default(0);
             $table->text('characteristics');
             $table->text('tech_description');
             $table->text('tech_description_short');

+ 15 - 11
resources/views/products/index.blade.php

@@ -65,15 +65,15 @@
                 <table class="table">
                     <thead>
                     <tr class="align-middle">
-                        <th class="text-center">Артикул, Серия<br>
+                        <th class="text-center">Артикул<br>
                             <label><input type="checkbox" class="check_all" onchange="toggle(this)">&nbsp;Все</label>
                         </th>
-                        <th>Группа <br> Наименование <br> Наименование под образец формы</th>
-                        <th>Цена</th>
+{{--                        <th>Группа <br> Наименование <br> Наименование под образец формы</th>--}}
+                        <th>Цена, руб.</th>
                         <th>Характеристики</th>
                         <th>Техническое описание</th>
-                        <th>Создан / Изменён</th>
                         <th>Изображение</th>
+                        <th>Создан / Изменён</th>
                     </thead>
                     </tr>
                     <tbody>
@@ -83,21 +83,24 @@
                                 <label>
                                     <input type="checkbox" onchange="select_product({{ $product->id }}, this)" class="form-check-inline me-0 prd-chk" name="ids" value="{{ $product->id }}"><br>
                                     {{ $product->article }}<br>
-                                    {{ $product->series }}
+
                                 </label>
                             </td>
 
+{{--                            <td>--}}
+{{--                                <a href="{{ route('view_product', $product->id) }}">--}}
+{{--                                    <strong>Группа:</strong> {{ $product->product_group }}<br>--}}
+{{--                                    <strong>Наименование:</strong> {{ $product->name }}<br>--}}
+{{--                                    <strong>Наименование под образец формы:</strong> {{ $product->name_for_form }}--}}
+{{--                                </a>--}}
+{{--                            </td>--}}
+                            <td>{!! number_format($product->price, 2, ',', '&nbsp') !!}</td>
                             <td>
                                 <a href="{{ route('view_product', $product->id) }}">
-                                    <strong>Группа:</strong> {{ $product->product_group }}<br>
-                                    <strong>Наименование:</strong> {{ $product->name }}<br>
-                                    <strong>Наименование под образец формы:</strong> {{ $product->name_for_form }}
+                                    {!! nl2br($product->characteristics) !!}
                                 </a>
                             </td>
-                            <td>{!! number_format($product->price, 2, ',', '&nbsp') !!}</td>
-                            <td>{!! nl2br($product->characteristics) !!}</td>
                             <td>{!! Str::words(nl2br($product->tech_description), 70) !!}</td>
-                            <td>{!! date('d.m.Y_H:i', strtotime($product->created_at)) . '<br>' .date('d.m.Y_H:i', strtotime($product->updated_at)) !!}</td>
                             <td class="text-center align-middle">
                                 @empty($product->image_path)
                                     Нет изображения
@@ -107,6 +110,7 @@
                                          alt="{{ $product->article }}">
                                 @endempty
                             </td>
+                            <td>{!! date('d.m.Y_H:i', strtotime($product->created_at)) . '<br>' .date('d.m.Y_H:i', strtotime($product->updated_at)) !!}</td>
                         </tr>
                     @endforeach
                     </tbody>

+ 2 - 2
resources/views/products/product.blade.php

@@ -71,9 +71,9 @@
                         </div>
                         <div class="col-6">
                             <label for="price" class="form-label">Цена</label>
-                            <input type="number" class="form-control @error('price') border-danger @enderror"
+                            <input type="number" step="0.01" class="form-control @error('price') border-danger @enderror"
                                    name="price" id="price"  {{ $disabled }}
-                                   value="{{ old('price', $product->price) }}">
+                                   value="{{ old('price', number_format($product->price, 2, '.', '')) }}">
                             @error('price')
                             <div class="form-text text-danger" id="titleError">
                                 {{ $message }}

+ 1 - 1
resources/views/products/select_export.blade.php

@@ -26,7 +26,7 @@
                         </div>
                     </div>
                     <input type="hidden" name="ids" value="{{ json_encode($ids) }}">
-                    <button type="submit" class="btn btn-primary">Экспорт в .docx</button>
+                    <button onclick="this.disabled()" type="submit" class="btn btn-primary">Экспорт в .docx</button>
                 </form>
 
             </div>

+ 16 - 16
resources/views/products/wait.blade.php

@@ -4,29 +4,29 @@
     <div class="container">
         <div class="row">
             <div class="col-12 text-center mt-5 fs-1">
-                <span id="percent">XX</span>%
+                Формирование документа: <span id="percent">{{ $percent }}</span>%
             </div>
         </div>
 
-        <div class="progress" role="progressbar" aria-label="Basic example" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
-            <div class="progress-bar" style="width: 0%"></div>
+        <div class="progress" role="progressbar" aria-label="Basic example" aria-valuenow="0" aria-valuemin="0"
+             aria-valuemax="100">
+            <div class="progress-bar" style="width: {{ $percent }}%"></div>
         </div>
+        @if($percent == 100)
+            <div class="col-12 my-5 fs-1 text-center">
+                Скачать <a href="/exported/docx/{{ $filename }}">{{ $filename }}</a>
+            </div>
+        @endif
 
     </div>
 
-    <script>
-        function check_export(){
-            let url = '/exported/docx/{{ $filename }}.txt';
-            fetch(url)
-                .then(response => response.text())
-                .then(percent => console.log(percent));
-        }
-
-        setInterval(check_export(), 1000);
-
-
-    </script>
-
+    @if($percent < 100)
+        <script>
+            setTimeout(function () {
+                window.location.reload();
+            }, 1000);
+        </script>
+    @endif
 
 @endsection
 

+ 1 - 0
routes/web.php

@@ -27,6 +27,7 @@ Route::middleware('mgrauth')->group(function (){
     Route::post('/update_image/{id}', [ProductController::class, 'update_image'])->name('update_image');
     Route::post('/select_export', [ProductController::class, 'select_export'])->name('select_export');
     Route::post('/export_docx', [ExportController::class, 'export_docx'])->name('export_docx');
+    Route::get('/wait_export/{filename}', [ExportController::class, 'wait_export'])->name('wait_export');
 });
 
 Route::get('logout', function (){

BIN
storage/template_maf.docx


+ 1 - 1
vite.config.js

@@ -5,7 +5,7 @@ import path from 'path'
 export default defineConfig({
     plugins: [
         laravel({
-	        publicDirectory: 'www',
+	    publicDirectory: 'www',
             input: [
                 'resources/sass/app.scss',
                 'resources/js/app.js',