2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2015-04-27 23:19:26 +00:00
|
|
|
module Ticket::Number::Date
|
2019-04-18 03:09:14 +00:00
|
|
|
extend Ticket::Number::Base
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2019-04-18 03:09:14 +00:00
|
|
|
def self.generate
|
|
|
|
date = Time.zone.now.strftime('%F')
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2019-04-18 03:09:14 +00:00
|
|
|
counter = Ticket::Counter.create_with(content: '0')
|
|
|
|
.find_or_create_by(generator: 'Date')
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2019-04-18 03:09:14 +00:00
|
|
|
counter.with_lock do
|
|
|
|
counter_increment = if counter.content.end_with?(date)
|
|
|
|
counter.content.split(';').first.to_i.next
|
2016-01-15 17:22:57 +00:00
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2019-04-18 03:09:14 +00:00
|
|
|
counter.update(content: "#{counter_increment};#{date}")
|
2015-04-27 23:19:26 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2020-02-18 19:51:31 +00:00
|
|
|
number = date.delete('-') + Setting.get('system_id').to_s + format('%<counter>04d', counter: counter.content.split(';').first)
|
2019-04-18 03:09:14 +00:00
|
|
|
number += checksum(number) if config[:checksum]
|
2016-11-16 06:58:42 +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?
|
2015-04-27 21:27:51 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get config
|
|
|
|
system_id = Setting.get('system_id') || ''
|
|
|
|
ticket_hook = Setting.get('ticket_hook')
|
|
|
|
ticket_hook_divider = Setting.get('ticket_hook_divider') || ''
|
|
|
|
ticket = nil
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2018-01-16 05:56:40 +00:00
|
|
|
if Setting.get('ticket_number_ignore_system_id') == true
|
|
|
|
system_id = ''
|
|
|
|
end
|
|
|
|
|
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)}(\d{4,10}#{system_id}\d{2,40})\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
|
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}(\d{4,10}#{system_id}\d{2,40})\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
|
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
|
|
|
|
config = Setting.get('ticket_number_date')
|
|
|
|
return config if !config.in?([true, false])
|
|
|
|
|
|
|
|
# fix for https://github.com/zammad/zammad/issues/413 - can be removed later
|
|
|
|
{ checksum: config }
|
|
|
|
end
|
|
|
|
private_class_method :config
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|