dolibarr 25.0.0-alpha
functions_fi.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Mikko Virtanen
4 * Copyright (C) 2026 Nick Fragoulis
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
38function dolFICalculatePaymentReference($invoice_number, $statut, $use_rf)
39{
40 if ($statut >= 1) {
41 $invoice_number = preg_replace('/[^0-9]/', '', $invoice_number); // Keep only numbers
42 $invoice_number = ltrim($invoice_number, '0'); // Remove leading zeros
43 // A finnish reference is 4 to 20 characters, check digit included, so the base
44 // is 3 to 19 digits. Leading zeros do not change the weighted sum, so padding
45 // a short base is safe. A longer base is cut to keep the reference valid, and
46 // to keep the RF form within the 25 characters of ISO 11649.
47 if (dol_strlen($invoice_number) < 3) {
48 $invoice_number = str_pad($invoice_number, 3, '0', STR_PAD_LEFT);
49 } elseif (dol_strlen($invoice_number) > 19) {
50 dol_syslog('dolFICalculatePaymentReference: base "'.$invoice_number.'" is longer than 19 digits, cut to keep the reference valid', LOG_WARNING);
51 $invoice_number = dol_substr($invoice_number, 0, 19);
52 }
53 $coefficients = array(7, 3, 1); // Define the coefficient numbers (rotating 7, 3, 1)
54 $sum = 0;
55 $stlen_invoice_number = (int) strlen($invoice_number);
56 // Calculate the weighted sum from right to left
57 for ($i = 0; $i < $stlen_invoice_number; $i++) {
58 $sum += (int) $invoice_number[$stlen_invoice_number - $i - 1] * $coefficients[$i % 3];
59 }
60 $check_digit = (10 - ($sum % 10)) % 10; // Calculate the check digit
61 $bank_reference_fi = $invoice_number . $check_digit; // Concatenate the reference number and the check digit
62 if ($use_rf) { // SEPA RF creditor reference
63 $reference_with_suffix = $bank_reference_fi . "271500"; // Append "271500" to the end of the payment reference number
64 // bcmath is not a Dolibarr requirement, so the remainder is computed
65 // without it. See dolCreditorRefMod97().
66 include_once DOL_DOCUMENT_ROOT.'/core/lib/functions_creditorref.lib.php';
67 $remainder = dolCreditorRefMod97($reference_with_suffix); // Calculate the remainder when dividing by 97
68 $check_digit = 98 - $remainder; // Subtract the remainder from 98
69 if ($check_digit < 10) { // If below 10 -> add leading zero
70 $check_digit = '0' . $check_digit;
71 }
72 $bank_reference = "RF" . $check_digit . $bank_reference_fi; // Add "RF" and the check digit in front of the payment reference number
73 } else { // FI payment reference number
74 $bank_reference = $bank_reference_fi;
75 }
76 } else {
77 $bank_reference = '';
78 }
79 return wordwrap($bank_reference, 4, ' ', true); // Split the string into chunks of 4 characters to improve readability
80}
81
91function dolFIGenerateInvoiceBarcodeData($recipient_account, $amount, $bank_reference, $due_date)
92{
93 $barcodeData = '0';
94 if ($amount >= 0 && !empty($bank_reference)) {
95 if (substr($bank_reference, 0, 2) === "RF") {
96 $recipient_account = preg_replace('/[^0-9]/', '', $recipient_account); // Remove non-numeric characters from account number
97 $recipient_account = str_pad($recipient_account, 16, '0', STR_PAD_LEFT); // Add leading zeros if necessary
98 $referencetobarcode = preg_replace('/[^0-9]/', '', $bank_reference); // Remove non-numeric characters (spaces)
99 $referencetobarcode = substr($referencetobarcode, 0, 2) . str_pad(substr($referencetobarcode, 2), 21, '0', STR_PAD_LEFT);
100 $euros = floor((float) $amount); // Separate euros and cents
101 $cents = round(((float) $amount - $euros) * 100);
102 $due_date = date('ymd', (int) $due_date); // Format the due date to YYMMDD
103 $barcodeData = '5'; // Version number // Construct the string
104 $barcodeData .= $recipient_account; // Recipient's account number (IBAN)
105 $barcodeData .= sprintf('%06d', (int) $euros); // Euros
106 $barcodeData .= sprintf('%02d', (int) $cents); // Cents
107 $barcodeData .= $referencetobarcode; // Reference number
108 $barcodeData .= $due_date; // Due date YYMMDD, kept as a string so a leading zero survives
109 } elseif (substr($bank_reference, 0, 2) !== "RF") {
110 $recipient_account = preg_replace('/[^0-9]/', '', $recipient_account); // Remove non-numeric characters from account number
111 $recipient_account = str_pad($recipient_account, 16, '0', STR_PAD_LEFT); // Add leading zeros if necessary
112 $referencetobarcode = preg_replace('/[^0-9]/', '', $bank_reference); // Remove non-numeric characters (spaces)
113 $euros = floor((float) $amount); // Separate euros and cents
114 $cents = round(((float) $amount - $euros) * 100);
115 $due_date = date('ymd', (int) $due_date); // Format the due date to YYMMDD
116 $barcodeData = '4'; // Version number // Construct the string
117 $barcodeData .= $recipient_account; // Recipient's account number (IBAN)
118 $barcodeData .= sprintf('%06d', (int) $euros); // Euros
119 $barcodeData .= sprintf('%02d', (int) $cents); // Cents
120 $barcodeData .= '000'; // Reserved
121 $barcodeData .= str_pad($referencetobarcode, 20, '0', STR_PAD_LEFT); // Reference number
122 $barcodeData .= $due_date; // Due date YYMMDD, kept as a string so a leading zero survives
123 }
124 } else {
125 $barcodeData = '';
126 }
127 return $barcodeData;
128}
129
143{
144 if (!is_object($object) || empty($object->payment_reference) || empty($object->date_lim_reglement)) {
145 return '';
146 }
147
148 include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
149
150 $idofbankaccount = $object->fk_account;
151 if (empty($idofbankaccount)) {
152 $idofbankaccount = $object->fk_bank; // for backward compatibility
153 }
154 if (empty($idofbankaccount)) {
155 $idofbankaccount = getDolGlobalInt('FACTURE_RIB_NUMBER');
156 }
157 if (empty($idofbankaccount)) {
158 return '';
159 }
160
161 $bankaccount = new Account($object->db);
162 if ($bankaccount->fetch($idofbankaccount) <= 0 || empty($bankaccount->iban)) {
163 return '';
164 }
165
166 // The account field of the barcode is 16 digits wide, so the barcode is only
167 // defined for a finnish IBAN. A foreign one would overflow that field and the
168 // barcode would then scan as a different account.
169 $iban = dol_strtoupper(str_replace(' ', '', $bankaccount->iban));
170 if (strpos($iban, 'FI') !== 0) {
171 dol_syslog('dolFIGetInvoiceBarcodeData: barcode needs a finnish IBAN, got '.$iban, LOG_WARNING);
172 return '';
173 }
174
175 // The reference field of the barcode is numeric, an alphanumeric RF cannot fit
176 $reference = str_replace(' ', '', (string) $object->payment_reference);
177 if (!ctype_digit(dol_substr($reference, 0, 2) === 'RF' ? dol_substr($reference, 2) : $reference)) {
178 dol_syslog('dolFIGetInvoiceBarcodeData: barcode needs a numeric reference, got '.$reference, LOG_WARNING);
179 return '';
180 }
181
182 $amount = $object->getRemainToPay();
183 if ($amount < 0) {
184 return '';
185 }
186
187 $barcodedata = dolFIGenerateInvoiceBarcodeData($bankaccount->iban, (string) $amount, $object->payment_reference, $object->date_lim_reglement);
188
189 // A barcode of the wrong length would still be printed and scanned, so refuse it
190 if (dol_strlen($barcodedata) != 54) {
191 dol_syslog('dolFIGetInvoiceBarcodeData: barcode length is '.dol_strlen($barcodedata).', expected 54, discarded', LOG_WARNING);
192 return '';
193 }
194
195 return $barcodedata;
196}
197
207function dolFIFormatVirtualBarcode($barcodedata)
208{
209 if (empty($barcodedata)) {
210 return '';
211 }
212
213 return trim(chunk_split($barcodedata, 5, ' '));
214}
215
225function dolFIIsValidReference($reference)
226{
227 $reference = str_replace(' ', '', (string) $reference);
228
229 if (!ctype_digit($reference) || dol_strlen($reference) < 4 || dol_strlen($reference) > 20) {
230 return false;
231 }
232
233 $base = substr($reference, 0, -1);
234 $given = (int) substr($reference, -1);
235
236 $coefficients = array(7, 3, 1);
237 $sum = 0;
238 $i = 0;
239 for ($pos = dol_strlen($base) - 1; $pos >= 0; $pos--) {
240 $sum += ((int) $base[$pos]) * $coefficients[$i % 3];
241 $i++;
242 }
243
244 return $given === ((10 - ($sum % 10)) % 10);
245}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage bank accounts.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.
dolCreditorRefMod97($digits)
Remainder of a long decimal string divided by 97.
dolFIGenerateInvoiceBarcodeData($recipient_account, $amount, $bank_reference, $due_date)
Calculate payment Barcode data with FI/RF bank payment reference number.
dolFIIsValidReference($reference)
Check that a string is a valid finnish national reference.
dolFIGetInvoiceBarcodeData($object)
Build the finnish payment barcode data of an invoice.
dolFIFormatVirtualBarcode($barcodedata)
Format the finnish payment barcode for reading, as banks print it.
dolFICalculatePaymentReference($invoice_number, $statut, $use_rf)
Calculate Creditor Reference RF / FI Bank payment reference number.
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:492