GenerateDocumentsService.php 33 KB

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