line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Business::ReportWriter; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
43
|
|
4
|
1
|
|
|
1
|
|
6
|
use POSIX qw(setlocale LC_NUMERIC); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub new { |
7
|
1
|
|
|
1
|
1
|
25
|
my ( $class, %parms ) = @_; |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
|
|
3
|
my $self = {}; |
10
|
1
|
|
|
|
|
4
|
$self = bless $self, $class; |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
|
|
4
|
return $self; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub process_report { |
16
|
0
|
|
|
0
|
1
|
|
my ( $self, $outfile, $report, $head, $list ) = @_; |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
|
my %report = %$report; |
19
|
0
|
|
|
|
|
|
my @list = @$list; |
20
|
0
|
0
|
|
|
|
|
push @list, {} if $#list < 1; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Setting up the report hash with user's configuration |
23
|
0
|
|
|
|
|
|
$self->init_report( $report{report} ); |
24
|
0
|
|
|
|
|
|
$self->init_page_header( $report{page}{header} ); |
25
|
0
|
|
|
|
|
|
$self->init_body( $report{body} ); |
26
|
0
|
|
|
|
|
|
$self->init_graphics( $report{graphics} ); |
27
|
0
|
|
|
|
|
|
$self->init_logos( $report{page}{logo} ); |
28
|
0
|
|
|
|
|
|
$self->init_breaks( $report{breaks} ); |
29
|
0
|
|
|
|
|
|
$self->init_fields( $report{fields} ); |
30
|
0
|
|
|
|
|
|
$self->print_list( \@list, \%$head ); |
31
|
|
|
|
|
|
|
|
32
|
0
|
0
|
|
|
|
|
return $outfile ? $self->print_doc($outfile) : $self->get_doc; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub init_report { |
36
|
0
|
|
|
0
|
0
|
|
my ( $self, $parms ) = @_; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
$self->{report} = $parms; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub init_page_header { |
42
|
0
|
|
|
0
|
0
|
|
my ( $self, $parms ) = @_; |
43
|
0
|
|
|
|
|
|
$self->{report}{page} = $parms; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub init_body { |
47
|
0
|
|
|
0
|
0
|
|
my ( $self, $parms ) = @_; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
$self->{report}{body} = $parms; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub init_graphics { |
53
|
0
|
|
|
0
|
0
|
|
my ( $self, $parms ) = @_; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
$self->{report}{graphics} = $parms; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub init_logos { |
59
|
0
|
|
|
0
|
0
|
|
my ( $self, $parms ) = @_; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
$self->{report}{logo} = $parms; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub init_fields { |
65
|
0
|
|
|
0
|
0
|
|
my ( $self, $parms ) = @_; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
$self->{report}{fields} = $parms; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub init_breaks { |
71
|
0
|
|
|
0
|
0
|
|
my ( $self, $parms ) = @_; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
$self->{report}{breaks} = $parms; |
74
|
0
|
|
|
|
|
|
my @breakorder; |
75
|
0
|
|
|
|
|
|
for ( keys %$parms ) { |
76
|
0
|
|
|
|
|
|
$breakorder[ $parms->{$_}{order} ] = $_; |
77
|
|
|
|
|
|
|
} |
78
|
0
|
|
|
|
|
|
$self->{report}{breaks}{_order} = [@breakorder]; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Report writing |
82
|
0
|
|
|
0
|
0
|
|
sub begin_break { |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
0
|
0
|
|
sub begin_line { |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
0
|
0
|
|
sub begin_field { |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub make_field_headers { |
92
|
0
|
|
|
0
|
0
|
|
my ( $self, $fh ) = @_; |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
if ( $fh->{show} ne 'off' ) { |
95
|
0
|
|
|
|
|
|
for ( @{ $self->{report}{fields} } ) { |
|
0
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
$self->out_field( $_->{text}, $_, $fh ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
0
|
0
|
|
sub out_field { |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub break_fields { |
105
|
0
|
|
|
0
|
0
|
|
my ( $self, $break_name, $tot ) = @_; |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
my $name = $tot->{name}; |
108
|
0
|
|
|
|
|
|
my $rec = $self->{totals}{$break_name}; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
$self->process_field( $tot, $rec ); |
111
|
0
|
|
|
|
|
|
$self->{totals}{$break_name}{$name} = 0; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub process_break { |
115
|
0
|
|
|
0
|
0
|
|
my ( $self, $break_name ) = @_; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my $p = $self->{pdf}; |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
my $break = $self->{report}{breaks}{$break_name}; |
120
|
0
|
|
|
|
|
|
$self->begin_line($break); |
121
|
|
|
|
|
|
|
|
122
|
0
|
0
|
|
|
|
|
if ( defined( $break->{total} ) ) { |
123
|
0
|
|
|
|
|
|
foreach my $tot ( @{ $break->{total} } ) { |
|
0
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
$self->break_fields( $break_name, $tot ); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub print_break_header { |
130
|
0
|
|
|
0
|
0
|
|
my ( $self, $rec, $break_name ) = @_; |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
my $p = $self->{pdf}; |
133
|
0
|
|
|
|
|
|
my $break = $self->{report}{breaks}{$break_name}; |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
$self->begin_break( $rec, $break ); |
136
|
0
|
|
|
|
|
|
$self->begin_line( $rec, $break->{header} ); |
137
|
0
|
|
|
|
|
|
for my $bh ( @{ $break->{header}{text} } ) { |
|
0
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
$self->process_field( $bh, $rec ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
$self->begin_line( $rec, $break->{header}{FieldHeaders} ); |
142
|
0
|
|
|
|
|
|
$self->make_field_headers( $break->{header}{FieldHeaders} ); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub print_break { |
146
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
for my $break_name ( @{ $self->{report}{breaks}{_order} } ) { |
|
0
|
|
|
|
|
|
|
149
|
0
|
|
0
|
|
|
|
my $self_break = $self->{breaks}{$break_name} || ''; |
150
|
0
|
0
|
|
|
|
|
if ( $self_break eq '_break' ) { |
151
|
0
|
|
|
|
|
|
$self->process_break($break_name); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub process_field { |
157
|
0
|
|
|
0
|
0
|
|
my ( $self, $fld, $rec ) = @_; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
return |
160
|
0
|
0
|
0
|
|
|
|
if ( defined( $fld->{depends} ) |
161
|
|
|
|
|
|
|
&& !eval( $self->make_text( $rec, $fld->{depends} ) ) ); |
162
|
0
|
0
|
|
|
|
|
my $text = |
163
|
|
|
|
|
|
|
defined( $fld->{function} ) |
164
|
|
|
|
|
|
|
? $self->make_func( $rec, $fld->{function} ) |
165
|
|
|
|
|
|
|
: $self->make_text( $rec, $fld->{text} ); |
166
|
0
|
0
|
|
|
|
|
$self->out_field( $text, $fld ) if $text; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub make_fieldtext { |
170
|
0
|
|
|
0
|
0
|
|
my ( $self, $rec, $text ) = @_; |
171
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
my @fields = ( $text =~ /(\w*)/g ); |
173
|
0
|
|
|
|
|
|
for my $field (@fields) { |
174
|
0
|
|
|
|
|
|
$text =~ s/$field/$rec->{$field}/eg; |
|
0
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
0
|
|
|
|
|
|
return $text; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub process_linefield { |
181
|
0
|
|
|
0
|
0
|
|
my ( $self, $fld, $rec ) = @_; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
return |
184
|
0
|
0
|
0
|
|
|
|
if ( defined( $fld->{depends} ) |
185
|
|
|
|
|
|
|
&& !eval( $self->make_text( $rec, $fld->{depends} ) ) ); |
186
|
|
|
|
|
|
|
|
187
|
0
|
|
|
|
|
|
$self->begin_field($fld); |
188
|
0
|
0
|
|
|
|
|
my $text = |
189
|
|
|
|
|
|
|
defined( $fld->{function} ) |
190
|
|
|
|
|
|
|
? $self->make_func( $rec, $fld->{function} ) |
191
|
|
|
|
|
|
|
: $self->make_fieldtext( $rec, $fld->{name} ); |
192
|
0
|
0
|
|
|
|
|
$self->out_field( $text, $fld ) if $text; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub out_textarray { |
196
|
0
|
|
|
0
|
0
|
|
my ( $self, $fld, $rec ) = @_; |
197
|
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
|
for ( @{ $rec->{ $fld->{name} } } ) { |
|
0
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
$self->begin_field($fld); |
200
|
0
|
0
|
|
|
|
|
$self->out_field( $_, $fld ) if $_; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub print_line { |
205
|
0
|
|
|
0
|
0
|
|
my ( $self, $rec ) = @_; |
206
|
|
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
$self->begin_line($rec); |
208
|
0
|
|
|
|
|
|
for ( @{ $self->{report}{fields} } ) { |
|
0
|
|
|
|
|
|
|
209
|
0
|
0
|
|
|
|
|
$self->out_textarray( $_, $rec ), next |
210
|
|
|
|
|
|
|
if lc( $_->{fieldtype} ) eq 'textarray'; |
211
|
0
|
|
|
|
|
|
$self->process_linefield( $_, $rec ); |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub sum_totals { |
216
|
0
|
|
|
0
|
0
|
|
my ($self, $rec) = @_; |
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
for my $break ( @{ $self->{report}{breaks}{_order} } ) { |
|
0
|
|
|
|
|
|
|
219
|
0
|
0
|
|
|
|
|
if ( defined( $self->{report}{breaks}{$break}{total} ) ) { |
220
|
0
|
|
|
|
|
|
foreach my $tot ( @{ $self->{report}{breaks}{$break}{total} } ) { |
|
0
|
|
|
|
|
|
|
221
|
0
|
|
|
|
|
|
my $name = $tot->{name}; |
222
|
0
|
|
|
|
|
|
$self->{totals}{$break}{$name} += $rec->{$name}; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
sub check_for_break { |
229
|
0
|
|
|
0
|
0
|
|
my ($self, $rec, $last) = @_; |
230
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
my $brk = ''; |
232
|
0
|
|
|
|
|
|
for my $break ( reverse @{ $self->{report}{breaks}{_order} } ) { |
|
0
|
|
|
|
|
|
|
233
|
0
|
|
0
|
|
|
|
my $self_break = $self->{breaks}{$break} || ''; |
234
|
0
|
|
0
|
|
|
|
my $rec_break = $rec->{$break} || ''; |
235
|
0
|
0
|
0
|
|
|
|
if ( ( $last && !( $break eq '_page' ) ) |
|
|
|
0
|
|
|
|
|
236
|
|
|
|
|
|
|
|| $self_break ne $rec_break ) |
237
|
|
|
|
|
|
|
{ |
238
|
0
|
|
|
|
|
|
$brk = '_break'; |
239
|
|
|
|
|
|
|
} |
240
|
0
|
0
|
|
|
|
|
$self->{breaks}{$break} = $brk if $brk; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
sub save_breaks { |
245
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
246
|
|
|
|
|
|
|
|
247
|
0
|
|
|
|
|
|
my ( $rec, $first ) = @_; |
248
|
0
|
|
|
|
|
|
for my $break ( reverse @{ $self->{report}{breaks}{_order} } ) { |
|
0
|
|
|
|
|
|
|
249
|
0
|
|
0
|
|
|
|
my $self_break = $self->{breaks}{$break} || ''; |
250
|
0
|
|
0
|
|
|
|
my $rec_break = $rec->{$break} || ''; |
251
|
0
|
0
|
0
|
|
|
|
$self->print_break_header( $rec, $break ) |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
252
|
|
|
|
|
|
|
if ( $first and $break ne '_total' and $break ne '_page' ) |
253
|
|
|
|
|
|
|
|| $self_break ne $rec_break; |
254
|
0
|
|
|
|
|
|
$self->{breaks}{$break} = $rec->{$break}; |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
sub process_totals { |
259
|
0
|
|
|
0
|
0
|
|
my ($self, $rec) = @_; |
260
|
|
|
|
|
|
|
|
261
|
0
|
|
|
|
|
|
my $first = ( !defined( $self->{started} ) ); |
262
|
0
|
|
|
|
|
|
$self->{started} = 1; |
263
|
0
|
|
|
|
|
|
my $last = ( ref $rec ne 'HASH' ); |
264
|
0
|
0
|
|
|
|
|
$self->print_totals($rec) if !$first; |
265
|
0
|
0
|
|
|
|
|
$self->save_breaks( $rec, $first ) if !$last; |
266
|
0
|
0
|
|
|
|
|
$self->sum_totals($rec) if !$last; |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
0
|
|
|
0
|
0
|
|
sub begin_list { |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
0
|
|
|
0
|
0
|
|
sub check_page { |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
sub print_list { |
276
|
0
|
|
|
0
|
0
|
|
my ( $self, $list, $page ) = @_; |
277
|
|
|
|
|
|
|
|
278
|
0
|
|
|
|
|
|
my @list = @$list; |
279
|
0
|
|
|
|
|
|
$self->{pageData} = $page; |
280
|
|
|
|
|
|
|
|
281
|
0
|
|
|
|
|
|
$self->begin_list; |
282
|
|
|
|
|
|
|
|
283
|
0
|
|
|
|
|
|
foreach my $rec (@list) { |
284
|
0
|
|
|
|
|
|
$self->check_page; |
285
|
0
|
|
|
|
|
|
$self->process_totals($rec); |
286
|
0
|
|
|
|
|
|
$self->print_line($rec); |
287
|
|
|
|
|
|
|
} |
288
|
0
|
|
|
|
|
|
$self->end_print(); |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
sub print_totals { |
292
|
0
|
|
|
0
|
0
|
|
my ( $self, $rec ) = @_; |
293
|
|
|
|
|
|
|
|
294
|
0
|
|
|
|
|
|
my $last = ( ref $rec ne 'HASH' ); |
295
|
0
|
|
|
|
|
|
$self->check_for_break( $rec, $last ); |
296
|
0
|
|
|
|
|
|
$self->print_break(); |
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
sub end_print { |
300
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
301
|
|
|
|
|
|
|
|
302
|
0
|
|
|
|
|
|
$self->process_totals(); |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
# Support |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
sub make_text { |
308
|
0
|
|
|
0
|
0
|
|
my ( $self, $rec, $text ) = @_; |
309
|
|
|
|
|
|
|
|
310
|
0
|
|
|
|
|
|
my @fields = ( $text =~ /\$(\w*)/g ); |
311
|
0
|
|
|
|
|
|
for my $field (@fields) { |
312
|
0
|
|
|
|
|
|
$text =~ s/\$$field/$rec->{$field}/eg; |
|
0
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
} |
314
|
0
|
|
|
|
|
|
return $text; |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
sub make_func { |
318
|
0
|
|
|
0
|
0
|
|
my ( $self, $rec, $func ) = @_; |
319
|
|
|
|
|
|
|
|
320
|
0
|
|
|
|
|
|
my @fields = ( $func =~ /\$(\w*)/g ); |
321
|
0
|
|
|
|
|
|
for my $field (@fields) { |
322
|
0
|
|
|
|
|
|
$func =~ s/\$$field/\$rec->{$field}/g; |
323
|
|
|
|
|
|
|
} |
324
|
|
|
|
|
|
|
|
325
|
0
|
|
|
|
|
|
my $text; |
326
|
0
|
|
|
|
|
|
setlocale( LC_NUMERIC, $self->{report}{locale} ); |
327
|
0
|
|
|
|
|
|
eval( '$text = ' . $func ); |
328
|
0
|
|
|
|
|
|
setlocale( LC_NUMERIC, "C" ); |
329
|
0
|
|
|
|
|
|
return $text; |
330
|
|
|
|
|
|
|
} |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
1; |
333
|
|
|
|
|
|
|
__END__ |