Class: EventsImport

Inherits:
Import
  • Object
show all
Includes:
Imports::Rollback, Imports::Validations
Defined in:
app/models/imports/events_import.rb

Constant Summary

Constants inherited from Import

Import::DATE_INPUT_FORMAT, Import::DATE_INPUT_FORMAT_WITH_TIME

Instance Method Summary collapse

Methods included from Imports::Validations

#valid_amount?, #valid_date?

Methods included from Imports::Rollback

#rollback_orders, #rollback_people

Methods inherited from Import

#attach_person, build, #cache_data, #create_person, #fail!, #hash_address, #headers, #import, #message, #parsed_rows, #perform, #recallable?, #rows

Methods included from Imports::Processing

#csv_data, included, #s3_service, #time_zone_parser

Methods included from Imports::Status

#approve!, #caching!, #failed!, #failed?, #importing!, included, #invalidate!, #pending!, #recalled!, #recalling!

Instance Method Details

#create_actions(parsed_row, person, event, show, order) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/imports/events_import.rb', line 134

def create_actions(parsed_row, person, event, show, order)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Creating actions")
  go_action = GoAction.for(show, person) do |go_action|
    go_action.import = self
    go_action.save
  end
   
  get_action = GetAction.where(:subject_id => order.id).first
  if get_action
    get_action.details = order.ticket_details
    get_action.occurred_at = time_zone_parser.parse(parsed_row.order_date) unless parsed_row.order_date.blank?
    get_action.save
  else
    order.create_purchase_action
  end
  get_action ||= GetAction.where(:subject_id => order.id).first

  return go_action, get_action
end

#create_chart(parsed_row, event, show) ⇒ Object



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
# File 'app/models/imports/events_import.rb', line 46

def create_chart(parsed_row, event, show)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Creating chart")
  chart = show.chart || show.create_chart({ :name => event.name, :skip_create_first_section => true })   
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Using chart:")
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: #{chart.inspect}")
  amount = parsed_row.amount || 0
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Amount is [#{amount}]")
  section = chart.sections.first || chart.sections.build(:name => event.name, :capacity => 1)
  section.skip_create_first_ticket_type = true
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Using section:")
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: #{section.inspect}")
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Bumping section capacity")
  section.capacity = section.capacity + 1 unless section.new_record?
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Saving section")
  section.save
  ticket_type = section.ticket_types.where(:price => amount).first || section.ticket_types.create({:name => event.name, :price => amount, :limit => 0})
  ticket_type.limit = ticket_type.limit + 1
  ticket_type.save    
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: #{section.inspect}")
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Saving chart")
  chart.save
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: #{show.inspect}")
  saved = show.save(:validate => false)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Show saved[#{saved}]")
  chart
end

#create_event(parsed_row, person) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/imports/events_import.rb', line 73

def create_event(parsed_row, person)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Creating event")
  event = Event.where(:name => parsed_row.event_name).where(:organization_id => self.organization).first
  return event if event
    
  event = Event.new
  event.name = parsed_row.event_name
  event.organization = self.organization
  event.venue = Venue.new
  event.venue.name = parsed_row.venue_name
  event.venue.organization = self.organization
  event.venue.time_zone = self.organization.time_zone
  event.contact_email = self.organization.email || self.user.email
  event.import = self
  event.save!
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Created event #{event.inspect}")
  unless event.charts.empty?
    Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Default event chart created #{event.charts.first.inspect}") 
  end
  event
end

#create_order(parsed_row, person, event, show, ticket) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/imports/events_import.rb', line 175

def create_order(parsed_row, person, event, show, ticket)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Creating order")
  order_key = [show.id.to_s,person.id.to_s,parsed_row.payment_method].join('-')
  @imported_orders ||= {}
  order = @imported_orders[order_key] || ImportedOrder.new
  order.organization = self.organization
  order.payment_method = parsed_row.payment_method
  order.person = person
  order.details = "Imported by #{user.email} on #{I18n.l self.created_at_local_to_organization, :format => :date}"
  order.import = self
  item = Item.for(ticket)
  item.state = "settled"
  order.items << item
  order.skip_actions = true
  order.save
  order.update_attribute(:created_at, time_zone_parser.parse(parsed_row.order_date)) unless parsed_row.order_date.blank?

  @imported_orders[order_key] = order
  order
end

#create_show(parsed_row, event) ⇒ Object



129
130
131
132
# File 'app/models/imports/events_import.rb', line 129

def create_show(parsed_row, event)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Creating show")
  return existing_show(parsed_row.show_date, event) || new_show(parsed_row, event)
end

#create_ticket(parsed_row, person, event, show, chart) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/imports/events_import.rb', line 154

def create_ticket(parsed_row, person, event, show, chart)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Creating ticket")
  amount = parsed_row.amount || 0
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Amount is [#{amount}]")
  section = chart.sections.first
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Section is [#{section.inspect}]")
  ticket_type = section.ticket_types.where(:price => amount).first
  
  raise Import::RuntimeError, 'No section found for ticket' unless section
  
  ticket = Ticket.build_one(show, section,1, true)
  ticket.ticket_type = ticket_type
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Ticket built [#{ticket.inspect}]")
  ticket.sell_to person
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Ticket sold to [#{person.inspect}]")
  ticket.sold_price = amount
  ticket.save
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Ticket saved [#{ticket.inspect}]")
  ticket
end

#eight_pm?(show_date) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
# File 'app/models/imports/events_import.rb', line 105

def eight_pm?(show_date)
  begin
    Time.parse(show_date.match(/[0-9]+\:[0-9][0-9][a|p]m?/).to_s)
  rescue ArgumentError
    show_date = show_date + " 8:00pm"
  end
  
  show_date
end

#existing_show(show_date, event_name) ⇒ Object



99
100
101
102
103
# File 'app/models/imports/events_import.rb', line 99

def existing_show(show_date, event_name)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Checking existing show")
  @imported_shows ||= {}
  show = @imported_shows[show_key(show_date, event_name)]    
end

#imported!Object



20
21
22
23
# File 'app/models/imports/events_import.rb', line 20

def imported!
  @imported_shows.values.each {|show| show.delay.refresh_stats}
  super
end

#kindObject



5
6
7
# File 'app/models/imports/events_import.rb', line 5

def kind
  "events"
end

#new_show(parsed_row, event) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/imports/events_import.rb', line 115

def new_show(parsed_row, event)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Creating new show")
  show = Show.new
  show.datetime = time_zone_parser.parse(eight_pm?(parsed_row.show_date))
  show.event = event
  show.organization = self.organization
  show.state = "unpublished"                      #Hacky end-around state machine here because we don't have a chart yet
  show.save(:validate => false)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Show saved #{show.inspect}")
  
  @imported_shows[show_key(parsed_row.show_date, event.name)] = show
  show    
end

#parse_and_hash(rows) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'app/models/imports/events_import.rb', line 196

def parse_and_hash(rows)
  rows.reduce({}) do | all,imported_row|
    row = ParsedRow.new(headers, imported_row.content)
    key = (row.event_name || "") + (row.venue_name || "") + (row.show_date.to_s || "")
    all[key] ||= []
    all[key] << row
    all
  end
end

#process(parsed_row) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'app/models/imports/events_import.rb', line 9

def process(parsed_row)
  row_valid?(parsed_row)
  person      = create_person(parsed_row)
  event       = create_event(parsed_row, person)
  show        = create_show(parsed_row, event)
  chart       = create_chart(parsed_row, event, show)
  ticket      = create_ticket(parsed_row, person, event, show, chart)
  order       = create_order(parsed_row, person, event, show, ticket)
  actions     = create_actions(parsed_row, person, event, show, order)
end

#rollbackObject



29
30
31
32
33
# File 'app/models/imports/events_import.rb', line 29

def rollback 
  rollback_orders
  rollback_events
  rollback_people
end

#rollback_eventsObject



25
26
27
# File 'app/models/imports/events_import.rb', line 25

def rollback_events
  Event.where(:import_id => self.id).all.each {|e| e.destroy}
end

#row_valid?(parsed_row) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



35
36
37
38
39
40
41
42
43
44
# File 'app/models/imports/events_import.rb', line 35

def row_valid?(parsed_row)
  Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Validating Row")
  raise Import::RowError, "No Event Name included in this row: #{parsed_row.row}" unless parsed_row.event_name 
  raise Import::RowError, "No Show Date included in this row: #{parsed_row.row}" unless parsed_row.show_date
  raise Import::RowError, "Please include a payment method in this row: #{parsed_row.row}" if parsed_row.payment_method.blank?
  valid_date?   parsed_row.show_date    
  valid_amount? parsed_row.unparsed_amount      unless parsed_row.unparsed_amount.blank?
  valid_date?   parsed_row.order_date           unless parsed_row.order_date.blank?
  true
end

#show_key(show_date, event_name) ⇒ Object



95
96
97
# File 'app/models/imports/events_import.rb', line 95

def show_key(show_date, event_name)
  [show_date, event_name].join("-")
end