diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index ccfdcb3f3..2864107d5 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] @@ -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 @@ -75,9 +75,46 @@ def send_invoice @invoice.update(status: 'sent') flash[:success] = "Factuur verzonden naar #{@invoice.user.name}" + redirect_to invoice_path(@invoice) + end + + def mark_as_paid # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + @invoice = Invoice.find(params.expect(:id)) + authorize @invoice + + 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 invoice_path(@invoice) + rescue ActiveRecord::RecordInvalid => e + flash[:error] = e.record.errors.full_messages.join(', ') + redirect_to invoice_path(@invoice) + 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 + + @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 + redirect_to invoice_path(@invoice) + end + private def integer_id?(id) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 956fa84e4..0855c88a9 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 @@ -12,7 +12,10 @@ 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_save :set_booked_status before_create :set_human_id def name @@ -31,12 +34,40 @@ def row_amount rows.sum(&:total) end + # Paid, either still to be booked or already booked + def settled? + paid? || booked? + 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? + + CreditMutation.create!(user:, amount:, description: "Betaling factuur #{human_id}", created_by:) + update!(status: 'paid') + end + end + 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 + 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 61ba0a17f..a52bd0b00 100644 --- a/app/policies/invoice_policy.rb +++ b/app/policies/invoice_policy.rb @@ -7,6 +7,18 @@ def send_invoice? user&.treasurer? end + def mark_as_paid? + user&.treasurer? + end + + 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 ffa778d96..adf81fdc2 100644 --- a/app/views/invoices/index.html.erb +++ b/app/views/invoices/index.html.erb @@ -32,9 +32,6 @@