number.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Ticket::Number
  3. include ApplicationLib
  4. =begin
  5. generate new ticket number
  6. result = Ticket::Number.generate
  7. returns
  8. result = "1234556" # new ticket number
  9. =end
  10. def self.generate
  11. # generate number
  12. 49_999.times do
  13. number = adapter.generate
  14. ticket = Ticket.find_by(number: number)
  15. return number if !ticket
  16. end
  17. raise "Can't generate new ticket number!"
  18. end
  19. =begin
  20. check if string contrains a valid ticket number
  21. result = Ticket::Number.check('some string [Ticket#123456]')
  22. returns
  23. result = ticket # Ticket model of ticket with matching ticket number
  24. =end
  25. def self.check(string)
  26. adapter.check(string)
  27. end
  28. def self.adapter
  29. # load backend based on config
  30. adapter_name = Setting.get('ticket_number')
  31. if !adapter_name
  32. raise 'Missing ticket_number setting option'
  33. end
  34. adapter = load_adapter(adapter_name)
  35. if !adapter
  36. raise "Can't load ticket_number adapter '#{adapter_name}'"
  37. end
  38. adapter
  39. end
  40. end