Class: DonationsImport

Inherits:
Import
  • Object
show all
Includes:
ArtfullyOseHelper, Imports::Rollback, Imports::Validations
Defined in:
app/models/imports/donations_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 ArtfullyOseHelper

#amount_and_nongift, #bootstrapped_type, #build_action_path, #build_order_location, #channel_checkbox, #channel_text, #check_mark, #clean_full_error_messages, #contextual_menu, #credit_card_message, #date_field_tag, #datetime_field_tag, #events_to_options, #fully_qualified_asset_path, #get_selected_class, #icon_link_to, #icon_tag, #link_to_add_fields, #link_to_remove_fields, #nav_dropdown, #number_as_cents, #number_to_dollars, #pluralize_word, #refund_header, #select_event_for_sales_search, #select_membership_type_for_sales_search, #select_pass_type_for_sales_search, #select_show_for_sales_search, #sorted_us_state_abbreviations, #sorted_us_state_names, #thanks_message, #ticket_seller_name, #time_ago_sentence, #time_zone_description, #us_states, #verb_for_save, #widget_script, #with_kit

Methods included from LinkHelper

#active?, #active_link_to, #active_section, #calendar_active_link_to, #in_section, #in_section?, #in_sub_section

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?, #imported!, #importing!, included, #invalidate!, #pending!, #recalled!, #recalling!

Instance Method Details

#create_contribution(parsed_row, person) ⇒ Object



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

def create_contribution(parsed_row, person)
  Rails.logger.info("Processing Import [#{id}] DONATION_IMPORT: Creating contribution")
  validate_amounts(parsed_row)
  amount              = parsed_row.amount
  deductible_amount   = parsed_row.unparsed_deductible_amount.blank?  ? amount - parsed_row.nongift_amount     : parsed_row.deductible_amount
  nongift_amount      = parsed_row.unparsed_nongift_amount.blank?     ? amount - deductible_amount             : parsed_row.nongift_amount
  
  occurred_at = parsed_row.donation_date.blank? ? time_zone_parser.now : time_zone_parser.parse(parsed_row.donation_date)
  params = {}
  params[:subtype] = parsed_row.donation_type
  params[:amount] = deductible_amount
  params[:nongift_amount] = nongift_amount
  params[:payment_method] = parsed_row.payment_method
  
  params[:organization_id] = self.organization.id
  params[:occurred_at] = occurred_at.to_s
  params[:details] = "Imported by #{user.email} on  #{I18n.l self.created_at_local_to_organization, :format => :date}"
  params[:person_id] = person.id
  params[:creator_id] = user.id
  
  contribution = Contribution.new(params)
  contribution.save(ImportedOrder) do |contribution|
    contribution.order.import_id  = self.id
    contribution.order.save
    contribution.order.reload
    contribution.action.import_id = self.id 
    contribution.action.creator = self.user
    contribution.action.details = "Donated #{number_as_cents contribution.order.total}"
    contribution.action.save
  end
  contribution
end

#kindObject



6
7
8
# File 'app/models/imports/donations_import.rb', line 6

def kind
  "donations"
end

#process(parsed_row) ⇒ Object



10
11
12
13
14
# File 'app/models/imports/donations_import.rb', line 10

def process(parsed_row)
  row_valid?(parsed_row)
  person        = create_person(parsed_row)
  contribution  = create_contribution(parsed_row, person)
end

#rollbackObject



16
17
18
19
# File 'app/models/imports/donations_import.rb', line 16

def rollback 
  rollback_orders
  rollback_people
end

#row_valid?(parsed_row) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/imports/donations_import.rb', line 37

def row_valid?(parsed_row)
  raise Import::RowError, "No Amount included in this row: #{parsed_row.row}" if parsed_row.unparsed_amount.blank?
  raise Import::RowError, "Please include a first name, last name, email, or company name in this row: #{parsed_row.row}" unless attach_person(parsed_row).naming_details_available?
  raise Import::RowError, "Please include a payment method in this row: #{parsed_row.row}" if parsed_row.payment_method.blank?
  raise Import::RowError, "Donation type must be 'Monetary' or 'In-Kind': #{parsed_row.row}" unless GiveAction.subtypes.include? (parsed_row.donation_type)
  valid_date?   parsed_row.donation_date
  
  [:unparsed_amount, :unparsed_nongift_amount, :unparsed_deductible_amount].each do |amt|
    valid_amount? parsed_row.send(amt)   unless parsed_row.send(amt).blank?
  end
  
  validate_amounts(parsed_row)
  true
end

#validate_amounts(parsed_row) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/imports/donations_import.rb', line 21

def validate_amounts(parsed_row)
  if !parsed_row.unparsed_nongift_amount.blank? && parsed_row.nongift_amount > parsed_row.amount
    raise Import::RowError, "Non-deductible amount (#{parsed_row.unparsed_nongift_amount}) cannot be more than the total doantion amount (#{parsed_row.unparsed_amount})': #{parsed_row.row}"
  end

  if !parsed_row.unparsed_deductible_amount.blank? && parsed_row.deductible_amount > parsed_row.amount
    raise Import::RowError, "Deductible amount (#{parsed_row.unparsed_deductible_amount}) cannot be more than the total doantion amount (#{parsed_row.unparsed_amount})': #{parsed_row.row}"
  end    
  
  if !parsed_row.unparsed_deductible_amount.blank? &&
     !parsed_row.unparsed_nongift_amount.blank? &&
     (parsed_row.deductible_amount + parsed_row.nongift_amount != parsed_row.amount)
    raise Import::RowError, "Deductible amount (#{parsed_row.unparsed_deductible_amount}) + Non-Deductible Amount (#{parsed_row.unparsed_nongift_amount}) does not equal Amount of  in this row: #{parsed_row.row}"
  end  
end