remove customer deletion
[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 $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
36
37 $worksheet->protect();
38
39 my($r,$c) = (0,0);
40
41 my $header_format = $workbook->add_format(
42   bold     => 1,
43   locked   => 1,
44   bg_color => 55, #22,
45   bottom   => 3,
46 );
47 my $footer_format = $workbook->add_format(
48   italic   => 1,
49   locked   => 1,
50   bg_color => 55,
51   top      => 3,
52 );
53 my $default_format = $workbook->add_format(locked => 0);
54
55 my %money_format;
56 my $money_char = FS::Conf->new->config('money_char') || '$';
57
58 my %date_format;
59 xl_parse_date_init();
60
61 my %bold_format;
62
63 my $writer = sub {
64   # Wrapper for $worksheet->write.
65   # Do any massaging of the value/format here.
66   my ($r, $c, $value, $format) = @_;
67   #warn "writer called with format $format\n";
68
69   if ( $style->[$c] eq 'b' or $value =~ /<b>/i ) { # the only one in common use
70     $value =~ s[</?b>][]ig;
71     if ( !exists($bold_format{$format}) ) {
72       $bold_format{$format} = $workbook->add_format();
73       $bold_format{$format}->copy($format);
74       $bold_format{$format}->set_bold();
75     }
76     $format = $bold_format{$format};
77   }
78
79   # convert HTML entities
80   # both Spreadsheet::WriteExcel and Excel::Writer::XLSX accept UTF-8 strings
81   $value = decode_entities($value);
82
83   if ( $value =~ /^\Q$money_char\E(-?\d+\.?\d*)$/ ) {
84     # Currency: strip the symbol, clone the requested format,
85     # and format it for currency
86     $value = $1;
87 #    warn "formatting $value as money\n";
88     if ( !exists($money_format{$format}) ) {
89       $money_format{$format} = $workbook->add_format();
90       $money_format{$format}->copy($format);
91       $money_format{$format}->set_num_format($money_char.'#0.00#');
92     }
93     $format = $money_format{$format};
94   }
95   elsif ( $value =~ /^([A-Z][a-z]{2}) (\d{2}) (\d{4})$/ ) {
96     # Date: convert the value to an Excel date number and set 
97     # the format
98     $value = xl_parse_date($value);
99 #    warn "formatting $value as date\n";
100     if ( !exists($date_format{$format}) ) {
101       $date_format{$format} = $workbook->add_format();
102       $date_format{$format}->copy($format);
103       $date_format{$format}->set_num_format('mmm dd yyyy');
104     }
105     $format = $date_format{$format};
106   }
107   else {
108     # String: replace line breaks with newlines
109     $value =~ s/<BR>/\n/gi;
110   }
111   #warn "writing with format $format\n";
112   $worksheet->write($r, $c, $value, $format);
113 };
114
115 $writer->( $r, $c++, $_, $header_format ) foreach @$header;
116
117 foreach my $row ( @$rows ) {
118   $r++;
119   $c = 0;
120
121   if ( $opt{'fields'} ) {
122
123     #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
124     #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
125     #could also translate color, size, style into xls equivalents?
126     my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
127
128     foreach my $field ( @{$opt{'fields'}} ) {
129
130       my $xls_format = $default_format;
131
132       if ( my $format = shift @$formats ) {
133         $format = &{$format}($row) if ref($format) eq 'CODE';
134         $format ||= {};
135         $xls_format = $workbook->add_format(locked=>0, %$format);
136       }
137
138       if ( ref($field) eq 'CODE' ) {
139         foreach my $value ( &{$field}($row) ) {
140           if ( ref($value) eq 'ARRAY' ) { 
141             $writer->($r, $c++, '(N/A)' ); #unimplemented
142           } else {
143             $writer->($r, $c++, $value, $xls_format );
144           }
145         }
146       } else {
147         $writer->( $r, $c++, $row->$field(), $xls_format );
148       }
149     }
150
151   } else {
152     # no need for each row to need a new format
153     #my $xls_format = $workbook->add_format(locked=>0);
154     $writer->( $r, $c++, $_, $default_format ) foreach @$row;
155   }
156
157 }
158
159 if ( $opt{'footer'} ) {
160   $r++;
161   $c = 0;
162   foreach my $item (@{ $opt{'footer'} }) {
163     if ( ref($item) eq 'CODE' ) {
164       $item = &{$item}();
165     }
166     $writer->( $r, $c++, $item, $footer_format );
167   }
168 }
169
170 $workbook->close();# or die "Error creating .xls file: $!";
171
172 http_header('Content-Length' => length($data) );
173 $m->print($data);
174
175 </%init>