| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- 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
- * @return string
- * @throws Exception
- */
- 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 . '/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 . '/tmp', self::INSTALL_FILENAME . $order->common_name . '.zip', $userId);
- // remove temp files
- Storage::disk('public')->deleteDirectory('orders/' . $order->id . '/tmp');
- $order->documents()->syncWithoutDetaching($fileModel);
- // return link
- return $fileModel->link;
- }
- }
- }
|