99 lines
3.8 KiB
PHP
99 lines
3.8 KiB
PHP
<?php
|
|
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\Mail\IMailer;
|
|
|
|
class InvoiceController extends Controller
|
|
{
|
|
private Bdd $gestionRepository;
|
|
private InvoicePdfService $invoicePdfService;
|
|
private $rootFolder;
|
|
private $mailer;
|
|
private $currentUserIdNextcloud;
|
|
private $storage;
|
|
public function __construct(
|
|
$UserId,
|
|
$AppName,
|
|
IRequest $request,
|
|
Bdd $bdd,
|
|
InvoicePdfService $invoicePdfService,
|
|
IRootFolder $rootFolder,
|
|
IMailer $mailer
|
|
)
|
|
{
|
|
$this->currentUserIdNextcloud = $UserId;
|
|
$this->invoicePdfService = $invoicePdfService;
|
|
$this->rootFolder = $rootFolder;
|
|
$this->mailer = $mailer;
|
|
$this->gestionRepository = $bdd;
|
|
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"];
|
|
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");
|
|
$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']);
|
|
}
|
|
} |