Class: Caboose::LineItemsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/line_items_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_edit, #admin_index, #admin_json_single, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject

POST /admin/orders/:order_id/line-items



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/caboose/line_items_controller.rb', line 18

def admin_add
  return if !user_is_allowed('orders', 'edit')      
  
  resp = StdClass.new
  v = Variant.find(params[:variant_id])
  li = LineItem.new(
    :order_id   => params[:order_id],
    :variant_id => params[:variant_id],
    :quantity   => 1,
    :price      => v.price,
    :status     => 'pending'                
  )         
  resp.success = li.save
  render :json => resp
end

#admin_deleteObject

DELETE /admin/orders/:order_id/line-items/:id



84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/caboose/line_items_controller.rb', line 84

def admin_delete
  return if !user_is_allowed('orders', 'delete')
  li = LineItem.find(params[:id])
  order = li.order
  li.destroy
  order.calculate_total
  order.save                  
  render :json => Caboose::StdClass.new({
    :redirect => '/admin/orders'
  })
end

#admin_highlightObject

GET /admin/orders/:order_id/line-items/:id/highlight



97
98
99
100
101
102
# File 'app/controllers/caboose/line_items_controller.rb', line 97

def admin_highlight
  return if !user_is_allowed('orders', 'view')
  li = LineItem.find(params[:id])
  v = li.variant
  redirect_to "/admin/products/#{v.product_id}/variants?highlight=#{v.id}"
end

#admin_jsonObject

GET /admin/orders/:id/line-items/json



5
6
7
8
9
# File 'app/controllers/caboose/line_items_controller.rb', line 5

def admin_json
  return if !user_is_allowed('orders', 'edit')    
  order = Order.find(params[:id])
  render :json => order.line_items.as_json(:include => :order_package)
end

#admin_newObject

GET /admin/orders/:order_id/line-items/new



12
13
14
15
# File 'app/controllers/caboose/line_items_controller.rb', line 12

def admin_new
  return if !user_is_allowed('orders', 'edit')      
  render :layout => 'caboose/modal'            
end

#admin_product_stubsObject

GET /admin/line-items/product-stubs



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/caboose/line_items_controller.rb', line 118

def admin_product_stubs      
  title = params[:title].strip.downcase.split(' ')
  render :json => [] and return if title.length == 0      
  where = []
  query = [""]
  title.each do |str|
    where << 'lower(title) like ?'
    query << "%#{str}%"
  end
  where = where.join(' and ')
  query[0] = "select id, title from store_products where #{where} order by title limit 20"          
  rows = ActiveRecord::Base.connection.select_rows(ActiveRecord::Base.send(:sanitize_sql_array, query))
  arr = rows.collect{ |row| { :id => row[0], :title => row[1] }}
  render :json => arr
end

#admin_status_optionsObject

GET /admin/orders/line-items/status-options



105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/caboose/line_items_controller.rb', line 105

def admin_status_options
  arr = ['pending', 'ready to ship', 'shipped', 'backordered', 'canceled']
  options = []
  arr.each do |status|
    options << {
      :value => status,
      :text  => status
    }
  end
  render :json => options
end

#admin_updateObject

PUT /admin/orders/:order_id/line-items/:id



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
# File 'app/controllers/caboose/line_items_controller.rb', line 35

def admin_update
  return if !user_is_allowed('orders', 'edit')
  
  resp = Caboose::StdClass.new({'attributes' => {}})
  li = LineItem.find(params[:id])    
  
  save = true
  send_status_email = false
  params.each do |name,value|        
    case name
      when 'order_id'         then li.order_id          = value
      when 'order_package_id' then li.order_package_id  = value
      when 'variant_id'       then li.variant_id        = value
      when 'parent_id'        then li.parent_id         = value          
      when 'price'            then li.price             = value
      when 'notes'            then li.notes             = value
      when 'custom1'          then li.custom1           = value
      when 'custom2'          then li.custom2           = value
      when 'custom3'          then li.custom3           = value
      when 'quantity'
        li.quantity = value
        li.save
                  	  
        # Recalculate everything
        r = ShippingCalculator.rate(li.order, li.order.shipping_method_code)
        li.order.shipping = r['negotiated_rate'] / 100
        li.order.handling = (r['negotiated_rate'] / 100) * 0.05
        tax_rate = TaxCalculator.tax_rate(li.order.shipping_address)
        li.order.tax = li.order.subtotal * tax_rate
        li.order.calculate_total
        li.order.save
        
      when 'tracking_number'
        li.tracking_number = value
        send_status_email = true
      when 'status'
        li.status = value
        resp.attributes['status'] = {'text' => value}
        send_status_email = true
    end
  end
  if send_status_email       
    OrdersMailer.configure_for_site(@site.id).customer_status_updated(li.order).deliver
  end    
  resp.success = save && li.save
  render :json => resp
end