FIX dolFICalculatePaymentReference for Finland in functions_fi.lib.php (#31281)

* Update functions_fi.lib.php

Fix calculation from some base numbers

* Update functions_fi.lib.php
This commit is contained in:
Mikko Virtanen 2024-10-07 15:52:12 +03:00 committed by GitHub
parent d8a980444a
commit 0811ff5f07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,6 @@
<?php
/* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2024 Mikko Virtanen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -37,16 +38,16 @@ function dolFICalculatePaymentReference($invoice_number, $statut, $use_rf)
{
if ($statut >= 1) {
$invoice_number = preg_replace('/[^0-9]/', '', $invoice_number); // Keep only numbers
$invoice_number = ltrim($invoice_number, '0'); //Remove any leading zero or zeros
$invoice_number = strrev($invoice_number); // Reverse the reference number
$coefficients = array(7, 3, 1, 7, 3); // Define the coefficient numbers
$invoice_number = ltrim($invoice_number, '0'); // Remove leading zeros
$coefficients = array(7, 3, 1); // Define the coefficient numbers (rotating 7, 3, 1)
$sum = 0;
$stlen_invoice_number = (int) strlen($invoice_number);
for ($i = 0; $i < $stlen_invoice_number; $i++) { // Calculate the sum using coefficients
$sum += (int) $invoice_number[$i] * $coefficients[$i % 5];
// Calculate the weighted sum from right to left
for ($i = 0; $i < $stlen_invoice_number; $i++) {
$sum += (int) $invoice_number[$stlen_invoice_number - $i - 1] * $coefficients[$i % 3];
}
$check_digit = (10 - ($sum % 10)) % 10; // Calculate the check digit
$bank_reference_fi = strrev($invoice_number) . $check_digit; // Concatenate the Reversed reference number and the check digit
$bank_reference_fi = $invoice_number . $check_digit; // Concatenate the reference number and the check digit
if ($use_rf) { // SEPA RF creditor reference
$reference_with_suffix = $bank_reference_fi . "271500"; // Append "271500" to the end of the payment reference number
$remainder = (int) bcmod($reference_with_suffix, '97'); // Calculate the remainder when dividing by 97