Class: Caboose::GiftCardsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/gift_cards_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/controllers/caboose/gift_cards_controller.rb', line 144

def admin_add
  return if !user_is_allowed('giftcards', 'add')
  
  resp = StdClass.new
  code = params[:code].strip
  
  if code.length == 0
    resp.error = "A valid code is required."
  elsif GiftCard.where(:code => code).exists?
    resp.error = "A gift card with that code already exists."
  else
    gc = GiftCard.new(
      :site_id => @site.id,
      :code    => code,
      :status  => GiftCard::STATUS_INACTIVE              
    )
    resp.success = gc.save
    resp.new_id = gc.id
    resp.redirect = "/admin/gift-cards/#{gc.id}"
  end
  
  render :json => resp        
end

#admin_bulk_addObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/caboose/gift_cards_controller.rb', line 117

def admin_bulk_add
  return if !user_is_allowed('sites', 'add')
  
  resp = Caboose::StdClass.new
  i = 0
  CSV.parse(params[:csv_data].strip).each do |row|        
    if row[0].nil? || row[0].strip.length == 0        
      resp.error = "Code not defined on row #{i+1}."        
    end
    i = i + 1
  end
  
  if resp.error.nil?
    CSV.parse(params[:csv_data]).each do |row|
      Caboose::GiftCard.create(
        :site_id  => @site.id,
        :code => row[0].strip,            
        :status => GiftCard::STATUS_INACTIVE                        
      )
    end
    resp.success = true
  end
  
  render :json => resp
end

#admin_bulk_deleteObject



169
170
171
172
173
174
175
176
177
178
# File 'app/controllers/caboose/gift_cards_controller.rb', line 169

def admin_bulk_delete
  return if !user_is_allowed('sites', 'delete')
  
  resp = Caboose::StdClass.new
  params[:model_ids].each do |gc_id|
    GiftCard.find(gc_id).destroy
  end
  resp.success = true
  render :json => resp
end

#admin_card_type_optionsObject



62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/caboose/gift_cards_controller.rb', line 62

def admin_card_type_options
  return if !user_is_allowed('categories', 'view')
  types = [
    GiftCard::CARD_TYPE_AMOUNT,
    GiftCard::CARD_TYPE_PERCENTAGE,
    GiftCard::CARD_TYPE_NO_SHIPPING,
    GiftCard::CARD_TYPE_NO_TAX,
  ]        
  options = types.collect{ |s| { 'text' => s, 'value' => s }}       
  render :json => options            
end

#admin_deleteObject



181
182
183
184
185
186
187
# File 'app/controllers/caboose/gift_cards_controller.rb', line 181

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

#admin_editObject



82
83
84
85
86
# File 'app/controllers/caboose/gift_cards_controller.rb', line 82

def admin_edit
  return if !user_is_allowed('giftcards', 'edit')
  @gift_card = GiftCard.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_indexObject



5
6
7
8
# File 'app/controllers/caboose/gift_cards_controller.rb', line 5

def admin_index
  return if !user_is_allowed('giftcards', 'view')      
  render :layout => 'caboose/admin'
end

#admin_jsonObject



11
12
13
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
# File 'app/controllers/caboose/gift_cards_controller.rb', line 11

def admin_json
  return if !user_is_allowed('giftcards', 'view')
  
  pager = PageBarGenerator.new(params, {
      'site_id'               => @site.id,          
      'name'                  => '',
      'code'                  => '',      
      'card_type'             => '',
      'total_lte'             => '',
      'total_gte'             => '',
      'balance_lte'           => '',
      'balance_gte'           => '',
      'min_invoice_total_lte' => '',
      'min_invoice_total_gte' => '',
      'date_available_lte'    => '',
      'date_available_gte'    => '',
      'date_expires_lte'      => '',
      'date_expires_gte'      => '',
      'status'                => ''
		},{
		  'model'          => 'Caboose::GiftCard',
	    'sort'			     => 'code',
		  'desc'			     => false,
		  'base_url'		   => "/admin/gift-cards",
		  'use_url_params' => false
	})
	render :json => {
	  :pager => pager,
	  :models => pager.items
	}
end

#admin_json_singleObject



75
76
77
78
79
# File 'app/controllers/caboose/gift_cards_controller.rb', line 75

def admin_json_single
  return if !user_is_allowed('giftcards', 'view')    
  gc = GiftCard.find(params[:id])      
  render :json => gc
end

#admin_newObject



44
45
46
47
# File 'app/controllers/caboose/gift_cards_controller.rb', line 44

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

#admin_status_optionsObject



50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/caboose/gift_cards_controller.rb', line 50

def admin_status_options
  return if !user_is_allowed('categories', 'view')
  statuses = [      
    GiftCard::STATUS_INACTIVE,
    GiftCard::STATUS_ACTIVE,
    GiftCard::STATUS_EXPIRED
  ]        
  options = statuses.collect{ |s| { 'text' => s, 'value' => s }}       
  render :json => options
end

#admin_updateObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/caboose/gift_cards_controller.rb', line 89

def admin_update
  return unless user_is_allowed_to 'edit', 'sites'

  resp = Caboose::StdClass.new    
  gift_cards = params[:id] == 'bulk' ? params[:model_ids].collect{ |gc_id| GiftCard.find(gc_id) } : [GiftCard.find(params[:id])]

  save = true
  params.each do |k,v|
    case k
      when 'site_id'           then gift_cards.each{ |gc| gc.site_id           = v }                 
      when 'name'              then gift_cards.each{ |gc| gc.name              = v }
      when 'code'              then gift_cards.each{ |gc| gc.code              = v }
      when 'card_type'         then gift_cards.each{ |gc| gc.card_type         = v }
      when 'total'             then gift_cards.each{ |gc| gc.total             = v }
      when 'balance'           then gift_cards.each{ |gc| gc.balance           = v }
      when 'min_invoice_total' then gift_cards.each{ |gc| gc.min_invoice_total = v }            
      when 'date_available'    then gift_cards.each{ |gc| gc.date_available    = DateTime.strptime(v, '%m/%d/%Y') }
      when 'date_expires'      then gift_cards.each{ |gc| gc.date_expires      = DateTime.strptime(v, '%m/%d/%Y') }
      when 'status'            then gift_cards.each{ |gc| gc.status            = v }                      
    end        
  end
  gift_cards.each{ |gc| gc.save }

  resp.success = true
  render :json => resp
end