app/models/imports/events_import.rb in artfully_ose-1.1.0 vs app/models/imports/events_import.rb in artfully_ose-1.2.0.alpha.1

- old
+ new

@@ -14,10 +14,15 @@ 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 + + def imported! + @imported_shows.values.each {|show| show.delay.refresh_stats} + super + end def rollback_events Event.where(:import_id => self.id).all.each {|e| e.destroy} end @@ -26,63 +31,49 @@ rollback_events rollback_people end def row_valid?(parsed_row) - Rails.logger.info("Import #{id} EVENT_IMPORT: Validating 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 - def create_person(parsed_row) - if !attach_person(parsed_row).person_info - person = self.organization.dummy - elsif !parsed_row.email.blank? - person = Person.first_or_create(parsed_row.email, self.organization, parsed_row.person_attributes) do |p| - p.import = self - end - else - person = attach_person(parsed_row) - if !person.save - self.import_errors.create! :row_data => parsed_row.row, :error_message => person.errors.full_messages.join(", ") - self.reload - fail! - end - end - person - end - def create_chart(parsed_row, event, show) - Rails.logger.info("Import #{id} EVENT_IMPORT: Creating chart") + 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("Import #{id} EVENT_IMPORT: Using chart:") - Rails.logger.info("Import #{id} EVENT_IMPORT: #{chart.inspect}") + 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("Import #{id} EVENT_IMPORT: Amount is [#{amount}]") - section = chart.sections.where(:price => amount).first || chart.sections.build(:name => event.name,:price => amount, :capacity => 1) - Rails.logger.info("Import #{id} EVENT_IMPORT: Using section:") - Rails.logger.info("Import #{id} EVENT_IMPORT: #{section.inspect}") - Rails.logger.info("Import #{id} EVENT_IMPORT: Bumping section capacity") + 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("Import #{id} EVENT_IMPORT: Saving section") + Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Saving section") section.save - Rails.logger.info("Import #{id} EVENT_IMPORT: #{section.inspect}") - Rails.logger.info("Import #{id} EVENT_IMPORT: Saving chart") + 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("Import #{id} EVENT_IMPORT: #{show.inspect}") + Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: #{show.inspect}") saved = show.save(:validate => false) - Rails.logger.info("Import #{id} EVENT_IMPORT: Show saved[#{saved}]") + Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Show saved[#{saved}]") chart end def create_event(parsed_row, person) - Rails.logger.info("Import #{id} EVENT_IMPORT: Creating event") + 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 @@ -92,23 +83,23 @@ 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("Import #{id} EVENT_IMPORT: Created event #{event.inspect}") + Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Created event #{event.inspect}") unless event.charts.empty? - Rails.logger.info("Import #{id} EVENT_IMPORT: Default event chart created #{event.charts.first.inspect}") + Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Default event chart created #{event.charts.first.inspect}") end event end def show_key(show_date, event_name) [show_date, event_name].join("-") end def existing_show(show_date, event_name) - Rails.logger.info("Import #{id} EVENT_IMPORT: Checking existing show") + Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Checking existing show") @imported_shows ||= {} show = @imported_shows[show_key(show_date, event_name)] end def eight_pm?(show_date) @@ -120,62 +111,71 @@ show_date end def new_show(parsed_row, event) - Rails.logger.info("Import #{id} EVENT_IMPORT: Creating new show") + 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("Import #{id} EVENT_IMPORT: Show saved #{show.inspect}") + 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 def create_show(parsed_row, event) - Rails.logger.info("Import #{id} EVENT_IMPORT: Creating show") + Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Creating show") return existing_show(parsed_row.show_date, event) || new_show(parsed_row, event) end def create_actions(parsed_row, person, event, show, order) - Rails.logger.info("Import #{id} EVENT_IMPORT: Creating actions") - go_action = GoAction.for(show, person) - go_action.import = self - go_action.save + 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 is created by the order get_action = GetAction.where(:subject_id => order.id).first - get_action.update_attribute(:occurred_at, time_zone_parser.parse(parsed_row.order_date)) unless parsed_row.order_date.blank? - + 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 def create_ticket(parsed_row, person, event, show, chart) - Rails.logger.info("Import #{id} EVENT_IMPORT: Creating ticket") + Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Creating ticket") amount = parsed_row.amount || 0 - Rails.logger.info("Import #{id} EVENT_IMPORT: Amount is [#{amount}]") - section = chart.sections.where(:price => amount).first - Rails.logger.info("Import #{id} EVENT_IMPORT: Section is [#{section.inspect}]") + 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, section.price,1, true) - Rails.logger.info("Import #{id} EVENT_IMPORT: Ticket built [#{ticket.inspect}]") + 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("Import #{id} EVENT_IMPORT: Ticket sold to [#{person.inspect}]") + Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Ticket sold to [#{person.inspect}]") ticket.sold_price = amount ticket.save - Rails.logger.info("Import #{id} EVENT_IMPORT: Ticket saved [#{ticket.inspect}]") + Rails.logger.info("Processing Import [#{id}] EVENT_IMPORT: Ticket saved [#{ticket.inspect}]") ticket end def create_order(parsed_row, person, event, show, ticket) - Rails.logger.info("Import #{id} EVENT_IMPORT: Creating order") + 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 @@ -186,11 +186,10 @@ 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? - order.create_purchase_action_without_delay - order.actions.where(:type => "GetAction").first.update_attribute(:occurred_at, parsed_row.order_date) unless parsed_row.order_date.blank? + @imported_orders[order_key] = order order end def events_hash \ No newline at end of file