diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index ec87224e5..b757574e3 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -8,17 +8,7 @@ def user_list includes(:user). order("members.member_uid") - respond_to do |format| - format.html - format.csv do - report = Report::Csv::Member.new(current_organization, @members) - send_data report.run, filename: report.name, type: report.mime_type - end - format.pdf do - report = Report::Pdf::Member.new(current_organization, @members) - send_data report.run, filename: report.name, type: report.mime_type - end - end + report_responder('Member', current_organization, @members) end def post_list @@ -32,16 +22,27 @@ def post_list to_a. sort_by { |category, _| category.try(:name).to_s } + report_responder('Post', current_organization, @posts, @post_type) + end + + def transfer_list + @transfers = current_organization.all_transfers_with_accounts + + report_responder('Transfer', current_organization, @transfers) + end + + private + + def report_responder(report_class, *args) respond_to do |format| format.html - format.csv do - report = Report::Csv::Post.new(current_organization, @posts, @post_type) - send_data report.run, filename: report.name, type: report.mime_type - end - format.pdf do - report = Report::Pdf::Post.new(current_organization, @posts, @post_type) - send_data report.run, filename: report.name, type: report.mime_type - end + format.csv { download_report("Report::Csv::#{report_class}", *args) } + format.pdf { download_report("Report::Pdf::#{report_class}", *args) } end end + + def download_report(report_class, *args) + report = report_class.constantize.new(*args) + send_data report.run, filename: report.name, type: report.mime_type + end end diff --git a/app/controllers/statistics_controller.rb b/app/controllers/statistics_controller.rb index ba2e4c9e3..aa719b219 100644 --- a/app/controllers/statistics_controller.rb +++ b/app/controllers/statistics_controller.rb @@ -83,10 +83,8 @@ def type_swaps end def all_transfers - @transfers = current_organization.all_transfers. - includes(movements: {account: :accountable}). - order("transfers.created_at DESC"). - distinct. + @transfers = current_organization. + all_transfers_with_accounts. page(params[:page]). per(20) end diff --git a/app/decorators/member_decorator.rb b/app/decorators/member_decorator.rb index e9385ce7b..f08f33e5c 100644 --- a/app/decorators/member_decorator.rb +++ b/app/decorators/member_decorator.rb @@ -28,7 +28,7 @@ def avatar_img(size=32) end def account_balance - view.seconds_to_hm(object.account.try(:balance) || 0) + view.seconds_to_hm(object.account.try(:balance)) end def toggle_manager_member_path diff --git a/app/decorators/member_report_decorator.rb b/app/decorators/report/member_decorator.rb similarity index 97% rename from app/decorators/member_report_decorator.rb rename to app/decorators/report/member_decorator.rb index 93cc9dd93..6d552c7b9 100644 --- a/app/decorators/member_report_decorator.rb +++ b/app/decorators/report/member_decorator.rb @@ -1,4 +1,4 @@ -class MemberReportDecorator +class Report::MemberDecorator def initialize(org, collection) @org = org @collection = collection diff --git a/app/decorators/post_report_decorator.rb b/app/decorators/report/post_decorator.rb similarity index 96% rename from app/decorators/post_report_decorator.rb rename to app/decorators/report/post_decorator.rb index ba64f3a61..4c3ef1952 100644 --- a/app/decorators/post_report_decorator.rb +++ b/app/decorators/report/post_decorator.rb @@ -1,4 +1,4 @@ -class PostReportDecorator +class Report::PostDecorator def initialize(org, collection, type) @org = org @collection = collection diff --git a/app/decorators/report/transfer_decorator.rb b/app/decorators/report/transfer_decorator.rb new file mode 100644 index 000000000..305a4dcdc --- /dev/null +++ b/app/decorators/report/transfer_decorator.rb @@ -0,0 +1,38 @@ +class Report::TransferDecorator + include Rails.application.routes.url_helpers + include ActionView::Helpers + include ApplicationHelper, TransfersHelper + + def initialize(org, collection) + @org = org + @collection = collection + end + + def name(extension) + "#{@org.name}_"\ + "#{Transfer.model_name.human(count: :many)}_"\ + "#{Date.today}."\ + "#{extension}" + end + + def headers + [ + I18n.t('statistics.all_transfers.date'), + I18n.t('statistics.all_transfers.from'), + I18n.t('statistics.all_transfers.to'), + I18n.t('statistics.all_transfers.post'), + I18n.t('statistics.all_transfers.quantity') + ] + end + + def rows + @collection.map do |transfer| + [ + transfer.created_at.to_s, + accounts_from_movements(transfer), + transfer.post.to_s, + seconds_to_hm(transfer.movements.first.amount.abs, 0) + ].flatten + end + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index cbb832429..b5f41ab56 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,5 +1,4 @@ module ApplicationHelper - TEXT_SUCCESS = 'text-success'.freeze TEXT_DANGER = 'text-danger'.freeze @@ -24,14 +23,15 @@ def mdash raw "—" end - def seconds_to_hm(seconds) + def seconds_to_hm(seconds, default = mdash) sign = seconds <=> 0 + if sign.try :nonzero? minutes, _seconds = seconds.abs.divmod(60) hours, minutes = minutes.divmod(60) - raw format("%s%d:%02d", ("-" if sign < 0), hours, minutes) + format("%s%d:%02d", ("-" if sign < 0), hours, minutes) else - mdash + default end end diff --git a/app/helpers/transfers_helper.rb b/app/helpers/transfers_helper.rb index 1110e0e24..074229e30 100644 --- a/app/helpers/transfers_helper.rb +++ b/app/helpers/transfers_helper.rb @@ -6,4 +6,20 @@ def accountable_path(accountable) user_path(accountable.user) end end + + def accounts_from_movements(transfer, with_links: false) + transfer.movements.sort_by(&:amount).map do |movement| + account = movement.account + + if account.accountable.blank? + I18n.t('.deleted_user') + elsif account.accountable_type == 'Organization' + link_to_if(with_links, account, organization_path(account.accountable)) + elsif account.accountable.active + link_to_if(with_links, account.accountable.display_name_with_uid, user_path(account.accountable.user)) + else + I18n.t('.inactive_user') + end + end + end end diff --git a/app/models/organization.rb b/app/models/organization.rb index 3e8d3a530..1fc189017 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -16,6 +16,13 @@ class Organization < ApplicationRecord before_validation :ensure_url after_create :create_account + def all_transfers_with_accounts + all_transfers. + includes(movements: { account: :accountable }). + order("transfers.created_at DESC"). + distinct + end + def to_s "#{name}" end diff --git a/app/services/report/csv/member.rb b/app/services/report/csv/member.rb index 83880ece2..35f5db89d 100644 --- a/app/services/report/csv/member.rb +++ b/app/services/report/csv/member.rb @@ -2,7 +2,7 @@ module Report module Csv class Member < Base def initialize(org, collection) - self.decorator = MemberReportDecorator.new(org, collection) + self.decorator = Report::MemberDecorator.new(org, collection) end end end diff --git a/app/services/report/csv/post.rb b/app/services/report/csv/post.rb index 99dc78065..67798bf9c 100644 --- a/app/services/report/csv/post.rb +++ b/app/services/report/csv/post.rb @@ -2,7 +2,7 @@ module Report module Csv class Post < Base def initialize(org, collection, type) - self.decorator = PostReportDecorator.new(org, collection, type) + self.decorator = Report::PostDecorator.new(org, collection, type) end end end diff --git a/app/services/report/csv/transfer.rb b/app/services/report/csv/transfer.rb new file mode 100644 index 000000000..a99f960e8 --- /dev/null +++ b/app/services/report/csv/transfer.rb @@ -0,0 +1,9 @@ +module Report + module Csv + class Transfer < Base + def initialize(org, collection) + self.decorator = Report::TransferDecorator.new(org, collection) + end + end + end +end diff --git a/app/services/report/pdf/member.rb b/app/services/report/pdf/member.rb index 650c2184a..6ce1abaa3 100644 --- a/app/services/report/pdf/member.rb +++ b/app/services/report/pdf/member.rb @@ -2,7 +2,7 @@ module Report module Pdf class Member < Base def initialize(org, collection) - self.decorator = MemberReportDecorator.new(org, collection) + self.decorator = Report::MemberDecorator.new(org, collection) end end end diff --git a/app/services/report/pdf/post.rb b/app/services/report/pdf/post.rb index 1232ee3e4..242cf057e 100644 --- a/app/services/report/pdf/post.rb +++ b/app/services/report/pdf/post.rb @@ -2,7 +2,7 @@ module Report module Pdf class Post < Base def initialize(org, collection, type) - self.decorator = PostReportDecorator.new(org, collection, type) + self.decorator = Report::PostDecorator.new(org, collection, type) end end end diff --git a/app/services/report/pdf/transfer.rb b/app/services/report/pdf/transfer.rb new file mode 100644 index 000000000..c6abe47ab --- /dev/null +++ b/app/services/report/pdf/transfer.rb @@ -0,0 +1,9 @@ +module Report + module Pdf + class Transfer < Base + def initialize(org, collection) + self.decorator = Report::TransferDecorator.new(org, collection) + end + end + end +end diff --git a/app/views/application/_navbar.html.erb b/app/views/application/_navbar.html.erb index 4f41013a3..d239ae4d1 100644 --- a/app/views/application/_navbar.html.erb +++ b/app/views/application/_navbar.html.erb @@ -70,13 +70,13 @@ <%= Inquiry.model_name.human(count: :many) %> <% end %> +
<%= t('statistics.all_transfers.date') %> | +<%= t('statistics.all_transfers.from') %> | +<%= t('statistics.all_transfers.to') %> | +<%= t('statistics.all_transfers.post') %> | +<%= t('statistics.all_transfers.quantity') %> | +
---|---|---|---|---|
<%= l(transfer.created_at, format: :long) %> | + <% accounts_from_movements(transfer).each do |account| %> +<%= account %> | + <% end %> +<%= transfer.post %> | +<%= seconds_to_hm(transfer.movements.first.amount.abs) %> | +