GenerateDocumentsService.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. }
  35. // generate xlsx order file
  36. // ...
  37. // create zip archive
  38. $fileModel = (new FileService())->createZipArchive('orders/' . $order->id . '/tmp', self::INSTALL_FILENAME . $order->common_name . '.zip', $userId);
  39. // remove temp files
  40. Storage::disk('public')->deleteDirectory('orders/' . $order->id . '/tmp');
  41. $order->documents()->syncWithoutDetaching($fileModel);
  42. // return link
  43. return $fileModel?->link ?? '';
  44. }
  45. }