Skip to content

Commit

Permalink
implement review
Browse files Browse the repository at this point in the history
  • Loading branch information
njaeggi committed Aug 19, 2024
1 parent aee00b2 commit a7820a8
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 218 deletions.
74 changes: 40 additions & 34 deletions app/controllers/people/membership_invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,75 @@
# https://github.com/hitobito/hitobito_sac_cas.

class People::MembershipInvoicesController < ApplicationController

helper_method :invoice_possible?, :date_range, :currently_paying_zusatzsektionen

def create
authorize!(:update, person)
authorize!(:create, external_invoice)

invoice_form.attributes = invoice_form_params

if invoice_form.valid?
generate_invoice
redirect_to external_invoices_group_person_path(group, person)
if invoice_form.valid? && generate_invoice
redirect_to external_invoices_group_person_path(group, person), notice: t("people.membership_invoices.success_notice")
else
set_flash(:alert, message: invoice_form.errors.full_messages.join(", "))
redirect_to new_group_person_membership_invoice_path(group, person)
redirect_to new_group_person_membership_invoice_path(group, person), alert: I18n.t("people.membership_invoices.alert_notice", message: invoice_form.errors.full_messages.join(", "))
end
end

def new
authorize!(:update, person)
authorize!(:update, external_invoice)

@invoice_form = invoice_form
@group = group
@date = date
@person = person
@context = context
@member = member
end

private

def external_invoice = @external_invoice ||= ExternalInvoice.new(person: person)

def invoice_form
@invoice_form ||= People::Membership::Invoice.new({}, person)
@invoice_form ||= People::Membership::InvoiceForm.new({}, person)
end

def invoice_form_params
params.require(:people_membership_invoice).permit(:reference_date, :invoice_date, :send_date, :section_id, :new_entry, :discount)
params.require(:people_membership_invoice_form).permit(:reference_date, :invoice_date, :send_date, :section_id, :new_entry, :discount)
end

def generate_invoice
handle_exceptions do
ExternalInvoice::SacMembership.create!(
state: :draft,
year: Date.parse(@invoice_form.reference_date).year,
issued_at: @invoice_form.invoice_date,
sent_at: @invoice_form.send_date,
person: person,
link: Group.find(@invoice_form.section_id)
)
set_flash(:success)
end
ExternalInvoice::SacMembership.create(
state: :draft,
year: @invoice_form.reference_date.year,
issued_at: @invoice_form.invoice_date,
sent_at: @invoice_form.send_date,
person: person,
link: Group.find(@invoice_form.section_id)
)
end

def set_flash(type, **args)
kind = (type == :success) ? :notice : :alert
flash[kind] = t("people.membership_invoices.#{type}_notice", **args) # rubocop:disable Rails/ActionControllerFlashBeforeRender
def invoice_possible?(member, date)
memberships = member.active_memberships
memberships.present? && Invoices::Abacus::MembershipInvoice.new(member, memberships).invoice?
end

def handle_exceptions
yield
rescue => e
set_flash(:alert, message: e.message)
options = {}
if e.respond_to?(:response)
options[:extra] = {response: e.response.body.force_encoding("UTF-8")}
def date_range(attr)
if attr == :send_date
Time.zone.today.beginning_of_year..(already_member_next_year?(@person) ? Time.zone.today.next_year.end_of_year : Time.zone.today.end_of_year)
else
Time.zone.today.beginning_of_year..Time.zone.today.next_year.end_of_year
end
Raven.capture_exception(e, options)
end

def already_member_next_year?(person)
next_year = Time.zone.today.year + 1
delete_on_date = person.sac_membership.stammsektion_role.delete_on
delete_on_date >= Date.new(next_year, 1, 1) && delete_on_date <= Date.new(next_year, 12, 31)
end

def currently_paying_zusatzsektionen(member)
memberships = member.additional_membership_roles + member.new_additional_section_membership_roles
paying_memberships = memberships.select { |membership| member.paying_person?(membership.beitragskategorie) }
paying_memberships.map(&:layer_group)
end

def member
Expand Down
25 changes: 0 additions & 25 deletions app/helpers/membership_invoices_helper.rb

This file was deleted.

62 changes: 0 additions & 62 deletions app/models/people/membership/invoice.rb

This file was deleted.

42 changes: 42 additions & 0 deletions app/models/people/membership/invoice_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

# Copyright (c) 2023, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito

class People::Membership::InvoiceForm
include ActiveModel::Model
include ActiveModel::Attributes

DISCOUNTS = ["0", "50", "100"].freeze

attribute :reference_date, :date
attribute :invoice_date, :date
attribute :send_date, :date
attribute :discount, :string
attribute :new_entry, :boolean
attribute :section_id, :string

validates :reference_date, :invoice_date, :send_date, :discount, presence: true

validates_date :reference_date, :invoice_date, between: Time.zone.today.beginning_of_year..Time.zone.today.next_year.end_of_year
validates_date :send_date, between: [Time.zone.today.beginning_of_year, :send_date_end_date]

validates :discount, inclusion: {in: DISCOUNTS}

def initialize(attributes = {}, person = nil)
super(attributes)
@person = person
end

private

def send_date_end_date
@person.sac_membership.stammsektion_role.delete_on.year > date_today.year ? date_today.end_of_year : date_today.next_year.end_of_year
end

def date_today
Time.zone.today
end
end
11 changes: 6 additions & 5 deletions app/views/people/membership_invoices/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
.alert.alert-info=t(".alert_info")

= standard_form(@invoice_form, url: group_person_membership_invoices_path(@group, @person), method: :post) do |f|
%h1 Mitgliedschaftsrechnung erstellen
%h1
= t(".form_title")
= f.labeled(:reference_date) do
= f.date_field(:reference_date, class: 'date col-6', minDate: Date.today.beginning_of_year, maxDate: (Date.today.end_of_year + 1.year))
= f.date_field(:reference_date, class: 'date col-6', minDate: date_range(:reference_date).min, maxDate: date_range(:reference_date_date).max)
= f.labeled(:invoice_date) do
= f.date_field(:invoice_date, class: 'date col-6', minDate: Date.today.beginning_of_year, maxDate: (Date.today.end_of_year + 1.year))
= f.date_field(:invoice_date, class: 'date col-6', minDate: date_range(:invoice_date).min, maxDate: date_range(:invoice_date).max)
= f.labeled(:send_date) do
= f.date_field(:send_date, class: 'date col-6', minDate: Date.today.beginning_of_year, maxDate: (already_member_next_year?(@person) ? Date.today.end_of_year + 1.year : Date.today.end_of_year))
= f.date_field(:send_date, class: 'date col-6', minDate: date_range(:send_date).min, maxDate: date_range(:send_date).max)

= f.labeled(:section_id) do
- main_section = @person.sac_membership.stammsektion_role.layer_group
Expand All @@ -28,7 +29,7 @@
= f.inline_radio_button :discount, 100, "100%", true

= f.indented do
= submit_button(f, "Rechnung erstellen")
= submit_button(f, t(".create_invoice"))
= cancel_link(external_invoices_group_person_path(@group, @person))
- else
.alert.alert-warning= t(".alert_warning")
Expand Down
4 changes: 3 additions & 1 deletion config/locales/wagon.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ de:
activemodel:
attributes:
people/membership/invoice:
people/membership/invoice_form:
reference_date: "Stichtag"
invoice_date: "Rechnungsdatum"
send_date: "Versanddatum"
Expand Down Expand Up @@ -1086,8 +1086,10 @@ de:
success_notice: "Die gewünschte Rechnung wird erzeugt und an Abacus übermittelt"
alert_notice: "Die Rechnung konnte nicht erstellt werden: %{message}"
new:
form_title: "Mitgliedschaftsrechnung erstellen"
alert_info: "Nach Bestätigung dieses Dialogs wird eine Mitgliedschaftsrechnung erstellt und per E-Mail oder Post an das Mitglied versandt. Es erfolgt keine Überprüfung, ob bereits Mitgliedschaftsrechnungen existieren oder in welchem Status sich diese befinden. Es wird in jedem Fall eine neue Rechnung ausgestellt. Es werden keine Rollenmutationen oder -verlängerungen vorgenommen."
alert_warning: "Diese Person verfügt über keine eigene Mitgliedschaftsrechnung. Die Gebühren werden allenfalls mit der Rechnung einer anderen Person verrechnet."
create_invoice: "Rechnung erstellen"
mv_yearly_invoice: "MV-Jahresrechnung"
zusatzsektion_eintrittsrechnung: "Zusatzsektions-Eintrittsrechnung Sektion"

Expand Down
Loading

0 comments on commit a7820a8

Please sign in to comment.