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 $check_re->($ref_nr_data, 'type', qr{^(?:QRR|SCOR|NON)$});
97 $check_re->($ref_nr_data, 'ref_number', qr{^\d{27}$});
102 my $out_file = defined $_[0] ? $_[0] : $Config{out_file};
104 $self->{qrcode} = $self->_qrcode();
105 $self->{cross} = $self->_cross();
106 $self->{img} = $self->_plot();
109 $self->_write($out_file);
115 return Imager::QRCode->new(
125 my $cross = Imager->new();
126 $cross->read(file => $Config{cross_file}) or die $cross->errstr, "\n";
128 return $cross->scale(xpixels => 27, ypixels => 27, qtype => 'mixing');
135 @{$self->{data}{header}},
136 @{$self->{data}{biller_information}},
137 @{$self->{data}{biller_data}},
138 ('') x 7, # for future use
139 @{$self->{data}{payment_information}},
140 @{$self->{data}{invoice_recipient_data}},
141 @{$self->{data}{ref_nr_data}},
142 @{$self->{data}{additional_information}},
152 my $text = join "\015\012", @data;
154 return $self->{qrcode}->plot($text);
161 src => $self->{cross},
162 left => ($self->{img}->getwidth / 2) - ($self->{cross}->getwidth / 2),
163 top => ($self->{img}->getheight / 2) - ($self->{cross}->getheight / 2),
171 $self->{img}->write(file => $out_file) or die $self->{img}->errstr, "\n";
182 SL::Helper::QrBill - Helper methods for generating Swiss QR-Code
186 use SL::Helper::QrBill;
189 my $qr_image = SL::Helper::QrBill->new(
190 \%biller_information,
192 \%payment_information,
193 \%invoice_recipient_data,
196 $qr_image->generate($outfile);
198 local $_ = $@; chomp; my $error = $_;
199 $::form->error($::locale->text('QR-Image generation failed: ' . $error));
204 This module generates the Swiss QR-Code with data provided to the constructor.
210 Creates a new object. Expects five references to hashes as arguments.
212 The hashes are structured as follows:
216 =item C<%biller_information>
224 Fixed length; 21 alphanumerical characters, only IBANs with CH- or LI-
229 =item C<%biller_data>
231 Fields: address_type, company, address_row1, address_row2 and countrycode.
235 =item C<address_type>
237 Fixed length; 1-digit, alphanumerical. 'K' implemented only.
241 Maximum of 70 characters, name (surname allowable) or company.
243 =item C<address_row1>
245 Maximum of 70 characters, street/nr.
247 =item C<address_row2>
249 Maximum of 70 characters, postal code/place.
253 2-digit country code according to ISO 3166-1.
257 =item C<%payment_information>
259 Fields: amount and currency.
265 Decimal, no leading zeroes, maximum of 12 digits (inclusive decimal
266 separator and places). Only dot as decimal separator is permitted.
274 =item C<%invoice_recipient_data>
276 Fields: address_type, name, address_row1, address_row2 and countrycode.
280 =item C<address_type>
282 Fixed length; 1-digit, alphanumerical. 'K' implemented only.
286 Maximum of 70 characters, name (surname allowable) or company.
288 =item C<address_row1>
290 Maximum of 70 characters, street/nr.
292 =item C<address_row2>
294 Maximum of 70 characters, postal code/place.
298 2-digit country code according to ISO 3166-1.
302 =item C<%ref_nr_data>
304 Fields: type and ref_number.
310 Maximum of 4 characters, alphanumerical. QRR/SCOR/NON.
314 27 characters, numerical. QR-Reference.
322 Generates the QR-Code image. Accepts filename of image as argument.
323 Defaults to C<out.png>.
327 Steven Schubiger E<lt>stsc@refcnt.orgE<gt>