2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
module Ticket::Number::Increment
|
2019-04-18 03:09:14 +00:00
|
|
|
extend Ticket::Number::Base
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2019-04-18 03:09:14 +00:00
|
|
|
def self.generate
|
|
|
|
counter = Ticket::Counter.create_with(content: '0')
|
|
|
|
.find_or_create_by(generator: 'Increment')
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2019-04-18 03:09:14 +00:00
|
|
|
counter.with_lock do
|
|
|
|
counter.update(content: counter.content.to_i.next.to_s)
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# fill up number counter
|
2019-04-18 03:09:14 +00:00
|
|
|
head = (Setting.get('system_id') || 1).to_s
|
|
|
|
tail = counter.content
|
2015-05-07 09:59:10 +00:00
|
|
|
|
2019-04-18 03:09:14 +00:00
|
|
|
padding_length = (config[:min_size] || 4).to_i - head.length - tail.length
|
|
|
|
padding_length -= 1 if config[:checksum]
|
|
|
|
padding_length = 0 if padding_length.negative?
|
|
|
|
padding_length = 99 if padding_length > 99
|
2015-05-07 09:59:10 +00:00
|
|
|
|
2019-04-18 03:09:14 +00:00
|
|
|
number = head + ('0' * padding_length) + tail
|
|
|
|
number += checksum(number) if config[:checksum]
|
2012-11-27 23:01:33 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
number
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2019-04-18 03:09:14 +00:00
|
|
|
def self.check(string)
|
2016-12-02 11:24:00 +00:00
|
|
|
return if string.blank?
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get config
|
2019-04-18 03:09:14 +00:00
|
|
|
system_id = Setting.get('ticket_number_ignore_system_id') ? '' : Setting.get('system_id').to_s
|
2015-04-27 23:19:26 +00:00
|
|
|
ticket_hook = Setting.get('ticket_hook')
|
2019-04-18 03:09:14 +00:00
|
|
|
ticket_hook_divider = Setting.get('ticket_hook_divider').to_s
|
2015-04-27 23:19:26 +00:00
|
|
|
ticket = nil
|
2012-11-27 23:01:33 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# probe format
|
2018-06-22 10:38:56 +00:00
|
|
|
# NOTE: we use `(?<=\W|^)` at the start of the regular expressions below
|
|
|
|
# because `\b` fails when ticket_hook begins with a non-word character (like '#')
|
2021-05-12 11:37:44 +00:00
|
|
|
string.scan(%r{(?<=\W|^)#{Regexp.quote(ticket_hook)}#{Regexp.quote(ticket_hook_divider)}(#{system_id}\d{2,48})\b}i) do
|
2016-03-16 09:36:51 +00:00
|
|
|
ticket = Ticket.find_by(number: $1)
|
|
|
|
break if ticket
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2019-04-18 03:09:14 +00:00
|
|
|
|
2016-03-16 09:36:51 +00:00
|
|
|
if !ticket
|
2021-05-12 11:37:44 +00:00
|
|
|
string.scan(%r{(?<=\W|^)#{Regexp.quote(ticket_hook)}\s{0,2}(#{system_id}\d{2,48})\b}i) do
|
2016-03-16 09:36:51 +00:00
|
|
|
ticket = Ticket.find_by(number: $1)
|
|
|
|
break if ticket
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2019-04-18 03:09:14 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
ticket
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2019-04-18 03:09:14 +00:00
|
|
|
|
|
|
|
def self.config
|
|
|
|
Setting.get('ticket_number_increment')
|
|
|
|
end
|
|
|
|
private_class_method :config
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|