Class: Caboose::InvoicePackagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/invoice_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



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

def admin_add
  return if !user_is_allowed('invoices', '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 = InvoicePackage.new(
      :invoice_id          => params[:invoice_id],
      :shipping_package_id => params[:shipping_package_id],
      :shipping_method_id  => params[:shipping_method_id],
      :status              => InvoicePackage::STATUS_PENDING          
    )        
    op.save
    
    InvoiceLog.create(
      :invoice_id        => params[:invoice_id],
      :invoice_packag_id => op.id,
      :user_id           => logged_in_user.id,
      :date_logged       => DateTime.now.utc,
      :invoice_action    => InvoiceLog::ACTION_INVOICE_PACKAGE_CREATED                                                                            
    )        
    resp.new_id = op.id
    resp.redirect = "/admin/invoices/#{params[:invoice_id]}/packages/#{op.id}"
    
  end
  
  render :json => resp
end

#admin_deleteObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/caboose/invoice_packages_controller.rb', line 131

def admin_delete
  return if !user_is_allowed('invoices', 'delete')
  resp = StdClass.new
  op = InvoicePackage.find(params[:id])
  if op.line_items.nil? || op.line_items.count == 0
    op.destroy
    InvoiceLog.create(
      :invoice_id         => params[:invoice_id],
      :invoice_package_id => params[:id],
      :user_id            => logged_in_user.id,
      :date_logged        => DateTime.now.utc,
      :invoice_action     => InvoiceLog::ACTION_INVOICE_PACKAGE_DELETED                                                                            
    )
    resp.redirect = "/admin/invoices/#{params[:invoice_id]}"
  else
    resp.error = "Only empty packages can be deleted."
  end
  render :json => resp
end

#admin_jsonObject



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

def admin_json      
  return if !user_is_allowed('invoices', 'view')
  invoice = Invoice.find(params[:invoice_id])
  render :json => invoice.invoice_packages.as_json(
    :include => { :shipping_package => { :include => :shipping_methods} }
  )
end

#admin_optionsObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/controllers/caboose/invoice_packages_controller.rb', line 193

def admin_options
  return if !user_is_allowed('invoices', 'edit')
  
  options = []
  case params[:field]
    when 'status'
      options = [
        { :value => InvoicePackage::STATUS_PENDING       , :text => 'Pending'       },
        { :value => InvoicePackage::STATUS_READY_TO_SHIP , :text => 'Ready to Ship' },
        { :value => InvoicePackage::STATUS_SHIPPED       , :text => 'Shipped'       }            
      ]
  end
  render :json => options
end

#admin_updateObject



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

def admin_update
  return if !user_is_allowed('invoices', 'edit')
  
  resp = Caboose::StdClass.new
  op = InvoicePackage.find(params[:id])    
  
  save = true
  fields_to_log = ['invoice_id','shipping_method_id','shipping_package_id','status','tracking_number','total','package_method']
  params.each do |name,value|
    if fields_to_log.include?(name)              
      InvoiceLog.create(
        :invoice_id         => params[:invoice_id],
        :invoice_package_id => op.id,
        :user_id            => logged_in_user.id,
        :date_logged        => DateTime.now.utc,
        :invoice_action     => InvoiceLog::ACTION_INVOICE_PACKAGE_UPDATED,
        :field              => name,
        :old_value          => op[name.to_sym],
        :new_value          => value
      )                      
    end                
    case name
      when 'invoice_id'          then op.invoice_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.invoice.shipping = op.invoice.calculate_shipping
  op.invoice.total = op.invoice.calculate_total
  op.invoice.save
  
  resp.success = true
  render :json => resp
end

#calculate_shippingObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/caboose/invoice_packages_controller.rb', line 152

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

  op = InvoicePackage.find(params[:id])
  invoice = op.invoice
  
  render :json => { :error => "Empty invoice" } and return if invoice.nil?
  render :json => { :error => "No shippable items in invoice package" } and return if !invoice.has_shippable_items?
  render :json => { :error => "Empty shipping address" } and return if invoice.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
  
  invoice.calculate_shipping
  invoice.calculate_total
  invoice.save
  
  render :json => { :error => "No rate found for shipping method" } and return if rate.nil?                   
  render :json => { :success => true, :rate => rate }            
end

#shipping_ratesObject



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/caboose/invoice_packages_controller.rb', line 177

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

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