TopQuickSearch für den Benutzer konfigurierbar gemacht.
[kivitendo-erp.git] / SL / BackgroundJob / CreatePeriodicInvoices.pm
index ad16949..b7b3e33 100644 (file)
@@ -36,7 +36,7 @@ sub run {
     _log_msg("Periodic invoice configuration ID " . $config->id . " extended through " . $new_end_date->strftime('%d.%m.%Y') . "\n") if $new_end_date;
   }
 
-  my (@new_invoices, @invoices_to_print, @invoices_to_email);
+  my (@new_invoices, @invoices_to_print, @invoices_to_email, @disabled_orders);
 
   _log_msg("Number of configs: " . scalar(@{ $configs}));
 
@@ -61,21 +61,28 @@ sub run {
       push @invoices_to_print, $data if $config->print;
       push @invoices_to_email, $data if $config->send_email;
 
-      # last;
+      my $inactive_ordnumber = $config->disable_one_time_config;
+      if ($inactive_ordnumber) {
+        # disable one time configs and skip eventual invoices
+        _log_msg("Order " . $inactive_ordnumber . " deavtivated \n");
+        push @disabled_orders, $inactive_ordnumber;
+        last;
+      }
     }
   }
 
-  $self->_print_invoice($_) for @invoices_to_print;
-  $self->_email_invoice($_) for @invoices_to_email;
+  foreach my $inv ( @invoices_to_print ) { $self->_print_invoice($inv); }
+  foreach my $inv ( @invoices_to_email ) { $self->_email_invoice($inv); }
 
   $self->_send_summary_email(
     [ map { $_->{invoice} } @new_invoices      ],
     [ map { $_->{invoice} } @invoices_to_print ],
     [ map { $_->{invoice} } @invoices_to_email ],
+                             \@disabled_orders  ,
   );
 
   if (@{ $self->{job_errors} }) {
-    my $msg = @{ $self->{job_errors} };
+    my $msg = join "\n", @{ $self->{job_errors} };
     _log_msg("Errors: $msg");
     die $msg;
   }
@@ -205,7 +212,7 @@ sub _create_periodic_invoice {
 
   my $order   = $config->order;
   my $invoice;
-  if (!$self->{db_obj}->db->do_transaction(sub {
+  if (!$self->{db_obj}->db->with_transaction(sub {
     1;                          # make Emacs happy
 
     $invoice = SL::DB::Invoice->new_from($order);
@@ -254,6 +261,8 @@ sub _create_periodic_invoice {
     _log_msg("_create_invoice created for period start date $period_start_date id " . $invoice->id . " number " . $invoice->invnumber . " netamount " . $invoice->netamount . " amount " . $invoice->amount);
 
     # die $invoice->transaction_description;
+
+    1;
   })) {
     $::lxdebug->message(LXDebug->WARN(), "_create_invoice failed: " . join("\n", (split(/\n/, $self->{db_obj}->db->error))[0..2]));
     return undef;
@@ -273,8 +282,8 @@ sub _calculate_dates {
 }
 
 sub _send_summary_email {
-  my ($self, $posted_invoices, $printed_invoices, $emailed_invoices) = @_;
-
+  my ($self, $posted_invoices, $printed_invoices, $emailed_invoices,
+      $disabled_orders) = @_;
   my %config = %::lx_office_conf;
 
   return if !$config{periodic_invoices} || !$config{periodic_invoices}->{send_email_to} || !scalar @{ $posted_invoices };
@@ -296,7 +305,8 @@ sub _send_summary_email {
   my $filename       = $email_template || ( (SL::DB::Default->get->templates || "templates/webpages") . "/oe/periodic_invoices_email.txt" );
   my %params         = ( POSTED_INVOICES  => $posted_invoices,
                          PRINTED_INVOICES => $printed_invoices,
-                         EMAILED_INVOICES => $emailed_invoices );
+                         EMAILED_INVOICES => $emailed_invoices,
+                         DISABLED_ORDERS  => $disabled_orders );
 
   my $output;
   $template->process($filename, \%params, \$output);
@@ -330,13 +340,8 @@ sub _print_invoice {
   $form->{OUT}          = $config->printer->printer_command;
   $form->{OUT_MODE}     = '|-';
 
-  $form->{TEMPLATE_DRIVER_OPTIONS} = {
-    variable_content_types => {
-      longdescription => 'html',
-      partnotes       => 'html',
-      notes           => 'html',
-    },
-  };
+  $form->{TEMPLATE_DRIVER_OPTIONS} = { };
+  $form->{TEMPLATE_DRIVER_OPTIONS}->{variable_content_types} = $form->get_variable_content_types();
 
   $form->prepare_for_printing;
 
@@ -395,14 +400,19 @@ sub _email_invoice {
       );
     }
 
+    my $global_bcc = SL::DB::Default->get->global_bcc;
+
     for my $recipient (@recipients) {
       my $mail             = Mailer->new;
+      $mail->{record_id}   = $data->{invoice}->id,
+      $mail->{record_type} = 'invoice',
       $mail->{from}        = $data->{config}->email_sender || $::lx_office_conf{periodic_invoices}->{email_from};
       $mail->{to}          = $recipient;
+      $mail->{bcc}         = $global_bcc;
       $mail->{subject}     = $data->{config}->email_subject;
       $mail->{message}     = $data->{config}->email_body;
       $mail->{attachments} = [{
-        filename => $pdf_file_name,
+        path     => $pdf_file_name,
         name     => sprintf('%s %s.pdf', $::locale->text('Invoice'), $data->{invoice}->invnumber),
       }];