include discounts in gross sales reports, #25943
[freeside.git] / httemplate / search / elements / search-html.html
1 %   if ( exists($opt{'redirect'}) && $opt{'redirect'}
2 %        && scalar(@$rows) == 1 && $total == 1
3 %        && $type ne 'html-print'
4 %      ) {
5 %     my $redirect = $opt{'redirect'};
6 %     $redirect = &{$redirect}($rows->[0], $cgi) if ref($redirect) eq 'CODE';
7 %     my( $url, $method ) = @$redirect;
8 %     redirect( $url. $rows->[0]->$method() );
9 %   } elsif ( exists($opt{'redirect_empty'}) && ! scalar(@$rows) && $total == 0
10 %             && $type ne 'html-print'
11 %             && $opt{'redirect_empty'}
12 %             && ( ref($opt{'redirect_empty'}) ne 'CODE'
13 %                  || &{$opt{'redirect_empty'}}($cgi)    )
14 %      ) {
15 %     my $redirect = $opt{'redirect_empty'};
16 %     $redirect = &{$redirect}($cgi) if ref($redirect) eq 'CODE';
17 %     redirect( $redirect );
18 %   } else {
19 %     if ( $opt{'name_singular'} ) {
20 %       $opt{'name'} = PL($opt{'name_singular'});
21 %     }
22 %     ( my $xlsname = $opt{'name'} ) =~ s/\W//g;
23 %     if ( $total == 1 ) {
24 %       if ( $opt{'name_singular'} ) {
25 %         $opt{'name'} = $opt{'name_singular'}
26 %       } else {
27 %         #$opt{'name'} =~ s/s$// if $total == 1;
28 %         $opt{'name'} =~ s/((s)e)?s$/$2/ if $total == 1;
29 %       }
30 %     }
31 %
32 %     unless ( $opt{nohtmlheader} ) {
33 %
34 %       if ( $type eq 'html-print' ) {
35           <& /elements/header-popup.html, $opt{'title'} &>
36 %       } else {
37 %         if ( $type eq 'select' ) {
38             <&/elements/header-popup.html, $opt{'title'} &>
39 %         } else {
40 %
41 %           my @menubar = ();
42 %           if ( $opt{'menubar'} ) {
43 %             @menubar = @{ $opt{'menubar'} };
44 %           #} else {
45 %           #  @menubar = ( 'Main menu' => $p );
46 %           }
47
48             <& /elements/header.html, $opt{'title'},
49                  include( '/elements/menubar.html', @menubar )
50             &>
51
52 %         }
53 %       }
54 %
55 %     }
56 %
57 %     unless ( $type eq 'html-print' ) {
58
59 %       if ( $opt{'add_link'} ) { #or after html_init?
60           <A HREF="<%$p%>edit/<% $opt{query}->{table} %>.html"><I>Add a <% $opt{'name_singular'} %></I></A><BR><BR>
61 %       }
62
63         <% defined($opt{'html_init'}) 
64               ? ( ref($opt{'html_init'})
65                     ? &{$opt{'html_init'}}()
66                     : $opt{'html_init'}
67                 )
68               : ''
69         %>
70
71 %     }
72
73 %     unless ( $total ) { 
74 %       unless ( $opt{'disable_nonefound'} ) { 
75           <BR><BR>No matching <% $opt{'name'} %> found.<BR>
76 %       } 
77 %     }
78 %
79 %     if ( $total || $opt{'disableable'} ) { #hmm... and there *are* ones to show??
80
81         <TABLE>
82           <TR>
83
84             <TD VALIGN="bottom">
85
86               <FORM>
87
88 %               if (! $opt{'disable_total'}) {
89                   <% $total %> total <% $opt{'name'} %>
90 %               }
91
92 %               if ( $confmax && $total > $confmax
93 %                    && ! $opt{'disable_maxselect'}
94 %                    && $type ne 'html-print' )
95 %               {
96 %                 $cgi->delete('maxrecords');
97 %                 $cgi->param('_dummy', 1);
98
99                   ( show <SELECT NAME="maxrecords" onChange="window.location = '<% "$self_url?". $cgi->query_string %>;maxrecords=' + this.options[this.selectedIndex].value;">
100
101 %                   foreach my $max ( map { $_ * $confmax } qw( 1 5 10 25 ) ) {
102                   <OPTION VALUE="<% $max %>" <% ( $maxrecords == $max ) ? 'SELECTED' : '' %>><% $max %></OPTION>
103 %                   }
104
105                   </SELECT> per page )
106
107 %                 $cgi->param('maxrecords', $maxrecords);
108 %               }
109
110 %               if ( defined($opt{'html_posttotal'}) && $type ne 'html-print' ) {
111                     <% ref($opt{'html_posttotal'})
112                          ? &{$opt{'html_posttotal'}}()
113                          : $opt{'html_posttotal'}
114                     %>
115 %               }
116                 <BR>
117
118 %               if ( $opt{'count_addl'} ) { 
119 %                 my $n=0;
120 %                 foreach my $count ( @{$opt{'count_addl'}} ) { 
121 %                   my $data = $count_arrayref->[++$n];
122 %                   if ( ref($count) ) {
123                       <% &{ $count }( $data ) %>
124 %                   } else {
125                       <% sprintf( $count, $data ) %><BR>
126 %                   }
127 %                 } 
128 %               } 
129               </FORM>
130
131             </TD>
132
133 %           if ( $curuser->access_right('Download report data')
134 %                 and !$opt{'disable_download'}
135 %                 and $type ne 'html-print' ) { 
136
137               <TD ALIGN="right" CLASS="noprint">
138
139                 <% $opt{'download_label'} || 'Download full results' %><BR>
140
141 %               $cgi->param('_type', "$xlsname.xls" ); 
142                 as <A HREF="<% "$self_url?". $cgi->query_string %>">Excel spreadsheet</A><BR>
143
144 %               $cgi->param('_type', 'csv'); 
145                 as <A HREF="<% "$self_url?". $cgi->query_string %>">CSV file</A><BR>
146
147 %             if ( defined($opt{xml_elements}) ) {
148 %               $cgi->param('_type', 'xml'); 
149                 as <A HREF="<% "$self_url?". $cgi->query_string %>">XML file</A><BR>
150 %             }
151
152 %               $cgi->param('_type', 'html-print'); 
153                 as <A HREF="<% "$self_url?". $cgi->query_string %>">printable copy</A>
154
155               </TD>
156 %             $cgi->param('_type', "html" ); 
157 %           } 
158
159           </TR>
160           <TR>
161             <TD COLSPAN=2>
162
163 %             my $pager = '';
164 %             unless ( $type eq 'html_print' ) {
165
166                 <% $pager = include( '/elements/pager.html',
167                                        'offset'     => $offset,
168                                        'num_rows'   => scalar(@$rows),
169                                        'total'      => $total,
170                                        'maxrecords' => $maxrecords,
171                                    )
172                 %>
173
174                 <% defined($opt{'html_form'}) 
175                      ? ( ref($opt{'html_form'})
176                            ? &{$opt{'html_form'}}()
177                            : $opt{'html_form'}
178                        )
179                      : ''
180                 %>
181
182 %             }
183
184               <& SELF:data_table,
185                   rows            => $rows,
186                   null_link       => $null_link,
187                   link_agentnums  => \@link_agentnums,
188                   self_url        => $self_url,
189                   %opt
190               &>
191
192               <% $pager %>
193   
194             </TD>
195           </TR>
196         </TABLE>
197 %     }
198
199 %     if ( $type eq 'html-print' ) {
200 %       unless ( $opt{nohtmlheader} ) {
201
202         </BODY></HTML>
203       
204 %       }
205 %     } else {
206
207         <% defined($opt{'html_foot'}) 
208               ? ( ref($opt{'html_foot'})
209                     ? &{$opt{'html_foot'}}()
210                     : $opt{'html_foot'}
211                 )
212               : ''
213         %>
214
215         <% $opt{nohtmlheader}
216              ? ''
217              : include( '/elements/footer.html' )
218         %>
219
220 %     }
221
222 %   } 
223 <%init>
224
225 my $curuser = $FS::CurrentUser::CurrentUser;
226
227 my %args = @_;
228 my $type           = $args{'type'};
229 my $header         = $args{'header'};
230 my $rows           = $args{'rows'};
231 my @link_agentnums = @{ $args{'link_agentnums'} };
232 my $null_link      = $args{'null_link'};
233 my $confmax        = $args{'confmax'};
234 my $maxrecords     = $args{'maxrecords'};
235 my $offset         = $args{'offset'};
236 my %opt            = %{ $args{'opt'} };
237
238 # must be an arrayref of the row count, followed by any other totals
239 my $count_arrayref = $args{'totals'};
240 my $total = $count_arrayref->[0];
241
242 # there used to be an option to override this, for highly dubious reasons
243 my $self_url = $cgi->url('-path_info' => 1, '-full' =>1);
244
245 </%init>
246 <%method data_table>
247 % my %opt = @_;
248 % my $rows = delete $opt{rows};
249 % my $self_url = delete $opt{self_url};
250 <& /elements/table-grid.html &>
251
252 <THEAD>
253 <& SELF:header_row,
254   'header'      => $opt{'header'},
255   'header2'     => $opt{'header2'},
256   'sort_fields' => ($opt{'sort_fields'} || $opt{'fields'}),
257 &>
258 </THEAD>
259
260 <TBODY>
261 <& SELF:data_rows, rows => $rows, opt => \%opt &>
262 </TBODY>
263
264 % if ( $opt{'footer'} ) {
265 <TFOOT>
266 <& SELF:footer_row, row => $opt{'footer'}, opt => \%opt &>
267 </TFOOT> 
268 % } 
269 </TABLE>
270 </%method>
271 <%method header_row>
272 <%args>
273 @sort_fields
274 @header
275 @header2 => ()
276 </%args>
277   <TR>
278 % my $h2 = 0;
279 % my $colspan = 0;
280 % my $order_by = $cgi->param('order_by');
281 % my $self_url = $cgi->url('-path_info' => 1, '-full' =>1);
282 % foreach my $header ( @header ) { 
283 %
284 %   my $field = shift @sort_fields;
285 %
286 %   $colspan-- if $colspan > 0;
287 %   next if $colspan;
288 %
289 %   my $label = ref($header) ? $header->{label} : $header;
290 %   unless ( ref($field) || !$field ) {
291 %     if ( $order_by eq $field ) {
292 %       $cgi->param('order_by', "$field DESC");
293 %     } else {
294 %       $cgi->param('order_by', $field);
295 %     }
296 %     $label = qq(<A HREF="$self_url?). $cgi->query_string.
297 %              qq(">$label</A>);
298 %   }
299 %
300 %   $colspan = ref($header) ? $header->{colspan} : 0;
301 %   my $rowspan = 1;
302 %   my $style = '';
303 %   if ( @header2 ) {
304 %     if ( !length($header2[$h2]) ) {
305 %       $rowspan = 2;
306 %       splice @header2, $h2, 1;
307 %     } else {
308 %       $h2++;
309 %       $style = 'STYLE="border-bottom: none"'
310 %     }
311 %   }
312     <TH CLASS   = "grid"
313         BGCOLOR = "#cccccc"
314         ROWSPAN = "<% $rowspan %>"
315         <% $colspan ? 'COLSPAN = "'.$colspan.'"' : '' %>
316         <% $style %>
317
318     >
319       <% $label %>
320     </TH>
321 % } 
322   </TR>
323
324 % if ( @header2 ) {
325   <TR>
326 %   foreach my $header ( @header2 ) { 
327 %     my $label = ref($header) ? $header->{label} : $header;
328       <TH CLASS="grid" BGCOLOR="#cccccc">
329         <FONT SIZE="-1"><% $label %></FONT>
330       </TH>
331 %   } 
332   </TR>
333 % }
334 </%method>
335 <%method data_rows>
336 <%args>
337 $rows => []
338 %opt
339 </%args>
340 % my %align = (
341 %   'l' => 'left',
342 %   'r' => 'right',
343 %   'c' => 'center',
344 %   ' ' => '',
345 %   '.' => '',
346 % );
347 % if ( $opt{align} and !ref($opt{align}) ) {
348 %   $opt{align} = [ map $align{$_}, split(//, $opt{align}) ];
349 % }
350
351 % my $i = 0; # for row striping # XXX CSS - nth-child
352 % foreach my $row ( @$rows ) {
353 %
354 %   my $rowstyle = '';
355 %   if ( $row eq $opt{'footer_data'} ) { # XXX CSS - tfoot
356 %     $rowstyle = ' STYLE="border-top: dashed 1px black; font-style: italic background-color=#dddddd"';
357 %   }
358 %
359 %   my $trid = '';
360 %   if ( $opt{'link_field' } ) {
361 %     my $link_field = $opt{'link_field'};
362 %     if ( ref($link_field) eq 'CODE' ) {
363 %       $trid = &{$link_field}($row);
364 %     } else {
365 %       $trid = $row->$link_field();
366 %     }
367 %   }
368     <TR ID="<%$trid |h%>" CLASS="row<% $i % 2 %>"<%$rowstyle%>>
369
370 %   if ( $opt{'fields'} ) {
371 %
372 %     my $links    = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
373 %     my $onclicks = $opt{'link_onclicks'} ? [ @{$opt{'link_onclicks'}} ] : [];
374 %     my $tooltips = $opt{'tooltips'} ? [ @{$opt{'tooltips'}} ] : [];
375 %     my $aligns   = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
376 %     my $colors   = $opt{'color'} ? [ @{$opt{'color'}} ] : [];
377 %     my $sizes    = $opt{'size'}  ? [ @{$opt{'size'}}  ] : [];
378 %     my $styles   = $opt{'style'} ? [ @{$opt{'style'}} ] : [];
379 %     my $cstyles  = $opt{'cell_style'} ? [ @{$opt{'cell_style'}} ] : [];
380 %     my $formats  = $opt{'format'} ? [ @{$opt{'format'}} ] : [];
381 %
382 %     foreach my $field (
383 %
384 %       # if the value of the field is an arrayref, then construct a table in
385 %       # the cell.
386 %       # if it's a (non-empty) scalar, and a format has been specified, then
387 %       # format the scalar with that.
388 %       # otherwise, just output the value.
389 %       # XXX we should also do date formats like this
390 %       map {
391 %             if ( ref($_) eq 'ARRAY' ) {
392 %
393 %               my $tableref = $_;
394 %
395 %               '<TABLE CLASS="inv" CELLSPACING=0 CELLPADDING=0 WIDTH="100%">'.
396 %
397 %               join('', map {
398 %
399 %                 my $rowref = $_;
400 %
401 %                 '<tr>'.
402 %
403 %                 join('', map {
404 %
405 %                   my $e = $_;
406 %
407 %                   '<TD '.
408 %                     join(' ', map {
409 %                       uc($_).'="'. $e->{$_}. '"';
410 %                     }
411 %                     grep exists($e->{$_}),
412 %                          qw( align bgcolor colspan rowspan
413 %                              style valign width )
414 %                     ).
415 %                   '>'.
416 %
417 %                   ( $e->{'link'}
418 %                       ? '<A HREF="'. $e->{'link'}. '">'
419 %                       : ''
420 %                   ).
421 %                   ( $e->{'onclick'} # don't use with 'link'
422 %                       ? '<A HREF="#" onclick="' .
423 %                         $e->{'onclick'}.'">'
424 %                       : ''
425 %                   ).
426 %                   ( $e->{'size'}
427 %                      ? '<FONT SIZE="'.uc($e->{'size'}).'">'
428 %                      : ''
429 %                   ).
430 %                   ( $e->{'data_style'}
431 %                       ? '<'. uc($e->{'data_style'}). '>'
432 %                       : ''
433 %                   ).
434 %                   $e->{'data'}.
435 %                   ( $e->{'data_style'}
436 %                       ? '</'. uc($e->{'data_style'}). '>'
437 %                       : ''
438 %                   ).
439 %                   ( $e->{'size'} ? '</FONT>' : '' ).
440 %                   ( $e->{'link'} || $e->{'onclick'} 
441 %                       ? '</A>'
442 %                       : '' ).
443 %                   '</td>';
444 %
445 %                 } @$rowref ).
446 %
447 %                 '</tr>';
448 %               } @$tableref ).
449 %
450 %               '</table>';
451 %
452 %             } else {
453 %               if ( length($_) > 0 and my $format = shift @$formats ) {
454 %                 $_ = sprintf($format, $_);
455 %               }
456 %               $_;
457 %             }
458 %           }
459 %
460 %       # get the value of the field spec:
461 %       # - if the spec is a coderef, evaluate the coderef
462 %       # - if the spec is a string, call that string as a method
463 %       # - if the spec is an integer, get the field in that position
464 %       map {
465 %             if ( ref($_) eq 'CODE' ) {
466 %               &{$_}($row);
467 %             } elsif ( ref($row) eq 'ARRAY' and 
468 %                       $_ =~ /^\d+$/ ) {
469 %             # for the 'straight SQL' case: specify fields
470 %             # by position
471 %               encode_entities($row->[$_]);
472 %             } else {
473 %               encode_entities($row->$_());
474 %             }
475 %           }
476 %       @{$opt{'fields'}}
477 %
478 %     ) {
479 %
480 %       my $class = ( $field =~ /^<TABLE/i ) ? 'inv' : 'grid';
481 %       my $class = 'grid';
482 %
483 %       my $align = $aligns ? shift @$aligns : '';
484 %       $align = " ALIGN=$align" if $align;
485 %
486 %       my $a = '';
487 %       if ( $links ) {
488 %         my $link = shift @$links;
489 %         my $onclick = shift @$onclicks;
490 %         my $tooltip = shift @$tooltips;
491 %
492 %         if (    ! $opt{'agent_virt'}
493 %              || ( $opt{'null_link'} && ! $row->agentnum )
494 %              || grep { $row->agentnum == $_ }
495 %                      @{ $opt{link_agentnums} }
496 %            ) {
497 %
498 %           $link = &{$link}($row)
499 %             if ref($link) eq 'CODE';
500 %
501 %           $onclick = &{$onclick}($row)
502 %             if ref($onclick) eq 'CODE';
503 %           $onclick = qq( onClick="$onclick") if $onclick;
504 %
505 %           $tooltip = &{$tooltip}($row)
506 %             if ref($tooltip) eq 'CODE';
507 %           $tooltip = qq! title="<% $tooltip |h %>"!;
508 %
509 %           if ( $link ) {
510 %             my( $url, $method ) = @{$link};
511 %             if ( ref($method) eq 'CODE' ) {
512 %               $a = $url. &{$method}($row);
513 %             } else {
514 %               $a = $url. $row->$method();
515 %             }
516 %             $a = qq(<A HREF="$a"$onclick$tooltip>);
517 %           }
518 %           elsif ( $onclick ) {
519 %             $a = qq(<A HREF="javascript:void(0);"$onclick>);
520 %           }
521 %           elsif ( $tooltip ) {
522 %             $a = qq(<A $tooltip>);
523 %           }
524
525 %         }
526 %
527 %       }
528 %
529 %       my $font = '';
530 %       my $color = shift @$colors;
531 %       $color = &{$color}($row) if ref($color) eq 'CODE';
532 %       my $size = shift @$sizes;
533 %       $size = &{$size}($row) if ref($size) eq 'CODE';
534 %       if ( $color || $size ) {
535 %         $font = '<FONT '.
536 %                 ( $color ? "COLOR=#$color "   : '' ).
537 %                 ( $size  ? qq(SIZE="$size" )  : '' ).
538 %                 '>';
539 %       }
540 %
541 %       my($s, $es) = ( '', '' );
542 %       my $style = shift @$styles;
543 %       $style = &{$style}($row) if ref($style) eq 'CODE';
544 %       if ( $style ) {
545 %         $s = join( '', map "<$_>", split('', $style) );
546 %         $es = join( '', map "</$_>", split('', $style) );
547 %       }
548 %
549 %       my $cstyle = shift @$cstyles;
550 %       $cstyle = &{$cstyle}($row) if ref($cstyle) eq 'CODE';
551 %       $cstyle = qq(STYLE="$cstyle")
552 %         if $cstyle;
553
554         <TD CLASS="<% $class %>" <% $align %> <% $cstyle %>><% $a %><% $font %><% $s %><% $field %><% $es %><% $font ? '</FONT>' : '' %><% $a ? '</A>' : '' %></TD>
555
556 %     } 
557 %
558 %   } else { # not $opt{'fields'}
559 %
560 %     foreach ( @$row ) { 
561         <TD CLASS="grid"><% $_ %></TD>
562 %     }
563 %
564 %   }
565
566     </TR>
567
568 %   $i++;
569 %
570 % } # foreach $row
571 </%method>
572 <%method footer_row>
573 <%args>
574 $row
575 %opt
576 </%args>
577 %# don't try to respect all the styling options, just the ones that are
578 %# hard to replicate with CSS
579 % my %align = (
580 %   'l' => 'left',
581 %   'r' => 'right',
582 %   'c' => 'center',
583 %   ' ' => '',
584 %   '.' => '',
585 % );
586 % if ( $opt{align} and !ref($opt{align}) ) {
587 %   $opt{align} = [ map $align{$_}, split(//, $opt{align}) ];
588 % }
589 % my @aligns = @{ $opt{align} };
590
591 <TR>
592 % foreach my $footer ( @$row ) {
593 %   $footer = &{$footer}() if ref($footer) eq 'CODE';
594 %   my $align = shift @aligns;
595 %   my $style = '';
596 %   $style .= "text-align: $align;" if $align;
597     <TD CLASS="grid" STYLE="<% $style %>"><% $footer %></TD>
598 % } 
599 </TR>
600 </%method>
601