Add csv export to activities #2691

This commit is contained in:
Martin Lensment 2015-07-07 12:40:16 +03:00
parent af40038160
commit f7556f48c8
4 changed files with 25 additions and 4 deletions

View file

@ -6,12 +6,18 @@ class Registrar::AccountActivitiesController < RegistrarController
account = current_user.registrar.cash_account
ca_cache = params[:q][:created_at_lteq]
end_time = params[:q][:created_at_lteq].try(:to_date)
params[:q][:created_at_lteq] = end_time.try(:end_of_day)
begin
end_time = params[:q][:created_at_lteq].try(:to_date)
params[:q][:created_at_lteq] = end_time.try(:end_of_day)
rescue; end
@q = account.activities.includes(:invoice).search(params[:q])
@q.sorts = 'id desc' if @q.sorts.empty?
@account_activities = @q.result.page(params[:page])
respond_to do |format|
format.html { @account_activities = @q.result.page(params[:page]) }
format.csv { send_data @q.result.to_csv, filename: "account_activities_#{Time.zone.now.to_formatted_s(:number)}.csv" }
end
params[:q][:created_at_lteq] = ca_cache
end

View file

@ -18,6 +18,18 @@ class AccountActivity < ActiveRecord::Base
def types_for_select
[CREATE, RENEW, ADD_CREDIT].map { |x| [I18n.t(x), x] }
end
def to_csv
attributes = %w(description activity_type created_at sum)
CSV.generate(headers: true) do |csv|
csv << %w(description activity_type receipt_date sum)
all.each do |x|
csv << attributes.map{ |attr| x.send(attr) }
end
end
end
end
end

View file

@ -1,5 +1,7 @@
- content_for :actions do
= link_to(t(:back_to_billing), registrar_invoices_path, class: 'btn btn-default')
= link_to(t(:export_csv), url_for(params.merge(format: 'csv')), class: 'btn btn-default')
= render 'shared/title', name: t(:account_activity)
.row
@ -23,7 +25,7 @@
.form-group
= f.label t(:receipt_date_until)
= f.search_field :created_at_lteq, value: params[:q][:created_at_lteq], class: 'form-control datepicker', placeholder: t(:receipt_date_until), autocomplete: 'off'
.col-md-3{style: 'padding-top: 25px;'}
.col-md-6{style: 'padding-top: 25px;'}
%button.btn.btn-default
&nbsp;
%span.glyphicon.glyphicon-search

View file

@ -860,3 +860,4 @@ en:
receipt_date_from: 'Receipt date from'
receipt_date_until: 'Receipt date until'
add_credit: 'Add credit'
export_csv: 'Export CSV'