From ac9d2f306759e439c3a131968bcf9daa0d7ad357 Mon Sep 17 00:00:00 2001 From: vgreb Date: Wed, 24 Dec 2025 08:55:40 +0100 Subject: [PATCH] =?UTF-8?q?Refonte=20-=20Tr=C3=A9sorerie=20>=20Journal=20>?= =?UTF-8?q?=20Upload=20des=20justificatifs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../routing/admin_accounting/journal.yml | 8 ++ .../pages/administration/compta_journal.php | 100 ------------------ .../administration/compta_journal.html | 2 +- .../administration/compta_journal.js.html | 4 + .../Journal/UploadAttachmentAction.php | 69 ++++++++++++ 5 files changed, 82 insertions(+), 101 deletions(-) create mode 100644 sources/AppBundle/Controller/Admin/Accounting/Journal/UploadAttachmentAction.php diff --git a/app/config/routing/admin_accounting/journal.yml b/app/config/routing/admin_accounting/journal.yml index b10c03a51..9b9ce10e0 100644 --- a/app/config/routing/admin_accounting/journal.yml +++ b/app/config/routing/admin_accounting/journal.yml @@ -42,3 +42,11 @@ admin_accounting_journal_update_info: methods: [POST] requirements: id: \d+ + +admin_accounting_journal_upload: + path: /upload/{id} + defaults: + _controller: AppBundle\Controller\Admin\Accounting\Journal\UploadAttachmentAction + methods: [POST] + requirements: + id: \d+ diff --git a/htdocs/pages/administration/compta_journal.php b/htdocs/pages/administration/compta_journal.php index 15434a10c..7d1877f81 100755 --- a/htdocs/pages/administration/compta_journal.php +++ b/htdocs/pages/administration/compta_journal.php @@ -251,103 +251,3 @@ $smarty->assign('formulaire', genererFormulaire($formulaire)); } - -/** - * Upload an attachment and save it on the specific line. - * We save the uploads in a directory at the same month of the line - * and we don't forget to rename the file with the date of the line - * and a unique identifier to keep it safe. - * If the line already has an attachment, we remove it before saving - * the new one in the line. - */ elseif ($action === 'upload_attachment') { - try { - // Bad request? - if (!isset($_GET['id']) || !($line = $compta->obtenir((int) $_GET['id']))) { - throw new Exception("Please verify parameters", 400); - } - - // Test line existence - if (!$line['id']) { - throw new Exception("Not found", 404); - } - - // Avoid multiple upload - if ( - !isset($_FILES['file']['error']) - || is_array($_FILES['file']['error']) - ) { - throw new RuntimeException('Invalid parameters. You can\'t upload multiple files.'); - } - - // The directory - $directory = date('Ym', strtotime((string) $line['date_ecriture'])) . DIRECTORY_SEPARATOR; - $uploadDirectory = AFUP_CHEMIN_RACINE . 'uploads' . DIRECTORY_SEPARATOR . $directory; - if (!is_dir($uploadDirectory)) { - mkdir($uploadDirectory, 0750, true); - } - - // Get the file, rename it, and move it. - // Check $_FILES['file']['error'] value. - switch ($_FILES['file']['error']) { - case UPLOAD_ERR_OK: - break; - case UPLOAD_ERR_NO_FILE: - throw new RuntimeException('No file sent.'); - case UPLOAD_ERR_INI_SIZE: - case UPLOAD_ERR_FORM_SIZE: - throw new RuntimeException('Exceeded filesize limit.'); - default: - throw new RuntimeException('Unknown errors.'); - } - - // You should also check filesize here. - if ($_FILES['upfile']['size'] > 1000000) { - throw new RuntimeException('Exceeded filesize limit.'); - } - - // Check MIME Type - $finfo = new finfo(FILEINFO_MIME_TYPE); - if (false === $ext = array_search( - $finfo->file($_FILES['file']['tmp_name']), - [ - 'jpg' => 'image/jpeg', - 'png' => 'image/png', - 'pdf' => 'application/pdf', - ], - true, - )) { - throw new RuntimeException('Invalid file format. Only jpg/png/pdf allowed.'); - } - - // Move/Rename - $filename = sprintf('%s.%s', - date('Y-m-d', strtotime((string) $line['date_ecriture'])) . '_' . $line['id'] . '_' . substr(sha1_file($_FILES['file']['tmp_name']), 0, 6), - $ext, - ); - $moved = move_uploaded_file( - $_FILES['file']['tmp_name'], - $uploadDirectory . $filename, - ); - if (!$moved) { - throw new RuntimeException('Failed to move uploaded file.'); - } - - // Remove old file if exists - if ($line['attachment_filename']) { - $oldFilename = AFUP_CHEMIN_RACINE . 'uploads' . DIRECTORY_SEPARATOR . $line['attachment_filename']; - if (is_file($oldFilename)) { - unlink($oldFilename); - } - } - - // Update line - $compta->modifierColonne($line['id'], 'attachment_filename', $directory . $filename); - - header('HTTP/1.1 200 OK'); - header('X-Info: File uploaded \o/'); - } catch (Exception $e) { - header('HTTP/1.1 400 Bad Request'); - echo $e->getMessage(); - } - exit; -} diff --git a/htdocs/templates/administration/compta_journal.html b/htdocs/templates/administration/compta_journal.html index 5071aac95..d583ba1d3 100644 --- a/htdocs/templates/administration/compta_journal.html +++ b/htdocs/templates/administration/compta_journal.html @@ -163,7 +163,7 @@

Journal

data-position="left center" data-tooltip="Cliquez ou déposez un fichier dans la zone pour ajouter un justificatif" > -