GenerateDocumentsService.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Order;
  4. use Exception;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Support\Str;
  7. class GenerateDocumentsService
  8. {
  9. const INSTALL_FILENAME = 'Монт аж_';
  10. const HANDOVER_FILENAME = 'Сдача_';
  11. /**
  12. * @param Order $order
  13. * @param int $userId
  14. * @return string
  15. * @throws Exception
  16. */
  17. public function generateInstallationPack(Order $order, int $userId): string
  18. {
  19. $techDocsPath = base_path('/tech-docs/');
  20. $products_sku = $order->products_sku;
  21. $articles = [];
  22. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp/Схемы сборки');
  23. foreach ($products_sku as $sku) {
  24. if(!in_array($sku->product->article, $articles)) {
  25. $articles[] = $sku->product->article;
  26. // find and copy scheme files to installation directory
  27. if(file_exists($techDocsPath . $sku->product->article . '/')) {
  28. foreach(Storage::disk('base')->allFiles('tech-docs/' . $sku->product->article) as $p) {
  29. $content = Storage::disk('base')->get($p);
  30. Storage::disk('public')->put('orders/' . $order->id . '/tmp/Схемы сборки/' . basename($p), $content);
  31. }
  32. }
  33. }
  34. // generate xlsx order file
  35. // ...
  36. // create zip archive
  37. $fileModel = (new FileService())->createZipArchive('orders/' . $order->id . '/tmp', self::INSTALL_FILENAME . $order->common_name . '.zip', $userId);
  38. // remove temp files
  39. Storage::disk('public')->deleteDirectory('orders/' . $order->id . '/tmp');
  40. $order->documents()->syncWithoutDetaching($fileModel);
  41. // return link
  42. return $fileModel->link;
  43. }
  44. }
  45. }