diff --git a/app/controllers/admin/bank_statements_controller.rb b/app/controllers/admin/bank_statements_controller.rb index b667d0fb6..1e69aa1af 100644 --- a/app/controllers/admin/bank_statements_controller.rb +++ b/app/controllers/admin/bank_statements_controller.rb @@ -22,6 +22,22 @@ class Admin::BankStatementsController < AdminController def create @bank_statement = BankStatement.new(bank_statement_params) + if @bank_statement.save + flash[:notice] = I18n.t('record_created') + redirect_to [:admin, @bank_statement] + else + flash.now[:alert] = I18n.t('failed_to_create_record') + render 'new' + end + end + + def import + @bank_statement = BankStatement.new + end + + def create_from_import + @bank_statement = BankStatement.new(bank_statement_params) + if @bank_statement.import flash[:notice] = I18n.t('record_created') redirect_to [:admin, @bank_statement] @@ -53,6 +69,6 @@ class Admin::BankStatementsController < AdminController end def bank_statement_params - params.require(:bank_statement).permit(:th6_file) + params.require(:bank_statement).permit(:th6_file, :bank_code, :iban) end end diff --git a/app/controllers/admin/bank_transactions_controller.rb b/app/controllers/admin/bank_transactions_controller.rb index ca1da48a8..1aaf452a8 100644 --- a/app/controllers/admin/bank_transactions_controller.rb +++ b/app/controllers/admin/bank_transactions_controller.rb @@ -1,6 +1,25 @@ class Admin::BankTransactionsController < AdminController load_and_authorize_resource + def new + @bank_statement = BankStatement.find(params[:bank_statement_id]) + @bank_transaction = BankTransaction.new(currency: 'EUR') + end + + def create + @bank_transaction = BankTransaction.new( + bank_transaction_params.merge(bank_statement_id: params[:bank_statement_id]) + ) + + if @bank_transaction.save + flash[:notice] = I18n.t('record_created') + redirect_to [:admin, @bank_transaction] + else + flash.now[:alert] = I18n.t('failed_to_create_record') + render 'new' + end + end + def update if @bank_transaction.update(bank_transaction_params) flash[:notice] = I18n.t('record_updated') @@ -24,6 +43,10 @@ class Admin::BankTransactionsController < AdminController private def bank_transaction_params - params.require(:bank_transaction).permit(:description, :sum, :reference_no) + params.require(:bank_transaction).permit( + :description, :sum, :reference_no, :document_no, + :bank_reference, :iban, :buyer_bank_code, :buyer_iban, + :buyer_name, :currency, :paid_at + ) end end diff --git a/app/controllers/admin/invoices_controller.rb b/app/controllers/admin/invoices_controller.rb index fdbe80070..ce368742c 100644 --- a/app/controllers/admin/invoices_controller.rb +++ b/app/controllers/admin/invoices_controller.rb @@ -1,6 +1,24 @@ class Admin::InvoicesController < AdminController load_and_authorize_resource + def new + @deposit = Deposit.new + end + + def create + r = Registrar.find_by(id: deposit_params[:registrar_id]) + @deposit = Deposit.new(deposit_params.merge(registrar: r)) + @invoice = @deposit.issue_prepayment_invoice + + if @invoice.persisted? + flash[:notice] = t(:record_created) + redirect_to [:admin, @invoice] + else + flash[:alert] = t(:failed_to_create_record) + render 'new' + end + end + def index @q = Invoice.includes(:account_activity).search(params[:q]) @q.sorts = 'id desc' if @q.sorts.empty? @@ -20,4 +38,10 @@ class Admin::InvoicesController < AdminController render :show end end + + private + + def deposit_params + params.require(:deposit).permit(:amount, :description, :registrar_id) + end end diff --git a/app/models/bank_statement.rb b/app/models/bank_statement.rb index f1f6b8714..7267b4773 100644 --- a/app/models/bank_statement.rb +++ b/app/models/bank_statement.rb @@ -4,7 +4,7 @@ class BankStatement < ActiveRecord::Base attr_accessor :th6_file - validates :bank_code, :iban, :queried_at, presence: true + validates :bank_code, :iban, presence: true FULLY_BINDED = 'fully_binded' PARTIALLY_BINDED = 'partially_binded' diff --git a/app/models/deposit.rb b/app/models/deposit.rb index 18cb8e05d..fae4336cf 100644 --- a/app/models/deposit.rb +++ b/app/models/deposit.rb @@ -4,7 +4,7 @@ class Deposit extend ActiveModel::Naming include DisableHtml5Validation - attr_accessor :amount, :description, :registrar + attr_accessor :amount, :description, :registrar, :registrar_id validates :amount, :registrar, presence: true diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 7c05e6cf0..271e37f2f 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -15,7 +15,7 @@ class Invoice < ActiveRecord::Base validates :invoice_type, :due_date, :currency, :seller_name, :seller_iban, :buyer_name, :invoice_items, :vat_prc, presence: true - before_save :set_invoice_number + before_create :set_invoice_number def set_invoice_number last_no = Invoice.order(number: :desc).where('number IS NOT NULL').limit(1).pluck(:number).first diff --git a/app/views/admin/bank_statements/_form.haml b/app/views/admin/bank_statements/_form.haml new file mode 100644 index 000000000..158d69e89 --- /dev/null +++ b/app/views/admin/bank_statements/_form.haml @@ -0,0 +1,19 @@ += form_for([:admin, @bank_statement], html: { class: 'form-horizontal' }) do |f| + = render 'shared/full_errors', object: @bank_statement + + .row + .col-md-8 + .form-group + .col-md-4.control-label + = f.label :bank_code + .col-md-8 + = f.text_field(:bank_code, class: 'form-control') + .form-group + .col-md-4.control-label + = f.label :iban + .col-md-8 + = f.text_field(:iban, class: 'form-control') + %hr + .row + .col-md-8.text-right + = button_tag(t(:save), class: 'btn btn-warning') diff --git a/app/views/admin/bank_statements/import.haml b/app/views/admin/bank_statements/import.haml new file mode 100644 index 000000000..e432abdf1 --- /dev/null +++ b/app/views/admin/bank_statements/import.haml @@ -0,0 +1,20 @@ +- content_for :actions do + = link_to(t(:back_to_bank_statements), admin_bank_statements_path, class: 'btn btn-default') += render 'shared/title', name: t(:import_th6_bank_statement) + += form_for(@bank_statement, url: { action: :create_from_import }, multipart: true) do |f| + = render 'shared/full_errors', object: @bank_statement + + .row + .col-md-8 + .form-group + .col-md-4.control-label + = f.label :th6_file + .col-md-8 + = f.file_field :th6_file + .col-md-4 + %p= t(:bank_statement_desc).html_safe + %hr + .row + .col-md-8.text-right + = button_tag(t(:save), class: 'btn btn-primary') diff --git a/app/views/admin/bank_statements/index.haml b/app/views/admin/bank_statements/index.haml index afb3a7be1..0810c15fe 100644 --- a/app/views/admin/bank_statements/index.haml +++ b/app/views/admin/bank_statements/index.haml @@ -1,5 +1,6 @@ - content_for :actions do - = link_to(t(:import), new_admin_bank_statement_path, class: 'btn btn-primary') + = link_to(t(:add), new_admin_bank_statement_path, class: 'btn btn-primary') + = link_to(t(:import), import_admin_bank_statements_path, class: 'btn btn-primary') = render 'shared/title', name: t(:bank_statements) .row diff --git a/app/views/admin/bank_statements/new.haml b/app/views/admin/bank_statements/new.haml index 452ffd264..4e5ff09d3 100644 --- a/app/views/admin/bank_statements/new.haml +++ b/app/views/admin/bank_statements/new.haml @@ -1,20 +1,6 @@ - content_for :actions do - = link_to(t(:back_to_bank_statements), admin_bank_statements_path, class: 'btn btn-default') -= render 'shared/title', name: t(:import_th6_bank_statement) + = link_to(t(:back), admin_bank_statements_path, class: 'btn btn-default') -= form_for([:admin, @bank_statement], multipart: true) do |f| - = render 'shared/full_errors', object: @bank_statement += render 'shared/title', name: t(:create_bank_statement) - .row - .col-md-8 - .form-group - .col-md-4.control-label - = f.label :th6_file - .col-md-8 - = f.file_field :th6_file - .col-md-4 - %p= t(:bank_statement_desc).html_safe - %hr - .row - .col-md-8.text-right - = button_tag(t(:save), class: 'btn btn-primary') += render 'form' diff --git a/app/views/admin/bank_statements/show.haml b/app/views/admin/bank_statements/show.haml index 6eb808515..db8d64004 100644 --- a/app/views/admin/bank_statements/show.haml +++ b/app/views/admin/bank_statements/show.haml @@ -1,5 +1,5 @@ - content_for :actions do - = link_to(t(:bind_invoices), bind_invoices_admin_bank_statement_path, + = link_to(t(:bind_invoices), bind_invoices_admin_bank_statement_path, class: 'btn btn-primary', method: :post) = link_to(t(:back_to_bank_statements), admin_bank_statements_path, class: 'btn btn-default') = render 'shared/title', name: t(:bank_statement) @@ -23,17 +23,24 @@ - sc = 'text-danger' if @bank_statement.not_binded? %dd{class: sc}= t(@bank_statement.status) - %dt= t(:queried_at) - %dd= l(@bank_statement.queried_at) + - if @bank_statement.queried_at + %dt= t(:queried_at) + %dd= l(@bank_statement.queried_at) - %dt= t(:imported_at) + %dt= t(:created_at) %dd= l(@bank_statement.created_at) - if @bank_statement.import_file_path %dt= t(:import_file) %dd= link_to(t(:download), download_import_file_admin_bank_statement_path(@bank_statement)) -%h2.text-center-xs= t(:bank_transactions) +.row + .col-sm-6 + %h3.text-center-xs + = t(:bank_transactions) + .col-sm-6.text-right + %h3.text-right.text-center-xs + = link_to(t(:add), new_admin_bank_statement_bank_transaction_path(@bank_statement), class: 'btn btn-primary') %hr .row .col-md-12 diff --git a/app/views/admin/bank_transactions/_form.haml b/app/views/admin/bank_transactions/_form.haml new file mode 100644 index 000000000..244284fda --- /dev/null +++ b/app/views/admin/bank_transactions/_form.haml @@ -0,0 +1,71 @@ += form_for([:admin, @bank_statement, @bank_transaction], html: { class: 'form-horizontal' }) do |f| + = render 'shared/full_errors', object: @bank_transaction + + .row + .col-md-8 + - if @bank_transaction.persisted? + .form-group + = f.label :status, class: 'col-md-4 control-label' + - c = @bank_transaction.binded? ? 'text-success' : 'text-danger' + .col-md-8.form-control-static{class: c} + = @bank_transaction.binded? ? t(:binded) : t(:not_binded) + + .form-group + = f.label :description, class: 'col-md-4 control-label required' + .col-md-8 + = f.text_field(:description, class: 'form-control', required: true) + + .form-group + = f.label :sum, class: 'col-md-4 control-label required' + .col-md-8 + = f.text_field(:sum, class: 'form-control', required: true) + + .form-group + = f.label :reference_no, class: 'col-md-4 control-label required' + .col-md-8 + = f.text_field(:reference_no, class: 'form-control', required: true) + + .form-group + = f.label :document_no, class: 'col-md-4 control-label' + .col-md-8 + = f.text_field(:document_no, class: 'form-control') + + .form-group + = f.label :bank_reference, class: 'col-md-4 control-label' + .col-md-8 + = f.text_field(:bank_reference, class: 'form-control') + + .form-group + = f.label :iban, class: 'col-md-4 control-label' + .col-md-8 + = f.text_field(:iban, class: 'form-control') + + .form-group + = f.label :buyer_bank_code, class: 'col-md-4 control-label' + .col-md-8 + = f.text_field(:buyer_bank_code, class: 'form-control') + + .form-group + = f.label :buyer_iban, class: 'col-md-4 control-label' + .col-md-8 + = f.text_field(:buyer_iban, class: 'form-control') + + .form-group + = f.label :buyer_name, class: 'col-md-4 control-label' + .col-md-8 + = f.text_field(:buyer_name, class: 'form-control') + + .form-group + = f.label :currency, class: 'col-md-4 control-label' + .col-md-8 + = f.text_field(:currency, class: 'form-control', readonly: true) + + .form-group + = f.label :paid_at, class: 'col-md-4 control-label required' + .col-md-8 + = f.text_field(:paid_at, class: 'form-control datepicker', required: true) + + %hr + .row + .col-md-8.text-right + = button_tag(t(:save), class: 'btn btn-warning') diff --git a/app/views/admin/bank_transactions/edit.haml b/app/views/admin/bank_transactions/edit.haml index 5c38ea6e0..8bb5a2666 100644 --- a/app/views/admin/bank_transactions/edit.haml +++ b/app/views/admin/bank_transactions/edit.haml @@ -2,73 +2,4 @@ = 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| - = 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') += render 'form' diff --git a/app/views/admin/bank_transactions/new.haml b/app/views/admin/bank_transactions/new.haml new file mode 100644 index 000000000..403eb415e --- /dev/null +++ b/app/views/admin/bank_transactions/new.haml @@ -0,0 +1,6 @@ +- content_for :actions do + = link_to(t(:back), admin_bank_statement_path(@bank_statement), class: 'btn btn-default') + += render 'shared/title', name: t(:create_bank_transaction) + += render 'form' diff --git a/app/views/admin/invoices/index.haml b/app/views/admin/invoices/index.haml index 45efe74de..837300133 100644 --- a/app/views/admin/invoices/index.haml +++ b/app/views/admin/invoices/index.haml @@ -1,7 +1,7 @@ -.row - .col-sm-12 - %h2.text-center-xs= t(:invoices) -%hr +- content_for :actions do + = link_to(t(:add), new_admin_invoice_path, class: 'btn btn-primary') += render 'shared/title', name: t(:invoices) + .row .col-md-12 .table-responsive diff --git a/app/views/admin/invoices/new.haml b/app/views/admin/invoices/new.haml new file mode 100644 index 000000000..3b10341af --- /dev/null +++ b/app/views/admin/invoices/new.haml @@ -0,0 +1,33 @@ +- content_for :actions do + = link_to(t(:back), admin_invoices_path, class: 'btn btn-default') += render 'shared/title', name: t(:create_new_invoice) + += form_for([:admin, @deposit], url: admin_invoices_path, method: :post, html: { class: 'form-horizontal' }) do |f| + = render 'shared/full_errors', object: @deposit + + .row + .col-md-8 + .form-group + .col-md-4.control-label + = f.label :registrar_id, class: 'required' + .col-md-8 + = f.select :registrar_id, Registrar.all.map { |r| [r.name, r.id] }, { include_blank: true }, class: 'form-control selectize', required: true + + .form-group + .col-md-4.control-label + = f.label :amount, class: 'required' + .col-md-8 + .input-group + = f.text_field :amount, class: 'form-control', required: true + .input-group-addon + EUR + + .form-group + .col-md-4.control-label + = f.label :description + .col-md-8 + = f.text_area :description, class: 'form-control' + %hr + .row + .col-md-8.text-right + = button_tag(t(:save), class: 'btn btn-warning') diff --git a/app/views/admin/invoices/show.haml b/app/views/admin/invoices/show.haml index 9bab21f1d..c96cbb583 100644 --- a/app/views/admin/invoices/show.haml +++ b/app/views/admin/invoices/show.haml @@ -5,7 +5,7 @@ .col-sm-6 %h1.text-right.text-center-xs - if !@invoice.cancelled? && !@invoice.binded? - = link_to(t(:cancel), cancel_admin_invoice_path(@invoice), method: :patch, class: 'btn btn-default') + = link_to(t(:cancel), cancel_admin_invoice_path(@invoice), method: :patch, class: 'btn btn-warning') = link_to(t(:back), admin_invoices_path, class: 'btn btn-default') %hr = render 'shared/full_errors', object: @invoice diff --git a/config/locales/en.yml b/config/locales/en.yml index 2275b13b1..6864c37c8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -831,3 +831,6 @@ en: domain_expiring: 'Domain expiring' domain_validation_rules: 'Domain validation rules' bank_statement_desc: 'Import file row will match only when matching following attributes:
ref number
payment amount
invoice number (the very first number in comment field)
.' + create_bank_statement: 'Create bank statement' + create_bank_transaction: 'Create bank transaction' + create_new_invoice: 'Create new invoice' diff --git a/config/routes.rb b/config/routes.rb index 5986f213f..632e55e54 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -161,6 +161,12 @@ Rails.application.routes.draw do resources :pricelists resources :bank_statements do + resources :bank_transactions + collection do + get 'import' + post 'create_from_import' + end + post 'bind_invoices', on: :member get 'download_import_file', on: :member end diff --git a/spec/features/admin/bank_statement_spec.rb b/spec/features/admin/bank_statement_spec.rb new file mode 100644 index 000000000..bca3136fb --- /dev/null +++ b/spec/features/admin/bank_statement_spec.rb @@ -0,0 +1,52 @@ +require 'rails_helper' + +feature 'BankStatement', type: :feature do + before :all do + @user = Fabricate(:admin_user) + end + + before do + sign_in @user + end + + it 'should add a bank statement and transactions manually' do + visit admin_bank_statements_url + + click_link 'Add' + fill_in 'Bank code', with: '767' + fill_in 'Iban', with: 'EE557700771000598731' + click_button 'Save' + + page.should have_content('Record created') + page.should have_content('Bank statement ') + page.should have_content('767') + page.should have_content('EE557700771000598731') + page.should have_content('Not binded') + + click_link 'Add' + fill_in 'Description', with: 'Payment 12345' + fill_in 'Sum', with: '120' + fill_in 'Reference no', with: 'RF4663930489' + fill_in 'Document no', with: '123' + fill_in 'Bank reference', with: '767' + fill_in 'Iban', with: 'EE557700771000598731' + fill_in 'Buyer bank code', with: '767' + fill_in 'Buyer iban', with: 'EE557700771000598000' + fill_in 'Buyer name', with: 'Test buyer' + fill_in 'Paid at', with: '2015-01-01' + + click_button 'Save' + + page.should have_content('Record created') + page.should have_content('Bank transaction') + page.should have_content('RF4663930489') + page.should have_content('EE557700771000598000') + page.should have_content('Not binded') + page.should have_content('Bind manually') + + click_link 'Back to bank statement' + + page.should have_content('120.0') + page.should have_content('Test buyer') + end +end diff --git a/spec/features/admin/invoice_spec.rb b/spec/features/admin/invoice_spec.rb index dd6fff2f2..4e3747373 100644 --- a/spec/features/admin/invoice_spec.rb +++ b/spec/features/admin/invoice_spec.rb @@ -6,15 +6,17 @@ feature 'Invoice', type: :feature do Fabricate(:invoice) end - it 'should show index of invoices' do + before do sign_in @user + end + + it 'should show index of invoices' do visit admin_invoices_url i = Invoice.first page.should have_link("Invoice no. #{i.id}") end it 'should show invoice' do - sign_in @user visit admin_invoices_url i = Invoice.first @@ -23,4 +25,21 @@ feature 'Invoice', type: :feature do page.should have_content("Details") page.should have_content("Paldiski mnt. 123") end + + it 'should issue an invoice' do + Fabricate(:eis) + r = Fabricate(:registrar) + visit admin_invoices_url + click_link('Add') + page.should have_content('Create new invoice') + select r.name, from: 'Registrar' + fill_in 'Amount', with: '100' + fill_in 'Description', with: 'test issue' + click_button 'Save' + page.should have_content('Record created') + page.should have_content('Invoice no.') + page.should have_content('Prepayment') + page.should have_content('120.0') + page.should have_content(r.name) + end end diff --git a/spec/models/bank_statement_spec.rb b/spec/models/bank_statement_spec.rb index ed9a0218e..d5812fbeb 100644 --- a/spec/models/bank_statement_spec.rb +++ b/spec/models/bank_statement_spec.rb @@ -12,8 +12,7 @@ describe BankStatement do @bank_statement.valid? @bank_statement.errors.full_messages.should match_array([ "Bank code is missing", - "Iban is missing", - "Queried at is missing" + "Iban is missing" ]) end