1
1
class Spree ::Admin ::PosController < Spree ::Admin ::BaseController
2
-
2
+ before_filter :get_order , :except => :new
3
+
4
+ def get_order
5
+ order_number = params [ :number ]
6
+ @order = Spree ::Order . find_by_number ( order_number )
7
+ raise "No order found for -#{ order_number } -" unless @order
8
+ end
9
+
3
10
def new
4
11
init
5
- redirect_to :action => :index
12
+ redirect_to :action => :show , :number => @order . number
6
13
end
7
14
8
15
def export
9
16
unless session [ :items ]
10
- index
17
+ show
11
18
return
12
19
end
13
20
missing = [ ]
@@ -16,7 +23,7 @@ def export
16
23
end
17
24
unless missing . empty?
18
25
flash [ :error ] = "All items must have ean set, missing: #{ missing . join ( ' , ' ) } "
19
- redirect_to :action => "index"
26
+ redirect_to :action => :show
20
27
return
21
28
end
22
29
opt = { }
@@ -54,7 +61,7 @@ def import
54
61
end
55
62
end
56
63
add_notice "Added #{ added } products" if added
57
- redirect_to :action => :index
64
+ redirect_to :action => :show
58
65
end
59
66
60
67
def inventory
@@ -77,84 +84,69 @@ def inventory
77
84
def add
78
85
if pid = params [ :item ]
79
86
add_variant Spree ::Variant . find pid
87
+ flash . notice = t ( 'product_added' )
80
88
end
81
- redirect_to :action => :index
89
+ redirect_to :action => :show
82
90
end
83
91
84
92
def remove
85
93
if pid = params [ :item ]
86
- if ( item = session [ :items ] [ pid ] )
87
- if item . quantity > 1
88
- item . quantity = item . quantity - 1
89
- else
90
- session [ :items ] . delete ( pid )
91
- end
92
- flash . notice = t ( 'product_removed' )
94
+ variant = Spree :: Variant . find ( pid )
95
+ line_item = @order . line_items . find { | line_item | line_item . variant_id == variant . id }
96
+ line_item . quantity -= 1
97
+ if line_item . quantity == 0
98
+ @order . line_items . delete line_item
99
+ else
100
+ line_item . save
93
101
end
102
+ flash . notice = t ( 'product_removed' )
94
103
end
95
- redirect_to :action => :index
104
+ redirect_to :action => :show
96
105
end
97
106
98
107
def print
99
- order_id = session [ :pos_order ]
100
- if order_id
101
- order = Spree ::Order . find order_id
102
- order . line_items . clear
103
- else
104
- order = Spree ::Order . new
105
- order . user = current_user
106
- order . email = current_user . email
107
- order . save!
108
- if id_or_name = Spree ::Config [ :pos_shipping ]
109
- method = Spree ::ShippingMethod . find_by_name id_or_name
110
- method = Spree ::ShippingMethod . find_by_id ( id_or_name ) unless method
111
- end
112
- order . shipping_method = method || Spree ::ShippingMethod . first
113
- order . create_shipment!
108
+ unless @order . payment_ids . empty?
109
+ @order . payments . first . delete
114
110
end
115
- session [ :items ] . each_value do |item |
116
- puts "Variant #{ item . variant . name } #{ item . id } "
117
- new_item = Spree ::LineItem . new ( :quantity => item . quantity )
118
- new_item . variant_id = item . id
119
- puts "PRICE #{ item . no_tax_price } #{ item . no_tax_price . class } "
120
- new_item . price = item . no_tax_price
121
- order . line_items << new_item
122
- end
123
- if order_id
124
- order . payment . delete
125
- end
126
- payment = Spree ::Payment . new ( :payment_method => Spree ::PaymentMethod . find_by_type ( "PaymentMethod::Check" ) ,
127
- :amount => order . total , :order_id => order . id )
111
+ payment = Spree ::Payment . new ( :payment_method => Spree ::PaymentMethod . find_by_type ( "Spree::PaymentMethod::Check" ) ,
112
+ :amount => @order . total , :order_id => @order . id )
128
113
payment . save!
129
114
payment . payment_source . capture ( payment )
130
- order . state = "complete"
131
- order . completed_at = Time . now
132
- order . finalize!
133
- order . save!
134
- session [ :pos_order ] = order . id
135
- redirect_to "/admin/invoice/#{ order . number } /receipt"
115
+ @order . state = "complete"
116
+ @order . completed_at = Time . now
117
+ @order . finalize!
118
+ @order . save!
119
+ redirect_to "/admin/invoice/#{ @order . number } /receipt"
136
120
end
137
121
138
122
def index
123
+ redirect_to :action => :new
124
+ end
125
+
126
+ def show
139
127
if ( pid = params [ :price ] ) && request . post?
140
128
item = session [ :items ] [ pid ]
141
129
puts "#{ session [ :items ] . first [ 0 ] . class } + item #{ item . class } "
142
130
item . price = params [ "price#{ pid } " ] . to_f
143
131
end
144
- if ( pid = params [ :quantity_id ] ) && request . post?
145
- item = session [ :items ] [ pid ]
146
- puts "#{ session [ :items ] . first [ 0 ] . class } + item #{ item . class } "
132
+ if params [ :quantity_id ] && request . post?
133
+ iid = params [ :quantity_id ] . to_i
134
+ item = @order . line_items . find { |line_item | line_item . id == iid }
135
+ #TODO error handling
147
136
item . quantity = params [ :quantity ] . to_i
137
+ item . save
138
+ @order . reload # must be something cached in there, because it doesnt work without. shame.
148
139
end
149
140
if discount = params [ :discount ]
150
- if params [ :item ]
151
- item = session [ :items ] [ params [ :item ] ]
152
- item . discount ( discount )
141
+ if i_id = params [ :item ]
142
+ item = @order . line_items . find { | line_item | line_item . id . to_s == i_id }
143
+ item_discount ( item , discount )
153
144
else
154
- session [ :items ] . each_value do |item |
155
- item . discount ( discount )
145
+ @order . line_items . each do |item |
146
+ item_discount ( item , discount )
156
147
end
157
148
end
149
+ @order . reload # must be something cached in there, because it doesnt work without. shame.
158
150
end
159
151
if sku = params [ :sku ]
160
152
prods = Spree ::Variant . where ( :sku => sku ) . limit ( 2 )
@@ -168,7 +160,11 @@ def index
168
160
return
169
161
end
170
162
end
171
- render :index
163
+ end
164
+
165
+ def item_discount item , discount
166
+ item . price = item . variant . price * ( 1.0 - discount . to_f /100.0 )
167
+ item . save!
172
168
end
173
169
174
170
def find
@@ -179,7 +175,6 @@ def find
179
175
search [ "variants_including_master_sku_contains" ] = nil
180
176
init_search
181
177
end
182
- render :find
183
178
end
184
179
185
180
private
@@ -194,15 +189,21 @@ def add_error no
194
189
end
195
190
196
191
def init
197
- session [ :items ] = { }
198
- session [ :pos_order ] = nil
192
+ @order = Spree ::Order . new
193
+ @order . user = current_user
194
+ @order . email = current_user . email
195
+ @order . save!
196
+ if id_or_name = "1647757474" #Spree::Config[:pos_shipping]
197
+ method = Spree ::ShippingMethod . find_by_name id_or_name
198
+ method = Spree ::ShippingMethod . find_by_id ( id_or_name ) unless method
199
+ end
200
+ @order . shipping_method = method || Spree ::ShippingMethod . first
201
+ @order . create_shipment!
202
+ session [ :pos_order ] = @order . number
199
203
end
200
204
def add_variant var , quant = 1
201
- session [ :items ] = { } unless session [ :items ]
202
- item = session [ :items ] [ var . id . to_s ] || PosItem . new ( var )
203
- item . quantity = item . quantity + quant . to_i
204
- session [ :items ] [ var . id . to_s ] = item
205
- #flash.notice = t('product_added')
205
+ init unless @order
206
+ @order . add_variant ( var , quant )
206
207
end
207
208
208
209
def init_search
0 commit comments