date.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. module Ticket::Number::Date
  3. extend self
  4. def generate
  5. # get config
  6. config = Setting.get('ticket_number_date')
  7. t = Time.now
  8. date = t.strftime("%Y-%m-%d")
  9. # read counter
  10. counter_increment = nil
  11. Ticket::Counter.transaction do
  12. counter = Ticket::Counter.where( :generator => 'Date' ).lock(true).first
  13. if !counter
  14. counter = Ticket::Counter.new( :generator => 'Date', :content => '0' )
  15. end
  16. # increase counter
  17. counter_increment, date_file = counter.content.to_s.split(';')
  18. if date_file == date
  19. counter_increment = counter_increment.to_i + 1
  20. else
  21. counter_increment = 1
  22. end
  23. # store new counter value
  24. counter.content = counter_increment.to_s + ';' + date
  25. counter.save
  26. end
  27. system_id = Setting.get('system_id') || ''
  28. number = t.strftime("%Y%m%d") + system_id.to_s + sprintf( "%04d", counter_increment)
  29. # calculate a checksum
  30. # The algorithm to calculate the checksum is derived from the one
  31. # Deutsche Bundesbahn (german railway company) uses for calculation
  32. # of the check digit of their vehikel numbering.
  33. # The checksum is calculated by alternately multiplying the digits
  34. # with 1 and 2 and adding the resulsts from left to right of the
  35. # vehikel number. The modulus to 10 of this sum is substracted from
  36. # 10. See: http://www.pruefziffernberechnung.de/F/Fahrzeugnummer.shtml
  37. # (german)
  38. if config[:checksum]
  39. chksum = 0
  40. mult = 1
  41. (1..number.length).each do |i|
  42. digit = number.to_s[i, 1]
  43. chksum = chksum + ( mult * digit.to_i )
  44. mult += 1
  45. if mult == 3
  46. mult = 1
  47. end
  48. end
  49. chksum %= 10
  50. chksum = 10 - chksum
  51. if chksum == 10
  52. chksum = 1
  53. end
  54. number += chksum.to_s
  55. end
  56. return number
  57. end
  58. def check(string)
  59. # get config
  60. system_id = Setting.get('system_id') || ''
  61. ticket_hook = Setting.get('ticket_hook')
  62. ticket_hook_divider = Setting.get('ticket_hook_divider') || ''
  63. ticket = nil
  64. # probe format
  65. if string =~ /#{ticket_hook}#{ticket_hook_divider}(#{system_id}\d{2,50})/i then
  66. ticket = Ticket.where( :number => $1 ).first
  67. elsif string =~ /#{ticket_hook}\s{0,2}(#{system_id}\d{2,50})/i then
  68. ticket = Ticket.where( :number => $1 ).first
  69. end
  70. return ticket
  71. end
  72. end