app/models/donation_search.rb in artfully_ose-1.2.0 vs app/models/donation_search.rb in artfully_ose-1.3.0.pre1
- old
+ new
@@ -1,27 +1,205 @@
class DonationSearch
+ extend ::ArtfullyOseHelper
- attr_reader :start, :stop
+ attr_reader :start, :stop, :type, :campaign_id, :appeal_id, :only_unacknowledged, :search
+ attr_accessor :rows
- def initialize(start, stop, organization)
+ def initialize(organization, params)
@organization = organization
- @start = start_with(start)
- @stop = stop_with(stop)
- @results = yield(results) if block_given?
+
+ @start = start_with(params[:start], params[:has_default_range])
+ @stop = stop_with(params[:stop], params[:has_default_range])
+ @type = params[:type]
+ @campaign_id = params[:campaign_id]
+ @appeal_id = params[:appeal_id]
+ @search = params[:search]
+ @only_unacknowledged = params[:only_unacknowledged]
+
+ self.rows = results
end
+
def results
- @results ||= Order.in_range(@start, @stop, @organization.id).select(&:has_donation?).sort_by(&:created_at)
+ orders = @organization.orders.includes(:person, :donation => [:campaign, :appeal])
+ orders = orders.where("EXISTS(SELECT 1 FROM items WHERE items.order_id = orders.id AND items.product_type = 'Donation')")
+ orders = orders.where('donations.campaign_id=?', @campaign_id) if @campaign_id.to_i > 0
+ orders = orders.where('donations.appeal_id=?', @appeal_id) if @appeal_id.to_i > 0
+ orders = orders.where('donations.pledge=?', true) if @type == 'pledge'
+ orders = orders.where('donations.pledge=? AND orders.parent_id IS NULL', false) if @type == 'donation'
+ orders = orders.where('donations.pledge=? AND orders.parent_id IS NOT NULL', false) if @type == 'pledge_payment'
+ orders = orders.where('donations.ack_sent_date is null') if @only_unacknowledged
+ orders = orders.where('people.id in (?)', Person.search_index(@search.dup, @organization).collect(&:id)) if @search.present?
+
+ rows = []
+ orders.each do |order|
+ row = Row.new(@organization, order)
+ rows << row if (@start.blank? || row.date >= @start) && (@stop.blank? || row.date <= @stop)
+ end
+
+ rows
end
+ def campaign(campaign_id = self.campaign_id)
+ @campaign ||= (campaign_id.present? && campaign_id.to_i > 0) ? Campaign.find(campaign_id) : nil
+ end
+
+
+ class Row
+ attr_accessor :order, :donation, :person, :type, :date
+
+ def initialize(organization, order)
+ @organization = organization
+ self.order = order
+ self.donation = order.donation || Donation.new
+ self.person = order.person
+
+ self.type = order.gift_type
+ if self.type == 'Pledge'
+ date = donation.commitment_date || order.created_at
+ else
+ date = order.created_at
+ end
+
+ self.date = ActiveSupport::TimeZone.create(@organization.time_zone).utc_to_local(date)
+ end
+
+ def self.find_person_email(person_id)
+ p = Person.find(person_id) if person_id.to_i > 0
+ p.email if p
+ end
+
+ def self.soft_credit_amount(donation_id)
+ soft_credit = SoftCredit.find_by_donation_id(donation_id)
+ soft_credit.amount.to_i unless soft_credit.nil?
+ end
+
+ def self.soft_credit_person(donation_id)
+ soft_credit = SoftCredit.find_by_donation_id(donation_id)
+ Person.find(soft_credit.person_id) unless soft_credit.nil?
+ end
+
+ comma do
+ order("Order Date") { |order| order.created_at }
+ order("Payment Method") { |order| order.payment_method }
+ order("Amount") { |order| order.sum_donations.to_i / 100.00 }
+ order("Non Deductible Amount") { |order| order.nongift_amount.to_i / 100.00 }
+ order("Donation Type") { |order| order.actions.select{|action| action.subject_type == 'Order'}.first.subtype }
+ order("Notes") { |order| order.notes }
+ order("Creator") { |order| order.creator && order.creator.email }
+ donation("Fiscal Year") { |donation| donation.fiscal_year}
+
+ person("Email") { |person| person.email }
+ person("Salutation") { |person| person.salutation }
+ person("First Name") { |person| person.first_name }
+ person("Middle Name") { |person| person.middle_name }
+ person("Last Name") { |person| person.last_name }
+ person("Suffix") { |person| person.suffix }
+ person("Title") { |person| person.title }
+ person("Type") { |person| person.type }
+ person("Subtype") { |person| person.subtype }
+ person("Company Name") { |person| person.company_name }
+
+ ('A'..'C').each_with_index do |letter, i|
+ person("Address#{letter} Type") { |person| person.addresses[i] && person.addresses[i].kind }
+ person("Address#{letter} Address 1") { |person| person.addresses[i] && person.addresses[i].address1 }
+ person("Address#{letter} Address 2") { |person| person.addresses[i] && person.addresses[i].address2 }
+ person("Address#{letter} City") { |person| person.addresses[i] && person.addresses[i].city }
+ person("Address#{letter} State") { |person| person.addresses[i] && person.addresses[i].state }
+ person("Address#{letter} Zip") { |person| person.addresses[i] && person.addresses[i].zip }
+ person("Address#{letter} Country") { |person| person.addresses[i] && person.addresses[i].country }
+ end
+
+ (1..3).each do |i|
+ person("Phone#{i} type") { |person| person.phones[i-1] && person.phones[i-1].kind }
+ person("Phone#{i} number") { |person| person.phones[i-1] && person.phones[i-1].number }
+ end
+
+ person("Website") { |person| person.website }
+ person("Twitter Handle") { |person| person.twitter_handle }
+ person("Facebook URL") { |person| person.facebook_url }
+ person("Linked In Url") { |person| person.linked_in_url }
+ person("Tags") { |person| person.tags.join("|") }
+ person("Do Not Email") { |person| person.do_not_email }
+ person("Do Not Call") { |person| person.do_not_call }
+ person("Household Name") { |person| person.household && person.household.name }
+ end
+
+ comma :with_campaigns do
+ order("Order Date") { |order| order.created_at }
+ order("Payment Method") { |order| order.payment_method }
+ order("Amount") { |order| order.sum_donations.to_i / 100.00 }
+ order("Non Deductible Amount") { |order| order.nongift_amount.to_i / 100.00 }
+ order("Donation Type") { |order| order.actions.select{|action| action.subject_type == 'Order'}.first.subtype }
+ order("Notes") { |order| order.notes }
+ order("Creator") { |order| order.creator && order.creator.email }
+
+ donation("Campaign") { |donation| donation.campaign.name unless donation.campaign.nil?}
+ donation("Appeal") { |donation| donation.appeal.name unless donation.appeal.nil?}
+ donation("Acknowledgement") { |donation| donation.ack_sent_date}
+ donation("Match Eligible Matcher") { |donation| Row.find_person_email(donation.matcher)}
+ donation("Match Eligible Ratio") { |donation| donation.ratio}
+ donation("Match Eligible Amount") { |donation| donation.eligible_amount.to_i / 100.00}
+ donation("Soft Credit Email") { |donation| Row.soft_credit_person(donation.id).email unless Row.soft_credit_person(donation.id).nil?}
+ donation("Soft Credit First Name") { |donation| Row.soft_credit_person(donation.id).first_name unless Row.soft_credit_person(donation.id).nil?}
+ donation("Soft Credit Last Name") { |donation| Row.soft_credit_person(donation.id).last_name unless Row.soft_credit_person(donation.id).nil?}
+ donation("Soft Credit Amount") { |donation| Row.soft_credit_amount(donation.id)}
+ donation("Fiscal Year") { |donation| donation.fiscal_year}
+
+ person("Email") { |person| person.email }
+ person("Salutation") { |person| person.salutation }
+ person("First Name") { |person| person.first_name }
+ person("Middle Name") { |person| person.middle_name }
+ person("Last Name") { |person| person.last_name }
+ person("Suffix") { |person| person.suffix }
+ person("Title") { |person| person.title }
+ person("Type") { |person| person.type }
+ person("Subtype") { |person| person.subtype }
+ person("Company Name") { |person| person.company_name }
+
+ ('A'..'C').each_with_index do |letter, i|
+ person("Address#{letter} Type") { |person| person.addresses[i] && person.addresses[i].kind }
+ person("Address#{letter} Address 1") { |person| person.addresses[i] && person.addresses[i].address1 }
+ person("Address#{letter} Address 2") { |person| person.addresses[i] && person.addresses[i].address2 }
+ person("Address#{letter} City") { |person| person.addresses[i] && person.addresses[i].city }
+ person("Address#{letter} State") { |person| person.addresses[i] && person.addresses[i].state }
+ person("Address#{letter} Zip") { |person| person.addresses[i] && person.addresses[i].zip }
+ person("Address#{letter} Country") { |person| person.addresses[i] && person.addresses[i].country }
+ end
+
+ (1..3).each do |i|
+ person("Phone#{i} type") { |person| person.phones[i-1] && person.phones[i-1].kind }
+ person("Phone#{i} number") { |person| person.phones[i-1] && person.phones[i-1].number }
+ end
+
+ person("Website") { |person| person.website }
+ person("Twitter Handle") { |person| person.twitter_handle }
+ person("Facebook URL") { |person| person.facebook_url }
+ person("Linked In Url") { |person| person.linked_in_url }
+ person("Tags") { |person| person.tags.join("|") }
+ person("Do Not Email") { |person| person.do_not_email }
+ person("Do Not Call") { |person| person.do_not_call }
+ person("Household Name") { |person| person.household && person.household.name }
+ end
+
+ end
+
private
- def start_with(start)
- start.present? ? DateTime.parse(start) : default_start
+ def start_with(start, has_default_range)
+ if start.present?
+ DateTime.parse(start)
+ else
+ has_default_range ? default_start : nil
+ end
end
- def stop_with(stop)
- stop.present? ? Sundial.midnightish(@organization, stop) : default_stop
+ def stop_with(stop, has_default_range)
+ if stop.present?
+ Sundial.midnightish(@organization, stop)
+ else
+ has_default_range ? default_stop : nil
+ end
end
def default_start
DateTime.now.in_time_zone(@organization.time_zone).beginning_of_month
end
\ No newline at end of file