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