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