0eefc101aac9c536af4b24b617e7a2f23e98d050
[freeside.git] / httemplate / search / elements / cust_pay_or_refund.html
1 <%doc>
2
3 Examples:
4
5   include( 'elements/cust_pay_or_refund.html',
6                'thing'          => 'pay',
7                'amount_field'   => 'paid',
8                'name_singular'  => 'payment',
9                'name_verb'      => 'paid',
10          )
11
12   include( 'elements/cust_pay_or_refund.html',
13                'thing'          => 'refund',
14                'amount_field'   => 'refund',
15                'name_singular'  => 'refund',
16                'name_verb'      => 'refunded',
17          )
18
19   include( 'elements/cust_pay_or_refund.html',
20                'thing'          => 'pay_pending',
21                'amount_field'   => 'paid',
22                'name_singular'  => 'pending payment',
23                'name_verb'      => 'pending',
24                'disable_link'   => 1,
25                'disable_by'     => 1,
26                'html_init'      => '',
27                'addl_header'    => [],
28                'addl_fields'    => [],
29                'addl_sort_fields' => [],
30                'redirect_empty' => $redirect_empty,
31           )
32
33   include( 'elements/cust_pay_or_refund.html',
34                'table'          => 'h_cust_pay',
35                'amount_field'   => 'paid',
36                'name_singular'  => 'payment',
37                'name_verb'      => 'paid',
38                'pre_header'     => [ 'Transaction',    'By' ],
39                'pre_fields'     => [ 'history_action', 'history_user' ],
40          )
41
42 </%doc>
43 <& grouped-search.html,
44                 'title'          => $title, # XXX: translate
45                 'name_singular'  => $name_singular,
46                 'query'          => $sql_query,
47                 'count_query'    => $count_query,
48                 'count_addl'     => \@count_addl,
49                 'redirect_empty' => $opt{'redirect_empty'},
50                 'header'         => \@header,
51                 'fields'         => \@fields,
52                 'sort_fields'    => \@sort_fields,
53                 'align'          => $align,
54                 'links'          => \@links,
55                 'link_onclicks'  => \@link_onclicks,
56                 'color'          => \@color,
57                 'style'          => \@style,
58
59                 'group_column'   => 'payby',
60                 'group_label'    => 'payby_name',
61                 'subtotal'       => { $opt{amount_field} => "sum($amount_field)" },
62                 'subtotal_row'   => [ 'Subtotal',
63                                       sub { sprintf($money, $_[0]->$amount_field) },
64                                     ],
65                 'total_row'      => [ '<B>Total</B>',
66                                       sub { sprintf("<B>$money</B>", $_[0]->$amount_field) },
67                                     ],
68                 'show_combined'  => 1,
69 &>
70 <%init>
71
72 my %opt = @_;
73
74 my $curuser = $FS::CurrentUser::CurrentUser;
75
76 my $conf = FS::Conf->new;
77 my $money = ($conf->config('money_char') || '$') . '%.2f';
78
79 die "access denied"
80   unless $curuser->access_right('Financial reports');
81
82 my $table = $opt{'table'} || 'cust_'.$opt{'thing'};
83
84 my $has_reason = dbdef->table($table)->column('reasonnum') ? 1 : 0;
85
86 my $amount_field = $opt{'amount_field'};
87 my $name_singular = $opt{'name_singular'};
88
89 my $unapplied = $cgi->param('unapplied');
90 my $title = '';
91 $title = 'Unapplied ' if $unapplied;
92 $title .= "\u$name_singular Search Results";
93
94 ###NOT USED???
95 #my $link = '';
96 #if (    ( $curuser->access_right('View invoices') #remove in 2.5 (2.7?)
97 #          || ($curuser->access_right('View payments') && $table =~ /^cust_pay/)
98 #          || ($curuser->access_right('View refunds') && $table eq 'cust_refund')
99 #        )
100 #     && ! $opt{'disable_link'}
101 #   )
102 #{
103 #
104 #  my $key;
105 #  my $q = '';
106 #  if ( $table eq 'cust_pay_void' ) {
107 #    $key = 'paynum';
108 #    $q .= 'void=1;';
109 #  } elsif ( $table eq /^cust_(\w+)$/ ) {
110 #    $key = $1.'num';
111 #  }
112 #  
113 #  if ( $key ) {
114 #    $q .= "$key=";
115 #    $link = [ "${p}view/$table.html?$q", $key ]
116 #  }
117 #}
118
119 my $cust_link = sub {
120   my $cust_thing = shift;
121   $cust_thing->cust_main_custnum
122     ? [ "${p}view/cust_main.cgi?", 'custnum' ] 
123     : '';
124 };
125
126 # only valid for $table == 'cust_pay' atm
127 my  $tax_names = '';
128 if ( $cgi->param('tax_names') ) {
129   if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
130
131     $tax_names = "
132       array_to_string(
133         array(
134           SELECT itemdesc
135             FROM cust_bill_pay
136             LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
137             LEFT JOIN cust_bill_pkg USING ( billpkgnum )
138               WHERE cust_bill_pkg.pkgnum = 0
139                 AND cust_bill_pay.paynum = cust_pay.paynum
140         ), '|'
141       ) AS tax_names"
142     ;
143
144   } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
145
146     $tax_names = "GROUP_CONCAT(itemdesc SEPARATOR '|') AS tax_names";
147
148   } else {
149
150     warn "warning: unknown database type ". dbh->{Driver}->{Name}.
151          "omitting tax name information from report.";
152
153   }
154 }
155
156 my @header;
157 my @fields;
158 my @sort_fields;
159 my $align = '';
160 my @links;
161 my @link_onclicks;
162 if ( $opt{'pre_header'} ) {
163   push @header, @{ $opt{'pre_header'} };
164   $align .= 'c' x scalar(@{ $opt{'pre_header'} });
165   push @links, map '', @{ $opt{'pre_header'} };
166   push @fields, @{ $opt{'pre_fields'} };
167   push @sort_fields, @{ $opt{'pre_fields'} };
168 }
169
170 my $sub_receipt = $opt{'disable_link'} ? '' : sub {
171   my $obj = shift;
172   my $objnum = $obj->primary_key . '=' . $obj->get($obj->primary_key);
173   my $table = $obj->table;
174   my $void = '';
175   if ($table eq 'cust_pay_void') {
176     $table = 'cust_pay';
177     $void = ';void=1';
178   }
179
180   include('/elements/popup_link_onclick.html',
181     'action'  => $p.'view/'.$table.'.html?link=popup;'.$objnum.$void,
182     'actionlabel' => emt('Payment Receipt'),
183   );
184 };
185
186 push @header, "\u$name_singular",
187               'Amount',
188 ;
189 $align .= 'rr';
190 push @links, '', '';
191 push @fields, 'payby_payinfo_pretty',
192               sub { sprintf($money, shift->$amount_field() ) },
193 ;
194 push @link_onclicks, $sub_receipt, '';
195 push @sort_fields, 'paysort', $amount_field;
196
197 if ($opt{'show_card_type'}) {
198   push @header, emt('Card Type');
199   $align .= 'r';
200   push @links, '';
201   push @fields, sub { 
202     (($_[0]->payby eq 'CARD') && ($_[0]->paymask !~ /N\/A/)) ? cardtype($_[0]->paymask) : ''
203   };
204   push @sort_fields, '';
205 }
206
207 if ( $unapplied ) {
208   push @header, emt('Unapplied');
209   $align .= 'r';
210   push @links, '';
211   push @fields, sub { sprintf($money, shift->unapplied_amount) };
212   push @sort_fields, '';
213 }
214
215 push @header, emt('Date');
216 $align .= 'r';
217 push @links, '';
218 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
219 push @sort_fields, '_date';
220
221 if ($cgi->param('show_order_number')) {
222   push @header, emt('Order Number');
223   $align .= 'r';
224   push @links, '';
225   push @fields, 'order_number';
226   push @sort_fields, 'order_number';
227 }
228
229 unless ( $opt{'disable_by'} ) {
230   push @header, emt('By');
231   $align .= 'c';
232   push @links, '';
233   push @fields, sub { my $o = shift->otaker;
234                       $o = 'auto billing'          if $o eq 'fs_daily';
235                       $o = 'customer self-service' if $o eq 'fs_selfservice';
236                       $o;
237                     };
238   push @sort_fields, '';
239 }
240
241 if ( $tax_names ) {
242   push @header, (emt('Tax names'), emt('Tax province'));
243   $align .= 'cc';
244   push @links, ('','');
245   push @fields, sub { join (' + ', map { /^(.*?)(, \w\w)?$/; $1 }
246                                    split('\|', shift->tax_names)
247                            );
248                     };
249   push @fields, sub { join (' + ', map { if (/^(?:.*)(?:, )(\w\w)$/){ $1 }
250                                          else { () }
251                                        }
252                                    split('\|', shift->tax_names)
253                            );
254                     };
255   push @sort_fields, '', '';
256 }
257
258 push @header, FS::UI::Web::cust_header();
259 $align .=  FS::UI::Web::cust_aligns();
260 push @links, map { $_ ne 'Cust. Status' ? $cust_link : '' }
261                  FS::UI::Web::cust_header();
262 my @color = ( ( map '', @fields ), FS::UI::Web::cust_colors() );
263 my @style = ( ( map '', @fields ), FS::UI::Web::cust_styles() );
264 push @fields, \&FS::UI::Web::cust_fields;
265 push @sort_fields, FS::UI::Web::cust_sort_fields;
266
267 push @header, @{ $opt{'addl_header'} }
268   if $opt{'addl_header'};
269 push @fields, @{ $opt{'addl_fields'} }
270   if $opt{'addl_fields'};
271 push @sort_fields, @{ $opt{'addl_sort_fields'} }
272   if $opt{'addl_sort_fields'};
273
274 my( $count_query, $sql_query, @count_addl );
275 if ( $cgi->param('magic') ) {
276
277   my @search = ();
278   my @select = (
279     "$table.*",
280     "( $table.payby || ' ' || coalesce($table.paymask, $table.payinfo) ) AS paysort",
281     FS::UI::Web::cust_sql_fields(),
282     'cust_main.custnum AS cust_main_custnum',
283   );
284   push @select, $tax_names if $tax_names;
285
286   my $orderby;
287   if ( $cgi->param('magic') eq '_date' ) {
288
289     if ( $cgi->param('agentnum') && $cgi->param('agentnum') =~ /^(\d+)$/ ) {
290       push @search, "cust_main.agentnum = $1"; # $search{'agentnum'} = $1;
291       my $agent = qsearchs('agent', { 'agentnum' => $1 } );
292       die "unknown agentnum $1" unless $agent;
293       $title = $agent->agent. " $title";
294     }
295
296     if ( $cgi->param('refnum') && $cgi->param('refnum') =~ /^(\d+)$/ ) {
297       push @search, "cust_main.refnum = $1";
298       my $part_referral = qsearchs('part_referral', { 'refnum' => $1 } );
299       die "unknown refnum $1" unless $part_referral;
300       $title = $part_referral->referral. " $title";
301     }
302
303     # cust_classnum - standard matching
304     push @search, $m->comp('match-classnum',
305         param => 'cust_classnum', field => 'cust_main.classnum'
306       );
307
308     if ( $cgi->param('custnum') =~ /^(\d+)$/ ) {
309       push @search, "$table.custnum = $1";
310     }
311
312     if ( $cgi->param('payby') ) {
313
314       my @all_payby_search = ();
315       foreach my $payby ( $cgi->param('payby') ) {
316
317         $payby =~
318           /^(CARD|CHEK|BILL|CASH|PPAL|APPL|ANRD|PREP|WIRE|WEST|IDTP|EDI|MCRD|MCHK)(-(VisaMC|Amex|Discover|Maestro|Tokenized))?$/
319             or die "illegal payby $payby";
320
321         my $payby_search = "$table.payby = '$1'";
322
323         if ( $3 ) {
324
325           my $cardtype = $3;
326
327           my $similar_to = dbh->{Driver}->{Name} =~ /^mysql/i
328                              ? 'REGEXP' #doesn't behave exactly the same, but
329                                         #should work for our patterns
330                              : 'SIMILAR TO';
331
332           my $search;
333           if ( $cardtype eq 'VisaMC' ) {
334
335             #avoid posix regexes for portability
336             $search =
337               # Visa
338               " ( (     substring($table.payinfo from 1 for 1) = '4'     ".
339               #   is not Switch
340               "     AND substring($table.payinfo from 1 for 4) != '4936' ".
341               "     AND substring($table.payinfo from 1 for 6)           ".
342               "         NOT $similar_to '49030[2-9]'                        ".
343               "     AND substring($table.payinfo from 1 for 6)           ".
344               "         NOT $similar_to '49033[5-9]'                        ".
345               "     AND substring($table.payinfo from 1 for 6)           ".
346               "         NOT $similar_to '49110[1-2]'                        ".
347               "     AND substring($table.payinfo from 1 for 6)           ".
348               "         NOT $similar_to '49117[4-9]'                        ".
349               "     AND substring($table.payinfo from 1 for 6)           ".
350               "         NOT $similar_to '49118[1-2]'                        ".
351               "   )".
352               # MasterCard
353               "   OR substring($table.payinfo from 1 for 2) = '51' ".
354               "   OR substring($table.payinfo from 1 for 2) = '52' ".
355               "   OR substring($table.payinfo from 1 for 2) = '53' ".
356               "   OR substring($table.payinfo from 1 for 2) = '54' ".
357               "   OR substring($table.payinfo from 1 for 2) = '54' ".
358               "   OR substring($table.payinfo from 1 for 2) = '55' ".
359               "   OR substring($table.payinfo from 1 for 4) $similar_to '222[1-9]' ".
360               "   OR substring($table.payinfo from 1 for 3) $similar_to '22[3-9]' ".
361               "   OR substring($table.payinfo from 1 for 2) $similar_to '2[3-6]' ".
362               "   OR substring($table.payinfo from 1 for 3) $similar_to '27[0-1]' ".
363               "   OR substring($table.payinfo from 1 for 4) = '2720' ".
364               "   OR substring($table.payinfo from 1 for 3) = '2[2-7]x' ".
365               " ) ";
366
367           } elsif ( $cardtype eq 'Amex' ) {
368
369             $search =
370               " (    substring($table.payinfo from 1 for 2 ) = '34' ".
371               "   OR substring($table.payinfo from 1 for 2 ) = '37' ".
372               " ) ";
373
374           } elsif ( $cardtype eq 'Discover' ) {
375
376             my $country = $conf->config('countrydefault') || 'US';
377
378             $search =
379               " (    substring($table.payinfo from 1 for 4 ) = '6011'  ".
380               "   OR substring($table.payinfo from 1 for 3 ) = '60x'   ".
381               "   OR substring($table.payinfo from 1 for 2 ) = '65'    ".
382
383               # diner's 300-305 / 3095
384               "   OR substring($table.payinfo from 1 for 3 ) = '300'   ".
385               "   OR substring($table.payinfo from 1 for 3 ) = '301'   ".
386               "   OR substring($table.payinfo from 1 for 3 ) = '302'   ".
387               "   OR substring($table.payinfo from 1 for 3 ) = '303'   ".
388               "   OR substring($table.payinfo from 1 for 3 ) = '304'   ".
389               "   OR substring($table.payinfo from 1 for 3 ) = '305'   ".
390               "   OR substring($table.payinfo from 1 for 4 ) = '3095'  ".
391               "   OR substring($table.payinfo from 1 for 3 ) = '30x'   ".
392
393               # diner's 36, 38, 39
394               "   OR substring($table.payinfo from 1 for 2 ) = '36'    ".
395               "   OR substring($table.payinfo from 1 for 2 ) = '38'    ".
396               "   OR substring($table.payinfo from 1 for 2 ) = '39'    ".
397
398               "   OR substring($table.payinfo from 1 for 3 ) = '644'   ".
399               "   OR substring($table.payinfo from 1 for 3 ) = '645'   ".
400               "   OR substring($table.payinfo from 1 for 3 ) = '646'   ".
401               "   OR substring($table.payinfo from 1 for 3 ) = '647'   ".
402               "   OR substring($table.payinfo from 1 for 3 ) = '648'   ".
403               "   OR substring($table.payinfo from 1 for 3 ) = '649'   ".
404               "   OR substring($table.payinfo from 1 for 3 ) = '64x'   ".
405
406               # JCB cards in the 3528-3589 range identified as Discover inside US & territories (NOT Canada)
407               ( $country =~ /^(US|PR|VI|MP|PW|GU)$/
408                ?" OR substring($table.payinfo from 1 for 4 ) = '3528'  ".
409                 " OR substring($table.payinfo from 1 for 4 ) = '3529'  ".
410                 " OR substring($table.payinfo from 1 for 3 ) = '353'   ".
411                 " OR substring($table.payinfo from 1 for 3 ) = '354'   ".
412                 " OR substring($table.payinfo from 1 for 3 ) = '355'   ".
413                 " OR substring($table.payinfo from 1 for 3 ) = '356'   ".
414                 " OR substring($table.payinfo from 1 for 3 ) = '357'   ".
415                 " OR substring($table.payinfo from 1 for 3 ) = '358'   ".
416                 " OR substring($table.payinfo from 1 for 3 ) = '35x'   "
417                :""
418               ).
419
420               #China Union Pay processed as Discover in US, Mexico and Caribbean
421               ( $country =~ /^(US|MX|AI|AG|AW|BS|BB|BM|BQ|VG|KY|CW|DM|DO|GD|GP|JM|MQ|MS|BL|KN|LC|VC|MF|SX|TT|TC)$/
422                ?" OR substring($table.payinfo from 1 for 3 ) $similar_to '62[24-68x]'   "
423                :""
424               ).
425
426               " ) ";
427
428           } elsif ( $cardtype eq 'Maestro' ) {
429
430             $search =
431               " (    substring($table.payinfo from 1 for 2 ) = '63'     ".
432               "   OR substring($table.payinfo from 1 for 2 ) = '67'     ".
433               "   OR substring($table.payinfo from 1 for 6 ) = '564182' ".
434               "   OR substring($table.payinfo from 1 for 4 ) = '4936'   ".
435               "   OR substring($table.payinfo from 1 for 6 )            ".
436               "      $similar_to '49030[2-9]'                             ".
437               "   OR substring($table.payinfo from 1 for 6 )            ".
438               "      $similar_to '49033[5-9]'                             ".
439               "   OR substring($table.payinfo from 1 for 6 )            ".
440               "      $similar_to '49110[1-2]'                             ".
441               "   OR substring($table.payinfo from 1 for 6 )            ".
442               "      $similar_to '49117[4-9]'                             ".
443               "   OR substring($table.payinfo from 1 for 6 )            ".
444               "      $similar_to '49118[1-2]'                             ".
445               " ) ";
446
447           } elsif ( $cardtype eq 'Tokenized' ) {
448
449             $search = " substring($table.payinfo from 1 for 2 ) = '99' ";
450
451           } else {
452             die "unknown card type $cardtype";
453           }
454
455           my $masksearch = $search;
456           $masksearch =~ s/$table\.payinfo/$table.paymask/gi;
457
458           $payby_search = "( $payby_search AND ( $search OR ( $table.paymask IS NOT NULL AND $masksearch ) ) )";
459
460         }
461
462         push @all_payby_search, $payby_search;
463
464       }
465
466       push @search, ' ( '. join(' OR ', @all_payby_search). ' ) ' if @all_payby_search;
467
468     }
469
470     if ( $cgi->param('paymask') ) {
471       $cgi->param('paymask') =~ /^\s*(\d+)\s*$/
472         or die "illegal paymask ". $cgi->param('paymask');
473       my $regexp = regexp_sql();
474       push @search, "$table.paymask $regexp '$1\$'";
475     } 
476
477     if ( $cgi->param('payinfo') ) {
478       $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/
479         or die "illegal payinfo ". $cgi->param('payinfo');
480       my $regexp = regexp_sql();
481       push @search, "$table.payinfo $regexp '^0*$1\$'";
482     }
483
484     if ( $cgi->param('ccpay') =~ /^([\w-:]+)$/ ) {
485       # I think that's all the characters we need to allow.
486       # To avoid confusion, this parameter searches both auth and order_number.
487       push @search, "($table.auth LIKE '$1%') OR ($table.order_number LIKE '$1%')";
488       push @fields, 'auth', 'order_number';
489       push @header, 'Auth #', 'Transaction #';
490       push @sort_fields, '', '';
491       $align .= 'rr';
492
493     }
494
495     if ( $cgi->param('usernum') =~ /^(\d+)$/ ) {
496       push @search, "$table.usernum = $1";
497     }
498
499     #for cust_pay_pending...  statusNOT=done
500     if ( $cgi->param('statusNOT') =~ /^(\w+)$/ ) {
501       push @search, "$table.status != '$1'";
502     }
503
504     my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
505
506     push @search, "$table._date >= $beginning ",
507                   "$table._date <= $ending";
508
509     if ( $table eq 'cust_pay_void' ) {
510       my($v_beginning, $v_ending) =
511         FS::UI::Web::parse_beginning_ending($cgi, 'void');
512       push @search, "$table.void_date >= $v_beginning ",
513                     "$table.void_date <= $v_ending";
514     }
515
516     push @search, FS::UI::Web::parse_lt_gt($cgi, $amount_field, $table);
517
518     $orderby = '_date';
519
520   } elsif ( $cgi->param('magic') eq 'paybatch' ) {
521
522     $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/
523       or die "illegal paybatch: ". $cgi->param('paybatch');
524
525     $orderby = "LOWER(company || ' ' || last || ' ' || first )";
526
527   } elsif ( $cgi->param('magic') eq 'batchnum' ) {
528
529     $cgi->param('batchnum') =~ /^(\d+)$/
530       or die "illegal batchnum: ".$cgi->param('batchnum');
531
532     push @search, "batchnum = $1";
533
534     $orderby = "LOWER(company || ' ' || last || ' ' || first )";
535
536   } else {
537     die "unknown search magic: ". $cgi->param('magic');
538   }
539
540   if ( $cgi->param('paybatch') =~ /^([\w\/\:\-\.]+)$/ ) {
541     push @search, "paybatch = '$1'";
542   }
543
544   #unapplied payment/refund
545   if ( $unapplied ) {
546     push @select, '(' . "FS::$table"->unapplied_sql . ') AS unapplied_amount';
547     push @search, "FS::$table"->unapplied_sql . ' > 0';
548
549   }
550
551   #for the history search
552   if ( $cgi->param('history_action') =~ /^([\w,]+)$/ ) {
553     my @history_action = split(/,/, $1);
554     push @search, 'history_action IN ('.
555                     join(',', map "'$_'", @history_action ). ')';
556   }
557
558   if (    $cgi->param('history_date_beginning')
559        || $cgi->param('history_date_ending')    ) {
560       my($h_beginning, $h_ending) =
561         FS::UI::Web::parse_beginning_ending($cgi, 'history_date');
562       push @search, "history_date >= $h_beginning ",
563                     "history_date <= $h_ending";
564   }
565
566   #here is the agent virtualization
567   push @search, $curuser->agentnums_sql;
568
569   my $addl_from = FS::UI::Web::join_cust_main($table);
570   my $group_by = '';
571
572   # reasons, for refunds and voided payments
573   if ( $has_reason ) {
574     push @select, "reason.reason";
575     $addl_from .= " LEFT JOIN reason USING (reasonnum)\n";
576     push @fields, 'reason';
577     push @sort_fields, 'reason.reason';
578     push @header, emt('Reason');
579     if ( $cgi->param('reasonnum') =~ /^(\d+)$/ ) {
580       push @search, "COALESCE(reasonnum, 0) = $1";
581     }
582   }
583
584   #check for customer tags
585   my @tags = $cgi->param('tagnum');
586   if (@tags) {
587    my $tags = join(',',@tags);
588    $addl_from .= " LEFT JOIN cust_tag ON (cust_tag.custnum = cust_pay.custnum)";
589    push @search, "cust_tag.tagnum in ($tags)"; 
590   }
591
592   if ( $cgi->param('tax_names') ) {
593     if ( dbh->{Driver}->{Name} =~ /^Pg/i ) {
594
595       0;#twiddle thumbs
596
597     } elsif ( dbh->{Driver}->{Name} =~ /^mysql/i ) {
598
599       $addl_from .= "LEFT JOIN cust_bill_pay USING ( paynum )
600                      LEFT JOIN cust_bill_pay_pkg USING ( billpaynum )
601                      LEFT JOIN cust_bill_pkg USING ( billpkgnum ) AS tax_names";
602       $group_by  .= "GROUP BY $table.*,cust_main_custnum,".
603                     FS::UI::Web::cust_sql_fields();
604       push @search,
605        "( cust_bill_pkg.pkgnum = 0 OR cust_bill_pkg.pkgnum is NULL )";
606
607     } else {
608
609       warn "warning: unknown database type ". dbh->{Driver}->{Name}.
610            "omitting tax name information from report.";
611
612     }
613   }
614
615   my $search = ' WHERE '. join(' AND ', @search);
616
617   $count_query = "SELECT COUNT(*), SUM($table.$amount_field) ";
618   $count_query .= ', SUM(' . "FS::$table"->unapplied_sql . ') ' 
619     if $unapplied;
620   $count_query .= "FROM $table $addl_from".
621                   "$search $group_by";
622
623   @count_addl = ( '$%.2f total '.$opt{name_verb} );
624   push @count_addl, '$%.2f unapplied' if $unapplied;
625
626   $sql_query = {
627     'table'     => $table,
628     'select'    => join(', ', @select),
629     'hashref'   => {},
630     'extra_sql' => "$search $group_by",
631     'order_by'  => "ORDER BY $orderby",
632     'addl_from' => $addl_from,
633   };
634
635 } else {
636
637   #hmm... is this still used?
638   warn "undefined search magic";
639
640   $cgi->param('payinfo') =~ /^\s*(\d+)\s*$/ or die "illegal payinfo";
641   my $payinfo = $1;
642
643   $cgi->param('payby') =~ /^(\w+)$/ or die "illegal payby";
644   my $payby = $1;
645
646   $count_query = "SELECT COUNT(*), SUM($table.$amount_field) FROM $table".
647                  "  WHERE payinfo = '$payinfo' AND payby = '$payby'".
648                  "  AND ". $curuser->agentnums_sql;
649   @count_addl = ( '$%.2f total '.$opt{name_verb} );
650
651   $sql_query = {
652     'table'     => $table,
653     'hashref'   => { 'payinfo' => $payinfo,
654                      'payby'   => $payby    },
655     'extra_sql' => $curuser->agentnums_sql.
656                    " ORDER BY _date",
657   };
658
659 }
660
661 # for consistency
662 $title = join('',map {ucfirst} split(/\b/,$title));
663
664 </%init>