d82a83368ec17d0cb194102bba15a33c87f13fe7
[freeside.git] / httemplate / search / cust_bill_event.cgi
1 <%
2
3 my $title = $cgi->param('failed') ? 'Failed invoice events' : 'Invoice events';
4
5 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
6
7 ##tie my %hash, 'Tie::DxHash', 
8 #my %hash = (
9 #      _date => { op=> '>=', value=>$beginning },
10 ## i wish...
11 ##      _date => { op=> '<=', value=>$ending },
12 #);
13 #$hash{'statustext'} = { op=> '!=', value=>'' }
14 #  if $cgi->param('failed');
15
16 my $where = " WHERE cust_bill_event._date >= $beginning".
17             "   AND cust_bill_event._date <= $ending";
18
19 if ( $cgi->param('failed') ) {
20   $where .= " AND statustext != '' ".
21             " AND statustext IS NOT NULL ".
22             " AND statustext != 'N/A' "
23 }
24
25 if ( $cgi->param('part_bill_event.payby') =~ /^(\w+)$/ ) {
26   $where .= " AND part_bill_event.payby = '$1' ";
27 }
28
29 my $sql_query = {
30   'table'     => 'cust_bill_event',
31   #'hashref'   => \%hash,
32   'hashref'   => {}, 
33   'select'    => join(', ',
34                    'cust_bill_event.*',
35                    'part_bill_event.event',
36                    'cust_bill.custnum',
37                    'cust_bill._date AS cust_bill_date',
38                    'cust_main.custnum AS cust_main_custnum',
39                    FS::UI::Web::cust_sql_fields(),
40                  ),
41   'extra_sql' => "$where ORDER BY _date ASC",
42   'addl_from' => 'LEFT JOIN part_bill_event USING ( eventpart ) '.
43                  'LEFT JOIN cust_bill       USING ( invnum    ) '.
44                  'LEFT JOIN cust_main       USING ( custnum   ) ',
45 };
46
47 my $count_sql = "SELECT COUNT(*) FROM cust_bill_event ".
48                 "LEFT JOIN part_bill_event USING ( eventpart ) ".
49                 $where;
50
51 my $conf = new FS::Conf;
52
53 my $failed = $cgi->param('failed');
54
55 my $html_init = join("\n", map {
56   ( my $action = $_ ) =~ s/_$//;
57   include('/elements/progress-init.html',
58             $_.'form',
59             [ 'action', 'beginning', 'ending', 'failed' ],
60             "../misc/${_}invoice_events.cgi",
61             { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
62             $_, #key
63          ),
64   qq!<FORM NAME="${_}form">!,
65   qq!<INPUT TYPE="hidden" NAME="action" VALUE="$_">!, #not used though
66   qq!<INPUT TYPE="hidden" NAME="beginning" VALUE="$beginning">!,
67   qq!<INPUT TYPE="hidden" NAME="ending"    VALUE="$ending">!,
68   qq!<INPUT TYPE="hidden" NAME="failed"    VALUE="$failed">!,
69   qq!</FORM>!
70 } qw( print_ email_ fax_ ) );
71
72 my $menubar =  [
73                  'Main menu' => $p,
74                  'Re-print these events' =>
75                    "javascript:print_process()",
76                  'Re-email these events' =>
77                    "javascript:email_process()",
78                ];
79
80 push @$menubar, 'Re-fax these events' =>
81                   "javascript:fax_process()"
82   if $conf->exists('hylafax');
83
84 my $link_cust = sub {
85   my $cust_bill_event = shift;
86   $cust_bill_event->cust_main_custnum
87     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
88     : '';
89 };
90
91 %><%= include( 'elements/search.html',
92                  'title'       => $title,
93                  'html_init'   => $html_init,
94                  'menubar'     => $menubar,
95                  'name'        => 'billing events',
96                  'query'       => $sql_query,
97                  'count_query' => $count_sql,
98                  'header'      => [ 'Event',
99                                     'Date',
100                                     'Status',
101                                     #'Inv #', 'Inv Date', 'Cust #',
102                                     'Invoice',
103                                     FS::UI::Web::cust_header(),
104                                   ],
105                  'fields' => [
106                                'event',
107                                sub { time2str("%b %d %Y %T", $_[0]->_date) },
108                                sub { 
109                                      #my $cust_bill_event = shift;
110                                      my $status = $_[0]->status;
111                                      $status .= ': '.$_[0]->statustext
112                                        if $_[0]->statustext;
113                                      $status;
114                                    },
115                                sub {
116                                      #my $cust_bill_event = shift;
117                                      'Invoice #'. $_[0]->invnum.
118                                      ' ('.
119                                        time2str("%D", $_[0]->cust_bill_date).
120                                      ')';
121                                    },
122                                \&FS::UI::Web::cust_fields,
123                              ],
124                  'links' => [
125                               '',
126                               '',
127                               '',
128                               sub {
129                                 my $part_bill_event = shift;
130                                 my $template = $part_bill_event->templatename;
131                                 $template .= '-' if $template;
132                                 [ "${p}view/cust_bill.cgi?$template", 'invnum'];
133                               },
134                               ( map { $link_cust } FS::UI::Web::cust_header() ),
135                             ],
136              )
137 %>