2019-07-30 21:29:24 +00:00
# frozen_string_literal: true
2020-06-12 20:03:43 +00:00
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
2018-03-25 17:30:42 +00:00
module ImportCivicrmPayments
## MINIMALLY TESTED!!!
def self . import_from_csv ( csv_body , nonprofit , field_of_supporter_id )
2021-03-06 21:54:01 +00:00
questionable_records = [ ]
2019-07-30 21:29:24 +00:00
Qx . transaction do
CSV :: Converters [ :blank_to_nil ] = lambda do | field |
field && field . empty? ? nil : field
2018-03-25 17:30:42 +00:00
end
2019-07-30 21:29:24 +00:00
csv = CSV . new ( csv_body , headers : true , converters : [ :blank_to_nil ] )
contrib_records = csv . to_a . map ( & :to_hash )
pay_imp = PaymentImport . create ( nonprofit : nonprofit )
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
supporter_id_custom_field = CustomFieldMaster . where ( 'nonprofit_id = ? AND name = ?' , nonprofit . id , field_of_supporter_id ) . first
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
unless supporter_id_custom_field
raise ParamValidation :: ValidationError . new ( " There is no custom field for nonprofit #{ nonprofit . id } and field named #{ field_of_supporter_id } " , key : :field_of_supporter_id )
2018-03-25 17:30:42 +00:00
end
2019-07-30 21:29:24 +00:00
supporters_with_fields = Supporter . includes ( :custom_field_joins ) . where ( 'supporters.nonprofit_id = ? AND custom_field_joins.custom_field_master_id = ?' , nonprofit . id , supporter_id_custom_field . id )
contrib_records . each do | r |
our_supporter = supporters_with_fields . where ( 'custom_field_joins.value = ?' , r [ field_of_supporter_id ] . to_s ) . first
unless our_supporter
questionable_records . push ( r )
next
end
known_fields = [ 'Date Received' , 'Total Amount' ]
notes = ''
r . except ( known_fields ) . keys . each do | k |
notes += " #{ k } : #{ r [ k ] } \n "
end
offsite = nil
if r [ 'payment_instrument' ] == 'Check'
offsite = { kind : 'check' , check_number : r [ 'Check Number' ] }
end
puts r [ 'Date Received' ]
date_received = nil
Time . use_zone ( 'Pacific Time (US & Canada)' ) do
date_received = Time . zone . parse ( r [ 'Date Received' ] )
puts date_received
end
d = InsertDonation . offsite (
{
amount : Format :: Currency . dollars_to_cents ( r [ 'Total Amount' ] ) ,
nonprofit_id : nonprofit . id ,
supporter_id : our_supporter . id ,
comment : notes ,
date : date_received . to_s ,
offsite_payment : offsite
} . with_indifferent_access
)
puts d
pay_imp . donations . push ( Donation . find ( d [ :json ] [ 'donation' ] [ 'id' ] ) )
2018-03-25 17:30:42 +00:00
end
2019-07-30 21:29:24 +00:00
end
2021-03-06 21:54:01 +00:00
questionable_records
2018-03-25 17:30:42 +00:00
end
def self . undo ( import_id )
Qx . transaction do
2019-07-30 21:29:24 +00:00
import = PaymentImport . find ( import_id )
import . donations . each do | d |
d . payments . each ( & :destroy )
d . offsite_payment & . destroy
2018-03-25 17:30:42 +00:00
2019-07-30 21:29:24 +00:00
d . destroy
2018-03-25 17:30:42 +00:00
end
2019-07-30 21:29:24 +00:00
import . destroy
2018-03-25 17:30:42 +00:00
end
end
2019-07-30 21:29:24 +00:00
end