GenerateDocumentsService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. <?php
  2. namespace App\Services;
  3. use App\Helpers\CountHelper;
  4. use App\Helpers\DateHelper;
  5. use App\Helpers\ExcelHelper;
  6. use App\Models\Contract;
  7. use App\Models\File;
  8. use App\Models\Order;
  9. use App\Models\ProductSKU;
  10. use App\Models\Reclamation;
  11. use App\Models\Setting;
  12. use App\Models\Ttn;
  13. use App\Models\User;
  14. use Exception;
  15. use Illuminate\Support\Collection;
  16. use Illuminate\Support\Facades\Storage;
  17. use Illuminate\Support\Str;
  18. use PhpOffice\PhpSpreadsheet\IOFactory;
  19. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  20. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  21. class GenerateDocumentsService
  22. {
  23. const INSTALL_FILENAME = 'Монтаж ';
  24. const HANDOVER_FILENAME = 'Сдача ';
  25. const RECLAMATION_FILENAME = 'Рекламация ';
  26. /**
  27. * @param Order $order
  28. * @param int $userId
  29. * @return string
  30. * @throws Exception
  31. */
  32. public function generateInstallationPack(Order $order, int $userId): string
  33. {
  34. $techDocsPath = base_path('/tech-docs/');
  35. $order = Order::query()
  36. ->withoutGlobalScopes()
  37. ->whereKey($order->id)
  38. ->where('year', $order->year)
  39. ->with('documents')
  40. ->firstOrFail();
  41. $products_sku = $order->products_sku()
  42. ->withoutGlobalScopes()
  43. ->where('year', $order->year)
  44. ->get();
  45. $order->setRelation('products_sku', $products_sku);
  46. $articles = [];
  47. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp/Схемы сборки/');
  48. foreach ($products_sku as $sku) {
  49. if (!in_array($sku->product->article, $articles)) {
  50. $articles[] = $sku->product->article;
  51. // find and copy scheme files to installation directory
  52. if (file_exists($techDocsPath . $sku->product->article . '/')) {
  53. foreach (Storage::disk('base')->allFiles('tech-docs/' . $sku->product->article) as $p) {
  54. $content = Storage::disk('base')->get($p);
  55. Storage::disk('public')->put('orders/' . $order->id . '/tmp/Схемы сборки/' . basename($p), $content);
  56. }
  57. }
  58. }
  59. }
  60. $orderDocumentsDir = 'orders/' . $order->id . '/tmp/Документы площадки';
  61. foreach ($order->documents as $document) {
  62. if ($this->shouldSkipArchiveDocument($document)) {
  63. continue;
  64. }
  65. $this->copyFileToDir($document->path, $orderDocumentsDir, $document->original_name);
  66. }
  67. // generate xlsx order file
  68. $this->generateOrderForMount($order);
  69. // create zip archive
  70. $fileModel = (new FileService())->createZipArchive('orders/' . $order->id . '/tmp', self::INSTALL_FILENAME . fileName($order->common_name) . '.zip', $userId);
  71. // remove temp files
  72. Storage::disk('public')->deleteDirectory('orders/' . $order->id . '/tmp');
  73. $order->documents()->syncWithoutDetaching($fileModel);
  74. // return link
  75. return $fileModel?->link ?? '';
  76. }
  77. private function generateOrderForMount(Order $order): void
  78. {
  79. $inputFileType = 'Xlsx'; // Xlsx - Xml - Ods - Slk - Gnumeric - Csv
  80. $inputFileName = './templates/OrderForMount.xlsx';
  81. $reader = IOFactory::createReader($inputFileType);
  82. $spreadsheet = $reader->load($inputFileName);
  83. $sheet = $spreadsheet->getActiveSheet();
  84. // менеджер
  85. $sheet->setCellValue('F8', $order->user->name);
  86. $sheet->setCellValue('X8', $order->user->phone);
  87. // округ и район
  88. $sheet->setCellValue('L10', $order->district->shortname);
  89. $sheet->setCellValue('W10', $order->area->name);
  90. if ($order->area->responsible) {
  91. // ответственный
  92. $sheet->setCellValue('C12', $order->area->responsible?->name
  93. . ', ' . $order->area->responsible?->phone . ', ' . $order->area->responsible?->post);
  94. } else {
  95. $sheet->setCellValue('C12', '');
  96. }
  97. // адрес
  98. $sheet->setCellValue('L14', $order->object_address);
  99. $str = Str::replace('<div>', '', $order->products_with_count);
  100. $str = Str::replace('</div>', "\n", $str);
  101. // дата монтажа
  102. $installationDate = ($order->installation_date) ? DateHelper::getHumanDate($order->installation_date, true) : '';
  103. $sheet->setCellValue('L15', $installationDate);
  104. // мафы
  105. $sheet->setCellValue('G33', Str::trim($str));
  106. //
  107. $fileName = 'Заявка на монтаж - ' . fileName($order->object_address) . '.xlsx';
  108. $writer = new Xlsx($spreadsheet);
  109. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp');
  110. $writer->save(storage_path('app/public/orders/') . $order->id . '/tmp/' . $fileName);
  111. }
  112. /**
  113. * @throws Exception
  114. */
  115. public function generateHandoverPack(Order $order, int $userId): string
  116. {
  117. $productsSku = $order->products_sku()
  118. ->withoutGlobalScopes()
  119. ->where('year', $order->year)
  120. ->get();
  121. $order->setRelation('products_sku', $productsSku);
  122. $articles = [];
  123. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp/ПАСПОРТ/');
  124. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp/СЕРТИФИКАТ/');
  125. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp/ФОТО ПСТ/');
  126. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp/ФОТО ТН/');
  127. // copy app photos
  128. foreach ($order->photos as $photo) {
  129. $from = $photo->path;
  130. $to = 'orders/' . $order->id . '/tmp/ФОТО ПСТ/' . $photo->original_name;
  131. if (!Storage::disk('public')->exists($to)) {
  132. Storage::disk('public')->copy($from, $to);
  133. }
  134. }
  135. foreach ($productsSku as $sku) {
  136. // copy certificates
  137. if ($sku->product->certificate_id) {
  138. $from = $sku->product->certificate->path;
  139. $to = 'orders/' . $order->id . '/tmp/СЕРТИФИКАТ/' . $sku->product->certificate->original_name;
  140. if (!Storage::disk('public')->exists($to)) {
  141. Storage::disk('public')->copy($from, $to);
  142. }
  143. }
  144. // copy passport
  145. if ($sku->passport_id) {
  146. $from = $sku->passport->path;
  147. $to = 'orders/' . $order->id . '/tmp/ПАСПОРТ/';
  148. if (!Storage::disk('public')->exists($to . $sku->passport->original_name)) {
  149. $f = Storage::disk('public')->get($from);
  150. $ext = \File::extension($sku->passport->original_name);
  151. $targetName = $to . 'Паспорт ' . $sku->factory_number . ' арт. ' . $sku->product->article . '.' . $ext;
  152. Storage::disk('public')->put($targetName, $f);
  153. }
  154. }
  155. }
  156. // generate xlsx order files
  157. $this->generateStatement($order);
  158. $this->generateQualityDeclaration($order);
  159. $this->generateInventory($order);
  160. $this->generatePassport($order);
  161. // create zip archive
  162. $fileModel = (new FileService())->createZipArchive('orders/' . $order->id . '/tmp', self::HANDOVER_FILENAME . fileName($order->common_name) . '.zip', $userId);
  163. // remove temp files
  164. Storage::disk('public')->deleteDirectory('orders/' . $order->id . '/tmp');
  165. $order->documents()->syncWithoutDetaching($fileModel);
  166. // return link
  167. return $fileModel?->link ?? '';
  168. }
  169. private function generateStatement(Order $order): void
  170. {
  171. $inputFileType = 'Xlsx';
  172. $inputFileName = './templates/Statement.xlsx';
  173. $reader = IOFactory::createReader($inputFileType);
  174. $spreadsheet = $reader->load($inputFileName);
  175. $sheet = $spreadsheet->getActiveSheet();
  176. $contract = Contract::query()->where('contracts.year', $order->year)->first();
  177. $contract_number = $contract->contract_number ?? 'заполнить договор за ' . $order->year . ' год!';
  178. $contract_date = DateHelper::getHumanDate($contract->contract_date ?? '1970-01-01', true);
  179. $s = 'по Договору №' . $contract_number . ' от ' . $contract_date . ' г. Между ГБУ "Мосремонт" и ООО "НАШ ДВОР-СТ"';
  180. $sheet->setCellValue('B5', $s);
  181. // менеджер
  182. $sheet->setCellValue('G21', $order->user->name);
  183. // округ и район
  184. $sheet->setCellValue('C6', $order->district->shortname);
  185. $sheet->setCellValue('F6', $order->area->name);
  186. $i = 9; // start of table
  187. $nn = 1; // string number
  188. foreach ($order->products_sku as $sku) {
  189. if ($nn > 1) { // inset row
  190. $sheet->insertNewRowBefore($i);
  191. }
  192. $sheet->setCellValue('A' . $i, $nn++);
  193. $sheet->setCellValue('B' . $i, $sku->product->statement_name);
  194. $sheet->setCellValue('C' . $i, $sku->product->passport_name);
  195. $sheet->setCellValue('D' . $i, 'шт');
  196. $sheet->setCellValue('E' . $i, '1');
  197. $sheet->setCellValue('F' . $i, $order->name);
  198. $sheet->setCellValue('G' . $i, $sku->factory_number);
  199. $sheet->setCellValue('H' . $i++, $sku->rfid);
  200. }
  201. // save file
  202. $fileName = '1.Ведомость.xlsx';
  203. $writer = new Xlsx($spreadsheet);
  204. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp');
  205. $writer->save(storage_path('app/public/orders/') . $order->id . '/tmp/' . $fileName);
  206. }
  207. private function generateQualityDeclaration(Order $order): void
  208. {
  209. $inputFileType = 'Xlsx';
  210. $inputFileName = './templates/QualityDeclaration.xlsx';
  211. $reader = IOFactory::createReader($inputFileType);
  212. $spreadsheet = $reader->load($inputFileName);
  213. $sheet = $spreadsheet->getActiveSheet();
  214. $address = 'г. Москва, ' . $order->district->shortname . ', район ' . $order->area->name . ', по адресу: ' . $order->object_address;
  215. $i = 1; // start of table
  216. $n = 1;
  217. foreach ($order->products_sku as $sku) {
  218. if ($n++ > 1) {
  219. $range = 'A' . $i . ':I' . $i + 56;
  220. $i = $i + 57;
  221. ExcelHelper::copyRows($sheet, $range, 'A' . $i);
  222. // add header img
  223. $drawing = new Drawing();
  224. $drawing->setName('Header');
  225. $drawing->setDescription('Header');
  226. $drawing->setPath('templates/header.png');
  227. $drawing->setCoordinates('A' . $i);
  228. $drawing->setOffsetX(16);
  229. $drawing->setOffsetY(8);
  230. $drawing->setResizeProportional(true);
  231. $drawing->setWidth(680);
  232. $drawing->setWorksheet($sheet);
  233. }
  234. // document date?
  235. $sheet->setCellValue('A' . $i + 7, DateHelper::getHumanDate(date('Y-m-d'), true));
  236. $sheet->setCellValue('B' . $i + 16, $sku->product->passport_name);
  237. $sheet->setCellValue('C' . $i + 18, $sku->rfid);
  238. $sheet->setCellValue('C' . $i + 20, $address);
  239. // add page break and copy prev page
  240. }
  241. // save file
  242. $fileName = '2.Декларация качества.xlsx';
  243. $writer = new Xlsx($spreadsheet);
  244. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp');
  245. $writer->save(storage_path('app/public/orders/') . $order->id . '/tmp/' . $fileName);
  246. }
  247. private function generateInventory(Order $order): void
  248. {
  249. $inputFileType = 'Xlsx';
  250. $inputFileName = './templates/Inventory.xlsx';
  251. $reader = IOFactory::createReader($inputFileType);
  252. $spreadsheet = $reader->load($inputFileName);
  253. $sheet = $spreadsheet->getActiveSheet();
  254. $address = 'Округ: ' . $order->district->shortname . ' Район ' . $order->area->name;
  255. $sheet->setCellValue('C4', $order->name);
  256. $sheet->setCellValue('C5', $address);
  257. $sheet->setCellValue('C13', $order->user->name);
  258. $i = 8; // start of table
  259. $n = 1;
  260. foreach ($order->products_sku as $sku) {
  261. if ($n++ > 1) {
  262. $sheet->insertNewRowBefore($i + 3, 3);
  263. $range = 'A' . $i . ':E' . $i + 2;
  264. $i = $i + 3;
  265. ExcelHelper::copyRows($sheet, $range, 'A' . $i);
  266. }
  267. $sheet->setCellValue('A' . $i, $n - 1);
  268. $sheet->setCellValue('B' . $i, $sku->product->passport_name);
  269. $sheet->setCellValue('B' . $i + 2, $sku->rfid);
  270. }
  271. // save file
  272. $fileName = '3.Опись.xlsx';
  273. $writer = new Xlsx($spreadsheet);
  274. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp');
  275. $writer->save(storage_path('app/public/orders/') . $order->id . '/tmp/' . $fileName);
  276. }
  277. private function generatePassport(Order $order): void
  278. {
  279. $inputFileType = 'Xlsx';
  280. $inputFileName = './templates/Passport.xlsx';
  281. $reader = IOFactory::createReader($inputFileType);
  282. $spreadsheet = $reader->load($inputFileName);
  283. $sheet = $spreadsheet->getActiveSheet();
  284. $i = 3; // start of table
  285. $n = 1;
  286. foreach ($order->products_sku as $sku) {
  287. if ($n++ > 1) {
  288. $sheet->insertNewRowBefore($i + 1, 1);
  289. $range = 'A' . $i . ':J' . $i;
  290. $i++;
  291. ExcelHelper::copyRows($sheet, $range, 'A' . $i);
  292. }
  293. $sheet->setCellValue('A' . $i, $n - 1);
  294. $sheet->setCellValue('B' . $i, $sku->product->passport_name);
  295. $sheet->setCellValue('C' . $i, $sku->product->type_tz);
  296. $sheet->setCellValue('D' . $i, $sku->rfid);
  297. $sheet->setCellValue('E' . $i, $sku->product->certificate_number);
  298. $sheet->setCellValue('G' . $i, $sku->factory_number);
  299. $sheet->setCellValue('H' . $i, DateHelper::getHumanDate($sku->manufacture_date, true));
  300. }
  301. // save file
  302. $fileName = '4.Паспорт объекта - ' . fileName($order->object_address) . '.xlsx';
  303. $writer = new Xlsx($spreadsheet);
  304. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp');
  305. $writer->save(storage_path('app/public/orders/') . $order->id . '/tmp/' . $fileName);
  306. }
  307. /**
  308. * @param Reclamation $reclamation
  309. * @param int $userId
  310. * @return string
  311. * @throws Exception
  312. */
  313. public function generateReclamationPack(Reclamation $reclamation, int $userId): string
  314. {
  315. Storage::disk('public')->makeDirectory('reclamations/' . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address) . '/ФОТО НАРУШЕНИЯ/');
  316. // copy photos
  317. foreach ($reclamation->photos_before as $photo) {
  318. $from = $photo->path;
  319. $to = 'reclamations/' . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address) . '/ФОТО НАРУШЕНИЯ/' . $photo->original_name;
  320. if (!Storage::disk('public')->exists($to)) {
  321. Storage::disk('public')->copy($from, $to);
  322. }
  323. }
  324. // create xls and pdf
  325. $this->generateReclamationOrder($reclamation);
  326. $this->generateReclamationAct($reclamation);
  327. $this->generateReclamationGuarantee($reclamation);
  328. // create zip archive
  329. $fileModel = (new FileService())->createZipArchive('reclamations/' . $reclamation->id . '/tmp', self::RECLAMATION_FILENAME . fileName($reclamation->order->object_address) . '.zip', $userId);
  330. // remove temp files
  331. Storage::disk('public')->deleteDirectory('reclamations/' . $reclamation->id . '/tmp');
  332. $reclamation->documents()->syncWithoutDetaching($fileModel);
  333. // return link
  334. return $fileModel?->link ?? '';
  335. }
  336. /**
  337. * @throws Exception
  338. */
  339. public function generateReclamationPaymentPack(Reclamation $reclamation, int $userId): string
  340. {
  341. $reclamation->loadMissing([
  342. 'order.statements',
  343. 'documents',
  344. 'acts',
  345. 'photos_before',
  346. 'photos_after',
  347. ]);
  348. $tmpRoot = 'reclamations/' . $reclamation->id . '/tmp';
  349. $baseDir = $tmpRoot . '/Пакет документов на оплату - ' . fileName($reclamation->order->object_address);
  350. $beforeDir = $baseDir . '/Фотографии проблемы';
  351. $afterDir = $baseDir . '/Фотографии после устранения';
  352. Storage::disk('public')->makeDirectory($beforeDir);
  353. Storage::disk('public')->makeDirectory($afterDir);
  354. $this->copyPhotosWithEmptyFallback($reclamation->photos_before, $beforeDir);
  355. $this->copyPhotosWithEmptyFallback($reclamation->photos_after, $afterDir);
  356. foreach ($reclamation->documents as $document) {
  357. if ($this->shouldSkipDocumentForPaymentPack($document)) {
  358. continue;
  359. }
  360. $this->copyFileToDir($document->path, $baseDir, $document->original_name);
  361. }
  362. foreach ($reclamation->acts as $act) {
  363. $this->copyFileToDir($act->path, $baseDir, $act->original_name);
  364. }
  365. foreach ($reclamation->order?->statements ?? [] as $statement) {
  366. $this->copyFileToDir($statement->path, $baseDir, $statement->original_name);
  367. }
  368. $archiveName = 'Пакет документов на оплату - ' . fileName($reclamation->order->object_address) . '.zip';
  369. $fileModel = (new FileService())->createZipArchive($tmpRoot, $archiveName, $userId);
  370. Storage::disk('public')->deleteDirectory($tmpRoot);
  371. $reclamation->documents()->syncWithoutDetaching($fileModel);
  372. return $fileModel?->link ?? '';
  373. }
  374. /**
  375. * @throws Exception
  376. */
  377. private function generateReclamationOrder(Reclamation $reclamation): void
  378. {
  379. $inputFileType = 'Xlsx';
  380. $inputFileName = './templates/ReclamationOrder.xlsx';
  381. $reader = IOFactory::createReader($inputFileType);
  382. $spreadsheet = $reader->load($inputFileName);
  383. $sheet = $spreadsheet->getActiveSheet();
  384. $articles = [];
  385. foreach ($reclamation->skus as $p) {
  386. $articles[] = $p->product->article;
  387. }
  388. $sheet->setCellValue('J4', DateHelper::getHumanDate($reclamation->create_date, true));
  389. $sheet->setCellValue('L10', $reclamation->order->common_name);
  390. $sheet->setCellValue('L11', $reclamation->order->area?->responsible?->name);
  391. $sheet->setCellValue('W11', $reclamation->order->area?->responsible?->phone);
  392. $sheet->setCellValue('L12', $reclamation->order->year);
  393. $sheet->setCellValue('G13', $reclamation->guarantee);
  394. $sheet->setCellValue('G14', implode(', ', $articles));
  395. $sheet->setCellValue('Y15', DateHelper::getHumanDate($reclamation->finish_date, true));
  396. $sheet->setCellValue('U20', DateHelper::getHumanDate($reclamation->create_date, true));
  397. // save file
  398. $fileName = 'Монтажная заявка - ' . fileName($reclamation->order->object_address) . '.xlsx';
  399. $writer = new Xlsx($spreadsheet);
  400. $fd = 'reclamations/' . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address);
  401. Storage::disk('public')->makeDirectory($fd);
  402. $fp = storage_path('app/public/reclamations/') . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address) . '/' . $fileName;
  403. Storage::disk('public')->delete($fd . '/' . $fileName);
  404. $writer->save($fp);
  405. PdfConverterClient::convert($fp);
  406. }
  407. /**
  408. * @throws Exception
  409. */
  410. private function generateReclamationAct(Reclamation $reclamation): void
  411. {
  412. $inputFileType = 'Xlsx';
  413. $inputFileName = './templates/ReclamationAct.xlsx';
  414. $reader = IOFactory::createReader($inputFileType);
  415. $spreadsheet = $reader->load($inputFileName);
  416. $sheet = $spreadsheet->getActiveSheet();
  417. $representativeId = Setting::getInt(Setting::KEY_RECLAMATION_ACT_REPRESENTATIVE_USER_ID);
  418. if ($representativeId) {
  419. $representative = User::query()->withTrashed()->find($representativeId);
  420. if ($representative) {
  421. $sheet->setCellValue('A14', 'службы сервиса ' . $representative->name);
  422. }
  423. }
  424. $mafs = [];
  425. foreach ($reclamation->skus as $p) {
  426. $mafs[] = $p->product->passport_name . ', тип ' . $p->product->nomenclature_number;
  427. }
  428. $sheet->setCellValue('A17', $reclamation->order->object_address);
  429. $sheet->setCellValue('A22', implode('; ', $mafs));
  430. $sheet->setCellValue('A27', $reclamation->whats_done);
  431. $i = 24;
  432. $n = 1;
  433. foreach ($reclamation->skus as $p) {
  434. if ($n++ > 1) {
  435. $i++;
  436. $sheet->insertNewRowBefore($i, 1);
  437. $range = 'D' . $i . ':I' . $i;
  438. $sheet->mergeCells($range);
  439. }
  440. $sheet->setCellValue('D' . $i, $p->rfid);
  441. }
  442. // save file
  443. $fileName = 'Акт - ' . fileName($reclamation->order->object_address) . '.xlsx';
  444. $writer = new Xlsx($spreadsheet);
  445. $fd = 'reclamations/' . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address);
  446. Storage::disk('public')->makeDirectory($fd);
  447. $fp = storage_path('app/public/reclamations/') . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address) . '/' . $fileName;
  448. Storage::disk('public')->delete($fd . '/' . $fileName);
  449. $writer->save($fp);
  450. PdfConverterClient::convert($fp);
  451. }
  452. /**
  453. * @throws Exception
  454. */
  455. private function generateReclamationGuarantee(Reclamation $reclamation): void
  456. {
  457. $inputFileType = 'Xlsx';
  458. $inputFileName = './templates/ReclamationGuarantee.xlsx';
  459. $reader = IOFactory::createReader($inputFileType);
  460. $spreadsheet = $reader->load($inputFileName);
  461. $sheet = $spreadsheet->getActiveSheet();
  462. $mafs = [];
  463. foreach ($reclamation->skus as $p) {
  464. $mafs[] = 'Тип ' . $p->product->nomenclature_number . ' (' . $p->product->passport_name . ')' ;
  465. }
  466. $contract = Contract::query()->where('contracts.year', $reclamation->order->year)->first();
  467. $text = "ООО «НАШ ДВОР-СТ» в рамках обязательств по Договору №{$contract?->contract_number}" .
  468. " от " . DateHelper::getHumanDate($contract?->contract_date ?? '1970-01-01', true) .
  469. " г. на выполнение комплекса работ по поставке, монтажу устанавливаемых на городских территориях малых архитектурных форм гарантирует " .
  470. $reclamation->guarantee . " на оборудовании «" . implode('; ', $mafs) .
  471. "» установленному по адресу г. Москва, " . $reclamation->order->object_address . " в срок до " .
  472. DateHelper::getHumanDate($reclamation->finish_date, true). " г. в связи с отсутствием детали в наличии и ее производством.";
  473. $sheet->setCellValue('B9', $reclamation->id);
  474. $sheet->setCellValue('D9', DateHelper::getHumanDate($reclamation->create_date, true));
  475. $sheet->setCellValue('A19', $text);
  476. // save file
  477. $fileName = 'Гарантийное письмо - ' . fileName($reclamation->order->object_address) . '.xlsx';
  478. $writer = new Xlsx($spreadsheet);
  479. $fd = 'reclamations/' . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address);
  480. Storage::disk('public')->makeDirectory($fd);
  481. $fp = storage_path('app/public/reclamations/') . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address) . '/' . $fileName;
  482. Storage::disk('public')->delete($fd . '/' . $fileName);
  483. $writer->save($fp);
  484. PdfConverterClient::convert($fp);
  485. }
  486. public function generateFilePack(Collection $files, int $userId, string $name = 'files'): \App\Models\File
  487. {
  488. $dir = Str::random(2);
  489. Storage::disk('public')->makeDirectory('files/' . $dir . '/tmp/');
  490. // copy files
  491. foreach ($files as $file) {
  492. $from = $file->path;
  493. $to = 'files/' . $dir . '/tmp/' . $file->original_name;
  494. if (!Storage::disk('public')->exists($to)) {
  495. Storage::disk('public')->copy($from, $to);
  496. }
  497. }
  498. // create zip archive
  499. $fileModel = (new FileService())->createZipArchive('files/' . $dir . '/tmp', $name .'_' . date('Y-m-d_h-i-s') . '.zip', $userId);
  500. // remove temp files
  501. Storage::disk('public')->deleteDirectory('files/' . $dir . '/tmp');
  502. // return link
  503. return $fileModel;
  504. }
  505. private function copyPhotosWithEmptyFallback(Collection $photos, string $targetDir): void
  506. {
  507. if ($photos->isEmpty()) {
  508. Storage::disk('public')->put(
  509. $targetDir . '/empty.txt',
  510. 'Данный файл создан для возможности создания пустой папки в архиве'
  511. );
  512. return;
  513. }
  514. foreach ($photos as $photo) {
  515. $this->copyFileToDir($photo->path, $targetDir, $photo->original_name);
  516. }
  517. }
  518. private function shouldSkipDocumentForPaymentPack($file): bool
  519. {
  520. return $this->shouldSkipArchiveDocument($file);
  521. }
  522. private function shouldSkipArchiveDocument($file): bool
  523. {
  524. if (!$file) {
  525. return true;
  526. }
  527. $fileName = Str::lower((string)$file->original_name);
  528. $mimeType = Str::lower((string)$file->mime_type);
  529. $archiveSuffixes = [
  530. '.zip',
  531. '.rar',
  532. '.7z',
  533. '.tar',
  534. '.tar.gz',
  535. '.tgz',
  536. '.tar.bz2',
  537. '.tbz2',
  538. '.bz2',
  539. '.tar.xz',
  540. '.txz',
  541. '.xz',
  542. ];
  543. if (Str::endsWith($fileName, $archiveSuffixes)) {
  544. return true;
  545. }
  546. if (str_contains($mimeType, 'zip') || str_contains($mimeType, 'compressed') || str_contains($mimeType, '7z') || str_contains($mimeType, 'rar')) {
  547. return true;
  548. }
  549. return false;
  550. }
  551. private function copyFileToDir(string $fromPath, string $targetDir, string $originalName): void
  552. {
  553. if (!Storage::disk('public')->exists($fromPath)) {
  554. return;
  555. }
  556. $safeName = $this->uniqueFileName($targetDir, $originalName);
  557. $to = $targetDir . '/' . $safeName;
  558. if (!Storage::disk('public')->exists($to)) {
  559. Storage::disk('public')->copy($fromPath, $to);
  560. }
  561. }
  562. private function uniqueFileName(string $dir, string $name): string
  563. {
  564. $base = pathinfo($name, PATHINFO_FILENAME);
  565. $ext = pathinfo($name, PATHINFO_EXTENSION);
  566. $extPart = $ext ? '.' . $ext : '';
  567. $candidate = $base . $extPart;
  568. $counter = 1;
  569. while (Storage::disk('public')->exists($dir . '/' . $candidate)) {
  570. $candidate = $base . ' (' . $counter . ')' . $extPart;
  571. $counter++;
  572. }
  573. return $candidate;
  574. }
  575. public function generateTtnPack(Ttn $ttn, int $userId): string
  576. {
  577. $skus = ProductSKU::query()->withoutGlobalScopes()->whereIn('id', json_decode($ttn->skus))->get();
  578. $volume = $weight = $places = 0;
  579. foreach ($skus as $sku) {
  580. if(!isset($order)) {
  581. $order = $sku->order;
  582. }
  583. $volume += $sku->product->volume;
  584. $weight += $sku->product->weight;
  585. $places += $sku->product->places;
  586. }
  587. $installationDate = ($order->installation_date) ? DateHelper::getHumanDate($order->installation_date, true) : '-';
  588. $ttnNumber = ($ttn->ttn_number_suffix) ? $ttn->ttn_number . '-' . $ttn->ttn_number_suffix : $ttn->ttn_number;
  589. $inputFileType = 'Xlsx';
  590. $inputFileName = './templates/Ttn.xlsx';
  591. $reader = IOFactory::createReader($inputFileType);
  592. $spreadsheet = $reader->load($inputFileName);
  593. $sheet = $spreadsheet->getActiveSheet();
  594. $sheet->setCellValue('D8', $installationDate);
  595. $sheet->setCellValue('R8', $ttnNumber);
  596. $sheet->setCellValue('AF8', DateHelper::getHumanDate($ttn->order_date, true));
  597. $sheet->setCellValue('AT8', $ttn->order_number);
  598. $sheet->setCellValue('B19', $order->object_address ?? '');
  599. $sheet->setCellValue('AD22', CountHelper::humanCount($places, 'место', 'места', 'мест') . ', способ упаковки: поддон, ящик, картон, полиэтилен');
  600. $sheet->setCellValue('B24', $weight . ' кг, ' . $volume . ' м.куб');
  601. $sheet->setCellValue('B36', $installationDate);
  602. $sheet->setCellValue('AB57', $installationDate . ' 8-00');
  603. $sheet->setCellValue('B59', $installationDate . ' 8-10');
  604. $sheet->setCellValue('AB59', $installationDate . ' 8-40');
  605. $sheet->setCellValue('B61', $weight . ' кг');
  606. $sheet->setCellValue('B63', CountHelper::humanCount($places, 'место', 'места', 'мест'));
  607. $sheet->setCellValue('B75', $order->object_address ?? '');
  608. $sheet->setCellValue('AB75', $installationDate);
  609. $sheet->setCellValue('B77', $installationDate);
  610. $sheet->setCellValue('AB77', $installationDate);
  611. $sheet->setCellValue('AB79', CountHelper::humanCount($places, 'место', 'места', 'мест'));
  612. $sheet->setCellValue('B81', $weight . ' кг');
  613. $sheet->setCellValue('B83', $order->brigadier?->name ?? '');
  614. $sheet->setCellValue('B89', $ttn->order_sum);
  615. $sheet->setCellValue('AD89', $ttn->order_sum);
  616. $sheet->setCellValue('AS89', $ttn->order_sum);
  617. // save file
  618. $fileName = 'ТН №' . $ttn->ttn_number . ' от ' . DateHelper::getHumanDate($ttn->order_date) . '.xlsx';
  619. $writer = new Xlsx($spreadsheet);
  620. $fd = 'ttn/' . $ttn->year;
  621. Storage::disk('public')->makeDirectory($fd);
  622. $fp = storage_path('app/public/ttn/') . $ttn->year . '/' . $fileName;
  623. Storage::disk('public')->delete($fd . '/' . $fileName);
  624. $writer->save($fp);
  625. $fileModel = File::query()->updateOrCreate([
  626. 'link' => url('/storage/') . '/ttn/' . $ttn->year . '/' .$fileName,
  627. 'path' => $fp . '/' .$fileName,
  628. 'user_id' => $userId,
  629. 'original_name' => $fileName,
  630. 'mime_type' => 'application/xlsx',
  631. ]);
  632. $ttn->file_id = $fileModel->id;
  633. $ttn->save();
  634. $order->documents()->attach($fileModel->id);
  635. return $fileModel->link ?? '';
  636. }
  637. }