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
# 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        
    resp.new_id = op.id
    resp.redirect = "/admin/invoices/#{params[:invoice_id]}/packages/#{op.id}"
    
  end
  
  render :json => resp
end

#admin_deleteObject



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

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
    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_updateObject



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/invoice_packages_controller.rb', line 39

def admin_update
  return if !user_is_allowed('invoices', 'edit')
  
  resp = Caboose::StdClass.new
  op = InvoicePackage.find(params[:id])    
  
  save = true    
  params.each do |name,value|
    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

#admin_update_line_itemObject



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/invoice_packages_controller.rb', line 71

def admin_update_line_item
  return if !user_is_allowed('invoices', '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.invoice, li.invoice.shipping_method_code)
        li.invoice.shipping = r['negotiated_rate'] / 100
        li.invoice.handling = (r['negotiated_rate'] / 100) * 0.05
        li.invoice.tax = TaxCalculator.tax(li.invoice)            
        li.invoice.calculate_total
        li.invoice.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       
    InvoicesMailer.configure_for_site(@site.id).customer_status_updated(li.invoice).deliver
  end
  resp.success = save && li.save
  render :json => resp
end

#calculate_shippingObject



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/invoice_packages_controller.rb', line 124

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



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

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