ref: master
plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
require 'base64' class ShoppingCartPluginController < OrdersPluginController include ShoppingCartPlugin::CartHelper helper ShoppingCartPlugin::CartHelper skip_before_action :verify_authenticity_token, only: :send_request def get config = if cart.nil? { profile_id: params[:profile_id], has_products: false, visible: false, products: []} else { profile_id: cart[:profile_id], profile_short_name: cart_profile.short_name, has_products: (cart[:items].keys.size > 0), visible: visible?, products: products, } end config[:has_previous_orders] = if cart_profile then previous_orders.first.present? else false end render text: config.to_json end def add product = find_product(params[:id]) if product && (profile = validate_same_profile(product)) self.cart = { profile_id: profile.id, items: {} } if self.cart.nil? self.cart[:items][product.id] = 0 if self.cart[:items][product.id].nil? self.cart[:items][product.id] += 1 render text: { ok: true, error: {code: 0}, products: [{ id: product.id, name: product.name, price: get_price(product, profile.environment), description: product.description, picture: product.default_image(:minor), quantity: self.cart[:items][product.id] }] }.to_json end end def remove id = params[:id].to_i if validate_cart_presence && validate_cart_has_product(id) self.cart[:items].delete(id) self.cart = nil if self.cart[:items].empty? render text: { ok: true, error: {code: 0}, product_id: id }.to_json end end def list if validate_cart_presence render text: { ok: true, error: {code: 0}, products: products }.to_json end end def update_quantity quantity = params[:quantity].to_i id = params[:id].to_i if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) product = Product.find(id) self.cart[:items][product.id] = quantity render text: { ok: true, error: {code: 0}, product_id: id, quantity: quantity }.to_json end end def clean self.cart = nil render text: { ok: true, error: {code: 0} }.to_json end # override from OrdersPluginController def repeat unless params[:id].present? @orders = previous_orders.last(5).reverse @orders.each{ |o| o.enable_product_diff } else @order = cart_profile.orders.find params[:id] self.cart = { profile_id: cart_profile.id, items: {} } @order.items.each do |item| next unless item.repeat_product and item.repeat_product.available self.cart[:items][item.repeat_product.id] = item.quantity_consumer_ordered.to_i end self.cart[:repeat_order_id] = @order.id render json: { products: products, } end end def buy @no_design_blocks = true @customer = user || Person.new return redirect_to request.referer || environment.top_url if self.cart.nil? @settings = cart_profile.shopping_cart_settings @cart = cart @profile = cart_profile supplier_delivery = profile.delivery_methods.first @order = build_order self.cart[:items], supplier_delivery last_delivery_option_id = session[:cart][:last_delivery_option_id] if session[:cart] @order.supplier_delivery = profile.delivery_methods.where(id: last_delivery_option_id).first if last_delivery_option_id if repeat_order_id = self.cart[:repeat_order_id] repeat_order = cart_profile.orders.where(id: repeat_order_id).first @order.consumer_delivery_data = repeat_order.consumer_delivery_data if repeat_order end end def send_request order = register_order begin ShoppingCartPlugin::Mailer.customer_notification(order, self.cart[:items]).deliver ShoppingCartPlugin::Mailer.supplier_notification(order, self.cart[:items]).deliver session[:notice] = _('Your order has been sent successfully! You will receive a confirmation e-mail shortly.') @success = true @profile = cart_profile session[:cart] ||= {} session[:cart][:last_delivery_option_id] = order.supplier_delivery_id self.cart = nil rescue ActiveRecord::ActiveRecordError => exception @success = false @error = exception.message end end def visibility render text: visible?.to_json end def show begin self.cart[:visibility] = true render text: { ok: true, message: _('Basket displayed.'), error: {code: 0} }.to_json rescue Exception => exception render text: { ok: false, error: { code: 7, message: exception.message } }.to_json end end def hide begin self.cart[:visibility] = false render text: { ok: true, message: _('Basket hidden.'), error: {code: 0} }.to_json rescue Exception => exception render text: { ok: false, error: { code: 8, message: exception.message } }.to_json end end def update_supplier_delivery @profile = cart_profile supplier_delivery = @profile.delivery_methods.where(id: params[:order][:supplier_delivery_id]).first order = build_order self.cart[:items], supplier_delivery order.supplier_delivery = supplier_delivery total_price = order.total render json: { ok: true, delivery_price: float_to_currency_cart(supplier_delivery.cost(total_price), environment, unit: ''), total_price: float_to_currency_cart(total_price, environment, unit: ''), message: _('Delivery option updated.'), error: {code: 0} } end # must be public def profile cart_profile end protected def validate_same_profile(product) if self.cart && self.cart[:profile_id] && product.profile_id != self.cart[:profile_id] render text: { ok: false, error: { code: 1, message: _("Your basket contains items from '%{profile_name}'. Please empty the basket or checkout before adding items from here.") % {profile_name: cart_profile.short_name} } }.to_json return nil end product.profile end def validate_cart_presence if self.cart.nil? render text: { ok: false, error: { code: 2, message: _("There is no basket.") } }.to_json return false end true end def find_product(id) begin product = Product.find(id) rescue ActiveRecord::RecordNotFound render text: { ok: false, error: { code: 3, message: _("This enterprise doesn't have this product.") } }.to_json return nil end product end def validate_cart_has_product(id) if !self.cart[:items].has_key?(id) render text: { ok: false, error: { code: 4, message: _("The basket doesn't have this product.") } }.to_json return false end true end def validate_item_quantity(quantity) if quantity.to_i < 1 render text: { ok: false, error: { code: 5, message: _("Invalid quantity.") } }.to_json return false end true end def register_order products_list = {}; cart[:items].each do |id, quantity| product = Product.find(id) price = product.price || 0 products_list[id] = {quantity: quantity, price: price, name: product.name} end order = OrdersPlugin::Sale.new order.profile = environment.profiles.find(cart[:profile_id]) order.supplier_delivery = profile.delivery_methods.where(id: params[:order][:supplier_delivery_id]).first order.session_id = session_id unless user order.consumer = user order.source = 'shopping_cart_plugin' order.status = 'ordered' order.products_list = products_list order.consumer_data = params[:order][:consumer_data] order.payment_data = params[:order][:payment_data] order.consumer_delivery_data = params[:order][:consumer_delivery_data] order.save! order end def cart @cart ||= begin cookies[cookie_key] && YAML.load(Base64.decode64(cookies[cookie_key])) || nil end # migrate from old attribute @cart[:profile_id] ||= @cart.delete(:enterprise_id) if @cart and @cart[:enterprise_id].present? @cart end def cart_profile profile_id = if params[:profile_id].present? then params[:profile_id] elsif cart then cart[:profile_id] end @cart_profile ||= environment.profiles.find profile_id rescue nil end # from OrdersPluginController def supplier cart_profile end def cart=(data) @cart = data end after_filter :save_cookie def save_cookie if @cart.nil? # cookie.delete does not work, set to empty value cookies.permanent[cookie_key] = {value: '', path: '/plugin/shopping_cart'} else cookies.permanent[cookie_key] = { value: Base64.encode64(@cart.to_yaml), path: "/plugin/shopping_cart", } end end def cookie_key :_noosfero_plugin_shopping_cart end def visible? !self.cart.has_key?(:visibility) || self.cart[:visibility] end def products self.cart[:items].collect do |id, quantity| product = Product.find_by id: id if product { id: product.id, name: product.name, price: get_price(product, product.profile.environment), description: product.description, picture: product.default_image(:minor), quantity: quantity } else { id: id, name: _('Undefined product'), price: 0, description: _('Wrong product id'), picture: '', quantity: quantity } end end end end |