Class: Caboose::OrdersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/orders_controller.rb,
app/controllers/caboose/order_reports_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #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



44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/caboose/orders_controller.rb', line 44

def admin_add
  return if !user_is_allowed('orders', 'add')
  order = Order.create(
    :site_id => @site.id,
    :status => Order::STATUS_PENDING,                          
    :financial_status => Order::FINANCIAL_STATUS_PENDING,
    :order_number => @site.store_config.next_order_number
  )    
  render :json => { :sucess => true, :redirect => "/admin/orders/#{order.id}" }
end

#admin_calculate_handlingObject

GET /admin/orders/:id/calculate-handling



78
79
80
81
82
83
84
85
# File 'app/controllers/caboose/orders_controller.rb', line 78

def admin_calculate_handling
  return if !user_is_allowed('orders', 'edit')
  order = Order.find(params[:id])
  order.handling = order.calculate_handling
  order.total = order.calculate_total
  order.save
  render :json => { :success => true }      
end

#admin_calculate_taxObject

GET /admin/orders/:id/calculate-tax



68
69
70
71
72
73
74
75
# File 'app/controllers/caboose/orders_controller.rb', line 68

def admin_calculate_tax
  return if !user_is_allowed('orders', 'edit')
  order = Order.find(params[:id])
  order.tax = order.calculate_tax
  order.total = order.calculate_total
  order.save
  render :json => { :success => true }      
end

#admin_deleteObject

DELETE /admin/orders/:id



202
203
204
205
206
207
208
# File 'app/controllers/caboose/orders_controller.rb', line 202

def admin_delete
  return if !user_is_allowed('orders', 'delete')
  Order.find(params[:id]).destroy
  render :json => Caboose::StdClass.new({
    :redirect => '/admin/orders'
  })
end

#admin_editObject

GET /admin/orders/:id



56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/caboose/orders_controller.rb', line 56

def admin_edit
  return if !user_is_allowed('orders', 'edit')
  @order = Order.where(:id => params[:id]).first

  if params[:id].nil? || @order.nil?
    render :file => 'caboose/orders/admin_invalid_order', :layout => 'caboose/admin'
    return
  end            
  render :layout => 'caboose/admin'
end

#admin_google_feedObject

GET /admin/orders/google-feed



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'app/controllers/caboose/orders_controller.rb', line 256

def admin_google_feed
  d2 = DateTime.now
  d1 = DateTime.now
  if Caboose::Setting.exists?(:name => 'google_feed_date_last_submitted')                  
    d1 = Caboose::Setting.where(:name => 'google_feed_date_last_submitted').first.value      
    d1 = DateTime.parse(d1)
  elsif Order.exists?("status = ? and date_authorized is not null", Order::STATUS_SHIPPED)
    d1 = Order.where("status = ? and date_authorized is not null", Order::STATUS_SHIPPED).reorder("date_authorized DESC").limit(1).pluck('date_authorized')
    d1 = DateTime.parse(d1)
  end
  
  # Google Feed Docs
  # https://support.google.com/trustedstoresmerchant/answer/3272612?hl=en&ref_topic=3272286?hl=en
  tsv = ["merchant order id\ttracking number\tcarrier code\tother carrier name\tship date"]            
  if Order.exists?("status = ? and date_authorized > '#{d1.strftime("%F %T")}'", Order::STATUS_SHIPPED)
    Order.where("status = ? and date_authorized > ?", Order::STATUS_SHIPPED, d1).reorder(:id).all.each do |order|
      tracking_numbers = order.line_items.collect{ |li| li.tracking_number }.compact.uniq
      tn = tracking_numbers && tracking_numbers.count >= 1 ? tracking_numbers[0] : ""
      tsv << "#{order.id}\t#{tn}\tUPS\t\t#{order.date_shipped.strftime("%F")}"                              
    end
  end
  
  # Save when we made the last call
  setting = if Caboose::Setting.exists?(:name => 'google_feed_date_last_submitted')
    Caboose::Setting.where(:name => 'google_feed_date_last_submitted').first
  else
    Caboose::Setting.new(:name => 'google_feed_date_last_submitted')
  end
  
  setting.value = d2.strftime("%F %T")
  setting.save            
               
  # Print out the lines
  render :text => tsv.join("\n")
end

#admin_indexObject

GET /admin/orders



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

def admin_index
  return if !user_is_allowed('orders', 'view')
  
  @pager = Caboose::PageBarGenerator.new(params, {
    'site_id'              => @site.id,
    'customer_id'          => '', 
    'status'               => Order::STATUS_PENDING,
    'shipping_method_code' => '',
    'id'                   => ''
  }, {
    'model'          => 'Caboose::Order',
    'sort'           => 'id',
    'desc'           => 1,
    'base_url'       => '/admin/orders',
    'use_url_params' => false,
    'items_per_page' => 100
  })
  
  @orders    = @pager.items
  @customers = Caboose::User.where(:site_id => @site.id).reorder('last_name, first_name').all
  
  render :layout => 'caboose/admin'
end

#admin_jsonObject

GET /admin/orders/:id/json



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

def admin_json
  return if !user_is_allowed('orders', 'edit')    
  order = Order.find(params[:id])
  if order.shipping_address_id.nil?
    sa = Address.create
    order.shipping_address_id = sa.id
    order.save
  end
  render :json => order.as_json(:include => [        
    { :line_items => { :include => { :variant => { :include => :product }}}},
    { :order_packages => { :include => [:shipping_package, :shipping_method] }},
    { :discounts => { :include => :gift_card }},
    :customer,
    :shipping_address,
    :billing_address,
    :order_transactions
  ])
end

#admin_mail_test_gmailObject

GET /admin/orders/test-gmail



250
251
252
253
# File 'app/controllers/caboose/orders_controller.rb', line 250

def admin_mail_test_gmail
  TestMailer.test_gmail.deliver
  render :text => "Sent email to [email protected] on #{DateTime.now.strftime("%F %T")}"
end

#admin_mail_test_infoObject

GET /admin/orders/test-info



244
245
246
247
# File 'app/controllers/caboose/orders_controller.rb', line 244

def admin_mail_test_info
  TestMailer.test_info.deliver
  render :text => "Sent email to [email protected] on #{DateTime.now.strftime("%F %T")}"
end

#admin_newObject

GET /admin/orders/new



38
39
40
41
# File 'app/controllers/caboose/orders_controller.rb', line 38

def admin_new
  return if !user_is_allowed('orders', 'add')      
  render :layout => 'caboose/admin'
end

#admin_printObject

GET /admin/orders/:id/print.pdf



147
148
149
150
151
152
153
# File 'app/controllers/caboose/orders_controller.rb', line 147

def admin_print
  return if !user_is_allowed('orders', 'edit')           
  
  pdf = OrderPdf.new
  pdf.order = Order.find(params[:id])             
  send_data pdf.to_pdf, :filename => "order_#{pdf.order.id}.pdf", :type => "application/pdf", :disposition => "inline"   
end

#admin_print_pendingObject

GET /admin/orders/print-pending



156
157
158
159
160
161
162
# File 'app/controllers/caboose/orders_controller.rb', line 156

def admin_print_pending
  return if !user_is_allowed('orders', 'edit')    
  
  pdf = PendingOrdersPdf.new
  pdf.orders = Order.where(:site_id => @site.id, :status => Order::STATUS_PENDING).all      
  send_data pdf.to_pdf, :filename => "pending_orders.pdf", :type => "application/pdf", :disposition => "inline"            
end

#admin_refundObject

GET /admin/orders/:id/refund



108
109
110
111
112
113
114
115
# File 'app/controllers/caboose/orders_controller.rb', line 108

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

  order = Order.find(params[:id])
  resp = order.refund 
  
  render :json => resp            
end

#admin_resend_confirmationObject

POST /admin/orders/:id/resend-confirmation



118
119
120
121
122
123
124
# File 'app/controllers/caboose/orders_controller.rb', line 118

def admin_resend_confirmation
  if Order.find(params[:id]).resend_confirmation
    render :json => { success: "Confirmation re-sent successfully." }
  else
    render :json => { error: "There was an error re-sending the email." }
  end
end

#admin_send_for_authorizationObject

GET /admin/orders/:id/send-for-authorization



211
212
213
214
215
216
# File 'app/controllers/caboose/orders_controller.rb', line 211

def admin_send_for_authorization
  return if !user_is_allowed('orders', 'edit')
  order = Order.find(params[:id])
  order.delay.send_payment_authorization_email      
  render :json => { :success => true }
end

#admin_status_optionsObject

GET /admin/orders/status-options



230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/controllers/caboose/orders_controller.rb', line 230

def admin_status_options
  return if !user_is_allowed('orders', 'view')
  statuses = [
    Order::STATUS_CART, 
    Order::STATUS_PENDING, 
    Order::STATUS_READY_TO_SHIP, 
    Order::STATUS_SHIPPED, 
    Order::STATUS_CANCELED
  ]
  options = statuses.collect{ |s| { 'text' => s.capitalize, 'value' => s }}       
  render :json => options    
end

#admin_summary_reportObject

GET /admin/orders/summary-report



219
220
221
222
223
224
225
226
227
# File 'app/controllers/caboose/orders_controller.rb', line 219

def admin_summary_report
  return if !user_is_allowed('orders', 'view')

  @d1 = params[:d1] ? DateTime.strptime("#{params[:d1]} 00:00:00", '%Y-%m-%d %H:%M:%S') : DateTime.strptime(DateTime.now.strftime("%Y-%m-01 00:00:00"), '%Y-%m-%d %H:%M:%S')
  @d2 = params[:d2] ? DateTime.strptime("#{params[:d2]} 00:00:00", '%Y-%m-%d %H:%M:%S') : @d1 + 1.month      
  @rows = OrderReporter.summary_report(@site.id, @d1, @d2)
  
  render :layout => 'caboose/admin'    
end

#admin_updateObject

PUT /admin/orders/:id



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/controllers/caboose/orders_controller.rb', line 165

def admin_update
  return if !user_is_allowed('orders', 'edit')
  
  resp = Caboose::StdClass.new({'attributes' => {}})
  order = Order.find(params[:id])    
  
  save = true    
  params.each do |name,value|
    case name
      when 'tax'             then 
        order.tax = value
        order.total = order.calculate_total          
      when 'handling'        then
        order.handling = value
        order.total = order.calculate_total
      when 'custom_discount' then 
        order.custom_discount = value
        order.discount = order.calculate_discount
        order.total = order.calculate_total
      when 'status'          then
        order.status = value
        if value == 'Shipped'
          order.date_shipped = DateTime.now.utc
        end            
      when 'customer_id'     then order.customer_id     = value            
    end
  end

  #order.calculate
  #order.calculate_total
  #resp.attributes['total'] = { 'value' => order.total }
  
  resp.success = save && order.save
  render :json => resp
end

#admin_voidObject

GET /admin/orders/:id/void



98
99
100
101
102
103
104
105
# File 'app/controllers/caboose/orders_controller.rb', line 98

def admin_void
  return if !user_is_allowed('orders', 'edit')
        
  order = Order.find(params[:id])
  resp = order.void

  render :json => resp
end

#admin_weird_testObject

GET /admin/orders/weird-test



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

def admin_weird_test
  Caboose.log("Before the admin_weird_test")
  x = Order.new
  Caboose.log("After the admin_weird_test")
  render :json => x      
end

#capture_fundsObject

GET /admin/orders/:id/capture



88
89
90
91
92
93
94
95
# File 'app/controllers/caboose/orders_controller.rb', line 88

def capture_funds
  return if !user_is_allowed('orders', 'edit')
       
  order = Order.find(params[:id])
  resp = order.capture_funds      
  
  render :json => resp
end