deposit slips
[freeside.git] / httemplate / search / elements / grouped-search / xls
1 <%doc>
2
3 Excel spreadsheet view.
4
5 </%doc>
6 <%init>
7 my %opt = @_;
8
9 my $group_info = $m->comp('core', %opt);
10
11 # minor false laziness with search-xls.html
12 my $override = '';
13 $override = 'XLSX' if grep { $_->num_rows >= 65536 }
14                       @{ $group_info->{groups} };
15
16 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
17
18 my $filename = $opt{'name'} || PL($opt{'name_singular'});
19 $filename .= $format->{extension};
20
21 http_header('Content-Type' => $format->{mime_type} );
22 http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
23 $HTML::Mason::Commands::r->headers_out->{'Cache-control'} = 'max-age=0';
24
25 my $data = '';
26 my $XLS = new IO::Scalar \$data;
27 my $workbook = $format->{class}->new($XLS)
28   or die "Error opening Excel file: $!";
29
30 my $title = $opt{'title'};
31 $title =~ s/[\[\]\:\*\?\/\/]//g;
32 $title = substr($title, 0, 31);
33
34 for (my $curr_group = 0; $curr_group < $group_info->{num}; $curr_group++) {
35   my $group = $group_info->{groups}[$curr_group];
36   my $query = $group_info->{queries}[$curr_group];
37   my $footer = $group_info->{group_footers}[$curr_group];
38   my $label = $group_info->{group_labels}[$curr_group];
39   # run the query
40   my @rows = $query->qsearch;
41   #warn Dumper(\@rows); #DEBUG
42
43   # pass arrayrefs to write_row to write multiple rows
44   $opt{footer} = [ List::MoreUtils::pairwise { [ $a, $b ] }
45     @$footer,
46     @{$group_info->{total_footer}}
47   ];
48   $m->comp('/search/elements/search-xls.html:worksheet',
49     workbook  => $workbook,
50     title     => $label,
51     header    => $opt{header},
52     opt       => \%opt,
53     rows      => \@rows,
54   );
55 }
56
57 $workbook->close();
58
59 $m->clear_buffer();
60 $m->print($data);
61 </%init>