dolibarr 21.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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
37function dolFICalculatePaymentReference($invoice_number, $statut, $use_rf)
38{
39 if ($statut >= 1) {
40 $invoice_number = preg_replace('/[^0-9]/', '', $invoice_number); // Keep only numbers
41 $invoice_number = ltrim($invoice_number, '0'); // Remove leading zeros
42 $coefficients = array(7, 3, 1); // Define the coefficient numbers (rotating 7, 3, 1)
43 $sum = 0;
44 $stlen_invoice_number = (int) strlen($invoice_number);
45 // Calculate the weighted sum from right to left
46 for ($i = 0; $i < $stlen_invoice_number; $i++) {
47 $sum += (int) $invoice_number[$stlen_invoice_number - $i - 1] * $coefficients[$i % 3];
48 }
49 $check_digit = (10 - ($sum % 10)) % 10; // Calculate the check digit
50 $bank_reference_fi = $invoice_number . $check_digit; // Concatenate the reference number and the check digit
51 if ($use_rf) { // SEPA RF creditor reference
52 $reference_with_suffix = $bank_reference_fi . "271500"; // Append "271500" to the end of the payment reference number
53 $remainder = (int) bcmod($reference_with_suffix, '97'); // Calculate the remainder when dividing by 97
54 $check_digit = 98 - $remainder; // Subtract the remainder from 98
55 if ($check_digit < 10) { // If below 10 -> add leading zero
56 $check_digit = '0' . $check_digit;
57 }
58 $bank_reference = "RF" . $check_digit . $bank_reference_fi; // Add "RF" and the check digit in front of the payment reference number
59 } else { // FI payment reference number
60 $bank_reference = $bank_reference_fi;
61 }
62 } else {
63 $bank_reference = '';
64 }
65 return wordwrap($bank_reference, 4, ' ', true); // Split the string into chunks of 4 characters to improve readability
66}
67
77function dolFIGenerateInvoiceBarcodeData($recipient_account, $amount, $bank_reference, $due_date)
78{
79 $barcodeData = '0';
80 if ($amount >= 0 && !empty($bank_reference)) {
81 if (substr($bank_reference, 0, 2) === "RF") {
82 $recipient_account = preg_replace('/[^0-9]/', '', $recipient_account); // Remove non-numeric characters from account number
83 $recipient_account = str_pad($recipient_account, 16, '0', STR_PAD_LEFT); // Add leading zeros if necessary
84 $referencetobarcode = preg_replace('/[^0-9]/', '', $bank_reference); // Remove non-numeric characters (spaces)
85 $referencetobarcode = substr($referencetobarcode, 0, 2) . str_pad(substr($referencetobarcode, 2), 21, '0', STR_PAD_LEFT);
86 $euros = floor(floatval($amount)); // Separate euros and cents
87 $cents = round((floatval($amount) - $euros) * 100);
88 $due_date = date('ymd', (int) $due_date); // Format the due date to YYMMDD
89 $barcodeData = '5'; // Version number // Construct the string
90 $barcodeData .= $recipient_account; // Recipient's account number (IBAN)
91 $barcodeData .= sprintf('%06d', (int) $euros); // Euros
92 $barcodeData .= sprintf('%02d', (int) $cents); // Cents
93 $barcodeData .= $referencetobarcode; // Reference number
94 $barcodeData .= (int) $due_date; // Due date YYMMDD
95 } elseif (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 $euros = floor(floatval($amount)); // Separate euros and cents
100 $cents = round((floatval($amount) - $euros) * 100);
101 $due_date = date('ymd', (int) $due_date); // Format the due date to YYMMDD
102 $barcodeData = '4'; // Version number // Construct the string
103 $barcodeData .= $recipient_account; // Recipient's account number (IBAN)
104 $barcodeData .= sprintf('%06d', (int) $euros); // Euros
105 $barcodeData .= sprintf('%02d', (int) $cents); // Cents
106 $barcodeData .= '000'; // Reserved
107 $barcodeData .= str_pad($referencetobarcode, 20, '0', STR_PAD_LEFT); // Reference number
108 $barcodeData .= (int) $due_date; // Due date YYMMDD
109 }
110 } else {
111 $barcodeData = '';
112 }
113 return $barcodeData;
114}
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.