Преглед изворни кода

added tech docs to installation generation

Alexander Musikhin пре 7 месеци
родитељ
комит
86c5153b6f

+ 1 - 1
app/Http/Controllers/OrderController.php

@@ -316,7 +316,7 @@ class OrderController extends Controller
 
     public function generateInstallationPack(Order $order)
     {
-        GenerateInstallationPack::dispatch($order);
+        GenerateInstallationPack::dispatch($order, auth()->user()->id);
         return redirect()->route('order.show', $order)->with(['success' => 'Задача генерации документов создана!']);
     }
 

+ 4 - 9
app/Services/FileService.php

@@ -37,13 +37,12 @@ class FileService
      * @return File
      * @throws Exception
      */
-    public function createZipArchive(string $path, string $archiveName, int $userId = null, bool $deleteSourceFiles = false): File
+    public function createZipArchive(string $path, string $archiveName, int $userId = null): File
     {
 
         if (!($tempFile = tempnam(sys_get_temp_dir(), Str::random()))) {
             throw new Exception('Cant create temporary file!');
         }
-        var_dump(storage_path('app/public/' . $path));
 
         $zip = new ZipArchive();
         $fullPath = storage_path('app/public/' . $path);
@@ -56,20 +55,16 @@ class FileService
             if(!$file->isDir()) {
                 // Get real and relative path for current file
                 $filePath = $file->getRealPath();
-                var_dump($filePath);
                 $relativePath = substr($filePath, strlen($fullPath) + 1);
-                var_dump($relativePath);
                 // Add current file to archive
                 $zip->addFile($filePath, $relativePath);
-                if($deleteSourceFiles) {
-                    Storage::disk($file->disk)->delete($file->path);
-                    $file->delete();
-                }
+
             }
         }
         $zip->close();
+
         $fileModel = File::query()->updateOrCreate([
-            'link' => url('/storage/') . '/' . $path . '/' .$archiveName,
+            'link' => url('/storage/') . '/' . Str::replace('/tmp', '', $path) . '/' .$archiveName,
             'path' => $path . '/' .$archiveName,
             'user_id' => $userId ?? auth()->user()->id,
             'original_name' => $archiveName,

+ 18 - 5
app/Services/GenerateDocumentsService.php

@@ -5,9 +5,13 @@ namespace App\Services;
 use App\Models\Order;
 use Exception;
 use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Str;
 
 class GenerateDocumentsService
 {
+    const INSTALL_FILENAME = 'Монт аж_';
+    const HANDOVER_FILENAME = 'Сдача_';
+
     /**
      * @param Order $order
      * @param int $userId
@@ -16,23 +20,32 @@ class GenerateDocumentsService
      */
     public function generateInstallationPack(Order $order, int $userId): string
     {
+        $techDocsPath = base_path('/tech-docs/');
         $products_sku = $order->products_sku;
         $articles = [];
-        Storage::disk('public')->makeDirectory('orders/' . $order->id . '/installation/Схемы сборки');
+        Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp/Схемы сборки');
         foreach ($products_sku as $sku) {
             if(!in_array($sku->product->article, $articles)) {
                 $articles[] = $sku->product->article;
                 // find and copy scheme files to installation directory
-                // ...
+                if(file_exists($techDocsPath . $sku->product->article . '/')) {
+                    foreach(Storage::disk('base')->allFiles('tech-docs/' . $sku->product->article) as $p) {
+                        $content = Storage::disk('base')->get($p);
+                        Storage::disk('public')->put('orders/' . $order->id . '/tmp/Схемы сборки/' . basename($p), $content);
+                    }
+                }
             }
 
             // generate xlsx order file
             // ...
-
             // create zip archive
-            $fileModel = (new FileService())->createZipArchive('orders/' . $order->id . '/installation', 'Installation-' . $order->id . '.zip', $userId);
+            $fileModel = (new FileService())->createZipArchive('orders/' . $order->id . '/tmp', self::INSTALL_FILENAME . $order->common_name . '.zip', $userId);
+
+            // remove temp files
+            Storage::disk('public')->deleteDirectory('orders/' . $order->id . '/tmp');
+            $order->documents()->syncWithoutDetaching($fileModel);
 
-            // create link
+            // return link
             return $fileModel->link;
 
 

+ 6 - 0
config/filesystems.php

@@ -37,6 +37,12 @@ return [
             'throw' => false,
         ],
 
+        'base'  => [
+            'driver' => 'local',
+            'root' => base_path('/'),
+            'throw' => false,
+        ],
+
         'public' => [
             'driver' => 'local',
             'root' => storage_path('app/public'),

+ 4 - 0
resources/js/custom.js

@@ -22,6 +22,10 @@ $(document).ready(function () {
                 if(received.data.payload.download) {
                    document.location.href = '/storage/export/' + received.data.payload.download;
                 }
+                if(received.data.payload.link) {
+                    document.location.href = received.data.payload.link;
+                }
+
                 setTimeout(function () {
                         if (received.data.payload.error) {
                             $('.alerts').append('<div class="main-alert2 alert alert-danger" role="alert">' + received.data.message + '</div>');