Skip to content

Commit

Permalink
Add logic for reminder generation (#219)
Browse files Browse the repository at this point in the history
* first commit

* make class parameers optional / change classnames

* add ability to distinguish between attachments

* repair shemadb_default

* remove superflous line

* remove lacalization

* add "create_reminder"  to localization

* remove lacalization from admin folder

* repaired midgard l10n

* make class name dynamic

* add typehint to get_attachment

* hide button, when not needed / codingstyle

* reminder button appers after 14 days

* use overdue for status

* remove $invoice variable /change reminder classname

* change seccess / error message reminder pdf creation

* change config /remove lacalization

* change dialog window on reminder re-creation

* remove 'kind switch'

* remove "has alredy sent" message for overdue reminder

* include reminder view

* remove nested if from populate_toolbar()

* add l10n to filename

* coding style / remove $kind variable / change l10n

* change label name to reminder

* remove bracelets

* add reminder to l10n

* change Mahnung to mahnung.

* change: title/spelling/logic

* change mahnung lcfirst

* add test

---------

Co-authored-by: Simon Rautenberg <[email protected]>
  • Loading branch information
SimonRautenberg and Simon Rautenberg committed Apr 11, 2024
1 parent 0b96910 commit 76451e6
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/midcom/admin/folder/locale/default.de.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,4 @@ Stilvorlage

---STRING objects with no navigation entry are still accessible on-site if the url is known
Objekte ohne Navigationseintrag sind weiterhin verfügbar, insofern die URL bekannt ist
---STRINGEND
---STRINGEND
2 changes: 1 addition & 1 deletion lib/midcom/admin/folder/locale/default.en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,4 @@ Style template

---STRING objects with no navigation entry are still accessible on-site if the url is known
Objects that are hidden from navigation are still accessible on-site if the URL is known
---STRINGEND
---STRINGEND
1 change: 1 addition & 0 deletions lib/org/openpsa/invoices/config/config.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'invoice_number_format' => '#%06d',
//contains the class file to create the pdf for the invoice, if set to false pdf-creation is disabled
'invoice_pdfbuilder_class' => false, //implements org_openpsa_invoices_interfaces_pdfbuilder
'invoice_pdfbuilder_reminder_class' => false,

'invoice_mail_bcc' => false,
'invoice_mail_from_address' => '[email protected]',
Expand Down
4 changes: 4 additions & 0 deletions lib/org/openpsa/invoices/config/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ create_pdf:
path: /invoice/action/create_pdf/
defaults: { _controller: 'org_openpsa_invoices_handler_invoice_action::create_pdf' }

create_reminder:
path: /invoice/action/create_reminder/
defaults: { _controller: 'org_openpsa_invoices_handler_invoice_action::create_pdf_reminder' }

recalc_invoice:
path: /invoice/recalculation/{guid}/
defaults: { _controller: 'org_openpsa_invoices_handler_invoice_items::recalculation' }
Expand Down
11 changes: 11 additions & 0 deletions lib/org/openpsa/invoices/config/schemadb_default.inc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@
'index_method' => 'attachment',
'hidden' => true,
],
'pdf_file_reminder' => [
'title' => 'reminder',
'type' => 'blobs',
'widget' => 'downloads',
'type_config' => [
'sortable' => false,
'max_count' => 1,
],
'index_method' => 'attachment',
'hidden' => true,
],
'files' => [
'title' => 'Files',
'type' => 'blobs',
Expand Down
11 changes: 11 additions & 0 deletions lib/org/openpsa/invoices/handler/invoice/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ public function _handler_create_pdf()
}
}

public function _handler_create_pdf_reminder()
{
$pdf_helper = new org_openpsa_invoices_invoice_pdf($this->invoice);
try {
$pdf_helper->render_and_attach('reminder');
return $this->reply(true, $this->_l10n->get('reminder pdf created'));
} catch (midcom_error $e) {
return $this->reply(false, $this->_l10n->get('reminder pdf creation failed') . ': ' . $e->getMessage());
}
}

public function _handler_send_by_mail()
{
$customerCard = org_openpsa_widgets_contact::get($this->invoice->customerContact);
Expand Down
7 changes: 7 additions & 0 deletions lib/org/openpsa/invoices/handler/invoice/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ private function populate_toolbar()
}
}

if ($this->_config->get('invoice_pdfbuilder_reminder_class') && $this->invoice->get_status() == 'overdue') {
$button = $this->build_button('create_reminder', 'file-pdf-o');
$pdf_helper = new org_openpsa_invoices_invoice_pdf($this->invoice);
$button[MIDCOM_TOOLBAR_OPTIONS] = $pdf_helper->get_button_options('reminder');
$buttons[] = $button;
}

if ($this->invoice->is_cancelable()) {
$buttons[] = $this->build_button('create_cancelation', 'ban');
}
Expand Down
35 changes: 25 additions & 10 deletions lib/org/openpsa/invoices/invoice/pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ public function __construct(org_openpsa_invoices_invoice_dba $invoice)
$this->invoice = $invoice;
}

public function get_attachment(bool $autocreate = false) : ?midcom_db_attachment
public function get_attachment(bool $autocreate = false, string $kind = null) : ?midcom_db_attachment
{
$pdf_files = blobs::get_attachments($this->invoice, "pdf_file");
if ($kind == 'reminder') {
$pdf_files = blobs::get_attachments($this->invoice, "pdf_file_reminder");
} else {
$pdf_files = blobs::get_attachments($this->invoice, "pdf_file");
}
if (!empty($pdf_files)) {
return reset($pdf_files);
}
Expand All @@ -33,10 +37,10 @@ public function get_attachment(bool $autocreate = false) : ?midcom_db_attachment
return $this->render_and_attach();
}

public function get_button_options() : array
public function get_button_options(string $kind = 'invoice') : array
{
if ($attachment = $this->get_attachment()) {
if ($this->invoice->sent) {
if ($this->invoice->sent && $kind != 'reminder') {
$message = 'invoice has already been sent. should it be replaced?';
}
// check if auto generated parameter is same as md5 in current-file
Expand All @@ -63,9 +67,14 @@ public function get_button_options() : array
];
}

public function render_and_attach() : midcom_db_attachment
public function render_and_attach(string $kind = null) : midcom_db_attachment
{
$client_class = midcom_baseclasses_components_configuration::get('org.openpsa.invoices', 'config')->get('invoice_pdfbuilder_class');
if ($kind == null) {
$client_class = midcom_baseclasses_components_configuration::get('org.openpsa.invoices', 'config')->get('invoice_pdfbuilder_class');
} else {
$client_class = midcom_baseclasses_components_configuration::get('org.openpsa.invoices', 'config')->get('invoice_pdfbuilder_' . $kind . '_class');
}

if (!class_exists($client_class)) {
throw new midcom_error('Could not find PDF renderer ' . $client_class);
}
Expand All @@ -78,15 +87,21 @@ public function render_and_attach() : midcom_db_attachment
}
// renders the pdf and attaches it to the invoice
$pdf_builder = new $client_class($this->invoice);
$filename = midcom_helper_misc::urlize($this->invoice->get_label()) . '.pdf';
$l10n = midcom::get()->i18n->get_l10n('org.openpsa.invoices');
if ($kind == 'reminder') {
$name = "guids_pdf_file_reminder";
$filename = midcom_helper_misc::urlize($this->invoice->get_label()) . '_' . lcfirst($l10n->get('reminder')) . '.pdf';
} else {
$name = "guids_pdf_file";
$filename = midcom_helper_misc::urlize($this->invoice->get_label()) . '.pdf';
}

// tmp filename
$tmp_file = midcom::get()->config->get('midcom_tempdir') . "/" . $filename;

// render pdf to tmp filename
$pdf_builder->render($tmp_file);

$attachment = $this->get_attachment();
$attachment = $this->get_attachment(false, $kind);
if ($attachment) {
$attachment->name = $filename;
$attachment->title = $this->invoice->get_label();
Expand All @@ -95,7 +110,7 @@ public function render_and_attach() : midcom_db_attachment
} else {
$attachment = $this->invoice->create_attachment($filename, $this->invoice->get_label(), "application/pdf");
if ( !$attachment
|| !$this->invoice->set_parameter("midcom.helper.datamanager2.type.blobs", "guids_pdf_file", $attachment->guid . ":" . $attachment->guid)) {
|| !$this->invoice->set_parameter("midcom.helper.datamanager2.type.blobs", $name, $attachment->guid . ":" . $attachment->guid)) {
throw new midcom_error("Failed to create invoice attachment for pdf: " . midcom_connection::get_error_string());
}
}
Expand Down
16 changes: 16 additions & 0 deletions lib/org/openpsa/invoices/locale/default.de.txt
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,30 @@ Die derzeitige PDF-Datei wurde manuell hochgeladen. Soll diese Datei wirklich er
PDF erzeugen
---STRINGEND

---STRING reminder
Mahnung
---STRINGEND

---STRING create_reminder
Mahnung erzeugen
---STRINGEND

---STRING pdf created
PDF-Datei wurde erfolgreich erzeugt
---STRINGEND

---STRING reminder pdf created
Mahnungs PDF-Datei wurde erfolgreich erzeugt
---STRINGEND

---STRING pdf creation failed
PDF-Datei konnte nicht erzeugt werden
---STRINGEND

---STRING reminder pdf creation failed
Mahnungs PDF-Datei konnte nicht erzeugt werden
---STRINGEND

---STRING position
Position
---STRINGEND
4 changes: 4 additions & 0 deletions lib/org/openpsa/invoices/locale/default.en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,7 @@ Could not create PDF
---STRING position
Position
---STRINGEND

---STRING create_reminder
create reminder
---STRINGEND
4 changes: 4 additions & 0 deletions lib/org/openpsa/invoices/style/admin-read.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@
<p><strong><?php echo $data['l10n']->get('pdf file'); ?></strong></p>
<?php echo org_openpsa_helpers_fileinfo::render($invoice, 'pdf_file');
}
if ($view['pdf_file_reminder'] != "") { ?>
<p><strong><?php echo $data['l10n']->get('reminder'); ?></strong></p>
<?php echo org_openpsa_helpers_fileinfo::render($invoice, 'pdf_file_reminder');
}

$tabs = [];
if ($expenses_url) {
Expand Down
17 changes: 17 additions & 0 deletions test/org/openpsa/invoices/handler/invoice/actionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ public function testHandler_create_pdf()
midcom::get()->auth->drop_sudo();
}

public function testHandler_create_reminder()
{
midcom::get()->auth->request_sudo('org.openpsa.invoices');

$topic = $this->create_object(midcom_db_topic::class, ['component' => 'org.openpsa.invoices']);
$topic->set_parameter('org.openpsa.invoices', 'invoice_pdfbuilder_reminder_class', 'nonexistent');
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = [
'id' => self::$_invoice->id,
'relocate' => true
];
$url = $this->run_relocate_handler($topic, ['invoice', 'action', 'create_reminder']);
$this->assertEquals('invoice/' . self::$_invoice->guid . '/', $url);

midcom::get()->auth->drop_sudo();
}

public function testHandler_mark_sent()
{
midcom::get()->auth->request_sudo('org.openpsa.invoices');
Expand Down

0 comments on commit 76451e6

Please sign in to comment.