summaryrefslogtreecommitdiff
path: root/httemplate/search/elements/grid-report.html
blob: efc00972517ce6ace9c1e7ef39c9d256991e73f7 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<%doc>

Simple display front-end for reports that produce some kind of data table,
which the user can request as an Excel spreadsheet. /elements/header.html
and /elements/footer.html are included automatically, so don't include them
again.

This element defines "total", "shaded", and "totalshaded" CSS classes. For
anything else, insert a <style> element via the 'head' argument.

Usage:

<& elements/grid-report.html,
  title => 'My Report',
  rows => [
    { header => 1, },
    ...
  ],
  cells => [
    [ # row 0
      { value => '123.45',
        # optional
        format => 'money',
        header => 1,
        rowspan => 2,
        colspan => 3,
        class => 'shaded',
      },
      ...
    ],
  ],
  head => q[<div>Thing to insert before the table</div>],
  foot => q[<span>That's all folks!</span>].
&>
</%doc>
% if ( $cgi->param('_type') =~ /(xls)$/ ) {
<%perl>
  # egregious false laziness w/ search/report_tax-xls.cgi
  # and search/customer_cdr_profit.html
  my $format = $FS::CurrentUser::CurrentUser->spreadsheet_format;
  my $filename = $cgi->url(-relative => 1);
  $filename =~ s/\.html$//;
  $filename .= $format->{extension};
  http_header('Content-Type' => $format->{mime_type});
  http_header('Content-Disposition' => qq!attachment;filename="$filename"!);

  my $output = '';
  my $XLS = IO::String->new($output);
  my $workbook = $format->{class}->new($XLS)
    or die "Error opening .xls file: $!";

  my $worksheet = $workbook->add_worksheet('Summary');

  my %format = (
    header => {
      size      => 11,
      bold      => 1,
      align     => 'center',
      valign    => 'vcenter',
      text_wrap => 1,
    },
    money => {
      size      => 11,
      align     => 'right',
      valign    => 'bottom',
      num_format=> 8,
    },
    '' => {},
  );
  my %default = (
      font      => 'Calibri',
      border    => 1,
  );
  foreach (keys %format) {
    my %f = (%default, %{$format{$_}});
    $format{$_} = $workbook->add_format(%f);
    $format{"m_$_"} = $workbook->add_format(%f);
  }

  my ($r, $c) = (0, 0);
  # indices in these correspond to column positions
  my @rowspans;
  my @widths;
  
  for my $row (@rows) {
    $c = 0;
    my $thisrow = shift @cells;
    for my $cell (@$thisrow) {
      # skip over cells that are occupied by rowspans above them
      while ($rowspans[$c]) {
        $rowspans[$c]--;
        $c++;
      }

      # skip this cell if it's empty, also
      next if !ref($cell);
      # format name
      my $f = '';
      $f = 'header' if $row->{header} or $cell->{header};
      $f = 'money' if $cell->{format} eq 'money';
      if ( $cell->{rowspan} > 1 or $cell->{colspan} > 1 ) {
        my $range = xl_range_formula(
          'Summary',
          $r, $r - 1 + ($cell->{rowspan} || 1),
          $c, $c - 1 + ($cell->{colspan} || 1)
        );
        #warn "merging $range\n";
        $worksheet->merge_range($range, $cell->{value}, $format{"m_$f"});
      } else {
      #warn "writing ".xl_rowcol_to_cell($r, $c)."\n";
        $worksheet->write( $r, $c, $cell->{value}, $format{$f} );
      }

      # estimate column width, as in search-xls, but without date formats
      my $width = length($cell->{value}) / ($cell->{colspan} || 1);
      $width *= 1.1 if $f eq 'header';
      $width++ if $f eq 'money'; # for money symbol
      $width += 2; # pad it

      for (1 .. ($cell->{colspan} || 1)) {
        # adjust minimum widths to allow for this cell's contents
        $widths[$c] = $width if $width > ($widths[$c] || 0);

        # and if this cell has a rowspan, block off that many rows below it
        if ( $cell->{rowspan} > 1 ) {
          $rowspans[$c] = $cell->{rowspan} - 1;
        }
        $c++;
      }
    } #$cell
  $r++;
  } #$row

  $c = 0;
  for my $c (0 .. scalar(@widths) - 1) {
    $worksheet->set_column($c, $c, $widths[$c]);
  }
  $workbook->close;

  http_header('Content-Length' => length($output));
  $m->print($output);
</%perl>
% } else {
% unless ( $suppress_header ) {
<& /elements/header.html, $title &>
% }
<% $head %>
% my $myself = $cgi->self_url;
% unless ( $suppress_header ) {
<P ALIGN="right" CLASS="noprint">
Download full reports<BR>
as <A HREF="<% "$myself;_type=xls" %>">Excel spreadsheet</A><BR>
</P>
% }
<style type="text/css">
.report * {
  background-color: #f8f8f8;
  border: 1px solid #999999;
  padding: 2px;
}
.report td {
  text-align: right;
}
.total { background-color: #f5f6be; }
.shaded { background-color: #c8c8c8; }
.totalshaded { background-color: #bfc094; }
</style>
<table class="<% $table_class %>" width="<% $table_width %>" cellspacing=0>
% foreach my $rowinfo (@rows) {
  <tr<% $rowinfo->{class} ? ' class="'.$rowinfo->{class}.'"' : ''%>>
%   my $thisrow = shift @cells;
%   foreach my $cell (@$thisrow) {
%     next if !ref($cell); # placeholders
%     my $td = $cell->{header} ? 'th' : 'td';
%     my $style = '';
%     $style .= " rowspan=".$cell->{rowspan}
%       if exists $cell->{rowspan} && $cell->{rowspan} > 1;
%     $style .= " colspan=".$cell->{colspan}
%       if exists $cell->{colspan} && $cell->{colspan} > 1;
%     $style .= ' class="' . $cell->{class} . '"' if $cell->{class};
% if ($cell->{bypass_filter}) {
      <<%$td%><%$style%>><% $cell->{value} %></<%$td%>>
% } else {
      <<%$td%><%$style%>><% $cell->{value} |h %></<%$td%>>
% }
%   }
  </tr>
% }
</table>
<% $foot %>
% unless ( $suppress_footer ) {
<& /elements/footer.html &>
% }
% }
<%args>
$title
@rows
@cells
$head => ''
$foot => ''
$table_width => "100%"
$table_class => "report"
$suppress_header => undef
$suppress_footer => undef
</%args>