dolibarr 20.0.0
functions_fi.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
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 * or see https://www.gnu.org/
17 */
18
36function dolFICalculatePaymentReference($invoice_number, $statut, $use_rf)
37{
38 if ($statut >= 1) {
39 $invoice_number = preg_replace('/[^0-9]/', '', $invoice_number); // Keep only numbers
40 $invoice_number = ltrim($invoice_number, '0'); //Remove any leading zero or zeros
41 $invoice_number = strrev($invoice_number); // Reverse the reference number
42 $coefficients = array(7, 3, 1, 7, 3); // Define the coefficient numbers
43 $sum = 0;
44 $stlen_invoice_number = (int) strlen($invoice_number);
45 for ($i = 0; $i < $stlen_invoice_number; $i++) { // Calculate the sum using coefficients
46 $sum += (int) $invoice_number[$i] * $coefficients[$i % 5];
47 }
48 $check_digit = (10 - ($sum % 10)) % 10; // Calculate the check digit
49 $bank_reference_fi = strrev($invoice_number) . $check_digit; // Concatenate the Reversed reference number and the check digit
50 if ($use_rf) { // SEPA RF creditor reference
51 $reference_with_suffix = $bank_reference_fi . "271500"; // Append "271500" to the end of the payment reference number
52 $remainder = (int) bcmod($reference_with_suffix, '97'); // Calculate the remainder when dividing by 97
53 $check_digit = 98 - $remainder; // Subtract the remainder from 98
54 if ($check_digit < 10) { // If below 10 -> add leading zero
55 $check_digit = '0' . $check_digit;
56 }
57 $bank_reference = "RF" . $check_digit . $bank_reference_fi; // Add "RF" and the check digit in front of the payment reference number
58 } else { // FI payment reference number
59 $bank_reference = $bank_reference_fi;
60 }
61 } else {
62 $bank_reference = '';
63 }
64 return wordwrap($bank_reference, 4, ' ', true); // Split the string into chunks of 4 characters to improve readability
65}
66
76function dolFIGenerateInvoiceBarcodeData($recipient_account, $amount, $bank_reference, $due_date)
77{
78 $barcodeData = '0';
79 if ($amount >= 0 && !empty($bank_reference)) {
80 if (substr($bank_reference, 0, 2) === "RF") {
81 $recipient_account = preg_replace('/[^0-9]/', '', $recipient_account); // Remove non-numeric characters from account number
82 $recipient_account = str_pad($recipient_account, 16, '0', STR_PAD_LEFT); // Add leading zeros if necessary
83 $referencetobarcode = preg_replace('/[^0-9]/', '', $bank_reference); // Remove non-numeric characters (spaces)
84 $referencetobarcode = substr($referencetobarcode, 0, 2) . str_pad(substr($referencetobarcode, 2), 21, '0', STR_PAD_LEFT);
85 $euros = floor(floatval($amount)); // Separate euros and cents
86 $cents = round((floatval($amount) - $euros) * 100);
87 $due_date = date('ymd', (int) $due_date); // Format the due date to YYMMDD
88 $barcodeData = '5'; // Version number // Construct the string
89 $barcodeData .= $recipient_account; // Recipient's account number (IBAN)
90 $barcodeData .= sprintf('%06d', (int) $euros); // Euros
91 $barcodeData .= sprintf('%02d', (int) $cents); // Cents
92 $barcodeData .= $referencetobarcode; // Reference number
93 $barcodeData .= (int) $due_date; // Due date YYMMDD
94 } elseif (substr($bank_reference, 0, 2) !== "RF") {
95 $recipient_account = preg_replace('/[^0-9]/', '', $recipient_account); // Remove non-numeric characters from account number
96 $recipient_account = str_pad($recipient_account, 16, '0', STR_PAD_LEFT); // Add leading zeros if necessary
97 $referencetobarcode = preg_replace('/[^0-9]/', '', $bank_reference); // Remove non-numeric characters (spaces)
98 $euros = floor(floatval($amount)); // Separate euros and cents
99 $cents = round((floatval($amount) - $euros) * 100);
100 $due_date = date('ymd', (int) $due_date); // Format the due date to YYMMDD
101 $barcodeData = '4'; // Version number // Construct the string
102 $barcodeData .= $recipient_account; // Recipient's account number (IBAN)
103 $barcodeData .= sprintf('%06d', (int) $euros); // Euros
104 $barcodeData .= sprintf('%02d', (int) $cents); // Cents
105 $barcodeData .= '000'; // Reserved
106 $barcodeData .= str_pad($referencetobarcode, 20, '0', STR_PAD_LEFT); // Reference number
107 $barcodeData .= (int) $due_date; // Due date YYMMDD
108 }
109 } else {
110 $barcodeData = '';
111 }
112 return $barcodeData;
113}
dolFIGenerateInvoiceBarcodeData($recipient_account, $amount, $bank_reference, $due_date)
Calculate payment Barcode data with FI/RF bank payment reference number.
dolFICalculatePaymentReference($invoice_number, $statut, $use_rf)
Calculate Creditor Reference RF / FI Bank payment reference number.