29 |
29 |
my $self = shift;
|
30 |
30 |
$self->{db_obj} = shift;
|
31 |
31 |
|
32 |
|
$self->{job_errors} = [];
|
|
32 |
$self->{$_} = [] for qw(job_errors posted_invoices printed_invoices printed_failed emailed_invoices emailed_failed disabled_orders);
|
|
33 |
|
33 |
34 |
if (!$self->{db_obj}->db->with_transaction(sub {
|
34 |
35 |
1; # make Emacs happy
|
35 |
36 |
|
... | ... | |
40 |
41 |
_log_msg("Periodic invoice configuration ID " . $config->id . " extended through " . $new_end_date->strftime('%d.%m.%Y') . "\n") if $new_end_date;
|
41 |
42 |
}
|
42 |
43 |
|
43 |
|
my (@new_invoices, @invoices_to_print, @invoices_to_email, @disabled_orders);
|
|
44 |
my (@invoices_to_print, @invoices_to_email);
|
44 |
45 |
|
45 |
46 |
_log_msg("Number of configs: " . scalar(@{ $configs}));
|
46 |
47 |
|
... | ... | |
61 |
62 |
|
62 |
63 |
_log_msg("Invoice " . $data->{invoice}->invnumber . " posted for config ID " . $config->id . ", period start date " . $::locale->format_date(\%::myconfig, $date) . "\n");
|
63 |
64 |
|
64 |
|
push @new_invoices, $data;
|
|
65 |
push @{ $self->{posted_invoices} }, $data->{invoice};
|
65 |
66 |
push @invoices_to_print, $data if $config->print;
|
66 |
67 |
push @invoices_to_email, $data if $config->send_email;
|
67 |
68 |
|
... | ... | |
69 |
70 |
if ($inactive_ordnumber) {
|
70 |
71 |
# disable one time configs and skip eventual invoices
|
71 |
72 |
_log_msg("Order " . $inactive_ordnumber . " deavtivated \n");
|
72 |
|
push @disabled_orders, $inactive_ordnumber;
|
|
73 |
push @{ $self->{disabled_orders} }, $inactive_ordnumber;
|
73 |
74 |
last;
|
74 |
75 |
}
|
75 |
76 |
}
|
... | ... | |
78 |
79 |
foreach my $inv ( @invoices_to_print ) { $self->_print_invoice($inv); }
|
79 |
80 |
foreach my $inv ( @invoices_to_email ) { $self->_email_invoice($inv); }
|
80 |
81 |
|
81 |
|
$self->_send_summary_email(
|
82 |
|
[ map { $_->{invoice} } @new_invoices ],
|
83 |
|
[ map { $_->{invoice} } @invoices_to_print ],
|
84 |
|
[ map { $_->{invoice} } @invoices_to_email ],
|
85 |
|
\@disabled_orders ,
|
86 |
|
);
|
|
82 |
$self->_send_summary_email;
|
87 |
83 |
|
88 |
84 |
1;
|
89 |
85 |
})) {
|
... | ... | |
293 |
289 |
}
|
294 |
290 |
|
295 |
291 |
sub _send_summary_email {
|
296 |
|
my ($self, $posted_invoices, $printed_invoices, $emailed_invoices,
|
297 |
|
$disabled_orders) = @_;
|
|
292 |
my ($self) = @_;
|
298 |
293 |
my %config = %::lx_office_conf;
|
299 |
294 |
|
300 |
|
return if !$config{periodic_invoices} || !$config{periodic_invoices}->{send_email_to} || !scalar @{ $posted_invoices };
|
|
295 |
return if !$config{periodic_invoices} || !$config{periodic_invoices}->{send_email_to} || !scalar @{ $self->{posted_invoices} };
|
301 |
296 |
|
302 |
|
my $user = SL::DB::Manager::AuthUser->find_by(login => $config{periodic_invoices}->{send_email_to});
|
303 |
|
my $email = $user ? $user->get_config_value('email') : undef;
|
|
297 |
return if $config{periodic_invoices}->{send_for_errors_only} && !@{ $self->{printed_failed} } && !@{ $self->{emailed_failed} };
|
|
298 |
|
|
299 |
my $email = $config{periodic_invoices}->{send_email_to};
|
|
300 |
if ($email !~ m{\@}) {
|
|
301 |
my $user = SL::DB::Manager::AuthUser->find_by(login => $email);
|
|
302 |
$email = $user ? $user->get_config_value('email') : undef;
|
|
303 |
}
|
|
304 |
|
|
305 |
_log_msg("_send_summary_email: about to send to '" . ($email || '') . "'");
|
304 |
306 |
|
305 |
307 |
return unless $email;
|
306 |
308 |
|
... | ... | |
314 |
316 |
|
315 |
317 |
my $email_template = $config{periodic_invoices}->{email_template};
|
316 |
318 |
my $filename = $email_template || ( (SL::DB::Default->get->templates || "templates/webpages") . "/oe/periodic_invoices_email.txt" );
|
317 |
|
my %params = ( POSTED_INVOICES => $posted_invoices,
|
318 |
|
PRINTED_INVOICES => $printed_invoices,
|
319 |
|
EMAILED_INVOICES => $emailed_invoices,
|
320 |
|
DISABLED_ORDERS => $disabled_orders );
|
|
319 |
my %params = map { (uc($_) => $self->{$_}) } qw(posted_invoices printed_invoices printed_failed emailed_invoices emailed_failed disabled_orders);
|
321 |
320 |
|
322 |
321 |
my $output;
|
323 |
|
$template->process($filename, \%params, \$output);
|
|
322 |
$template->process($filename, \%params, \$output) || die $template->error;
|
324 |
323 |
|
325 |
324 |
my $mail = Mailer->new;
|
326 |
325 |
$mail->{from} = $config{periodic_invoices}->{email_from};
|
... | ... | |
379 |
378 |
$form->throw_on_error(sub {
|
380 |
379 |
eval {
|
381 |
380 |
$form->parse_template(\%::myconfig);
|
|
381 |
push @{ $self->{printed_invoices} }, $invoice;
|
382 |
382 |
1;
|
383 |
383 |
} or do {
|
384 |
384 |
push @{ $self->{job_errors} }, $EVAL_ERROR->error;
|
|
385 |
push @{ $self->{printed_failed} }, [ $invoice, $EVAL_ERROR->error ];
|
385 |
386 |
};
|
386 |
387 |
});
|
387 |
388 |
}
|
... | ... | |
440 |
441 |
}
|
441 |
442 |
|
442 |
443 |
my $global_bcc = SL::DB::Default->get->global_bcc;
|
|
444 |
my $overall_error;
|
443 |
445 |
|
444 |
446 |
for my $recipient (@recipients) {
|
445 |
447 |
my $mail = Mailer->new;
|
... | ... | |
457 |
459 |
|
458 |
460 |
my $error = $mail->send;
|
459 |
461 |
|
460 |
|
push @{ $self->{job_errors} }, $error if $error;
|
|
462 |
if ($error) {
|
|
463 |
push @{ $self->{job_errors} }, $error;
|
|
464 |
push @{ $self->{emailed_failed} }, [ $data->{invoice}, $error ];
|
|
465 |
$overall_error = 1;
|
|
466 |
}
|
461 |
467 |
}
|
462 |
468 |
|
|
469 |
push @{ $self->{emailed_invoices} }, $data->{invoice} unless $overall_error;
|
|
470 |
|
463 |
471 |
1;
|
464 |
472 |
|
465 |
473 |
} or do {
|
466 |
474 |
push @{ $self->{job_errors} }, $EVAL_ERROR;
|
|
475 |
push @{ $self->{emailed_failed} }, [ $data->{invoice}, $EVAL_ERROR ];
|
467 |
476 |
};
|
468 |
477 |
|
469 |
478 |
unlink $pdf_file_name if $pdf_file_name;
|
Wiederkehrende Rechnungen: Druck-/E-Mail-Fehler in Zusammenfassungs-Mail