Add MailerService and refact send invoice email functionality

This commit is contained in:
Narindra ezway 2025-03-14 17:08:14 +03:00
parent 1f4046d7ed
commit aad1f7962e
5 changed files with 165 additions and 41 deletions

View File

@ -1,15 +1,17 @@
<?php <?php
namespace OCA\Gestion\Controller; namespace OCA\Gestion\Controller;
use OCA\Gestion\Db\Bdd;
use OCA\Gestion\Service\InvoicePdfService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\DB\Exception;
use OCP\Files\IRootFolder;
use OCP\IRequest; use OCP\IRequest;
use OCP\DB\Exception;
use OCP\Mail\IMailer; use OCP\Mail\IMailer;
use OCA\Gestion\Db\Bdd;
use OCP\Files\IRootFolder;
use OCP\AppFramework\Controller;
use OCA\Gestion\Helpers\DateHelpers;
use OCA\Gestion\Service\MailerService;
use OCP\AppFramework\Http\DataResponse;
use OCA\Gestion\Service\InvoicePdfService;
date_default_timezone_set('Europe/Paris');
class InvoiceController extends Controller class InvoiceController extends Controller
{ {
private Bdd $gestionRepository; private Bdd $gestionRepository;
@ -18,6 +20,9 @@ class InvoiceController extends Controller
private $mailer; private $mailer;
private $currentUserIdNextcloud; private $currentUserIdNextcloud;
private $storage; private $storage;
private $mailerService;
public function __construct( public function __construct(
$UserId, $UserId,
$AppName, $AppName,
@ -25,7 +30,9 @@ class InvoiceController extends Controller
Bdd $bdd, Bdd $bdd,
InvoicePdfService $invoicePdfService, InvoicePdfService $invoicePdfService,
IRootFolder $rootFolder, IRootFolder $rootFolder,
IMailer $mailer IMailer $mailer,
MailerService $mailerService
) )
{ {
$this->currentUserIdNextcloud = $UserId; $this->currentUserIdNextcloud = $UserId;
@ -33,6 +40,8 @@ class InvoiceController extends Controller
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
$this->mailer = $mailer; $this->mailer = $mailer;
$this->gestionRepository = $bdd; $this->gestionRepository = $bdd;
$this->mailerService = $mailerService;
try{ try{
$this->storage = $rootFolder->getUserFolder($this->currentUserIdNextcloud); $this->storage = $rootFolder->getUserFolder($this->currentUserIdNextcloud);
}catch(\OC\User\NoUserException $e){ }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']); return new DataResponse("La facture n'a pas été générée correctement", 404, ['Content-Type' => 'application/json']);
} }
$factureContent = $factureGeneratedResponse["content"]; $factureContent = $factureGeneratedResponse["content"];
$factureDate = DateHelpers::convertInvoiceDateToMonthAndYearOnly($facture['date_paiement']);
try { try {
$message = $this->mailer->createMessage(); $message = $this->mailer->createMessage();
$message->setTo(recipients: [$email => "Facture"]); $message->setTo(recipients: [$email => "Facture"]);
$content = $this->mailer->createAttachment($factureContent, "Facture.pdf", "application/pdf"); $content = $this->mailer->createAttachment($factureContent, "Facture.pdf", "application/pdf");
$message->attach($content); $message->attach($content);
$message->setSubject("Facture"); $message->setSubject("Facture");
$message->setPlainBody("Veuiller trouver ci-joint votre facture");
$signature = $this->mailerService->getFooterContent();
$message->setHtmlBody(
"<p>Bonjour.</p>".
"<p> Vous trouverez en pièce jointe la facture des soins de « " . $factureDate . " » .</p>".
$signature
);
$this->mailer->send($message); $this->mailer->send($message);
$this->gestionRepository->setFactureSentDate($factureId); $this->gestionRepository->setFactureSentDate($factureId);
} catch (Exception $e) { } catch (Exception $e) {

View File

@ -1,36 +1,37 @@
<?php <?php
namespace OCA\Gestion\Controller; namespace OCA\Gestion\Controller;
use \FPDF;
use \Datetime;
use Exception; use Exception;
use OCA\Gestion\Constants\BddConstant; use OCP\IConfig;
use OCA\Gestion\Constants\DevisMentionConstant; use OCP\IRequest;
use OCA\Gestion\Constants\FactureTypeConstant;
use OCA\Gestion\Service\InvoicePdfHandler;
defined("TAB1") or define("TAB1", "\t"); defined("TAB1") or define("TAB1", "\t");
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Mail\IMailer; use OCP\Mail\IMailer;
use OCP\Files\IRootFolder; use OCP\IGroupManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\Gestion\Db\Bdd;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IConfig;
use \Datetime;
use \DatetimeImmutable; use \DatetimeImmutable;
use \IntlDateFormatter; use \IntlDateFormatter;
use \FPDF; use OCA\Gestion\Db\Bdd;
use OCA\Gestion\Constants\MultipleFactureTypeConstant; use OCP\Files\IRootFolder;
use OCP\AppFramework\Controller;
use OCA\Gestion\Service\TalkService;
use OCA\Gestion\Constants\BddConstant;
use OCA\Gestion\Service\MailerService;
use OCP\AppFramework\Http\DataResponse;
use OCA\Gestion\Helpers\FileExportHelpers; use OCA\Gestion\Helpers\FileExportHelpers;
use OCA\Gestion\Service\Certificate\CertificateService; use OCA\Gestion\Service\InvoicePdfHandler;
use OCA\Gestion\Service\InvoicePdfService;
use OCP\AppFramework\Http\TemplateResponse;
use OCA\Gestion\Constants\FactureTypeConstant;
use OCA\Gestion\Constants\DevisMentionConstant;
use OCA\Gestion\Service\Devis\Pdf\DevisPdfService; use OCA\Gestion\Service\Devis\Pdf\DevisPdfService;
use OCA\Gestion\Service\ExportClientStatisticService; use OCA\Gestion\Service\ExportClientStatisticService;
use OCA\Gestion\Constants\MultipleFactureTypeConstant;
use OCA\Gestion\Service\ExportThanatoStatisticService; use OCA\Gestion\Service\ExportThanatoStatisticService;
use OCA\Gestion\Service\InvoicePdfService; use OCA\Gestion\Service\Certificate\CertificateService;
use OCA\Gestion\Service\TalkService;
date_default_timezone_set('Europe/Paris'); date_default_timezone_set('Europe/Paris');
@ -76,7 +77,8 @@ class PageController extends Controller {
/** @var DevisPdfService */ /** @var DevisPdfService */
private $devisPdfService; private $devisPdfService;
private $rootFolder; private $rootFolder;
private $mailerService;
/** /**
* Constructor * Constructor
*/ */
@ -95,7 +97,9 @@ class PageController extends Controller {
InvoicePdfService $invoicePdfService, InvoicePdfService $invoicePdfService,
CertificateService $certificateService, CertificateService $certificateService,
TalkService $talkService, TalkService $talkService,
DevisPdfService $devisPdfService) { DevisPdfService $devisPdfService,
MailerService $mailerService
) {
parent::__construct($AppName, $request); parent::__construct($AppName, $request);
@ -112,6 +116,7 @@ class PageController extends Controller {
$this->talkService = $talkService; $this->talkService = $talkService;
$this->devisPdfService = $devisPdfService; $this->devisPdfService = $devisPdfService;
$this->rootFolder = $rootFolder; $this->rootFolder = $rootFolder;
$this->mailerService = $mailerService;
//$this->fpdf = $fpdf; //$this->fpdf = $fpdf;
if ($userSession->isLoggedIn()) { if ($userSession->isLoggedIn()) {
@ -1594,7 +1599,14 @@ class PageController extends Controller {
$message->attach($content); $message->attach($content);
$message->setSubject($subject); $message->setSubject($subject);
$body_text = $addName ? $body." de ".$devis['defunt_nom']: $body; $body_text = $addName ? $body." de ".$devis['defunt_nom']: $body;
$message->setPlainBody("Bonjour.\n\n".$body_text."!\n\nCordialement.");
$signature = $this->mailerService->getFooterContent();
$message->setHtmlBody(
"<p>Bonjour.</p>".
"<p>$body_text</p>".
$signature
);
$this->mailer->send($message); $this->mailer->send($message);
return new DataResponse("", 200, ['Content-Type' => 'application/json']); return new DataResponse("", 200, ['Content-Type' => 'application/json']);
// } catch (Exception $e) { // } 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 "<img style= 'width: 250px;height: 150;' src='data:image/jpeg;base64,".base64_encode($signatureImage)."'>" ;
}
/** /**
* @NoAdminRequired * @NoAdminRequired
@ -1852,6 +1872,17 @@ class PageController extends Controller {
return base64_encode($file->getContent()); 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 * @NoAdminRequired
@ -2277,7 +2308,7 @@ class PageController extends Controller {
if($ff_pdf != null && trim($email) != ''){ if($ff_pdf != null && trim($email) != ''){
//send 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(); $res = array();
@ -2471,7 +2502,7 @@ class PageController extends Controller {
if($ff_pdf != null && trim($email) != ''){ if($ff_pdf != null && trim($email) != ''){
//send 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(); $res = array();
@ -2687,7 +2718,7 @@ class PageController extends Controller {
$careCertificateFilename = $this->certificateService->generateCareCertificate($defuntId,$this->idNextcloud); $careCertificateFilename = $this->certificateService->generateCareCertificate($defuntId,$this->idNextcloud);
if($careCertificateFilename != null && trim($email) != '' ){ if($careCertificateFilename != null && trim($email) != '' ){
//send 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; return $careCertificateFilename;
} }
@ -2836,7 +2867,7 @@ class PageController extends Controller {
$careCertificateFilename = $this->certificateService->generatePacemakerCertificate($defuntId,$this->idNextcloud); $careCertificateFilename = $this->certificateService->generatePacemakerCertificate($defuntId,$this->idNextcloud);
if($careCertificateFilename != null && trim($email) != ''){ if($careCertificateFilename != null && trim($email) != ''){
//send 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; return $careCertificateFilename;

View File

@ -131,5 +131,13 @@ class DateHelpers
} }
return $totalHours; return $totalHours;
} }
public static function convertInvoiceDateToMonthAndYearOnly($invoiceDate = null){
if(!$invoiceDate){
return "";
}
$dateTime = new DateTime($invoiceDate);
$formattedDate = strftime('%B %Y', $dateTime->getTimestamp());
return $formattedDate;
}
} }

View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
/**
* Calendar App
*
* @copyright 2021 Anna Larch <anna.larch@gmx.net>
*
* @author Anna Larch <anna.larch@gmx.net>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
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 = "<p>Vous en souhaitant bonne réception. </p>";
$cordialement = "<p> Cordialement,</p>";
$userName = "<p> {$userName} </p>" ;
$signatureImage = $this->getSignatureHtmlEmailContent();
return $wish . $cordialement .$userName . $signatureImage ;
}
private function getSignatureHtmlEmailContent (){
$signatureImage = $this->getSignatureContent();
if (!$signatureImage) {
return "";
}
return "<img style= 'width: 250px;height: 150;' src='data:image/jpeg;base64,".base64_encode($signatureImage)."'>" ;
}
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;
}
}

View File

@ -72,7 +72,7 @@ $coverProducts = $_['coverProducts'];
</div> </div>
</div> </div>
<!-- Sexe --> <!-- Sexe -->
<div class="d-flex flex-row align-items-center col-12" style="margin-bottom: 8px"> <!-- <div class="d-flex flex-row align-items-center col-12" style="margin-bottom: 8px">
<div class="col-3">Sexe</div> <div class="col-3">Sexe</div>
<div class="col-9"> <div class="col-9">
<select class="gestion-select w-100" data-table="defunt" data-column="sexe" data-id="<?php echo $_['defunt'][0]->id ?>"> <select class="gestion-select w-100" data-table="defunt" data-column="sexe" data-id="<?php echo $_['defunt'][0]->id ?>">
@ -80,24 +80,24 @@ $coverProducts = $_['coverProducts'];
<option value="f" <?php if (strcmp($_['defunt'][0]->sexe, 'f') == 0) echo 'selected' ?>>Féminin</option> <option value="f" <?php if (strcmp($_['defunt'][0]->sexe, 'f') == 0) echo 'selected' ?>>Féminin</option>
</select> </select>
</div> </div>
</div> </div> -->
<!-- Référence pacemaker --> <!-- de série Prothèse-->
<div class="d-flex flex-row align-items-center col-12" style="margin-bottom: 16px"> <div class="d-flex flex-row align-items-center col-12" style="margin-bottom: 16px">
<div class="col-3">Référence pacemaker</div> <div class="col-3">de série Prothèse</div>
<div class="col-9"> <div class="col-9">
<input class="gestion-input w-100" type="text" value="<?php echo ($_['defunt'][0]->ref_pacemaker ?? "") ?>" data-table="defunt" data-column="ref_pacemaker" data-id="<?php echo $_['defunt'][0]->id ?>" /> <input class="gestion-input w-100" type="text" value="<?php echo ($_['defunt'][0]->ref_pacemaker ?? "") ?>" data-table="defunt" data-column="ref_pacemaker" data-id="<?php echo $_['defunt'][0]->id ?>" />
</div> </div>
</div> </div>
<!-- Product reference --> <!-- Product reference -->
<div class="d-flex flex-row align-items-center col-12" style="margin-bottom: 16px"> <!-- <div class="d-flex flex-row align-items-center col-12" style="margin-bottom: 16px">
<div class="col-3">Référence du produit</div> <div class="col-3">Référence du produit</div>
<div class="col-9"> <div class="col-9">
<input class="gestion-input w-100" type="text" value="<?php echo ($_['defunt'][0]->product_reference ?? "") ?>" data-table="defunt" data-column="product_reference" data-id="<?php echo $_['defunt'][0]->id ?>" /> <input class="gestion-input w-100" type="text" value="<?php echo ($_['defunt'][0]->product_reference ?? "") ?>" data-table="defunt" data-column="product_reference" data-id="<?php echo $_['defunt'][0]->id ?>" />
</div> </div>
</div> </div> -->
<!-- Product brand --> <!-- Product brand -->
<div class="d-flex flex-row align-items-center col-12" style="margin-bottom: 16px"> <div class="d-flex flex-row align-items-center col-12" style="margin-bottom: 16px">
<div class="col-3">Marque du produit</div> <div class="col-3">Marque Prothèse</div>
<div class="col-9"> <div class="col-9">
<input class="gestion-input w-100" type="text" value="<?php echo ($_['defunt'][0]->product_brand ?? "") ?>" data-table="defunt" data-column="product_brand" data-id="<?php echo $_['defunt'][0]->id ?>" /> <input class="gestion-input w-100" type="text" value="<?php echo ($_['defunt'][0]->product_brand ?? "") ?>" data-table="defunt" data-column="product_brand" data-id="<?php echo $_['defunt'][0]->id ?>" />
</div> </div>