From f93ec297adfcffb775a6b7fcbda9b9d5deeaa88d Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 10:41:55 +0000 Subject: [PATCH 01/11] Add ability to mark an invoice as paid (#1031) Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- app/controllers/invoices_controller.rb | 13 +++++++++++ app/policies/invoice_policy.rb | 4 ++++ app/views/invoices/index.html.erb | 11 +++++++++ config/routes.rb | 1 + .../invoices_controller/mark_as_paid_spec.rb | 23 +++++++++++++++++++ 5 files changed, 52 insertions(+) create mode 100644 spec/controllers/invoices_controller/mark_as_paid_spec.rb diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index ccfdcb3f3..9e67046d0 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -78,6 +78,19 @@ def send_invoice redirect_to invoices_path end + def mark_as_paid + @invoice = Invoice.find(params.expect(:id)) + authorize @invoice + + if @invoice.paid? + flash[:error] = 'Factuur is al betaald' + else + @invoice.update!(status: 'paid') + flash[:success] = "Factuur #{@invoice.human_id} gemarkeerd als betaald" + end + redirect_to invoices_path + end + private def integer_id?(id) diff --git a/app/policies/invoice_policy.rb b/app/policies/invoice_policy.rb index 61ba0a17f..edd0b2962 100644 --- a/app/policies/invoice_policy.rb +++ b/app/policies/invoice_policy.rb @@ -7,6 +7,10 @@ def send_invoice? user&.treasurer? end + def mark_as_paid? + user&.treasurer? + end + def download? user&.treasurer? end diff --git a/app/views/invoices/index.html.erb b/app/views/invoices/index.html.erb index ffa778d96..21c9dfb73 100644 --- a/app/views/invoices/index.html.erb +++ b/app/views/invoices/index.html.erb @@ -35,6 +35,9 @@ <% if policy(Invoice).send_invoice? %> Verstuur <% end %> + <% if policy(Invoice).mark_as_paid? %> + Betaald + <% end %> <% if policy(Invoice).download? %> Download <% end %> @@ -80,6 +83,14 @@ <% end %> <% end %> + <% if policy(Invoice).mark_as_paid? %> + + <% unless invoice.paid? %> + <%= button_to 'Markeer als betaald', mark_as_paid_invoice_path(invoice), method: :post, class: 'btn btn-secondary', + form: { data: { turbo_confirm: "Factuur #{invoice.human_id} als betaald markeren?" } } %> + <% end %> + + <% end %> <% if policy(Invoice).download? %> <%= link_to 'Downloaden', invoice_path(invoice, format: :pdf), class: 'btn btn-primary', download: "Factuur-#{invoice.human_id}.pdf" %> diff --git a/config/routes.rb b/config/routes.rb index 9721c42e8..6ec779322 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,6 +54,7 @@ member do get :pay post :send_invoice + post :mark_as_paid end end resources :zatladder, only: %i[index] diff --git a/spec/controllers/invoices_controller/mark_as_paid_spec.rb b/spec/controllers/invoices_controller/mark_as_paid_spec.rb new file mode 100644 index 000000000..12d4c95a5 --- /dev/null +++ b/spec/controllers/invoices_controller/mark_as_paid_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +describe InvoicesController do + describe 'POST mark_as_paid' do + let(:invoice) { create(:invoice) } + + it 'marks the invoice as paid' do + sign_in create(:user, :treasurer) + post :mark_as_paid, params: { id: invoice.id } + + expect(invoice.reload.status).to eq 'paid' + expect(response).to redirect_to(invoices_path) + end + + it 'is forbidden for a normal user' do + sign_in create(:user) + post :mark_as_paid, params: { id: invoice.id } + + expect(response).to have_http_status(:forbidden) + expect(invoice.reload.status).to eq 'pending' + end + end +end From a305dd38acd2cbb708a457e668314144c23a8607 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 10:46:55 +0000 Subject: [PATCH 02/11] Cover already-paid case for mark_as_paid Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- .../controllers/invoices_controller/mark_as_paid_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/controllers/invoices_controller/mark_as_paid_spec.rb b/spec/controllers/invoices_controller/mark_as_paid_spec.rb index 12d4c95a5..c68341860 100644 --- a/spec/controllers/invoices_controller/mark_as_paid_spec.rb +++ b/spec/controllers/invoices_controller/mark_as_paid_spec.rb @@ -12,6 +12,15 @@ expect(response).to redirect_to(invoices_path) end + it 'does not change an invoice that is already paid' do + invoice.update!(status: 'paid') + sign_in create(:user, :treasurer) + post :mark_as_paid, params: { id: invoice.id } + + expect(flash[:error]).to eq 'Factuur is al betaald' + expect(response).to redirect_to(invoices_path) + end + it 'is forbidden for a normal user' do sign_in create(:user) post :mark_as_paid, params: { id: invoice.id } From 2670a9942ef8f7cf29b41eed9410b327ac45338e Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 10:49:26 +0000 Subject: [PATCH 03/11] Create credit mutation when marking invoice as paid Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- app/controllers/invoices_controller.rb | 2 +- app/models/invoice.rb | 7 +++++++ .../invoices_controller/mark_as_paid_spec.rb | 20 +++++++++++++------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index 9e67046d0..0e44897f1 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -85,7 +85,7 @@ def mark_as_paid if @invoice.paid? flash[:error] = 'Factuur is al betaald' else - @invoice.update!(status: 'paid') + @invoice.mark_as_paid!(current_user) flash[:success] = "Factuur #{@invoice.human_id} gemarkeerd als betaald" end redirect_to invoices_path diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 956fa84e4..407f2b972 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -31,6 +31,13 @@ def row_amount rows.sum(&:total) end + def mark_as_paid!(created_by) + transaction do + CreditMutation.create!(user:, amount:, description: "Betaling factuur #{human_id}", created_by:) + update!(status: 'paid') + end + end + private def set_human_id diff --git a/spec/controllers/invoices_controller/mark_as_paid_spec.rb b/spec/controllers/invoices_controller/mark_as_paid_spec.rb index c68341860..6c8d3fa99 100644 --- a/spec/controllers/invoices_controller/mark_as_paid_spec.rb +++ b/spec/controllers/invoices_controller/mark_as_paid_spec.rb @@ -2,11 +2,17 @@ describe InvoicesController do describe 'POST mark_as_paid' do - let(:invoice) { create(:invoice) } + let!(:invoice) { create(:invoice) } - it 'marks the invoice as paid' do - sign_in create(:user, :treasurer) - post :mark_as_paid, params: { id: invoice.id } + it 'marks the invoice as paid and creates a credit mutation' do + treasurer = create(:user, :treasurer) + sign_in treasurer + + expect { post :mark_as_paid, params: { id: invoice.id } }.to change(CreditMutation, :count).by(1) + mutation = CreditMutation.last + expect(mutation.user).to eq invoice.user + expect(mutation.amount).to eq invoice.amount + expect(mutation.created_by).to eq treasurer expect(invoice.reload.status).to eq 'paid' expect(response).to redirect_to(invoices_path) @@ -15,7 +21,8 @@ it 'does not change an invoice that is already paid' do invoice.update!(status: 'paid') sign_in create(:user, :treasurer) - post :mark_as_paid, params: { id: invoice.id } + + expect { post :mark_as_paid, params: { id: invoice.id } }.not_to change(CreditMutation, :count) expect(flash[:error]).to eq 'Factuur is al betaald' expect(response).to redirect_to(invoices_path) @@ -23,7 +30,8 @@ it 'is forbidden for a normal user' do sign_in create(:user) - post :mark_as_paid, params: { id: invoice.id } + + expect { post :mark_as_paid, params: { id: invoice.id } }.not_to change(CreditMutation, :count) expect(response).to have_http_status(:forbidden) expect(invoice.reload.status).to eq 'pending' From 5304f909d96af99474cf5a1689bea77932669253 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 10:52:24 +0000 Subject: [PATCH 04/11] Split mark_as_paid spec examples for rubocop Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- .../invoices_controller/mark_as_paid_spec.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/spec/controllers/invoices_controller/mark_as_paid_spec.rb b/spec/controllers/invoices_controller/mark_as_paid_spec.rb index 6c8d3fa99..0ee2dade8 100644 --- a/spec/controllers/invoices_controller/mark_as_paid_spec.rb +++ b/spec/controllers/invoices_controller/mark_as_paid_spec.rb @@ -4,23 +4,24 @@ describe 'POST mark_as_paid' do let!(:invoice) { create(:invoice) } - it 'marks the invoice as paid and creates a credit mutation' do - treasurer = create(:user, :treasurer) - sign_in treasurer + let(:treasurer) { create(:user, :treasurer) } - expect { post :mark_as_paid, params: { id: invoice.id } }.to change(CreditMutation, :count).by(1) - mutation = CreditMutation.last - expect(mutation.user).to eq invoice.user - expect(mutation.amount).to eq invoice.amount - expect(mutation.created_by).to eq treasurer + before { sign_in treasurer } + + it 'marks the invoice as paid' do + post :mark_as_paid, params: { id: invoice.id } expect(invoice.reload.status).to eq 'paid' expect(response).to redirect_to(invoices_path) end + it 'creates a credit mutation for the invoice user' do + expect { post :mark_as_paid, params: { id: invoice.id } }.to change(CreditMutation, :count).by(1) + expect(CreditMutation.last).to have_attributes(user: invoice.user, amount: invoice.amount, created_by: treasurer) + end + it 'does not change an invoice that is already paid' do invoice.update!(status: 'paid') - sign_in create(:user, :treasurer) expect { post :mark_as_paid, params: { id: invoice.id } }.not_to change(CreditMutation, :count) From 91e39cca3fbcbc12253e9bd3348a26f90bdbe8b7 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 10:58:48 +0000 Subject: [PATCH 05/11] Add booking number to invoices and booked status Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- app/controllers/invoices_controller.rb | 21 +++++++++-- app/models/invoice.rb | 12 ++++++- app/policies/invoice_policy.rb | 4 +++ app/views/invoices/index.html.erb | 11 +++++- app/views/invoices/show.html.erb | 4 +-- config/locales/nl.yml | 1 + config/routes.rb | 1 + ...02000001_add_booking_number_to_invoices.rb | 5 +++ db/schema.rb | 3 +- .../invoices_controller/book_spec.rb | 36 +++++++++++++++++++ 10 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20261202000001_add_booking_number_to_invoices.rb create mode 100644 spec/controllers/invoices_controller/book_spec.rb diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index 0e44897f1..d9059e3d2 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -50,7 +50,7 @@ def pay # rubocop:disable Metrics/AbcSize, Metrics/MethodLength authorize @invoice, :pay? unless token_based_access - if @invoice.paid? + if @invoice.settled? redirect_to invoice_path params[:id] return end @@ -82,7 +82,7 @@ def mark_as_paid @invoice = Invoice.find(params.expect(:id)) authorize @invoice - if @invoice.paid? + if @invoice.settled? flash[:error] = 'Factuur is al betaald' else @invoice.mark_as_paid!(current_user) @@ -91,6 +91,23 @@ def mark_as_paid redirect_to invoices_path end + def book + @invoice = Invoice.find(params.expect(:id)) + authorize @invoice + + booking_number = params.expect(invoice: [:booking_number])[:booking_number].to_s.strip + if !@invoice.settled? + flash[:error] = 'Factuur is nog niet betaald' + elsif booking_number.blank? + flash[:error] = 'Vul een boeknummer in' + elsif @invoice.update(booking_number:) + flash[:success] = "Factuur #{@invoice.human_id} ingeboekt onder boeknummer #{booking_number}" + else + flash[:error] = @invoice.errors.full_messages.join(', ') + end + redirect_to invoices_path + end + private def integer_id?(id) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 407f2b972..78b9c6fc1 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -1,7 +1,7 @@ class Invoice < ApplicationRecord has_secure_token - enum :status, { pending: 0, sent: 1, paid: 3 } + enum :status, { pending: 0, sent: 1, paid: 3, booked: 4 } belongs_to :user belongs_to :activity @@ -14,6 +14,7 @@ class Invoice < ApplicationRecord validate :activity_is_locked before_create :set_human_id + before_save :set_booked_status def name name_override || user.name @@ -31,6 +32,11 @@ def row_amount rows.sum(&:total) end + # Paid, either still to be booked or already booked + def settled? + paid? || booked? + end + def mark_as_paid!(created_by) transaction do CreditMutation.create!(user:, amount:, description: "Betaling factuur #{human_id}", created_by:) @@ -40,6 +46,10 @@ def mark_as_paid!(created_by) private + def set_booked_status + self.status = 'booked' if booking_number.present? && paid? + end + def set_human_id year_prefix = Time.zone.now.year.to_s this_year_invoices = Invoice.where('human_id LIKE ?', "#{year_prefix}%") diff --git a/app/policies/invoice_policy.rb b/app/policies/invoice_policy.rb index edd0b2962..4549c64ba 100644 --- a/app/policies/invoice_policy.rb +++ b/app/policies/invoice_policy.rb @@ -11,6 +11,10 @@ def mark_as_paid? user&.treasurer? end + def book? + user&.treasurer? + end + def download? user&.treasurer? end diff --git a/app/views/invoices/index.html.erb b/app/views/invoices/index.html.erb index 21c9dfb73..7f84d3e76 100644 --- a/app/views/invoices/index.html.erb +++ b/app/views/invoices/index.html.erb @@ -37,6 +37,7 @@ <% end %> <% if policy(Invoice).mark_as_paid? %> Betaald + Boeknummer <% end %> <% if policy(Invoice).download? %> Download @@ -85,11 +86,19 @@ <% end %> <% if policy(Invoice).mark_as_paid? %> - <% unless invoice.paid? %> + <% unless invoice.settled? %> <%= button_to 'Markeer als betaald', mark_as_paid_invoice_path(invoice), method: :post, class: 'btn btn-secondary', form: { data: { turbo_confirm: "Factuur #{invoice.human_id} als betaald markeren?" } } %> <% end %> + + <% if invoice.settled? %> + <%= form_with url: book_invoice_path(invoice), scope: :invoice, method: :patch, class: 'd-flex gap-1' do |f| %> + <%= f.text_field :booking_number, value: invoice.booking_number, class: 'form-control form-control-sm', placeholder: 'Boeknummer', aria: { label: 'Boeknummer' } %> + <%= f.submit 'Inboeken', class: 'btn btn-sm btn-primary' %> + <% end %> + <% end %> + <% end %> <% if policy(Invoice).download? %> diff --git a/app/views/invoices/show.html.erb b/app/views/invoices/show.html.erb index 9d60067d6..4e8446084 100644 --- a/app/views/invoices/show.html.erb +++ b/app/views/invoices/show.html.erb @@ -54,7 +54,7 @@ Status - <% if @invoice.paid? %> + <% if @invoice.settled? %> Betaald <% else %> Niet betaald @@ -65,7 +65,7 @@ - <% if !@invoice.paid? && Rails.application.config.x.mollie_api_key.present? %> + <% if !@invoice.settled? && Rails.application.config.x.mollie_api_key.present? %> <%= link_to pay_invoice_url @invoice.token do %> <% end %> diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 27709e1cb..accd67994 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -27,6 +27,7 @@ nl: canceled: 'geanuleerd' expired: 'verlopen' sent: 'verstuurd' + booked: 'ingeboekt' views: pagination: diff --git a/config/routes.rb b/config/routes.rb index 6ec779322..635805543 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -55,6 +55,7 @@ get :pay post :send_invoice post :mark_as_paid + patch :book end end resources :zatladder, only: %i[index] diff --git a/db/migrate/20261202000001_add_booking_number_to_invoices.rb b/db/migrate/20261202000001_add_booking_number_to_invoices.rb new file mode 100644 index 000000000..1dbed5b94 --- /dev/null +++ b/db/migrate/20261202000001_add_booking_number_to_invoices.rb @@ -0,0 +1,5 @@ +class AddBookingNumberToInvoices < ActiveRecord::Migration[7.2] + def change + add_column :invoices, :booking_number, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 5c8a7a21d..ca7bd6450 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2026_09_16_095221) do +ActiveRecord::Schema[7.2].define(version: 2026_12_02_000001) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -66,6 +66,7 @@ t.string "email_override" t.string "name_override" t.string "token" + t.string "booking_number" t.index ["activity_id"], name: "index_invoices_on_activity_id" t.index ["token"], name: "index_invoices_on_token", unique: true t.index ["user_id"], name: "index_invoices_on_user_id" diff --git a/spec/controllers/invoices_controller/book_spec.rb b/spec/controllers/invoices_controller/book_spec.rb new file mode 100644 index 000000000..5bd6288aa --- /dev/null +++ b/spec/controllers/invoices_controller/book_spec.rb @@ -0,0 +1,36 @@ +require 'rails_helper' + +describe InvoicesController do + describe 'PATCH book' do + let!(:invoice) { create(:invoice, status: 'paid') } + + it 'stores the booking number and sets the status to booked' do + sign_in create(:user, :treasurer) + patch :book, params: { id: invoice.id, invoice: { booking_number: ' 2026-123 ' } } + + expect(invoice.reload).to have_attributes(booking_number: '2026-123', status: 'booked') + end + + it 'does not book without a booking number' do + sign_in create(:user, :treasurer) + patch :book, params: { id: invoice.id, invoice: { booking_number: ' ' } } + + expect(invoice.reload.status).to eq 'paid' + end + + it 'does not book an unpaid invoice' do + unpaid = create(:invoice) + sign_in create(:user, :treasurer) + patch :book, params: { id: unpaid.id, invoice: { booking_number: '1' } } + + expect(unpaid.reload.status).to eq 'pending' + end + + it 'is forbidden for a normal user' do + sign_in create(:user) + patch :book, params: { id: invoice.id, invoice: { booking_number: '1' } } + + expect(response).to have_http_status(:forbidden) + end + end +end From 581a067c7810d785ba77be7e76ad898531d44d55 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 11:00:35 +0000 Subject: [PATCH 06/11] Lock invoice when marking as paid and handle credit validation errors Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- app/controllers/invoices_controller.rb | 10 ++++++---- app/models/invoice.rb | 5 ++++- .../invoices_controller/mark_as_paid_spec.rb | 13 +++++++++++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index d9059e3d2..724f70275 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -82,13 +82,15 @@ def mark_as_paid @invoice = Invoice.find(params.expect(:id)) authorize @invoice - if @invoice.settled? - flash[:error] = 'Factuur is al betaald' - else - @invoice.mark_as_paid!(current_user) + if @invoice.mark_as_paid!(current_user) flash[:success] = "Factuur #{@invoice.human_id} gemarkeerd als betaald" + else + flash[:error] = 'Factuur is al betaald' end redirect_to invoices_path + rescue ActiveRecord::RecordInvalid => e + flash[:error] = e.record.errors.full_messages.join(', ') + redirect_to invoices_path end def book diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 78b9c6fc1..2853d0c29 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -37,8 +37,11 @@ def settled? paid? || booked? end + # Returns false when the invoice was already paid (also checked under a row lock to avoid double credit) def mark_as_paid!(created_by) - transaction do + with_lock do + next false if settled? + CreditMutation.create!(user:, amount:, description: "Betaling factuur #{human_id}", created_by:) update!(status: 'paid') end diff --git a/spec/controllers/invoices_controller/mark_as_paid_spec.rb b/spec/controllers/invoices_controller/mark_as_paid_spec.rb index 0ee2dade8..d56524450 100644 --- a/spec/controllers/invoices_controller/mark_as_paid_spec.rb +++ b/spec/controllers/invoices_controller/mark_as_paid_spec.rb @@ -2,7 +2,7 @@ describe InvoicesController do describe 'POST mark_as_paid' do - let!(:invoice) { create(:invoice) } + let!(:invoice) { create(:invoice).tap { |i| create(:invoice_row, invoice: i, amount: 2, price: 20) } } let(:treasurer) { create(:user, :treasurer) } @@ -17,7 +17,7 @@ it 'creates a credit mutation for the invoice user' do expect { post :mark_as_paid, params: { id: invoice.id } }.to change(CreditMutation, :count).by(1) - expect(CreditMutation.last).to have_attributes(user: invoice.user, amount: invoice.amount, created_by: treasurer) + expect(CreditMutation.last).to have_attributes(user: invoice.user, amount: 40, created_by: treasurer) end it 'does not change an invoice that is already paid' do @@ -29,6 +29,15 @@ expect(response).to redirect_to(invoices_path) end + it 'shows an error instead of crashing when the amount exceeds the credit mutation limit' do + create(:invoice_row, invoice:, amount: 1, price: 6000) + patch_count = -> { CreditMutation.count } + + expect { post :mark_as_paid, params: { id: invoice.id } }.not_to change(patch_count, :call) + expect(invoice.reload.status).to eq 'pending' + expect(flash[:error]).to be_present + end + it 'is forbidden for a normal user' do sign_in create(:user) From d445f1e0debb05ea725766062e609bc32689c6fc Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 11:02:50 +0000 Subject: [PATCH 07/11] Fix lint offenses and move booking validation into the model Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- app/controllers/invoices_controller.rb | 12 ++++-------- app/models/invoice.rb | 8 +++++++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index 724f70275..1cd701181 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -1,4 +1,4 @@ -class InvoicesController < ApplicationController +class InvoicesController < ApplicationController # rubocop:disable Metrics/ClassLength include ApplicationHelper before_action :authenticate_user!, except: %w[show pay] @@ -97,13 +97,9 @@ def book @invoice = Invoice.find(params.expect(:id)) authorize @invoice - booking_number = params.expect(invoice: [:booking_number])[:booking_number].to_s.strip - if !@invoice.settled? - flash[:error] = 'Factuur is nog niet betaald' - elsif booking_number.blank? - flash[:error] = 'Vul een boeknummer in' - elsif @invoice.update(booking_number:) - flash[:success] = "Factuur #{@invoice.human_id} ingeboekt onder boeknummer #{booking_number}" + @invoice.booking_number = params.expect(invoice: [:booking_number])[:booking_number].to_s.strip + if @invoice.save(context: :booking) + flash[:success] = "Factuur #{@invoice.human_id} ingeboekt onder boeknummer #{@invoice.booking_number}" else flash[:error] = @invoice.errors.full_messages.join(', ') end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 2853d0c29..3e37f14f7 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -12,9 +12,11 @@ class Invoice < ApplicationRecord validates :token, uniqueness: true validate :activity_is_locked + validates :booking_number, presence: { message: 'moet ingevuld zijn' }, on: :booking + validate :settled_before_booking, on: :booking - before_create :set_human_id before_save :set_booked_status + before_create :set_human_id def name name_override || user.name @@ -49,6 +51,10 @@ def mark_as_paid!(created_by) private + def settled_before_booking + errors.add(:base, 'Factuur is nog niet betaald') unless settled? + end + def set_booked_status self.status = 'booked' if booking_number.present? && paid? end From ed11b6da361ed066cb460b1f9807f432a50333df Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 11:06:29 +0000 Subject: [PATCH 08/11] Silence rubocop metrics on new invoice actions Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- app/controllers/invoices_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index 1cd701181..998606b6a 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -78,7 +78,7 @@ def send_invoice redirect_to invoices_path end - def mark_as_paid + def mark_as_paid # rubocop:disable Metrics/AbcSize, Metrics/MethodLength @invoice = Invoice.find(params.expect(:id)) authorize @invoice @@ -93,7 +93,7 @@ def mark_as_paid redirect_to invoices_path end - def book + def book # rubocop:disable Metrics/AbcSize @invoice = Invoice.find(params.expect(:id)) authorize @invoice From d1b4e33c879f489d13d534026d9d40b0130de46f Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 11:23:48 +0000 Subject: [PATCH 09/11] Allow deleting pending invoices Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- app/controllers/invoices_controller.rb | 9 +++++ app/models/invoice.rb | 9 +++-- app/policies/invoice_policy.rb | 4 +++ app/views/invoices/index.html.erb | 11 ++++++ config/routes.rb | 2 +- .../invoices_controller/destroy_spec.rb | 35 +++++++++++++++++++ spec/models/invoice_spec.rb | 5 +++ 7 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 spec/controllers/invoices_controller/destroy_spec.rb diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index 998606b6a..1d18c662b 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -93,6 +93,15 @@ def mark_as_paid # rubocop:disable Metrics/AbcSize, Metrics/MethodLength redirect_to invoices_path end + def destroy + @invoice = Invoice.find(params.expect(:id)) + authorize @invoice + + @invoice.destroy! + flash[:success] = "Factuur #{@invoice.human_id} verwijderd" + redirect_to invoices_path + end + def book # rubocop:disable Metrics/AbcSize @invoice = Invoice.find(params.expect(:id)) authorize @invoice diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 3e37f14f7..0855c88a9 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -40,6 +40,10 @@ def settled? end # Returns false when the invoice was already paid (also checked under a row lock to avoid double credit) + def deletable? + pending? + end + def mark_as_paid!(created_by) with_lock do next false if settled? @@ -61,8 +65,9 @@ def set_booked_status def set_human_id year_prefix = Time.zone.now.year.to_s - this_year_invoices = Invoice.where('human_id LIKE ?', "#{year_prefix}%") - invoice_number = this_year_invoices.count + 1 + # Based on the highest existing number (not the count), so deleting an invoice never causes duplicate numbers + last_human_id = Invoice.where('human_id LIKE ?', "#{year_prefix}%").maximum(:human_id) + invoice_number = last_human_id.to_s.delete_prefix(year_prefix).to_i + 1 self.human_id = "#{year_prefix}#{invoice_number.to_s.rjust(4, '0')}" end diff --git a/app/policies/invoice_policy.rb b/app/policies/invoice_policy.rb index 4549c64ba..a52bd0b00 100644 --- a/app/policies/invoice_policy.rb +++ b/app/policies/invoice_policy.rb @@ -15,6 +15,10 @@ def book? user&.treasurer? end + def destroy? + user&.treasurer? && record.deletable? + end + def download? user&.treasurer? end diff --git a/app/views/invoices/index.html.erb b/app/views/invoices/index.html.erb index 7f84d3e76..fc04aba0e 100644 --- a/app/views/invoices/index.html.erb +++ b/app/views/invoices/index.html.erb @@ -39,6 +39,9 @@ Betaald Boeknummer <% end %> + <% if policy(Invoice).send_invoice? %> + Verwijder + <% end %> <% if policy(Invoice).download? %> Download <% end %> @@ -100,6 +103,14 @@ <% end %> <% end %> + <% if policy(Invoice).send_invoice? %> + + <% if policy(invoice).destroy? %> + <%= button_to 'Verwijderen', invoice_path(invoice), method: :delete, class: 'btn btn-danger', + form: { data: { turbo_confirm: "Factuur #{invoice.human_id} verwijderen?" } } %> + <% end %> + + <% end %> <% if policy(Invoice).download? %> <%= link_to 'Downloaden', invoice_path(invoice, format: :pdf), class: 'btn btn-primary', download: "Factuur-#{invoice.human_id}.pdf" %> diff --git a/config/routes.rb b/config/routes.rb index 635805543..cd66e5535 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,7 +50,7 @@ resources :products, only: %i[create update], defaults: { format: :json } resources :credit_mutations, only: %i[index create] - resources :invoices, only: %i[index show create] do + resources :invoices, only: %i[index show create destroy] do member do get :pay post :send_invoice diff --git a/spec/controllers/invoices_controller/destroy_spec.rb b/spec/controllers/invoices_controller/destroy_spec.rb new file mode 100644 index 000000000..92bcc1d28 --- /dev/null +++ b/spec/controllers/invoices_controller/destroy_spec.rb @@ -0,0 +1,35 @@ +require 'rails_helper' + +describe InvoicesController do + describe 'DELETE destroy' do + let!(:invoice) { create(:invoice) } + + it 'deletes a pending invoice' do + sign_in create(:user, :treasurer) + + expect { delete :destroy, params: { id: invoice.id } }.to change(Invoice, :count).by(-1) + expect(response).to redirect_to(invoices_path) + end + + it 'does not delete a sent invoice' do + invoice.update!(status: 'sent') + sign_in create(:user, :treasurer) + + expect { delete :destroy, params: { id: invoice.id } }.not_to change(Invoice, :count) + expect(response).to have_http_status(:forbidden) + end + + it 'does not delete a paid invoice' do + invoice.update!(status: 'paid') + sign_in create(:user, :treasurer) + + expect { delete :destroy, params: { id: invoice.id } }.not_to change(Invoice, :count) + end + + it 'is forbidden for a normal user' do + sign_in create(:user) + + expect { delete :destroy, params: { id: invoice.id } }.not_to change(Invoice, :count) + end + end +end diff --git a/spec/models/invoice_spec.rb b/spec/models/invoice_spec.rb index 141c7ce9f..f52fdc9ec 100644 --- a/spec/models/invoice_spec.rb +++ b/spec/models/invoice_spec.rb @@ -74,5 +74,10 @@ end it { expect(invoice.human_id).to eq "#{Time.zone.now.year}0003" } + + it 'does not reuse a number still in use after an earlier invoice is deleted' do + Invoice.find_by(human_id: "#{Time.zone.now.year}0001").destroy + expect(create(:invoice).human_id).to eq "#{Time.zone.now.year}0004" + end end end From 7026b9b8a8831dcf47c0bec7bbbe2074151aad94 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 11:27:01 +0000 Subject: [PATCH 10/11] Use described_class in invoice spec Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- spec/models/invoice_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/invoice_spec.rb b/spec/models/invoice_spec.rb index f52fdc9ec..0f870fe79 100644 --- a/spec/models/invoice_spec.rb +++ b/spec/models/invoice_spec.rb @@ -76,7 +76,7 @@ it { expect(invoice.human_id).to eq "#{Time.zone.now.year}0003" } it 'does not reuse a number still in use after an earlier invoice is deleted' do - Invoice.find_by(human_id: "#{Time.zone.now.year}0001").destroy + described_class.find_by(human_id: "#{Time.zone.now.year}0001").destroy expect(create(:invoice).human_id).to eq "#{Time.zone.now.year}0004" end end From 6335c5c3c81e0fb4b5d19bedae2550194e20ff93 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 21:52:56 +0000 Subject: [PATCH 11/11] Move invoice actions from the list to the invoice page Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01SiPSKHWrkyQVUHGMJ84sqm --- app/controllers/invoices_controller.rb | 8 ++-- app/views/invoices/index.html.erb | 43 ------------------- app/views/invoices/show.html.erb | 31 +++++++++++++ .../invoices_controller/mark_as_paid_spec.rb | 4 +- .../invoices_controller/send_invoice_spec.rb | 2 +- 5 files changed, 38 insertions(+), 50 deletions(-) diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index 1d18c662b..2864107d5 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -75,7 +75,7 @@ def send_invoice @invoice.update(status: 'sent') flash[:success] = "Factuur verzonden naar #{@invoice.user.name}" - redirect_to invoices_path + redirect_to invoice_path(@invoice) end def mark_as_paid # rubocop:disable Metrics/AbcSize, Metrics/MethodLength @@ -87,10 +87,10 @@ def mark_as_paid # rubocop:disable Metrics/AbcSize, Metrics/MethodLength else flash[:error] = 'Factuur is al betaald' end - redirect_to invoices_path + redirect_to invoice_path(@invoice) rescue ActiveRecord::RecordInvalid => e flash[:error] = e.record.errors.full_messages.join(', ') - redirect_to invoices_path + redirect_to invoice_path(@invoice) end def destroy @@ -112,7 +112,7 @@ def book # rubocop:disable Metrics/AbcSize else flash[:error] = @invoice.errors.full_messages.join(', ') end - redirect_to invoices_path + redirect_to invoice_path(@invoice) end private diff --git a/app/views/invoices/index.html.erb b/app/views/invoices/index.html.erb index fc04aba0e..adf81fdc2 100644 --- a/app/views/invoices/index.html.erb +++ b/app/views/invoices/index.html.erb @@ -32,16 +32,6 @@ Naam Mailadres Status - <% if policy(Invoice).send_invoice? %> - Verstuur - <% end %> - <% if policy(Invoice).mark_as_paid? %> - Betaald - Boeknummer - <% end %> - <% if policy(Invoice).send_invoice? %> - Verwijder - <% end %> <% if policy(Invoice).download? %> Download <% end %> @@ -78,39 +68,6 @@ <%= t(invoice.status).humanize %> - <% if policy(Invoice).send_invoice? %> - - <% if invoice.pending? %> - <%= simple_form_for invoice, wrapper: :horizontal_form, url: send_invoice_invoice_path(id: invoice.id), method: :post do |f| %> - <%= f.button :submit, 'Factuur versturen', class: 'btn btn-primary' %> - <% end %> - <% end %> - - <% end %> - <% if policy(Invoice).mark_as_paid? %> - - <% unless invoice.settled? %> - <%= button_to 'Markeer als betaald', mark_as_paid_invoice_path(invoice), method: :post, class: 'btn btn-secondary', - form: { data: { turbo_confirm: "Factuur #{invoice.human_id} als betaald markeren?" } } %> - <% end %> - - - <% if invoice.settled? %> - <%= form_with url: book_invoice_path(invoice), scope: :invoice, method: :patch, class: 'd-flex gap-1' do |f| %> - <%= f.text_field :booking_number, value: invoice.booking_number, class: 'form-control form-control-sm', placeholder: 'Boeknummer', aria: { label: 'Boeknummer' } %> - <%= f.submit 'Inboeken', class: 'btn btn-sm btn-primary' %> - <% end %> - <% end %> - - <% end %> - <% if policy(Invoice).send_invoice? %> - - <% if policy(invoice).destroy? %> - <%= button_to 'Verwijderen', invoice_path(invoice), method: :delete, class: 'btn btn-danger', - form: { data: { turbo_confirm: "Factuur #{invoice.human_id} verwijderen?" } } %> - <% end %> - - <% end %> <% if policy(Invoice).download? %> <%= link_to 'Downloaden', invoice_path(invoice, format: :pdf), class: 'btn btn-primary', download: "Factuur-#{invoice.human_id}.pdf" %> diff --git a/app/views/invoices/show.html.erb b/app/views/invoices/show.html.erb index 4e8446084..c46b3b2d4 100644 --- a/app/views/invoices/show.html.erb +++ b/app/views/invoices/show.html.erb @@ -2,6 +2,37 @@ <% show_flux_image = Rails.application.config.x.company_name == "Stichting Sociƫteit Flux" %>