From 402d73671136cc70408a9c6acd0b683ac5ed1b9a Mon Sep 17 00:00:00 2001 From: JorgeU Date: Wed, 19 Aug 2026 09:44:11 +0200 Subject: [PATCH 1/3] allow attaching additional files when sending invoice by mail --- .../invoices/config/schemadb_send_mail.php | 11 ++++++++++ .../invoices/handler/invoice/action.php | 20 +++++++++++++++++++ .../openpsa/invoices/locale/default.de.txt | 4 ++++ .../openpsa/invoices/locale/default.en.txt | 4 ++++ 4 files changed, 39 insertions(+) diff --git a/lib/org/openpsa/invoices/config/schemadb_send_mail.php b/lib/org/openpsa/invoices/config/schemadb_send_mail.php index 1df7829be..f43e1762e 100644 --- a/lib/org/openpsa/invoices/config/schemadb_send_mail.php +++ b/lib/org/openpsa/invoices/config/schemadb_send_mail.php @@ -25,6 +25,17 @@ 'widget' => 'textarea', 'required' => true, ], + 'additional_attachments' => [ + 'title' => 'additional_attachments', + 'storage' => 'additional_attachments', + 'type' => 'select', + 'type_config' => [ + 'options' => [], + 'allow_multiple' => true, + ], + 'widget' => 'radiocheckselect', + 'hidden' => true, + ], ], ], ]; \ No newline at end of file diff --git a/lib/org/openpsa/invoices/handler/invoice/action.php b/lib/org/openpsa/invoices/handler/invoice/action.php index ddce819c5..5a4d0fc0c 100644 --- a/lib/org/openpsa/invoices/handler/invoice/action.php +++ b/lib/org/openpsa/invoices/handler/invoice/action.php @@ -12,6 +12,7 @@ use Symfony\Component\HttpFoundation\Request; use midcom\datamanager\controller; use midcom\datamanager\datamanager; +use midcom\datamanager\storage\blobs; /** * Invoice action handler @@ -176,6 +177,16 @@ private function get_email_type_config() : array private function load_send_mail_controller(array $config) : controller { $schemadb = schemadb::from_path($this->_config->get('schemadb_send_mail')); + + $attachment_options = []; + foreach (blobs::get_attachments($this->invoice, 'files') as $attachment) { + $attachment_options[$attachment->guid] = $attachment->name; + } + if (!empty($attachment_options)) { + $schemadb->get('default')->get_field('additional_attachments')['type_config']['options'] = $attachment_options; + $schemadb->get('default')->get_field('additional_attachments')['hidden'] = false; + } + $dm = new datamanager($schemadb); $billing_data = $this->invoice->get_billing_data(true); $to_email = $billing_data->email ?: $this->mail_recipient->email; @@ -243,6 +254,15 @@ public function save_callback(controller $controller) 'content' => $attachment->read() ]; + foreach ($data['additional_attachments'] as $additional_attachment_guid) { + $additional_attachment = new midcom_db_attachment($additional_attachment_guid); + $mail->attachments[] = [ + 'name' => $additional_attachment->name, + 'mimetype' => $additional_attachment->mimetype, + 'content' => $additional_attachment->read() + ]; + } + // define replacements for subject / body $mail->parameters = [ "INVOICE_LABEL" => $invoice_label, diff --git a/lib/org/openpsa/invoices/locale/default.de.txt b/lib/org/openpsa/invoices/locale/default.de.txt index 3fcc5b251..5060196c7 100644 --- a/lib/org/openpsa/invoices/locale/default.de.txt +++ b/lib/org/openpsa/invoices/locale/default.de.txt @@ -480,6 +480,10 @@ Betreff Nachricht ---STRINGEND +---STRING additional_attachments +Weitere Anhänge +---STRINGEND + ---STRING reminder_send_by_mail Zahlungserinnerung versenden ---STRINGEND diff --git a/lib/org/openpsa/invoices/locale/default.en.txt b/lib/org/openpsa/invoices/locale/default.en.txt index f40e3d492..bf7cb63f3 100644 --- a/lib/org/openpsa/invoices/locale/default.en.txt +++ b/lib/org/openpsa/invoices/locale/default.en.txt @@ -468,6 +468,10 @@ Subject Message ---STRINGEND +---STRING additional_attachments +Additional attachments +---STRINGEND + ---STRING reminder_send_by_mail Send payment reminder ---STRINGEND From 3593255d2fa47fd2b341e9d608f0024e1ec19e5c Mon Sep 17 00:00:00 2001 From: JorgeU Date: Tue, 25 Aug 2026 15:32:50 +0200 Subject: [PATCH 2/3] make invoice PDF a checkable/uncheckable attachment option --- .../invoices/config/schemadb_send_mail.php | 7 ++-- .../invoices/handler/invoice/action.php | 32 +++++++------------ .../openpsa/invoices/locale/default.de.txt | 4 +-- .../openpsa/invoices/locale/default.en.txt | 4 +-- 4 files changed, 19 insertions(+), 28 deletions(-) diff --git a/lib/org/openpsa/invoices/config/schemadb_send_mail.php b/lib/org/openpsa/invoices/config/schemadb_send_mail.php index f43e1762e..b8eec9b71 100644 --- a/lib/org/openpsa/invoices/config/schemadb_send_mail.php +++ b/lib/org/openpsa/invoices/config/schemadb_send_mail.php @@ -25,16 +25,15 @@ 'widget' => 'textarea', 'required' => true, ], - 'additional_attachments' => [ - 'title' => 'additional_attachments', - 'storage' => 'additional_attachments', + 'attachments' => [ + 'title' => 'attachments', + 'storage' => 'attachments', 'type' => 'select', 'type_config' => [ 'options' => [], 'allow_multiple' => true, ], 'widget' => 'radiocheckselect', - 'hidden' => true, ], ], ], diff --git a/lib/org/openpsa/invoices/handler/invoice/action.php b/lib/org/openpsa/invoices/handler/invoice/action.php index 5a4d0fc0c..a0fa1ede3 100644 --- a/lib/org/openpsa/invoices/handler/invoice/action.php +++ b/lib/org/openpsa/invoices/handler/invoice/action.php @@ -178,14 +178,14 @@ private function load_send_mail_controller(array $config) : controller { $schemadb = schemadb::from_path($this->_config->get('schemadb_send_mail')); - $attachment_options = []; + $pdf_helper = new org_openpsa_invoices_invoice_pdf($this->invoice); + $invoice_pdf = $pdf_helper->get_attachment(true); + + $attachment_options = [$invoice_pdf->guid => $invoice_pdf->name]; foreach (blobs::get_attachments($this->invoice, 'files') as $attachment) { $attachment_options[$attachment->guid] = $attachment->name; } - if (!empty($attachment_options)) { - $schemadb->get('default')->get_field('additional_attachments')['type_config']['options'] = $attachment_options; - $schemadb->get('default')->get_field('additional_attachments')['hidden'] = false; - } + $schemadb->get('default')->get_field('attachments')['type_config']['options'] = $attachment_options; $dm = new datamanager($schemadb); $billing_data = $this->invoice->get_billing_data(true); @@ -194,7 +194,8 @@ private function load_send_mail_controller(array $config) : controller $dm->set_defaults([ 'to_email'=> $to_email, 'subject' => $config['subject'], - 'message' => $config['message'] + 'message' => $config['message'], + 'attachments' => serialize([$invoice_pdf->guid]), ]); return $dm->get_controller(); @@ -244,22 +245,13 @@ public function save_callback(controller $controller) } $invoice_date = $this->_l10n->get_formatter()->date($this->invoice->date); - $pdf_helper = new org_openpsa_invoices_invoice_pdf($this->invoice); - $attachment = $pdf_helper->get_attachment(true); - $mail = new org_openpsa_mail(); - $mail->attachments[] = [ - 'name' => $attachment->name, - 'mimetype' => "application/pdf", - 'content' => $attachment->read() - ]; - - foreach ($data['additional_attachments'] as $additional_attachment_guid) { - $additional_attachment = new midcom_db_attachment($additional_attachment_guid); + foreach ($data['attachments'] as $attachment_guid) { + $attachment = new midcom_db_attachment($attachment_guid); $mail->attachments[] = [ - 'name' => $additional_attachment->name, - 'mimetype' => $additional_attachment->mimetype, - 'content' => $additional_attachment->read() + 'name' => $attachment->name, + 'mimetype' => $attachment->mimetype, + 'content' => $attachment->read() ]; } diff --git a/lib/org/openpsa/invoices/locale/default.de.txt b/lib/org/openpsa/invoices/locale/default.de.txt index 5060196c7..e30058270 100644 --- a/lib/org/openpsa/invoices/locale/default.de.txt +++ b/lib/org/openpsa/invoices/locale/default.de.txt @@ -480,8 +480,8 @@ Betreff Nachricht ---STRINGEND ----STRING additional_attachments -Weitere Anhänge +---STRING attachments +Anhänge ---STRINGEND ---STRING reminder_send_by_mail diff --git a/lib/org/openpsa/invoices/locale/default.en.txt b/lib/org/openpsa/invoices/locale/default.en.txt index bf7cb63f3..e2ca65894 100644 --- a/lib/org/openpsa/invoices/locale/default.en.txt +++ b/lib/org/openpsa/invoices/locale/default.en.txt @@ -468,8 +468,8 @@ Subject Message ---STRINGEND ----STRING additional_attachments -Additional attachments +---STRING attachments +Attachments ---STRINGEND ---STRING reminder_send_by_mail From b01a9b8dd92e67679f43b5d0c40c433898e67dc4 Mon Sep 17 00:00:00 2001 From: JorgeU Date: Tue, 25 Aug 2026 16:42:42 +0200 Subject: [PATCH 3/3] add a dummy PDF renderer for the invoice mailer unittests --- .../invoices/handler/invoice/actionTest.php | 5 ++++ test/utilities/autoload.php | 1 + test/utilities/helpers/pdfbuilder.php | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 test/utilities/helpers/pdfbuilder.php diff --git a/test/org/openpsa/invoices/handler/invoice/actionTest.php b/test/org/openpsa/invoices/handler/invoice/actionTest.php index 1c1ec53d0..69e2fa250 100644 --- a/test/org/openpsa/invoices/handler/invoice/actionTest.php +++ b/test/org/openpsa/invoices/handler/invoice/actionTest.php @@ -14,6 +14,7 @@ use org_openpsa_invoices_invoice_item_dba; use midcom; use midcom_db_topic; +use mock_pdfbuilder; /** * OpenPSA testcase @@ -143,6 +144,8 @@ public function testHandler_mark_paid() public function testHandler_send_by_mail() { + $this->set_config('org.openpsa.invoices', 'invoice_pdfbuilder_class', mock_pdfbuilder::class); + midcom::get()->auth->request_sudo('org.openpsa.invoices'); $invoice = $this->create_object(org_openpsa_invoices_invoice_dba::class, [ @@ -158,6 +161,8 @@ public function testHandler_send_by_mail() public function testHandler_send_payment_reminder() { + $this->set_config('org.openpsa.invoices', 'invoice_pdfbuilder_class', mock_pdfbuilder::class); + midcom::get()->auth->request_sudo('org.openpsa.invoices'); $invoice = $this->create_object(org_openpsa_invoices_invoice_dba::class, [ diff --git a/test/utilities/autoload.php b/test/utilities/autoload.php index 662f8b681..09f41c4ff 100644 --- a/test/utilities/autoload.php +++ b/test/utilities/autoload.php @@ -16,3 +16,4 @@ require __DIR__ . '/helpers/sessioning.php'; require __DIR__ . '/helpers/relocate.php'; +require __DIR__ . '/helpers/pdfbuilder.php'; diff --git a/test/utilities/helpers/pdfbuilder.php b/test/utilities/helpers/pdfbuilder.php new file mode 100644 index 000000000..429132766 --- /dev/null +++ b/test/utilities/helpers/pdfbuilder.php @@ -0,0 +1,24 @@ +