WebshopApi: falsche sql update Abhängigkeit
[kivitendo-erp.git] / SL / BackgroundJob / CreatePeriodicInvoices.pm
index 4777025..055dd16 100644 (file)
@@ -7,6 +7,7 @@ use parent qw(SL::BackgroundJob::Base);
 use Config::Std;
 use DateTime::Format::Strptime;
 use English qw(-no_match_vars);
+use List::MoreUtils qw(uniq);
 
 use SL::DB::AuthUser;
 use SL::DB::Default;
@@ -14,7 +15,9 @@ use SL::DB::Order;
 use SL::DB::Invoice;
 use SL::DB::PeriodicInvoice;
 use SL::DB::PeriodicInvoicesConfig;
+use SL::Helper::CreatePDF qw(create_pdf find_template);
 use SL::Mailer;
+use SL::Util qw(trim);
 
 sub create_job {
   $_[0]->create_standard_job('0 3 1 * *'); # first day of month at 3:00 am
@@ -24,6 +27,8 @@ sub run {
   my $self        = shift;
   $self->{db_obj} = shift;
 
+  $self->{job_errors} = [];
+
   my $configs = SL::DB::Manager::PeriodicInvoicesConfig->get_all(query => [ active => 1 ]);
 
   foreach my $config (@{ $configs }) {
@@ -31,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);
+  my (@new_invoices, @invoices_to_print, @invoices_to_email, @disabled_orders);
 
   _log_msg("Number of configs: " . scalar(@{ $configs}));
 
@@ -47,26 +52,43 @@ sub run {
     _log_msg("Dates: " . join(' ', map { $_->to_lxoffice } @dates));
 
     foreach my $date (@dates) {
-      my $invoice = $self->_create_periodic_invoice($config, $date);
-      next unless $invoice;
+      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 " . $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, $config ] if $config->print;
+      push @new_invoices,      $data;
+      push @invoices_to_print, $data if $config->print;
+      push @invoices_to_email, $data if $config->send_email;
 
       # 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;
   }
 
-  map { _print_invoice(@{ $_ }) } @invoices_to_print;
+  foreach my $inv ( @invoices_to_print ) { $self->_print_invoice($inv); }
+  foreach my $inv ( @invoices_to_email ) { $self->_email_invoice($inv); }
 
-  _send_email(\@new_invoices, [ map { $_->[0] } @invoices_to_print ]) if @new_invoices;
+  $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 = join "\n", @{ $self->{job_errors} };
+    _log_msg("Errors: $msg");
+    die $msg;
+  }
 
   return 1;
 }
 
 sub _log_msg {
-  my $message  = join('', @_);
+  my $message  = join('', 'SL::BackgroundJob::CreatePeriodicInvoices: ', @_);
   $message    .= "\n" unless $message =~ m/\n$/;
   $::lxdebug->message(LXDebug::DEBUG1(), $message);
 }
@@ -74,7 +96,7 @@ sub _log_msg {
 sub _generate_time_period_variables {
   my $config            = shift;
   my $period_start_date = shift;
-  my $period_end_date   = $period_start_date->clone->truncate(to => 'month')->add(months => $config->get_period_length)->subtract(days => 1);
+  my $period_end_date   = $period_start_date->clone->add(months => $config->get_billing_period_length)->subtract(days => 1);
 
   my @month_names       = ('',
                            $::locale->text('January'), $::locale->text('February'), $::locale->text('March'),     $::locale->text('April'),   $::locale->text('May'),      $::locale->text('June'),
@@ -97,37 +119,83 @@ sub _generate_time_period_variables {
     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,                                   sub { $::locale->format_date(\%::myconfig, $_[0]) } ],
+    period_start_date   => [ $period_start_date->clone, sub { $::locale->format_date(\%::myconfig, $_[0]) } ],
+    period_end_date     => [ $period_end_date,          sub { $::locale->format_date(\%::myconfig, $_[0]) } ],
   };
 
   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 _adjust_sellprices_for_period_lengths {
+  my (%params) = @_;
+
+  my $billing_len     = $params{config}->get_billing_period_length;
+  my $order_value_len = $params{config}->get_order_value_period_length;
+
+  return if $billing_len == $order_value_len;
+
+  my $is_last_invoice_in_cycle = $params{config}->is_last_bill_date_in_order_value_cycle(date => $params{period_start_date});
+
+  _log_msg("_adjust_sellprices_for_period_lengths: period_start_date $params{period_start_date} is_last_invoice_in_cycle $is_last_invoice_in_cycle billing_len $billing_len order_value_len $order_value_len");
+
+  if ($order_value_len < $billing_len) {
+    my $num_orders_per_invoice = $billing_len / $order_value_len;
+
+    $_->sellprice($_->sellprice * $num_orders_per_invoice) for @{ $params{invoice}->items };
+
+    return;
+  }
+
+  my $num_invoices_in_cycle = $order_value_len / $billing_len;
+
+  foreach my $item (@{ $params{invoice}->items }) {
+    my $sellprice_one_invoice = $::form->round_amount($item->sellprice * $billing_len / $order_value_len, 2);
+
+    if ($is_last_invoice_in_cycle) {
+      $item->sellprice($item->sellprice - ($num_invoices_in_cycle - 1) * $sellprice_one_invoice);
+
+    } else {
+      $item->sellprice($sellprice_one_invoice);
+    }
+  }
 }
 
 sub _create_periodic_invoice {
@@ -141,7 +209,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);
@@ -152,42 +220,57 @@ sub _create_periodic_invoice {
     $invoice->assign_attributes(deliverydate => $period_start_date,
                                 intnotes     => $intnotes,
                                 employee     => $order->employee, # new_from sets employee to import user
+                                direct_debit => $config->direct_debit,
                                );
 
-    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;
+    _adjust_sellprices_for_period_lengths(invoice => $invoice, config => $config, period_start_date => $period_start_date);
 
-    # like $form->add_shipto, but we don't need to check for a manual exception,
-    # because we can already assume this (otherwise no shipto_id from order)
-    if ($order->shipto_id) {
+    $invoice->post(ar_id => $config->ar_chart_id) || die;
 
-      my $shipto_oe = SL::DB::Manager::Shipto->find_by(shipto_id => $order->shipto_id);
-      my $shipto_ar = $shipto_oe->clone_and_reset;
+    $order->link_to_record($invoice);
 
-      $shipto_ar->module('AR');            # alter module OE -> AR
-      $shipto_ar->trans_id($invoice->id);  # alter trans_id -> new id from invoice
-      $shipto_ar->save;
+    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"};
+         }
+      }
     }
 
-    $order->link_to_record($invoice);
-
     SL::DB::PeriodicInvoice->new(config_id         => $config->id,
                                  ar_id             => $invoice->id,
                                  period_start_date => $period_start_date)
       ->save;
 
+    _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;
   }
 
-  return $invoice;
+  return {
+    config            => $config,
+    period_start_date => $period_start_date,
+    invoice           => $invoice,
+    time_period_vars  => $time_period_vars,
+  };
 }
 
 sub _calculate_dates {
@@ -195,9 +278,9 @@ sub _calculate_dates {
   return $config->calculate_invoice_dates(end_date => DateTime->today_local);
 }
 
-sub _send_email {
-  my ($posted_invoices, $printed_invoices) = @_;
-
+sub _send_summary_email {
+  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 };
@@ -218,7 +301,9 @@ sub _send_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 );
+                         PRINTED_INVOICES => $printed_invoices,
+                         EMAILED_INVOICES => $emailed_invoices,
+                         DISABLED_ORDERS  => $disabled_orders );
 
   my $output;
   $template->process($filename, \%params, \$output);
@@ -234,7 +319,10 @@ sub _send_email {
 }
 
 sub _print_invoice {
-  my ($invoice, $config) = @_;
+  my ($self, $data) = @_;
+
+  my $invoice       = $data->{invoice};
+  my $config        = $data->{config};
 
   return unless $config->print && $config->printer_id && $config->printer->printer_command;
 
@@ -249,16 +337,91 @@ sub _print_invoice {
   $form->{OUT}          = $config->printer->printer_command;
   $form->{OUT_MODE}     = '|-';
 
+  $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);
       1;
-    } || die $EVAL_ERROR->getMessage;
+    } or do {
+      push @{ $self->{job_errors} }, $EVAL_ERROR->getMessage;
+    };
   });
 }
 
+sub _email_invoice {
+  my ($self, $data) = @_;
+
+  $data->{config}->load;
+
+  return unless $data->{config}->send_email;
+
+  my @recipients =
+    uniq
+    map  { lc       }
+    grep { $_       }
+    map  { trim($_) }
+    (split(m{,}, $data->{config}->email_recipient_address),
+     $data->{config}->email_recipient_contact ? ($data->{config}->email_recipient_contact->cp_email) : ());
+
+  return unless @recipients;
+
+  my %create_params = (
+    template               => $self->find_template(name => 'invoice'),
+    variables              => Form->new(''),
+    return                 => 'file_name',
+    variable_content_types => {
+      longdescription => 'html',
+      partnotes       => 'html',
+      notes           => 'html',
+    },
+  );
+
+  $data->{invoice}->flatten_to_form($create_params{variables}, format_amounts => 1);
+  $create_params{variables}->prepare_for_printing;
+
+  my $pdf_file_name;
+
+  eval {
+    $pdf_file_name = $self->create_pdf(%create_params);
+
+    for (qw(email_subject email_body)) {
+      _replace_vars(
+        object           => $data->{config},
+        vars             => $data->{time_period_vars},
+        attribute        => $_,
+        attribute_format => 'text'
+      );
+    }
+
+    for my $recipient (@recipients) {
+      my $mail             = Mailer->new;
+      $mail->{from}        = $data->{config}->email_sender || $::lx_office_conf{periodic_invoices}->{email_from};
+      $mail->{to}          = $recipient;
+      $mail->{subject}     = $data->{config}->email_subject;
+      $mail->{message}     = $data->{config}->email_body;
+      $mail->{attachments} = [{
+        path     => $pdf_file_name,
+        name     => sprintf('%s %s.pdf', $::locale->text('Invoice'), $data->{invoice}->invnumber),
+      }];
+
+      my $error        = $mail->send;
+
+      push @{ $self->{job_errors} }, $error if $error;
+    }
+
+    1;
+
+  } or do {
+    push @{ $self->{job_errors} }, $EVAL_ERROR;
+  };
+
+  unlink $pdf_file_name if $pdf_file_name;
+}
+
 1;
 
 __END__