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( "
Bonjour.
". "Vous trouverez en pièce jointe la facture des soins de " . $factureDate . ".
". $signature ); $appAdminEmail = BddConstant::DEFAULT_INVOICE_CC_EMAIL; $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']); } }