131 lines
4.6 KiB
PHP
131 lines
4.6 KiB
PHP
<?php
|
|
namespace OCA\Gestion\Controller;
|
|
|
|
use OCA\Gestion\Constants\BddConstant;
|
|
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']);
|
|
}
|
|
$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
|
|
);
|
|
$appAdminEmail = $this->gestionRepository->getUserEmailByNextcloudId(BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD);
|
|
if ($appAdminEmail) {
|
|
$message->setCc([$appAdminEmail]);
|
|
}
|
|
|
|
$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']);
|
|
}
|
|
} |