RT# 78547 Future autobill report - report runs in job queue
[freeside.git] / FS / FS / Report / Queued / FutureAutobill.pm
1 package FS::Report::Queued::FutureAutobill;
2 use strict;
3 use warnings;
4 use vars qw( $job );
5
6 use FS::Conf;
7 use FS::cust_main;
8 use FS::cust_main::Location;
9 use FS::cust_payby;
10 use FS::CurrentUser;
11 use FS::Log;
12 use FS::Mason qw(mason_interps);
13 use FS::Record qw( qsearch );
14 use FS::UI::Web;
15 use FS::UID qw( dbh );
16
17 use DateTime;
18 use File::Temp;
19 use Data::Dumper;
20 use HTML::Entities qw( encode_entities );
21
22 =head1 NAME
23
24 FS::Report::Queued::FutureAutobill - Future Auto-Bill Transactions Report
25
26 =head1 DESCRIPTION
27
28 Future Autobill report generated within the job queue.
29
30 Report results are saved to temp storage as a Mason fragment
31 that is rendered by the queued report viewer.
32
33 For every customer with a valid auto-bill payment method,
34 report runs bill_and_collect() for each day, from today through
35 the report target date.  After recording the results, all
36 operations are rolled back.
37
38 This report relies on the ability to safely run bill_and_collect(),
39 with all exports and messaging disabled, and then to roll back the
40 results.
41
42 =head1 PARAMETERS
43
44 C<agentnum>, C<target_date>
45
46 =cut
47
48 sub make_report {
49   $job = shift;
50   my $param = shift;
51   my $outbuf;
52   my $DEBUG = 0;
53
54   my $time_begin = time();
55
56   my $report_fh = File::Temp->new(
57     TEMPLATE => 'report.future_autobill.XXXXXXXX',
58     DIR      => sprintf( '%s/cache.%s', $FS::Conf::base_dir, $FS::UID::datasrc ),
59     UNLINK   => 0
60   ) or die "Cannot create report file: $!";
61
62   if ( $DEBUG ) {
63     warn Dumper( $job );
64     warn Dumper( $param );
65     warn $report_fh;
66     warn $report_fh->filename;
67   }
68
69   my $curuser = FS::CurrentUser->load_user( $param->{CurrentUser} )
70     or die 'Unable to set report user';
71
72   my ( $fs_interp ) = FS::Mason::mason_interps(
73     'standalone',
74     outbuf => \$outbuf,
75   );
76   $fs_interp->error_mode('fatal');
77   $fs_interp->error_format('text');
78
79   $FS::Mason::Request::QUERY_STRING = sprintf(
80     'target_date=%s&agentnum=%s',
81     encode_entities( $param->{target_date} ),
82     encode_entities( $param->{agentnum} || '' ),
83   );
84   $FS::Mason::Request::FSURL = $param->{RootURL};
85
86   my $mason_request = $fs_interp->make_request(
87     comp => '/search/future_autobill.html'
88   );
89
90   {
91     local $@;
92     eval{ $mason_request->exec() };
93     if ( $@ ) {
94       my $error = ref $@ eq 'HTML::Mason::Exception' ? $@->error : $@;
95
96       my $log = FS::Log->new('FS::Report::Queued::FutureAutobill');
97       $log->error(
98         "Error generating report: $FS::Mason::Request::QUERY_STRING $error"
99       );
100       die $error;
101     }
102   }
103
104   my $report_fn;
105   if ( $report_fh->filename =~ /report\.(future_autobill.+)$/ ) {
106       $report_fn = $1
107   } else {
108     die 'Error parsing report filename '.$report_fh->filename;
109   }
110
111   my $report_title = FS::cust_payby->future_autobill_report_title();
112   my $time_rendered = time() - $time_begin;
113
114   if ( $DEBUG ) {
115     warn "Generated content:\n";
116     warn $outbuf;
117     warn $report_fn;
118     warn $report_title;
119   }
120
121   print $report_fh qq{<% include("/elements/header.html", '$report_title') %>\n};
122   print $report_fh $outbuf;
123   print $report_fh qq{<!-- Time to render report $time_rendered seconds -->};
124   print $report_fh qq{<% include("/elements/footer.html") %>\n};
125
126   die sprintf
127     "<a href=%s/misc/queued_report.html?report=%s>view</a>\n",
128     $param->{RootURL},
129     $report_fn;
130 }
131
132 1;