From 46f9d91b44c432aa017f44b6b2f53114e190428e Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 14 Feb 2019 14:48:38 +0100 Subject: [PATCH] Drucken: Variablen in Positionen interpolieren MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Alle Variablen, die in Schleifen ausgegeben werden (z.B. Artikelbeschreibung, Langtext, aber auch die Steuern) werden nun ebenfalls der Interpolation unterzogen, sodass z.B. in wiederkehrenden Rechnungen <%current_quarter%> in Artikelbeschreibungen genutzt werden kann — aber auch Belegfelder. --- SL/BackgroundJob/CreatePeriodicInvoices.pm | 4 ++- SL/DB/MetaSetup/Default.pm | 1 + SL/Form.pm | 26 +++++++++++++++++++ bin/mozilla/io.pl | 4 +++ doc/changelog | 4 +++ locale/de/all | 2 ++ ...int_interpolate_variables_in_positions.sql | 6 +++++ .../client_config/_miscellaneous.html | 6 +++++ 8 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 sql/Pg-upgrade2/defaults_print_interpolate_variables_in_positions.sql diff --git a/SL/BackgroundJob/CreatePeriodicInvoices.pm b/SL/BackgroundJob/CreatePeriodicInvoices.pm index 6151ec158..58f1febab 100644 --- a/SL/BackgroundJob/CreatePeriodicInvoices.pm +++ b/SL/BackgroundJob/CreatePeriodicInvoices.pm @@ -143,8 +143,10 @@ sub _replace_vars { my $sub_fmt = lc($params{attribute_format} // 'text'); my ($start_tag, $end_tag) = $sub_fmt eq 'html' ? ('<%', '%>') : ('<%', '%>'); + my @invoice_keys = $params{invoice} ? (map { $_->name } $params{invoice}->meta->columns) : (); + my $key_name_re = join '|', map { quotemeta } (@invoice_keys, keys %{ $params{vars} }); - $str =~ s{ ${start_tag} ([a-z0-9_]+) ( \s+ format \s*=\s* (.*?) \s* )? ${end_tag} }{ + $str =~ s{ ${start_tag} ($key_name_re) ( \s+ format \s*=\s* (.*?) \s* )? ${end_tag} }{ my ($key, $format) = ($1, $3); $key = $::locale->unquote_special_chars('html', $key) if $sub_fmt eq 'html'; my $new_value; diff --git a/SL/DB/MetaSetup/Default.pm b/SL/DB/MetaSetup/Default.pm index 97e3b7058..86da0f835 100644 --- a/SL/DB/MetaSetup/Default.pm +++ b/SL/DB/MetaSetup/Default.pm @@ -131,6 +131,7 @@ __PACKAGE__->meta->columns( pdonumber => { type => 'text' }, ponumber => { type => 'text' }, precision => { type => 'numeric', default => '0.01', not_null => 1, precision => 15, scale => 5 }, + print_interpolate_variables_in_positions => { type => 'boolean', default => 'true', not_null => 1 }, produce_assembly_same_warehouse => { type => 'boolean', default => 'true' }, produce_assembly_transfer_service => { type => 'boolean', default => 'false' }, profit_carried_forward_chart_id => { type => 'integer' }, diff --git a/SL/Form.pm b/SL/Form.pm index 06c0b2317..a95f4e0db 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -3214,6 +3214,32 @@ sub prepare_for_printing { today => DateTime->today, }; + if ($defaults->print_interpolate_variables_in_positions) { + $self->substitute_placeholders_in_template_arrays({ field => 'description', type => 'text' }, { field => 'longdescription', type => 'html' }); + } + + return $self; +} + +sub substitute_placeholders_in_template_arrays { + my ($self, @fields) = @_; + + foreach my $spec (@fields) { + $spec = { field => $spec, type => 'text' } if !ref($spec); + my $field = $spec->{field}; + + next unless exists $self->{TEMPLATE_ARRAYS} && exists $self->{TEMPLATE_ARRAYS}->{$field}; + + my $tag_start = $spec->{type} eq 'html' ? '<%' : '<%'; + my $tag_end = $spec->{type} eq 'html' ? '%>' : '%>'; + my $formatter = $spec->{type} eq 'html' ? sub { $::locale->quote_special_chars('html', $_[0] // '') } : sub { $_[0] }; + + $self->{TEMPLATE_ARRAYS}->{$field} = [ + apply { s{${tag_start}(.+?)${tag_end}}{ $formatter->($self->{$1}) }eg } + @{ $self->{TEMPLATE_ARRAYS}->{$field} } + ]; + } + return $self; } diff --git a/bin/mozilla/io.pl b/bin/mozilla/io.pl index a69521b12..917720c9d 100644 --- a/bin/mozilla/io.pl +++ b/bin/mozilla/io.pl @@ -1570,6 +1570,10 @@ sub print_form { today => DateTime->today, }; + if ($defaults->print_interpolate_variables_in_positions) { + $form->substitute_placeholders_in_template_arrays({ field => 'description', type => 'text' }, { field => 'longdescription', type => 'html' }); + } + $form->parse_template(\%myconfig); $form->{callback} = ""; diff --git a/doc/changelog b/doc/changelog index 3004a0df2..9443ffd93 100644 --- a/doc/changelog +++ b/doc/changelog @@ -35,6 +35,10 @@ Kleinere neue Features und Detailverbesserungen: - Pflichtenhefte: wenn in einem Auftrag, das mit einem Pflichtenheft verknüpft ist, ein Projekt ausgewählt, so wird dieses Projekt auch automatisch beim verknüpften Pflichtenheft eingetragen. +- Druckvorlagen: die in Positionen verwendeten Variablen können nun + Platzhalter enthalten, die vom Beleg selber stammen. So könnte + z.B. in der Artikelbeschreibung automatisch die Rechnungsnummer + ersetzt werden. Beispiel: »Abrechnungszeitraum bis <%invnumber%>« Bugfixes (Tracker: https://www.kivitendo.de/redmine): diff --git a/locale/de/all b/locale/de/all index fbc82037b..291b30ad1 100755 --- a/locale/de/all +++ b/locale/de/all @@ -1798,6 +1798,7 @@ $self->{texts} = { 'Internal Phone List' => 'Interne Telefonliste', 'Internal comment' => 'Interne Bemerkungen', 'Internet' => 'Internet', + 'Interpolate variables in texts of positions' => 'Variablen in Positionstexten interpolieren', 'Into bin' => 'Eingelagert', 'Intra-Community supply' => 'Gelangensbestätigung', 'Introduction of clients' => 'Einführung von Mandanten', @@ -4150,6 +4151,7 @@ $self->{texts} = { 'What this template contains' => 'Was diese Vorlage enthält', 'What type of item is this?' => 'Was ist dieser Artikel?', 'When converting a requirement spec into a quotation or an oder each section gets converted into a line position in the new record. This is the article used by default for this conversion.' => 'Wenn ein Pflichtenheft in ein Angebot oder Auftrag umgewandelt wird, wird für jeden Abschnitt eine Position im neuen Beleg angelegt. Dies ist der Artikel, der standardmäßig bei dieser Umwandlung genutzt wird.', + 'Whether or not to replace variable placeholders such as "<%invdate%>" in texts in positions such as the part description by the record\'s actual value' => 'Ob Variablenplatzhalter wie z.B. <%invdate%> in Positionstexten wie der Artikelbeschreibung durch den tatsächlichen Wert aus dem Beleg ersetzt werden sollen', 'Which is located at doc/kivitendo-Dokumentation.pdf. Click here: ' => 'Diese befindet sich unter doc/kivitendo-Dokumentation.pdf. Klicken Sie hier:', 'With Attachments' => 'Journal mit Anhängen', 'With Extension Of Time' => 'mit Dauerfristverlängerung', diff --git a/sql/Pg-upgrade2/defaults_print_interpolate_variables_in_positions.sql b/sql/Pg-upgrade2/defaults_print_interpolate_variables_in_positions.sql new file mode 100644 index 000000000..04aa91731 --- /dev/null +++ b/sql/Pg-upgrade2/defaults_print_interpolate_variables_in_positions.sql @@ -0,0 +1,6 @@ +-- @tag: defaults_print_interpolate_variables_in_positions +-- @description: Drucken: Variablen in Belegpositionen interpolieren (abschaltbar via Mandantenkonfiguration) +-- @depends: release_3_5_8 +ALTER TABLE defaults +ADD COLUMN print_interpolate_variables_in_positions BOOLEAN +DEFAULT TRUE NOT NULL; diff --git a/templates/webpages/client_config/_miscellaneous.html b/templates/webpages/client_config/_miscellaneous.html index f938f7268..06067676e 100644 --- a/templates/webpages/client_config/_miscellaneous.html +++ b/templates/webpages/client_config/_miscellaneous.html @@ -99,6 +99,12 @@ + + [% LxERP.t8('Interpolate variables in texts of positions') %] + [% L.yes_no_tag('defaults.print_interpolate_variables_in_positions', SELF.defaults.print_interpolate_variables_in_positions) %] + [% LxERP.t8('Whether or not to replace variable placeholders such as "<%invdate%>" in texts in positions such as the part description by the record\'s actual value') %] + + [% LxERP.t8("Currencies") %] -- 2.20.1