Class: Caboose::InvoicePackagesController
Instance Method Summary
collapse
#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_add ⇒ Object
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_delete ⇒ Object
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_json ⇒ Object
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_update ⇒ Object
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
|
#calculate_shipping ⇒ Object
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_rates ⇒ Object
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
|