2022-06-25 12:48:46 +00:00
|
|
|
class ContributionsController < ApplicationController
|
2022-06-25 22:45:47 +00:00
|
|
|
before_action :require_login
|
2022-06-25 12:48:46 +00:00
|
|
|
before_action :set_member, only: %i[ new create ]
|
2022-06-25 20:27:02 +00:00
|
|
|
before_action :set_contribution, only: %i[ edit update delete destroy ]
|
2022-06-25 12:48:46 +00:00
|
|
|
|
2023-03-19 17:49:31 +00:00
|
|
|
def to_csv(collection, keys)
|
|
|
|
csv = [keys].join(",") + "\n"
|
|
|
|
collection.each do |item|
|
|
|
|
csv << keys.map { |k| item.send(k) }.join(",") + "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
csv
|
|
|
|
end
|
|
|
|
|
2023-03-19 15:15:56 +00:00
|
|
|
# GET /contributions
|
|
|
|
def index
|
|
|
|
@contributions = Contribution.all.order(payment_on: 'DESC')
|
|
|
|
@contributions = @contributions.select {|c| c.payment_on.year == params[:year].to_i } if params[:year]
|
2023-03-19 17:49:31 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.csv { render plain: to_csv(@contributions, %i[payment_on member_number member_display_name amount payment_method]) }
|
|
|
|
end
|
2023-03-19 15:15:56 +00:00
|
|
|
end
|
|
|
|
|
2023-03-23 18:08:25 +00:00
|
|
|
# GET /contributions/due
|
|
|
|
def due
|
|
|
|
@due_members = Member.where(status: %i[expired cancelled]).to_a
|
|
|
|
|
|
|
|
@due_contributions = (2000..Date.today.year)
|
|
|
|
.to_a
|
|
|
|
.map do |year|
|
|
|
|
[year, @due_members.select {|m| m.expires_on < Date.new(year, 12, 31) }.map { |m|
|
|
|
|
OpenStruct.new(
|
|
|
|
member: m,
|
2023-08-07 12:50:50 +00:00
|
|
|
amount: m.employed? ? Config.regular_payment_value : Config.reduced_payment_value,
|
2023-03-23 18:08:25 +00:00
|
|
|
)
|
|
|
|
}]
|
|
|
|
end
|
|
|
|
.reject { |(_, m)| m.empty? }
|
|
|
|
end
|
|
|
|
|
2023-03-19 15:15:56 +00:00
|
|
|
# GET /contributions/new
|
2022-06-25 12:48:46 +00:00
|
|
|
def new
|
|
|
|
@contribution = Contribution.new
|
|
|
|
end
|
|
|
|
|
2023-03-19 15:15:56 +00:00
|
|
|
# GET /contributions/1/edit
|
2022-06-25 12:48:46 +00:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
2023-03-19 15:15:56 +00:00
|
|
|
# POST /contributions
|
2022-06-25 12:48:46 +00:00
|
|
|
def create
|
2023-08-07 12:50:50 +00:00
|
|
|
success = @member.register_contribution(
|
|
|
|
contribution_params,
|
|
|
|
params.dig(:contribution, :overriden_expires_on),
|
|
|
|
params.dig(:contribution, :should_send_notification) == "1")
|
2022-06-25 12:48:46 +00:00
|
|
|
|
2023-08-07 12:50:50 +00:00
|
|
|
if success
|
|
|
|
redirect_to @member, notice: "Contribution was successfully created."
|
|
|
|
else
|
|
|
|
render :new, status: :unprocessable_entity
|
2022-06-25 12:48:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-19 15:15:56 +00:00
|
|
|
# PATCH/PUT /contributions/1
|
2022-06-25 20:27:02 +00:00
|
|
|
def update
|
|
|
|
if @contribution.update(contribution_params)
|
|
|
|
redirect_to @contribution.member, notice: "Contribution was successfully updated."
|
|
|
|
else
|
|
|
|
render :edit, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
#
|
2023-03-19 15:15:56 +00:00
|
|
|
# GET /contributions/1/delete
|
2022-06-25 20:27:02 +00:00
|
|
|
def delete
|
|
|
|
end
|
|
|
|
|
2023-03-19 15:15:56 +00:00
|
|
|
# DELETE /contributions/1
|
2022-06-25 20:27:02 +00:00
|
|
|
def destroy
|
|
|
|
@member = @contribution.member
|
|
|
|
@contribution.destroy
|
|
|
|
|
|
|
|
redirect_to @member, notice: "Member personal data permanently removed."
|
|
|
|
end
|
2022-06-25 12:48:46 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
|
|
def set_member
|
|
|
|
@member = Member.find(params[:member_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_contribution
|
|
|
|
@contribution = Contribution.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
# Only allow a list of trusted parameters through.
|
|
|
|
def contribution_params
|
|
|
|
params.fetch(:contribution, {}).permit(:eurocents, :payment_method, :payment_on, :payment_reference)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|