finish up initial work on customer view tabs (ensure links back to customer view...
[freeside.git] / httemplate / search / cust_event.html
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                                     'Trigger',
12                                     #'Inv #', 'Inv Date', 'Cust #',
13                                     #'Invoice',
14                                     
15                                     FS::UI::Web::cust_header(), #'cust_main_custnum',
16                                   ],
17                  'fields' => [
18                                'event',
19                                sub { time2str("%b %d %Y %T", $_[0]->_date) },
20                                $status_sub,
21                                $trigger_sub,
22                                #sub {
23                                #      #my $cust_event = shift;
24                                #      'Invoice #'. $_[0]->invnum.
25                                #      ' ('.
26                                #        time2str("%D", $_[0]->cust_bill_date).
27                                #      ')';
28                                #    },
29                                \&FS::UI::Web::cust_fields,
30                              ],
31                 'align' => 'lrll'.FS::UI::Web::cust_aligns(),
32                 'links' => [
33                               '',
34                               '',
35                               '',
36                               $trigger_link,
37                               #sub {
38                               #  my $part_event = shift;
39                               #  #XXX
40                               #  my $template = $part_event->templatename;
41                               #  $template .= '-' if $template;
42                               #  [ "${p}view/cust_bill.cgi?$template", 'invnum'];
43                               #},
44
45                               ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
46                                     FS::UI::Web::cust_header()
47                               ),
48                             ],
49                  'color' => [ 
50                               '',
51                               '',
52                               '',
53                               '',
54                               #'',
55                               FS::UI::Web::cust_colors(),
56                             ],
57                  'style' => [ 
58                               '',
59                               '',
60                               '',
61                               '',
62                               #'',
63                               FS::UI::Web::cust_styles(),
64                             ],
65              )
66 %>
67 <%once>
68
69 my $status_sub = sub { 
70   my $cust_event = shift;
71
72   my $status = $cust_event->status;
73   $status .= ': '.$cust_event->statustext
74     if $cust_event->statustext;
75
76   my $part_event = $cust_event->part_event;
77
78   if ( $part_event->eventtable eq 'cust_bill' && $part_event->templatename ) {
79     my $alt_templatename = $part_event->templatename;
80     my $alt_link = "$alt_templatename-". $cust_event->tablenum;
81
82     my $conf = new FS::Conf;
83     my $cust_bill = $cust_event->cust_X;
84
85     $status .= qq{
86           ( <A HREF="${p}view/cust_bill.cgi?$alt_link">view</A>
87           | <A HREF="${p}view/cust_bill-pdf.cgi?$alt_link.pdf">view
88               typeset</A>
89           | <A HREF="${p}misc/print-invoice.cgi?$alt_link">re-print</A>
90     };
91
92     if ( grep { $_ ne 'POST' } $cust_bill->cust_main->invoicing_list ) { 
93       $status .= qq{
94             | <A HREF="${p}misc/email-invoice.cgi?$alt_link">re-email</A>
95       };
96     } 
97    
98     if ( $conf->exists('hylafax') && length($cust_bill->cust_main->fax) ) { 
99       $status .= qq{
100             | <A HREF="${p}misc/fax-invoice.cgi?$alt_link">re-fax</A>
101       }
102     } 
103
104     $status .= ' ) ';
105
106   }
107
108   $status;
109 };
110
111 my $trigger_sub = sub {
112   my $cust_event = shift;
113   my $eventtable = $cust_event->eventtable;
114   my $label = FS::part_event->eventtable_labels->{$eventtable};
115   #if ( $eventtable eq 'cust_pkg' || $eventtable eq 'cust_bill' ) {
116     "$label #". $cust_event->tablenum;
117   #} else {
118   #  $label;
119   #}
120 };
121
122 my $trigger_link = sub {
123   my $cust_event = shift;
124   my $eventtable = $cust_event->eventtable;
125   if ( $eventtable eq 'cust_pkg' ) {
126     my $custnum = $cust_event->cust_main_custnum;
127     my $show = $FS::CurrentUser::CurrentUser->default_customer_view =~ /^(jumbo|packages)$/
128                  ? ''
129                  : ';show=packages';
130     my $pkgnum = $cust_event->tablenum;
131     my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
132     [ "${p}view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#cust_pkg", 'tablenum' ];
133   } else {
134     [ "${p}view/$eventtable.cgi?", 'tablenum' ];
135   }
136 };
137
138 </%once>
139 <%init>
140
141 my $curuser = $FS::CurrentUser::CurrentUser;
142
143 die "access denied"
144   unless $curuser->access_right('Billing event reports')
145       or $curuser->access_right('View customer billing events')
146          && (    $cgi->param('custnum') =~ /^(\d+)$/
147               || $cgi->param('invnum')  =~ /^(\d+)$/
148               || $cgi->param('pkgnum')  =~ /^(\d+)$/
149             );
150          
151
152 my $title = $cgi->param('failed')
153               ? 'Failed billing events'
154               : 'Billing events';
155
156 my @search = ();
157
158 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
159   push @search, "cust_main.agentnum = $1";
160   #my $agent = qsearchs('agent', { 'agentnum' => $1 } );
161   #die "unknown agentnum $1" unless $agent;
162 }
163
164 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
165 push @search, "cust_event._date >= $beginning",
166               "cust_event._date <= $ending";
167
168 if ( $cgi->param('failed') ) {
169   push @search, "statustext != ''",
170                 "statustext IS NOT NULL",
171                 "statustext != 'N/A'";
172 }
173
174 #if ( $cgi->param('part_event.payby') =~ /^(\w+)$/ ) {
175 #  push @search, "part_event.payby = '$1'";
176 #}
177
178 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
179   push @search, "cust_main.custnum = '$1'";
180 }
181 if ( $cgi->param('invnum') =~ /^(\d+)$/ ) {
182   push @search, "part_event.eventtable = 'cust_bill'",
183                 "tablenum = '$1'";
184 }
185 if ( $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
186   push @search, "part_event.eventtable = 'cust_pkg'",
187                 "tablenum = '$1'";
188 }
189
190 #here is the agent virtualization
191 push @search, $curuser->agentnums_sql( 'table' => 'cust_main' );
192
193 my $where = 'WHERE '. join(' AND ', @search );
194
195 my $join = "
196        JOIN part_event USING ( eventpart )
197   LEFT JOIN cust_bill ON ( eventtable = 'cust_bill' AND tablenum = invnum  )
198   LEFT JOIN cust_pkg  ON ( eventtable = 'cust_pkg'  AND tablenum = pkgnum  )
199   LEFT JOIN cust_main ON (    ( eventtable = 'cust_main' AND tablenum = cust_main.custnum )
200                            OR ( eventtable = 'cust_bill' AND cust_bill.custnum = cust_main.custnum )
201                            OR ( eventtable = 'cust_pkg'  AND cust_pkg.custnum  = cust_main.custnum )
202                          )
203 ";
204            #'LEFT JOIN cust_main  USING ( custnum   ) ';
205
206 my $sql_query = {
207   'table'     => 'cust_event',
208   'select'    => join(', ',
209                     'cust_event.*',
210                     'part_event.*',
211                     #'cust_bill.custnum',
212                     #'cust_bill._date AS cust_bill_date',
213                     'cust_main.custnum AS cust_main_custnum',
214                     FS::UI::Web::cust_sql_fields(),
215                   ),
216   'hashref'   => {}, 
217   'extra_sql' => "$where ORDER BY _date ASC",
218   'addl_from' => $join,
219 };
220
221 my $count_sql = "SELECT COUNT(*) FROM cust_event $join $where";
222
223 my $conf = new FS::Conf;
224
225 my $failed = $cgi->param('failed');
226
227 my $html_init = join("\n", map {
228   ( my $action = $_ ) =~ s/_$//;
229   include('/elements/progress-init.html',
230             $_.'form',
231             [ 'action', 'beginning', 'ending', 'failed' ],
232             "../misc/${_}events.cgi",
233             { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
234             $_, #key
235          ),
236   qq!<FORM NAME="${_}form">!,
237   qq!<INPUT TYPE="hidden" NAME="action" VALUE="$_">!, #not used though
238   qq!<INPUT TYPE="hidden" NAME="beginning" VALUE="$beginning">!,
239   qq!<INPUT TYPE="hidden" NAME="ending"    VALUE="$ending">!,
240   qq!<INPUT TYPE="hidden" NAME="failed"    VALUE="$failed">!,
241   qq!</FORM>!
242 } qw( print_ email_ fax_ ) ).
243
244 '<SCRIPT TYPE="text/javascript">
245
246 function confirm_print_process() {
247   if ( ! confirm("Are you sure you want to reprint these invoices?") ) {
248     return;
249   }
250   print_process();
251 }
252 function confirm_email_process() {
253   if ( ! confirm("Are you sure you want to re-email these invoices?") ) {
254     return;
255   }
256   email_process();
257 }
258 function confirm_fax_process() {
259   if ( ! confirm("Are you sure you want to re-fax these invoices?") ) {
260     return;
261   }
262   fax_process();
263 }
264
265 </SCRIPT>';
266
267 my $menubar = [];
268
269 if ( $curuser->access_right('Resend invoices') ) {
270
271   push @$menubar, 'Re-print these events' =>
272                     "javascript:confirm_print_process()",
273                   'Re-email these events' =>
274                     "javascript:confirm_email_process()",
275                 ;
276
277   push @$menubar, 'Re-fax these events' =>
278                     "javascript:confirm_fax_process()"
279     if $conf->exists('hylafax');
280
281 }
282
283 my $link_cust = sub {
284   my $cust_event = shift;
285   $cust_event->cust_main_custnum
286     ? [ "${p}view/cust_main.cgi?", 'cust_main_custnum' ]
287     : '';
288 };
289
290 </%init>