From: Mark Wells Date: Tue, 27 Nov 2012 21:03:36 +0000 (-0800) Subject: ICS invoice batch: send batch and summary report in the same message, #17620 X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=642bb1970300250739af3ce7a8706a9771781e05 ICS invoice batch: send batch and summary report in the same message, #17620 --- diff --git a/FS/FS/Cron/upload.pm b/FS/FS/Cron/upload.pm index 08819fce9..65773200e 100644 --- a/FS/FS/Cron/upload.pm +++ b/FS/FS/Cron/upload.pm @@ -326,14 +326,16 @@ sub spool_upload { } } - send_report('bridgestone-confirm_template', - { - agentnum=> $agentnum, - zipfile => $zipfile, - prefix => $prefix, - seq => $seq, - rows => $rows, - } + send_email( + prepare_report('bridgestone-confirm_template', + { + agentnum=> $agentnum, + zipfile => $zipfile, + prefix => $prefix, + seq => $seq, + rows => $rows, + } + ) ); $seq++; @@ -376,16 +378,26 @@ sub spool_upload { close $reg; close $big; + # zip up all three files for transport my $zipfile = "$basename" . '.zip'; my $command = "cd $dir; zip $zipfile $regfile $bigfile"; system($command) and die "'$command' failed\n"; - my $error = $upload_target->put("$dir/$zipfile"); + # upload them, unless we're using email, in which case + # the zip file will ride along with the report. yes, this + # kind of defeats the purpose of the upload_target interface, + # but at least we have a place to store the configuration. + my $error = ''; + if ( $upload_target->protocol ne 'email' ) { + $error = $upload_target->put("$dir/$zipfile"); + } + + # create the report for (values %sum) { $_ = sprintf('%.2f', $_); } - send_report('ics-confirm_template', + my %report = prepare_report('ics-confirm_template', { agentnum => $agentnum, count => \%count, @@ -393,8 +405,23 @@ sub spool_upload { error => $error, } ); + if ( $upload_target->protocol eq 'email' ) { + $report{'to'} = + join('@', $upload_target->username, $upload_target->hostname); + $report{'subject'} = $upload_target->subject; + $report{'mimeparts'} = [ + { Path => "$dir/$zipfile", + Type => 'application/zip', + Encoding => 'base64', + Filename => $zipfile, + Disposition => 'attachment', + } + ]; + } + $error = send_email(%report); if ( $error ) { + # put the original spool file back rename "$dir/$file-$date.csv", "$dir/$file.csv"; die $error; } @@ -421,7 +448,8 @@ sub spool_upload { =item send_report CONFIG PARAMS Retrieves the config value named CONFIG, parses it as a Text::Template, -extracts "to" and "subject" headers, and sends it by email. +extracts "to" and "subject" headers, and returns a hash that can be passed +to L. PARAMS is a hashref to be passed to C. It must contain 'agentnum' to look up the per-agent config. @@ -429,7 +457,8 @@ PARAMS is a hashref to be passed to C. It must contain =cut # we used it twice, so it's now a subroutine -sub send_report { + +sub prepare_report { my ($config, $params) = @_; my $agentnum = $params->{agentnum}; @@ -452,7 +481,7 @@ sub send_report { $head =~ /^to:\s*(.*)$/im; my $to = $1; - send_email( + ( to => $to, from => $conf->config('invoice_from', $agentnum), subject => $subject,