trabajo-afectivo/app/models/ticket/number/date.rb

65 lines
2 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module Ticket::Number::Date
extend Ticket::Number::Base
def self.generate
date = Time.zone.now.strftime('%F')
2012-04-10 14:06:46 +00:00
counter = Ticket::Counter.create_with(content: '0')
.find_or_create_by(generator: 'Date')
2012-04-10 14:06:46 +00:00
counter.with_lock do
counter_increment = if counter.content.end_with?(date)
counter.content.split(';').first.to_i.next
else
1
end
2012-04-10 14:06:46 +00:00
counter.update(content: "#{counter_increment};#{date}")
end
2012-04-10 14:06:46 +00:00
number = date.delete('-') + Setting.get('system_id').to_s + format('%<counter>04d', counter: counter.content.split(';').first)
number += checksum(number) if config[:checksum]
number
end
2012-04-10 14:06:46 +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
# 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
if Setting.get('ticket_number_ignore_system_id') == true
system_id = ''
end
# probe format
# 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 '#')
string.scan(%r{(?<=\W|^)#{Regexp.quote(ticket_hook)}#{Regexp.quote(ticket_hook_divider)}(\d{4,10}#{system_id}\d{2,40})\b}i) do
ticket = Ticket.find_by(number: $1)
break if ticket
end
if !ticket
string.scan(%r{(?<=\W|^)#{Regexp.quote(ticket_hook)}\s{0,2}(\d{4,10}#{system_id}\d{2,40})\b}i) do
ticket = Ticket.find_by(number: $1)
break if ticket
end
2012-04-10 14:06:46 +00:00
end
ticket
2012-04-10 14:06:46 +00:00
end
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
end