From af8bc4155607918f43c7b3d159034a737b7abf20 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Thu, 22 Apr 2021 11:49:30 +0300 Subject: [PATCH] implement paid cancel --- app/controllers/admin/invoices_controller.rb | 17 +++++++++++++++++ app/views/admin/invoices/show.haml | 4 ++++ config/locales/en.yml | 4 ++++ config/routes.rb | 4 ++++ 4 files changed, 29 insertions(+) diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index c9f1d0c46..43a1b1ee1 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -20,6 +20,23 @@ module Admin end end + def cancel_paid + invoice_id = params[:invoice_id] + invoice = Invoice.find(invoice_id) + + account_activity = AccountActivity.find_by(invoice_id: invoice_id) + account_activity_dup = account_activity.dup + account_activity_dup.sum = -account_activity.sum + + if account_activity_dup.save and invoice.update(cancelled_at: Time.zone.today) + flash[:notice] = t(:payment_was_cancelled) + redirect_to admin_invoices_path + else + flash[:alert] = t(:failed_to_payment_cancel) + redirect_to admin_invoices_path + end + end + def index @q = Invoice.includes(:account_activity).search(params[:q]) @q.sorts = 'number desc' if @q.sorts.empty? diff --git a/app/views/admin/invoices/show.haml b/app/views/admin/invoices/show.haml index d0450469f..b121c8337 100644 --- a/app/views/admin/invoices/show.haml +++ b/app/views/admin/invoices/show.haml @@ -6,6 +6,10 @@ %h1.text-right.text-center-xs - if @invoice.unpaid? = link_to(t(:payment_received), new_admin_bank_statement_path(invoice_id: @invoice.id), class: 'btn btn-default') + + - if @invoice.paid? and !@invoice.cancelled? + = link_to(t(:cancel_payment), cancel_paid_admin_invoices_path(invoice_id: @invoice.id), method: 'post', data: { confirm: t(:are_you_sure) }, class: 'btn btn-warning') + = link_to(t('.download_btn'), download_admin_invoice_path(@invoice), class: 'btn btn-default') = link_to(t('.deliver_btn'), new_admin_invoice_delivery_path(@invoice), class: 'btn btn-default') - if @invoice.cancellable? diff --git a/config/locales/en.yml b/config/locales/en.yml index 97c968996..1d5a469a2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -293,6 +293,9 @@ en: record_deleted: 'Record deleted' failed_to_delete_record: 'Failed to delete record' + payment_was_cancelled: 'Payment was cancelled' + failed_to_payment_cancel: 'Failed to payment cancel' + authentication_error: 'Authentication error' sign_in_cancelled: "Sign in cancelled" @@ -601,6 +604,7 @@ en: no_transfers_found: 'No transfers found' parameter_value_range_error: 'Parameter value range error: %{key}' payment_received: 'Payment received' + cancel_payment: 'Cancel Payment' api_user_not_found: 'API user not found' notes: Notes active_price_for_this_operation_is: 'Active price for this operation is %{price}' diff --git a/config/routes.rb b/config/routes.rb index 4e78b7c0f..9a8f50b41 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -231,6 +231,10 @@ Rails.application.routes.draw do end resources :invoices, except: %i[edit update destroy] do + collection do + # get ':id/cancel_paid', to: 'invoices#cancel_paid', as: 'get_cancel_paid' + post ':id/cancel_paid', to: 'invoices#cancel_paid', as: 'cancel_paid' + end resource :delivery, controller: 'invoices/delivery', only: %i[new create] member do