base.rb 846 B

12345678910111213141516171819202122232425262728
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Ticket::Number::Base
  3. extend self
  4. private
  5. # The algorithm to calculate the checksum is derived from the one
  6. # Deutsche Bundesbahn (german railway company) uses for calculation
  7. # of the check digit of their vehikel numbering.
  8. # The checksum is calculated by alternately multiplying the digits
  9. # with 1 and 2 and adding the resulsts from left to right of the
  10. # vehikel number. The modulus to 10 of this sum is substracted from
  11. # 10. See: http://www.pruefziffernberechnung.de/F/Fahrzeugnummer.shtml
  12. # (german)
  13. def checksum(number)
  14. chksum = 0
  15. mult = 1
  16. number.to_s.chars.map(&:to_i).each do |digit|
  17. chksum += digit * mult
  18. mult = (mult % 3) + 1
  19. end
  20. chksum = 10 - (chksum % 10)
  21. chksum.to_s[0]
  22. end
  23. end