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
|
2015-05-07 12:47:13 +00:00
|
|
|
module_function
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def generate
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# get config
|
|
|
|
config = Setting.get('ticket_number_date')
|
2012-11-27 23:01:33 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
t = Time.zone.now
|
|
|
|
date = t.strftime('%Y-%m-%d')
|
2012-11-27 23:01:33 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# read counter
|
|
|
|
counter_increment = nil
|
|
|
|
Ticket::Counter.transaction do
|
2016-03-16 09:36:51 +00:00
|
|
|
counter = Ticket::Counter.where(generator: 'Date').lock(true).first
|
2015-04-27 23:19:26 +00:00
|
|
|
if !counter
|
2016-03-16 09:36:51 +00:00
|
|
|
counter = Ticket::Counter.new(generator: 'Date', content: '0')
|
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
|
|
|
# increase counter
|
|
|
|
counter_increment, date_file = counter.content.to_s.split(';')
|
2016-01-15 17:22:57 +00:00
|
|
|
counter_increment = if date_file == date
|
|
|
|
counter_increment.to_i + 1
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# store new counter value
|
|
|
|
counter.content = counter_increment.to_s + ';' + date
|
|
|
|
counter.save
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
system_id = Setting.get('system_id') || ''
|
2015-05-04 14:37:53 +00:00
|
|
|
number = t.strftime('%Y%m%d') + system_id.to_s + format('%04d', counter_increment)
|
2012-11-27 23:01:33 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# calculate a checksum
|
|
|
|
# The algorithm to calculate the checksum is derived from the one
|
|
|
|
# Deutsche Bundesbahn (german railway company) uses for calculation
|
|
|
|
# of the check digit of their vehikel numbering.
|
|
|
|
# The checksum is calculated by alternately multiplying the digits
|
|
|
|
# with 1 and 2 and adding the resulsts from left to right of the
|
|
|
|
# vehikel number. The modulus to 10 of this sum is substracted from
|
|
|
|
# 10. See: http://www.pruefziffernberechnung.de/F/Fahrzeugnummer.shtml
|
|
|
|
# (german)
|
2016-11-16 06:58:42 +00:00
|
|
|
|
|
|
|
# fix for https://github.com/zammad/zammad/issues/413 - can be removed later
|
|
|
|
if config.class == FalseClass || config.class == TrueClass
|
|
|
|
config = {
|
|
|
|
checksum: config
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
if config[:checksum]
|
|
|
|
chksum = 0
|
|
|
|
mult = 1
|
|
|
|
(1..number.length).each do |i|
|
|
|
|
digit = number.to_s[i, 1]
|
2016-03-16 09:36:51 +00:00
|
|
|
chksum = chksum + (mult * digit.to_i)
|
2015-04-27 23:19:26 +00:00
|
|
|
mult += 1
|
|
|
|
if mult == 3
|
|
|
|
mult = 1
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
end
|
2015-04-27 23:19:26 +00:00
|
|
|
chksum %= 10
|
|
|
|
chksum = 10 - chksum
|
|
|
|
if chksum == 10
|
|
|
|
chksum = 1
|
|
|
|
end
|
|
|
|
number += chksum.to_s
|
|
|
|
end
|
|
|
|
number
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
def 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
|
|
|
|
2015-04-27 23:19:26 +00:00
|
|
|
# probe format
|
2016-03-16 09:36:51 +00:00
|
|
|
string.scan(/#{ticket_hook}#{ticket_hook_divider}(#{system_id}\d{2,48})/i) {
|
|
|
|
ticket = Ticket.find_by(number: $1)
|
|
|
|
break if ticket
|
|
|
|
}
|
|
|
|
if !ticket
|
|
|
|
string.scan(/#{ticket_hook}\s{0,2}(#{system_id}\d{2,48})/i) {
|
|
|
|
ticket = Ticket.find_by(number: $1)
|
|
|
|
break if ticket
|
|
|
|
}
|
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
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|