Moved to ticket counter log in database.

This commit is contained in:
Martin Edenhofer 2012-11-28 00:01:33 +01:00
parent 09864f330e
commit 4d72dce327
6 changed files with 60 additions and 73 deletions

View file

@ -386,7 +386,7 @@ class Ticket < ApplicationModel
private
def number_generate
Ticket.new.number_adapter = Setting.get('ticket_number')
(1..15_000).each do |i|
(1..25_000).each do |i|
number = @@number_adapter.number_generate_item()
ticket = Ticket.where( :number => number ).first
if ticket != nil

View file

@ -0,0 +1,2 @@
class Ticket::Counter < ApplicationModel
end

View file

@ -2,44 +2,37 @@ module Ticket::Number::Date
extend self
def number_generate_item
# get config
config = Setting.get('ticket_number_date')
t = Time.now
date = t.strftime("%Y-%m-%d")
# read counter
file_name = Rails.root.to_s + '/' + config[:file]
contents = ""
begin
file = File.open(file_name)
file.each {|line|
contents << line
}
file.close
rescue
contents = '0'
end
# increase counter
counter, date_file = contents.to_s.split(';')
counter_increment = nil
Ticket::Counter.transaction do
counter = Ticket::Counter.where( :generator => 'Date' ).lock(true).first
if !counter
counter = Ticket::Counter.new( :generator => 'Date', :content => '0' )
end
if date_file == date
counter = counter.to_i + 1
else
counter = 1
end
contents = counter.to_s + ';' + date
# increase counter
counter_increment, date_file = counter.content.to_s.split(';')
if date_file == date
counter_increment = counter_increment.to_i + 1
else
counter_increment = 1
end
# write counter
file = File.open(file_name, 'w')
file.write(contents)
file.close
# store new counter value
counter.content = counter_increment.to_s + ';' + date
counter.save
end
system_id = Setting.get('system_id') || ''
number = t.strftime("%Y%m%d") + system_id.to_s + sprintf( "%04d", counter)
number = t.strftime("%Y%m%d") + system_id.to_s + sprintf( "%04d", counter_increment)
# calculate a checksum
# The algorithm to calculate the checksum is derived from the one
# Deutsche Bundesbahn (german railway company) uses for calculation
@ -55,9 +48,9 @@ module Ticket::Number::Date
(1..number.length).each do |i|
digit = number.to_s[i, 1]
chksum = chksum + ( mult * digit.to_i )
mult += 1;
mult += 1
if mult == 3
mult = 1;
mult = 1
end
end
chksum %= 10

View file

@ -8,40 +8,34 @@ module Ticket::Number::Increment
# read counter
min_digs = config[:min_size] || 4;
file_name = Rails.root.to_s + '/' + config[:file]
contents = ""
begin
file = File.open( file_name )
file.each {|line|
contents << line
}
file.close
rescue
contents = '0'
counter_increment = nil
Ticket::Counter.transaction do
counter = Ticket::Counter.where( :generator => 'Increment' ).lock(true).first
if !counter
counter = Ticket::Counter.new( :generator => 'Increment', :content => '0' )
end
counter_increment = counter.content.to_i
# increase counter
counter_increment += 1
# store new counter value
counter.content = counter_increment.to_s
counter.save
end
# increase counter
counter, date_file = contents.to_s.split(';')
counter = counter.to_i + 1
contents = counter.to_s + ';'
# write counter
file = File.open( file_name, 'w' )
file.write(contents)
file.close
# fill up number counter
if config[:checksum]
min_digs = min_digs.to_i - 1
end
fillup = Setting.get('system_id') || '1'
( 1..100 ).each do |i|
if ( fillup.length.to_i + counter.to_s.length.to_i ) < min_digs.to_i
if ( fillup.length.to_i + counter_increment.to_s.length.to_i ) < min_digs.to_i
fillup = fillup + '0'
end
end
number = fillup.to_s + counter.to_s
number = fillup.to_s + counter_increment.to_s
# calculate a checksum
# The algorithm to calculate the checksum is derived from the one
# Deutsche Bundesbahn (german railway company) uses for calculation
@ -57,9 +51,9 @@ module Ticket::Number::Increment
(1..number.length).each do |i|
digit = number.to_s[i, 1]
chksum = chksum + ( mult * digit.to_i )
mult += 1;
mult += 1
if mult == 3
mult = 1;
mult = 1
end
end
chksum %= 10
@ -79,11 +73,11 @@ module Ticket::Number::Increment
ticket_hook = Setting.get('ticket_hook')
ticket_hook_divider = Setting.get('ticket_hook_divider') || ''
ticket = nil
# probe format
if string =~ /#{ticket_hook}#{ticket_hook_divider}(#{system_id}\d{2,50})/i then
if string =~ /#{ticket_hook}#{ticket_hook_divider}(#{system_id}\d{2,48})/i then
ticket = Ticket.where( :number => $1 ).first
elsif string =~ /#{ticket_hook}\s{0,2}(#{system_id}\d{2,50})/i then
elsif string =~ /#{ticket_hook}\s{0,2}(#{system_id}\d{2,48})/i then
ticket = Ticket.where( :number => $1 ).first
end
return ticket

View file

@ -0,0 +1,12 @@
class TicketCounter < ActiveRecord::Migration
def up
create_table :ticket_counters do |t|
t.column :content, :string, :limit => 100, :null => false
t.column :generator, :string, :limit => 100, :null => false
end
add_index :ticket_counters, [:generator], :unique => true
end
def down
end
end

View file

@ -748,18 +748,11 @@ Setting.create(
20 => 20,
},
},
{
:display => 'Logfile',
:null => false,
:name => 'file',
:tag => 'input',
},
],
},
:state => {
:value => {
:checksum => false,
:file => 'tmp/counter.log',
:min_size => 5,
},
},
@ -782,18 +775,11 @@ Setting.create(
false => 'no',
},
},
{
:display => 'Logfile',
:null => false,
:name => 'file',
:tag => 'input',
},
],
},
:state => {
:value => {
:checksum => false,
:file => 'tmp/counter.log',
}
},
:frontend => false