From 935c5470165de7d0ea224080937d7d536f19853e Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Fri, 26 Jun 2015 17:26:47 +0300 Subject: [PATCH] Issue invoices via admin #2566 --- app/controllers/admin/invoices_controller.rb | 24 ++++++++++++++ app/models/deposit.rb | 2 +- app/models/invoice.rb | 2 +- app/views/admin/invoices/index.haml | 8 ++--- app/views/admin/invoices/new.haml | 33 ++++++++++++++++++++ app/views/admin/invoices/show.haml | 2 +- config/locales/en.yml | 1 + spec/features/admin/invoice_spec.rb | 23 ++++++++++++-- 8 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 app/views/admin/invoices/new.haml 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/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/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..a799b4468 --- /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(:add), 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 04b370c61..6864c37c8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -833,3 +833,4 @@ en: 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/spec/features/admin/invoice_spec.rb b/spec/features/admin/invoice_spec.rb index dd6fff2f2..4c5640d7c 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 'Add' + 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