From a26ce110b2a4918a21f52ee52968de5e3a818f7e Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Mon, 27 Apr 2015 11:15:09 +0300 Subject: [PATCH] Invoice cancelling --- app/assets/stylesheets/shared/general.sass | 3 +++ .../admin/bank_transactions_controller.rb | 2 +- app/controllers/registrar/invoices_controller.rb | 10 ++++++++++ app/models/bank_transaction.rb | 16 +++++++++++----- app/models/invoice.rb | 13 +++++++++++++ app/views/admin/bank_transactions/edit.haml | 2 +- app/views/admin/bank_transactions/show.haml | 2 +- app/views/admin/invoices/index.haml | 8 +++++++- app/views/registrar/invoices/index.haml | 8 +++++++- .../registrar/invoices/partials/_details.haml | 11 ++++++++++- app/views/registrar/invoices/pdf.haml | 11 ++++++++++- app/views/registrar/invoices/show.haml | 13 +++++++++---- config/locales/en.yml | 5 +++++ config/routes.rb | 7 +++++-- ...20150427073517_add_cancelled_at_to_invoice.rb | 5 +++++ db/schema.rb | 7 ++----- spec/models/bank_transaction_spec.rb | 15 +++++++++++++-- 17 files changed, 113 insertions(+), 25 deletions(-) create mode 100644 db/migrate/20150427073517_add_cancelled_at_to_invoice.rb diff --git a/app/assets/stylesheets/shared/general.sass b/app/assets/stylesheets/shared/general.sass index be509f416..1bc204b09 100644 --- a/app/assets/stylesheets/shared/general.sass +++ b/app/assets/stylesheets/shared/general.sass @@ -51,3 +51,6 @@ @media (min-width: 768px) and (max-width: 991px) .control-label text-align: left !important + +.text-grey + color: grey diff --git a/app/controllers/admin/bank_transactions_controller.rb b/app/controllers/admin/bank_transactions_controller.rb index 6f2bba0c6..ca1da48a8 100644 --- a/app/controllers/admin/bank_transactions_controller.rb +++ b/app/controllers/admin/bank_transactions_controller.rb @@ -12,7 +12,7 @@ class Admin::BankTransactionsController < AdminController end def bind - if @bank_transaction.bind_invoice(params[:invoice_id]) + if @bank_transaction.bind_invoice(params[:invoice_no]) flash[:notice] = I18n.t('record_created') redirect_to [:admin, @bank_transaction] else diff --git a/app/controllers/registrar/invoices_controller.rb b/app/controllers/registrar/invoices_controller.rb index f985e4670..6c606a0bc 100644 --- a/app/controllers/registrar/invoices_controller.rb +++ b/app/controllers/registrar/invoices_controller.rb @@ -28,6 +28,16 @@ class Registrar::InvoicesController < RegistrarController end end + def cancel + if @invoice.cancel + flash[:notice] = t('record_updated') + redirect_to([:registrar, @invoice]) + else + flash.now[:alert] = t('failed_to_update_record') + render :show + end + end + def download_pdf # render 'pdf', layout: false diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index 774ef9e5d..339d9ab9b 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -25,13 +25,14 @@ class BankTransaction < ActiveRecord::Base match = description.match(/^[^\d]*(\d+)/) return unless match - invoice_id = match[1].to_i - return unless invoice_id + invoice_no = match[1].to_i + return unless invoice_no - invoice = registrar.invoices.find_by(id: invoice_id) + invoice = registrar.invoices.find_by(number: invoice_no) return unless invoice return if invoice.binded? + return if invoice.cancelled? return if invoice.sum != sum create_activity(registrar, invoice) @@ -39,13 +40,13 @@ class BankTransaction < ActiveRecord::Base # rubocop: enable Metrics/PerceivedComplexity # rubocop: enable Metrics/CyclomaticComplexity - def bind_invoice(invoice_id) + def bind_invoice(invoice_no) if binded? errors.add(:base, I18n.t('transaction_is_already_binded')) return end - invoice = Invoice.find_by(id: invoice_id) + invoice = Invoice.find_by(number: invoice_no) unless invoice errors.add(:base, I18n.t('invoice_was_not_found')) @@ -57,6 +58,11 @@ class BankTransaction < ActiveRecord::Base return end + if invoice.cancelled? + errors.add(:base, I18n.t('cannot_bind_cancelled_invoice')) + return + end + if invoice.sum != sum errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match')) return diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 405fa9873..16f42f4d0 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -67,6 +67,19 @@ class Invoice < ActiveRecord::Base "invoice-#{number}.pdf" end + def cancel + if binded? + errors.add(:base, I18n.t('cannot_cancel_paid_invoice')) + return false + end + self.cancelled_at = Time.zone.now + save + end + + def cancelled? + cancelled_at.present? + end + def forward(html) return false unless valid? return false unless billing_email.present? diff --git a/app/views/admin/bank_transactions/edit.haml b/app/views/admin/bank_transactions/edit.haml index a644dcfac..5985f3e3a 100644 --- a/app/views/admin/bank_transactions/edit.haml +++ b/app/views/admin/bank_transactions/edit.haml @@ -1,5 +1,5 @@ - content_for :actions do - = link_to(t('back'), :back, class: 'btn btn-default') + = link_to(t('back'), admin_bank_transaction_path(@bank_transaction), class: 'btn btn-default') = render 'shared/title', name: t(:bank_transaction) = form_for([:admin, @bank_transaction], html: { class: 'form-horizontal' }) do |f| diff --git a/app/views/admin/bank_transactions/show.haml b/app/views/admin/bank_transactions/show.haml index 97b2ba496..4a325ab53 100644 --- a/app/views/admin/bank_transactions/show.haml +++ b/app/views/admin/bank_transactions/show.haml @@ -57,5 +57,5 @@ %dl.dl-horizontal %dt{style: 'padding-top: 5px'}= t('binded_invoice') %dd - = text_field_tag(:invoice_id, params[:invoice_id], class: 'form-control') + = text_field_tag(:invoice_no, params[:invoice_no], class: 'form-control') = button_tag(t('bind_manually'), class: 'btn btn-primary') diff --git a/app/views/admin/invoices/index.haml b/app/views/admin/invoices/index.haml index e1c571d59..207f74c1d 100644 --- a/app/views/admin/invoices/index.haml +++ b/app/views/admin/invoices/index.haml @@ -21,9 +21,15 @@ %tr %td= link_to(x, [:admin, x]) %td= link_to(x.buyer_name, admin_registrar_path(x.buyer_id)) - %td= l(x.due_date) + - if x.cancelled? + %td.text-grey= t('cancelled') + - else + %td= l(x.due_date) + - if x.binded? %td= l(x.receipt_date) + - elsif x.cancelled? + %td.text-grey= t('cancelled') - else %td.text-danger= t('unpaid') .row diff --git a/app/views/registrar/invoices/index.haml b/app/views/registrar/invoices/index.haml index 91673e13c..a3a128526 100644 --- a/app/views/registrar/invoices/index.haml +++ b/app/views/registrar/invoices/index.haml @@ -25,9 +25,15 @@ %td= link_to(x, [:registrar, x]) - if x.receipt_date %td= l(x.receipt_date) + - elsif x.cancelled? + %td.text-grey= t('cancelled') - else %td{class: 'text-danger'}= t(:unpaid) - %td= l(x.due_date) + + - if x.cancelled? + %td.text-grey= t('cancelled') + - else + %td= l(x.due_date) %td= x.sum .row .col-md-12 diff --git a/app/views/registrar/invoices/partials/_details.haml b/app/views/registrar/invoices/partials/_details.haml index bbe51c9c9..0e5528514 100644 --- a/app/views/registrar/invoices/partials/_details.haml +++ b/app/views/registrar/invoices/partials/_details.haml @@ -4,12 +4,21 @@ %dt= t('issue_date') %dd= l(@invoice.created_at) + - if @invoice.cancelled? + %dt= t('cancel_date') + %dd= l(@invoice.cancelled_at) + %dt= t('due_date') - %dd= l(@invoice.due_date) + - if @invoice.cancelled? + %dd.text-grey= t('cancelled') + - else + %dd= l(@invoice.due_date) %dt= t('receipt_date') - if @invoice.binded? %dd= l(@invoice.receipt_date) + - elsif @invoice.cancelled? + %dd.text-grey= t('cancelled') - else %dd{class: 'text-danger'}= t('unpaid') diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml index 0f812b6a2..95cb3c1a0 100644 --- a/app/views/registrar/invoices/pdf.haml +++ b/app/views/registrar/invoices/pdf.haml @@ -151,12 +151,21 @@ %dt= t('issue_date') %dd= l(@invoice.created_at) + - if @invoice.cancelled? + %dt= t('cancel_date') + %dd= l(@invoice.cancelled_at) + %dt= t('due_date') - %dd= l(@invoice.due_date) + - if @invoice.cancelled? + %dd= t('cancelled') + - else + %dd= l(@invoice.due_date) %dt= t('receipt_date') - if @invoice.binded? %dd= l(@invoice.receipt_date) + - elsif @invoice.cancelled? + %dd= t('cancelled') - else %dd{class: 'text-danger'}= t('unpaid') diff --git a/app/views/registrar/invoices/show.haml b/app/views/registrar/invoices/show.haml index 4b1943a45..dc4145bf2 100644 --- a/app/views/registrar/invoices/show.haml +++ b/app/views/registrar/invoices/show.haml @@ -1,8 +1,11 @@ - content_for :actions do = link_to(t(:download), download_pdf_registrar_invoice_path(@invoice), class: 'btn btn-default') - = link_to(t(:forward_invoice), forward_registrar_invoice_path(@invoice), class: 'btn btn-default') - = link_to(t(:back), :back, class: 'btn btn-default') + = link_to(t(:forward), forward_registrar_invoice_path(@invoice), class: 'btn btn-default') + - if !@invoice.cancelled? && !@invoice.binded? + = link_to(t(:cancel), cancel_registrar_invoice_path(@invoice), method: :patch, class: 'btn btn-default') + = link_to(t(:back), registrar_invoices_path, class: 'btn btn-default') = render 'shared/title', name: @invoice.to_s += render 'shared/full_errors', object: @invoice .row .col-md-6= render 'registrar/invoices/partials/details' @@ -11,5 +14,7 @@ .col-md-6= render 'registrar/invoices/partials/buyer' .row .col-md-12= render 'registrar/invoices/partials/items' -.row.semifooter - .col-md-12.text-right= render 'registrar/invoices/partials/banklinks' + +- if !@invoice.cancelled? && !@invoice.binded? + .row.semifooter + .col-md-12.text-right= render 'registrar/invoices/partials/banklinks' diff --git a/config/locales/en.yml b/config/locales/en.yml index c3d409aef..711ac4c1f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -734,3 +734,8 @@ en: expiry: 'Expiry' failed_to_create_crt_csr_already_signed: 'Failed to create certificate: CSR is already signed' certificates: 'Certificates' + cancel: 'Cancel' + cancelled: 'Cancelled' + cancel_date: 'Cancel date' + cannot_cancel_paid_invoice: 'Cannot cancel paid invoice' + cannot_bind_cancelled_invoice: 'Cannot bind cancelled invoice' diff --git a/config/routes.rb b/config/routes.rb index 276e07198..d332402a2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,8 +21,11 @@ Rails.application.routes.draw do root 'polls#show' resources :invoices do - get 'download_pdf', on: :member - match 'forward', on: :member, via: [:post, :get] + member do + get 'download_pdf' + match 'forward', via: [:post, :get] + patch 'cancel' + end end resources :deposits diff --git a/db/migrate/20150427073517_add_cancelled_at_to_invoice.rb b/db/migrate/20150427073517_add_cancelled_at_to_invoice.rb new file mode 100644 index 000000000..9727c20d3 --- /dev/null +++ b/db/migrate/20150427073517_add_cancelled_at_to_invoice.rb @@ -0,0 +1,5 @@ +class AddCancelledAtToInvoice < ActiveRecord::Migration + def change + add_column :invoices, :cancelled_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 7cbf92109..04d476149 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150423083308) do +ActiveRecord::Schema.define(version: 20150427073517) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -297,12 +297,8 @@ ActiveRecord::Schema.define(version: 20150423083308) do t.integer "legacy_id" t.integer "legacy_registrar_id" t.integer "legacy_registrant_id" - t.datetime "outzone_at" - t.datetime "delete_at" end - add_index "domains", ["delete_at"], name: "index_domains_on_delete_at", using: :btree - add_index "domains", ["outzone_at"], name: "index_domains_on_outzone_at", using: :btree add_index "domains", ["registrant_id"], name: "index_domains_on_registrant_id", using: :btree add_index "domains", ["registrar_id"], name: "index_domains_on_registrar_id", using: :btree @@ -371,6 +367,7 @@ ActiveRecord::Schema.define(version: 20150423083308) do t.string "creator_str" t.string "updator_str" t.integer "number" + t.datetime "cancelled_at" end add_index "invoices", ["buyer_id"], name: "index_invoices_on_buyer_id", using: :btree diff --git a/spec/models/bank_transaction_spec.rb b/spec/models/bank_transaction_spec.rb index ef963aba2..f7e8a3c65 100644 --- a/spec/models/bank_transaction_spec.rb +++ b/spec/models/bank_transaction_spec.rb @@ -42,7 +42,7 @@ describe BankTransaction do invoice = r.issue_prepayment_invoice(200, 'add some money') bt = Fabricate(:bank_transaction, { sum: 240 }) - bt.bind_invoice(invoice.id) + bt.bind_invoice(invoice.number) invoice.receipt_date.should_not be_blank r.cash_account.balance.should == 240.0 @@ -53,11 +53,22 @@ describe BankTransaction do invoice = r.issue_prepayment_invoice(200, 'add some money') bt = Fabricate(:bank_transaction, { sum: 10 }) - bt.bind_invoice(invoice.id) + bt.bind_invoice(invoice.number) bt.errors.full_messages.should match_array(["Invoice and transaction sums do not match"]) end + it 'should not bind transaction with cancelled invoice' do + r = Fabricate(:registrar_with_no_account_activities, reference_no: 'RF7086666663') + invoice = r.issue_prepayment_invoice(200, 'add some money') + invoice.cancel + + bt = Fabricate(:bank_transaction, { sum: 240 }) + bt.bind_invoice(invoice.number) + + bt.errors.full_messages.should match_array(["Cannot bind cancelled invoice"]) + end + it 'should have one version' do with_versioning do @bank_transaction.versions.should == []