From ef0888f38d28630266a6380bc9dca2310b4bd7e8 Mon Sep 17 00:00:00 2001 From: wvengen Date: Tue, 6 May 2014 19:02:01 +0200 Subject: [PATCH] add view for all transactions + csv --- .../financial_transactions_controller.rb | 20 +++++++++---- app/helpers/application_helper.rb | 6 +++- .../_transactions.html.haml | 14 +++++++-- .../_transactions_search.html.haml | 22 ++++++++++++++ .../financial_transactions/index.html.haml | 8 ++--- .../financial_transactions/index.js.erb | 1 + .../financial_transactions/index.js.haml | 1 - .../index_collection.html.haml | 8 +++++ .../index_collection.js.erb | 1 + app/views/finance/index.html.haml | 2 +- app/views/finance/ordergroups/index.html.haml | 3 +- config/locales/en.yml | 7 ++++- config/locales/nl.yml | 7 ++++- config/routes.rb | 1 + lib/financial_transactions_csv.rb | 29 +++++++++++++++++++ 15 files changed, 111 insertions(+), 19 deletions(-) create mode 100644 app/views/finance/financial_transactions/_transactions_search.html.haml create mode 100644 app/views/finance/financial_transactions/index.js.erb delete mode 100644 app/views/finance/financial_transactions/index.js.haml create mode 100644 app/views/finance/financial_transactions/index_collection.html.haml create mode 100644 app/views/finance/financial_transactions/index_collection.js.erb create mode 100644 lib/financial_transactions_csv.rb diff --git a/app/controllers/finance/financial_transactions_controller.rb b/app/controllers/finance/financial_transactions_controller.rb index 8865958e..7e9e26ec 100644 --- a/app/controllers/finance/financial_transactions_controller.rb +++ b/app/controllers/finance/financial_transactions_controller.rb @@ -1,7 +1,7 @@ # encoding: utf-8 class Finance::FinancialTransactionsController < ApplicationController before_filter :authenticate_finance - before_filter :find_ordergroup, :except => [:new_collection, :create_collection] + before_filter :find_ordergroup, :except => [:new_collection, :create_collection, :index_collection] inherit_resources # belongs_to :ordergroup @@ -19,13 +19,23 @@ class Finance::FinancialTransactionsController < ApplicationController sort = "created_on DESC" end - @financial_transactions = @ordergroup.financial_transactions.includes(:user).order(sort). - page(params[:page]).per(@per_page) - if params[:query].present? - @financial_transactions = @financial_transactions.where('note LIKE ?', "%#{params[:query]}%") + @q = FinancialTransaction.search(params[:q]) + @financial_transactions_all = @q.result(distinct: true).includes(:user).order(sort) + @financial_transactions_all = @financial_transactions_all.where(ordergroup_id: @ordergroup.id) if @ordergroup + @financial_transactions = @financial_transactions_all.page(params[:page]).per(@per_page) + + respond_to do |format| + format.js; format.html { render } + format.csv do + send_data FinancialTransactionsCsv.new(@financial_transactions_all).to_csv, filename: 'transactions.csv', type: 'text/csv' + end end end + def index_collection + index + end + def new @financial_transaction = @ordergroup.financial_transactions.build end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index dc4ff1eb..2c711547 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -40,8 +40,12 @@ module ApplicationHelper link_to(per_page, params, :remote => true, class: link_class) end - content_tag :div, class: 'btn-group pull-right' do + if options[:wrap] == false links.join.html_safe + else + content_tag :div, class: 'btn-group pull-right' do + links.join.html_safe + end end end diff --git a/app/views/finance/financial_transactions/_transactions.html.haml b/app/views/finance/financial_transactions/_transactions.html.haml index 1dfbc2cb..c6a8cc39 100644 --- a/app/views/finance/financial_transactions/_transactions.html.haml +++ b/app/views/finance/financial_transactions/_transactions.html.haml @@ -1,10 +1,18 @@ -- if @ordergroup.financial_transactions.count > 20 - = items_per_page +- with_ordergroup ||= false +.pull-right + .btn-group + = link_to url_for(q: params[:q], format: :csv), class: 'btn' do + = glyph :download + CSV + - if @financial_transactions.total_pages > 1 + .btn-group= items_per_page wrap: false = pagination_links_remote @financial_transactions %table.table.table-striped %thead %tr %th= sort_link_helper heading_helper(FinancialTransaction, :created_on), "date" + - if with_ordergroup + %th= heading_helper FinancialTransaction, :ordergroup %th= heading_helper FinancialTransaction, :user %th= sort_link_helper heading_helper(FinancialTransaction, :note), "note" %th= sort_link_helper heading_helper(FinancialTransaction, :amount), "amount" @@ -12,6 +20,8 @@ - @financial_transactions.each do |t| %tr %td= format_time(t.created_on) + - if with_ordergroup + %td= h link_to t.ordergroup.name, finance_ordergroup_transactions_path(t.ordergroup) %td= h show_user(t.user) %td= h t.note %td.currency{:style => "color:#{t.amount < 0 ? 'red' : 'black'}; width:5em"}= number_to_currency(t.amount) diff --git a/app/views/finance/financial_transactions/_transactions_search.html.haml b/app/views/finance/financial_transactions/_transactions_search.html.haml new file mode 100644 index 00000000..916ebc22 --- /dev/null +++ b/app/views/finance/financial_transactions/_transactions_search.html.haml @@ -0,0 +1,22 @@ +.well.well-small + = search_form_for @q, url: url, + html: {data: {'submit-onchange' => true}, method: :get, remote: true, class: 'form-search'} do |f| + = f.text_field :note_cont, class: 'search-query', placeholder: t('.search_placeholder') + +   + = label_tag 'q_created_on_gteq', FinancialTransaction.human_attribute_name(:created_on) +   + .input-append.input-prepend + = f.text_field :created_on_gteq, class: 'input-small datepicker' + %span.add-on - + = f.text_field :created_on_lteq, class: 'input-small search-query datepicker' + +   + = label_tag 'q_amount_gteq', FinancialTransaction.human_attribute_name(:amount) +   + .input-append.input-prepend + %span.add-on= t 'number.currency.format.unit' + = f.text_field :amount_gteq, class: 'input-mini' + %span.add-on - + = f.text_field :amount_lteq, class: 'input-mini search-query' + diff --git a/app/views/finance/financial_transactions/index.html.haml b/app/views/finance/financial_transactions/index.html.haml index 7de3526a..f53078c2 100644 --- a/app/views/finance/financial_transactions/index.html.haml +++ b/app/views/finance/financial_transactions/index.html.haml @@ -8,11 +8,7 @@ %strong= t('.balance', balance: number_to_currency(@ordergroup.account_balance)) %br/ %small= t('.last_updated_at', when: distance_of_time_in_words(Time.now, @ordergroup.account_updated)) -.well.well-small - = form_tag finance_ordergroup_transactions_path(@ordergroup), :method => :get, :remote => true, - 'data-submit-onchange' => true, class: 'form-search' do - = text_field_tag :query, params[:query], class: 'input-medium search-query', - placeholder: t('.search_placeholder') += render 'transactions_search', url: finance_ordergroup_transactions_path(@ordergroup) -#transactions= render 'transactions' \ No newline at end of file +#transactions= render 'transactions' diff --git a/app/views/finance/financial_transactions/index.js.erb b/app/views/finance/financial_transactions/index.js.erb new file mode 100644 index 00000000..c456548d --- /dev/null +++ b/app/views/finance/financial_transactions/index.js.erb @@ -0,0 +1 @@ +$('#transactions').html('<%= escape_javascript(render("transactions")) %>'); diff --git a/app/views/finance/financial_transactions/index.js.haml b/app/views/finance/financial_transactions/index.js.haml deleted file mode 100644 index 53f69018..00000000 --- a/app/views/finance/financial_transactions/index.js.haml +++ /dev/null @@ -1 +0,0 @@ -$('#transactions').html('#{escape_javascript(render("transactions"))}'); diff --git a/app/views/finance/financial_transactions/index_collection.html.haml b/app/views/finance/financial_transactions/index_collection.html.haml new file mode 100644 index 00000000..b67dd176 --- /dev/null +++ b/app/views/finance/financial_transactions/index_collection.html.haml @@ -0,0 +1,8 @@ +- title t('.title') + +- content_for :actionbar do + = link_to t('.show_groups'), finance_ordergroups_path, class: 'btn' + += render 'transactions_search', url: finance_transactions_path + +#transactions= render 'transactions', with_ordergroup: true diff --git a/app/views/finance/financial_transactions/index_collection.js.erb b/app/views/finance/financial_transactions/index_collection.js.erb new file mode 100644 index 00000000..60739382 --- /dev/null +++ b/app/views/finance/financial_transactions/index_collection.js.erb @@ -0,0 +1 @@ +$('#transactions').html('<%= escape_javascript(render("transactions", with_ordergroup: true)) %>'); diff --git a/app/views/finance/index.html.haml b/app/views/finance/index.html.haml index e2d64811..2b4f8503 100644 --- a/app/views/finance/index.html.haml +++ b/app/views/finance/index.html.haml @@ -22,7 +22,7 @@ %h2 = t('.last_transactions') - %small= link_to(t('.show_all'), finance_ordergroups_path) + %small= link_to(t('.show_all'), finance_transactions_path) %table.table.table-striped %thead %tr diff --git a/app/views/finance/ordergroups/index.html.haml b/app/views/finance/ordergroups/index.html.haml index e9c73f21..577ad6d1 100644 --- a/app/views/finance/ordergroups/index.html.haml +++ b/app/views/finance/ordergroups/index.html.haml @@ -1,6 +1,7 @@ - title t('.title') - content_for :actionbar do + = link_to t('.show_all'), finance_transactions_path, class: 'btn' = link_to t('.new_transaction'), finance_new_transaction_collection_path, class: 'btn btn-primary' .well.well-small @@ -10,4 +11,4 @@ placeholder: t('.search_placeholder') #ordergroupsTable - = render :partial => "ordergroups" \ No newline at end of file + = render :partial => "ordergroups" diff --git a/config/locales/en.yml b/config/locales/en.yml index b3424676..e47da771 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -590,8 +590,10 @@ en: balance: 'Balance of account: %{balance}' last_updated_at: (last updated %{when} ago) new_transaction: Create new transaction - search_placeholder: Search ... title: Account statement for %{name} + index_collection: + title: Financial transactions + show_groups: Manage accounts new: paragraph: Here you can credit and debit money for the order group %{name}. title: New transaction @@ -603,6 +605,8 @@ en: ordergroup: remove: Remove remove_group: Remove group + transactions_search: + search_placeholder: Search note ... index: amount_fc: Amount(FC) end: End @@ -633,6 +637,7 @@ en: index: new_transaction: Add new transactions search_placeholder: Search ... + show_all: All transactions title: Manage accounts ordergroups: account_statement: Account statement diff --git a/config/locales/nl.yml b/config/locales/nl.yml index 387fc11b..97e20735 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -588,8 +588,10 @@ nl: balance: 'Tegoed: %{balance}' last_updated_at: (laatst bijgewerkt %{when} geleden) new_transaction: Nieuwe transactie toevoegen - search_placeholder: Zoeken ... title: Rekeningoverzicht voor %{name} + index_collection: + title: FinanciĆ«le transacties + show_groups: Tegoeden new: paragraph: Hier kun je het tegoed van huishouden %{name} ophogen en verlagen. title: Nieuwe transactie @@ -601,6 +603,8 @@ nl: ordergroup: remove: Verwijderen remove_group: Huishouden verwijderen + transactions_search: + search_placeholder: Zoeken in notitie... index: amount_fc: Bedrag(FC) end: Einde @@ -631,6 +635,7 @@ nl: index: new_transaction: Nieuwe transacties toevoegen search_placeholder: Zoeken ... + show_all: Transactieoverzicht title: Tegoeden beheren ordergroups: account_statement: Rekeningafschrift diff --git a/config/routes.rb b/config/routes.rb index ebf638a9..3af42129 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -159,6 +159,7 @@ Foodsoft::Application.routes.draw do resources :ordergroups, :only => [:index] do resources :financial_transactions, :as => :transactions end + get 'transactions' => 'financial_transactions#index_collection' get 'transactions/new_collection' => 'financial_transactions#new_collection', :as => 'new_transaction_collection' post 'transactions/create_collection' => 'financial_transactions#create_collection', :as => 'create_transaction_collection' diff --git a/lib/financial_transactions_csv.rb b/lib/financial_transactions_csv.rb new file mode 100644 index 00000000..20375cac --- /dev/null +++ b/lib/financial_transactions_csv.rb @@ -0,0 +1,29 @@ +require 'csv' + +class FinancialTransactionsCsv < RenderCSV + include ApplicationHelper + + def header + [ + FinancialTransaction.human_attribute_name(:created_on), + FinancialTransaction.human_attribute_name(:ordergroup), + FinancialTransaction.human_attribute_name(:ordergroup), + FinancialTransaction.human_attribute_name(:user), + FinancialTransaction.human_attribute_name(:note), + FinancialTransaction.human_attribute_name(:amount) + ] + end + + def data + @object.includes(:user, :ordergroup).each do |t| + yield [ + t.created_on, + t.ordergroup_id, + t.ordergroup.name, + show_user(t.user), + t.note, + number_to_currency(t.amount) + ] + end + end +end