Swiss QR-Bill: QrBill.pm: Ändere Parameter für Imager::QRCode Instanziierung
[kivitendo-erp.git] / SL / Helper / QrBill.pm
1 package SL::Helper::QrBill;
2
3 use strict;
4 use warnings;
5
6 use Imager;
7 use Imager::QRCode;
8
9 my %Config = (
10   cross_file => 'image/CH-Kreuz_7mm.png',
11   out_file   => 'out.png',
12 );
13
14 sub new {
15   my $class = shift;
16
17   my $self = bless {}, $class;
18
19   $self->_init_check(@_);
20   $self->_init(@_);
21
22   return $self;
23 }
24
25 sub _init {
26   my $self = shift;
27   my ($biller_information, $biller_data, $payment_information, $invoice_recipient_data, $ref_nr_data) = @_;
28
29   $self->{data}{header} = [
30     'SPC',  # QRType
31     '0200', # Version
32      1,     # Coding Type
33   ];
34   $self->{data}{biller_information} = [
35     $biller_information->{iban},
36   ];
37   $self->{data}{biller_data} = [
38     $biller_data->{address_type},
39     $biller_data->{company},
40     $biller_data->{address_row1},
41     $biller_data->{address_row2},
42     '',
43     '',
44     $biller_data->{countrycode},
45   ];
46   $self->{data}{payment_information} = [
47     $payment_information->{amount},
48     $payment_information->{currency},
49   ];
50   $self->{data}{invoice_recipient_data} = [
51     $invoice_recipient_data->{address_type},
52     $invoice_recipient_data->{name},
53     $invoice_recipient_data->{address_row1},
54     $invoice_recipient_data->{address_row2},
55     '',
56     '',
57     $invoice_recipient_data->{countrycode},
58   ];
59   $self->{data}{ref_nr_data} = [
60     $ref_nr_data->{type},
61     $ref_nr_data->{ref_number},
62   ];
63   $self->{data}{additional_information} = [
64     '',
65     'EPD', # End Payment Data
66   ];
67 }
68
69 sub _init_check {
70   my $self = shift;
71   my ($biller_information, $biller_data, $payment_information, $invoice_recipient_data, $ref_nr_data) = @_;
72
73   my $check_re = sub {
74     my ($href, $elem, $regex) = @_;
75     defined $href->{$elem} && $href->{$elem} =~ $regex
76       or die "parameter '$elem' not valid", "\n";
77   };
78
79   $check_re->($biller_information, 'iban', qr{^(?:CH|LI)[0-9a-zA-Z]{19}$});
80
81   $check_re->($biller_data, 'address_type', qr{^[KS]$});
82   $check_re->($biller_data, 'company', qr{^.{1,70}$});
83   $check_re->($biller_data, 'address_row1', qr{^.{0,70}$});
84   $check_re->($biller_data, 'address_row2', qr{^.{0,70}$});
85   $check_re->($biller_data, 'countrycode', qr{^[A-Z]{2}$});
86
87   $check_re->($payment_information, 'amount', qr{^(?:(?:0|[1-9][0-9]{0,8})\.[0-9]{2})?$});
88   $check_re->($payment_information, 'currency', qr{^(?:CHF|EUR)$});
89
90   $check_re->($invoice_recipient_data, 'address_type', qr{^[KS]$});
91   $check_re->($invoice_recipient_data, 'name', qr{^.{1,70}$});
92   $check_re->($invoice_recipient_data, 'address_row1', qr{^.{0,70}$});
93   $check_re->($invoice_recipient_data, 'address_row2', qr{^.{0,70}$});
94   $check_re->($invoice_recipient_data, 'countrycode', qr{^[A-Z]{2}$});
95
96   my %ref_nr_regexes = (
97     QRR => qr{^\d{27}$},
98     NON => qr{^$},
99   );
100   $check_re->($ref_nr_data, 'type', qr{^(?:QRR|SCOR|NON)$});
101   $check_re->($ref_nr_data, 'ref_number', $ref_nr_regexes{$ref_nr_data->{type}});
102 }
103
104 sub generate {
105   my $self = shift;
106   my $out_file = defined $_[0] ? $_[0] : $Config{out_file};
107
108   $self->{qrcode} = $self->_qrcode();
109   $self->{cross}  = $self->_cross();
110   $self->{img}    = $self->_plot();
111
112   $self->_paste();
113   $self->_write($out_file);
114 }
115
116 sub _qrcode {
117   my $self = shift;
118
119   return Imager::QRCode->new(
120     size   =>  4,
121     margin =>  0,
122     level  => 'M',
123   );
124 }
125
126 sub _cross {
127   my $self = shift;
128
129   my $cross = Imager->new();
130   $cross->read(file => $Config{cross_file}) or die $cross->errstr, "\n";
131
132   return $cross->scale(xpixels => 27, ypixels => 27, qtype => 'mixing');
133 }
134
135 sub _plot {
136   my $self = shift;
137
138   my @data = (
139     @{$self->{data}{header}},
140     @{$self->{data}{biller_information}},
141     @{$self->{data}{biller_data}},
142     ('') x 7, # for future use
143     @{$self->{data}{payment_information}},
144     @{$self->{data}{invoice_recipient_data}},
145     @{$self->{data}{ref_nr_data}},
146     @{$self->{data}{additional_information}},
147   );
148
149   foreach (@data) {
150     s/[\r\n]/ /g;
151     s/ {2,}/ /g;
152     s/^\s+//;
153     s/\s+$//;
154   }
155                   # CR + LF
156   my $text = join "\015\012", @data;
157
158   return $self->{qrcode}->plot($text);
159 }
160
161 sub _paste {
162   my $self = shift;
163
164   $self->{img}->paste(
165     src  => $self->{cross},
166     left => ($self->{img}->getwidth  / 2) - ($self->{cross}->getwidth  / 2),
167     top  => ($self->{img}->getheight / 2) - ($self->{cross}->getheight / 2),
168   );
169 }
170
171 sub _write {
172   my $self = shift;
173   my ($out_file) = @_;
174
175   $self->{img}->write(file => $out_file) or die $self->{img}->errstr, "\n";
176 }
177
178 1;
179
180 __END__
181
182 =encoding utf-8
183
184 =head1 NAME
185
186 SL::Helper::QrBill - Helper methods for generating Swiss QR-Code
187
188 =head1 SYNOPSIS
189
190      use SL::Helper::QrBill;
191
192      eval {
193        my $qr_image = SL::Helper::QrBill->new(
194          \%biller_information,
195          \%biller_data,
196          \%payment_information,
197          \%invoice_recipient_data,
198          \%ref_nr_data,
199        );
200        $qr_image->generate($outfile);
201      } or do {
202        local $_ = $@; chomp; my $error = $_;
203        $::form->error($::locale->text('QR-Image generation failed: ' . $error));
204      };
205
206 =head1 DESCRIPTION
207
208 This module generates the Swiss QR-Code with data provided to the constructor.
209
210 =head1 METHODS
211
212 =head2 C<new>
213
214 Creates a new object. Expects five references to hashes as arguments.
215
216 The hashes are structured as follows:
217
218 =over 4
219
220 =item C<%biller_information>
221
222 Fields: iban.
223
224 =over 4
225
226 =item C<iban>
227
228 Fixed length; 21 alphanumerical characters, only IBANs with CH- or LI-
229 country code.
230
231 =back
232
233 =item C<%biller_data>
234
235 Fields: address_type, company, address_row1, address_row2 and countrycode.
236
237 =over 4
238
239 =item C<address_type>
240
241 Fixed length; 1-digit, alphanumerical. 'K' implemented only.
242
243 =item C<company>
244
245 Maximum of 70 characters, name (surname allowable) or company.
246
247 =item C<address_row1>
248
249 Maximum of 70 characters, street/nr.
250
251 =item C<address_row2>
252
253 Maximum of 70 characters, postal code/place.
254
255 =item C<countrycode>
256
257 2-digit country code according to ISO 3166-1.
258
259 =back
260
261 =item C<%payment_information>
262
263 Fields: amount and currency.
264
265 =over 4
266
267 =item C<amount>
268
269 Decimal, no leading zeroes, maximum of 12 digits (inclusive decimal
270 separator and places). Only dot as decimal separator is permitted.
271
272 =item C<currency>
273
274 CHF/EUR.
275
276 =back
277
278 =item C<%invoice_recipient_data>
279
280 Fields: address_type, name, address_row1, address_row2 and countrycode.
281
282 =over 4
283
284 =item C<address_type>
285
286 Fixed length; 1-digit, alphanumerical. 'K' implemented only.
287
288 =item C<name>
289
290 Maximum of 70 characters, name (surname allowable) or company.
291
292 =item C<address_row1>
293
294 Maximum of 70 characters, street/nr.
295
296 =item C<address_row2>
297
298 Maximum of 70 characters, postal code/place.
299
300 =item C<countrycode>
301
302 2-digit country code according to ISO 3166-1.
303
304 =back
305
306 =item C<%ref_nr_data>
307
308 Fields: type and ref_number.
309
310 =over 4
311
312 =item C<type>
313
314 Maximum of 4 characters, alphanumerical. QRR/SCOR/NON.
315
316 =item C<ref_number>
317
318 QR-Reference: 27 characters, numerical; without Reference: empty.
319
320 =back
321
322 =back
323
324 =head2 C<generate>
325
326 Generates the QR-Code image. Accepts filename of image as argument.
327 Defaults to C<out.png>.
328
329 =head1 AUTHOR
330
331 Steven Schubiger E<lt>stsc@refcnt.orgE<gt>
332
333 =cut