combine ticket notification scrips, #15353
[freeside.git] / httemplate / search / report_tax-xls.cgi
1 <% $data %>
2 <%init>
3
4 my $data = '';
5 my $XLS = new IO::Scalar \$data;
6 my $workbook = Spreadsheet::WriteExcel->new($XLS)
7   or die "Error opening .xls file: $!";
8
9 # hardcoded formats, this could be handled better
10 my $light_gray = $workbook->set_custom_color(63, '#eeeeee');
11 my %format = (
12   title => {
13     size      => 24,
14     align     => 'center',
15     bg_color  => 'silver',
16   },
17   colhead => {
18     size      => 11,
19     bold      => 1,
20     align     => 'center',
21     valign    => 'vcenter',
22     text_wrap => 1,
23   },
24   rowhead => {
25     size      => 11,
26     valign    => 'bottom',
27     text_wrap => 1,
28   },
29   amount  => {
30     size      => 11,
31     align     => 'right',
32     valign    => 'bottom',
33     num_format=> 8,
34   },
35   'size-1' => {
36     size      => 7.5,
37     align     => 'center',
38     valign    => 'vcenter',
39     bold      => 1,
40     text_wrap => 1,
41   },
42   'size+1' => {
43     size      => 12,
44     align     => 'center',
45     valign    => 'vcenter',
46     bold      => 1,
47   },
48   text => {
49     size      => 11,
50     text_wrap => 1,
51   },
52 );
53 my %default = (
54   font      => 'Calibri',
55   bg_color  => $light_gray,
56   border    => 1,
57 );
58 my @widths = ( #ick
59   18, (10.5, 3) x 6, 10.5, 10.5, 3, 10.5, 3, 10.5, 3, 10.5
60 );
61 foreach (keys(%format)) {
62   my %f = (%default, %{$format{$_}});
63   $format{$_} = $workbook->add_format(%f);
64   $format{"m_$_"} = $workbook->add_format(%f); # for merged cells
65   $format{"t_$_"} = $workbook->add_format(%f, bg_color => 'yellow'); # totals
66 }
67 my $ws = $workbook->add_worksheet('taxreport');
68
69 my $htmldoc = include('report_tax.cgi');
70
71 my ($title) = ($htmldoc =~ /<title>\s*(.*)\s*<\/title>/i);
72
73 # attribs option: how to locate the table?  It's the only one with class="grid".
74 my $te = HTML::TableExtract->new(attribs => {class => 'grid'});
75 $te->parse($htmldoc);
76 my $table = $te->first_table_found;
77
78 my @sheet;
79 $sheet[0][0] = {
80   text    => $title,
81   format  => 'title',
82   colspan => '18',
83 };  
84 # excel position
85 my $x = 0;
86 my $y = 3;
87 foreach my $row ($table->rows()) {
88   $x = 0;
89   $sheet[$y] = [];
90   foreach my $cell (@$row) {
91     if ($cell and ref($cell) eq 'HTML::ElementTable::DataElement') {
92       my $f = 'text';
93       if ( $cell->as_HTML =~ /font/i ) {
94         my ($el) = $cell->content_list;
95         $f = 'size'.$el->attr('size') if $el->attr('size');
96       }
97       elsif ( $cell->as_text =~ /^\$/ ) {
98         $f = 'amount'
99       }
100       elsif ( $cell->tag eq 'th' ) {
101         $f = 'colhead';
102       }
103       elsif ( $x == 0 ) {
104         $f = 'rowhead';
105       }
106       $sheet[$y][$x] = {
107         text    => $cell->as_text,
108         format  => $f,
109         rowspan => $cell->attr('rowspan'),
110         colspan => $cell->attr('colspan'),
111       };
112     }
113     $x++;
114   } #for $cell
115   $y++;
116 }
117
118 $y = 0;
119 foreach my $row (@sheet) {
120   $x = 0;
121   my $t_row = 1 if($row->[0]->{'text'} eq 'Total');
122   foreach my $cell (@$row) {
123     if ($cell) {
124       my $f = $cell->{format};
125       if ($cell->{rowspan} > 1 or $cell->{colspan} > 1) {
126         my $range = xl_range_formula(
127           'Taxreport', 
128           $y,
129           $y - 1 + ($cell->{rowspan} || 1),
130           $x,
131           $x - 1 + ($cell->{colspan} || 1)
132         );
133         $ws->merge_range($range, $cell->{text}, $format{"m_$f"});
134       }
135       else {
136         $f = "t_$f" if $t_row;
137         $ws->write($y, $x, $cell->{text}, $format{$f});
138       }
139     } #if $cell
140     $x++;
141   }
142   $y++;
143 }
144
145 for my $x (0..scalar(@widths)-1) {
146   $ws->set_column($x, $x, $widths[$x]);
147 }
148
149 $workbook->close;
150
151 http_header('Content-Type' => 'application/vnd.ms-excel');
152 http_header('Content-Disposition' => 'attachment;filename="report_tax.xls"');
153 </%init>