Merge branch 'debian' into b-3.6.1
[kivitendo-erp.git] / SL / BackgroundJob / CreatePeriodicInvoices.pm
index 1b57cbe..4e32063 100644 (file)
@@ -9,15 +9,18 @@ use DateTime::Format::Strptime;
 use English qw(-no_match_vars);
 use List::MoreUtils qw(uniq);
 
+use SL::Common;
 use SL::DB::AuthUser;
 use SL::DB::Default;
 use SL::DB::Order;
 use SL::DB::Invoice;
 use SL::DB::PeriodicInvoice;
 use SL::DB::PeriodicInvoicesConfig;
+use SL::File;
 use SL::Helper::CreatePDF qw(create_pdf find_template);
 use SL::Mailer;
 use SL::Util qw(trim);
+use SL::System::Process;
 
 sub create_job {
   $_[0]->create_standard_job('0 3 1 * *'); # first day of month at 3:00 am
@@ -27,62 +30,69 @@ sub run {
   my $self        = shift;
   $self->{db_obj} = shift;
 
-  $self->{job_errors} = [];
+  $self->{$_} = [] for qw(job_errors posted_invoices printed_invoices printed_failed emailed_invoices emailed_failed disabled_orders);
 
-  my $configs = SL::DB::Manager::PeriodicInvoicesConfig->get_all(query => [ active => 1 ]);
+  if (!$self->{db_obj}->db->with_transaction(sub {
+    1;                          # make Emacs happy
 
-  foreach my $config (@{ $configs }) {
-    my $new_end_date = $config->handle_automatic_extension;
-    _log_msg("Periodic invoice configuration ID " . $config->id . " extended through " . $new_end_date->strftime('%d.%m.%Y') . "\n") if $new_end_date;
-  }
+    my $configs = SL::DB::Manager::PeriodicInvoicesConfig->get_all(query => [ active => 1 ]);
 
-  my (@new_invoices, @invoices_to_print, @invoices_to_email, @disabled_orders);
+    foreach my $config (@{ $configs }) {
+      my $new_end_date = $config->handle_automatic_extension;
+      _log_msg("Periodic invoice configuration ID " . $config->id . " extended through " . $new_end_date->strftime('%d.%m.%Y') . "\n") if $new_end_date;
+    }
+
+    my (@invoices_to_print, @invoices_to_email);
 
-  _log_msg("Number of configs: " . scalar(@{ $configs}));
+    _log_msg("Number of configs: " . scalar(@{ $configs}));
 
-  foreach my $config (@{ $configs }) {
-    # A configuration can be set to inactive by
-    # $config->handle_automatic_extension. Therefore the check in
-    # ...->get_all() does not suffice.
-    _log_msg("Config " . $config->id . " active " . $config->active);
-    next unless $config->active;
+    foreach my $config (@{ $configs }) {
+      # A configuration can be set to inactive by
+      # $config->handle_automatic_extension. Therefore the check in
+      # ...->get_all() does not suffice.
+      _log_msg("Config " . $config->id . " active " . $config->active);
+      next unless $config->active;
 
-    my @dates = _calculate_dates($config);
+      my @dates = _calculate_dates($config);
 
-    _log_msg("Dates: " . join(' ', map { $_->to_lxoffice } @dates));
+      _log_msg("Dates: " . join(' ', map { $_->to_lxoffice } @dates));
 
-    foreach my $date (@dates) {
-      my $data = $self->_create_periodic_invoice($config, $date);
-      next unless $data;
+      foreach my $date (@dates) {
+        my $data = $self->_create_periodic_invoice($config, $date);
+        next unless $data;
 
-      _log_msg("Invoice " . $data->{invoice}->invnumber . " posted for config ID " . $config->id . ", period start date " . $::locale->format_date(\%::myconfig, $date) . "\n");
+        _log_msg("Invoice " . $data->{invoice}->invnumber . " posted for config ID " . $config->id . ", period start date " . $::locale->format_date(\%::myconfig, $date) . "\n");
 
-      push @new_invoices,      $data;
-      push @invoices_to_print, $data if $config->print;
-      push @invoices_to_email, $data if $config->send_email;
+        push @{ $self->{posted_invoices} }, $data->{invoice};
+        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 @{ $self->{disabled_orders} }, $inactive_ordnumber;
+          last;
+        }
+      }
     }
-    # disable one time configs (periodicity is only one time).
-    my $inactive_ordnumber = $config->disable_one_time_config;
-    push @disabled_orders, $inactive_ordnumber if $inactive_ordnumber;
-  }
 
-  foreach my $inv ( @invoices_to_print ) { $self->_print_invoice($inv); }
-  foreach my $inv ( @invoices_to_email ) { $self->_email_invoice($inv); }
+    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  ,
-  );
+    $self->_send_summary_email;
 
-  if (@{ $self->{job_errors} }) {
-    my $msg = join "\n", @{ $self->{job_errors} };
-    _log_msg("Errors: $msg");
-    die $msg;
-  }
+      1;
+    })) {
+      $::lxdebug->message(LXDebug->WARN(), "_create_invoice failed: " . join("\n", (split(/\n/, $self->{db_obj}->db->error))[0..2]));
+      return undef;
+    }
+
+    if (@{ $self->{job_errors} }) {
+      my $msg = join "\n", @{ $self->{job_errors} };
+      _log_msg("Errors: $msg");
+      die $msg;
+    }
 
   return 1;
 }
@@ -133,16 +143,15 @@ sub _replace_vars {
   my $sub_fmt  = lc($params{attribute_format} // 'text');
 
   my ($start_tag, $end_tag) = $sub_fmt eq 'html' ? ('&lt;%', '%&gt;') : ('<%', '%>');
+  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;
 
-    if (!$params{vars}->{$key}) {
-      $new_value = '';
-
-    } elsif ($format) {
+    if ($params{vars}->{$key} && $format) {
       $format    = $::locale->unquote_special_chars('html', $format) if $sub_fmt eq 'html';
 
       $new_value = DateTime::Format::Strptime->new(
@@ -151,11 +160,15 @@ sub _replace_vars {
         time_zone   => 'local',
       )->format_datetime($params{vars}->{$key}->[0]);
 
-    } else {
+    } elsif ($params{vars}->{$key}) {
       $new_value = $params{vars}->{$1}->[1]->($params{vars}->{$1}->[0]);
+
+    } elsif ($params{invoice} && $params{invoice}->can($key)) {
+      $new_value = $params{invoice}->$key;
     }
 
-    $new_value = $::locale->quote_special_chars('html', $new_value) if $sub_fmt eq 'html';
+    $new_value //= '';
+    $new_value   = $::locale->quote_special_chars('html', $new_value) if $sub_fmt eq 'html';
 
     $new_value;
 
@@ -167,6 +180,8 @@ sub _replace_vars {
 sub _adjust_sellprices_for_period_lengths {
   my (%params) = @_;
 
+  return if $params{config}->periodicity eq 'o';
+
   my $billing_len     = $params{config}->get_billing_period_length;
   my $order_value_len = $params{config}->get_order_value_period_length;
 
@@ -214,10 +229,17 @@ sub _create_periodic_invoice {
 
     $invoice = SL::DB::Invoice->new_from($order);
 
+    my $tax_point = ($invoice->tax_point // $time_period_vars->{period_end_date}->[0])->clone;
+
+    while ($tax_point < $period_start_date) {
+      $tax_point->add(months => $config->get_billing_period_length);
+    }
+
     my $intnotes  = $invoice->intnotes ? $invoice->intnotes . "\n\n" : '';
     $intnotes    .= "Automatisch am " . $invdate->to_lxoffice . " erzeugte Rechnung";
 
     $invoice->assign_attributes(deliverydate => $period_start_date,
+                                tax_point    => $tax_point,
                                 intnotes     => $intnotes,
                                 employee     => $order->employee, # new_from sets employee to import user
                                 direct_debit => $config->direct_debit,
@@ -279,14 +301,20 @@ sub _calculate_dates {
 }
 
 sub _send_summary_email {
-  my ($self, $posted_invoices, $printed_invoices, $emailed_invoices,
-      $disabled_orders) = @_;
+  my ($self) = @_;
   my %config = %::lx_office_conf;
 
-  return if !$config{periodic_invoices} || !$config{periodic_invoices}->{send_email_to} || !scalar @{ $posted_invoices };
+  return if !$config{periodic_invoices} || !$config{periodic_invoices}->{send_email_to} || !scalar @{ $self->{posted_invoices} };
 
-  my $user  = SL::DB::Manager::AuthUser->find_by(login => $config{periodic_invoices}->{send_email_to});
-  my $email = $user ? $user->get_config_value('email') : undef;
+  return if $config{periodic_invoices}->{send_for_errors_only} && !@{ $self->{printed_failed} } && !@{ $self->{emailed_failed} };
+
+  my $email = $config{periodic_invoices}->{send_email_to};
+  if ($email !~ m{\@}) {
+    my $user = SL::DB::Manager::AuthUser->find_by(login => $email);
+    $email   = $user ? $user->get_config_value('email') : undef;
+  }
+
+  _log_msg("_send_summary_email: about to send to '" . ($email || '') . "'");
 
   return unless $email;
 
@@ -300,13 +328,10 @@ sub _send_summary_email {
 
   my $email_template = $config{periodic_invoices}->{email_template};
   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,
-                         DISABLED_ORDERS  => $disabled_orders );
+  my %params         = map { (uc($_) => $self->{$_}) } qw(posted_invoices printed_invoices printed_failed emailed_invoices emailed_failed disabled_orders);
 
   my $output;
-  $template->process($filename, \%params, \$output);
+  $template->process($filename, \%params, \$output) || die $template->error;
 
   my $mail              = Mailer->new;
   $mail->{from}         = $config{periodic_invoices}->{email_from};
@@ -318,6 +343,49 @@ sub _send_summary_email {
   $mail->send;
 }
 
+sub _store_pdf_in_webdav {
+  my ($self, $pdf_file_name, $invoice) = @_;
+
+  return unless $::instance_conf->get_webdav_documents;
+
+  my $form = Form->new('');
+
+  $form->{cwd}              = SL::System::Process->exe_dir;
+  $form->{tmpdir}           = ($pdf_file_name =~ m{(.+)/})[0];
+  $form->{tmpfile}          = ($pdf_file_name =~ m{.+/(.+)})[0];
+  $form->{format}           = 'pdf';
+  $form->{formname}         = 'invoice';
+  $form->{type}             = 'invoice';
+  $form->{vc}               = 'customer';
+  $form->{invnumber}        = $invoice->invnumber;
+  $form->{recipient_locale} = $invoice->language ? $invoice->language->template_code : '';
+
+  Common::copy_file_to_webdav_folder($form);
+}
+
+sub _store_pdf_in_filemanagement {
+  my ($self, $pdf_file, $invoice) = @_;
+
+  return unless $::instance_conf->get_doc_storage;
+
+  # create a form for generate_attachment_filename
+  my $form = Form->new('');
+  $form->{invnumber} = $invoice->invnumber;
+  $form->{type}      = 'invoice';
+  $form->{format}    = 'pdf';
+  $form->{formname}  = 'invoice';
+  $form->{language}  = '_' . $invoice->language->template_code if $invoice->language;
+  my $doc_name       = $form->generate_attachment_filename();
+
+  SL::File->save(object_id   => $invoice->id,
+                 object_type => 'invoice',
+                 mime_type   => 'application/pdf',
+                 source      => 'created',
+                 file_type   => 'document',
+                 file_name   => $doc_name,
+                 file_path   => $pdf_file);
+}
+
 sub _print_invoice {
   my ($self, $data) = @_;
 
@@ -337,22 +405,19 @@ 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;
 
   $form->throw_on_error(sub {
     eval {
       $form->parse_template(\%::myconfig);
+      push @{ $self->{printed_invoices} }, $invoice;
       1;
     } or do {
-      push @{ $self->{job_errors} }, $EVAL_ERROR->getMessage;
+      push @{ $self->{job_errors} }, $EVAL_ERROR->error;
+      push @{ $self->{printed_failed} }, [ $invoice, $EVAL_ERROR->error ];
     };
   });
 }
@@ -370,18 +435,23 @@ sub _email_invoice {
     grep { $_       }
     map  { trim($_) }
     (split(m{,}, $data->{config}->email_recipient_address),
-     $data->{config}->email_recipient_contact ? ($data->{config}->email_recipient_contact->cp_email) : ());
+     $data->{config}->email_recipient_contact   ? ($data->{config}->email_recipient_contact->cp_email) : (),
+     $data->{invoice}->{customer}->invoice_mail ? ($data->{invoice}->{customer}->invoice_mail) : ()
+    );
 
   return unless @recipients;
 
+  my $language      = $data->{invoice}->language ? $data->{invoice}->language->template_code : undef;
   my %create_params = (
-    template               => $self->find_template(name => 'invoice'),
+    template               => scalar($self->find_template(name => 'invoice', language => $language)),
     variables              => Form->new(''),
     return                 => 'file_name',
+    record                 => $data->{invoice},
     variable_content_types => {
       longdescription => 'html',
       partnotes       => 'html',
       notes           => 'html',
+      $::form->get_variable_content_types_for_cvars,
     },
   );
 
@@ -389,39 +459,59 @@ sub _email_invoice {
   $create_params{variables}->prepare_for_printing;
 
   my $pdf_file_name;
+  my $label = $language && Locale::is_supported($language) ? Locale->new($language)->text('Invoice') : $::locale->text('Invoice');
 
   eval {
     $pdf_file_name = $self->create_pdf(%create_params);
 
+    $self->_store_pdf_in_webdav        ($pdf_file_name, $data->{invoice});
+    $self->_store_pdf_in_filemanagement($pdf_file_name, $data->{invoice});
+
     for (qw(email_subject email_body)) {
       _replace_vars(
         object           => $data->{config},
+        invoice          => $data->{invoice},
         vars             => $data->{time_period_vars},
         attribute        => $_,
-        attribute_format => 'text'
+        attribute_format => ($_ eq 'email_body' ? 'html' : 'text')
       );
     }
 
+    my $global_bcc = SL::DB::Default->get->global_bcc;
+    my $overall_error;
+
     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->{message}    .= SL::DB::Default->get->signature;
+      $mail->{content_type} = 'text/html';
       $mail->{attachments} = [{
-        filename => $pdf_file_name,
-        name     => sprintf('%s %s.pdf', $::locale->text('Invoice'), $data->{invoice}->invnumber),
+        path     => $pdf_file_name,
+        name     => sprintf('%s %s.pdf', $label, $data->{invoice}->invnumber),
       }];
 
       my $error        = $mail->send;
 
-      push @{ $self->{job_errors} }, $error if $error;
+      if ($error) {
+        push @{ $self->{job_errors} }, $error;
+        push @{ $self->{emailed_failed} }, [ $data->{invoice}, $error ];
+        $overall_error = 1;
+      }
     }
 
+    push @{ $self->{emailed_invoices} }, $data->{invoice} unless $overall_error;
+
     1;
 
   } or do {
     push @{ $self->{job_errors} }, $EVAL_ERROR;
+    push @{ $self->{emailed_failed} }, [ $data->{invoice}, $EVAL_ERROR ];
   };
 
   unlink $pdf_file_name if $pdf_file_name;