date.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Ticket::Number::Date
  3. extend Ticket::Number::Base
  4. def self.generate
  5. date = Time.zone.now.strftime('%F')
  6. counter = Ticket::Counter.create_with(content: '0')
  7. .find_or_create_by(generator: 'Date')
  8. counter.with_lock do
  9. counter_increment = if counter.content.end_with?(date)
  10. counter.content.split(';').first.to_i.next
  11. else
  12. 1
  13. end
  14. counter.update(content: "#{counter_increment};#{date}")
  15. end
  16. number = date.delete('-') + Setting.get('system_id').to_s + format('%<counter>04d', counter: counter.content.split(';').first)
  17. number += checksum(number) if config[:checksum]
  18. number
  19. end
  20. def self.check(string)
  21. return if string.blank?
  22. # get config
  23. system_id = Setting.get('system_id') || ''
  24. ticket_hook = Setting.get('ticket_hook')
  25. ticket_hook_divider = Setting.get('ticket_hook_divider') || ''
  26. ticket = nil
  27. if Setting.get('ticket_number_ignore_system_id') == true
  28. system_id = ''
  29. end
  30. # probe format
  31. # NOTE: we use `(?<=\W|^)` at the start of the regular expressions below
  32. # because `\b` fails when ticket_hook begins with a non-word character (like '#')
  33. string.scan(%r{(?<=\W|^)#{Regexp.quote(ticket_hook)}#{Regexp.quote(ticket_hook_divider)}(\d{4,10}#{system_id}\d{2,40})\b}i) do
  34. ticket = Ticket.find_by(number: $1)
  35. break if ticket
  36. end
  37. if !ticket
  38. string.scan(%r{(?<=\W|^)#{Regexp.quote(ticket_hook)}\s{0,2}(\d{4,10}#{system_id}\d{2,40})\b}i) do
  39. ticket = Ticket.find_by(number: $1)
  40. break if ticket
  41. end
  42. end
  43. ticket
  44. end
  45. def self.config
  46. config = Setting.get('ticket_number_date')
  47. return config if !config.in?([true, false])
  48. # fix for https://github.com/zammad/zammad/issues/413 - can be removed later
  49. { checksum: config }
  50. end
  51. private_class_method :config
  52. end