From 2604ecddfc79f6f69d9fb2bcf5e28251761ba667 Mon Sep 17 00:00:00 2001 From: Janiss Binder Date: Mon, 11 Dec 2017 11:34:05 +0100 Subject: [PATCH] added payment_for attribute to invoice, refs #425 --- .../modules/invoice_configs.js.coffee | 25 +++++++++++++++++++ app/controllers/invoice_configs_controller.rb | 2 +- app/models/invoice.rb | 10 +++++++- app/models/invoice_config.rb | 5 ++-- app/views/invoice_configs/_attrs.html.haml | 1 + app/views/invoice_configs/_form.html.haml | 1 + app/views/invoices/_infos.html.haml | 3 ++- config/locales/models.de.yml | 2 ++ ...171211085054_add_payment_for_to_invoice.rb | 6 +++++ db/schema.rb | 4 ++- spec/fixtures/invoice_configs.yml | 3 +++ spec/models/invoice_config_spec.rb | 4 +-- spec/models/invoice_spec.rb | 4 +-- 13 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 app/assets/javascripts/modules/invoice_configs.js.coffee create mode 100644 db/migrate/20171211085054_add_payment_for_to_invoice.rb diff --git a/app/assets/javascripts/modules/invoice_configs.js.coffee b/app/assets/javascripts/modules/invoice_configs.js.coffee new file mode 100644 index 0000000000..8144c4560c --- /dev/null +++ b/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() diff --git a/app/controllers/invoice_configs_controller.rb b/app/controllers/invoice_configs_controller.rb index b65dec7074..31a44ced74 100644 --- a/app/controllers/invoice_configs_controller.rb +++ b/app/controllers/invoice_configs_controller.rb @@ -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 diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 3ec77c27ff..32acb4e1ab 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -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? @@ -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 diff --git a/app/models/invoice_config.rb b/app/models/invoice_config.rb index 2d4a87f81d..76239c277b 100644 --- a/app/models/invoice_config.rb +++ b/app/models/invoice_config.rb @@ -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 diff --git a/app/views/invoice_configs/_attrs.html.haml b/app/views/invoice_configs/_attrs.html.haml index 845cb186c5..f78f49da8e 100644 --- a/app/views/invoice_configs/_attrs.html.haml +++ b/app/views/invoice_configs/_attrs.html.haml @@ -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) diff --git a/app/views/invoice_configs/_form.html.haml b/app/views/invoice_configs/_form.html.haml index c842c39382..67dad185f0 100644 --- a/app/views/invoice_configs/_form.html.haml +++ b/app/views/invoice_configs/_form.html.haml @@ -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') diff --git a/app/views/invoices/_infos.html.haml b/app/views/invoices/_infos.html.haml index 3fd04d41dc..db0db2c4a2 100644 --- a/app/views/invoices/_infos.html.haml +++ b/app/views/invoices/_infos.html.haml @@ -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? diff --git a/config/locales/models.de.yml b/config/locales/models.de.yml index fcc9ce9229..c87214a1f0 100644 --- a/config/locales/models.de.yml +++ b/config/locales/models.de.yml @@ -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 @@ -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) diff --git a/db/migrate/20171211085054_add_payment_for_to_invoice.rb b/db/migrate/20171211085054_add_payment_for_to_invoice.rb new file mode 100644 index 0000000000..46bc008d7c --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index f36e306fd7..81ae91d7b4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 @@ -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" @@ -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" diff --git a/spec/fixtures/invoice_configs.yml b/spec/fixtures/invoice_configs.yml index 87783538bc..8f8d98fd67 100644 --- a/spec/fixtures/invoice_configs.yml +++ b/spec/fixtures/invoice_configs.yml @@ -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' @@ -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' @@ -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' diff --git a/spec/models/invoice_config_spec.rb b/spec/models/invoice_config_spec.rb index 0ea842f537..46effabee7 100644 --- a/spec/models/invoice_config_spec.rb +++ b/spec/models/invoice_config_spec.rb @@ -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 diff --git a/spec/models/invoice_spec.rb b/spec/models/invoice_spec.rb index 18dd267b1e..de6213bf16 100644 --- a/spec/models/invoice_spec.rb +++ b/spec/models/invoice_spec.rb @@ -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) @@ -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)