GenerateDocumentsService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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. ->withoutGlobalScope(\App\Models\Scopes\YearScope::class)
  37. ->whereKey($order->id)
  38. ->where('year', $order->year)
  39. ->with('documents')
  40. ->firstOrFail();
  41. $products_sku = $order->products_sku()
  42. ->withoutGlobalScope(\App\Models\Scopes\YearScope::class)
  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. ->withoutGlobalScope(\App\Models\Scopes\YearScope::class)
  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. $sheet->setCellValue('J' . $i, $sku->product->service_life ?? 84);
  301. }
  302. // save file
  303. $fileName = '4.Паспорт объекта - ' . fileName($order->object_address) . '.xlsx';
  304. $writer = new Xlsx($spreadsheet);
  305. Storage::disk('public')->makeDirectory('orders/' . $order->id . '/tmp');
  306. $writer->save(storage_path('app/public/orders/') . $order->id . '/tmp/' . $fileName);
  307. }
  308. /**
  309. * @param Reclamation $reclamation
  310. * @param int $userId
  311. * @return string
  312. * @throws Exception
  313. */
  314. public function generateReclamationPack(Reclamation $reclamation, int $userId): string
  315. {
  316. Storage::disk('public')->makeDirectory('reclamations/' . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address) . '/ФОТО НАРУШЕНИЯ/');
  317. // copy photos
  318. foreach ($reclamation->photos_before as $photo) {
  319. $from = $photo->path;
  320. $to = 'reclamations/' . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address) . '/ФОТО НАРУШЕНИЯ/' . $photo->original_name;
  321. if (!Storage::disk('public')->exists($to)) {
  322. Storage::disk('public')->copy($from, $to);
  323. }
  324. }
  325. // create xls and pdf
  326. $this->generateReclamationOrder($reclamation);
  327. $this->generateReclamationAct($reclamation);
  328. $this->generateReclamationGuarantee($reclamation);
  329. // create zip archive
  330. $fileModel = (new FileService())->createZipArchive('reclamations/' . $reclamation->id . '/tmp', self::RECLAMATION_FILENAME . fileName($reclamation->order->object_address) . '.zip', $userId);
  331. // remove temp files
  332. Storage::disk('public')->deleteDirectory('reclamations/' . $reclamation->id . '/tmp');
  333. $reclamation->documents()->syncWithoutDetaching($fileModel);
  334. // return link
  335. return $fileModel?->link ?? '';
  336. }
  337. /**
  338. * @throws Exception
  339. */
  340. public function generateReclamationPaymentPack(Reclamation $reclamation, int $userId): string
  341. {
  342. $reclamation->loadMissing([
  343. 'order.statements',
  344. 'documents',
  345. 'acts',
  346. 'photos_before',
  347. 'photos_after',
  348. ]);
  349. $tmpRoot = 'reclamations/' . $reclamation->id . '/tmp';
  350. $baseDir = $tmpRoot . '/Пакет документов на оплату - ' . fileName($reclamation->order->object_address);
  351. $beforeDir = $baseDir . '/Фотографии проблемы';
  352. $afterDir = $baseDir . '/Фотографии после устранения';
  353. Storage::disk('public')->makeDirectory($beforeDir);
  354. Storage::disk('public')->makeDirectory($afterDir);
  355. $this->copyPhotosWithEmptyFallback($reclamation->photos_before, $beforeDir);
  356. $this->copyPhotosWithEmptyFallback($reclamation->photos_after, $afterDir);
  357. foreach ($reclamation->documents as $document) {
  358. if ($this->shouldSkipDocumentForPaymentPack($document)) {
  359. continue;
  360. }
  361. $this->copyFileToDir($document->path, $baseDir, $document->original_name);
  362. }
  363. foreach ($reclamation->acts as $act) {
  364. $this->copyFileToDir($act->path, $baseDir, $act->original_name);
  365. }
  366. foreach ($reclamation->order?->statements ?? [] as $statement) {
  367. $this->copyFileToDir($statement->path, $baseDir, $statement->original_name);
  368. }
  369. $archiveName = 'Пакет документов на оплату - ' . fileName($reclamation->order->object_address) . '.zip';
  370. $fileModel = (new FileService())->createZipArchive($tmpRoot, $archiveName, $userId);
  371. Storage::disk('public')->deleteDirectory($tmpRoot);
  372. $reclamation->documents()->syncWithoutDetaching($fileModel);
  373. return $fileModel?->link ?? '';
  374. }
  375. /**
  376. * @throws Exception
  377. */
  378. private function generateReclamationOrder(Reclamation $reclamation): void
  379. {
  380. $inputFileType = 'Xlsx';
  381. $inputFileName = './templates/ReclamationOrder.xlsx';
  382. $reader = IOFactory::createReader($inputFileType);
  383. $spreadsheet = $reader->load($inputFileName);
  384. $sheet = $spreadsheet->getActiveSheet();
  385. $articles = [];
  386. foreach ($reclamation->skus as $p) {
  387. $articles[] = $p->product->article;
  388. }
  389. $sheet->setCellValue('J4', DateHelper::getHumanDate($reclamation->create_date, true));
  390. $sheet->setCellValue('L10', $reclamation->order->common_name);
  391. $sheet->setCellValue('L11', $reclamation->order->area?->responsible?->name);
  392. $sheet->setCellValue('W11', $reclamation->order->area?->responsible?->phone);
  393. $sheet->setCellValue('L12', $reclamation->order->year);
  394. $sheet->setCellValue('G13', $reclamation->guarantee);
  395. $sheet->setCellValue('G14', implode(', ', $articles));
  396. $sheet->setCellValue('Y15', DateHelper::getHumanDate($reclamation->finish_date, true));
  397. $sheet->setCellValue('U20', DateHelper::getHumanDate($reclamation->create_date, true));
  398. // save file
  399. $fileName = 'Монтажная заявка - ' . fileName($reclamation->order->object_address) . '.xlsx';
  400. $writer = new Xlsx($spreadsheet);
  401. $fd = 'reclamations/' . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address);
  402. Storage::disk('public')->makeDirectory($fd);
  403. $fp = storage_path('app/public/reclamations/') . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address) . '/' . $fileName;
  404. Storage::disk('public')->delete($fd . '/' . $fileName);
  405. $writer->save($fp);
  406. PdfConverterClient::convert($fp);
  407. }
  408. /**
  409. * @throws Exception
  410. */
  411. private function generateReclamationAct(Reclamation $reclamation): void
  412. {
  413. $inputFileType = 'Xlsx';
  414. $inputFileName = './templates/ReclamationAct.xlsx';
  415. $reader = IOFactory::createReader($inputFileType);
  416. $spreadsheet = $reader->load($inputFileName);
  417. $sheet = $spreadsheet->getActiveSheet();
  418. $representativeId = Setting::getInt(Setting::KEY_RECLAMATION_ACT_REPRESENTATIVE_USER_ID);
  419. if ($representativeId) {
  420. $representative = User::query()->withTrashed()->find($representativeId);
  421. if ($representative) {
  422. $sheet->setCellValue('A14', 'службы сервиса ' . $representative->name);
  423. }
  424. }
  425. $mafs = [];
  426. foreach ($reclamation->skus as $p) {
  427. $mafs[] = $p->product->passport_name . ', тип ' . $p->product->nomenclature_number;
  428. }
  429. $sheet->setCellValue('A17', $reclamation->order->object_address);
  430. $sheet->setCellValue('A22', implode('; ', $mafs));
  431. $sheet->setCellValue('A27', $reclamation->whats_done);
  432. $i = 24;
  433. $n = 1;
  434. foreach ($reclamation->skus as $p) {
  435. if ($n++ > 1) {
  436. $i++;
  437. $sheet->insertNewRowBefore($i, 1);
  438. $range = 'D' . $i . ':I' . $i;
  439. $sheet->mergeCells($range);
  440. }
  441. $sheet->setCellValue('D' . $i, $p->rfid);
  442. }
  443. // save file
  444. $fileName = 'Акт - ' . fileName($reclamation->order->object_address) . '.xlsx';
  445. $writer = new Xlsx($spreadsheet);
  446. $fd = 'reclamations/' . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address);
  447. Storage::disk('public')->makeDirectory($fd);
  448. $fp = storage_path('app/public/reclamations/') . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address) . '/' . $fileName;
  449. Storage::disk('public')->delete($fd . '/' . $fileName);
  450. $writer->save($fp);
  451. PdfConverterClient::convert($fp);
  452. }
  453. /**
  454. * @throws Exception
  455. */
  456. private function generateReclamationGuarantee(Reclamation $reclamation): void
  457. {
  458. $inputFileType = 'Xlsx';
  459. $inputFileName = './templates/ReclamationGuarantee.xlsx';
  460. $reader = IOFactory::createReader($inputFileType);
  461. $spreadsheet = $reader->load($inputFileName);
  462. $sheet = $spreadsheet->getActiveSheet();
  463. $mafs = [];
  464. foreach ($reclamation->skus as $p) {
  465. $mafs[] = 'Тип ' . $p->product->nomenclature_number . ' (' . $p->product->passport_name . ')' ;
  466. }
  467. $contract = Contract::query()->where('contracts.year', $reclamation->order->year)->first();
  468. $text = "ООО «НАШ ДВОР-СТ» в рамках обязательств по Договору №{$contract?->contract_number}" .
  469. " от " . DateHelper::getHumanDate($contract?->contract_date ?? '1970-01-01', true) .
  470. " г. на выполнение комплекса работ по поставке, монтажу устанавливаемых на городских территориях малых архитектурных форм гарантирует " .
  471. $reclamation->guarantee . " на оборудовании «" . implode('; ', $mafs) .
  472. "» установленному по адресу г. Москва, " . $reclamation->order->object_address . " в срок до " .
  473. DateHelper::getHumanDate($reclamation->finish_date, true). " г. в связи с отсутствием детали в наличии и ее производством.";
  474. $sheet->setCellValue('B9', $reclamation->id);
  475. $sheet->setCellValue('D9', DateHelper::getHumanDate($reclamation->create_date, true));
  476. $sheet->setCellValue('A19', $text);
  477. // save file
  478. $fileName = 'Гарантийное письмо - ' . fileName($reclamation->order->object_address) . '.xlsx';
  479. $writer = new Xlsx($spreadsheet);
  480. $fd = 'reclamations/' . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address);
  481. Storage::disk('public')->makeDirectory($fd);
  482. $fp = storage_path('app/public/reclamations/') . $reclamation->id . '/tmp/' . fileName($reclamation->order->object_address) . '/' . $fileName;
  483. Storage::disk('public')->delete($fd . '/' . $fileName);
  484. $writer->save($fp);
  485. PdfConverterClient::convert($fp);
  486. }
  487. public function generateFilePack(Collection $files, int $userId, string $name = 'files'): \App\Models\File
  488. {
  489. $dir = Str::random(2);
  490. Storage::disk('public')->makeDirectory('files/' . $dir . '/tmp/');
  491. // copy files
  492. foreach ($files as $file) {
  493. $from = $file->path;
  494. $to = 'files/' . $dir . '/tmp/' . $file->original_name;
  495. if (!Storage::disk('public')->exists($to)) {
  496. Storage::disk('public')->copy($from, $to);
  497. }
  498. }
  499. // create zip archive
  500. $fileModel = (new FileService())->createZipArchive('files/' . $dir . '/tmp', $name .'_' . date('Y-m-d_h-i-s') . '.zip', $userId);
  501. // remove temp files
  502. Storage::disk('public')->deleteDirectory('files/' . $dir . '/tmp');
  503. // return link
  504. return $fileModel;
  505. }
  506. private function copyPhotosWithEmptyFallback(Collection $photos, string $targetDir): void
  507. {
  508. if ($photos->isEmpty()) {
  509. Storage::disk('public')->put(
  510. $targetDir . '/empty.txt',
  511. 'Данный файл создан для возможности создания пустой папки в архиве'
  512. );
  513. return;
  514. }
  515. foreach ($photos as $photo) {
  516. $this->copyFileToDir($photo->path, $targetDir, $photo->original_name);
  517. }
  518. }
  519. private function shouldSkipDocumentForPaymentPack($file): bool
  520. {
  521. return $this->shouldSkipArchiveDocument($file);
  522. }
  523. private function shouldSkipArchiveDocument($file): bool
  524. {
  525. if (!$file) {
  526. return true;
  527. }
  528. $fileName = Str::lower((string)$file->original_name);
  529. $mimeType = Str::lower((string)$file->mime_type);
  530. $archiveSuffixes = [
  531. '.zip',
  532. '.rar',
  533. '.7z',
  534. '.tar',
  535. '.tar.gz',
  536. '.tgz',
  537. '.tar.bz2',
  538. '.tbz2',
  539. '.bz2',
  540. '.tar.xz',
  541. '.txz',
  542. '.xz',
  543. ];
  544. if (Str::endsWith($fileName, $archiveSuffixes)) {
  545. return true;
  546. }
  547. if (str_contains($mimeType, 'zip') || str_contains($mimeType, 'compressed') || str_contains($mimeType, '7z') || str_contains($mimeType, 'rar')) {
  548. return true;
  549. }
  550. return false;
  551. }
  552. private function copyFileToDir(string $fromPath, string $targetDir, string $originalName): void
  553. {
  554. if (!Storage::disk('public')->exists($fromPath)) {
  555. return;
  556. }
  557. $safeName = $this->uniqueFileName($targetDir, $originalName);
  558. $to = $targetDir . '/' . $safeName;
  559. if (!Storage::disk('public')->exists($to)) {
  560. Storage::disk('public')->copy($fromPath, $to);
  561. }
  562. }
  563. private function uniqueFileName(string $dir, string $name): string
  564. {
  565. $base = pathinfo($name, PATHINFO_FILENAME);
  566. $ext = pathinfo($name, PATHINFO_EXTENSION);
  567. $extPart = $ext ? '.' . $ext : '';
  568. $candidate = $base . $extPart;
  569. $counter = 1;
  570. while (Storage::disk('public')->exists($dir . '/' . $candidate)) {
  571. $candidate = $base . ' (' . $counter . ')' . $extPart;
  572. $counter++;
  573. }
  574. return $candidate;
  575. }
  576. public function generateTtnPack(Ttn $ttn, int $userId): string
  577. {
  578. $skus = ProductSKU::query()->withoutGlobalScope(\App\Models\Scopes\YearScope::class)->whereIn('id', json_decode($ttn->skus))->get();
  579. $volume = $weight = $places = 0;
  580. foreach ($skus as $sku) {
  581. if(!isset($order)) {
  582. $order = $sku->order;
  583. }
  584. $volume += $sku->product->volume;
  585. $weight += $sku->product->weight;
  586. $places += $sku->product->places;
  587. }
  588. $installationDate = ($order->installation_date) ? DateHelper::getHumanDate($order->installation_date, true) : '-';
  589. $ttnNumber = ($ttn->ttn_number_suffix) ? $ttn->ttn_number . '-' . $ttn->ttn_number_suffix : $ttn->ttn_number;
  590. $inputFileType = 'Xlsx';
  591. $inputFileName = './templates/Ttn.xlsx';
  592. $reader = IOFactory::createReader($inputFileType);
  593. $spreadsheet = $reader->load($inputFileName);
  594. $sheet = $spreadsheet->getActiveSheet();
  595. $sheet->setCellValue('D8', $installationDate);
  596. $sheet->setCellValue('R8', $ttnNumber);
  597. $sheet->setCellValue('AF8', DateHelper::getHumanDate($ttn->order_date, true));
  598. $sheet->setCellValue('AT8', $ttn->order_number);
  599. $sheet->setCellValue('B19', $order->object_address ?? '');
  600. $sheet->setCellValue('AD22', CountHelper::humanCount($places, 'место', 'места', 'мест') . ', способ упаковки: поддон, ящик, картон, полиэтилен');
  601. $sheet->setCellValue('B24', $weight . ' кг, ' . $volume . ' м.куб');
  602. $sheet->setCellValue('B36', $installationDate);
  603. $sheet->setCellValue('AB57', $installationDate . ' 8-00');
  604. $sheet->setCellValue('B59', $installationDate . ' 8-10');
  605. $sheet->setCellValue('AB59', $installationDate . ' 8-40');
  606. $sheet->setCellValue('B61', $weight . ' кг');
  607. $sheet->setCellValue('B63', CountHelper::humanCount($places, 'место', 'места', 'мест'));
  608. $sheet->setCellValue('B75', $order->object_address ?? '');
  609. $sheet->setCellValue('AB75', $installationDate);
  610. $sheet->setCellValue('B77', $installationDate);
  611. $sheet->setCellValue('AB77', $installationDate);
  612. $sheet->setCellValue('AB79', CountHelper::humanCount($places, 'место', 'места', 'мест'));
  613. $sheet->setCellValue('B81', $weight . ' кг');
  614. $sheet->setCellValue('B83', $order->brigadier?->name ?? '');
  615. $sheet->setCellValue('B89', $ttn->order_sum);
  616. $sheet->setCellValue('AD89', $ttn->order_sum);
  617. $sheet->setCellValue('AS89', $ttn->order_sum);
  618. // save file
  619. $fileName = 'ТН №' . $ttn->ttn_number . ' от ' . DateHelper::getHumanDate($ttn->order_date) . '.xlsx';
  620. $writer = new Xlsx($spreadsheet);
  621. $fd = 'ttn/' . $ttn->year;
  622. Storage::disk('public')->makeDirectory($fd);
  623. $fp = storage_path('app/public/ttn/') . $ttn->year . '/' . $fileName;
  624. Storage::disk('public')->delete($fd . '/' . $fileName);
  625. $writer->save($fp);
  626. $fileModel = File::query()->updateOrCreate([
  627. 'link' => url('/storage/') . '/ttn/' . $ttn->year . '/' .$fileName,
  628. 'path' => $fp . '/' .$fileName,
  629. 'user_id' => $userId,
  630. 'original_name' => $fileName,
  631. 'mime_type' => 'application/xlsx',
  632. ]);
  633. $ttn->file_id = $fileModel->id;
  634. $ttn->save();
  635. $order->documents()->attach($fileModel->id);
  636. return $fileModel->link ?? '';
  637. }
  638. }