dolibarr 25.0.0-alpha
functions_creditorref.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Nick Fragoulis
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
36{
37 $out = '';
38 // Only A-Z and 0-9 take part in the computation, so drop anything else first.
39 // The remaining string is pure ASCII, one byte per character.
40 $str = preg_replace('/[^A-Z0-9]/', '', dol_strtoupper((string) $str));
41 $len = dol_strlen($str);
42 for ($i = 0; $i < $len; $i++) {
43 $c = $str[$i];
44 if (ctype_digit($c)) {
45 $out .= $c;
46 } else {
47 $out .= (string) (ord($c) - ord('A') + 10);
48 }
49 }
50 return $out;
51}
52
63{
64 $check = 98 - dolCreditorRefMod97(dolCreditorRefAlphaToDigits($raw.'RF00'));
65 return ($check < 10) ? '0'.$check : (string) $check;
66}
67
79function dolCreditorRefMod97($digits)
80{
81 $rest = 0;
82 $len = strlen($digits);
83 for ($i = 0; $i < $len; $i += 7) {
84 $rest = (int) ($rest.substr($digits, $i, 7)) % 97;
85 }
86 return $rest;
87}
88
95function dolPayRefStrip($ref)
96{
97 return str_replace(' ', '', (string) $ref);
98}
99
106function dolPayRefFormat($ref)
107{
108 return wordwrap((string) $ref, 4, ' ', true);
109}
110
121function dolCreditorRefGenerate($payload, $statut)
122{
123 if ($statut < 1) {
124 return '';
125 }
126
127 $raw = dol_strtoupper(preg_replace('/[^A-Za-z0-9]/', '', (string) $payload));
128
129 if ($raw === '' || dol_strlen($raw) > 21) {
130 if (dol_strlen($raw) > 21) {
131 dol_syslog('dolCreditorRefGenerate: payload "'.$raw.'" exceeds 21 chars', LOG_WARNING);
132 }
133 return '';
134 }
135
136 return 'RF'.dolCreditorRefCheckDigits($raw).$raw;
137}
138
149function dolCreditorRefGenerateFromParts($parts, $statut)
150{
151 if ($statut < 1) {
152 return '';
153 }
154
155 $combined = '';
156 foreach ($parts as $part) {
157 $combined .= preg_replace('/[^A-Za-z0-9]/', '', (string) $part);
158 }
159 $combined = dol_strtoupper($combined);
160
161 if (dol_strlen($combined) > 21) {
162 dol_syslog('dolCreditorRefGenerateFromParts: payload truncated to 21 chars', LOG_WARNING);
163 $combined = dol_substr($combined, 0, 21);
164 }
165
166 return dolCreditorRefGenerate($combined, $statut);
167}
168
176{
177 $ref = dolPayRefStrip($ref);
178
179 if (dol_substr($ref, 0, 2) !== 'RF' || dol_strlen($ref) < 5 || dol_strlen($ref) > 25) {
180 return false;
181 }
182 if (!ctype_alnum(dol_substr($ref, 4))) {
183 return false;
184 }
185
186 return dol_substr($ref, 2, 2) === dolCreditorRefCheckDigits(dol_substr($ref, 4));
187}
188
201{
202 $ref = trim((string) $ref);
203
204 if ($ref === '') {
205 return '';
206 }
207 if (strpos($ref, '+++') === 0) {
208 return 'BBA';
209 }
210 if (dol_substr(dolPayRefStrip($ref), 0, 2) === 'RF') {
211 return 'SCOR';
212 }
213 if (preg_match('/^\d{27}$/', dolPayRefStrip($ref))) {
214 return 'QRR';
215 }
216 if (ctype_digit(dolPayRefStrip($ref))) {
217 return 'FI';
218 }
219
220 return '';
221}
$c
Definition line.php:353
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
dol_strtoupper($string, $encoding="UTF-8")
Convert a string to upper.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dolCreditorRefIsValid($ref)
Check that a string is a valid ISO 11649 Structured Creditor Reference.
dolCreditorRefGenerateFromParts($parts, $statut)
Generate an ISO 11649 reference from an ordered list of parts.
dolPayRefStrip($ref)
Remove formatting spaces from a payment reference (machine form).
dolCreditorRefAlphaToDigits($str)
Convert an alphanumeric string to its all-digit form for ISO 7064 mod-97.
dolPayRefDetectScheme($ref)
Detect which structured payment reference scheme a stored value belongs to.
dolCreditorRefMod97($digits)
Remainder of a long decimal string divided by 97.
dolCreditorRefGenerate($payload, $statut)
Generate an ISO 11649 Structured Creditor Reference from a payload.
dolPayRefFormat($ref)
Format a payment reference into groups of 4 characters (human form).
dolCreditorRefCheckDigits($raw)
Calculate the two ISO 11649 (ISO 7064 mod-97-10) check digits.