X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FBackgroundJob%2FCreatePeriodicInvoices.pm;h=7779c091c820780ae1dd0c36c67bab883f52d881;hb=3a3683515f26794fa3bff9f670c437bbdab6bb86;hp=b7b3e3305d87971c36386f2d353000542c435b5e;hpb=d63305ba3bea55eec4aac2df26149f439c086508;p=kivitendo-erp.git diff --git a/SL/BackgroundJob/CreatePeriodicInvoices.pm b/SL/BackgroundJob/CreatePeriodicInvoices.pm index b7b3e3305..7779c091c 100644 --- a/SL/BackgroundJob/CreatePeriodicInvoices.pm +++ b/SL/BackgroundJob/CreatePeriodicInvoices.pm @@ -9,6 +9,7 @@ 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; @@ -18,6 +19,7 @@ use SL::DB::PeriodicInvoicesConfig; 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 @@ -28,64 +30,72 @@ sub run { $self->{db_obj} = shift; $self->{job_errors} = []; + if (!$self->{db_obj}->db->with_transaction(sub { + 1; # make Emacs happy - my $configs = SL::DB::Manager::PeriodicInvoicesConfig->get_all(query => [ 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; - _log_msg("Periodic invoice configuration ID " . $config->id . " extended through " . $new_end_date->strftime('%d.%m.%Y') . "\n") if $new_end_date; - } + 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 (@new_invoices, @invoices_to_print, @invoices_to_email, @disabled_orders); + my (@new_invoices, @invoices_to_print, @invoices_to_email, @disabled_orders); - _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 @new_invoices, $data; + push @invoices_to_print, $data if $config->print; + push @invoices_to_email, $data if $config->send_email; - 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 @disabled_orders, $inactive_ordnumber; - 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 @disabled_orders, $inactive_ordnumber; + last; + } } } - } - 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( + [ 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; - } + 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; } @@ -321,6 +331,26 @@ 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 _print_invoice { my ($self, $data) = @_; @@ -350,7 +380,7 @@ sub _print_invoice { $form->parse_template(\%::myconfig); 1; } or do { - push @{ $self->{job_errors} }, $EVAL_ERROR->getMessage; + push @{ $self->{job_errors} }, $EVAL_ERROR->error; }; }); } @@ -368,12 +398,15 @@ 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', variable_content_types => { @@ -387,10 +420,13 @@ 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}); + for (qw(email_subject email_body)) { _replace_vars( object => $data->{config}, @@ -413,7 +449,7 @@ sub _email_invoice { $mail->{message} = $data->{config}->email_body; $mail->{attachments} = [{ path => $pdf_file_name, - name => sprintf('%s %s.pdf', $::locale->text('Invoice'), $data->{invoice}->invnumber), + name => sprintf('%s %s.pdf', $label, $data->{invoice}->invnumber), }]; my $error = $mail->send;