Class: Inv

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_inventory_cl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item = 'nothing', **specs) ⇒ Inv

Returns a new instance of Inv.



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

def initialize(item='nothing',**specs)
	self.item = item
	self.stock = specs[:stock]
	@desc = specs[:desc]
end

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc.



7
8
9
# File 'lib/simple_inventory_cl.rb', line 7

def desc
  @desc
end

#itemObject

Returns the value of attribute item.



7
8
9
# File 'lib/simple_inventory_cl.rb', line 7

def item
  @item
end

#stockObject

Returns the value of attribute stock.



7
8
9
# File 'lib/simple_inventory_cl.rb', line 7

def stock
  @stock
end

Instance Method Details

#delete(id_prod) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/simple_inventory_cl.rb', line 64

def delete(id_prod)
	begin
		db = SQLite3::Database.open('inventory.db')
		db.transaction
		db.execute2 "DELETE from stock WHERE Id = :id_prod OR Product = :id_prod", id_prod
		db.commit
		puts db.changes.to_s + " deletion"
	rescue SQLite3::Exception => e
		puts "error in delete " , e
		db.rollback
	ensure
		db.close if db
	end
end

#find_to_change(id_prod) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/simple_inventory_cl.rb', line 80

def find_to_change(id_prod)
	begin
		db = SQLite3::Database.open('inventory.db')
		print_out = db.execute2 "Select * from stock where Id = :id_prod OR Product = :id_prod" ,id_prod 
		puts
		print_out.each { |line| puts "%3s %-10s -%-3s- [%5s] %-5s %20s" % [line[0], line[1], line[2], line[3], line[4], line[5]]} 
		puts
		pull = db.execute2 "Select Level from stock where Id = :id_prod OR Product = :id_prod" ,id_prod 
		@pull = Integer(pull[1][0])
	rescue SQLite3::Exception => e
		puts "error in find_to_change " , e
	ensure
		db.close if db
	end
	@pull
end

#reorder(id_prod) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/simple_inventory_cl.rb', line 113

def reorder(id_prod)
	begin
		db = SQLite3::Database.open('inventory.db')
		current_level = db.execute2 "SELECT Level from stock WHERE Id = :id_prod OR Product = :id_prod", id_prod
		@current_level = Integer(current_level[1][0])
		reorder_level = db.execute2 "SELECT RO_at from stock WHERE Id = :id_prod OR Product = :id_prod", id_prod
		@reorder_level = Integer(reorder_level[1][0])
		if @current_level <= @reorder_level
			replenish = "YES"
		else
			replenish = ''
		end	
		db.transaction
		db.execute2 "UPDATE stock SET RO = :replenish WHERE Id = :id_prod OR Product = :id_prod", replenish, id_prod
		db.commit
		puts db.changes.to_s + " change"
		print_out = db.execute2 "SELECT * from stock WHERE Id = :id_prod OR Product = :id_prod", id_prod
		puts
		print_out.each { |p| puts "%3s %-10s -%-3s- [%5s] %-5s %20s" % [p[0],p[1],p[2],p[3],p[4],p[5]]}
		puts
	rescue SQLite3::Exception => e
		puts "error in reorder method " , e
		db.rollback
	ensure
		db.close if db
	end
	@replenish = replenish
end

#run(item, furthermore) ⇒ Object



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

def run(item, furthermore)
	begin
		db = SQLite3::Database.open('inventory.db')
		puts db.get_first_value "select SQLite_VERSION()"
		db.transaction
		#db.execute2 "drop table stock"
		db.execute2 "create table if not exists stock(Id INTEGER PRIMARY KEY, Product TEXT, RO TEXT, Level INTEGER, RO_at INTEGER, Description TEXT)"
		puts "foo"
		db.execute2 "insert into stock(Product, Level, RO_at, Description) values(:Product, :Level, :RO_at, :Description)", item, furthermore[:stock], furthermore[:reorder_lvl], furthermore[:desc]
		db.commit
		puts db.changes.to_s + " change"
		puts "You added #{item}, with stock level #{stock}"
		puts "#{item} is described as: #{desc}."
		puts
		print_me = db.execute2 "select * from stock where Product = :item" , item
		print_me.each { |p| puts "%3s %-10s -%-3s- [%5s] %-5s %20s" % [p[0],p[1],p[2],p[3],p[4],p[5]] }
		puts
	rescue SQLite3::Exception => e
		print "error in run " , e
		db.rollback
	ensure
		db.close if db
	end
end

#sell_replenish(new_quant, id_prod) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/simple_inventory_cl.rb', line 97

def sell_replenish(new_quant, id_prod)
	begin
		db = SQLite3::Database.open('inventory.db')
		puts new_quant.class
		db.transaction
		db.execute2 "UPDATE stock SET Level = :new_quant WHERE Id = :id_prod OR Product = :id_prod" , new_quant, id_prod 
		db.commit
		puts db.changes.to_s + " change"
	rescue SQLite3::Exception => e
		puts "error in sell_replenish ", e
		db.rollback
	ensure
		db.close if db
	end
end

#summaryObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/simple_inventory_cl.rb', line 48

def summary
	begin
		db = SQLite3::Database.open('inventory.db')
		return "no match" unless File.exists?('inventory.db')
		all = db.execute2 "SELECT * from stock"
		puts
		puts "this is your current inventory"
		all.each { |p| puts "%3s %-10s -%-3s- [%5s] %-5s %20s " % [p[0],p[1],p[2],p[3],p[4],p[5]]}
		puts
	rescue SQLite3::Exception => e
		puts "error in summary " , e
	ensure
		db.close if db
	end
end