Created
July 25, 2023 17:59
-
-
Save timint/59a8b9f3dcb096941e2e9a4d34d7f633 to your computer and use it in GitHub Desktop.
Function to generate OCR line on invoices payable via Bankgirot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Author: T. Almroth | |
Documentation: https://www.bankgirot.se/globalassets/dokument/anvandarmanualer/bankgiroinbetalningar_anvandarmanual_sv.pdf | |
Note: Use font OCR B. | |
Example of use: | |
echo generate_bankgirot_ocr('123456789', '100.00', '0000-0000'); | |
*/ | |
function modulus10($number) { | |
$number = preg_replace('#[^0-9]#', '', $number); | |
$digits = str_split(strrev($number)); | |
$stack = 0; | |
foreach ($digits as $key => $value) { | |
if ($key % 2 == 0) { | |
$value = array_sum(str_split($value * 2)); | |
} | |
$stack += $value; | |
} | |
$stack %= 10; | |
if ($stack != 0) { | |
$stack -= 10; | |
$stack = abs($stack); | |
} | |
return strval($stack); | |
} | |
function generate_bankgirot_ocr(string $ocr_number, string $amount, string $account) { | |
return strtr('#.{ocr}.#{amount}.{cents}...{checksum}.>.................{account}#{code}#....', [ | |
'.' => ' ', | |
'{ocr}' => str_pad($ocr_number, 25, ' ', STR_PAD_LEFT), | |
'{account}' => str_pad(str_replace('-', '', $account_number), 8, ' ', STR_PAD_LEFT), | |
'{amount}' => str_pad($whole = floor($amount), 8, ' ', STR_PAD_LEFT), | |
'{cents}' => str_pad(round(($amount - $whole) * 100), 2, '0', STR_PAD_LEFT), | |
'{checksum}' => modulus10($amount), | |
'{code}' => 41, | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment