increment.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Ticket::Number::Increment
  3. extend Ticket::Number::Base
  4. def self.generate
  5. counter = Ticket::Counter.create_with(content: '0')
  6. .find_or_create_by(generator: 'Increment')
  7. counter.with_lock do
  8. counter.update(content: counter.content.to_i.next.to_s)
  9. end
  10. # fill up number counter
  11. head = (Setting.get('system_id') || 1).to_s
  12. tail = counter.content
  13. padding_length = (config[:min_size] || 4).to_i - head.length - tail.length
  14. padding_length -= 1 if config[:checksum]
  15. padding_length = 0 if padding_length.negative?
  16. padding_length = 99 if padding_length > 99
  17. number = head + ('0' * padding_length) + tail
  18. number += checksum(number) if config[:checksum]
  19. number
  20. end
  21. def self.check(string)
  22. return if string.blank?
  23. # get config
  24. system_id = Setting.get('ticket_number_ignore_system_id') ? '' : Setting.get('system_id').to_s
  25. ticket_hook = Setting.get('ticket_hook')
  26. ticket_hook_divider = Setting.get('ticket_hook_divider').to_s
  27. ticket = nil
  28. # probe format
  29. # NOTE: we use `(?<=\W|^)` at the start of the regular expressions below
  30. # because `\b` fails when ticket_hook begins with a non-word character (like '#')
  31. string.scan(%r{(?<=\W|^)#{Regexp.quote(ticket_hook)}#{Regexp.quote(ticket_hook_divider)}(#{system_id}\d{2,48})\b}i) do
  32. ticket = Ticket.find_by(number: $1)
  33. break if ticket
  34. end
  35. if !ticket
  36. string.scan(%r{(?<=\W|^)#{Regexp.quote(ticket_hook)}\s{0,2}(#{system_id}\d{2,48})\b}i) do
  37. ticket = Ticket.find_by(number: $1)
  38. break if ticket
  39. end
  40. end
  41. ticket
  42. end
  43. def self.config
  44. Setting.get('ticket_number_increment')
  45. end
  46. private_class_method :config
  47. end