send mail to client from facture group, wip facture show

This commit is contained in:
Tiavina 2025-03-07 16:23:27 +03:00
parent 6e194570f8
commit 565ee75b56
8 changed files with 217 additions and 16 deletions

View File

@ -177,5 +177,9 @@ return [
// API
['name' => 'api#getDefundIdByCalendarUuid', 'url' => '/api/getDefundIdByCalendarUuid/{uuid}', 'verb' => 'GET'],
//invoice controller
['name' => 'invoice#sendInvoicePdfViaMail', 'url' => '/invoice/{factureId}/sendInvoicePdfViaMail', 'verb' => 'POST'],
['name' => 'invoice#getInvoicePdfContent', 'url' => '/invoice/{factureId}/getInvoicePdfContent', 'verb' => 'GET'],
]
];

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,98 @@
<?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)
{
$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']);
}
}

View File

@ -2504,8 +2504,8 @@ class PageController extends Controller {
public function exportFactureToPdf($factureId){
try{
$factureFilenames = $this->invoicePdfService->generateFacturePdfByFactureId($factureId,$this->idNextcloud);
return json_encode($factureFilenames);
$factureGeneratedResponse = $this->invoicePdfService->generateFacturePdfByFactureId($factureId,$this->idNextcloud);
return json_encode($factureGeneratedResponse["filenames"]);
}
catch(\OCP\Files\NotFoundException $e) { }

View File

@ -4525,4 +4525,51 @@ class Bdd {
$factureId
]);
}
public function getFactureClientMailByFactureId($factureId){
$sql = "SELECT
facture.id,
facture.facture_type,
facture_client.mail as facture_client_mail,
facture_client_group_facturation.email as facture_client_group_mail,
facture.fk_client_id as facture_client_id,
facture.fk_client_group_facturation_id as facture_client_group_facturation_id,
client.id as client_id,
client.fk_client_group_facturation_id as devis_client_group_facturation_id,
client.mail as devis_client_mail,
client_group_facturation.email as devis_client_group_mail
FROM ".$this->tableprefix."facture as facture
LEFT JOIN ".$this->tableprefix."devis as devis
on facture.id_devis = devis.id
LEFT JOIN ".$this->tableprefix."client as client
on devis.id_client = client.id
LEFT JOIN ".$this->tableprefix."client_group_facturation as client_group_facturation
on client.fk_client_group_facturation_id = client_group_facturation.id
LEFT JOIN ".$this->tableprefix."client as facture_client
on facture.fk_client_id = facture_client.id
LEFT JOIN ".$this->tableprefix."client_group_facturation as facture_client_group_facturation
on facture.fk_client_group_facturation_id = client_group_facturation.id
WHERE facture.id = ?
LIMIT 1;";
$mail = null;
$result = $this->execSQLNoJsonReturn($sql,[$factureId]);
if(!empty($result)){
$facture = $result[0];
$factureIsSingle = $facture["facture_type"] == FactureTypeConstant::TYPE_SINGLE;
if($factureIsSingle){
$mail = $facture["fk_client_group_facturation_id"] != null ?
$facture["devis_client_group_mail"] :
$facture["devis_client_mail"];
}
else{
$factureIsClientWithoutGroup = $facture["facture_client_id"] != null && $facture["facture_client_id"] != 0;
$mail = $factureIsClientWithoutGroup ?
$facture["facture_client_mail"] :
$facture["facture_client_group_mail"];
}
}
return $mail;
}
}

View File

@ -88,7 +88,7 @@ class InvoicePdfService {
$logo = $this->getLogo();
$invoicePdfData = $this->gestionBdd->getInvoicePdfData($factureId,$currentConfig);
if($invoicePdfData == null){
return "";
return null;
}
$clean_folder = html_entity_decode(string: $currentConfig->path).'/';
$factureFolders = $this->getFacturesFolder($invoicePdfData,$clean_folder);
@ -113,7 +113,10 @@ class InvoicePdfService {
$filenames[] = $ff_pdf;
}
$this->gestionBdd->setFactureGeneratedDate($factureId);
return $filenames;
return [
"content" => $pdfContent,
"filenames" => $filenames
];
}
public function generateFacturePdfByFactureId($factureId,$idNextCloud){
@ -197,7 +200,10 @@ class InvoicePdfService {
$filenames[] = $ff_pdf;
}
$this->gestionBdd->setFactureGeneratedDate($factureId);
return $filenames;
return [
"content" => $pdfContent,
"filenames" => $filenames
];
}
public function generateFacturePdfByFactureIds(array $factureIds,$idNextCloud){
@ -281,8 +287,8 @@ class InvoicePdfService {
}
$this->gestionBdd->invoiceListOfDevisIds($devisIds);
$filenames = $this->generateFactureGroupPdfByFactureId($factureId,$idNextcloud);
return $filenames;
$factureGeneratedResponse = $this->generateFactureGroupPdfByFactureId($factureId,$idNextcloud);
return $factureGeneratedResponse["filenames"];
}
catch(Exception){
return null;

View File

@ -13,9 +13,57 @@ $('body').on('click', '#closeSendFacturePdfMail', function () {
});
$('body').on('click', '#showPdfPreview', function () {
let pdfPath = $(this).data('path');
if(pdfPath != ""){
let url = generateUrl(pdfPath);
window.open(url, "_blank");
const factureId = $('#factureIdentifier').data('id');
if(factureId){
showLoader();
$.ajax({
url: baseUrl + '/invoice/'+factureId+'/getInvoicePdfContent',
type: 'GET',
contentType: 'application/json'
}).done(function (response, textStatus, xhr) {
if (xhr.status === 200) {
var base64PDF = response.content;
var byteCharacters = atob(base64PDF);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
var fileBlob = new Blob([byteArray], { type: "application/pdf" });
var fileURL = URL.createObjectURL(fileBlob);
window.open(fileURL, "_blank");
} else {
showError(t('gestion', "Erreur dans la récupération du document de la facture"));
}
}).fail(function (response, code) {
showError(response);
}).always(function () {
hideLoader();
});
}
});
});
$('body').on('click','#sendFacturePdf',function(){
const factureId = $('#factureIdentifier').data('id');
if(factureId){
showLoader();
$.ajax({
url: baseUrl + '/invoice/'+factureId+'/sendInvoicePdfViaMail',
type: 'POST',
contentType: 'application/json'
}).done(function (response, textStatus, xhr) {
if (xhr.status === 200) {
showSuccess(response);
} else {
showError(t('gestion', "Erreur dans l'envoi du mail chez le client"));
}
$('#sendFacturePdfMail').hide();
}).fail(function (response, code) {
$('#sendFacturePdfMail').hide();
showError(response);
}).always(function () {
hideLoader();
});
}
})

View File

@ -7,12 +7,10 @@ $factureOrderNumber = $facture->facture_order_number == '' ? '-' : $facture->fac
$factureCaseNumber = $facture->facture_case_number == '' ? '-' : $facture->facture_case_number;
$isFactureClientGroup = $facture->isFactureClientGroup;
$currentConfig = json_decode($_['configuration'])[0];
$facturePdfPath = 'remote.php/dav/files/'.$facture->path_to_file;
?>
<div class="bootstrap-iso">
<div id="factureId" data-id="<?php echo $facture->id; ?>"></div>
<div id="factureIdentifier" data-id="<?php echo $facture->id; ?>"></div>
<div id="facturePdfPath" data-path="<?php echo $facturePdfPath; ?>"></div>
<h2 class="mt-3 mb-3 text-center"> <?php echo ('Facture n° '.$facture->num); ?>
</h2>
<hr />
@ -150,7 +148,7 @@ $facturePdfPath = 'remote.php/dav/files/'.$facture->path_to_file;
<h5 class="modal-title">Envoyer la facture par email à <?= $facture->mail; ?></h5>
</div>
<div class="modal-footer">
<button id="showPdfPreview" type="button" class="btn btn-secondary" data-path="<?php echo $facturePdfPath; ?>">Voir l'aperçu</button>
<button id="showPdfPreview" type="button" class="btn btn-secondary">Voir l'aperçu</button>
<button id="closeSendFacturePdfMail" type="button" class="btn btn-secondary">Annuler</button>
<button id="sendFacturePdf" type="button" class="btn btn-primary">Envoyer</button>
</div>