Skip to content

Commit

Permalink
create new recurring donation record when appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
winsonwan committed Mar 10, 2024
1 parent 4ef7359 commit 1c0414b
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Order < ActiveRecord::Base
attr_accessor :purchase_args
attr_accessor :comments
attr_reader :donation
attr_reader :recurring_donation

# pending and errored states of a CC order (pending = payment has occurred but order has not
# yet been finalized; errored = payment has occurred and order NEVER got finalized, eg due
Expand Down Expand Up @@ -100,9 +101,16 @@ def purchaser_name ; purchaser.full_name ; end

def purchase_medium ; Purchasemethod.get(purchasemethod).purchase_medium ; end

def self.new_from_donation(amount, account_code, donor)
def self.new_from_donation(amount, account_code, donor, donation_frequency)
order = Order.new(:purchaser => donor, :customer => donor)
order.add_donation(Donation.from_amount_and_account_code_id(amount, account_code.id))
if donation_frequency == 'monthly'
recurring_donation = RecurringDonation.from_account_code(account_code)
order.add_recurring_donation(recurring_donation)
donation = recurring_donation.donations.new(amount: amount, account_code: account_code)
else
donation = Donation.from_amount_and_account_code_id(amount, account_code.id)
end
order.add_donation(donation)
order
end

Expand Down Expand Up @@ -238,6 +246,11 @@ def includes_donation?
end
end

def add_recurring_donation(rd) ; self.recurring_donation = rd ; end
def recurring_donation=(rd)
@recurring_donation = rd
end

def includes_bundle?
if completed?
items.any? { |v| v.kind_of?(Voucher) && v.bundle? }
Expand Down Expand Up @@ -386,6 +399,7 @@ def finalize!(sold_on_date = Time.current)
self.items += vouchers
self.items += retail_items
self.items << donation if donation
self.items << recurring_donation if recurring_donation
self.items.each do |i|
i.assign_attributes(:finalized => true,
:sold_on => sold_on_date,
Expand All @@ -398,6 +412,7 @@ def finalize!(sold_on_date = Time.current)
customer.add_items(vouchers)
customer.add_items(retail_items)
purchaser.add_items([donation]) if donation
purchaser.add_items([recurring_donation]) if recurring_donation
customer.save!
purchaser.save!
self.sold_on = sold_on_date
Expand Down

0 comments on commit 1c0414b

Please sign in to comment.