]> wagnertech.de Git - mfinanz.git/blobdiff - SL/BackgroundJob/CreatePeriodicInvoices.pm
CreatePeriodicInvoices: HTML-Formatierung in Langtexten berücksichtigen Teil 2
[mfinanz.git] / SL / BackgroundJob / CreatePeriodicInvoices.pm
index 18abf7c1adb6b4e98d500a7e70ef0d6ac9a5e498..d80ba4eeba36a7770c1d21a221ac42b4adad2756 100644 (file)
@@ -58,7 +58,7 @@ sub run {
     }
   }
 
-  map { _print_invoice(@{ $_ }) } @invoices_to_print;
+  _print_invoice(@{ $_ }) for @invoices_to_print;
 
   _send_email(\@new_invoices, [ map { $_->[0] } @invoices_to_print ]) if @new_invoices;
 
@@ -89,46 +89,62 @@ sub _generate_time_period_variables {
     previous_month      => [ $period_start_date->clone->truncate(to => 'month')->subtract(months => 1), sub { $_[0]->month } ],
     next_month          => [ $period_start_date->clone->truncate(to => 'month')->add(     months => 1), sub { $_[0]->month } ],
 
+    current_month_long  => [ $period_start_date->clone->truncate(to => 'month'),                        sub { $month_names[ $_[0]->month ] } ],
+    previous_month_long => [ $period_start_date->clone->truncate(to => 'month')->subtract(months => 1), sub { $month_names[ $_[0]->month ] } ],
+    next_month_long     => [ $period_start_date->clone->truncate(to => 'month')->add(     months => 1), sub { $month_names[ $_[0]->month ] } ],
+
     current_year        => [ $period_start_date->clone->truncate(to => 'year'),                         sub { $_[0]->year } ],
     previous_year       => [ $period_start_date->clone->truncate(to => 'year')->subtract(years => 1),   sub { $_[0]->year } ],
     next_year           => [ $period_start_date->clone->truncate(to => 'year')->add(     years => 1),   sub { $_[0]->year } ],
 
     period_start_date   => [ $period_start_date->clone->truncate(to => 'month'), sub { $::locale->format_date(\%::myconfig, $_[0]) } ],
-    period_end_date     => [ $period_end_date  ->clone->truncate(to => 'month'), sub { $::locale->format_date(\%::myconfig, $_[0]) } ],
+    period_end_date     => [ $period_end_date,                                   sub { $::locale->format_date(\%::myconfig, $_[0]) } ],
   };
 
-  map { $vars->{"${_}_month_long"} = $month_names[ $vars->{"${_}_month"} ] } qw(current previous next);
-
   return $vars;
 }
 
 sub _replace_vars {
-  my $object = shift;
-  my $vars   = shift;
-  my $sub    = shift;
-  my $str    = $object->$sub;
+  my (%params) = @_;
+  my $sub      = $params{attribute};
+  my $str      = $params{object}->$sub;
+  my $sub_fmt  = lc($params{attribute_format} // 'text');
 
-  $str =~ s{ <\% ([a-z0-9_]+) ( \s+ format \s*=\s* (.*?) \s* )? \%>}{
+  my ($start_tag, $end_tag) = $sub_fmt eq 'html' ? ('&lt;%', '%&gt;') : ('<%', '%>');
+
+  $str =~ s{ ${start_tag} ([a-z0-9_]+) ( \s+ format \s*=\s* (.*?) \s* )? ${end_tag} }{
     my ($key, $format) = ($1, $3);
-    if (!$vars->{$key}) {
-      '';
+    $key               = $::locale->unquote_special_chars('html', $key) if $sub_fmt eq 'html';
+    my $new_value;
+
+    if (!$params{vars}->{$key}) {
+      $new_value = '';
 
     } elsif ($format) {
-      DateTime::Format::Strptime->new(
+      $format    = $::locale->unquote_special_chars('html', $format) if $sub_fmt eq 'html';
+
+      $new_value = DateTime::Format::Strptime->new(
         pattern     => $format,
         locale      => 'de_DE',
         time_zone   => 'local',
-      )->format_datetime($vars->{$key}->[0]);
+      )->format_datetime($params{vars}->{$key}->[0]);
 
     } else {
-      $vars->{$1}->[1]->($vars->{$1}->[0]);
+      $new_value = $params{vars}->{$1}->[1]->($params{vars}->{$1}->[0]);
     }
+
+    $new_value = $::locale->quote_special_chars('html', $new_value) if $sub_fmt eq 'html';
+
+    $new_value;
+
   }eigx;
 
-  $object->$sub($str);
+  $params{object}->$sub($str);
 }
 
 sub _create_periodic_invoice {
+  $main::lxdebug->enter_sub();
+
   my $self              = shift;
   my $config            = shift;
   my $period_start_date = shift;
@@ -149,12 +165,13 @@ sub _create_periodic_invoice {
 
     $invoice->assign_attributes(deliverydate => $period_start_date,
                                 intnotes     => $intnotes,
+                                employee     => $order->employee, # new_from sets employee to import user
                                );
 
-    map { _replace_vars($invoice, $time_period_vars, $_) } qw(notes intnotes transaction_description);
+    _replace_vars(object => $invoice, vars => $time_period_vars, attribute => $_, attribute_format => ($_ eq 'notes' ? 'html' : 'text')) for qw(notes intnotes transaction_description);
 
     foreach my $item (@{ $invoice->items }) {
-      map { _replace_vars($item, $time_period_vars, $_) } qw(description longdescription);
+      _replace_vars(object => $item, vars => $time_period_vars, attribute => $_, attribute_format => ($_ eq 'longdescription' ? 'html' : 'text')) for qw(description longdescription);
     }
 
     $invoice->post(ar_id => $config->ar_chart_id) || die;
@@ -173,6 +190,21 @@ sub _create_periodic_invoice {
 
     $order->link_to_record($invoice);
 
+    foreach my $item (@{ $invoice->items }) {
+      foreach (qw(orderitems)) {    # expand if needed (delivery_order_items)
+          if ($item->{"converted_from_${_}_id"}) {
+            die unless $item->{id};
+            RecordLinks->create_links('mode'       => 'ids',
+                                      'from_table' => $_,
+                                      'from_ids'   => $item->{"converted_from_${_}_id"},
+                                      'to_table'   => 'invoice',
+                                      'to_id'      => $item->{id},
+            ) || die;
+            delete $item->{"converted_from_${_}_id"};
+         }
+      }
+    }
+
     SL::DB::PeriodicInvoice->new(config_id         => $config->id,
                                  ar_id             => $invoice->id,
                                  period_start_date => $period_start_date)
@@ -183,7 +215,7 @@ sub _create_periodic_invoice {
     $::lxdebug->message(LXDebug->WARN(), "_create_invoice failed: " . join("\n", (split(/\n/, $self->{db_obj}->db->error))[0..2]));
     return undef;
   }
-
+  $main::lxdebug->leave_sub();
   return $invoice;
 }