Skip to content

Commit

Permalink
added payment_for attribute to invoice, refs #425
Browse files Browse the repository at this point in the history
  • Loading branch information
bihorco36 committed Dec 11, 2017
1 parent efdec94 commit 2604ecd
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 10 deletions.
25 changes: 25 additions & 0 deletions app/assets/javascripts/modules/invoice_configs.js.coffee
@@ -0,0 +1,25 @@
# Copyright (c) 2012-2017, Jungwacht Blauring Schweiz. This file is part of
# hitobito 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.

app = window.App ||= {}

class app.InvoiceConfigs
constructor: () ->

showPaymentSlipSpecificAttributes: ->
payment_slip = $('#invoice_config_payment_slip').find(":selected").val()
beneficiary_control_group = $('#invoice_config_beneficiary').closest('.control-group')

if payment_slip == 'ch_besr' || payment_slip == 'ch_bes'
beneficiary_control_group.slideDown()
else
beneficiary_control_group.hide()

bind: ->
self = this
$(document).on('change', '#invoice_config_payment_slip', (e) -> self.showPaymentSlipSpecificAttributes())
$(document).on('turbolinks:load', (e) -> self.showPaymentSlipSpecificAttributes())

new app.InvoiceConfigs().bind()
2 changes: 1 addition & 1 deletion app/controllers/invoice_configs_controller.rb
Expand Up @@ -9,7 +9,7 @@ class InvoiceConfigsController < CrudController

self.nesting = Group
self.permitted_attrs = [:payment_information, :address, :iban, :account_number,
:contact_id, :payment_slip, :beneficiary]
:contact_id, :payment_slip, :beneficiary, :payment_for]

private

Expand Down
10 changes: 9 additions & 1 deletion app/models/invoice.rb
Expand Up @@ -78,6 +78,13 @@ class Invoice < ActiveRecord::Base
end
end

InvoiceConfig::PAYMENT_SLIPS.each do |payment_slip|
scope payment_slip.to_sym, -> { where(payment_slip: payment_slip) }
define_method "#{payment_slip}?" do
self.payment_slip == payment_slip
end
end

def self.to_contactables(invoices)
invoices.collect do |invoice|
next if invoice.recipient_address.blank?
Expand Down Expand Up @@ -170,7 +177,8 @@ def set_esr_number
end

def set_payment_attributes
[:address, :account_number, :iban, :payment_slip, :beneficiary].each do |at|
[:address, :account_number, :iban,
:payment_slip, :beneficiary, :payment_for].each do |at|
assign_attributes(at => invoice_config.send(at))
end
end
Expand Down
5 changes: 3 additions & 2 deletions app/models/invoice_config.rb
Expand Up @@ -28,14 +28,15 @@ class InvoiceConfig < ActiveRecord::Base

validates :group_id, uniqueness: true
validates :address, presence: true, on: :update
validates :beneficiary, presence: true, on: :update
validates :payment_for, presence: true, on: :update
validates :beneficiary, presence: true, on: :update, if: proc { |ic| ic.ch_bes? || ic.ch_besr? }


# TODO: probably the if condition is not correct, it has to be specified by the product owner
validates :iban, presence: true, on: :update, if: proc { |ic| ic.ch_es? || ic.ch_bes? }
validates :iban, format: { with: /\A[A-Z]{2}[0-9]{2}\s?([A-Z]|[0-9]\s?){12,30}\z/ },
on: :update, allow_blank: true

# TODO: Same as todo above
validates :account_number, presence: true, on: :update
validates :account_number, format: { with: /\A[0-9][-0-9]{4,20}[0-9]\z/ },
on: :update, allow_blank: true
Expand Down
1 change: 1 addition & 0 deletions app/views/invoice_configs/_attrs.html.haml
Expand Up @@ -10,6 +10,7 @@
= labeled_attr(entry, :payment_information)

= labeled(captionize(:payment_slip, object_class(entry)), entry.payment_slip_label)
= labeled_attr(entry, :payment_for)
= labeled_attr(entry, :beneficiary)
= labeled_attr(entry, :address)
= labeled_attr(entry, :account_number)
Expand Down
1 change: 1 addition & 0 deletions app/views/invoice_configs/_form.html.haml
Expand Up @@ -11,6 +11,7 @@
%hr
= f.labeled(:payment_slip) do
= f.collection_select(:payment_slip, InvoiceConfig.payment_slip_labels.to_a, :first, :second, {}, class: 'span6')
= f.labeled_input_field :payment_for
= f.labeled_input_field :beneficiary
= f.labeled_input_field :address
= f.labeled_input_field :account_number, help: t('.account_number_example')
Expand Down
3 changes: 2 additions & 1 deletion app/views/invoices/_infos.html.haml
Expand Up @@ -12,7 +12,8 @@

.invoice-payment
= labeled(captionize(:payment_slip, object_class(entry)), entry.payment_slip_label)
= labeled_attr(entry, :beneficiary)
= labeled_attr(entry, :payment_for)
= labeled_attr(entry, :beneficiary) if entry.ch_besr? || entry.ch_bes?
= labeled_attr(entry, :address)
= labeled_attr(entry, :account_number)
= labeled_attr(entry, :iban) if entry.iban.present?
Expand Down
2 changes: 2 additions & 0 deletions config/locales/models.de.yml
Expand Up @@ -635,6 +635,7 @@ de:
payment_purpose: Zahlungszweck
address: Absender Adresse
beneficiary: Zugunsten von
payment_for: Einzahlung für
account_number: Kontonummer
iban: IBAN
payment_slip: Einzahlungsschein
Expand All @@ -658,6 +659,7 @@ de:
contact: Kontaktperson
contact_id: Kontaktperson
beneficiary: Zugunsten von
payment_for: Einzahlung für
payment_slip: Einzahlungsschein
payment_slips:
ch_es: Roter Einzahlungsschein Post (CH)
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20171211085054_add_payment_for_to_invoice.rb
@@ -0,0 +1,6 @@
class AddPaymentForToInvoice < ActiveRecord::Migration
def change
add_column :invoices, :payment_for, :text
add_column :invoice_configs, :payment_for, :text
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171206164713) do
ActiveRecord::Schema.define(version: 20171211085054) do

create_table "additional_emails", force: :cascade do |t|
t.integer "contactable_id", limit: 4, null: false
Expand Down Expand Up @@ -260,6 +260,7 @@
t.string "iban"
t.string "payment_slip", default: "ch_es", null: false
t.text "beneficiary"
t.text "payment_for"
end

add_index "invoice_configs", ["contact_id"], name: "index_invoice_configs_on_contact_id"
Expand Down Expand Up @@ -299,6 +300,7 @@
t.text "payment_information"
t.string "payment_slip", default: "ch_es", null: false
t.text "beneficiary"
t.text "payment_for"
end

add_index "invoices", ["esr_number"], name: "index_invoices_on_esr_number"
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/invoice_configs.yml
Expand Up @@ -15,6 +15,7 @@ top_layer:
group: top_layer
sequence_number: 1
beneficiary: 'Hitobito AG'
payment_for: 'Hans Gerber'
address: 'top layer address'
iban: 'CH93 0076 2011 6238 5295 7'
account_number: '10-5318-01'
Expand All @@ -23,6 +24,7 @@ bottom_layer_one:
group: bottom_layer_one
sequence_number: 1
beneficiary: 'Hitobito AG'
payment_for: 'Fritz von Gunten'
address: 'bottom layer one address'
iban: 'CH93 0076 2011 6238 5295 8'
account_number: '10-3991-803'
Expand All @@ -31,6 +33,7 @@ bottom_layer_two:
group: bottom_layer_two
sequence_number: 1
beneficiary: 'Hitobito AG'
payment_for: 'Anna Streit'
address: 'bottom layer two address'
iban: 'CH84 0221 1981 6169 5329 8'
account_number: '10-3991-803'
4 changes: 2 additions & 2 deletions spec/models/invoice_config_spec.rb
Expand Up @@ -28,8 +28,8 @@
expect(invoice_config.errors.full_messages).to include('Kontonummer ist nicht gültig')
end

it 'validates presence of beneficiary' do
invoice_config.update(beneficiary: '')
it 'validates presence of payment_for' do
invoice_config.update(payment_for: '')

expect(invoice_config).not_to be_valid
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/invoice_spec.rb
Expand Up @@ -189,7 +189,7 @@
it 'soft deleting group does not delete invoices' do
other = Group::BottomLayer.create!(name: 'x', parent: group)
other.invoice_config.update(iban: 'CH12 2134 1234 1234 1234',
beneficiary: 'fuu',
payment_for: 'fuu',
address: 'fuu',
account_number: 123_443)

Expand All @@ -200,7 +200,7 @@
it 'hard deleting group does delete invoices' do
other = Group::BottomLayer.create!(name: 'x', parent: group)
other.invoice_config.update(iban: 'CH12 2134 1234 1234 1234',
beneficiary: 'fuu',
payment_for: 'fuu',
address: 'fuu',
account_number: 123_443)

Expand Down

0 comments on commit 2604ecd

Please sign in to comment.