Commit 553c713a991ef2eaa38320dded283bf9adb093fa
1 parent
8849f0f9
Exists in
master
and in
28 other branches
Controller and fixes
* Feature: - Controller for cart
Showing
1 changed file
with
162 additions
and
0 deletions
Show diff stats
plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb
0 → 100644
@@ -0,0 +1,162 @@ | @@ -0,0 +1,162 @@ | ||
1 | +include ActionView::Helpers::NumberHelper | ||
2 | + | ||
3 | +class ShoppingCartPluginProfileController < ProfileController | ||
4 | + | ||
5 | + def add | ||
6 | + session[:cart] = { :enterprise_id => profile.id, :items => {} } if session[:cart].nil? | ||
7 | + if validate_same_enterprise && product = validate_enterprise_has_product(params[:id]) | ||
8 | + session[:cart][:items][product.id] = 0 if session[:cart][:items][product.id].nil? | ||
9 | + session[:cart][:items][product.id] += 1 | ||
10 | + render :text => { | ||
11 | + :ok => true, | ||
12 | + :error => {:code => 0}, | ||
13 | + :products => [{ | ||
14 | + :id => product.id, | ||
15 | + :name => product.name, | ||
16 | + :price => get_price(product), | ||
17 | + :description => product.description, | ||
18 | + :picture => product.default_image(:minor), | ||
19 | + :quantity => session[:cart][:items][product.id] | ||
20 | + }] | ||
21 | + }.to_json | ||
22 | + end | ||
23 | + end | ||
24 | + | ||
25 | + def remove | ||
26 | + id = params[:id].to_i | ||
27 | + if validate_cart_has_product(id) | ||
28 | + session[:cart][:items].delete(id) | ||
29 | + session[:cart] = nil if session[:cart][:items].empty? | ||
30 | + render :text => { | ||
31 | + :ok => true, | ||
32 | + :error => {:code => 0}, | ||
33 | + :product_id => id | ||
34 | + }.to_json | ||
35 | + end | ||
36 | + end | ||
37 | + | ||
38 | + def list | ||
39 | + if validate_cart_presence | ||
40 | + products = session[:cart][:items].collect do |id, quantity| | ||
41 | + product = Product.find(id) | ||
42 | + { :id => product.id, | ||
43 | + :name => product.name, | ||
44 | + :price => get_price(product), | ||
45 | + :description => product.description, | ||
46 | + :picture => product.default_image(:minor), | ||
47 | + :quantity => quantity | ||
48 | + } | ||
49 | + end | ||
50 | + render :text => { | ||
51 | + :ok => true, | ||
52 | + :error => {:code => 0}, | ||
53 | + :enterprise => Enterprise.find(session[:cart][:enterprise_id]).identifier, | ||
54 | + :products => products | ||
55 | + }.to_json | ||
56 | + end | ||
57 | + end | ||
58 | + | ||
59 | + def update_quantity | ||
60 | + quantity = params[:quantity].to_i | ||
61 | + id = params[:id].to_i | ||
62 | + if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) | ||
63 | + product = Product.find(id) | ||
64 | + session[:cart][:items][product.id] = quantity | ||
65 | + render :text => { | ||
66 | + :ok => true, | ||
67 | + :error => {:code => 0}, | ||
68 | + :product_id => id, | ||
69 | + :quantity => quantity | ||
70 | + }.to_json | ||
71 | + end | ||
72 | + end | ||
73 | + | ||
74 | + def clean | ||
75 | + session[:cart] = nil | ||
76 | + render :text => { | ||
77 | + :ok => true, | ||
78 | + :error => {:code => 0} | ||
79 | + }.to_json | ||
80 | + end | ||
81 | + | ||
82 | + private | ||
83 | + | ||
84 | + def validate_same_enterprise | ||
85 | + if profile.id != session[:cart][:enterprise_id] | ||
86 | + render :text => { | ||
87 | + :ok => false, | ||
88 | + :error => { | ||
89 | + :code => 1, | ||
90 | + :message => _("Can't join items from different enterprises.") | ||
91 | + } | ||
92 | + }.to_json | ||
93 | + return false | ||
94 | + end | ||
95 | + true | ||
96 | + end | ||
97 | + | ||
98 | + def validate_cart_presence | ||
99 | + if session[:cart].nil? | ||
100 | + render :text => { | ||
101 | + :ok => false, | ||
102 | + :error => { | ||
103 | + :code => 2, | ||
104 | + :message => _("There is no cart.") | ||
105 | + } | ||
106 | + }.to_json | ||
107 | + return false | ||
108 | + end | ||
109 | + true | ||
110 | + end | ||
111 | + | ||
112 | + def validate_enterprise_has_product(id) | ||
113 | + begin | ||
114 | + product = profile.products.find(id) | ||
115 | + rescue | ||
116 | + render :text => { | ||
117 | + :ok => false, | ||
118 | + :error => { | ||
119 | + :code => 3, | ||
120 | + :message => _("This enterprise doesn't have this product.") | ||
121 | + } | ||
122 | + }.to_json | ||
123 | + return nil | ||
124 | + end | ||
125 | + product | ||
126 | + end | ||
127 | + | ||
128 | + def validate_cart_has_product(id) | ||
129 | + if !session[:cart][:items].has_key?(id) | ||
130 | + render :text => { | ||
131 | + :ok => false, | ||
132 | + :error => { | ||
133 | + :code => 4, | ||
134 | + :message => _("The cart doesn't have this product.") | ||
135 | + } | ||
136 | + }.to_json | ||
137 | + return false | ||
138 | + end | ||
139 | + true | ||
140 | + end | ||
141 | + | ||
142 | + def validate_item_quantity(quantity) | ||
143 | + if quantity.to_i < 1 | ||
144 | + render :text => { | ||
145 | + :ok => false, | ||
146 | + :error => { | ||
147 | + :code => 5, | ||
148 | + :message => _("Invalid quantity.") | ||
149 | + } | ||
150 | + }.to_json | ||
151 | + return false | ||
152 | + end | ||
153 | + true | ||
154 | + end | ||
155 | + | ||
156 | + def get_price(product) | ||
157 | + float_to_currency( product.discount ? product.price_with_discount : product.price ) | ||
158 | + end | ||
159 | + | ||
160 | + | ||
161 | +end | ||
162 | + |