Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSV downloads for BankTransaction and Invoice #813

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/controllers/finance/bank_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ def index
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
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/finance/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/views/finance/bank_transactions/_transactions.html.haml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 7 additions & 2 deletions app/views/finance/invoices/_invoices.html.haml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 29 additions & 0 deletions lib/bank_transactions_csv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'csv'

class BankTransactionsCsv < RenderCSV
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BankTransactionsCSV?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's aligned with ArticlesCsv, OrderCsv and OrdergroupsCsv

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 changes: 37 additions & 0 deletions lib/invoices_csv.rb
Original file line number Diff line number Diff line change
@@ -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