X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FBackgroundJob%2FCreatePeriodicInvoices.pm;h=ce8957770e95cdc5b7e0c35a3d0c54d3fd1b3b3d;hb=a99ac2d9b3621c82b7aeddf85e9640442c42433c;hp=fa5d649357965257c3f9b95fe06b158370146866;hpb=efb9a24f2252104ab4af5c25334119d7c5c70a8c;p=kivitendo-erp.git diff --git a/SL/BackgroundJob/CreatePeriodicInvoices.pm b/SL/BackgroundJob/CreatePeriodicInvoices.pm index fa5d64935..ce8957770 100644 --- a/SL/BackgroundJob/CreatePeriodicInvoices.pm +++ b/SL/BackgroundJob/CreatePeriodicInvoices.pm @@ -5,6 +5,7 @@ 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; @@ -57,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; @@ -79,36 +80,66 @@ sub _generate_time_period_variables { $::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' ? ('<%', '%>') : ('<%', '%>'); + + $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 _create_periodic_invoice { @@ -132,12 +163,13 @@ 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 => $_) 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; @@ -171,27 +203,8 @@ sub _create_periodic_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 { @@ -215,7 +228,7 @@ sub _send_email { return unless $template; my $email_template = $config{periodic_invoices}->{email_template}; - my $filename = $email_template || ( (SL::DB::Default->get->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 );