Merge branch 'master' into currency
[kivitendo-erp.git] / SL / BackgroundJob / CreatePeriodicInvoices.pm
index 226883c..a316c3a 100644 (file)
@@ -22,7 +22,7 @@ sub run {
   my $self        = shift;
   $self->{db_obj} = shift;
 
-  my $configs = SL::DB::Manager::PeriodicInvoicesConfig->get_all(where => [ active => 1 ]);
+  my $configs = SL::DB::Manager::PeriodicInvoicesConfig->get_all(query => [ active => 1 ]);
 
   foreach my $config (@{ $configs }) {
     my $new_end_date = $config->handle_automatic_extension;
@@ -50,23 +50,23 @@ sub run {
 
       _log_msg("Invoice " . $invoice->invnumber . " posted for config ID " . $config->id . ", period start date " . $::locale->format_date(\%::myconfig, $date) . "\n");
       push @new_invoices,      $invoice;
-      push @invoices_to_print, $invoice if $config->print;
+      push @invoices_to_print, [ $invoice, $config ] if $config->print;
 
       # last;
     }
   }
 
-  map { _print_invoice($_) } @invoices_to_print;
+  map { _print_invoice(@{ $_ }) } @invoices_to_print;
 
-  _send_email(\@new_invoices, \@invoices_to_print) if @new_invoices;
+  _send_email(\@new_invoices, [ map { $_->[0] } @invoices_to_print ]) if @new_invoices;
 
   return 1;
 }
 
 sub _log_msg {
-  my $message  = join('', @_);
-  $message    .= "\n" unless $message =~ m/\n$/;
-  # $::lxdebug->message(0, $message);
+  my $message  = join('', @_);
+  $message    .= "\n" unless $message =~ m/\n$/;
+  $::lxdebug->message(LXDebug::DEBUG1(), $message);
 }
 
 sub _generate_time_period_variables {
@@ -75,8 +75,8 @@ sub _generate_time_period_variables {
   my $period_end_date   = $period_start_date->clone->truncate(to => 'month')->add(months => $config->get_period_length)->subtract(days => 1);
 
   my @month_names       = ('',
-                           'Januar', 'Februar', 'März',      'April',   'Mai',      'Juni',
-                           'Juli',   'August',  'September', 'Oktober', 'November', 'Dezember');
+                           $::locale->text('January'), $::locale->text('February'), $::locale->text('March'),     $::locale->text('April'),   $::locale->text('May'),      $::locale->text('June'),
+                           $::locale->text('July'),    $::locale->text('August'),   $::locale->text('September'), $::locale->text('October'), $::locale->text('November'), $::locale->text('December'));
 
   my $vars = { current_quarter     => $period_start_date->quarter,
                previous_quarter    => $period_start_date->clone->subtract(months => 3)->quarter,
@@ -184,7 +184,7 @@ sub _calculate_dates {
 sub _send_email {
   my ($posted_invoices, $printed_invoices) = @_;
 
-  read_config 'config/periodic_invoices.conf' => my %config;
+  my %config = %::lx_office_conf;
 
   return if !$config{periodic_invoices} || !$config{periodic_invoices}->{send_email_to} || !scalar @{ $posted_invoices };
 
@@ -219,6 +219,32 @@ sub _send_email {
   $mail->send;
 }
 
+sub _print_invoice {
+  my ($invoice, $config) = @_;
+
+  return unless $config->print && $config->printer_id && $config->printer->printer_command;
+
+  my $form = Form->new;
+  $invoice->flatten_to_form($form, format_amounts => 1);
+
+  $form->{printer_code} = $config->printer->template_code;
+  $form->{copies}       = $config->copies;
+  $form->{formname}     = $form->{type};
+  $form->{format}       = 'pdf';
+  $form->{media}        = 'printer';
+  $form->{OUT}          = $config->printer->printer_command;
+  $form->{OUT_MODE}     = '|-';
+
+  $form->prepare_for_printing;
+
+  $form->throw_on_error(sub {
+    eval {
+      $form->parse_template(\%::myconfig);
+      1;
+    } || die $EVAL_ERROR->getMessage;
+  });
+}
+
 1;
 
 __END__
@@ -247,10 +273,6 @@ each date.
 
 Strings like month names are hardcoded to German in this file.
 
-=item *
-
-Implement printing the invoices if requested.
-
 =back
 
 =head1 AUTHOR