8 use List::Util qw(first sum);
9 use List::MoreUtils qw(any);
10 use POSIX qw(strftime);
14 use SL::SEPA::XML::Transaction;
31 $self->{transactions} = [];
32 $self->{src_charset} = 'UTF-8';
35 map { $self->{$_} = $params{$_} if (exists $params{$_}) } qw(src_charset company creditor_id message_id grouped collection);
37 $self->{iconv} = SL::Iconv->new($self->{src_charset}, "UTF-8") || croak "Unsupported source charset $self->{src_charset}.";
39 my $missing_parameter = first { !$self->{$_} } qw(company message_id);
40 croak "Missing parameter: $missing_parameter" if ($missing_parameter);
41 croak "Missing parameter: creditor_id" if !$self->{creditor_id} && $self->{collection};
43 map { $self->{$_} = $self->_replace_special_chars($self->{iconv}->convert($self->{$_})) } qw(company message_id creditor_id);
49 foreach my $transaction (@_) {
50 croak "Expecting hash reference." if (ref $transaction ne 'HASH');
51 push @{ $self->{transactions} }, SL::SEPA::XML::Transaction->new(%{ $transaction }, 'sepa' => $self);
57 sub _replace_special_chars {
72 map { $text =~ s/$_/$special_chars{$_}/g; } keys %special_chars;
81 return sprintf '%.02f', $amount;
84 sub _group_transactions {
92 foreach my $transaction (@{ $self->{transactions} }) {
93 my $key = $self->{grouped} ? join("\t", map { $transaction->get($_) } qw(src_bic src_iban execution_date)) : 'all';
94 $grouped->{groups}->{$key} ||= {
96 'transactions' => [ ],
99 push @{ $grouped->{groups}->{$key}->{transactions} }, $transaction;
101 $grouped->{groups}->{$key}->{sum_amount} += $transaction->{amount};
102 $grouped->{sum_amount} += $transaction->{amount};
108 sub _restricted_identification_sepa1 {
109 my ($self, $string) = @_;
111 $string =~ s/[^A-Za-z0-9\+\?\/\-:\(\)\.,' ]//g;
112 return substr $string, 0, 35;
115 sub _restricted_identification_sepa2 {
116 my ($self, $string) = @_;
118 $string =~ s/[^A-Za-z0-9\+\?\/\-:\(\)\.,']//g;
119 return substr $string, 0, 35;
125 croak "No transactions added yet." if (!@{ $self->{transactions} });
129 my $xml = XML::Writer->new(OUTPUT => \$output,
132 ENCODING => 'utf-8');
135 my $time_zone = strftime "%z", @now;
136 my $now_str = strftime('%Y-%m-%dT%H:%M:%S', @now) . substr($time_zone, 0, 3) . ':' . substr($time_zone, 3, 2);
138 my $is_coll = $self->{collection};
139 my $cd_src = $is_coll ? 'Cdtr' : 'Dbtr';
140 my $cd_dst = $is_coll ? 'Dbtr' : 'Cdtr';
141 my $pain_id = $is_coll ? 'pain.008.002.02' : 'pain.001.002.03';
142 my $pain_elmt = $is_coll ? 'CstmrDrctDbtInitn' : 'CstmrCdtTrfInitn';
143 my @pii_base = (strftime('PII%Y%m%d%H%M%S', @now), rand(1000000000));
145 my $grouped_transactions = $self->_group_transactions();
149 $xml->startTag('Document',
150 'xmlns' => "urn:iso:std:iso:20022:tech:xsd:${pain_id}",
151 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
152 'xsi:schemaLocation' => "urn:iso:std:iso:20022:tech:xsd:${pain_id} ${pain_id}.xsd");
154 $xml->startTag($pain_elmt);
156 $xml->startTag('GrpHdr');
157 $xml->dataElement('MsgId', encode('UTF-8', $self->_restricted_identification_sepa1($self->{message_id})));
158 $xml->dataElement('CreDtTm', $now_str);
159 $xml->dataElement('NbOfTxs', scalar @{ $self->{transactions} });
160 $xml->dataElement('CtrlSum', $self->_format_amount($grouped_transactions->{sum_amount}));
162 $xml->startTag('InitgPty');
163 $xml->dataElement('Nm', encode('UTF-8', substr($self->{company}, 0, 70)));
164 $xml->endTag('InitgPty');
166 $xml->endTag('GrpHdr');
168 foreach my $key (keys %{ $grouped_transactions->{groups} }) {
169 my $transaction_group = $grouped_transactions->{groups}->{$key};
170 my $master_transaction = $transaction_group->{transactions}->[0];
172 $xml->startTag('PmtInf');
173 $xml->dataElement('PmtInfId', sprintf('%s%010d', @pii_base));
175 $xml->dataElement('PmtMtd', $is_coll ? 'DD' : 'TRF');
176 $xml->dataElement('NbOfTxs', scalar @{ $transaction_group->{transactions} });
177 $xml->dataElement('CtrlSum', $self->_format_amount($transaction_group->{sum_amount}));
179 $xml->startTag('PmtTpInf');
180 $xml->startTag('SvcLvl');
181 $xml->dataElement('Cd', 'SEPA');
182 $xml->endTag('SvcLvl');
185 $xml->startTag('LclInstrm');
186 $xml->dataElement('Cd', 'CORE');
187 $xml->endTag('LclInstrm');
188 $xml->dataElement('SeqTp', 'OOFF');
190 $xml->endTag('PmtTpInf');
192 $xml->dataElement($is_coll ? 'ReqdColltnDt' : 'ReqdExctnDt', $master_transaction->get('execution_date'));
193 $xml->startTag($cd_src);
194 $xml->dataElement('Nm', encode('UTF-8', substr($self->{company}, 0, 70)));
195 $xml->endTag($cd_src);
197 $xml->startTag($cd_src . 'Acct');
198 $xml->startTag('Id');
199 $xml->dataElement('IBAN', $master_transaction->get('src_iban', 34));
201 $xml->endTag($cd_src . 'Acct');
203 $xml->startTag($cd_src . 'Agt');
204 $xml->startTag('FinInstnId');
205 $xml->dataElement('BIC', $master_transaction->get('src_bic', 20));
206 $xml->endTag('FinInstnId');
207 $xml->endTag($cd_src . 'Agt');
209 $xml->dataElement('ChrgBr', 'SLEV');
211 foreach my $transaction (@{ $transaction_group->{transactions} }) {
212 $xml->startTag($is_coll ? 'DrctDbtTxInf' : 'CdtTrfTxInf');
214 $xml->startTag('PmtId');
215 $xml->dataElement('EndToEndId', $self->_restricted_identification_sepa1($transaction->get('end_to_end_id')));
216 $xml->endTag('PmtId');
219 $xml->startTag('InstdAmt', 'Ccy' => 'EUR');
220 $xml->characters($self->_format_amount($transaction->{amount}));
221 $xml->endTag('InstdAmt');
223 $xml->startTag('DrctDbtTx');
225 $xml->startTag('MndtRltdInf');
226 $xml->dataElement('MndtId', $self->_restricted_identification_sepa2($transaction->get('reference')));
227 $xml->dataElement('DtOfSgntr', $transaction->get('reference_date', 2010-12-02));
228 $xml->endTag('MndtRltdInf');
230 $xml->startTag('CdtrSchmeId');
231 $xml->startTag('Id');
232 $xml->startTag('PrvtId');
233 $xml->startTag('Othr');
234 $xml->dataElement('Id', encode('UTF-8', substr($self->{creditor_id}, 0, 35)));
235 $xml->startTag('SchmeNm');
236 $xml->dataElement('Prtry', 'SEPA');
237 $xml->endTag('SchmeNm');
238 $xml->endTag('Othr');
239 $xml->endTag('PrvtId');
241 $xml->endTag('CdtrSchmeId');
243 $xml->endTag('DrctDbtTx');
246 $xml->startTag('Amt');
247 $xml->startTag('InstdAmt', 'Ccy' => 'EUR');
248 $xml->characters($self->_format_amount($transaction->{amount}));
249 $xml->endTag('InstdAmt');
253 $xml->startTag("${cd_dst}Agt");
254 $xml->startTag('FinInstnId');
255 $xml->dataElement('BIC', $transaction->get('dst_bic', 20));
256 $xml->endTag('FinInstnId');
257 $xml->endTag("${cd_dst}Agt");
259 $xml->startTag("${cd_dst}");
260 $xml->dataElement('Nm', $transaction->get('company', 70));
261 $xml->endTag("${cd_dst}");
263 $xml->startTag("${cd_dst}Acct");
264 $xml->startTag('Id');
265 $xml->dataElement('IBAN', $transaction->get('dst_iban', 34));
267 $xml->endTag("${cd_dst}Acct");
269 $xml->startTag('RmtInf');
270 $xml->dataElement('Ustrd', $transaction->get('reference', 140));
271 $xml->endTag('RmtInf');
273 $xml->endTag($is_coll ? 'DrctDbtTxInf' : 'CdtTrfTxInf');
276 $xml->endTag('PmtInf');
279 $xml->endTag($pain_elmt);
280 $xml->endTag('Document');