Class: Caboose::OrderPackagesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #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, #under_construction_or_forwarding_domain?, #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/packages



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/caboose/order_packages_controller.rb', line 14

def admin_add
  return if !user_is_allowed('orders', 'add')
  
  resp = StdClass.new
                          
  if    params[:shipping_package_id].strip.length  == 0 then resp.error = "Please select a shipping package."
  elsif params[:shipping_method_id].strip.length   == 0 then resp.error = "Please select a shipping method."      
  else

    op = OrderPackage.new(
      :order_id            => params[:order_id],
      :shipping_package_id => params[:shipping_package_id],
      :shipping_method_id  => params[:shipping_method_id],
      :status              => OrderPackage::STATUS_PENDING          
    )        
    op.save        
    resp.new_id = op.id
    resp.redirect = "/admin/orders/#{params[:order_id]}/packages/#{op.id}"
    
  end
  
  render :json => resp
end

#admin_deleteObject

DELETE /admin/orders/:order_id/packages/:id



110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/caboose/order_packages_controller.rb', line 110

def admin_delete
  return if !user_is_allowed('orders', 'delete')
  resp = StdClass.new
  op = OrderPackage.find(params[:id])
  if op.line_items.nil? || op.line_items.count == 0
    op.destroy
    resp.redirect = "/admin/orders/#{params[:order_id]}"
  else
    resp.error = "Only empty packages can be deleted."
  end
  render :json => resp
end

#admin_jsonObject

GET /admin/orders/:order_id/packages/json



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

def admin_json      
  return if !user_is_allowed('orders', 'view')
  order = Order.find(params[:order_id])
  render :json => order.order_packages.as_json(
    :include => { :shipping_package => { :include => :shipping_methods} }
  )
end

#admin_updateObject

PUT /admin/orders/:order_id/packages/:id



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

def admin_update
  return if !user_is_allowed('orders', 'edit')
  
  resp = Caboose::StdClass.new
  op = OrderPackage.find(params[:id])    
  
  save = true    
  params.each do |name,value|
    case name
      when 'order_id'            then op.order_id              = value
      when 'shipping_method_id'  then op.shipping_method_id    = value
      when 'shipping_package_id' then op.shipping_package_id   = value
      when 'status'              then op.status                = value
      when 'tracking_number'     then op.tracking_number       = value
      when 'total'               then op.total                 = value
      when 'package_method'      then
        arr = value.split('_')
        op.shipping_package_id = arr[0]
        op.shipping_method_id = arr[1]                      
    end
  end
  
  op.save
  op.order.shipping = op.order.calculate_shipping
  op.order.total = op.order.calculate_total
  op.order.save
  
  resp.success = true
  render :json => resp
end

#admin_update_line_itemObject

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



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

def admin_update_line_item
  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 '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
        li.order.tax = TaxCalculator.tax(li.order)            
        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

#calculate_shippingObject

GET /admin/orders/:order_id/packages/:id/calculate-shipping



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/caboose/order_packages_controller.rb', line 124

def calculate_shipping
  return if !user_is_allowed('orders', 'edit')

  op = OrderPackage.find(params[:id])
  order = op.order
  
  render :json => { :error => "Empty order" } and return if order.nil?
  render :json => { :error => "No shippable items in order package" } and return if !order.has_shippable_items?
  render :json => { :error => "Empty shipping address" } and return if order.shipping_address.nil?

  rate = ShippingCalculator.calculate_rate(op)
  render :json => { :error => "No rate found for given shipping package and method" } and return if rate.nil?

  op.total = rate
  op.save
  
  order.calculate_shipping
  order.calculate_total
  order.save
  
  render :json => { :error => "No rate found for shipping method" } and return if rate.nil?                   
  render :json => { :success => true, :rate => rate }            
end

#shipping_ratesObject

GET /admin/orders/:order_id/packages/:id/shipping-rates



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/caboose/order_packages_controller.rb', line 149

def shipping_rates
  return if !user_is_allowed('orders', 'edit')

  op = OrderPackage.find(params[:id])
  order = op.order
  
  render :json => { :error => "Empty order" } and return if order.nil?
  render :json => { :error => "No shippable items in order package" } and return if !order.has_shippable_items?
  render :json => { :error => "Empty shipping address" } and return if order.shipping_address.nil?
                                                    
  rates = ShippingCalculator.order_package_rates(op)      
  render :json => rates            
end