Excel currency format, #1313
[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 #http_header('Content-Type' => 'application/excel' ); #eww
11 #http_header('Content-Type' => 'application/msexcel' ); #alas
12 #http_header('Content-Type' => 'application/x-msexcel' ); #?
13
14 #http://support.microsoft.com/kb/199841
15 http_header('Content-Type' => 'application/vnd.ms-excel' );
16 http_header('Content-Disposition' => 
17   'attachment;filename="'.($opt{'name'} || PL($opt{'name_singular'}) ).'.xls"');
18  
19 #http://support.microsoft.com/kb/812935
20 #http://support.microsoft.com/kb/323308
21 $HTML::Mason::Commands::r->headers_out->{'Cache-control'} = 'max-age=0';
22
23 my $data = '';
24 my $XLS = new IO::Scalar \$data;
25 my $workbook = Spreadsheet::WriteExcel->new($XLS)
26   or die "Error opening .xls file: $!";
27
28 my $worksheet = $workbook->add_worksheet(substr($opt{'title'},0,31));
29
30 $worksheet->protect();
31
32 my($r,$c) = (0,0);
33
34 my $header_format = $workbook->add_format(
35   bold     => 1,
36   locked   => 1,
37   bg_color => 55, #22,
38   bottom   => 3,
39 );
40 my $default_format = $workbook->add_format(locked => 0);
41
42 my %money_format;
43 my $money_char = FS::Conf->new->config('money_char') || '$';
44
45 my $writer = sub {
46   # Wrapper for $worksheet->write.
47   # Do any massaging of the value/format here.
48   my ($r, $c, $value, $format) = @_;
49   if ( $value =~ /^\Q$money_char\E(\d+\.?\d*)$/ ) {
50     # Currency: strip the symbol, clone the requested format,
51     # and format it for currency
52     $value = $1;
53     if ( !exists($money_format{$format}) ) {
54       $money_format{$format} = $workbook->add_format();
55       $money_format{$format}->copy($format);
56       $money_format{$format}->set_num_format($money_char.'#0.00#');
57     }
58     $format = $money_format{$format};
59   }
60   $worksheet->write($r, $c, $value, $format);
61 };
62
63 $writer->( $r, $c++, $_, $header_format ) foreach @$header;
64
65 foreach my $row ( @$rows ) {
66   $r++;
67   $c = 0;
68
69   if ( $opt{'fields'} ) {
70
71     #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
72     #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
73     #could also translate color, size, style into xls equivalents?
74     my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
75
76     foreach my $field ( @{$opt{'fields'}} ) {
77
78       my $xls_format = $default_format;
79
80       if ( my $format = shift @$formats ) {
81         $format = &{$format}($row) if ref($format) eq 'CODE';
82         $format ||= {};
83         $xls_format = $workbook->add_format(locked=>0, %$format);
84       }
85
86       if ( ref($field) eq 'CODE' ) {
87         foreach my $value ( &{$field}($row) ) {
88           if ( ref($value) eq 'ARRAY' ) { 
89             $writer->($r, $c++, '(N/A)' ); #unimplemented
90           } else {
91             $writer->($r, $c++, $value, $xls_format );
92           }
93         }
94       } else {
95         $writer->( $r, $c++, $row->$field(), $xls_format );
96       }
97     }
98
99   } else {
100     # no need for each row to need a new format
101     #my $xls_format = $workbook->add_format(locked=>0);
102     $writer->( $r, $c++, $_, $default_format ) foreach @$row;
103   }
104
105 }
106
107 $workbook->close();# or die "Error creating .xls file: $!";
108
109 http_header('Content-Length' => length($data) );
110
111 </%init>