diff --git a/app/controllers/admin/invoices/mark_as_paids_controller.rb b/app/controllers/admin/invoices/mark_as_paids_controller.rb new file mode 100644 index 000000000..176bdd0f7 --- /dev/null +++ b/app/controllers/admin/invoices/mark_as_paids_controller.rb @@ -0,0 +1,40 @@ +module Admin + module Invoices + class MarkAsPaidsController < BaseController + rescue_from Errors::InvoiceAlreadyPaid, with: :invoice_already_paid + + before_action :set_invoice + + # order is important! before set invoice, otherwise @invoice wont be set + include ::Invoices::UpdateAuthorizable + + def edit; end + + def update + raise(Errors::InvoiceAlreadyPaid, @invoice.id) if @invoice.paid? + + @invoice.assign_attributes(invoice_params) + @invoice.mark_as_paid_at(invoice_params[:paid_at]) + @invoice.save! + + flash[:notice] = t('invoices.marked_as_paid') + redirect_to admin_invoice_path(@invoice), status: :see_other + end + + private + + def set_invoice + @invoice = Invoice.find(params[:invoice_id]) + end + + def invoice_already_paid + flash[:alert] = t('invoices.already_paid') + redirect_to admin_invoice_path(@invoice), status: :see_other + end + + def invoice_params + params.require(:invoice).permit(:notes, :paid_at) + end + end + end +end diff --git a/app/controllers/admin/invoices/toggle_partial_payments_controller.rb b/app/controllers/admin/invoices/toggle_partial_payments_controller.rb new file mode 100644 index 000000000..49ab912da --- /dev/null +++ b/app/controllers/admin/invoices/toggle_partial_payments_controller.rb @@ -0,0 +1,24 @@ +module Admin + module Invoices + class TogglePartialPaymentsController < BaseController + before_action :set_invoice + + # order is important! before set invoice, otherwise @invoice wont be set + include ::Invoices::UpdateAuthorizable + + def update + if @invoice.toggle(:partial_payments).save + + action = @invoice.partial_payments? ? 'activated' : 'deactivated' + redirect_to admin_invoice_path(@invoice), notice: t("invoices.partial_payments_#{action}"), status: :see_other + else + redirect_to admin_invoice_path(@invoice), alert: t(:something_went_wrong), status: :see_other + end + end + + def set_invoice + @invoice = Invoice.find(params[:invoice_id]) + end + end + end +end diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index d1e429d7d..4a6188881 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -4,9 +4,8 @@ module Admin class InvoicesController < BaseController before_action :authorize_user - before_action :create_invoice_if_needed, except: :toggle_partial_payments - before_action :set_invoice, only: %i[show download update edit toggle_partial_payments] - before_action :authorize_for_update, only: %i[edit update] + before_action :create_invoice_if_needed + before_action :set_invoice, only: %i[show download] # GET /admin/invoices/aa450f1a-45e2-4f22-b2c3-f5f46b5f906b def show @@ -24,7 +23,6 @@ def index else @pagy, @invoices = pagy(invoices, items: params[:per_page] ||= 15) end - end # GET /admin/invoices/aa450f1a-45e2-4f22-b2c3-f5f46b5f906b/download @@ -35,80 +33,16 @@ def download send_data(raw_pdf, filename: @invoice.filename) end - # GET /admin/invoices/aa450f1a-45e2-4f22-b2c3-f5f46b5f906b/edit - def edit - if @invoice.paid? - respond_to do |format| - format.html do - redirect_to admin_invoice_path(@invoice), notice: t('invoices.already_paid') - end - format.json { render json: @invoice.errors, status: :unprocessable_entity } - end - end - end - - # PUT /admin/invoices/aa450f1a-45e2-4f22-b2c3-f5f46b5f906b - def update - respond_to do |format| - if update_predicate - format.html do - redirect_to admin_invoice_path(@invoice), notice: t('invoices.marked_as_paid') - end - format.json { render :show, status: :ok, location: @invoice } - else - format.html { redirect_to admin_invoice_path(@invoice), notice: t(:something_went_wrong) } - format.json { render json: @invoice.errors, status: :unprocessable_entity } - end - end - rescue Errors::InvoiceAlreadyPaid - respond_to do |format| - format.html { redirect_to admin_invoice_path(@invoice), notice: t('invoices.already_paid') } - format.json { render json: @invoice.errors, status: :unprocessable_entity } - end - end - - # POST /admin/invoices/aa450f1a-45e2-4f22-b2c3-f5f46b5f906b/toggle_partial_payments - def toggle_partial_payments - respond_to do |format| - if @invoice.toggle(:partial_payments).save - format.html do - action = @invoice.partial_payments? ? 'activated' : 'deactivated' - redirect_to admin_invoice_path(@invoice), notice: t("invoices.partial_payments_#{action}") - end - format.json { render :show, status: :ok, location: @invoice } - else - format.html { redirect_to admin_invoice_path(@invoice), notice: t(:something_went_wrong) } - format.json { render json: @invoice.errors, status: :unprocessable_entity } - end - end - end - private def set_invoice @invoice = Invoice.includes(:invoice_items).find(params[:id]) end - def update_params - update_params = params.require(:invoice).permit(:notes) - merge_updated_by(update_params) - end - - def update_predicate - @invoice.assign_attributes(update_params) - raise(Errors::InvoiceAlreadyPaid, @invoice.id) if @invoice.paid? - - @invoice.mark_as_paid_at(Time.zone.now) - end - def authorize_user authorize! :read, Invoice end - def authorize_for_update - authorize! :update, @invoice - end - def create_invoice_if_needed InvoiceCreationJob.perform_later if InvoiceCreationJob.needs_to_run? end diff --git a/app/controllers/concerns/invoices/update_authorizable.rb b/app/controllers/concerns/invoices/update_authorizable.rb new file mode 100644 index 000000000..e0233a7da --- /dev/null +++ b/app/controllers/concerns/invoices/update_authorizable.rb @@ -0,0 +1,20 @@ +module Invoices + module UpdateAuthorizable + extend ActiveSupport::Concern + + included do + before_action :authorize_user + before_action :authorize_for_update + end + + private + + def authorize_user + authorize! :read, Invoice + end + + def authorize_for_update + authorize! :update, @invoice + end + end +end diff --git a/app/views/admin/invoices/mark_as_paids/edit.html.erb b/app/views/admin/invoices/mark_as_paids/edit.html.erb new file mode 100644 index 000000000..4b0922627 --- /dev/null +++ b/app/views/admin/invoices/mark_as_paids/edit.html.erb @@ -0,0 +1,40 @@ + +<%= turbo_frame_tag "modal" do %> + +
| <%= t('invoices.status') %> | +<%= I18n.t("activerecord.enums.invoice.statuses.#{@invoice.status}") %> | +
| <%= t('invoices.issued_for') %> | +<%= @invoice.recipient %> <%= @invoice.address %> |
+
| <%= t(:updated_by) %> | +<%= @invoice.updated_by %> | +
| <%= t('invoices.notes') %> | +<%= @invoice.notes %> | +
| <%= t('invoices.paid_at') %> | +<%= @invoice.paid_at %> | +
| <%= t('invoices.issuer') %> | +<%= Setting.find_by(code: 'invoice_issuer').retrieve %> | +
| <%= t('invoices.issue_date') %> | +<%= @invoice.issue_date %> | +
| <%= t('invoices.due_date') %> | +<%= @invoice.due_date %> | +
| <%= t('invoices.paid_through') %> | +<%= @invoice.paid_with_payment_order&.channel %> | +