diff --git a/lib/org/openpsa/invoices/config/schemadb_send_mail.php b/lib/org/openpsa/invoices/config/schemadb_send_mail.php index 1df7829be..b8eec9b71 100644 --- a/lib/org/openpsa/invoices/config/schemadb_send_mail.php +++ b/lib/org/openpsa/invoices/config/schemadb_send_mail.php @@ -25,6 +25,16 @@ 'widget' => 'textarea', 'required' => true, ], + 'attachments' => [ + 'title' => 'attachments', + 'storage' => 'attachments', + 'type' => 'select', + 'type_config' => [ + 'options' => [], + 'allow_multiple' => true, + ], + 'widget' => 'radiocheckselect', + ], ], ], ]; \ 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..a0fa1ede3 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')); + + $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; + } + $schemadb->get('default')->get_field('attachments')['type_config']['options'] = $attachment_options; + $dm = new datamanager($schemadb); $billing_data = $this->invoice->get_billing_data(true); $to_email = $billing_data->email ?: $this->mail_recipient->email; @@ -183,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(); @@ -233,15 +245,15 @@ 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['attachments'] as $attachment_guid) { + $attachment = new midcom_db_attachment($attachment_guid); + $mail->attachments[] = [ + 'name' => $attachment->name, + 'mimetype' => $attachment->mimetype, + 'content' => $attachment->read() + ]; + } // define replacements for subject / body $mail->parameters = [ diff --git a/lib/org/openpsa/invoices/locale/default.de.txt b/lib/org/openpsa/invoices/locale/default.de.txt index 3fcc5b251..e30058270 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 attachments +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..e2ca65894 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 attachments +Attachments +---STRINGEND + ---STRING reminder_send_by_mail Send payment reminder ---STRINGEND 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 @@ +