Skip to content

Commit 635f6af

Browse files
author
Torsten Ruger
committed
total rework to use orders, basics ok, but some polishing to be done
1 parent 7075b1b commit 635f6af

File tree

9 files changed

+100
-96
lines changed

9 files changed

+100
-96
lines changed

app/controllers/spree/admin/pos_controller.rb

Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
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+
310
def new
411
init
5-
redirect_to :action => :index
12+
redirect_to :action => :show , :number => @order.number
613
end
714

815
def export
916
unless session[:items]
10-
index
17+
show
1118
return
1219
end
1320
missing = []
@@ -16,7 +23,7 @@ def export
1623
end
1724
unless missing.empty?
1825
flash[:error] = "All items must have ean set, missing: #{missing.join(' , ')}"
19-
redirect_to :action => "index"
26+
redirect_to :action => :show
2027
return
2128
end
2229
opt = {}
@@ -54,7 +61,7 @@ def import
5461
end
5562
end
5663
add_notice "Added #{added} products" if added
57-
redirect_to :action => :index
64+
redirect_to :action => :show
5865
end
5966

6067
def inventory
@@ -77,84 +84,69 @@ def inventory
7784
def add
7885
if pid = params[:item]
7986
add_variant Spree::Variant.find pid
87+
flash.notice = t('product_added')
8088
end
81-
redirect_to :action => :index
89+
redirect_to :action => :show
8290
end
8391

8492
def remove
8593
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
93101
end
102+
flash.notice = t('product_removed')
94103
end
95-
redirect_to :action => :index
104+
redirect_to :action => :show
96105
end
97106

98107
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
114110
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 )
128113
payment.save!
129114
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"
136120
end
137121

138122
def index
123+
redirect_to :action => :new
124+
end
125+
126+
def show
139127
if (pid = params[:price]) && request.post?
140128
item = session[:items][pid]
141129
puts "#{session[:items].first[0].class} + item #{item.class}"
142130
item.price = params["price#{pid}"].to_f
143131
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
147136
item.quantity = params[:quantity].to_i
137+
item.save
138+
@order.reload # must be something cached in there, because it doesnt work without. shame.
148139
end
149140
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 )
153144
else
154-
session[:items].each_value do |item|
155-
item.discount( discount )
145+
@order.line_items.each do |item|
146+
item_discount( item , discount )
156147
end
157148
end
149+
@order.reload # must be something cached in there, because it doesnt work without. shame.
158150
end
159151
if sku = params[:sku]
160152
prods = Spree::Variant.where(:sku => sku ).limit(2)
@@ -168,7 +160,11 @@ def index
168160
return
169161
end
170162
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!
172168
end
173169

174170
def find
@@ -179,7 +175,6 @@ def find
179175
search["variants_including_master_sku_contains"] = nil
180176
init_search
181177
end
182-
render :find
183178
end
184179

185180
private
@@ -194,15 +189,21 @@ def add_error no
194189
end
195190

196191
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
199203
end
200204
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)
206207
end
207208

208209
def init_search

app/models/product_decorator.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.

app/models/variant_decorator.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/views/spree/admin/pos/index.html.erb renamed to app/views/spree/admin/pos/show.html.erb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div class="search">
1414
<legend><%= t('add_product') %> </legend>
1515

16-
<%= form_tag "/admin/pos/index" , :id => "product_search" do %>
16+
<%= form_tag "/admin/pos/show" , :id => "product_search" do %>
1717
<input type="hidden" value="1" name="index">
1818
<div class="box">
1919
<p>
@@ -39,7 +39,7 @@
3939
</form>
4040

4141
</div><br/><br/><br/>
42-
<a href="javascript:poptastic('/admin/pos/print');" alt="Print"/>
42+
<a href="javascript:poptastic('/admin/pos/print/<%=@order.number%>');" alt="Print"/>
4343
<%= image_tag "admin/pos/print.png" %>
4444
</a>
4545
<a href="javascript:if (newwindow) newwindow.close()">
@@ -69,7 +69,7 @@
6969
<th><%= t('remove') + "/"+ t('add') %></th>
7070
<th><%= t('discount') %></th>
7171
</tr>
72-
<% items.each_value do | item | %>
72+
<% @order.line_items.each do | item | %>
7373
<tr>
7474
<td width="300">
7575
<%= image_tag item.variant.product.images.first.attachment.url(:mini) if item.variant.product.images.first %>
@@ -93,8 +93,9 @@
9393
</big>
9494
</td>
9595
<td>
96-
<%= link_to image_tag("admin/pos/delete.png", :width =>@pix , :height =>@pix) , "/admin/pos/remove/#{item.id}" %>
97-
<%= link_to image_tag("admin/pos/plus.png", :width =>@pix , :height =>@pix) , "/admin/pos/add/#{item.id}" %> </td>
96+
<%= link_to image_tag("admin/pos/delete.png", :width =>@pix , :height =>@pix) , "/admin/pos/remove/#{@order.number}/#{item.variant.id}" %>
97+
<%= link_to image_tag("admin/pos/plus.png", :width =>@pix , :height =>@pix) ,
98+
"/admin/pos/add/#{@order.number}/#{item.variant.id}" %> </td>
9899
<td>
99100
<big>
100101
<%= form_tag do %>
@@ -112,7 +113,7 @@
112113

113114
<tr class="total">
114115
<td colspan="3"><b><%= t('order_total') %>:</b></td>
115-
<td colspan="2" class="total"><span id="order_total"><%= number_to_currency item_total %></span></td>
116+
<td colspan="2" class="total"><span id="order_total"><%= number_to_currency @order.total %></span></td>
116117
</tr>
117118
</table>
118119
<br/>

config/locales/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
en:
22
pos: POS
3+
product_added: Product added
4+
product_removed: Product removed
5+

config/locales/fi.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
fi:
22
pos: POS
3-
3+
product_added: Tuote lisätty
4+
product_removed: Product poistettu
5+

config/routes.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
Spree::Core::Engine.routes.prepend do
22
namespace :admin do
3+
34
match "pos/new" => "pos#new"
4-
match "pos/export" => "pos#export"
5-
match "pos/import" => "pos#import"
6-
match "pos/add/:item" => "pos#add"
7-
match "pos/remove/:item" => "pos#remove"
8-
match "pos/find" => "pos#find"
9-
match "pos/index" => "pos#index"
10-
match "pos/print" => "pos#print"
11-
match "pos/inventory" => "pos#inventory"
12-
get "pos" , :to => "pos#index"
5+
match "pos/show/:number" => "pos#show"
6+
match "pos/find/search/:number" => "pos#find"
7+
match "pos/add/:number/:item" => "pos#add"
8+
match "pos/remove/:number/:item" => "pos#remove"
9+
match "pos/print/:number" => "pos#print"
10+
# match "pos/export" => "pos#export"
11+
# match "pos/import" => "pos#import"
12+
# match "pos/index" => "pos#index"
13+
# match "pos/inventory" => "pos#inventory"
14+
get "pos" , :to => "pos#new"
1315
end
1416
# match '/admin' => 'admin/pos#index', :as => :admin
1517
end

lib/spree_pos.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
require "spree_pos/engine"
2-

lib/spree_pos/engine.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,17 @@ def self.activate
3939
else
4040
puts "POS: EAN support disabled, run migration to activate"
4141
end
42+
Spree::Variant.class_eval do
4243

44+
def tax_price
45+
(self.price * (1 + self.product.effective_tax_rate)).round(2, BigDecimal::ROUND_HALF_UP)
46+
end
47+
end
48+
Spree::Product.class_eval do
49+
50+
delegate_belongs_to :master, :ean
51+
52+
end
4353
end
4454

4555
config.to_prepare &method(:activate).to_proc

0 commit comments

Comments
 (0)