hitobito-kjv/app/controllers/kjv/membership_fees_controller.rb
KJV Bautzen e9675d5d0e Phase 1: Beitragsstatus und Mitgliedsausweis 🤖
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 20:19:06 +02:00

93 lines
2.3 KiB
Ruby

# frozen_string_literal: true
# Copyright (c) 2026, Kreisjagdverband Bautzen e.V. This file is part of
# hitobito_kjv and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory.
# Setzt "Beitrag bezahlt bis" für eine oder mehrere Personen aus der Personenliste
# (Mehrfachauswahl) oder aus der Detailansicht einer Person.
# Angelehnt an TagListsController im Core.
class Kjv::MembershipFeesController < ApplicationController
include FilteredPeople
skip_authorization_check
skip_authorize_resource
helper_method :group, :people_ids, :people_count, :default_year, :return_url
respond_to :js, only: [:new]
def new
end
def create
year = parse_year
return redirect_back_with_alert unless year
count = update_people(year == :clear ? nil : year)
redirect_to return_url, notice: t(".success", count: count)
end
private
def update_people(year)
count = 0
Person.transaction do
manageable_people.each do |person|
person.beitrag_bezahlt_bis = year
# Validierung übergehen, damit unvollständige Altdaten den Lauf nicht blockieren.
# PaperTrail protokolliert die Änderung trotzdem im Verlauf der Person.
count += 1 if person.save(validate: false)
end
end
count
end
def parse_year
value = params[:beitrag_bezahlt_bis].to_s.strip
return :clear if params[:clear] == "1"
return unless value.match?(/\A\d{4}\z/)
year = value.to_i
year if Kjv::MembershipFee::YEARS.cover?(year)
end
def redirect_back_with_alert
redirect_to return_url, alert: t(".invalid_year")
end
def group
@group ||= Group.find(params[:group_id])
end
def people_ids
@people_ids ||= manageable_people.map(&:id)
end
def people_count
people_ids.size
end
def default_year
Date.current.year
end
def return_url
url = params[:return_url].presence
(url&.start_with?("/") && !url.start_with?("//")) ? url : group_people_path(group)
end
def manageable_people
@manageable_people ||= candidates.select do |person|
current_ability.can?(:update_membership_fee, person)
end
end
def candidates
if params[:ids] == "all"
person_filter(PersonFullReadables).entries.to_a
else
Person.where(id: list_param(:ids)).distinct.to_a
end
end
end