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) { $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: [$factureClientMail => "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); } catch (Exception $e) { return new DataResponse("Veuillez configurer le mail sur nextcloud ?", 500, ['Content-Type' => 'application/json']); } return new DataResponse("Mail envoyé avec succès", 200, ['Content-Type' => 'application/json']); } }