Class: Caboose::ShippingPackagesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#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/shipping-packages



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

def admin_add
  return if !user_is_allowed('sites', 'add')
  
  resp = StdClass.new
                          
  if    params[:inside_length].strip.length  == 0 then resp.error = "Please enter a valid inside length."
  elsif params[:inside_width ].strip.length  == 0 then resp.error = "Please enter a valid inside width."
  elsif params[:inside_height].strip.length  == 0 then resp.error = "Please enter a valid inside height."
  elsif params[:outside_length].strip.length == 0 then resp.error = "Please enter a valid outside length."
  elsif params[:outside_width ].strip.length == 0 then resp.error = "Please enter a valid outside width."
  elsif params[:outside_height].strip.length == 0 then resp.error = "Please enter a valid outside height."
  else

    sp = ShippingPackage.new(
      :site_id         => @site.id,
      :name            => params[:name].strip,
      :inside_length   => params[:inside_length].to_f,
      :inside_width    => params[:inside_width ].to_f,
      :inside_height   => params[:inside_height].to_f,
      :outside_length  => params[:outside_length].to_f,
      :outside_width   => params[:outside_width ].to_f,
      :outside_height  => params[:outside_height].to_f
    )
    sp.volume = sp.inside_length * sp.inside_width * sp.inside_height
    sp.save        
    resp.redirect = "/admin/sites/#{@site.id}/shipping-packages/#{sp.id}"
    
  end
  
  render :json => resp
end

#admin_bulk_addObject

POST /admin/shipping-packages/bulk



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

def admin_bulk_add
  return if !user_is_allowed('sites', 'add')
  
  resp = Caboose::StdClass.new

  i = 0
  CSV.parse(params[:csv_data].strip).each do |row|
       if row[1].nil? || row[0].strip.length == 0 then resp.error = "Inside Length  not defined on row #{i+1}."
    elsif row[2].nil? || row[1].strip.length == 0 then resp.error = "Inside Width   not defined on row #{i+1}."
    elsif row[3].nil? || row[2].strip.length == 0 then resp.error = "Inside Height  not defined on row #{i+1}."
    elsif row[4].nil? || row[3].strip.length == 0 then resp.error = "Outside Length  not defined on row #{i+1}."
    elsif row[5].nil? || row[4].strip.length == 0 then resp.error = "Outside Width   not defined on row #{i+1}."
    elsif row[6].nil? || row[5].strip.length == 0 then resp.error = "Outside Height  not defined on row #{i+1}."
    end
    i = i + 1
  end
  
  if resp.error.nil?
    CSV.parse(params[:csv_data]).each do |row|
      sp = Caboose::ShippingPackage.new(
        :site_id  => @site.id,
        :name => row[0].strip,            
        :inside_length   => row[1].to_f,
        :inside_width    => row[2].to_f,
        :inside_height   => row[3].to_f,
        :outside_length  => row[4].to_f,
        :outside_width   => row[5].to_f,
        :outside_height  => row[6].to_f            
      )                      
      sp.volume = sp.inside_length * sp.inside_width * sp.inside_height
      sp.save
    end
    resp.success = true
  end
  
  render :json => resp
end

#admin_bulk_deleteObject

DELETE /admin/shipping-packages/:id/bulk



200
201
202
203
204
205
206
207
208
209
210
# File 'app/controllers/caboose/shipping_packages_controller.rb', line 200

def admin_bulk_delete
  return if !user_is_allowed('sites', 'delete')
  
  resp = Caboose::StdClass.new
  params[:model_ids].each do |sp_id|
    sp = ShippingPackage.find(sp_id)
    sp.destroy        
  end
  resp.success = true
  render :json => resp
end

#admin_bulk_updateObject

PUT /admin/:shipping-packages/bulk



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

def admin_bulk_update
  return unless user_is_allowed_to 'edit', 'sites'

  resp = Caboose::StdClass.new    
  shipping_packages = params[:model_ids].collect{ |sp_id| ShippingPackage.find(sp_id) }

  save = true
  params.each do |k,v|
    case k
      when 'site_id'            then shipping_packages.each{ |sp| sp.site_id             = v            }          
      when 'name'               then shipping_packages.each{ |sp| sp.name                = v            }
      when 'inside_length'      then shipping_packages.each{ |sp| sp.inside_length       = v.to_f       }
      when 'inside_width'       then shipping_packages.each{ |sp| sp.inside_width        = v.to_f       }
      when 'inside_height'      then shipping_packages.each{ |sp| sp.inside_height       = v.to_f       }
      when 'outside_length'     then shipping_packages.each{ |sp| sp.outside_length      = v.to_f       }
      when 'outside_width'      then shipping_packages.each{ |sp| sp.outside_width       = v.to_f       }
      when 'outside_height'     then shipping_packages.each{ |sp| sp.outside_height      = v.to_f       }
      when 'volume'             then shipping_packages.each{ |sp| sp.height              = v.to_f       }
      when 'empty_weight'       then shipping_packages.each{ |sp| sp.empty_weight        = v.to_f       }
      when 'cylinder'           then shipping_packages.each{ |sp| sp.cylinder            = v.to_i       }
      when 'flat_rate_price'    then shipping_packages.each{ |sp| sp.flat_rate_price     = v.to_f       }
      when 'priority'           then shipping_packages.each{ |sp| sp.priority            = v.to_i       }
      when 'shipping_method_id' then shipping_packages.each{ |sp| sp.toggle_shipping_method(v[0], v[1]) }
    end        
  end
  shipping_packages.each{ |sp| sp.save }

  resp.success = true
  render :json => resp
end

#admin_deleteObject

DELETE /admin/shipping-packages/:id



190
191
192
193
194
195
196
197
# File 'app/controllers/caboose/shipping_packages_controller.rb', line 190

def admin_delete
  return if !user_is_allowed('sites', 'delete')
  sp = ShippingPackage.find(params[:id])
  sp.destroy
  
  resp = StdClass.new({ 'redirect' => "/admin/shipping-packages" })
  render :json => resp
end

#admin_editObject

GET /admin/shipping-packages/:id



42
43
44
45
# File 'app/controllers/caboose/shipping_packages_controller.rb', line 42

def admin_edit
  return if !user_is_allowed('sites', 'edit')
  @shipping_package = ShippingPackage.find(params[:id])      
end

#admin_indexObject

GET /admin/shipping-packages



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

def admin_index
  return if !user_is_allowed('sites', 'view')            
end

#admin_jsonObject

GET /admin/shipping-packages/json



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

def admin_json
  return if !user_is_allowed('sites', 'view')
  
  pager = PageBarGenerator.new(params, {
      'site_id' => @site.id,    		  
		},{
		  'model'          => 'Caboose::ShippingPackage',
	    'sort'			     => 'name',
		  'desc'			     => false,
		  'base_url'		   => "/admin/shipping-packages",
		  'use_url_params' => false
	})
	render :json => {
	  :pages => pager,
	  :models => pager.items.as_json(:include => :shipping_methods)
	}    	      	  
end

#admin_json_singleObject

GET /admin/shipping-packages/:id/json



48
49
50
51
52
# File 'app/controllers/caboose/shipping_packages_controller.rb', line 48

def admin_json_single
  return if !user_is_allowed('sites', 'edit')
  sp = ShippingPackage.find(params[:id])
  render :json => sp.as_json(:include => :shipping_methods)
end

#admin_newObject

GET /admin/shipping-packages/new



36
37
38
39
# File 'app/controllers/caboose/shipping_packages_controller.rb', line 36

def admin_new
  return if !user_is_allowed('sites', 'add')
  @shipping_package = ShippingPackage.new      
end

#admin_package_method_optionsObject

GET /admin/shipping-packages/package-method-options



240
241
242
243
244
245
246
247
248
249
250
# File 'app/controllers/caboose/shipping_packages_controller.rb', line 240

def admin_package_method_options      
  return if !user_is_allowed('sites', 'view')
  options = []
  ShippingPackage.where(:site_id => @site.id).reorder('name').all.each do |sp|
    prefix = sp.name ? sp.name : "#{sp.outside_length}x#{sp.outside_width}x#{sp.outside_height}"
    sp.shipping_methods.each do |sm|
      options << { 'value' => "#{sp.id}_#{sm.id}", 'text' => "#{prefix} - #{sm.carrier} - #{sm.service_name}" }
    end
  end
  render :json => options
end

#admin_shipping_method_optionsObject

GET /admin/shipping-methods/options GET /admin/shipping-packages/:id/shipping-method-options



221
222
223
224
# File 'app/controllers/caboose/shipping_packages_controller.rb', line 221

def admin_shipping_method_options
  options = ShippingMethod.reorder(:carrier, :service_name).all.collect { |sm| { :value => sm.id, :text => sm.service_name }}
  render :json => options              
end

#admin_updateObject

PUT /admin/shipping-packages/:id



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

def admin_update
  return if !user_is_allowed('sites', 'edit')

  resp = StdClass.new     
  sp = ShippingPackage.find(params[:id])

  save = true
  params.each do |name,value|
    case name
      when 'site_id'            then sp.site_id             = value  
      when 'name'               then sp.name                = value
      when 'inside_length'      then sp.inside_length       = value.to_f
      when 'inside_width'       then sp.inside_width        = value.to_f
      when 'inside_height'      then sp.inside_height       = value.to_f
      when 'outside_length'     then sp.outside_length      = value.to_f
      when 'outside_width'      then sp.outside_width       = value.to_f
      when 'outside_height'     then sp.outside_height      = value.to_f
      when 'volume'             then sp.height              = value.to_f
      when 'empty_weight'       then sp.empty_weight        = value.to_f
      when 'cylinder'           then sp.cylinder            = value.to_i
      when 'flat_rate_price'    then sp.flat_rate_price     = value.to_f
      when 'priority'           then sp.priority            = value.to_i
      when 'shipping_method_id' then sp.toggle_shipping_method(value[0], value[1])          
    end
	end
	
	resp.success = save && sp.save
	render :json => resp
end

#before_actionObject



7
8
9
# File 'app/controllers/caboose/shipping_packages_controller.rb', line 7

def before_action
  @page = Page.page_with_uri(request.host_with_port, '/admin')
end

#optionsObject

GET /admin/shipping-packages/options



213
214
215
216
217
# File 'app/controllers/caboose/shipping_packages_controller.rb', line 213

def options
  return if !user_is_allowed('sites', 'view')
  options = ShippingPackage.where(:site_id => @site.id).reorder('service_name').all.collect { |sp| { 'value' => sp.id, 'text' => sp.service_name }}
  render :json => options
end