ig3tool 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,238 +0,0 @@
1
- module Ig3tool
2
-
3
- # The printing section of ig3tool
4
- #
5
- # Classes & methods:
6
- #
7
- # PrintUser
8
- # ---------
9
- # lookup(username)
10
- # looks for a printuser by username as well as alias
11
- # use this method to look usernames up
12
- #
13
- # add_credit(amount)
14
- # adds amount of credit to a user' saldo
15
- #
16
- # refund(amount, printid, msg)
17
- # refunds a failed job
18
- # should ONLY be called from PrintTransaction
19
- #
20
- # remove(username)
21
- # removes a printuser along its aliasses
22
- #
23
- # print(job, pages, copies, host, amount, queue, message)
24
- # handles the printing of a job
25
- # should ONLY be used by the printscript
26
- #
27
- # err(queue, message)
28
- # logs an error
29
- # again printscript ONLY
30
- #
31
- #
32
- #
33
- # PrintAlias
34
- # ----------
35
- # no special functions
36
- #
37
- #
38
- #
39
- # PrintTransaction
40
- # ----------------
41
- # refund()
42
- # refunds a specific job, calls aprintuser.refund()
43
- #
44
- #
45
- #
46
-
47
-
48
-
49
-
50
- # Implements a printuser.
51
- class PrintUser < ActiveRecord::Base
52
-
53
- set_table_name "print_users"
54
- set_nonauto_primary_key :username
55
- validates_uniqueness_of :username
56
-
57
- # _username_ and _saldo_ must be provided
58
- # a PrintUser must be linked with an existing _person_
59
- validates_presence_of :username, :saldo, :person
60
- belongs_to :person, :class_name => "Person", :foreign_key => "username"
61
- has_many :print_transactions, :class_name => "PrintTransaction", :foreign_key => "username"
62
- has_many :print_aliases, :class_name => "PrintAlias", :foreign_key => "username", :dependent => :destroy
63
- attr_accessible :username
64
- validates_associated :person
65
-
66
- # override because of non-standard primary key
67
- def username=(newusername)
68
- write_attribute(:username, newusername)
69
- save!
70
- end
71
-
72
- def self.lookup(username)
73
- user = nil
74
- begin
75
- user = PrintUser.find(username) # Error if not found
76
- rescue
77
- begin
78
- user = PrintAlias.find(username).print_user
79
- rescue
80
- raise NotFound, "the ig3tool imps found no such user..."
81
- end
82
- end
83
- user
84
- end
85
-
86
- def self.errortest
87
- raise NotFound
88
- end
89
-
90
- def remove_aliases
91
- print_aliases.each {|pa| pa.destroy }
92
- end
93
-
94
- def self.remove(username)
95
- user = PrintUser.lookup(username)
96
- user.destroy
97
- end
98
-
99
- def email
100
- self.person.email
101
- end
102
-
103
- def debugger?
104
- self.person.debugger?
105
- end
106
-
107
- def add_credit (amount)
108
- if self.debugger?
109
- raise PermissionDenied, "debuggers cannot add credits to their account..."
110
- else
111
- PrintUser.transaction do
112
- reload()
113
- PrintTransaction.new do |pt|
114
- pt.time = Time.now()
115
- pt.username = username
116
- pt.refunded = 0
117
- pt.amount = amount.to_i
118
- pt.queue = "ig3tool"
119
- pt.message = "added #{amount} eurocents to #{username}'s account."
120
- pt.category = "add"
121
- pt.save!
122
- end
123
- self.saldo += amount.to_i
124
- save!
125
- end
126
- end
127
- end
128
-
129
- def refund(amount, printid, undo, msg)
130
- PrintUser.transaction do
131
- reload()
132
- pt = PrintTransaction.new
133
- pt.time = Time.now()
134
- pt.username = username
135
- pt.refunded = 0
136
- pt.queue = "ig3tool"
137
- pt.amount = amount.to_i
138
- pt.message = "#{"undo " if undo}refunded print #{printid} of #{amount} euro to #{username}."
139
- pt.category = "refund"
140
- pt.save!
141
- if debugger?
142
- person.interne.refundprint(amount, "#{"undo " if undo }refund print, id: #{printid}", undo)
143
- else
144
- if undo
145
- self.saldo += amount
146
- else
147
- self.saldo -= amount
148
- end
149
- save!
150
- end
151
- end
152
- end
153
-
154
- def print(job, pages, copies, host, amount, queue, message)
155
- PrintUser.transaction do
156
- reload()
157
- PrintTransaction.new do |pt|
158
- pt.time = Time.now()
159
- pt.username = username
160
- pt.refunded = 0
161
- pt.amount = amount.to_i
162
- pt.pages = pages.to_i
163
- pt.copies = copies.to_i
164
- pt.host = host
165
- pt.queue = queue
166
- pt.job = job
167
- pt.message = "Printed #{job} of #{pages} pages for #{amount} EUR - #{message}"
168
- pt.category = "print"
169
- pt.save!
170
- end
171
- if debugger?
172
- person.interne.print(amount, "job: #{job}")
173
- else
174
- if self.saldo < amount.to_i
175
- raise NotEnoughCredit, "you are broke!"
176
- else
177
- self.saldo -= amount.to_i
178
- save!
179
- end
180
- end
181
- end
182
- end
183
-
184
-
185
- def err(message, queue)
186
- PrintTransaction.new do |pt|
187
- pt.time = Time.now()
188
- pt.username = username
189
- pt.queue = queue
190
- pt.category = "error"
191
- pt.save!
192
- end
193
- end
194
-
195
- end
196
-
197
-
198
- class PrintAlias < ActiveRecord::Base
199
-
200
- set_table_name "print_aliases"
201
- set_primary_key :alias
202
- validates_uniqueness_of :alias
203
- validates_presence_of :alias, :username, :print_user
204
- belongs_to :print_user, :class_name => "PrintUser", :foreign_key => "username"
205
- validates_associated :print_user
206
-
207
- def alias=(newalias)
208
- @attributes["alias"] = newalias
209
- end
210
-
211
- end
212
-
213
-
214
- class PrintTransaction < ActiveRecord::Base
215
-
216
- set_table_name "print_transactions"
217
- validates_presence_of :time, :username, :category, :queue
218
- validates_inclusion_of :category, :in => ["error", "add", "print", "refund", "debug"]
219
- belongs_to :printuser, :class_name => "PrintUser", :foreign_key => "username"
220
- attr_protected :refunded
221
-
222
- def refund
223
- PrintTransaction.transaction do
224
- reload()
225
- if category == "print" then
226
- printuser.refund(amount, id, !refunded.to_b, message)
227
- toggle!(:refunded)
228
- else
229
- raise WrongPrintlogType
230
- end
231
- end
232
- end
233
-
234
- end # class PrintTransaction
235
-
236
-
237
-
238
- end # module Ig3tool
@@ -1,14 +0,0 @@
1
- module Ig3tool
2
-
3
- class Request < ActiveRecord::Base
4
-
5
- set_table_name "requests"
6
-
7
- validates_presence_of :request, :bitch
8
-
9
- end
10
-
11
- end
12
-
13
-
14
-
@@ -1,158 +0,0 @@
1
- module Ig3tool
2
-
3
- class Category < ActiveRecord::Base
4
-
5
- set_table_name "categories"
6
-
7
- set_nonauto_primary_key :name
8
-
9
- validates_uniqueness_of :name
10
-
11
- validates_presence_of :name
12
-
13
- has_many :products,
14
- :class_name => "Product",
15
- :foreign_key => "category"
16
-
17
- end
18
-
19
-
20
- class Product < ActiveRecord::Base
21
-
22
- set_table_name "products"
23
-
24
- set_nonauto_primary_key :barcode
25
-
26
- validates_uniqueness_of :barcode
27
-
28
- validates_presence_of :barcode,
29
- :stock,
30
- :category,
31
- :nmprice,
32
- :mprice,
33
- :dprice,
34
- :name
35
-
36
- # validates_inclusion_of :category, :in => Category.find(:all)
37
-
38
- # attr_protected :stock
39
-
40
- has_many :sales,
41
- :class_name => "Sale",
42
- :foreign_key => "product"
43
-
44
- has_many :purchases,
45
- :class_name => "Purchase",
46
- :foreign_key => "product"
47
-
48
- # has_one :category,
49
- # :class_name => "Category",
50
- # :foreign_key => "category"
51
-
52
- validate :nmprice,
53
- :mprice,
54
- :dprice
55
- # XXX dit borkt :(
56
- #, { :nmprice < :mprice and :mprice < :dprice}
57
-
58
- before_save do |obj|
59
- m = obj.mprice
60
- nm = obj.nmprice
61
- d = obj.dprice
62
- obj.mprice = m = [m, d].max
63
- obj.nmprice = [m, nm].max
64
- end
65
-
66
- def price(status, count = 1)
67
- count *
68
- case status
69
- when "member"
70
- mprice
71
- when "debugger"
72
- dprice
73
- when "plebs", "non-member", "non member"
74
- nmprice
75
- else
76
- # XXX of willen we hier een exception gooien?
77
- warn "Invalid status: '#{status}'"
78
- nmprice
79
- end
80
- end
81
-
82
- def decrement_stock(amount)
83
- transaction do
84
- if stock >= amount
85
- update_attribute("stock", stock - amount)
86
- save
87
- else
88
- # XXX handle this bitch
89
- # we willen niet onder 0 gaan :)
90
- update_attribute("stock", 0)
91
- end
92
-
93
- purchases = Purchase.find_all_by_product(barcode,
94
- :conditions => [ "current > 0"],
95
- :order => "time")
96
-
97
- while amount > 0
98
- if purchases.empty?
99
- warn "stock corrupt: #{amount} items of #{barcode} - #{name} missing"
100
- break
101
- else
102
- p = purchases.shift
103
-
104
- amt = p.current - amount
105
- if amt < 0
106
- amount = amt.abs
107
- amt = 0
108
- else
109
- amount = 0
110
- end
111
-
112
- p.current = amt
113
- p.save
114
- end
115
- end
116
- reload
117
- end
118
- end
119
-
120
- def increase_stock(amount)
121
- # XXX mag enkel gebruikt worden bij de purchases?
122
- end
123
-
124
- end
125
-
126
- class Sale < ActiveRecord::Base
127
- # een verkoop
128
-
129
- set_table_name "sales"
130
-
131
- end
132
-
133
- class Purchase < ActiveRecord::Base
134
- # een aankoop
135
-
136
- set_table_name "purchases"
137
-
138
- belongs_to :product, :class_name => "Product", :foreign_key => "product"
139
- belongs_to :debugger, :class_name => "Person", :foreign_key => "debugger"
140
-
141
- validates_presence_of :debugger, :product, :count, :cost
142
- validates_numericality_of :count, :cost
143
-
144
- before_save do |obj|
145
- obj.current = [obj.count, 0].max if obj.current.nil?
146
- obj.time = Time.now if obj.time.nil?
147
- end
148
-
149
- after_create do |obj|
150
- p = obj.product
151
- p.stock += current
152
- p.save
153
- # p.recalculate_price # XXX wishful thinking :)
154
- end
155
-
156
- end
157
-
158
- end
@@ -1,106 +0,0 @@
1
- class Time
2
- # bepaling van de week van het academiejaar
3
- def week
4
- t = start_werkjaar(Time.werkjaar)
5
- return 1 + ((self - t) / (86400*7)).to_int
6
- end
7
-
8
- # we bepalen de start van het werkjaar (laatste ma van sept)
9
- def self.start_werkjaar(jaar)
10
- t = Time.mktime(jaar, 9, 30)
11
- t = t - 86400 until t.wday == 1
12
- t
13
- end
14
-
15
- def self.werkjaar
16
- t = Time.now()
17
- t > start_werkjaar(t.year) ? t.year : t.year - 1
18
- end
19
- end
20
-
21
- class Fixnum
22
- # Convenience method
23
- def weeks
24
- self * 86400 * 7
25
- end
26
- end
27
-
28
- class Integer
29
- def to_b
30
- !zero?
31
- end
32
- end
33
-
34
- class Array
35
- # File facets/conversion.rb, line 38
36
- def to_h(arrayed=nil)
37
- h = {}
38
- if arrayed #or (flatten.size % 2 == 1)
39
- #each{ |e| h[e.first] = e.slice(1..-1) }
40
- each{ |k,*v| h[k] = v }
41
- else
42
- h = Hash[*(self.flatten)]
43
- end
44
- h
45
- end
46
- end
47
-
48
- class Hash
49
- # File facets/hash/keyize.rb, line 49
50
- def normalize_keys!( &block )
51
- keys.each{ |k|
52
- nk = block[k]
53
- self[nk]=delete(k) if nk
54
- }
55
- self
56
- end
57
- end
58
-
59
- # XXX MOVE THIS TO BETTER PLACE
60
-
61
- def send_email(from, from_alias, to, to_alias, subject, message)
62
- msg = %q{<<END_OF_MESSAGE
63
- |From: #{from_alias} <#{from}>
64
- |To: #{to_alias} <#{to}>
65
- |Subject: #{subject}
66
-
67
- |#{message}}
68
-
69
- Net::SMTP.start('localhost') do |smtp|
70
- smtp.send_message msg, from, to
71
- end
72
- end
73
-
74
- def email(from, to, subject, message)
75
- from_alias = "#{from.first_name} #{from.last_name}"
76
- to_alias = "#{to.first_name} #{to.last_name}"
77
- send_email(from.email, from_alias, to.email, to_alias, subject, message)
78
- end
79
-
80
- module ActiveRecord
81
- class Base
82
- def self.hash_lookup(search, valid_keys)
83
- search = search.to_h unless search.is_a? Hash
84
-
85
- return find(:all) if search.empty?
86
-
87
- clauses = []
88
- values = []
89
- search.normalize_keys! { |k| k.to_s.downcase }
90
-
91
- search.each do |k, v|
92
- raise "Invalid search key '#{k}'" unless valid_keys.include? k
93
- if %w(barcode username).include? k or k == primary_key
94
- clauses << "#{k} = ?"
95
- values << v
96
- else
97
- clauses << "#{k} like ?"
98
- v = "%#{v}%"
99
- values << v
100
- end
101
- end
102
-
103
- find(:all, :conditions => [ clauses.join(" AND "), *values ])
104
- end
105
- end
106
- end