2013-08-28 11:25:54 +00:00
|
|
|
require 'csv'
|
|
|
|
class PaypalCsv < ActiveRecord::Base
|
|
|
|
attr_accessible :data, :_address_status, :_counterparty_status, :_currency, :_fee, :_from_email_address, :_gross, :_item_id, :_item_title, :_name, :_net, :_status, :_time, :_time_zone, :_to_email_address, :_transaction_id, :_type, :date, :string
|
|
|
|
belongs_to :payment
|
|
|
|
|
|
|
|
after_create :create_payment
|
|
|
|
|
|
|
|
def date_parsed
|
|
|
|
begin
|
|
|
|
Date.strptime(self._date, "%m/%d/%Y")
|
|
|
|
rescue
|
|
|
|
Date.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.batch_import_from_csv(filename)
|
|
|
|
csv = CSV.table(filename)
|
|
|
|
csv.each do |row|
|
|
|
|
paypal_csv = PaypalCsv.new()
|
2015-06-30 18:49:32 +00:00
|
|
|
logger.fatal row.inspect
|
2013-08-28 11:25:54 +00:00
|
|
|
paypal_csv.attributes.each do |c|
|
2015-06-30 18:49:32 +00:00
|
|
|
# Try finding the column without a prepended _ first (compatibility with new CSV format)
|
|
|
|
unless row[c.first[1..-1].to_sym].nil?
|
|
|
|
paypal_csv[c.first.to_sym] = row[c.first[1..-1].to_sym]
|
|
|
|
end
|
|
|
|
# If there's an exact match, it takes precedence
|
2013-08-28 11:25:54 +00:00
|
|
|
unless row[c.first.to_sym].nil?
|
|
|
|
paypal_csv[c.first.to_sym] = row[c.first.to_sym]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
paypal_csv.data = row.to_json
|
|
|
|
paypal_csv.save
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_payment
|
|
|
|
create_payment
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def create_payment
|
2015-06-30 18:49:32 +00:00
|
|
|
logger.fatal self.inspect
|
|
|
|
|
2013-08-28 11:25:54 +00:00
|
|
|
# find user by email, then by payee
|
2013-08-28 15:19:01 +00:00
|
|
|
user = User.where("lower(email) = ?", self._from_email_address.downcase).first
|
|
|
|
user = User.where("lower(payee) = ?", self._from_email_address.downcase).first if user.nil? && self._from_email_address.present?
|
2013-08-28 11:25:54 +00:00
|
|
|
|
|
|
|
# Only create payments if the CSV matches a member
|
|
|
|
if user.present?
|
|
|
|
# And is a payment (not a cancellation, etc)
|
2015-10-27 16:43:27 +00:00
|
|
|
payment_types = ["Subscription Payment Processed","Recurring Payment Received","Payment Received"]
|
2013-08-28 11:25:54 +00:00
|
|
|
if payment_types.include?(self._type)
|
|
|
|
# And a member level
|
|
|
|
if User.member_levels[self._gross.to_i].present?
|
|
|
|
payment = Payment.new
|
|
|
|
payment.date = Date.strptime(self._date, "%m/%d/%Y") #7/6/2013 for Jul 06
|
|
|
|
payment.user_id = user.id
|
|
|
|
payment.amount = self._gross
|
|
|
|
if payment.save
|
|
|
|
self.payment_id = payment.id
|
|
|
|
self.save!
|
|
|
|
else
|
|
|
|
return [false, "Unable to link payment. Payment error: #{payment.errors.full_messages.first}"]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return [false, "Unable to link payment. Couldn't find membership level '#{self._gross.to_i}'."]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return [false, "Unable to link payment. Transaction is a '#{self._type}' instead of '#{payment_types.inspect}'."]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return [false, "Unable to link payment. Couldn't find user/payee '#{self._from_email_address}'."]
|
|
|
|
end
|
|
|
|
|
|
|
|
return [true]
|
|
|
|
end
|
|
|
|
end
|