Class: Ig3tool::Actions

Inherits:
Object
  • Object
show all
Defined in:
lib/actions/bib.rb,
lib/actions/people.rb,
lib/actions/interne.rb,
lib/actions/kaching.rb,
lib/actions/product.rb,
lib/actions/bitching.rb,
lib/actions/printing.rb

Constant Summary collapse

STATUSSEN =
{ 0 => "non-member", 1 => "member", 2 => "honorary member", 3 => "debugger", 4 => "ex-debugger"}

Class Method Summary collapse

Class Method Details

.bib_add!(params) ⇒ Object

Een boek aanmaken. Verwacht “isbn”, “title”, “author”, “publisher”, “year”. Mogelijkheid om “copies” ook bij te zetten, default 1. Returnt het ISBN van het nieuwe boek.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/actions/bib.rb', line 143

def self.bib_add!(params)
	notfound = []
	[:isbn, :title, :author, :publisher, :year].each do |k|
		notfound << k if params[k.to_s].nil?
	end

	raise Needed, notfound unless notfound.empty?
	isbn = _isbn(params["isbn"]) or raise InvalidISBN, "Invalid isbn number"

	b = BibBook.find_by_isbn(isbn) 

	unless b.nil?
		b.title     = params["title"]
		b.author    = params["author"]
		b.publisher = params["publisher"]
		b.year      = params["year"]
		b.section   = params["section"]
		b.copies    = params["copies"] || 1
		b.save # DB Save

		Logger::log :bib, :update, "Book %s (%s) updated." %
		[ params["isbn"], params["title"] ]

	else
		b = BibBook.new(:isbn      => isbn,
		                :title     => params["title"],
		                :author    => params["author"],
		                :publisher => params["publisher"],
		                :year      => params["year"],
		                :section   => params["section"],
		                :copies    => params["copies"] || 1)

		b.save!

		Logger::log :bib, :add, "Book %s (%s) created." %
		[ isbn, params["title"] ]
	end

	b.isbn

	rescue ActiveRecord::RecordInvalid
		raise BibSectionNotFound, "the ig3tool imps want you to select a section..."
end

.bib_available(params) ⇒ Object

Kijkt of een bepaald boek url beschikbaar is

Raises:



54
55
56
57
58
# File 'lib/actions/bib.rb', line 54

def self.bib_available(params)
	raise Needed, "barcode" if params[:given_uri] != 1
	isbn = _isbn(params[:from_uri].first) or raise InvalidISBN, "Invalid isbn number"
	_available(isbn)
end

.bib_availableinfo(params) ⇒ Object

Kijkt of een bepaald boek url beschikbaar is

Raises:



47
48
49
50
51
# File 'lib/actions/bib.rb', line 47

def self.bib_availableinfo(params)
	raise Needed, "barcode" if params[:given_uri] != 1
	isbn = _isbn(params[:from_uri].first)
	_available(isbn,true)
end

.bib_books(params) ⇒ Object

Geeft een lijst van boeken terug. Geen params -> alle boeken ALL UPPERCASE -> alle boeken in die sectie else -> zoek in titels en auteurs



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/actions/bib.rb', line 13

def self.bib_books(params)
	p = params[:from_uri].first
	if p.nil? || p.empty? then
		BibBook.find(:all, :order => "title")
	elsif p =~ /^(\d|X){10,}/ # Indien hij begint met X of een digit -> ISBN
		BibBook.find(_isbn(p))
	elsif p == p.upcase  # Indien in uppercase -> Section
		BibSection.find(p).books
	else
		p = '%' + p + '%'
		BibBook.find(:all, :order => "title", :conditions => [ "title like ? or author like ?", p, p ])
	end
end

.bib_extend!(params) ⇒ Object

Verleng een lening voor het gegeven ‘isbn’ voor ‘member’.

Raises:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/actions/bib.rb', line 120

def self.bib_extend!(params)
	isbn = _isbn(params["isbn"]) or raise InvalidISBN, "Invalid isbn number"

	member = Membership.find params["member"]
	raise NotAMember, params["member"] if member.nil?

	book        = BibBook.find(isbn)
	loan = BibLoan.find_by_isbn_and_member_id(book.isbn, member.barcode)
	return_date = Time.parse(loan.return_date) + 3.weeks
	#return_date = Time.now + 3.weeks
    #
	loan.return_date = return_date
	loan.save!

	Logger::log :bib, :loan, "%s extended %s (%s)." %
	[ member.person.username, isbn, book.title ]

	loan # Return Loan
end

.bib_info(params) ⇒ Object

Geeft alle info over een boek terug Verwacht een ISBN

Raises:



29
30
31
32
33
# File 'lib/actions/bib.rb', line 29

def self.bib_info(params)
	raise Needed, "barcode" if params[:given_uri] != 1
	isbn = _isbn(params[:from_uri].first) or raise InvalidISBN, "Invalid isbn number"
	BibBook.find_by_isbn isbn # Return een boek
end

.bib_loan!(params) ⇒ Object

Maakt een lening aan voor het gegeven ‘isbn’ voor ‘member’ met ‘warranty’. Standaard datum van teruggave is nu+3 weken, standaard garantie is “debugger ? 0 : 5”

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/actions/bib.rb', line 94

def self.bib_loan!(params)
	isbn = _isbn(params["isbn"]) or raise InvalidISBN, "Invalid isbn number"
	book   = BibBook.find(isbn)

	raise BookNotAvaible, "All copies loaned out!" unless book.available?

	member = Membership.find params["member"]
	raise NotAMember, params["member"] if member.nil?

	warranty    = params["warranty"] || (member.debugger? ? 0 : 5)
	loan_date   = Time.now
	return_date = Time.now + 3.weeks

	BibLoan.create(:isbn        => book.isbn,
	               :member_id   => member.barcode,
	               :warranty    => warranty,
	               :loan_date   => loan_date,
	               :return_date => return_date)

	Logger::log :bib, :loan, "%s loaned %s (%s)." %
	[ member.person.username, isbn, book.title ]

	"OK"
end

.bib_loans(params) ⇒ Object

Geeft alle uitgeleende boeken terug Verwacht geen parameters



37
38
39
40
41
42
43
44
# File 'lib/actions/bib.rb', line 37

def self.bib_loans(params)
	if params[:given_uri] == 1
		memberid = params[:from_uri].first
		BibLoan.find_by_member_id(memberid)
	else
		BibLoan.find(:all, :order => "return_date ASC")
	end
end

.bib_lookup(params) ⇒ Object

Raises:



81
82
83
84
85
86
87
88
89
# File 'lib/actions/bib.rb', line 81

def self.bib_lookup(params)
	raise Needed, "search value"          if params[:given_uri] == 0
	raise Needed, "even number of values" if params[:given_uri] % 2 == 1

	valid_keys = %w(title author publisher year section copies isbn)
    h = params[:from_uri].to_h
    h["isbn"] = _isbn(h["isbn"]) unless h["isbn"].nil?
	BibBook.hash_lookup(h, valid_keys)
end

.bib_remove!(params) ⇒ Object

Een boek verwijderen, verwacht het ISBN nummer in :isbn.



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/actions/bib.rb', line 188

def self.bib_remove!(params)
	isbn = _isbn(params["isbn"]) or raise InvalidISBN, "Invalid isbn number"
	book = BibBook.find(isbn)
	raise BookNotAvaible, "Book is loaned" unless book.loans.empty?

	book.destroy

	Logger::log :bib, :remove, "Book %d removed." % params["isbn"]

	"OK"
	rescue ActiveRecord::RecordNotFound
		raise NotFound, "book"
end

.bib_return!(params) ⇒ Object

Vernietigt de lening van het gegeven ‘isbn’ en ‘member’



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/actions/bib.rb', line 61

def self.bib_return!(params)
	isbn = _isbn(params["isbn"]) or raise InvalidISBN, "Invalid isbn number"
	member = params["member"] or raise Needed, "membership"
	name   = Membership.find(member).username
    book = BibBook.find(isbn)
	l      = BibLoan.find(:all, :conditions =>
	         [ "member_id = ? and isbn = ?", member, isbn ])
	if l.empty?
		raise NotFound, "Book #{isbn} not loaned by #{name}" if loan.nil?
	else
		Logger::log :bib, :return, "%s returned %s (%s)" %
		[ name, isbn, book.title ]

		l.first.destroy
		"OK"
	end
rescue ActiveRecord::RecordNotFound
	raise NotAMember, params['member']
end

.bib_sections(params) ⇒ Object

Geeft alle secties terug die in de bib te vinden zijn.



5
6
7
# File 'lib/actions/bib.rb', line 5

def self.bib_sections(params)
	BibSection.find(:all, :order => "name")
end

.interne(params) ⇒ Object

Verwacht een ‘username’ in de URL Geeft saldo terug

Raises:



17
18
19
20
21
# File 'lib/actions/interne.rb', line 17

def self.interne(params)
	raise NeedDebugger unless params[:given_uri] == 1
	name = params[:from_uri].first
	_interne(name).saldo
end

.interne_log(params) ⇒ Object

Toon de InterneTransactions Geen debugger-username -> Alles Debugger-username -> Donor + Recipient



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/actions/interne.rb', line 26

def self.interne_log(params)
	# XXX TODO
	# Change from LogEntry to InterneTransaction
	# MAAR:
	# 	eerst nog zorgen dat alle transacties ivm interne (dus ook scribblen enzo) ook weergegeven worden
	# 	voorlopig dus via LogEntry zodat je meer info hebt
	name, max_lines = params[:from_uri]
	max_lines = 500 if max_lines.nil?

	if name
		name = "%#{name}%"
		LogEntry.find(:all, :conditions => ["system   = 'interne'  and 
		                                    subsystem = 'transfer' and
		                                    message like ?", name],
		                    :order => "timestamp DESC",
		                    :limit => max_lines)
	else
		LogEntry.find(:all, :order => "timestamp DESC",
		                    :limit => max_lines)
	end
end

.interne_transfer!(params) ⇒ Object

Verwacht in ‘from’,‘to’ usernames Verwacht in ‘saldo’ een getal Geeft saldo terug.

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/actions/interne.rb', line 51

def self.interne_transfer!(params)
	from   = params['from']        or raise Needed, "source interne"
	to     = params['to']          or raise Needed, "target interne"
	amount = params['amount'].to_i or raise Needed, "amount"

	if amount < 0 then
		amount *= -1
		from, to = to, from
	end

	raise IG3Error, "Having fun playing with yourself?"    if from == to
	raise IG3Error, "No sense in transferring 0 credits.." if amount == 0

	case
		when from == "kas" # geld UIT de kassa
			Logger::log :interne, :withdraw, '%s withdrew %.2f from kas' % [to, amount.to_f / 100]
			_with_interne(to) { |s| s - amount }

		when to == "kas"   # geld IN de kassa
			Logger::log :interne, :deposit, '%s deposited %.2f in kas' % [from, amount.to_f / 100]
			_with_interne(from) { |s| s + amount }

		else
			Logger::log :interne, :transfer, '%s transferred %.2f to %s' % [from, amount.to_f / 100, to]
			Interne.transaction do
				_with_interne(to)   { |s| s + amount }
				_with_interne(from) { |s| s - amount }
			end
	end
end

.internes(params) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/actions/interne.rb', line 7

def self.internes(params)
	internes = {}
	Interne.find(:all) \
	       .sort { |a,b| a.person.username <=> b.person.username } \
				 .each { |i| internes[i.person.username] = i.saldo }
	internes
end

.kaching!(params) ⇒ Object

Verwacht een hash van barcode => aantal in ‘items’ Optioneel een status in ‘status’ Geeft kost terug.



32
33
34
35
36
37
38
# File 'lib/actions/kaching.rb', line 32

def self.kaching!(params)
	Product.transaction do
		total = _kaching(params['items'], params['status'] || "plebs")
		_kaching_log :kaching, params['debugger'], params['items']
		total
	end
end

.kaching_cola(params) ⇒ Object

This one’s for you, joris :P



9
10
11
# File 'lib/actions/kaching.rb', line 9

def self.kaching_cola(params)
	scribble(params.update({"items" => {"5449000000996" => 1}}))
end

.person_add!(params) ⇒ Object

Voegt een persoon toe



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/actions/people.rb', line 86

def self.person_add!(params)
  p = Person.new
  p.username   = params['username'] or raise Needed, "Username"
  p.email      = params['email'] or raise Needed, "Email"
  p.first_name = params['first_name'] or raise Needed, "First_name"
  p.last_name  = params['last_name'] or raise Needed, "Last_name"
  p.rolnr   = params['rolnr']
  p.address = params['address']
  p.phone   = params['phone']
  p.gsm     = params['gsm']

  status   = params['status'] || "non-member"
  memberid = params['memberid']
  if status != "non-member"
    m = Membership.new
    m.barcode  = memberid
    m.username = p.username
    m.status   = status
    m.year     = Time.werkjaar
  end

  pu = PrintUser.new
  pu.saldo = 0

  Person.transaction do
    # Set username as latest because it calls save!
    p.save!
    pu.username = p.username
    pu.save!
    m.save! if status != "non-member"

    pu.add_credit(200) if status == "member"
    "OK"
  end
end

.person_bugger!(params) ⇒ Object

Raises:



157
158
159
160
161
162
163
164
165
166
# File 'lib/actions/people.rb', line 157

def self.person_bugger!(params)
  raise Needed, "Username" unless params['username']
  p = Person.find_by_username(params['username'])
  raise NotFound if p.nil?
  raise NotAMember unless p.member?
  raise IG3Error, "already a debugger" if p.debugger?

  p.bugger!
  "OK"
end

.person_isdebugger(params) ⇒ Object

Raises:



137
138
139
140
141
142
# File 'lib/actions/people.rb', line 137

def self.person_isdebugger(params)
  raise Needed, "Username" unless params[:from_uri].size > 0
  p = Person.find_by_username(params[:from_uri].first)
  raise NotFound if p.nil?
  p.debugger?
end

.person_lookup(params) ⇒ Object

Zoekt persons op adhv attributes Verwacht minstens 1 key => value pair, kan meerdere mensen teruggeven. Mogelijke keys: username, email, first_name, last_name, rolnr, address, phone, gsm



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/actions/people.rb', line 19

def self.person_lookup(params)
  raise Needed, "At least one search value"    if params[:given_uri] < 1
  raise Needed, "Even number of search values" if params[:given_uri] % 2 == 1

  keys   = params[:from_uri].to_h
  status = keys.delete 'status'
  valid_keys = %w(username email first_name last_name rolnr address phone gsm)
  res = Person.hash_lookup(keys, valid_keys)

  if status
    return res.select {|x| x.send "#{status}?".to_sym }
  else
    return res
  end
rescue RuntimeError => e
  raise IG3Error, e.message
end

.person_member(params) ⇒ Object

Returnt een persoon adhv een membership barcode

Raises:



45
46
47
48
49
50
51
# File 'lib/actions/people.rb', line 45

def self.person_member(params)
  raise Needed, "memberid" if params[:given_uri] != 1
  memberid = params[:from_uri].first
  member = Membership.find_by_barcode(memberid)
  raise NotAMember if member.nil?
  Person.find(member.username) # Zoek het member, en return de persoon
end

.person_memberships(params) ⇒ Object

Geeft de memberships van een persoon terug, gesorteerd op datum Verwacht een username.

Raises:



39
40
41
42
# File 'lib/actions/people.rb', line 39

def self.person_memberships(params)
  raise Needed, "username" if params[:given_uri] != 1
  Person.find(params[:from_uri].first).memberships.sort { |a,b| b.year <=> a.year }
end

.person_remove!(params) ⇒ Object

Verwijdert een member (indien mogelijk -> Geen openstaande leningen/niet-0-internes/…)

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/actions/people.rb', line 54

def self.person_remove!(params)
  username = params["username"] or raise Needed, "username"

  p = Person.find username

  # Controleer Interne
  i = p.interne
  raise SaldoNotZero, "Debugger: saldo not zero!" if !i.nil? and i.saldo != 0

  # Controleer leningen
  p.memberships.each do |m|
    unless m.nil?
      loans = BibLoan.find_by_member_id m.id
      raise StillHaveLoans, "Member: outstanding loans" unless loans.nil? or loans.empty?
    end
  end

  begin
    p.destroy
  rescue ActiveRecord::RecordNotFound
    raise NotFound, "Person"
  else
    :ok
  end
end

.person_status(params) ⇒ Object

Raises:



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/actions/people.rb', line 144

def self.person_status(params)
  raise Needed, "Username" unless params[:from_uri].size > 0
  p = Person.find_by_username(params[:from_uri].first)
  raise NotFound if p.nil?
  if p.debugger?
    "debugger"
  elsif p.member?
    "member"
  else 
    "non-member"
  end
end

.person_statussen(params) ⇒ Object

Returnt alle mogelijk statussen



81
82
83
# File 'lib/actions/people.rb', line 81

def self.person_statussen(params)
  STATUSSEN  # Return de Hash
end

.person_unbugger!(params) ⇒ Object

Raises:



168
169
170
171
172
173
174
175
176
# File 'lib/actions/people.rb', line 168

def self.person_unbugger!(params)
  raise Needed, "Username" unless params['username']
  p = Person.find_by_username(params['username'])
  raise NotFound if p.nil?
  raise NotADebugger unless p.debugger?

  p.unbugger!
  "OK"
end

.person_update!(params) ⇒ Object

Update een persoon (adhv een id)

Raises:



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/actions/people.rb', line 123

def self.person_update!(params)
  raise Needed, "Username" unless params['username']
  p = Person.find_by_username(params['username'])
  raise NotFound if p.nil?
  p.email      = params['email'] or raise Needed, "Email"
  p.first_name = params['first_name'] or raise Needed, "First_name"
  p.last_name  = params['last_name'] or raise Needed, "Last_name"
  p.rolnr      = params['rolnr']
  p.address    = params['address']
  p.phone      = params['phone']
  p.gsm        = params['gsm']
  p.save!
end


97
98
99
100
101
102
103
104
105
# File 'lib/actions/printing.rb', line 97

def self.print_addalias!(params)
	username = params["username"] or raise Needed, "username"
	nwalias = params["alias"] or raise Needed, "alias"
	pu = PrintUser.find(username) # dit is eigenlijk overkill, lekker!
	pa = PrintAlias.new
	pa.username = username
	pa.alias = nwalias
	pa.save!
end


33
34
35
36
37
38
39
# File 'lib/actions/printing.rb', line 33

def self.print_addcredit!(params)
	username = params["username"] or raise Needed, "username"
	amount = params["amount"] or raise Needed, "amount"
	pu = PrintUser.lookup(username) or raise NotFound, "username"
	pu.add_credit(amount)
	pu
end


12
13
14
15
# File 'lib/actions/printing.rb', line 12

def self.print_debuggers(params)
			users = PrintUser.find(:all, :order => "username")
  users.select{|user| user.debugger?}
end


41
42
43
# File 'lib/actions/printing.rb', line 41

def self.print_errortest(params)
	PrintUser.errortest
end


17
18
19
20
# File 'lib/actions/printing.rb', line 17

def self.print_logs(params)
	max_lines = (params[:given_uri] < 1 ? 50 : params[:given_uri].first)
	PrintTransaction.find(:all, :limit => max_lines, :order => "time DESC")
end


84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/actions/printing.rb', line 84

def self.print_print!(params)
	username = params["username"] or raise Needed, "username"
	job = params["job"] or raise Needed, "job"
	pages = params["pages"] or raise Needed, "pages"
	copies = params["copies"] or raise Needed, "copies"
	host = params["host"] or raise Needed, "host"
	amount = params["amount"] or raise Needed, "amount"
	queue = params["queue"] or raise Needed, "queue"
	message = params["message"] || ""
	pu = PrintUser.lookup(username)
	pu.print(job, pages, copies, host, amount, queue, message)
end


74
75
76
77
78
79
80
81
82
# File 'lib/actions/printing.rb', line 74

def self.print_refund!(params)
	logid = params["logid"] or raise Needed, "logid"
	begin
		transaction = PrintTransaction.find(logid)
		transaction.refund
	rescue
		raise TransactionNotFound, "this log id is not valid"
	end
end


28
29
30
31
# File 'lib/actions/printing.rb', line 28

def self.print_remove!(params)
	username = params["username"] or raise Needed, "username"
	PrintUser.remove(username)
end


107
108
109
110
111
112
113
114
115
116
# File 'lib/actions/printing.rb', line 107

def self.print_removealias!(params)
	username = params["username"] or raise Needed, "username"
	a = params["alias"] or raise Needed, "alias"
	pa = PrintAlias.find(a)
	if pa.print_user.username == username
		pa.destroy
	else
		raise NotFound, "wrong username"
	end
end


118
119
120
121
122
# File 'lib/actions/printing.rb', line 118

def self.print_removealiases!(params)
	username = params["username"] or raise Needed, "username"
	pu = PrintUser.lookup(username).remove_aliases
	pu
end

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/actions/printing.rb', line 45

def self.print_update!(params)
	username = params["username"] or raise Needed, "username"
	firstname = params["firstname"]
	lastname = params["lastname"] 
	email = params["email"]
  raise Needed, "at least one of: username, firstname, lastname" if lastname.nil? && firstname.nil? && email.nil?
	amount = params["amount"] || 0
	begin
		p = Person.find(username) 
	rescue
		p =	Person.new
		p.username = username
  	raise Needed, "username, firstname and lastname" if lastname.nil? && firstname.nil? && email.nil?
	end
	p.first_name = firstname unless firstname.nil?
	p.last_name = lastname unless lastname.nil?
	p.email = email unless email.nil?
	p.save!
	begin
		pu = PrintUser.lookup(username) 
	rescue NotFound => err
		pu =	PrintUser.new
		pu.username = username
		pu.save!
	end
	pu.add_credit(amount) if amount.to_i.to_b
	pu
end

Raises:



22
23
24
25
26
# File 'lib/actions/printing.rb', line 22

def self.print_user(params)
	raise Needed, "username or alias" if params[:given_uri] < 1
	username = params[:from_uri].first
	PrintUser.lookup(username)
end


8
9
10
# File 'lib/actions/printing.rb', line 8

def self.print_users(params)
	PrintUser.find(:all, :order => "username")
end

.product_add!(params) ⇒ Object

Voegt een product toe aan de DB Verwacht ‘barcode’, ‘name’, ‘category’, ‘nmprice’, ‘mprice’ en ‘dprice’

Raises:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/actions/product.rb', line 63

def self.product_add!(params)
	notfound = []

	[:barcode, :name, :category, :nmprice, :mprice, :dprice].each do |k|
		notfound << k unless params.include? k.to_s and !params[k.to_s].nil?
	end

	raise Needed, notfound unless notfound.empty?

	p = \
	Product.create(:barcode  => params["barcode"],
	               :name     => params["name"],
	               :category => Category.find_by_name(params["category"].capitalize),
	               :nmprice  => params["nmprice"],
	               :mprice   => params["mprice"],
	               :dprice   => params["dprice"],
	               :stock    => 0) # XXX of params["amount"] ?

	Logger::log :product, :add, "Added %d (%s)" % [ params["barcode"], params["name"] ]

	"OK"
end

.product_all(params) ⇒ Object

Verwacht geen parameters Geeft alle producten in de stock terug



27
28
29
30
# File 'lib/actions/product.rb', line 27

def self.product_all(params)
	#Product.find :all, :conditions => { 'stock_gt' => 0 } # Stock Greater than
	Product.find :all, :include => [:purchases]
end

.product_categories(params) ⇒ Object

Geeft alle categorieen terug.



6
7
8
# File 'lib/actions/product.rb', line 6

def self.product_categories(params)
	Category.find(:all)
end

.product_category(params) ⇒ Object

Geeft alle producten uit een categorie terug

Raises:



11
12
13
14
# File 'lib/actions/product.rb', line 11

def self.product_category(params)
	raise Needed, "Category" if params[:given_uri] != 1
	Category.find(params[:from_uri].first).products
end

.product_diff!(params) ⇒ Object

Verwacht een barcode en de stock in de URL Geeft de diff stock terug Verwijdert de oudste items indien newstock < oldstock Voegt items toe met de nieuwste prijs indien newstock > oldstock



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/actions/product.rb', line 45

def self.product_diff!(params)
	barcode = params["barcode"] or raise Needed, "Barcode"
	newstock = params["stock"] or raise Needed, "Stock"
	p = _product(barcode)
	oldstock = p.stock

	if oldstock < newstock
		# TODO XXX Set Price!
	else
		# TODO remove from stock!			
	end

	Logger::log :stock, :diff, "%s stock changed from %d to %d" %
		            [ p.name, oldstock, p.stock ]
end

.product_lookup(params) ⇒ Object

Verwacht een barcode in de URL Geeft het product terug

Raises:



18
19
20
21
22
23
# File 'lib/actions/product.rb', line 18

def self.product_lookup(params)
	raise Needed, "Barcode" if params[:given_uri] != 1

	p = params[:from_uri].first
	_product(p)
end

.product_purchase!(params) ⇒ Object

Laat een ‘debugger’ een aankoop ingeven, met factuurnummer ‘reference’. Verwacht een hash barcode => [amount, cost] in ‘items’.

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/actions/product.rb', line 99

def self.product_purchase!(params)
	name = params["debugger"] or raise NeedDebugger
	debugger = Person.find_by_username(name)
	raise NotADebugger.new(name) unless !debugger.nil? && debugger.debugger?

	notfound = []
	params["items"].each do |barcode, item|
		_product(barcode, false) or notfound << barcode
		item[0] = item[0].to_i
		item[1] = item[1].to_i
		amount, cost = item
		raise "You're funny." unless amount.integer?
	end

	raise ProductNotFound.new(notfound.join(', ')) unless notfound.empty?

	Product.transaction do
		params["items"].each do |barcode, data|
			amount, cost = data
			p = _product(barcode)
			p.stock += amount
			p.save

			Purchase.create(:time      => Time.now,
			                :debugger  => debugger,
			                :product   => p,
			                :count     => amount,
			                :cost      => cost)

			Logger::log :purchase, :add, "%s purchased %d x '%s' for %0.2f" %
			                [ debugger.username, amount, p.name, cost ]
		end
	end

	"OK"
end

.product_purchases(params) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/actions/product.rb', line 136

def self.product_purchases(params)
	p = params[:from_uri].first
	if p.nil? or p == "all"
		Purchase.find :all, :order => "product, time desc"
	else
		Purchase.find_all_by_barcode(p, :order => "product, time desc")
	end
end

.product_remove!(params) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/actions/product.rb', line 86

def self.product_remove!(params)
	barcode = params["barcode"] or raise Needed, "Barcode"
	p = _product(barcode)

	raise "still in stock, won't delete!" if p.stock > 0

	p.destroy

	"OK"
end

.product_restock!(params) ⇒ Object

Automaat bijvullen. ‘items’ zoals kaching, en ‘debugger’



41
42
43
44
45
46
47
48
49
50
# File 'lib/actions/kaching.rb', line 41

def self.product_restock!(params)
	debugger = params['debugger']
	items    = params['items']

	Product.transaction do
		total = _kaching(items, "plebs")
		_kaching_log(:restock, debugger, items)
		total
	end
end

.product_stock(params) ⇒ Object

Verwacht een barcode in de URL Geeft de stock terug

Raises:



34
35
36
37
38
39
# File 'lib/actions/product.rb', line 34

def self.product_stock(params)
	raise Needed, "Barcode" if params[:given_uri] != 1

	prod = _product(params[:from_uri].first)
	prod.stock
end

.register!(params) ⇒ Object



12
13
14
15
16
# File 'lib/actions/bitching.rb', line 12

def self.register!(params)
	username = params["username"] or raise Needed, "username"
	password = params["password"] or raise Needed, "password"
	Bitch.wannabe(username, password)
end

.scribble!(params) ⇒ Object

Verwacht een array van [barcode,aantal] in ‘items’ Verwacht een username in ‘debugger’ Geeft het nieuwe saldo terug



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/actions/kaching.rb', line 16

def self.scribble!(params)
	name = params['debugger'] or raise Needed, "debugger"

	interne = _interne(name) or raise NotADebugger, name
	Product.transaction do
		_kaching_log :scribble, name, params['items']
		total = _kaching(params['items'], "debugger")
		interne.saldo -= total
		interne.save!
		total
	end
end

.wannabe!(params) ⇒ Object



6
7
8
9
10
# File 'lib/actions/bitching.rb', line 6

def self.wannabe!(params)
	username = params["username"] or raise Needed, "username"
	password = params["password"] or raise Needed, "password"
	Bitch.wannabe(username, password)
end