Merge branch 'features/feature-save-multiple-invoice-at-once' into staging

This commit is contained in:
Tiavina 2025-01-06 11:53:19 +03:00
commit 508d13f630
10 changed files with 201 additions and 17 deletions

View File

@ -67,6 +67,7 @@ return [
['name' => 'page#insertFacture', 'url' => '/facture/insert', 'verb' => 'POST'],
['name' => 'page#exportDevisToFacture', 'url' => '/exportDevisToFacture', 'verb' => 'POST'],
['name' => 'page#exportFactureToPdf', 'url' => '/facture/exportFactureToPdf', 'verb' => 'POST'],
['name' => 'page#exportFactureByClientAndMonthYearToPdf', 'url' => '/facture/exportFactureByClientAndMonthYearToPdf', 'verb' => 'POST'],
['name' => 'page#getProduits', 'url' => '/getProduits', 'verb' => 'PROPFIND'],
['name' => 'page#getProduitsById', 'url' => '/getProduitsById', 'verb' => 'POST'],

File diff suppressed because one or more lines are too long

View File

@ -2777,6 +2777,24 @@ class PageController extends Controller {
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @param string $clientId
* @param string $month
* @param string $year
*
*/
public function exportFactureByClientAndMonthYearToPdf($clientId,$month,$year){
try{
$factureFilename = $this->invoicePdfService->generateMultipleInvoicePdfByClientAndMonthYear($clientId,$month,$year,$this->idNextcloud);
return $factureFilename;
}
catch(\OCP\Files\NotFoundException $e) { }
}
/**
* @NoAdminRequired
* @NoCSRFRequired

View File

@ -2302,6 +2302,68 @@ class Bdd {
return $factureData;
}
public function getInvoicePdfDataByClientAndMonthYear($clientId,$month,$year,$configuration){
$invoices = $this->getInvoiceByClientAndMonthYear($clientId,$month,$year);
$firstClient = $this->getFirstClient();
foreach($invoices as &$invoice){
$invoice["siret"] = $firstClient != null ? $firstClient['legal_one'] : '';
$products = $this->getDevisProduits($invoice["devis_id"]);
$invoice["products"] = $products;
$invoice["configuration"] = $configuration;
$clientAdresses = FileExportHelpers::GetAddressAndCityFromAddress($invoice["client_adresse"]);
$invoice["client_real_adress"] = $clientAdresses["address"];
$invoice["client_adress_city"] = $clientAdresses["city"];
$configurationAdresses = FileExportHelpers::GetAddressAndCityFromAddress($configuration->adresse);
$invoice["configuration_adresse"] = $configurationAdresses["address"];
$invoice["configuration_adresse_city"] = $configurationAdresses["city"];
}
return $invoices;
}
private function getInvoiceByClientAndMonthYear($clientId,$month,$year){
$sql = "SELECT
facture.id,
facture.date,
facture.date_paiement,
facture.num,
devis.id as devis_id,
devis.date as devis_date,
devis.num as calendar_uuid,
devis.comment as devis_comment,
devis.id_client as devis_id_client,
client.nom as client_nom,
client.entreprise as client_entreprise,
client.adresse as client_adresse,
defunt.nom as defunt_nom,
defunt.sexe as defunt_sexe,
lieu.nom as lieu_nom,
lieu.adresse as lieu_adresse,
thanato.nom as thanato_nom,
thanato.prenom as thanato_prenom
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."defunt as defunt on devis.id_defunt = defunt.id
LEFT JOIN ".$this->tableprefix."lieu as lieu on devis.id_lieu = lieu.id
LEFT JOIN ".$this->tableprefix."thanato as thanato on devis.id_thanato = thanato.id
WHERE devis.id_client = ? AND
YEAR(facture.date_paiement) = ?";
$conditions = [$clientId,$year];
if($month != 0){
$conditions[] = $month;
$sql .= " AND MONTH(facture.date_paiement) = ?";
}
$sql .= ";";
$factures = $this->execSQLNoJsonReturn(
$sql,
$conditions);
return $factures;
}
private function getFactureByIdWithDevis($factureId){
$sql = "SELECT
facture.id,

View File

@ -5,6 +5,7 @@ namespace OCA\Gestion\Helpers;
use DateTime;
use DateTimeZone;
use Exception;
use IntlDateFormatter;
class DateHelpers
{
@ -20,6 +21,23 @@ class DateHelpers
}
}
public static function GetMonthPlainString($month){
$formatter = new IntlDateFormatter(
'fr_FR',
IntlDateFormatter::FULL,
IntlDateFormatter::NONE,
null,
null,
'MMMM'
);
$date = DateTime::createFromFormat('!m', $month);
$monthString = strtoupper($formatter->format($date));
$normalizedMonth = mb_convert_case($monthString, MB_CASE_UPPER, "UTF-8");
return $normalizedMonth;
}
public static function GetDateWithFormatDayAndMonthPlainString(string $date){
$dateTime = new DateTime($date);
$formattedDate = $dateTime->format('d F');

View File

@ -28,11 +28,13 @@ namespace OCA\Gestion\Service;
use DateTime;
use \FPDF;
use OCA\Gestion\Helpers\DateHelpers;
use OCA\Gestion\Helpers\FileExportHelpers;
use OCA\Gestion\Helpers\PriceHelpers;
class InvoicePdfHandler extends FPDF {
private $multipleFactureData = [];
private $factureData = [];
private $logo = null;
private $logoPath = "/var/www/html/data/admin/files/.gestion/";
@ -44,7 +46,6 @@ class InvoicePdfHandler extends FPDF {
else{
$this->Cell(55,30,'');
}
$this->Ln(30);
}
function Footer()
{
@ -65,7 +66,23 @@ class InvoicePdfHandler extends FPDF {
$this->logo = $logo;
}
public function GetFilename(){
public function MutlipleInvoicePdfFactory(array $multipleInvoiceData,$logo = null){
$this->multipleFactureData = $multipleInvoiceData;
$this->logo = $logo;
}
public function GetMultipleInvoiceFilename($month,$year){
$filename = "";
foreach($this->multipleFactureData as $factureData){
$filename = strtoupper($factureData["client_entreprise"]);
$filename .= $month != 0 ? '_'.DateHelpers::GetMonthPlainString($month) :'';
$filename .= "_".$year;
break;
}
return $filename;
}
public function GetInvoiceFilename(){
$factureNum = $this->factureData['num'];
$factureNum = str_replace('/','-',$factureNum);
$defuntNom = str_replace(' ',' ',$this->factureData['defunt_nom']);
@ -73,6 +90,7 @@ class InvoicePdfHandler extends FPDF {
}
private function DrawInvoiceCompanyAndClientInfo(){
$this->SetY(40);
$this->SetFont('Arial', '', 12);
$this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->factureData['configuration']->entreprise), 0, 0);
$this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->factureData['client_nom']), 0, 1,'R');
@ -92,8 +110,8 @@ class InvoicePdfHandler extends FPDF {
$factureDatePaiement = DateTime::createFromFormat('Y-m-d',$factureDatePaiement);
$factureDateEcheance = $factureDatePaiement;
$factureDatePaiement = $factureDatePaiement->format('d-m-Y');
$factureDateEcheance->modify('+1 month');
$factureDateEcheance = $factureDateEcheance->format('t-m-Y');
$factureDateEcheance->modify('last day of next month');
$factureDateEcheance = $factureDateEcheance->format('d-m-Y');
$this->SetFont('Arial', 'B', 11);
$this->Cell(30, 7, 'DATE', 1, 0, 'C');
$this->Cell(80, 7, 'CLIENT', 1, 0, 'C');
@ -218,7 +236,14 @@ class InvoicePdfHandler extends FPDF {
}
}
public function GetFactureContent(){
public function SetMultipleFactureContent(){
foreach($this->multipleFactureData as $factureData){
$this->factureData = $factureData;
$this->SetFactureContent();
}
}
public function SetFactureContent(){
$this->AddPage();
$this->SetMargins(10,0,10);
$this->DrawInvoiceCompanyAndClientInfo();
@ -227,8 +252,6 @@ class InvoicePdfHandler extends FPDF {
$this->DrawArticlesTableHeader();
$totalPriceValue = $this->DrawArticlesTableValueAndReturnTotalPrice();
$this->DrawBankAndTotalPriceInfo($totalPriceValue);
$pdfContent = $this->Output('','S');
return $pdfContent;
}
function MultiAlignCell($w,$h,$text,$border=0,$ln=0,$align='L',$fill=false)

View File

@ -75,12 +75,13 @@ class InvoicePdfService {
if($invoicePdfData == null){
return "";
}
$clean_folder = html_entity_decode($currentConfig->path).'/';
$clean_folder = html_entity_decode(string: $currentConfig->path).'/';
$factureFolders = $this->getFacturesFolder($invoicePdfData,$clean_folder);
$pdf = new InvoicePdfHandler();
$pdf->InvoicePdfFactory($invoicePdfData,$logo);
$pdfContent = $pdf->GetFactureContent();
$pdfFilename = $pdf->GetFilename();
$pdf->SetFactureContent();
$pdfContent = $pdf->Output('','S');
$pdfFilename = $pdf->GetInvoiceFilename();
$filenames = [];
foreach($factureFolders as $folder){
try {
@ -116,4 +117,32 @@ class InvoicePdfService {
$this->generateFacturePdfByFactureId($factureId,$idNextCloud);
}
}
public function generateMultipleInvoicePdfByClientAndMonthYear($clientId,$month,$year,$idNextCloud){
$storage = $this->rootFolder->getUserFolder($idNextCloud);
$configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_ADMIN));
$currentConfig = $configs[0];
$logo = $this->getLogo();
$invoiceData = $this->gestionBdd->getInvoicePdfDataByClientAndMonthYear($clientId,$month,$year,$currentConfig);
if(empty($invoiceData)){
return null;
}
$pdf = new InvoicePdfHandler();
$pdf->MutlipleInvoicePdfFactory($invoiceData,$logo);
$pdf->SetMultipleFactureContent();
$racinePath = html_entity_decode(string: $currentConfig->path).'/';
$clientRacineFolder = $racinePath.'CLIENTS/'.strtoupper($invoiceData[0]["client_entreprise"]).'/';
$filename = $currentConfig->facture_prefixe.'_'.$pdf->GetMultipleInvoiceFilename($month,$year);
$filenamePath = $clientRacineFolder.$filename.'.pdf';
$pdfContent = $pdf->Output('','S');
try {
$storage->newFolder($clientRacineFolder);
}
catch(\OCP\Files\NotPermittedException $e) {
}
$storage->newFile($filenamePath);
$file_pdf = $storage->get($filenamePath);
$file_pdf->putContent($pdfContent);
return $filenamePath;
}
}

View File

@ -4,22 +4,23 @@ import "../css/mycss.css";
import { globalConfiguration } from "./modules/mainFunction.mjs";
import "./listener/main_listener";
import "./listener/invoiceListener";
import { getPDF } from "./pdf";
import { saveDocumentRecap } from "./modules/ajaxRequest.mjs";
window.addEventListener("DOMContentLoaded", function () {
globalConfiguration();
var pdf = document.getElementById("pdfFactures");
pdf.addEventListener("click",async ()=>{
getPDF('facture');
});
// var pdf = document.getElementById("pdfFactures");
// pdf.addEventListener("click",async ()=>{
// getPDF('facture');
// });
var documentRecap = document.getElementById("documentrecap");
documentRecap.addEventListener("click", async ()=> {
// Get the URL parameters
const urlParams = new URLSearchParams(window.location.search);
// Access specific parameter values
const idclient = urlParams.get('cli');
const annee = urlParams.get('annee');

View File

@ -0,0 +1,32 @@
import {showError, showSuccess } from "@nextcloud/dialogs";
import {baseUrl} from "../modules/mainFunction.mjs";
$('body').on('click', '#exportMultipleFactureToPdf', function () {
// Access specific parameter values
const urlParams = new URLSearchParams(window.location.search);
const clientId = urlParams.get('cli');
const year = urlParams.get('annee');
const month = urlParams.get('mois');
var exportMultipleFacturePayload = {
clientId : clientId,
month : month,
year : year
};
$.ajax({
url: baseUrl + '/facture/exportFactureByClientAndMonthYearToPdf',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(exportMultipleFacturePayload)
}).done(function (response) {
var fileName = response;
if(fileName != null){
showSuccess('Sauvegardé dans' + fileName);
return;
}
showError(t('gestion', "Les données pour sauvegarde sont vides"));
}).fail(function (response, code) {
showError(t('gestion', "Erreur dans la génération de facture multiple"));
});
});

View File

@ -58,7 +58,7 @@
if(sizeof($factures)>0) {
?>
<?php if($showRecapButton) {?><button class="btn btn-secondary" type="button" id="documentrecap">Generer le document recapitulatif</button><?php }?>
<button class="btn btn-secondary ml-2" type="button" id="pdfFactures"><?php p($l->t('Save in Nextcloud'));?></button>
<button class="btn btn-secondary ml-2" type="button" id="exportMultipleFactureToPdf"><?php p($l->t('Save in Nextcloud'));?></button>
<?php
}
}