Skip to content

Commit

Permalink
initial scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
rnickles committed Apr 14, 2024
1 parent aa7de8d commit d6612bd
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 0 deletions.
142 changes: 142 additions & 0 deletions app/controllers/recurring_donations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
class RecurringDonationsController < ApplicationController

before_filter :is_staff_filter
before_filter :load_customer, :only => [:new, :create]

private

def load_customer
return redirect_to(donations_path, :alert => 'You must select a customer.') unless @customer = Customer.find(params[:customer_id])
end

public

def index
@total = 0
@params = {}
@page_title = "Donation history"
@page = (params[:page] || '1').to_i
@header = ''
@donations = Donation.
includes(:order,:customer,:account_code).
where.not(:customer_id => Customer.walkup_customer.id).
order(:sold_on)
if (params[:use_cid] && !params[:cid].blank?) # cust id will be embedded in route in autocomplete field
cid = if params[:cid] =~ /^\d+$/ then params[:cid] else Customer.id_from_route(params[:cid]) end
@donations = @donations.where(:customer_id => cid)
@full_name = Customer.find(cid).full_name
end
if params[:use_date]
if params[:dates].blank?
mindate,maxdate = [Time.parse("2007-01-01"), Time.current]
else
mindate,maxdate = Time.range_from_params(params[:dates])
@header = "#{mindate.to_formatted_s(:compact)}-#{maxdate.to_formatted_s(:compact)}: "
# allow dates to be picked up as default form field for next time
params[:from] = mindate
params[:to] = maxdate
end
@donations = @donations.where(:sold_on => mindate..maxdate)
end
if params[:use_amount]
min,max = params[:donation_min].to_i, params[:donation_max].to_i
return redirect_to(donations_path, :alert => t('donations.errors.invalid_amounts')) if
(max.zero? && min.zero?) || max < 0 || min < 0
min,max = max,min if min > max
@donations = @donations.where(:amount => min..max)
end
if params[:use_ltr_sent]
@donations = @donations.where(:letter_sent => nil)
end
if !params[:use_fund].blank? && !params[:donation_funds].blank?
@donations = @donations.where(:account_code_id => params[:donation_funds])
end
@total = @donations.sum(:amount)
@params = params

if params[:commit] =~ /download/i
send_data @donations.to_csv, :type => 'text/csv', :filename => 'donations_report.csv'
else
@donations = @donations.paginate(:page => @page)
@header << "#{@donations.total_entries} transactions, " <<
ActionController::Base.helpers.number_to_currency(@total)
end
end

def new
@donation ||= @customer.donations.new(:amount => 0,:comments => '')
end

def create
@order = Order.create(:purchaser => @customer, :customer => @customer, :processed_by => current_user)
@donation = Donation.from_amount_and_account_code_id(
params[:amount].to_f, params[:fund].to_i, params[:comments].to_s)
@order.add_donation(@donation)
@order.processed_by = current_user()

sold_on = Date.from_year_month_day(params[:date])
case params[:payment]
when 'check'
@order.purchasemethod = Purchasemethod.get_type_by_name('box_chk')
when 'cash'
@order.purchasemethod = Purchasemethod.get_type_by_name('box_cash')
when 'credit_card'
@order.purchasemethod = Purchasemethod.get_type_by_name('web_cc')
@order.purchase_args = { :credit_card_token => params[:credit_card_token] }
sold_on = Time.current
end
@order.comments = params[:comments].to_s
unless @order.ready_for_purchase?
flash[:alert] = @order.errors.as_html
render :action => 'new'
return
end
begin
@order.finalize!(sold_on)
redirect_to(customer_path(@customer), :notice => 'Donation recorded.')
rescue Order::PaymentFailedError => e
@order.destroy
redirect_to(new_customer_donation_path(@customer), :alert => e.message)
rescue StandardError => e
@order.destroy
# rescue ActiveRecord::RecordInvalid => e
# rescue Order::OrderFinalizeError => e
# rescue RuntimeError => e
end
end

def update
if (t = Donation.find_by_id(params[:id])).kind_of?(Donation)
now = Time.current
c = current_user.email rescue "(??)"
t.update_attributes(:letter_sent => now,
:processed_by => current_user)
Txn.add_audit_record(:customer_id => t.customer_id,
:logged_in_id => current_user.id,
:txn_type => 'don_ack',
:comments => "Donation ID #{t.id} marked as acknowledged")
result = now.strftime("%D by #{c}")
else
result = '(ERROR)'
end
render :js => %Q{\$('#donation_#{params[:id]}').text('#{result}')}
end

# AJAX handler for updating the text of a donation's comment
def update_comment_for
begin
donation = Donation.find(params[:id])
comments = params[:comments]
donation.update_attributes!(:comments => comments)
Txn.add_audit_record(:customer_id => donation.customer_id, :logged_in_id => current_user.id,
:order_id => donation.order_id,
:comments => comments,
:txn_type => "don_edit")
# restore "save comment" button to look like a check mark
render :js => %Q{alert('Comment saved')}
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid => e
error = ActionController::Base.helpers.escape_javascript(e.message)
render :js => %Q{alert('There was an error saving the donation comment: #{error}')}
end
end
end
2 changes: 2 additions & 0 deletions app/views/donations/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
%h1= @header

= link_to 'Recurring Donations', recurring_donations_path, class: 'btn btn-primary', style: 'float: right; margin-top: 10px;'

.pagination-container
.pagination.mx-auto
= will_paginate @donations, :previous_label => '&laquo;', :next_label => '&raquo;', :container => false
Expand Down
5 changes: 5 additions & 0 deletions app/views/recurring_donations/_donation_letter_sent.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%div[donation]
- if donation.letter_sent
= "#{donation.letter_sent.strftime('%D')} by #{Customer.find(donation.processed_by_id).first_name rescue '(???)'}"
- else
= link_to "Mark Sent", donation_path(donation), 'data-remote' => true, 'data-method' => :put, 'data-type' => 'script'
66 changes: 66 additions & 0 deletions app/views/recurring_donations/_donation_search.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
.form-row
.col-md-5
= check_box_tag 'use_cid', 1, @params[:use_cid]
%label.col-form-label Only for customer (type first or last name):
.col-md-5#donor_autocomplete
:javascript
$('#use_cid').change(function() {
if (! $(this).is(':checked')) {
$('#show_vouchers').prop('checked',false);
$('#show_vouchers').prop('disabled',true);
} else {
$('#show_vouchers').prop('disabled',false);
$('#cid').val('');
};
});
$('#donor_name').focus(function() { $('#use_cid').prop('checked',true); })
= text_field_tag 'donor_name', @full_name, :class => '_autocomplete form-control', 'data-resultfield' => 'cid'
= hidden_field_tag 'cid', @params[:cid]

.form-row
.col-md-5
= check_box_tag 'use_date', 1, @params[:use_date]
%label.col-form-label Date is in the range:
.col-md-5= select_date_with_shortcuts 'dates', :from => @params[:from], :to => @params[:to], :enables => '#use_date', :class => 'form-control'

.form-row
.col-md-5
= check_box_tag 'use_amount', 1, @params[:use_amount]
%label.col-form-label Donation amount between:
.input-group.col-md-2
.input-group-prepend
%span.input-group-text.form-control $
= number_field_tag 'donation_min', @params[:donation_min], :class => 'form-control text-right a1-no-spinner'
.input-group-append
%span.input-group-text.form-control .00
.col-md-1.text-center.form-control-plaintext and
.input-group.col-md-2
.input-group-prepend
%span.input-group-text.form-control $
= number_field_tag 'donation_max', @params[:donation_max], :class => 'form-control text-right a1-no-spinner'
.input-group-append
%span.input-group-text.form-control .00

.form-row
.col-md-5
= check_box_tag 'use_fund', 1, @params[:use_fund]
%label.col-form-label Only donations to these funds:
%br
= link_to "Add/Edit Account Codes&hellip;".html_safe, account_codes_path, :class => 'btn btn-primary mx-1'

.col-md-5
= select_tag 'donation_funds', options_from_collection_for_select(AccountCode.all, :id, :name_with_code), :multiple => true, :class => 'form-control'

.form-row
.col-md-5
= check_box_tag 'use_ltr_sent', 1, @params[:use_ltr_sent]
%label.col-form-label Only if letter not yet sent
= popup_help_for :donation_search_by_letter_sent

.form-row
.col-md-12
= check_box_tag 'use_repeat_donor', 1, @params[:use_repeat_donor]
%label.col-form-label Only donors who have made at least
= select_tag 'num_donations', options_for_select(2..12)
donations since
= select_date((@params[:donated_since] || Date.today), :prefix => 'since')
1 change: 1 addition & 0 deletions app/views/recurring_donations/_update.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('#donation_<%= @id %>').text('#{result}');
50 changes: 50 additions & 0 deletions app/views/recurring_donations/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
%h1= @header

= link_to 'Donations', donations_path, class: 'btn btn-primary', style: 'float: right; margin-top: 10px;'

.pagination-container
.pagination.mx-auto
= will_paginate @donations, :previous_label => '&laquo;', :next_label => '&raquo;', :container => false
- first,last,total = @donations.offset+1, @[email protected], @donations.total_entries
- if total > @donations.per_page
= form_tag donations_path, :method => :get do
&nbsp;&bull;&nbsp;
%b #{first}-#{last} of #{total}
&nbsp;&bull;&nbsp; Jump to page:
= text_field_tag 'page', '', :size => 4
= submit_tag 'Go', :class => 'btn btn-outline-primary btn-sm'

= form_tag donations_path, {:method => :get} do
= render :partial => 'donation_search', :locals => {:params => @params}
.form-row
.col-md-4
= submit_tag "Search", :class => 'btn btn-success mx-1'
- unless @donations.empty?
= submit_tag 'Download to Excel', :class => 'btn btn-primary mx-1'
= popup_help_for 'download_to_excel'

- unless @donations.empty?
%table.a1-table.table.table-hover#donations
%thead
%tr
%th Customer
%th Order#
%th Date
%th Item Amount
%th Item Description or Acct Code
%th Thanks Ltr Sent?
%th Comments
%tbody
- @donations.each do |t|
%tr{:id => "donation_row_#{t.id}"}
%td= link_to t.customer.full_name, donations_path(:use_cid => 1, :cid => t.customer, :show_vouchers => true, :commit => 'Go')
%td= link_to_order_containing t
%td= t.sold_on.strftime '%D'
%td.right= number_to_currency(t.amount)
%td= t.item_description
%td= render :partial => 'donation_letter_sent', :locals => {:donation => t}
%td
= form_tag update_comment_for_donation_path(t), 'data-remote' => true, 'data-method' => 'put', 'data-type' => 'script', :class => 'form form-inline' do
= text_field_tag 'comments', t.comments, :id => "donation_comment_{t.id}", :class => 'donation-comment form-control form-control-sm'
= submit_tag '&#x2714;'.html_safe, :name => 'save', :id => "save_#{t.id}", :class => 'btn btn-sm btn-outline-success'
%br
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
end
end

resources :recurring_donations, :only => [:index, :update] do
member do
post :update_comment_for
end
end

# RSS

get '/ics/showdates.ics' => 'info#showdates'
Expand Down

0 comments on commit d6612bd

Please sign in to comment.