715d1ca9aebcc25fd3340fa6c9674af6b9d66a8a
[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 my $title = $cgi->param('failed') ? 'Failed billing events' : 'Billing events';
152
153 my %search = ();
154
155 my @scalars = qw ( agentnum custnum invnum pkgnum failed );
156 for my $param ( @scalars ) {
157   $search{$param} = scalar( $cgi->param($param) )
158     if $cgi->param($param);
159 }
160
161 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
162 $search{'beginning'} = $beginning;
163 $search{'ending'}    = $ending;
164
165 my $where = ' WHERE '. FS::cust_event->search_sql( \%search );
166
167 my $join = FS::cust_event->join_sql();
168
169 my $sql_query = {
170   'table'     => 'cust_event',
171   'select'    => join(', ',
172                     'cust_event.*',
173                     'part_event.*',
174                     #'cust_bill.custnum',
175                     #'cust_bill._date AS cust_bill_date',
176                     'cust_main.custnum AS cust_main_custnum',
177                     FS::UI::Web::cust_sql_fields(),
178                   ),
179   'hashref'   => {}, 
180   'extra_sql' => "$where ORDER BY _date ASC",
181   'addl_from' => $join,
182 };
183
184 my $count_sql = "SELECT COUNT(*) FROM cust_event $join $where";
185
186 my $conf = new FS::Conf;
187
188 my @params = ( @scalars, qw( beginning ending ) );
189
190 my $html_init = join("\n", map {
191   ( my $action = $_ ) =~ s/_$//;
192   include('/elements/progress-init.html',
193             $_.'form',
194             [ 'action', @params ],
195             "../misc/${_}events.cgi",
196             { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
197             $_, #key
198          ),
199   qq!<FORM NAME="${_}form">!,
200   qq!<INPUT TYPE="hidden" NAME="action" VALUE="$_">!, #not used though
201   ( map { my $value = encode_entities( $search{$_} );
202           qq(<INPUT TYPE="hidden" NAME="$_" VALUE="$value">);
203         }
204         @params #keys %search
205   ),
206   qq!</FORM>!
207 } qw( print_ email_ fax_ ) ).
208
209 '<SCRIPT TYPE="text/javascript">
210
211 function confirm_print_process() {
212   if ( ! confirm("Are you sure you want to reprint these invoices?") ) {
213     return;
214   }
215   print_process();
216 }
217 function confirm_email_process() {
218   if ( ! confirm("Are you sure you want to re-email these invoices?") ) {
219     return;
220   }
221   email_process();
222 }
223 function confirm_fax_process() {
224   if ( ! confirm("Are you sure you want to re-fax these invoices?") ) {
225     return;
226   }
227   fax_process();
228 }
229
230 </SCRIPT>';
231
232 my $menubar = [];
233
234 if ( $curuser->access_right('Resend invoices') ) {
235
236   push @$menubar, 'Re-print these events' =>
237                     "javascript:confirm_print_process()",
238                   'Re-email these events' =>
239                     "javascript:confirm_email_process()",
240                 ;
241
242   push @$menubar, 'Re-fax these events' =>
243                     "javascript:confirm_fax_process()"
244     if $conf->exists('hylafax');
245
246 }
247
248 my $link_cust = sub {
249   my $cust_event = shift;
250   $cust_event->cust_main_custnum
251     ? [ "${p}view/cust_main.cgi?", 'cust_main_custnum' ]
252     : '';
253 };
254
255 </%init>