1 package SL::Helper::QrBill;
10 cross_file => 'image/CH-Kreuz_7mm.png',
11 out_file => 'out.png',
17 my $self = bless {}, $class;
19 $self->_init_check(@_);
27 my ($biller_information, $biller_data, $payment_information, $invoice_recipient_data, $ref_nr_data) = @_;
29 $self->{data}{header} = [
34 $self->{data}{biller_information} = [
35 $biller_information->{iban},
37 $self->{data}{biller_data} = [
38 $biller_data->{address_type},
39 $biller_data->{company},
40 $biller_data->{address_row1},
41 $biller_data->{address_row2},
44 $biller_data->{countrycode},
46 $self->{data}{payment_information} = [
47 $payment_information->{amount},
48 $payment_information->{currency},
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},
57 $invoice_recipient_data->{countrycode},
59 $self->{data}{ref_nr_data} = [
61 $ref_nr_data->{ref_number},
63 $self->{data}{additional_information} = [
65 'EPD', # End Payment Data
71 my ($biller_information, $biller_data, $payment_information, $invoice_recipient_data, $ref_nr_data) = @_;
74 my ($href, $elem, $regex) = @_;
75 defined $href->{$elem} && $href->{$elem} =~ $regex
76 or die "parameter '$elem' not valid", "\n";
79 $check_re->($biller_information, 'iban', qr{^(?:CH|LI)[0-9a-zA-Z]{19}$});
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}$});
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)$});
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}$});
96 my %ref_nr_regexes = (
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}});
106 my $out_file = defined $_[0] ? $_[0] : $Config{out_file};
108 $self->{qrcode} = $self->_qrcode();
109 $self->{cross} = $self->_cross();
110 $self->{img} = $self->_plot();
113 $self->_write($out_file);
119 return Imager::QRCode->new(
129 my $cross = Imager->new();
130 $cross->read(file => $Config{cross_file}) or die $cross->errstr, "\n";
132 return $cross->scale(xpixels => 35, ypixels => 35, qtype => 'mixing');
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}},
156 my $text = join "\015\012", @data;
158 return $self->{qrcode}->plot($text);
165 src => $self->{cross},
166 left => ($self->{img}->getwidth / 2) - ($self->{cross}->getwidth / 2),
167 top => ($self->{img}->getheight / 2) - ($self->{cross}->getheight / 2),
175 $self->{img}->write(file => $out_file) or die $self->{img}->errstr, "\n";
186 SL::Helper::QrBill - Helper methods for generating Swiss QR-Code
190 use SL::Helper::QrBill;
193 my $qr_image = SL::Helper::QrBill->new(
194 \%biller_information,
196 \%payment_information,
197 \%invoice_recipient_data,
200 $qr_image->generate($outfile);
202 local $_ = $@; chomp; my $error = $_;
203 $::form->error($::locale->text('QR-Image generation failed: ' . $error));
208 This module generates the Swiss QR-Code with data provided to the constructor.
214 Creates a new object. Expects five references to hashes as arguments.
216 The hashes are structured as follows:
220 =item C<%biller_information>
228 Fixed length; 21 alphanumerical characters, only IBANs with CH- or LI-
233 =item C<%biller_data>
235 Fields: address_type, company, address_row1, address_row2 and countrycode.
239 =item C<address_type>
241 Fixed length; 1-digit, alphanumerical. 'K' implemented only.
245 Maximum of 70 characters, name (surname allowable) or company.
247 =item C<address_row1>
249 Maximum of 70 characters, street/nr.
251 =item C<address_row2>
253 Maximum of 70 characters, postal code/place.
257 2-digit country code according to ISO 3166-1.
261 =item C<%payment_information>
263 Fields: amount and currency.
269 Decimal, no leading zeroes, maximum of 12 digits (inclusive decimal
270 separator and places). Only dot as decimal separator is permitted.
278 =item C<%invoice_recipient_data>
280 Fields: address_type, name, address_row1, address_row2 and countrycode.
284 =item C<address_type>
286 Fixed length; 1-digit, alphanumerical. 'K' implemented only.
290 Maximum of 70 characters, name (surname allowable) or company.
292 =item C<address_row1>
294 Maximum of 70 characters, street/nr.
296 =item C<address_row2>
298 Maximum of 70 characters, postal code/place.
302 2-digit country code according to ISO 3166-1.
306 =item C<%ref_nr_data>
308 Fields: type and ref_number.
314 Maximum of 4 characters, alphanumerical. QRR/SCOR/NON.
318 QR-Reference: 27 characters, numerical; without Reference: empty.
326 Generates the QR-Code image. Accepts filename of image as argument.
327 Defaults to C<out.png>.
331 Steven Schubiger E<lt>stsc@refcnt.orgE<gt>