POD Fehler
[kivitendo-erp.git] / SL / BackgroundJob / CreatePeriodicInvoices.pm
index c725a6f..9f116be 100644 (file)
@@ -5,9 +5,11 @@ use strict;
 use parent qw(SL::BackgroundJob::Base);
 
 use Config::Std;
+use DateTime::Format::Strptime;
 use English qw(-no_match_vars);
 
 use SL::DB::AuthUser;
+use SL::DB::Default;
 use SL::DB::Order;
 use SL::DB::Invoice;
 use SL::DB::PeriodicInvoice;
@@ -22,7 +24,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;
@@ -56,7 +58,7 @@ sub run {
     }
   }
 
-  map { _print_invoice(@{ $_ }) } @invoices_to_print;
+  _print_invoice(@{ $_ }) for @invoices_to_print;
 
   _send_email(\@new_invoices, [ map { $_->[0] } @invoices_to_print ]) if @new_invoices;
 
@@ -64,53 +66,119 @@ sub run {
 }
 
 sub _log_msg {
-  # my $message  = join('', @_);
-  $message    .= "\n" unless $message =~ m/\n$/;
-  # $::lxdebug->message(0, $message);
+  my $message  = join('', 'SL::BackgroundJob::CreatePeriodicInvoices: ', @_);
+  $message    .= "\n" unless $message =~ m/\n$/;
+  $::lxdebug->message(LXDebug::DEBUG1(), $message);
 }
 
 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->truncate(to => 'month')->add(months => $config->get_billing_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,
-               next_quarter        => $period_start_date->clone->add(     months => 3)->quarter,
+  my $vars = {
+    current_quarter     => [ $period_start_date->clone->truncate(to => 'month'),                        sub { $_[0]->quarter } ],
+    previous_quarter    => [ $period_start_date->clone->truncate(to => 'month')->subtract(months => 3), sub { $_[0]->quarter } ],
+    next_quarter        => [ $period_start_date->clone->truncate(to => 'month')->add(     months => 3), sub { $_[0]->quarter } ],
 
-               current_month       => $period_start_date->month,
-               previous_month      => $period_start_date->clone->subtract(months => 1)->month,
-               next_month          => $period_start_date->clone->add(     months => 1)->month,
+    current_month       => [ $period_start_date->clone->truncate(to => 'month'),                        sub { $_[0]->month } ],
+    previous_month      => [ $period_start_date->clone->truncate(to => 'month')->subtract(months => 1), sub { $_[0]->month } ],
+    next_month          => [ $period_start_date->clone->truncate(to => 'month')->add(     months => 1), sub { $_[0]->month } ],
 
-               current_year        => $period_start_date->year,
-               previous_year       => $period_start_date->year - 1,
-               next_year           => $period_start_date->year + 1,
+    current_month_long  => [ $period_start_date->clone->truncate(to => 'month'),                        sub { $month_names[ $_[0]->month ] } ],
+    previous_month_long => [ $period_start_date->clone->truncate(to => 'month')->subtract(months => 1), sub { $month_names[ $_[0]->month ] } ],
+    next_month_long     => [ $period_start_date->clone->truncate(to => 'month')->add(     months => 1), sub { $month_names[ $_[0]->month ] } ],
 
-               period_start_date   => $::locale->format_date(\%::myconfig, $period_start_date),
-               period_end_date     => $::locale->format_date(\%::myconfig, $period_end_date),
-             };
+    current_year        => [ $period_start_date->clone->truncate(to => 'year'),                         sub { $_[0]->year } ],
+    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 } ],
 
-  map { $vars->{"${_}_month_long"} = $month_names[ $vars->{"${_}_month"} ] } qw(current previous next);
+    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]) } ],
+  };
 
   return $vars;
 }
 
 sub _replace_vars {
-  my $object = shift;
-  my $vars   = shift;
-  my $sub    = shift;
-  my $str    = $object->$sub;
-
-  my ($key, $value);
-  $str =~ s|<\%${key}\%>|$value|g while ($key, $value) = each %{ $vars };
-  $object->$sub($str);
+  my (%params) = @_;
+  my $sub      = $params{attribute};
+  my $str      = $params{object}->$sub // '';
+  my $sub_fmt  = lc($params{attribute_format} // 'text');
+
+  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);
+    $key               = $::locale->unquote_special_chars('html', $key) if $sub_fmt eq 'html';
+    my $new_value;
+
+    if (!$params{vars}->{$key}) {
+      $new_value = '';
+
+    } elsif ($format) {
+      $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($params{vars}->{$key}->[0]);
+
+    } else {
+      $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;
+
+  $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 {
+  $main::lxdebug->enter_sub();
+
   my $self              = shift;
   my $config            = shift;
   my $period_start_date = shift;
@@ -131,54 +199,67 @@ sub _create_periodic_invoice {
 
     $invoice->assign_attributes(deliverydate => $period_start_date,
                                 intnotes     => $intnotes,
+                                employee     => $order->employee, # new_from sets employee to import user
                                );
 
-    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);
     }
 
+    _adjust_sellprices_for_period_lengths(invoice => $invoice, config => $config, period_start_date => $period_start_date);
+
     $invoice->post(ar_id => $config->ar_chart_id) || die;
 
+    # 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) {
+
+      my $shipto_oe = SL::DB::Manager::Shipto->find_by(shipto_id => $order->shipto_id);
+      my $shipto_ar = $shipto_oe->clone_and_reset;
+
+      $shipto_ar->module('AR');            # alter module OE -> AR
+      $shipto_ar->trans_id($invoice->id);  # alter trans_id -> new id from invoice
+      $shipto_ar->save;
+    }
+
     $order->link_to_record($invoice);
 
+    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"};
+         }
+      }
+    }
+
     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;
   })) {
     $::lxdebug->message(LXDebug->WARN(), "_create_invoice failed: " . join("\n", (split(/\n/, $self->{db_obj}->db->error))[0..2]));
     return undef;
   }
-
+  $main::lxdebug->leave_sub();
   return $invoice;
 }
 
 sub _calculate_dates {
-  my $config     = shift;
-
-  my $cur_date   = $config->start_date;
-  my $start_date = $config->get_previous_invoice_date || DateTime->new(year => 1970, month => 1, day => 1);
-  my $end_date   = $config->end_date                  || DateTime->new(year => 2100, month => 1, day => 1);
-  my $tomorrow   = DateTime->today_local->add(days => 1);
-  my $period_len = $config->get_period_length;
-
-  $end_date      = $tomorrow if $end_date > $tomorrow;
-
-  my @dates;
-
-  while (1) {
-    last if $cur_date >= $end_date;
-
-    push @dates, $cur_date->clone if $cur_date > $start_date;
-
-    $cur_date->add(months => $period_len);
-  }
-
-  return @dates;
+  my ($config) = @_;
+  return $config->calculate_invoice_dates(end_date => DateTime->today_local);
 }
 
 sub _send_email {
@@ -202,7 +283,7 @@ sub _send_email {
   return unless $template;
 
   my $email_template = $config{periodic_invoices}->{email_template};
-  my $filename       = $email_template || ( ($user->get_config_value('templates') || "templates/webpages") . "/periodic_invoices_email.txt" );
+  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 );
 
@@ -232,7 +313,16 @@ sub _print_invoice {
   $form->{formname}     = $form->{type};
   $form->{format}       = 'pdf';
   $form->{media}        = 'printer';
-  $form->{OUT}          = "| " . $config->printer->printer_command;
+  $form->{OUT}          = $config->printer->printer_command;
+  $form->{OUT_MODE}     = '|-';
+
+  $form->{TEMPLATE_DRIVER_OPTIONS} = {
+    variable_content_types => {
+      longdescription => 'html',
+      partnotes       => 'html',
+      notes           => 'html',
+    },
+  };
 
   $form->prepare_for_printing;
 
@@ -240,7 +330,7 @@ sub _print_invoice {
     eval {
       $form->parse_template(\%::myconfig);
       1;
-    } || die $EVAL_ERROR->{error};
+    } || die $EVAL_ERROR->getMessage;
   });
 }