summaryrefslogtreecommitdiff
path: root/httemplate/search/report_tax-xls.cgi
blob: 1c278dfd1af6c7e675bb563b081b88cc64959b22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<% $data %>
<%init>

my $data = '';
my $XLS = new IO::Scalar \$data;
my $workbook = Spreadsheet::WriteExcel->new($XLS)
  or die "Error opening .xls file: $!";

# hardcoded formats, this could be handled better
my $light_gray = $workbook->set_custom_color(63, '#eeeeee');
my %format = (
  title => {
    size      => 24,
    align     => 'center',
    bg_color  => 'silver',
  },
  colhead => {
    size      => 11,
    bold      => 1,
    align     => 'center',
    valign    => 'vcenter',
    text_wrap => 1,
  },
  rowhead => {
    size      => 11,
    valign    => 'bottom',
    text_wrap => 1,
  },
  amount  => {
    size      => 11,
    align     => 'right',
    valign    => 'bottom',
    num_format=> 8,
  },
  'size-1' => {
    size      => 7.5,
    align     => 'center',
    valign    => 'vcenter',
    bold      => 1,
    text_wrap => 1,
  },
  'size+1' => {
    size      => 12,
    align     => 'center',
    valign    => 'vcenter',
    bold      => 1,
  },
  text => {
    size      => 11,
    text_wrap => 1,
  },
);
my %default = (
  font      => 'Calibri',
  bg_color  => $light_gray,
  border    => 1,
);
my @widths = ( #ick
  18, (10.5, 3) x 6, 10.5, 10.5, 3, 10.5, 3, 10.5, 3, 10.5
);
foreach (keys(%format)) {
  my %f = (%default, %{$format{$_}});
  $format{$_} = $workbook->add_format(%f);
  $format{"m_$_"} = $workbook->add_format(%f); # for merged cells
  $format{"t_$_"} = $workbook->add_format(%f, bg_color => 'yellow'); # totals
}
my $ws = $workbook->add_worksheet('taxreport');

my $htmldoc = include('report_tax.cgi');

my ($title) = ($htmldoc =~ /<title>\s*(.*)\s*<\/title>/i);

# attribs option: how to locate the table?  It's the only one with class="grid".
my $te = HTML::TableExtract->new(attribs => {class => 'grid'});
$te->parse($htmldoc);
my $table = $te->first_table_found;

my @sheet;
$sheet[0][0] = {
  text    => $title,
  format  => 'title',
  colspan => '18',
};  
# excel position
my $x = 0;
my $y = 3;
foreach my $row ($table->rows()) {
  $x = 0;
  $sheet[$y] = [];
  foreach my $cell (@$row) {
    if ($cell and ref($cell) eq 'HTML::ElementTable::DataElement') {
      my $f = 'text';
      if ( $cell->as_HTML =~ /font/i ) {
        my ($el) = $cell->content_list;
        $f = 'size'.$el->attr('size') if $el->attr('size');
      }
      elsif ( $cell->as_text =~ /^\$/ ) {
        $f = 'amount'
      }
      elsif ( $cell->tag eq 'th' ) {
        $f = 'colhead';
      }
      elsif ( $x == 0 ) {
        $f = 'rowhead';
      }
      $sheet[$y][$x] = {
        text    => $cell->as_text,
        format  => $f,
        rowspan => $cell->attr('rowspan'),
        colspan => $cell->attr('colspan'),
      };
    }
    $x++;
  } #for $cell
  $y++;
}

$y = 0;
foreach my $row (@sheet) {
  $x = 0;
  my $t_row = 1 if($row->[0]->{'text'} eq 'Total');
  foreach my $cell (@$row) {
    if ($cell) {
      my $f = $cell->{format};
      if ($cell->{rowspan} > 1 or $cell->{colspan} > 1) {
        my $range = xl_range_formula(
          'Taxreport', 
          $y,
          $y - 1 + ($cell->{rowspan} || 1),
          $x,
          $x - 1 + ($cell->{colspan} || 1)
        );
        $ws->merge_range($range, $cell->{text}, $format{"m_$f"});
      }
      else {
        $f = "t_$f" if $t_row;
        $ws->write($y, $x, $cell->{text}, $format{$f});
      }
    } #if $cell
    $x++;
  }
  $y++;
}

for my $x (0..scalar(@widths)-1) {
  $ws->set_column($x, $x, $widths[$x]);
}

$workbook->close;

http_header('Content-Type' => 'application/vnd.ms-excel');
http_header('Content-Disposition' => 'attachment;filename="report_tax.xls"');
</%init>