Add CSV downloads for BankTransaction and Invoice

This commit is contained in:
Patrick Gansterer 2021-02-08 01:07:10 +01:00
parent e0c0834690
commit 9f2e630266
6 changed files with 96 additions and 6 deletions

View File

@ -17,9 +17,16 @@ class Finance::BankTransactionsController < ApplicationController
end
@bank_account = BankAccount.find(params[:bank_account_id])
@bank_transactions = @bank_account.bank_transactions.order(sort).includes(:financial_link)
@bank_transactions = @bank_transactions.where('reference LIKE ? OR text LIKE ?', "%#{params[:query]}%", "%#{params[:query]}%") unless params[:query].nil?
@bank_transactions = @bank_transactions.page(params[:page]).per(@per_page)
@bank_transactions_all = @bank_account.bank_transactions.order(sort).includes(:financial_link)
@bank_transactions_all = @bank_transactions_all.where('reference LIKE ? OR text LIKE ?', "%#{params[:query]}%", "%#{params[:query]}%") unless params[:query].nil?
@bank_transactions = @bank_transactions_all.page(params[:page]).per(@per_page)
respond_to do |format|
format.js; format.html { render }
format.csv do
send_data BankTransactionsCsv.new(@bank_transactions_all).to_csv, filename: 'transactions.csv', type: 'text/csv'
end
end
end
def show

View File

@ -5,7 +5,15 @@ class Finance::InvoicesController < ApplicationController
before_action :ensure_can_edit, only: [:edit, :update, :destroy]
def index
@invoices = Invoice.includes(:supplier, :deliveries, :orders).order('date DESC').page(params[:page]).per(@per_page)
@invoices_all = Invoice.includes(:supplier, :deliveries, :orders).order('date DESC')
@invoices = @invoices_all.page(params[:page]).per(@per_page)
respond_to do |format|
format.js; format.html { render }
format.csv do
send_data InvoicesCsv.new(@invoices_all).to_csv, filename: 'invoices.csv', type: 'text/csv'
end
end
end
def unpaid

View File

@ -1,4 +1,8 @@
.pull-right
.btn-group
= link_to url_for(query: params[:query], format: :csv), class: 'btn' do
= glyph :download
CSV
- if @bank_transactions.total_pages > 1
.btn-group= items_per_page wrap: false
= pagination_links_remote @bank_transactions

View File

@ -1,5 +1,10 @@
- if Invoice.count > 20
= items_per_page
.pull-right
.btn-group
= link_to url_for(query: params[:query], format: :csv), class: 'btn' do
= glyph :download
CSV
- if @invoices.total_pages > 1
= items_per_page
= pagination_links_remote @invoices
%table.table.table-striped

View File

@ -0,0 +1,29 @@
require 'csv'
class BankTransactionsCsv < RenderCSV
include ApplicationHelper
def header
[
BankTransaction.human_attribute_name(:external_id),
BankTransaction.human_attribute_name(:date),
BankTransaction.human_attribute_name(:amount),
BankTransaction.human_attribute_name(:iban),
BankTransaction.human_attribute_name(:reference),
BankTransaction.human_attribute_name(:text)
]
end
def data
@object.each do |t|
yield [
t.external_id,
t.date,
t.amount,
t.iban,
t.reference,
t.text
]
end
end
end

37
lib/invoices_csv.rb Normal file
View File

@ -0,0 +1,37 @@
require 'csv'
class InvoicesCsv < RenderCSV
include ApplicationHelper
def header
[
Invoice.human_attribute_name(:created_at),
Invoice.human_attribute_name(:created_by),
Invoice.human_attribute_name(:date),
Invoice.human_attribute_name(:supplier),
Invoice.human_attribute_name(:number),
Invoice.human_attribute_name(:amount),
Invoice.human_attribute_name(:deposit),
Invoice.human_attribute_name(:deposit_credit),
Invoice.human_attribute_name(:paid_on),
Invoice.human_attribute_name(:note)
]
end
def data
@object.each do |t|
yield [
t.created_at,
show_user(t.created_by),
t.date,
t.supplier.name,
t.number,
t.amount,
t.deposit,
t.deposit_credit,
t.paid_on,
t.note,
]
end
end
end