Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / BackgroundJob / CreatePeriodicInvoices.pm
index 52415f2..4e32063 100644 (file)
@@ -16,6 +16,7 @@ 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);
@@ -142,8 +143,10 @@ 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;
@@ -177,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;
 
@@ -224,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,
@@ -351,6 +363,29 @@ sub _store_pdf_in_webdav {
   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) = @_;
 
@@ -416,6 +451,7 @@ sub _email_invoice {
       longdescription => 'html',
       partnotes       => 'html',
       notes           => 'html',
+      $::form->get_variable_content_types_for_cvars,
     },
   );
 
@@ -428,7 +464,8 @@ sub _email_invoice {
   eval {
     $pdf_file_name = $self->create_pdf(%create_params);
 
-    $self->_store_pdf_in_webdav($pdf_file_name, $data->{invoice});
+    $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(
@@ -436,7 +473,7 @@ sub _email_invoice {
         invoice          => $data->{invoice},
         vars             => $data->{time_period_vars},
         attribute        => $_,
-        attribute_format => 'text'
+        attribute_format => ($_ eq 'email_body' ? 'html' : 'text')
       );
     }
 
@@ -452,6 +489,8 @@ sub _email_invoice {
       $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} = [{
         path     => $pdf_file_name,
         name     => sprintf('%s %s.pdf', $label, $data->{invoice}->invnumber),