From aad1f7962ef507549dae1e4b66eae2ce7d9f0ced Mon Sep 17 00:00:00 2001 From: Narindra ezway Date: Fri, 14 Mar 2025 17:08:14 +0300 Subject: [PATCH] Add MailerService and refact send invoice email functionality --- gestion/lib/Controller/InvoiceController.php | 35 ++++++--- gestion/lib/Controller/PageController.php | 81 ++++++++++++++------ gestion/lib/Helpers/DateHelpers.php | 8 ++ gestion/lib/Service/MailerService.php | 68 ++++++++++++++++ gestion/templates/content/defuntshow.php | 14 ++-- 5 files changed, 165 insertions(+), 41 deletions(-) create mode 100644 gestion/lib/Service/MailerService.php diff --git a/gestion/lib/Controller/InvoiceController.php b/gestion/lib/Controller/InvoiceController.php index cf8aaf3..e43efdd 100644 --- a/gestion/lib/Controller/InvoiceController.php +++ b/gestion/lib/Controller/InvoiceController.php @@ -1,15 +1,17 @@ currentUserIdNextcloud = $UserId; @@ -33,6 +40,8 @@ class InvoiceController extends Controller $this->rootFolder = $rootFolder; $this->mailer = $mailer; $this->gestionRepository = $bdd; + $this->mailerService = $mailerService; + try{ $this->storage = $rootFolder->getUserFolder($this->currentUserIdNextcloud); }catch(\OC\User\NoUserException $e){ @@ -82,13 +91,21 @@ class InvoiceController extends Controller return new DataResponse("La facture n'a pas été générée correctement", 404, ['Content-Type' => 'application/json']); } $factureContent = $factureGeneratedResponse["content"]; + $factureDate = DateHelpers::convertInvoiceDateToMonthAndYearOnly($facture['date_paiement']); try { $message = $this->mailer->createMessage(); $message->setTo(recipients: [$email => "Facture"]); $content = $this->mailer->createAttachment($factureContent, "Facture.pdf", "application/pdf"); $message->attach($content); $message->setSubject("Facture"); - $message->setPlainBody("Veuiller trouver ci-joint votre facture"); + + $signature = $this->mailerService->getFooterContent(); + + $message->setHtmlBody( + "

Bonjour.

". + "

Vous trouverez en pièce jointe la facture des soins de « " . $factureDate . " » .

". + $signature + ); $this->mailer->send($message); $this->gestionRepository->setFactureSentDate($factureId); } catch (Exception $e) { diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index 38343c1..90f3c00 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -1,36 +1,37 @@ talkService = $talkService; $this->devisPdfService = $devisPdfService; $this->rootFolder = $rootFolder; + $this->mailerService = $mailerService; //$this->fpdf = $fpdf; if ($userSession->isLoggedIn()) { @@ -1594,7 +1599,14 @@ class PageController extends Controller { $message->attach($content); $message->setSubject($subject); $body_text = $addName ? $body." de ".$devis['defunt_nom']: $body; - $message->setPlainBody("Bonjour.\n\n".$body_text."!\n\nCordialement."); + + $signature = $this->mailerService->getFooterContent(); + + $message->setHtmlBody( + "

Bonjour.

". + "

$body_text

". + $signature + ); $this->mailer->send($message); return new DataResponse("", 200, ['Content-Type' => 'application/json']); // } catch (Exception $e) { @@ -1602,6 +1614,14 @@ class PageController extends Controller { // } } } + public function addSignatureEmailLogo (){ + $signatureImage = $this->getSignature(); + if (!$signatureImage) { + return ""; + } + //Add html img in base 64 + return "" ; + } /** * @NoAdminRequired @@ -1852,6 +1872,17 @@ class PageController extends Controller { return base64_encode($file->getContent()); } + private function getSignature(){ + try{ + if(isset($this->adminStorage)){ + $file = $this->adminStorage->get('/.gestion/sign.jpg'); + return $file->getContent(); + } + } + catch(\OCP\Files\NotFoundException $e) {} + + return false; + } /** * @NoAdminRequired @@ -2277,7 +2308,7 @@ class PageController extends Controller { if($ff_pdf != null && trim($email) != ''){ //send email - $this->sendAttachmentToClientByDefunt($defunt->id, $ff_pdf, $email,"Rapport soins", "Veuillez trouver ci-joint le rapport de soins de ".$nomDefunt."."); + $this->sendAttachmentToClientByDefunt($defunt->id, $ff_pdf, $email,"Rapport soins", "Vous trouverez en pièce jointe le rapport de soins de ".$nomDefunt."."); } $res = array(); @@ -2471,7 +2502,7 @@ class PageController extends Controller { if($ff_pdf != null && trim($email) != ''){ //send email - $this->sendAttachmentToClientByDefunt($defunt->id, $ff_pdf, $email,"Rapport des bijoux", "Veuillez trouver ci-joint le rapport des bijoux de ".$nomDefunt."."); + $this->sendAttachmentToClientByDefunt($defunt->id, $ff_pdf, $email,"Rapport des bijoux", "Vous trouverez en pièce jointe le rapport des bijoux de ".$nomDefunt."."); } $res = array(); @@ -2687,7 +2718,7 @@ class PageController extends Controller { $careCertificateFilename = $this->certificateService->generateCareCertificate($defuntId,$this->idNextcloud); if($careCertificateFilename != null && trim($email) != '' ){ //send email - $this->sendAttachmentToClientByDefunt($defuntId, $careCertificateFilename, $email,"Attestation de soins", " Veuillez trouver ci-joint l'attestation de soins ", true); + $this->sendAttachmentToClientByDefunt($defuntId, $careCertificateFilename, $email,"Attestation de soins", " Vous trouverez en pièce jointe l'attestation de soins ", true); } return $careCertificateFilename; } @@ -2836,7 +2867,7 @@ class PageController extends Controller { $careCertificateFilename = $this->certificateService->generatePacemakerCertificate($defuntId,$this->idNextcloud); if($careCertificateFilename != null && trim($email) != ''){ //send email - $this->sendAttachmentToClientByDefunt($defuntId, $careCertificateFilename, $email,"Attestation pacemaker", "Veuillez trouver ci-joint l'attestation de pacemaker ", true); + $this->sendAttachmentToClientByDefunt($defuntId, $careCertificateFilename, $email,"Attestation pacemaker", "Vous trouverez en pièce jointe l'attestation de pacemaker ", true); } return $careCertificateFilename; diff --git a/gestion/lib/Helpers/DateHelpers.php b/gestion/lib/Helpers/DateHelpers.php index 0f58ad2..67de497 100644 --- a/gestion/lib/Helpers/DateHelpers.php +++ b/gestion/lib/Helpers/DateHelpers.php @@ -131,5 +131,13 @@ class DateHelpers } return $totalHours; } + public static function convertInvoiceDateToMonthAndYearOnly($invoiceDate = null){ + if(!$invoiceDate){ + return ""; + } + $dateTime = new DateTime($invoiceDate); + $formattedDate = strftime('%B %Y', $dateTime->getTimestamp()); + return $formattedDate; + } } diff --git a/gestion/lib/Service/MailerService.php b/gestion/lib/Service/MailerService.php new file mode 100644 index 0000000..f064825 --- /dev/null +++ b/gestion/lib/Service/MailerService.php @@ -0,0 +1,68 @@ + + * + * @author Anna Larch + * @author Richard Steinmetz + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see . + * + */ + +namespace OCA\Gestion\Service; + +use OCA\Gestion\Constants\BddConstant; +use OCP\Files\IRootFolder; + +class MailerService { + + private $adminStorage; + + public function __construct( + IRootFolder $rootFolder, + ){ + $this->adminStorage = $rootFolder->getUserFolder(BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD); + } + public function getFooterContent ($userName = "Johann"){ + $wish = "

Vous en souhaitant bonne réception.

"; + $cordialement = "

Cordialement,

"; + $userName = "

{$userName}

" ; + $signatureImage = $this->getSignatureHtmlEmailContent(); + return $wish . $cordialement .$userName . $signatureImage ; + } + + private function getSignatureHtmlEmailContent (){ + $signatureImage = $this->getSignatureContent(); + if (!$signatureImage) { + return ""; + } + return "" ; + } + + private function getSignatureContent(){ + try{ + if(isset($this->adminStorage)){ + $file = $this->adminStorage->get('/.gestion/sign.jpg'); + return $file->getContent(); + } + } + catch(\OCP\Files\NotFoundException $e) {} + + return false; + } +} diff --git a/gestion/templates/content/defuntshow.php b/gestion/templates/content/defuntshow.php index f3898bd..7b99180 100644 --- a/gestion/templates/content/defuntshow.php +++ b/gestion/templates/content/defuntshow.php @@ -72,7 +72,7 @@ $coverProducts = $_['coverProducts']; -
+ +
--> +
-
Référence pacemaker
+
N° de série Prothèse
" data-table="defunt" data-column="ref_pacemaker" data-id="id ?>" />
-
+
-
Marque du produit
+
Marque Prothèse
" data-table="defunt" data-column="product_brand" data-id="id ?>" />