From 4d09ed37a31305c2783b07869dbcbdfc7ce06e7f Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 2 Mar 2017 12:34:16 +0100 Subject: [PATCH] export machines availabilities --- app/services/availabilities_export_service.rb | 65 +++++++++++++++++++ .../exports/availabilities_index.xlsx.axlsx | 33 ++++++++++ app/workers/availabilities_export_worker.rb | 27 ++++++++ 3 files changed, 125 insertions(+) create mode 100644 app/services/availabilities_export_service.rb create mode 100644 app/views/exports/availabilities_index.xlsx.axlsx create mode 100644 app/workers/availabilities_export_worker.rb diff --git a/app/services/availabilities_export_service.rb b/app/services/availabilities_export_service.rb new file mode 100644 index 000000000..80b31d754 --- /dev/null +++ b/app/services/availabilities_export_service.rb @@ -0,0 +1,65 @@ +require 'abstract_controller' +require 'action_controller' +require 'action_view' +require 'active_record' + +# require any helpers +require './app/helpers/application_helper' + +class UsersExportService + + # export subscriptions + def export_subscriptions(export) + @subscriptions = Subscription.all.includes(:plan, :user => [:profile]) + + ActionController::Base.prepend_view_path './app/views/' + # place data in view_assigns + view_assigns = {subscriptions: @subscriptions} + av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns) + av.class_eval do + # include any needed helpers (for the view) + include ApplicationHelper + end + + content = av.render template: 'exports/users_subscriptions.xlsx.axlsx' + # write content to file + File.open(export.file,"w+b") {|f| f.puts content } + end + + # export reservations + def export_reservations(export) + @reservations = Reservation.all.includes(:slots, :reservable, :user => [:profile]) + + ActionController::Base.prepend_view_path './app/views/' + # place data in view_assigns + view_assigns = {reservations: @reservations} + av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns) + av.class_eval do + # include any needed helpers (for the view) + include ApplicationHelper + end + + content = av.render template: 'exports/users_reservations.xlsx.axlsx' + # write content to file + File.open(export.file,"w+b") {|f| f.puts content } + end + + # export members + def export_members(export) + @members = User.with_role(:member).includes(:group, :trainings, :tags, :invoices, :projects, :subscriptions => [:plan], :profile => [:address, :organization => [:address]]) + + ActionController::Base.prepend_view_path './app/views/' + # place data in view_assigns + view_assigns = {members: @members} + av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns) + av.class_eval do + # include any needed helpers (for the view) + include ApplicationHelper + end + + content = av.render template: 'exports/users_members.xlsx.axlsx' + # write content to file + File.open(export.file,"w+b") {|f| f.puts content } + end + +end \ No newline at end of file diff --git a/app/views/exports/availabilities_index.xlsx.axlsx b/app/views/exports/availabilities_index.xlsx.axlsx new file mode 100644 index 000000000..5b1a08d38 --- /dev/null +++ b/app/views/exports/availabilities_index.xlsx.axlsx @@ -0,0 +1,33 @@ +wb = xlsx_package.workbook + +header = wb.styles.add_style :b => true, :bg_color => Stylesheet.primary.upcase.gsub('#', 'FF'), :fg_color => 'FFFFFFFF' +date = wb.styles.add_style :format_code => Rails.application.secrets.excel_date_format + +wb.add_worksheet(name: t('export_subscriptions.subscriptions')) do |sheet| + + ## data table + # heading labels + columns = [t('export_subscriptions.id'), t('export_subscriptions.customer'), t('export_subscriptions.email'), + t('export_subscriptions.subscription'), t('export_subscriptions.period'), t('export_subscriptions.start_date'), + t('export_subscriptions.expiration_date'), t('export_subscriptions.amount'), t('export_subscriptions.payment_method')] + sheet.add_row columns, :style => header + + # data rows + @subscriptions.each do |sub| + data = [ + sub.user.id, + sub.user.profile.full_name, + sub.user.email, + sub.plan.human_readable_name(group: true), + t("duration.#{sub.plan.interval}", count: sub.plan.interval_count), + sub.created_at.to_date, + sub.expired_at.to_date, + number_to_currency(sub.plan.amount / 100), + (sub.stp_subscription_id.nil?)? t('export_subscriptions.local_payment') : t('export_subscriptions.online_payment') + ] + styles = [nil, nil, nil, nil, nil, date, date, nil, nil] + types = [:integer, :string, :string, :string, :string, :date, :date, :string, :string] + + sheet.add_row data, :style => styles, :types => types + end +end \ No newline at end of file diff --git a/app/workers/availabilities_export_worker.rb b/app/workers/availabilities_export_worker.rb new file mode 100644 index 000000000..95857f94e --- /dev/null +++ b/app/workers/availabilities_export_worker.rb @@ -0,0 +1,27 @@ +class UsersExportWorker + include Sidekiq::Worker + + def perform(export_id) + export = Export.find(export_id) + + unless export.user.is_admin? + raise SecurityError, 'Not allowed to export' + end + + unless export.category == 'users' + raise KeyError, 'Wrong worker called' + end + + service = UsersExportService.new + method_name = "export_#{export.export_type}" + + if %w(members subscriptions reservations).include?(export.export_type) and service.respond_to?(method_name) + service.public_send(method_name, export) + + NotificationCenter.call type: :notify_admin_export_complete, + receiver: export.user, + attached_object: export + end + + end +end