fix ambiguous agentnum errors in cust_event.html & report_tax.cgi (provide a table...
[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     [ "${p}view/cust_main.cgi?$custnum#cust_pkg", 'tablenum' ];
128   } else {
129     [ "${p}view/$eventtable.cgi?", 'tablenum' ];
130   }
131 };
132
133 </%once>
134 <%init>
135
136 my $curuser = $FS::CurrentUser::CurrentUser;
137
138 die "access denied"
139   unless $curuser->access_right('Billing event reports')
140       or $curuser->access_right('View customer billing events')
141          && (    $cgi->param('custnum') =~ /^(\d+)$/
142               || $cgi->param('invnum')  =~ /^(\d+)$/
143               || $cgi->param('pkgnum')  =~ /^(\d+)$/
144             );
145          
146
147 my $title = $cgi->param('failed')
148               ? 'Failed billing events'
149               : 'Billing events';
150
151 my @search = ();
152
153 if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
154   push @search, "cust_main.agentnum = $1";
155   #my $agent = qsearchs('agent', { 'agentnum' => $1 } );
156   #die "unknown agentnum $1" unless $agent;
157 }
158
159 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
160 push @search, "cust_event._date >= $beginning",
161               "cust_event._date <= $ending";
162
163 if ( $cgi->param('failed') ) {
164   push @search, "statustext != ''",
165                 "statustext IS NOT NULL",
166                 "statustext != 'N/A'";
167 }
168
169 #if ( $cgi->param('part_event.payby') =~ /^(\w+)$/ ) {
170 #  push @search, "part_event.payby = '$1'";
171 #}
172
173 if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
174   push @search, "cust_main.custnum = '$1'";
175 }
176 if ( $cgi->param('invnum') =~ /^(\d+)$/ ) {
177   push @search, "part_event.eventtable = 'cust_bill'",
178                 "tablenum = '$1'";
179 }
180 if ( $cgi->param('pkgnum') =~ /^(\d+)$/ ) {
181   push @search, "part_event.eventtable = 'cust_pkg'",
182                 "tablenum = '$1'";
183 }
184
185 #here is the agent virtualization
186 push @search, $curuser->agentnums_sql( 'table' => 'cust_main' );
187
188 my $where = 'WHERE '. join(' AND ', @search );
189
190 my $join = "
191        JOIN part_event USING ( eventpart )
192   LEFT JOIN cust_bill ON ( eventtable = 'cust_bill' AND tablenum = invnum  )
193   LEFT JOIN cust_pkg  ON ( eventtable = 'cust_pkg'  AND tablenum = pkgnum  )
194   LEFT JOIN cust_main ON (    ( eventtable = 'cust_main' AND tablenum = cust_main.custnum )
195                            OR ( eventtable = 'cust_bill' AND cust_bill.custnum = cust_main.custnum )
196                            OR ( eventtable = 'cust_pkg'  AND cust_pkg.custnum  = cust_main.custnum )
197                          )
198 ";
199            #'LEFT JOIN cust_main  USING ( custnum   ) ';
200
201 my $sql_query = {
202   'table'     => 'cust_event',
203   'select'    => join(', ',
204                     'cust_event.*',
205                     'part_event.*',
206                     #'cust_bill.custnum',
207                     #'cust_bill._date AS cust_bill_date',
208                     'cust_main.custnum AS cust_main_custnum',
209                     FS::UI::Web::cust_sql_fields(),
210                   ),
211   'hashref'   => {}, 
212   'extra_sql' => "$where ORDER BY _date ASC",
213   'addl_from' => $join,
214 };
215
216 my $count_sql = "SELECT COUNT(*) FROM cust_event $join $where";
217
218 my $conf = new FS::Conf;
219
220 my $failed = $cgi->param('failed');
221
222 my $html_init = join("\n", map {
223   ( my $action = $_ ) =~ s/_$//;
224   include('/elements/progress-init.html',
225             $_.'form',
226             [ 'action', 'beginning', 'ending', 'failed' ],
227             "../misc/${_}events.cgi",
228             { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
229             $_, #key
230          ),
231   qq!<FORM NAME="${_}form">!,
232   qq!<INPUT TYPE="hidden" NAME="action" VALUE="$_">!, #not used though
233   qq!<INPUT TYPE="hidden" NAME="beginning" VALUE="$beginning">!,
234   qq!<INPUT TYPE="hidden" NAME="ending"    VALUE="$ending">!,
235   qq!<INPUT TYPE="hidden" NAME="failed"    VALUE="$failed">!,
236   qq!</FORM>!
237 } qw( print_ email_ fax_ ) ).
238
239 '<SCRIPT TYPE="text/javascript">
240
241 function confirm_print_process() {
242   if ( ! confirm("Are you sure you want to reprint these invoices?") ) {
243     return;
244   }
245   print_process();
246 }
247 function confirm_email_process() {
248   if ( ! confirm("Are you sure you want to re-email these invoices?") ) {
249     return;
250   }
251   email_process();
252 }
253 function confirm_fax_process() {
254   if ( ! confirm("Are you sure you want to re-fax these invoices?") ) {
255     return;
256   }
257   fax_process();
258 }
259
260 </SCRIPT>';
261
262 my $menubar =  [
263                  'Re-print these events' =>
264                    "javascript:confirm_print_process()",
265                  'Re-email these events' =>
266                    "javascript:confirm_email_process()",
267                ];
268
269 push @$menubar, 'Re-fax these events' =>
270                   "javascript:confirm_fax_process()"
271   if $conf->exists('hylafax');
272
273 my $link_cust = sub {
274   my $cust_event = shift;
275   $cust_event->cust_main_custnum
276     ? [ "${p}view/cust_main.cgi?", 'cust_main_custnum' ]
277     : '';
278 };
279
280 </%init>