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