Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

146 rindas
5.5KB

  1. <?php
  2. namespace domain\Document\Document;
  3. use domain\Producer\Producer\ProducerSolver;
  4. use domain\User\User\UserSolver;
  5. use kartik\mpdf\Pdf;
  6. use domain\_\AbstractManager;
  7. use yii\base\ErrorException;
  8. class DocumentManager extends AbstractManager
  9. {
  10. protected UserSolver $userSolver;
  11. protected DocumentSolver $documentSolver;
  12. protected DocumentBuilder $documentBuilder;
  13. protected ProducerSolver $producerSolver;
  14. protected DocumentReferenceGenerator $documentReferenceGenerator;
  15. public function loadDependencies(): void
  16. {
  17. $this->userSolver = $this->loadService(UserSolver::class);
  18. $this->documentSolver = $this->loadService(DocumentSolver::class);
  19. $this->documentBuilder = $this->loadService(DocumentBuilder::class);
  20. $this->producerSolver = $this->loadService(ProducerSolver::class);
  21. $this->documentReferenceGenerator = $this->loadService(DocumentReferenceGenerator::class);
  22. }
  23. public function changeStatus(DocumentInterface $document, string $status): void
  24. {
  25. $document->status = $status;
  26. if ($status == Document::STATUS_VALID) {
  27. $this->documentReferenceGenerator->generateReference($document);
  28. $this->generatePdf($document, Pdf::DEST_FILE);
  29. }
  30. $this->documentBuilder->update($document);
  31. }
  32. public function validateDocument(Document $document): void
  33. {
  34. if($this->documentSolver->isStatusDraft($document)) {
  35. $this->changeStatus($document, Document::STATUS_VALID);
  36. }
  37. }
  38. public function generatePdf(DocumentInterface $document, string $destination): ?string
  39. {
  40. $producer = $document->producer;
  41. $content = \Yii::$app->controller->renderPartial('/document/download', [
  42. 'producer' => $producer,
  43. 'document' => $document
  44. ]);
  45. $contentFooter = '<div id="footer">';
  46. if ($this->producerSolver->getConfig('document_image_bottom')) {
  47. $urlDocumentImageBottom = \Yii::$app->urlManagerProducer->getHostInfo() . '/' . \Yii::$app->urlManagerProducer->baseUrl . '/uploads/' . $producer->document_image_bottom;
  48. $contentFooter .= '<div class="image"><img src="'.$urlDocumentImageBottom.'" style="max-height:80px;" /></div>';
  49. }
  50. if ($this->producerSolver->getConfig('document_infos_bottom')) {
  51. $contentFooter .= '<div class="infos-bottom">'.nl2br($producer->document_infos_bottom).'</div>';
  52. }
  53. $contentFooter .= '</div>';
  54. $marginBottom = 10;
  55. if (strlen($this->producerSolver->getConfig('document_infos_bottom')) > 0) {
  56. $marginBottom = 40;
  57. }
  58. $this->initDirectoryPdf($document);
  59. $pdf = new Pdf([
  60. 'mode' => Pdf::MODE_UTF8,
  61. 'format' => Pdf::FORMAT_A4,
  62. 'orientation' => Pdf::ORIENT_PORTRAIT,
  63. 'destination' => $destination,
  64. 'content' => $content,
  65. 'filename' => $this->documentSolver->getFilenameComplete($document),
  66. 'cssFile' => \Yii::getAlias('@webroot/css/document/download.css'),
  67. 'marginBottom' => $marginBottom,
  68. 'methods' => [
  69. 'SetHTMLFooter' => $contentFooter
  70. ]
  71. ]);
  72. return $pdf->render();
  73. }
  74. public function initDirectoryPdf(DocumentInterface $document): void
  75. {
  76. $aliasDirectoryBase = $this->documentSolver->getAliasDirectoryBase($document);
  77. $directoryPdf = \Yii::getAlias($aliasDirectoryBase);
  78. if (!file_exists($directoryPdf)) {
  79. mkdir($directoryPdf, 0755);
  80. }
  81. }
  82. public function downloadPdf(DocumentInterface $document, bool $regenerate = false)
  83. {
  84. $filenameComplete = $this->documentSolver->getFilenameComplete($document);
  85. if (!file_exists($filenameComplete) || $this->documentSolver->isStatusDraft($document) || $regenerate) {
  86. $this->generatePdf($document, Pdf::DEST_FILE);
  87. }
  88. if (file_exists($filenameComplete)) {
  89. return \Yii::$app->response->sendFile($filenameComplete, $this->documentSolver->getFilename($document), ['inline' => true]);
  90. } else {
  91. throw new ErrorException('File ' . $filenameComplete . ' not found');
  92. }
  93. }
  94. public function sendDocument(DocumentInterface $document): bool
  95. {
  96. if (isset($document->user) && strlen($document->user->email) > 0) {
  97. $producer = $this->getProducerContext();
  98. $subjectEmail = $this->documentSolver->getType($document);
  99. if ($this->documentSolver->isStatusValid($document)) {
  100. $subjectEmail .= ' N°' . $document->reference;
  101. }
  102. $email = \Yii::$app->mailerService->getMailer()->compose(
  103. [
  104. 'html' => '@common/mail/sendDocument-html',
  105. 'text' => '@common/mail/sendDocument-text'
  106. ], [
  107. 'document' => $document,
  108. 'producer' => $producer
  109. ])
  110. ->setTo($this->userSolver->getEmailSendingInvoicingDocuments($document->user))
  111. ->setFrom([$this->producerSolver->getProducerEmailPlatform($producer) => $producer->name])
  112. ->setSubject('[' . $producer->name . '] ' . $subjectEmail);
  113. $this->generatePdf($document, Pdf::DEST_FILE);
  114. $email->attach($this->documentSolver->getFilenameComplete($document));
  115. // @TODO : gérer via un événement
  116. $this->documentBuilder->updateDocumentIsSend($document, true);
  117. return $email->send();
  118. }
  119. return false;
  120. }
  121. }