RT#34078: Payment History Report / Statement [Fixes for acl and text preview]
[freeside.git] / httemplate / misc / email-customer-statement.html
1
2  <% include('email-customers.html',
3       'form_action'       => 'email-customer-statement.html',
4       'title'             => 'Send statement to customer',
5       'no_search_fields'  => [ 'start_date', 'end_date' ],
6       'alternate_form'    => $alternate_form,
7       'post_search_hook'  => $post_search_hook,
8       'acl'               => $acl,
9       'process_url'       => 'process/email-customer-statement.html',
10     )
11  %>
12
13 <%init>
14
15 my $acl = 'Resend invoices';
16
17 die "access denied"
18   unless $FS::CurrentUser::CurrentUser->access_right($acl);
19
20 my $alternate_form = sub {
21   # this could maaaybe be a separate element, for cleanliness
22   # but it's really only for use by this page, and it's not overly complicated
23   my $noinit = 0;
24   return join("\n",
25     '<TABLE BORDER="0">',
26     (
27       map {
28         my $label = ucfirst($_);
29         $label =~ s/_/ /;
30         include('/elements/tr-input-date-field.html',{
31           'name' => $_,
32           'value' => $cgi->param($_) || '',
33           'label' => $label,
34           'noinit' => $noinit++
35         });
36       }
37       qw( start_date end_date )
38     ),
39     '</TABLE>',
40     '<INPUT TYPE="hidden" NAME="action" VALUE="preview">',
41     '<INPUT TYPE="submit" VALUE="Preview notice">',
42   );
43 };
44
45 my $post_search_hook = sub {
46   my %opt = @_;
47   return unless $cgi->param('action') eq 'preview';
48   my $cust_main = qsearchs('cust_main',$opt{'search'})
49     or die "Could not find customer";
50
51   # so that the statement indicates the latest date
52   my $date_format = $opt{'conf'}->config('date_format') || '%m/%d/%Y';
53   $cgi->param('end_date', time2str($date_format, time))
54     unless $cgi->param('end_date');
55
56   # set from/subject/html_body based on date range
57
58   $cgi->param('from',
59     $opt{'conf'}->config('invoice_from')
60   );
61
62   # shortcut for common text
63   my $summary_text  = $cust_main->name_short .
64     ($cgi->param('start_date') ? ' from ' : '') .
65     $cgi->param('start_date') .
66     ($cgi->param('end_date') ? ' through ' : '') .
67     $cgi->param('end_date');
68
69   $cgi->param('subject',
70     $opt{'conf'}->config('company_name') . 
71     ' statement for ' .
72     $summary_text
73   );
74
75   $cgi->param('html_body',
76     '<P>' .
77     $opt{'conf'}->config('company_name') . 
78     ' statement of charges and payments for ' .
79     $summary_text . 
80     "</P>" .
81     include('/elements/customer-statement.html',
82       'history' => [ 
83         $cust_main->payment_history(
84           map {
85             $_ => parse_datetime($cgi->param($_))
86           }
87           qw( start_date end_date ),
88         ),
89       ],
90     )
91   );
92 };
93
94 </%init>
95