THANASOFT-DV/gestion/lib/Controller/InvoiceController.php

134 lines
4.7 KiB
PHP

<?php
namespace OCA\Gestion\Controller;
use OCP\IConfig;
use OCP\IRequest;
use OCP\DB\Exception;
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;
use OCP\IUserSession;
date_default_timezone_set('Europe/Paris');
class InvoiceController extends Controller
{
private Bdd $gestionRepository;
private InvoicePdfService $invoicePdfService;
private $rootFolder;
private $mailer;
private $currentUserIdNextcloud;
private $storage;
private $mailerService;
private $config;
private $user;
public function __construct(
$UserId,
$AppName,
IRequest $request,
IConfig $config,
Bdd $bdd,
InvoicePdfService $invoicePdfService,
IRootFolder $rootFolder,
IMailer $mailer,
MailerService $mailerService,
IUserSession $userSession
)
{
$this->currentUserIdNextcloud = $UserId;
$this->invoicePdfService = $invoicePdfService;
$this->rootFolder = $rootFolder;
$this->mailer = $mailer;
$this->gestionRepository = $bdd;
$this->mailerService = $mailerService;
$this->config = $config;
$this->user = $userSession->getUser();
try{
$this->storage = $rootFolder->getUserFolder($this->currentUserIdNextcloud);
}catch(\OC\User\NoUserException $e){
}
parent::__construct($AppName, request: $request);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getInvoicePdfContent($factureId){
$facture = $this->gestionRepository->getFactureByFactureId($factureId);
if($facture == null)
{
return new DataResponse("La facture n'existe pas", 404, ['Content-Type' => 'application/json']);
}
$factureGeneratedResponse = $this->invoicePdfService->generateFacturePdfByFactureId($factureId,$this->currentUserIdNextcloud);
if($factureGeneratedResponse == null){
return new DataResponse("La facture n'a pas été générée correctement", 404, ['Content-Type' => 'application/json']);
}
$factureContent = base64_encode($factureGeneratedResponse['content']);
return new DataResponse([
"content" => $factureContent
], 200, ['Content-Type' => 'application/json']);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function sendInvoicePdfViaMail($factureId, $email = '')
{
$facture = $this->gestionRepository->getFactureByFactureId($factureId);
if($facture == null)
{
return new DataResponse("La facture n'existe pas", 404, ['Content-Type' => 'application/json']);
}
$factureClientMail = $this->gestionRepository->getFactureClientMailByFactureId($factureId);
if($factureClientMail == null){
return new DataResponse("Le mail de la facture n'existe pas", 404, ['Content-Type' => 'application/json']);
}
$factureGeneratedResponse = $this->invoicePdfService->generateFacturePdfByFactureId($factureId,$this->currentUserIdNextcloud);
if($factureGeneratedResponse == null){
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");
$signature = $this->mailerService->getFooterContent();
$message->setHtmlBody(
"<p>Bonjour.</p>".
"<p> Vous trouverez en pièce jointe la facture des soins de « " . $factureDate . " » .</p>".
$signature
);
$authUserEmail = $this->user->getEMailAddress();
if ($authUserEmail) {
$message->setCc([$authUserEmail]);
}
$this->mailer->send($message);
$this->gestionRepository->setFactureSentDate($factureId);
} catch (Exception $e) {
return new DataResponse("Veuillez configurer le serveur SMTP sur nextcloud ?", 500, ['Content-Type' => 'application/json']);
}
return new DataResponse("E-mail envoyé avec succès à ".$email.".", 200, ['Content-Type' => 'application/json']);
}
}