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