1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

export machines availabilities

This commit is contained in:
Sylvain 2017-03-02 12:34:16 +01:00
parent daefe626db
commit 4d09ed37a3
3 changed files with 125 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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