Class: Caboose::LineItemsController

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



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

def admin_add
  return if !user_is_allowed('invoices', 'edit')      
  
  resp = StdClass.new
  v = Variant.find(params[:variant_id])
  li = LineItem.create(
    :invoice_id => params[:invoice_id],
    :variant_id => params[:variant_id],
    :quantity   => 1,
    :unit_price => v.price,
    :subtotal   => v.price,
    :status     => LineItem::STATUS_PENDING                
  )
  resp.success = true
  resp.new_id = li.id
        
  InvoiceLog.create(
    :invoice_id     => params[:invoice_id],
    :line_item_id   => li.id,
    :user_id        => logged_in_user.id,
    :date_logged    => DateTime.now.utc,
    :invoice_action => InvoiceLog::ACTION_LINE_ITEM_CREATED                                                                          
  )      
  render :json => resp
end

#admin_deleteObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/caboose/line_items_controller.rb', line 110

def admin_delete
  return if !user_is_allowed('invoices', 'delete')
  li = LineItem.find(params[:id])      
  li.destroy
  
  invoice = Invoice.find(params[:invoice_id])
  invoice.subtotal = invoice.calculate_subtotal      
  invoice.total    = invoice.calculate_total
  invoice.save
  
  InvoiceLog.create(
    :invoice_id     => params[:invoice_id],
    :line_item_id   => params[:id],
    :user_id        => logged_in_user.id,
    :date_logged    => DateTime.now.utc,
    :invoice_action => InvoiceLog::ACTION_LINE_ITEM_DELETED                                                                          
  )
  
  render :json => Caboose::StdClass.new({
    :redirect => '/admin/invoices'
  })
end

#admin_highlightObject



134
135
136
137
138
139
# File 'app/controllers/caboose/line_items_controller.rb', line 134

def admin_highlight
  return if !user_is_allowed('invoices', 'view')
  li = LineItem.find(params[:id])
  v = li.variant
  redirect_to "/admin/products/#{v.product_id}/variants?highlight=#{v.id}"
end

#admin_jsonObject



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

def admin_json
  return if !user_is_allowed('invoices', 'edit')    
  invoice = Invoice.find(params[:id])
  render :json => invoice.line_items.as_json(:include => :invoice_package)
end

#admin_newObject



13
14
15
16
# File 'app/controllers/caboose/line_items_controller.rb', line 13

def admin_new
  return if !user_is_allowed('invoices', 'edit')      
  render :layout => 'caboose/modal'            
end

#admin_product_stubsObject



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

def admin_product_stubs      
  title = params[:title].strip.downcase.split(' ')
  render :json => [] and return if title.length == 0
  
  where = ["site_id = ?"]      
  vars = [@site.id]
  title.each do |str|
    where << 'lower(title) like ?'
    vars << "%#{str}%"
  end      
  where = where.join(' and ')
  query = ["select id, title, option1, option2, option3 from store_products where #{where} order by title limit 20"]
  vars.each{ |v| query << v }
  
  rows = ActiveRecord::Base.connection.select_rows(ActiveRecord::Base.send(:sanitize_sql_array, query))
  arr = rows.collect do |row|
    has_options = row[2] || row[3] || row[4] ? true : false
    variant_id = nil
    if !has_options
      v = Variant.where(:product_id => row[0].to_i, :status => 'Active').first
      variant_id = v.id if v
    end          
    { :id => row[0], :title => row[1], :variant_id => variant_id }
  end        
  render :json => arr
end

#admin_status_optionsObject



142
143
144
145
146
147
148
149
150
# File 'app/controllers/caboose/line_items_controller.rb', line 142

def admin_status_options      
  options = [
    { :value => LineItem::STATUS_PENDING       , :text => 'Pending'       },        
    { :value => LineItem::STATUS_BACKORDERED   , :text => 'Backordered'   },
    { :value => LineItem::STATUS_CANCELED      , :text => 'Canceled'      },
    { :value => LineItem::STATUS_PROCESSED     , :text => 'Processed'     }
  ]          
  render :json => options
end

#admin_updateObject



46
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/caboose/line_items_controller.rb', line 46

def admin_update
  return if !user_is_allowed('invoices', 'edit')
  
  resp = Caboose::StdClass.new({'attributes' => {}})
  li = LineItem.find(params[:id])    
  
  save = true
  send_status_email = false
  fields_to_log = ['invoice_id','invoice_package_id','variant_id','parent_id','notes','custom1','custom2','custom3','unit_price','quantity','tracking_number','status']
  params.each do |name,value|
    if fields_to_log.include?(name)                
      InvoiceLog.create(
        :invoice_id     => params[:invoice_id],
        :line_item_id   => li.id,
        :user_id        => logged_in_user.id,
        :date_logged    => DateTime.now.utc,
        :invoice_action => InvoiceLog::ACTION_LINE_ITEM_UPDATED,
        :field          => name,
        :old_value      => li[name.to_sym],
        :new_value      => value
      )
    end                
    case name
      when 'invoice_id'         then li.invoice_id          = value
      when 'invoice_package_id' then li.invoice_package_id  = value
      when 'variant_id'         then li.variant_id          = value
      when 'parent_id'          then li.parent_id           = value                    
      #when 'subtotal'          then li.subtotal            = value
      when 'notes'              then li.notes               = value
      when 'custom1'            then li.custom1             = value
      when 'custom2'            then li.custom2             = value
      when 'custom3'            then li.custom3             = value
      when 'unit_price'            
        li.unit_price = value
        li.save            
        li.subtotal = li.unit_price * li.quantity
        li.save
        li.invoice.subtotal = li.invoice.calculate_subtotal
        li.invoice.total = li.invoice.calculate_total
        
      when 'quantity'
        li.quantity = value
        li.subtotal = li.unit_price * li.quantity            
        li.save                        
        li.invoice.subtotal = li.invoice.calculate_subtotal
        li.invoice.total = li.invoice.calculate_total
        
      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