]> wagnertech.de Git - mfinanz.git/blob - SL/DB/Helper/LegacyPrinting.pm
date error in mapping
[mfinanz.git] / SL / DB / Helper / LegacyPrinting.pm
1 package SL::DB::Helper::LegacyPrinting;
2
3 use strict;
4
5 use parent qw(Exporter);
6 our @EXPORT = qw(map_keys_to_arrays format_as_number);
7
8 sub map_keys_to_arrays {
9   my ($items, $keys, $template_arrays) = @_;
10
11   for my $key (@$keys) {
12     # handle nested keys
13     if ($key =~ /\./) {
14       my ($k1, $k2) = split /\./, $key;
15       $template_arrays->{$k1} = [ map { { $k2 => $_->{$k1}->{$k2} } } @$items ];
16     } else {
17       $template_arrays->{$key} = [ map {
18         if (ref $_ eq 'HASH') {
19           $_->{$key}
20         } else {
21           $_->can($key) ? $_->$key
22                         : $_->{$key}
23         }
24       } @$items ];
25     }
26   }
27 }
28
29 sub format_as_number {
30   my ($keys, $template_arrays) = @_;
31
32   for my $key (@$keys) {
33     $template_arrays->{$key} = [ map {
34       $::form->format_amount(\%::myconfig, $_, 2, 0),
35     } @{ $template_arrays->{$key} } ];
36   }
37 }
38
39 1;
40
41 __END__
42
43 =pod
44
45 =encoding utf8
46
47 =head1 NAME
48
49 SL::DB::Helper::LegacyPrinting - Helper functions to support printing using the built-it template parser
50
51 =head1 DESCRIPTION
52
53 The new Reclamation controller, and possibly other future controllers, only support printing using Template Toolkit (TT)
54 for parsing the templates. For OpenDocument templates however, template toolkit cannot be used.
55
56 Template Toolkit (TT) can access Rose DB objects directly, which is a feature not available in the built-in parser.
57 For positions in a loop, such as positions in a table, the built-in parser expects the data in a specific format.
58 Therefore, we need to prepare the data accordingly before it can be processed by the built-in parser.
59
60 In the past this was done in the respective modules, e.g. SL/OE.pm. The idea would be to extract the data from the Rose
61 DB objects. That should hopefully result in less and simpler code.
62
63 =head1 FUNCTIONS
64
65 =head2 C<map_keys_to_arrays ($items, $keys, $template_arrays)>
66
67 Extracts the given keys from the given list of Rose DB objects and adds them to the given hash reference,
68 in the format that the built in template parser expects.
69
70 The expected format looks like the following, e.g.:
71
72   # 'qty_as_number' => [
73   #                      '1.00',
74   #                      '3.00'
75   #                    ],
76   # 'part' => [
77   #             {
78   #               'partnumber' => '000013'
79   #             },
80   #             {
81   #               'partnumber' => '000004'
82   #             }
83   #           ],
84   # 'position' => [
85   #                 1,
86   #                 2
87   #               ],
88   # 'unit' => [
89   #             'Stck',
90   #             'Stck'
91   #           ],
92
93 =over 4
94
95 =item C<$items>
96
97 A reference to a list of Rose DB objects from which the keys should be extracted.
98
99 =item C<$keys>
100
101 A reference to a list of keys that should be extracted from the Rose DB object.
102 Nested keys should be denoted by a dot. E.g.:
103
104   qw( qty_as_number part.partnumber position unit )
105
106 =item C<$template_arrays>
107
108 A reference to a hash to which the extracted keys should be added.
109
110 =back
111
112 =head2 C<format_as_number ($keys, $template_arrays)>
113
114 Formats the given keys in the given hash reference as numbers.
115
116 =head1 AUTHOR
117
118 Cem Aydin E<lt>cem.aydin@revamp-it.chE<gt>