diff --git a/app/controllers/admin/bank_transactions_controller.rb b/app/controllers/admin/bank_transactions_controller.rb index 36ab089e5..6f2bba0c6 100644 --- a/app/controllers/admin/bank_transactions_controller.rb +++ b/app/controllers/admin/bank_transactions_controller.rb @@ -1,5 +1,29 @@ class Admin::BankTransactionsController < AdminController load_and_authorize_resource - def show; end + def update + if @bank_transaction.update(bank_transaction_params) + flash[:notice] = I18n.t('record_updated') + redirect_to [:admin, @bank_transaction] + else + flash.now[:alert] = I18n.t('failed_to_update_record') + render 'edit' + end + end + + def bind + if @bank_transaction.bind_invoice(params[:invoice_id]) + flash[:notice] = I18n.t('record_created') + redirect_to [:admin, @bank_transaction] + else + flash.now[:alert] = I18n.t('failed_to_create_record') + render 'show' + end + end + + private + + def bank_transaction_params + params.require(:bank_transaction).permit(:description, :sum, :reference_no) + end end diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index c4f1b97ec..fff068914 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -16,7 +16,7 @@ class BankTransaction < ActiveRecord::Base # For successful binding, reference number, invoice id and sum must match with the invoice # rubocop: disable Metrics/PerceivedComplexity # rubocop: disable Metrics/CyclomaticComplexity - def bind_invoice + def autobind_invoice return if binded? registrar = Registrar.find_by(reference_no: reference_no) return unless registrar @@ -33,6 +33,38 @@ class BankTransaction < ActiveRecord::Base return if invoice.binded? return if invoice.sum != sum + create_activity(registrar, invoice) + end + # rubocop: enable Metrics/PerceivedComplexity + # rubocop: enable Metrics/CyclomaticComplexity + + def bind_invoice(invoice_id) + if binded? + errors.add(:base, I18n.t('transaction_is_already_binded')) + return + end + + invoice = Invoice.find_by(id: invoice_id) + + unless invoice + errors.add(:base, I18n.t('invoice_was_not_found')) + return + end + + if invoice.binded? + errors.add(:base, I18n.t('invoice_is_already_binded')) + return + end + + if invoice.sum != sum + errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match')) + return + end + + create_activity(invoice.buyer, invoice) + end + + def create_activity(registrar, invoice) create_account_activity( account: registrar.cash_account, invoice: invoice, @@ -41,6 +73,4 @@ class BankTransaction < ActiveRecord::Base description: description ) end - # rubocop: enable Metrics/PerceivedComplexity - # rubocop: enable Metrics/CyclomaticComplexity end diff --git a/app/views/admin/bank_statements/index.haml b/app/views/admin/bank_statements/index.haml index b8a6223f7..8e2d98dc2 100644 --- a/app/views/admin/bank_statements/index.haml +++ b/app/views/admin/bank_statements/index.haml @@ -1,9 +1,7 @@ -.row - .col-sm-6 - %h2.text-center-xs= t('bank_statements') - .col-sm-6 - %h2.text-right.text-center-xs - = link_to(t('import'), new_admin_bank_statement_path, class: 'btn btn-primary') +- content_for :actions do + = link_to(t('import'), new_admin_bank_statement_path, class: 'btn btn-default') += render 'admin/shared/title', name: "#{t(:bank_statements)}" + %hr .row .col-md-12 diff --git a/app/views/admin/bank_transactions/edit.haml b/app/views/admin/bank_transactions/edit.haml new file mode 100644 index 000000000..c55e8deab --- /dev/null +++ b/app/views/admin/bank_transactions/edit.haml @@ -0,0 +1,74 @@ +- content_for :actions do + = link_to(t('back'), :back, class: 'btn btn-default') += render 'admin/shared/title', name: "#{t('bank_transaction')}" + += form_for([:admin, @bank_transaction], html: { class: 'form-horizontal' }) do |f| + = render 'shared/full_errors', object: @bank_transaction + + .row + .col-md-8 + .form-group + = f.label :status, class: 'col-md-2 control-label' + - c = @bank_transaction.binded? ? 'text-success' : 'text-danger' + .col-md-10.form-control-static{class: c} + = @bank_transaction.binded? ? t('binded') : t('not_binded') + + .form-group + = f.label :description, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:description, class: 'form-control') + + .form-group + = f.label :sum, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:sum, class: 'form-control') + + .form-group + = f.label :reference_no, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:reference_no, class: 'form-control') + + .form-group + = f.label :document_no, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:document_no, class: 'form-control', disabled: :disabled) + + .form-group + = f.label :bank_reference, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:bank_reference, class: 'form-control', disabled: :disabled) + + .form-group + = f.label :iban, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:iban, class: 'form-control', disabled: :disabled) + + .form-group + = f.label :buyer_bank_code, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:buyer_bank_code, class: 'form-control', disabled: :disabled) + + .form-group + = f.label :buyer_iban, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:buyer_iban, class: 'form-control', disabled: :disabled) + + .form-group + = f.label :buyer_name, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:buyer_name, class: 'form-control', disabled: :disabled) + + .form-group + = f.label :currency, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:currency, class: 'form-control', disabled: :disabled) + + .form-group + = f.label :paid_at, class: 'col-md-2 control-label' + .col-md-10 + = f.text_field(:paid_at, class: 'form-control', disabled: :disabled) + + %hr + .row + .col-md-8.text-right + = button_tag(t('save'), class: 'btn btn-primary') diff --git a/app/views/admin/bank_transactions/manual_binding.haml b/app/views/admin/bank_transactions/manual_binding.haml new file mode 100644 index 000000000..b14cbd43c --- /dev/null +++ b/app/views/admin/bank_transactions/manual_binding.haml @@ -0,0 +1,27 @@ +- content_for :actions do + = link_to(t('back_to_bank_transaction'), [:admin, @bank_transaction], class: 'btn btn-default') += render 'admin/shared/title', name: "#{t('manual_binding')}" + += form_for([:admin, @bank_transaction], url: {action: :bind}, html: { class: 'form-horizontal' }) do |f| + = render 'shared/full_errors', object: @bank_transaction + + .row + .col-md-8 + .form-group + = f.label :status, class: 'col-md-2 control-label' + - c = @bank_transaction.binded? ? 'text-success' : 'text-danger' + .col-md-10.form-control-static{class: c} + = @bank_transaction.binded? ? t('binded') : t('not_binded') + + .form-group + = f.label :sum, class: 'col-md-2 control-label' + .col-md-10.form-control-static + = "#{@bank_transaction.sum} #{@bank_transaction.currency}" + .form-group + = label_tag :invoice_id, t('invoice'), class: 'col-md-2 control-label' + .col-md-10 + = text_field_tag(:invoice_id, params[:invoice_id], class: 'form-control') + %hr + .row + .col-md-8.text-right + = button_tag(t('save'), class: 'btn btn-primary') diff --git a/app/views/admin/bank_transactions/show.haml b/app/views/admin/bank_transactions/show.haml index 72ebdd2e1..f923f916b 100644 --- a/app/views/admin/bank_transactions/show.haml +++ b/app/views/admin/bank_transactions/show.haml @@ -1,11 +1,8 @@ -.row - .col-sm-6 - %h2.text-center-xs - = t('bank_transaction') - .col-sm-6 - %h2.text-right.text-center-xs - = link_to(t('back_to_bank_statement'), admin_bank_statement_path(@bank_transaction.bank_statement), class: 'btn btn-primary') -%hr +- content_for :actions do + = link_to(t('edit'), edit_admin_bank_transaction_path(@bank_transaction), class: 'btn btn-primary') + = link_to(t('back_to_bank_statement'), admin_bank_statement_path(@bank_transaction.bank_statement), class: 'btn btn-default') += render 'admin/shared/title', name: "#{t('bank_transaction')}" += render 'shared/full_errors', object: @bank_transaction %row .col-md-12 .panel.panel-default @@ -20,10 +17,6 @@ - c = @bank_transaction.binded? ? 'text-success' : 'text-danger' %dd{class: c}= @bank_transaction.binded? ? t('binded') : t('not_binded') - - if @bank_transaction.binded? - %dt= t('binded_invoice') - %dd= link_to(@bank_transaction.binded_invoice, admin_invoice_path(@bank_transaction.binded_invoice)) - %dt= t('bank_reference') %dd= @bank_transaction.bank_reference @@ -53,3 +46,16 @@ %dt= t('paid_at') %dd= l(@bank_transaction.paid_at, format: :date_long) + + - if @bank_transaction.binded? + %dt= t('binded_invoice') + %dd= link_to(@bank_transaction.binded_invoice, admin_invoice_path(@bank_transaction.binded_invoice)) + + - unless @bank_transaction.binded? + = form_for([:admin, @bank_transaction], url: {action: :bind}, html: { class: 'form-inline' }) do |f| + .form-group + %dl.dl-horizontal + %dt{style: 'padding-top: 5px'}= t('binded_invoice') + %dd + = text_field_tag(:invoice_id, params[:invoice_id], class: 'form-control') + = button_tag(t('bind_manually'), class: 'btn btn-primary') diff --git a/config/locales/en.yml b/config/locales/en.yml index 9255e535b..9578043e0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -713,3 +713,8 @@ en: is a computer that saves and forwards via a general-access data communications network such data that is connected with the domain name and corresponding IP addresses. Your IT helpdesk or Internet service provider will have the necessary information about the domain name servers. account_activity: 'Account activity' receipt_date: 'Receipt date' + manual_binding: 'Manual binding' + transaction_is_already_binded: 'Transaction is already binded' + invoice_was_not_found: 'Invoice was not found' + invoice_is_already_binded: 'Invoice is already binded' + invoice_and_transaction_sums_do_not_match: 'Invoice and transaction sums do not match' diff --git a/config/routes.rb b/config/routes.rb index e9c32b002..25d7b5664 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -91,7 +91,9 @@ Rails.application.routes.draw do get 'download_import_file', on: :member end - resources :bank_transactions + resources :bank_transactions do + patch 'bind', on: :member + end resources :invoices