Adding csv import

This commit is contained in:
2013-08-28 04:25:54 -07:00
parent 69a57bc63b
commit 211d79cbcd
11 changed files with 262 additions and 2 deletions

View File

@@ -0,0 +1,28 @@
class PaypalCsvsController < ApplicationController
load_and_authorize_resource :paypal_csv
before_filter :authenticate_user!
def index
end
def show
end
def new
end
def create
PaypalCsv.batch_import_from_csv(params[:file].path)
redirect_to paypal_csvs_path, :notice => 'Paypal CSV batch was successfully loaded.'
end
def link
result = @paypal_csv.link_payment
if result.first
redirect_to paypal_csvs_url, :notice => 'Payment was successfully linked.'
else
redirect_to paypal_csvs_url, :notice => result.last
end
end
end

View File

@@ -5,6 +5,14 @@ class Ipn < ActiveRecord::Base
after_create :create_payment
def date_parsed
begin
Date.strptime(self.payment_date, "%H:%M:%S %b %e, %Y %Z")
rescue
Date.new
end
end
def self.new_from_dynamic_params(params)
ipn = Ipn.new()

73
app/models/paypal_csv.rb Normal file
View File

@@ -0,0 +1,73 @@
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()
paypal_csv.attributes.each do |c|
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
# find user by email, then by payee
user = User.find_by_email(self._from_email_address)
user = User.find_by_payee(self._from_email_address) if user.nil? && self._from_email_address.present?
# Only create payments if the CSV matches a member
if user.present?
# And is a payment (not a cancellation, etc)
payment_types = ["Recurring Payment Received","Payment Received"]
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

View File

@@ -6,7 +6,7 @@
<th>Item</th>
<th>Amount</th>
</tr>
<% @ipns.reverse!.each do |ipn| %>
<% @ipns.sort_by(&:date_parsed).reverse!.each do |ipn| %>
<tr>
<td><%= ipn.payment_date %></td>
<td><%= ipn.first_name %> <%= ipn.last_name %></td>

View File

@@ -0,0 +1,31 @@
<%= link_to "Upload CSV", new_paypal_csv_path %>
<table>
<tr>
<th>Date</th>
<th>Name</th>
<th>Item</th>
<th>Amount</th>
</tr>
<% @paypal_csvs.sort_by(&:date_parsed).reverse!.each do |paypal_csv| %>
<tr>
<td><%= paypal_csv.date %></td>
<td><%= paypal_csv._name %></td>
<td><%= paypal_csv._item_title %></td>
<td>
<% if paypal_csv._gross.blank? %>
<%= paypal_csv._type %>
<% else %>
<%= paypal_csv._gross %>
<% end %>
</td>
<td>
<% if paypal_csv.payment.present? %>
<%= link_to "Linked Payment", paypal_csv.payment %>
<% else %>
<%= link_to "Try to link email '#{paypal_csv._from_email_address}' at membership level '#{paypal_csv._gross.to_i}'", link_paypal_csv_path(paypal_csv) %>
<% end %>
</td>
<td><%= link_to "Details", paypal_csv %></td>
</tr>
<% end %>
</table>

View File

@@ -0,0 +1,15 @@
<style type="text/css">
label {
width: 10em;
display: inline-block;}
</style>
<%= form_tag('/paypal_csvs', :multipart => true) do |f| %>
<div class="field">
<%= label_tag :file %>
<%= file_field_tag :file %>
</div>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>