sales by ad source report, #17971
[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 %date_format;
46 xl_parse_date_init();
47
48 my $writer = sub {
49   # Wrapper for $worksheet->write.
50   # Do any massaging of the value/format here.
51   my ($r, $c, $value, $format) = @_;
52   if ( $value =~ /^\Q$money_char\E(\d+\.?\d*)$/ ) {
53     # Currency: strip the symbol, clone the requested format,
54     # and format it for currency
55     $value = $1;
56 #    warn "formatting $value as money\n";
57     if ( !exists($money_format{$format}) ) {
58       $money_format{$format} = $workbook->add_format();
59       $money_format{$format}->copy($format);
60       $money_format{$format}->set_num_format($money_char.'#0.00#');
61     }
62     $format = $money_format{$format};
63   }
64   elsif ( $value =~ /^([A-Z][a-z]{2}) (\d{2}) (\d{4})$/ ) {
65     # Date: convert the value to an Excel date number and set 
66     # the format
67     $value = xl_parse_date($value);
68 #    warn "formatting $value as date\n";
69     if ( !exists($date_format{$format}) ) {
70       $date_format{$format} = $workbook->add_format();
71       $date_format{$format}->copy($format);
72       $date_format{$format}->set_num_format('mmm dd yyyy');
73     }
74     $format = $date_format{$format};
75   }
76   else {
77     # String: replace line breaks with newlines
78     $value =~ s/<BR>/\n/gi;
79   }
80   $worksheet->write($r, $c, $value, $format);
81 };
82
83 $writer->( $r, $c++, $_, $header_format ) foreach @$header;
84
85 foreach my $row ( @$rows ) {
86   $r++;
87   $c = 0;
88
89   if ( $opt{'fields'} ) {
90
91     #my $links = $opt{'links'} ? [ @{$opt{'links'}} ] : '';
92     #my $aligns = $opt{'align'} ? [ @{$opt{'align'}} ] : '';
93     #could also translate color, size, style into xls equivalents?
94     my $formats = $opt{'xls_format'} ? [ @{$opt{'xls_format'}} ] : [];
95
96     foreach my $field ( @{$opt{'fields'}} ) {
97
98       my $xls_format = $default_format;
99
100       if ( my $format = shift @$formats ) {
101         $format = &{$format}($row) if ref($format) eq 'CODE';
102         $format ||= {};
103         $xls_format = $workbook->add_format(locked=>0, %$format);
104       }
105
106       if ( ref($field) eq 'CODE' ) {
107         foreach my $value ( &{$field}($row) ) {
108           if ( ref($value) eq 'ARRAY' ) { 
109             $writer->($r, $c++, '(N/A)' ); #unimplemented
110           } else {
111             $writer->($r, $c++, $value, $xls_format );
112           }
113         }
114       } else {
115         $writer->( $r, $c++, $row->$field(), $xls_format );
116       }
117     }
118
119   } else {
120     # no need for each row to need a new format
121     #my $xls_format = $workbook->add_format(locked=>0);
122     $writer->( $r, $c++, $_, $default_format ) foreach @$row;
123   }
124
125 }
126
127 $workbook->close();# or die "Error creating .xls file: $!";
128
129 http_header('Content-Length' => length($data) );
130
131 </%init>