Skip to content

Commit

Permalink
Invoice, allow to edit unsent and other type invoices
Browse files Browse the repository at this point in the history
This is handy if a mistake was made when creating an invoice instead of
requiring to delete and recreate it.

Close #88
  • Loading branch information
thibaudgg committed Jul 28, 2023
1 parent d9ae4d3 commit f898b13
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
61 changes: 37 additions & 24 deletions app/admin/invoice.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ActiveAdmin.register Invoice do
menu parent: :billing, priority: 1
actions :all, except: %i[edit update]
actions :all

breadcrumb do
if params[:action] == 'new'
Expand Down Expand Up @@ -294,31 +294,33 @@
end
end
tabs do
if Current.acp.feature?('activity')
tab activities_human_name, id: 'activity_participation' do
f.inputs do
if f.object.object.is_a?(ActivityParticipation)
li(class: 'refused_activity_participation') do
parts = []
parts << link_to(
t('active_admin.resource.new.refused_activity_participation', date: f.object.object.activity.date),
activity_participation_path(f.object.object_id))
parts << ' – '
parts << link_to(
t('.erase').downcase,
new_invoice_path(member_id: f.object.member_id))
parts.join.html_safe
unless f.object.persisted?
if Current.acp.feature?('activity')
tab activities_human_name, id: 'activity_participation' do
f.inputs do
if f.object.object.is_a?(ActivityParticipation)
li(class: 'refused_activity_participation') do
parts = []
parts << link_to(
t('active_admin.resource.new.refused_activity_participation', date: f.object.object.activity.date),
activity_participation_path(f.object.object_id))
parts << ' – '
parts << link_to(
t('.erase').downcase,
new_invoice_path(member_id: f.object.member_id))
parts.join.html_safe
end
end
f.input :paid_missing_activity_participations, as: :number, step: 1
f.input :activity_price, as: :number, min: 0, max: 99999.95, step: 0.05, hint: true
end
f.input :paid_missing_activity_participations, as: :number, step: 1
f.input :activity_price, as: :number, min: 0, max: 99999.95, step: 0.05, hint: true
end
end
end
if Current.acp.share?
tab t_invoice_object_type('ACPShare'), id: 'acp_share' do
f.inputs do
f.input :acp_shares_number, as: :number, step: 1
if Current.acp.share?
tab t_invoice_object_type('ACPShare'), id: 'acp_share' do
f.inputs do
f.input :acp_shares_number, as: :number, step: 1
end
end
end
end
Expand All @@ -328,7 +330,7 @@
if Current.acp.vat_number?
f.input :vat_rate, as: :number, min: 0, max: 100, step: 0.01
end
f.has_many :items, new_record: t('.has_many_new_invoice_item') do |ff|
f.has_many :items, new_record: t('.has_many_new_invoice_item'), allow_destroy: true do |ff|
ff.input :description
ff.input :amount, as: :number, step: 0.05, min: 0, max: 99999.95
end
Expand All @@ -348,7 +350,7 @@
:activity_price,
:acp_shares_number,
:vat_rate,
items_attributes: %i[description amount]
items_attributes: %i[id description amount _destroy]

before_build do |invoice|
if params[:activity_participation_id]
Expand Down Expand Up @@ -392,6 +394,17 @@
def apply_sorting(chain)
super(chain).joins(:member).order('members.name', id: :desc)
end

after_action :refresh_invoice, only: :update

private

def refresh_invoice
if resource.valid?
resource.attach_pdf
Billing::PaymentsRedistributor.redistribute!(resource.member_id)
end
end
end

config.sort_order = 'date_desc'
Expand Down
6 changes: 5 additions & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Invoice < ApplicationRecord
has_many :items, class_name: 'InvoiceItem', dependent: :destroy
has_many :payments, dependent: :destroy

accepts_nested_attributes_for :items
accepts_nested_attributes_for :items, allow_destroy: true

has_one_attached :pdf_file

Expand Down Expand Up @@ -267,6 +267,10 @@ def sent?
sent_at?
end

def can_update?
!sent? && other_type?
end

def can_destroy?
!processing? && !sent_at? && payments.none?
end
Expand Down
4 changes: 4 additions & 0 deletions config/locales/active_admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ _:
_de: Allgemeine
_fr: Généraux
_it: Generali
has_many_new_invoice_item:
_de: Einen Artikel hinzufügen
_fr: Ajouter un article
_it: Aggiungere un articolo
invoice_qr:
_de: QR-Rechnung
_fr: Facture QR
Expand Down

0 comments on commit f898b13

Please sign in to comment.