show footers in XLS and CSV reports, #18823
[freeside.git] / httemplate / search / elements / search-xls.html
1 <% $data %>
2 <%init>
3
4 my %args = @_;
5 my $type   = $args{'type'};
6 my $header = $args{'header'};
7 my $rows   = $args{'rows'};
8 my %opt    = %{ $args{'opt'} };    
9
10 my $override = scalar(@$rows) >= 65536 ? 'XLSX' : '';
11
12 my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format($override);
13
14 my $filename = $opt{'name'} || PL($opt{'name_singular'});
15 $filename .= $format->{extension};
16
17 #http_header('Content-Type' => 'application/excel' ); #eww
18 #http_header('Content-Type' => 'application/msexcel' ); #alas
19 #http_header('Content-Type' => 'application/x-msexcel' ); #?
20
21 #http://support.microsoft.com/kb/199841
22 http_header('Content-Type' => $format->{mime_type} );
23 http_header('Content-Disposition' => qq!attachment;filename="$filename"! );
24  
25 #http://support.microsoft.com/kb/812935
26 #http://support.microsoft.com/kb/323308
27 $HTML::Mason::Commands::r->headers_out->{'Cache-control'} = 'max-age=0';
28
29 my $data = '';
30 my $XLS = new IO::Scalar \$data;
31 my $workbook = $format->{class}->new($XLS)
32   or die "Error opening Excel file: $!";
33
34 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
35
36 $worksheet->protect();
37
38 my($r,$c) = (0,0);
39
40 my $header_format = $workbook->add_format(
41   bold     => 1,
42   locked   => 1,
43   bg_color => 55, #22,
44   bottom   => 3,
45 );
46 my $default_format = $workbook->add_format(locked => 0);
47
48 my %money_format;
49 my $money_char = FS::Conf->new->config('money_char') || '$';
50
51 my %date_format;
52 xl_parse_date_init();
53
54 my $writer = sub {
55   # Wrapper for $worksheet->write.
56   # Do any massaging of the value/format here.
57   my ($r, $c, $value, $format) = @_;
58   # convert HTML entities
59   # both Spreadsheet::WriteExcel and Excel::Writer::XLSX accept UTF-8 strings
60   $value = decode_entities($value);
61
62   if ( $value =~ /^\Q$money_char\E(-?\d+\.?\d*)$/ ) {
63     # Currency: strip the symbol, clone the requested format,
64     # and format it for currency
65     $value = $1;
66 #    warn "formatting $value as money\n";
67     if ( !exists($money_format{$format}) ) {
68       $money_format{$format} = $workbook->add_format();
69       $money_format{$format}->copy($format);
70       $money_format{$format}->set_num_format($money_char.'#0.00#');
71     }
72     $format = $money_format{$format};
73   }
74   elsif ( $value =~ /^([A-Z][a-z]{2}) (\d{2}) (\d{4})$/ ) {
75     # Date: convert the value to an Excel date number and set 
76     # the format
77     $value = xl_parse_date($value);
78 #    warn "formatting $value as date\n";
79     if ( !exists($date_format{$format}) ) {
80       $date_format{$format} = $workbook->add_format();
81       $date_format{$format}->copy($format);
82       $date_format{$format}->set_num_format('mmm dd yyyy');
83     }
84     $format = $date_format{$format};
85   }
86   else {
87     # String: replace line breaks with newlines
88     $value =~ s/<BR>/\n/gi;
89   }
90   $worksheet->write($r, $c, $value, $format);
91 };
92
93 $writer->( $r, $c++, $_, $header_format ) foreach @$header;
94
95 foreach my $row ( @$rows ) {
96   $r++;
97   $c = 0;
98
99   if ( $opt{'fields'} ) {
100
101     #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
102     #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
103     #could also translate color, size, style into xls equivalents?
104     my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
105
106     foreach my $field ( @{$opt{'fields'}} ) {
107
108       my $xls_format = $default_format;
109
110       if ( my $format = shift @$formats ) {
111         $format = &{$format}($row) if ref($format) eq 'CODE';
112         $format ||= {};
113         $xls_format = $workbook->add_format(locked=>0, %$format);
114       }
115
116       if ( ref($field) eq 'CODE' ) {
117         foreach my $value ( &{$field}($row) ) {
118           if ( ref($value) eq 'ARRAY' ) { 
119             $writer->($r, $c++, '(N/A)' ); #unimplemented
120           } else {
121             $writer->($r, $c++, $value, $xls_format );
122           }
123         }
124       } else {
125         $writer->( $r, $c++, $row->$field(), $xls_format );
126       }
127     }
128
129   } else {
130     # no need for each row to need a new format
131     #my $xls_format = $workbook->add_format(locked=>0);
132     $writer->( $r, $c++, $_, $default_format ) foreach @$row;
133   }
134
135 }
136
137 if ( $opt{'footer'} ) {
138   $r++;
139   $c = 0;
140   foreach my $item (@{ $opt{'footer'} }) {
141     if ( ref($item) eq 'CODE' ) {
142       $item = &{$item}();
143     }
144     $writer->( $r, $c++, $item, $header_format );
145   }
146 }
147
148 $workbook->close();# or die "Error creating .xls file: $!";
149
150 http_header('Content-Length' => length($data) );
151
152 </%init>