1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
73
74
75
76
77
78
79
80
81
82
83
84
85
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
class StoresController < ApplicationController
before_filter :check_permissions
respond_to :xml, :html, :json
def live_search
if params[:page]
@page = params[:page]
else
@page = 1
end
if current_user.is_an_admin?
@stores = Store.where('heat > ? AND UPPER(name) LIKE UPPER(?)',-1,"%#{params[:term]}%").order("name ASC").limit(30)
else
@stores = current_user.stores.where('heat != ? AND UPPER(name) LIKE UPPER(?)',-1,"%#{params[:term]}%").order("name ASC").limit(30)
end
respond_to do |format|
format.html {render :layout=>false}
format.json {render json: @stores.map{|x|{:label=>x.name,:value=>x.id}}}
end
end
def index
if params[:page]
@page = params[:page]
else
@page = 1
end
if current_user.is_an_admin?
@stores = Store.where('heat > ?',0).order("updated_at DESC").page(@page)
else
@stores = current_user.stores.where('heat > ?',0).order("updated_at DESC").page(@page)
end
respond_to do |format|
format.html
format.xml { render :xml => @stores }
end
end
def new_from_hr
@store = Store.find_by_highrise_id(params[:id])
unless @store
@hr_company = Highrise::Company.find(params[:id])
@store = Store.create(:highrise_id => params[:id])
@store.heat = 10
@store.name = @hr_company.name
@store.save
end
redirect_to store_path(@store)
end
def show_by_hr_id
@store = Store.find_by_highrise_id(params[:id])
if @store
redirect_to store_path(@store)
else
@hr_company = Highrise::Company.find(params[:id])
respond_to do |format|
format.html
end
end
end
def show
@store = Store.find(params[:id])
if check_store_ownership
respond_to do |format|
format.html
format.xml { render :xml => @store }
end
else
render 'no_permission', :status => 550
end
end
def stock_history
if params[:page]
@page = params[:page]
else
@page = 1
end
@store = Store.find(params[:id])
@store_product_quantity_checks = @store.store_product_quantity_checks.order("created_at ASC").page(@page)
end
def new
@store = Store.new
@store.heat = 10
end
def create
@store = Store.new(params[:store])
@store.heat = 10
if @store.save
StoreProductQuantityCheck.create(:store=>@store, :quantity=>(@store.units_on_hand || 0), :product=>Product.find(:first))
redirect_to @store, :notice => "Successfully created store."
else
render :action => 'new'
end
end
def edit
@store = Store.find(params[:id])
end
def update
@store = Store.find(params[:id])
if params["store"]["units_on_hand"]
StoreProductQuantityCheck.create(:store=>@store, :quantity=>params["store"]["units_on_hand"], :product=>Product.find(:first))
else
StoreProductQuantityCheck.create(:store=>@store, :quantity=>(@store.units_on_hand || 0), :product=>Product.find(:first))
end
@store.update_attributes(params[:store])
respond_with @user
end
def destroy
@store = Store.find(params[:id])
@store.destroy
redirect_to stores_url, :notice => "Successfully destroyed store."
end
def update_units_on_hand
@store = Store.find(params[:id])
@store.updated_at = Time.now()
@store.update_attributes(params[:store])
StoreProductQuantityCheck.create(:store=>@store, :quantity=>(@store.units_on_hand || 0), :product=>Product.find(:first))
@store.save
respond_to do |format|
format.json { respond_with_bip(@store) }
end
end
private
def check_store_ownership
if current_user.is_an_admin? or current_user.stores.include?(@store)
return true
else
return false
end
end
end |