Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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'
79        && ( $part_event->templatename || $part_event->option('notice_name') )
80      )
81   {
82     my $link = 'invnum='. $cust_event->tablenum;
83     $link .= ';template='. uri_escape($part_event->templatename)
84       if $part_event->templatename;
85     $link .= ';notice_name='. uri_escape($part_event->option('notice_name'))
86       if $part_event->option('notice_name');
87
88     my $conf = new FS::Conf;
89     my $cust_bill = $cust_event->cust_X;
90
91     $status .= qq{
92           ( <A HREF="${p}view/cust_bill.cgi?$link">view</A>
93           | <A HREF="${p}view/cust_bill-pdf.cgi?$link">view&nbsp;typeset</A>
94           | <A HREF="${p}misc/send-invoice.cgi?method=print;$link">re-print</A>
95     };
96
97     if ( grep { $_ ne 'POST' } $cust_bill->cust_main->invoicing_list ) { 
98       $status .= qq{
99             | <A HREF="${p}misc/send-invoice.cgi?method=email;$link">re-email</A>
100       };
101     } 
102    
103     if ( $conf->exists('hylafax') && length($cust_bill->cust_main->fax) ) { 
104       $status .= qq{
105             | <A HREF="${p}misc/send-invoice.cgi?method=fax;$link">re-fax</A>
106       }
107     } 
108
109     $status .= ' ) ';
110
111   }
112
113   $status;
114 };
115
116 my $trigger_sub = sub {
117   my $cust_event = shift;
118   my $eventtable = $cust_event->eventtable;
119   my $label = FS::part_event->eventtable_labels->{$eventtable};
120   #if ( $eventtable eq 'cust_pkg' || $eventtable eq 'cust_bill' ) {
121     "$label #". $cust_event->tablenum;
122   #} else {
123   #  $label;
124   #}
125 };
126
127 my $trigger_link = sub {
128   my $cust_event = shift;
129   my $eventtable = $cust_event->eventtable;
130   if ( $eventtable eq 'cust_pkg' ) {
131     my $custnum = $cust_event->cust_main_custnum;
132     my $show = $FS::CurrentUser::CurrentUser->default_customer_view =~ /^(jumbo|packages)$/
133                  ? ''
134                  : ';show=packages';
135     my $pkgnum = $cust_event->tablenum;
136     my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
137     [ "${p}view/cust_main.cgi?custnum=$custnum$show;fragment=$frag#cust_pkg", 'tablenum' ];
138   } else {
139     [ "${p}view/$eventtable.cgi?", 'tablenum' ];
140   }
141 };
142
143 </%once>
144 <%init>
145
146 my $curuser = $FS::CurrentUser::CurrentUser;
147
148 die "access denied"
149   unless $curuser->access_right('Billing event reports')
150       or $curuser->access_right('View customer billing events')
151          && (    $cgi->param('custnum') =~ /^(\d+)$/
152               || $cgi->param('invnum')  =~ /^(\d+)$/
153               || $cgi->param('pkgnum')  =~ /^(\d+)$/
154             );
155
156 my $title = $cgi->param('failed') ? 'Failed billing events' : 'Billing events';
157
158 my %search = ();
159
160 my @scalars = qw( agentnum status custnum invnum pkgnum failed );
161 for my $param (@scalars) {
162   $search{$param} = scalar( $cgi->param($param) )
163     if $cgi->param($param);
164 }
165
166 #lists
167 my @lists = qw( payby eventpart );
168 foreach my $param (@lists) {
169   $search{$param} = [ $cgi->param($param) ];
170 }
171
172 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
173 $search{'beginning'} = $beginning;
174 $search{'ending'}    = $ending;
175
176 my $where = ' WHERE '. FS::cust_event->search_sql_where( \%search );
177
178 my $join = FS::cust_event->join_sql() .
179   'LEFT JOIN cust_location bill_location '.
180   'ON (cust_main.bill_locationnum = bill_location.locationnum) '.
181   'LEFT JOIN cust_location ship_location '.
182   'ON (cust_main.ship_locationnum = ship_location.locationnum)';
183   # warning: does not show the true service address for package events.
184   # the query to do that would be painfully slow.
185
186 my $sql_query = {
187   'table'     => 'cust_event',
188   'select'    => join(', ',
189                     'cust_event.*',
190                     'part_event.*',
191                     #'cust_bill.custnum',
192                     #'cust_bill._date AS cust_bill_date',
193                     'cust_main.custnum AS cust_main_custnum',
194                     FS::UI::Web::cust_sql_fields(),
195                   ),
196   'hashref'   => {}, 
197   'extra_sql' => $where,
198   'order_by'  => 'ORDER BY _date ASC',
199   'addl_from' => $join,
200   'debug' => 2,
201 };
202
203 my $count_sql = "SELECT COUNT(*) FROM cust_event $join $where";
204
205 my $conf = new FS::Conf;
206
207 my @params = ( @scalars, qw( beginning ending ) );
208
209 my $html_init = join("\n", map {
210   ( my $action = $_ ) =~ s/_$//;
211   include('/elements/progress-init.html',
212             $_.'form',
213             [ 'action', @params ],
214             "../misc/${_}events.cgi",
215             { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
216             $_, #key
217          ),
218   qq!<FORM NAME="${_}form">!,
219   qq!<INPUT TYPE="hidden" NAME="action" VALUE="$_">!, #not used though
220   ( map { my $value = encode_entities( $search{$_} );
221           qq(<INPUT TYPE="hidden" NAME="$_" VALUE="$value">);
222         }
223         @params #keys %search
224   ),
225   ( map { my $value = encode_entities( join(',', @{ $search{$_} } ) );
226           qq(<INPUT TYPE="hidden" NAME="$_" VALUE="$value">);
227         }
228         @lists
229   ),
230   qq!</FORM>!
231 } qw( print_ email_ fax_ ) ).
232
233 '<SCRIPT TYPE="text/javascript">
234
235 function confirm_print_process() {
236   if ( ! confirm("Are you sure you want to reprint these invoices?") ) {
237     return;
238   }
239   print_process();
240 }
241 function confirm_email_process() {
242   if ( ! confirm("Are you sure you want to re-email these invoices?") ) {
243     return;
244   }
245   email_process();
246 }
247 function confirm_fax_process() {
248   if ( ! confirm("Are you sure you want to re-fax these invoices?") ) {
249     return;
250   }
251   fax_process();
252 }
253
254 </SCRIPT>';
255
256 my $menubar = [];
257
258 if ( $curuser->access_right('Resend invoices') ) {
259
260   push @$menubar, 'Re-print these events' =>
261                     "javascript:confirm_print_process()",
262                   'Re-email these events' =>
263                     "javascript:confirm_email_process()",
264                 ;
265
266   push @$menubar, 'Re-fax these events' =>
267                     "javascript:confirm_fax_process()"
268     if $conf->exists('hylafax');
269
270 }
271
272 my $link_cust = sub {
273   my $cust_event = shift;
274   $cust_event->cust_main_custnum
275     ? [ "${p}view/cust_main.cgi?", 'cust_main_custnum' ]
276     : '';
277 };
278
279 </%init>