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