1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/views/exports/availabilities_index.xlsx.axlsx

126 lines
4.3 KiB
Plaintext
Raw Normal View History

2017-03-02 12:34:16 +01:00
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
2017-03-02 12:34:28 +01:00
## Machines slots
wb.add_worksheet(name: t('export_availabilities.machines')) do |sheet|
2017-03-02 12:34:16 +01:00
## data table
# heading labels
2017-03-02 12:34:28 +01:00
columns = [t('export_availabilities.date'), t('export_availabilities.slot'), t('export_availabilities.machine'),
t('export_availabilities.reservations')]
2017-03-02 12:34:16 +01:00
sheet.add_row columns, :style => header
# data rows
2017-03-02 12:34:28 +01:00
@availabilities.where(available_type: 'machines').order(:start_at).each do |a|
a.machines.each do |m|
((a.end_at - a.start_at) / ApplicationHelper::SLOT_DURATION.minutes).to_i.times do |i|
start_at = a.start_at + (i * ApplicationHelper::SLOT_DURATION).minutes
end_at = a.start_at + (i * ApplicationHelper::SLOT_DURATION).minutes + ApplicationHelper::SLOT_DURATION.minutes
reservations = 0
if a.slots and a.slots.map(&:start_at).include? start_at
reservations = 1
end
2017-03-02 12:34:16 +01:00
2017-03-02 12:34:28 +01:00
data = [
start_at.to_date,
print_slot(start_at, end_at),
m.name,
reservations
]
styles = [date, nil, nil, nil]
types = [:date, :string, :string, :integer]
sheet.add_row data, :style => styles, :types => types
end
end
2017-03-02 12:34:16 +01:00
end
2017-03-02 12:44:59 +01:00
end
2017-03-02 13:05:29 +01:00
## Trainings availabilities
2017-03-02 12:44:59 +01:00
wb.add_worksheet(name: t('export_availabilities.trainings')) do |sheet|
## data table
# heading labels
columns = [t('export_availabilities.date'), t('export_availabilities.slot'), t('export_availabilities.training'),
t('export_availabilities.reservations'), t('export_availabilities.available_seats')]
sheet.add_row columns, :style => header
# data rows
@availabilities.where(available_type: 'training').order(:start_at).each do |a|
data = [
a.start_at.to_date,
print_slot(a.start_at, a.end_at),
a.trainings.first.name,
a.reservations.count,
a.nb_total_places
]
styles = [date, nil, nil, nil, nil]
types = [:date, :string, :string, :integer, :integer]
sheet.add_row data, :style => styles, :types => types
end
2017-03-02 12:57:07 +01:00
end
## Spaces slots
if Rails.application.secrets.fablab_without_spaces != 'false'
wb.add_worksheet(name: t('export_availabilities.spaces')) do |sheet|
## data table
# heading labels
columns = [t('export_availabilities.date'), t('export_availabilities.slot'), t('export_availabilities.space'),
t('export_availabilities.reservations'), t('export_availabilities.available_seats')]
sheet.add_row columns, :style => header
# data rows
@availabilities.where(available_type: 'space').order(:start_at).each do |a|
((a.end_at - a.start_at) / ApplicationHelper::SLOT_DURATION.minutes).to_i.times do |i|
start_at = a.start_at + (i * ApplicationHelper::SLOT_DURATION).minutes
end_at = a.start_at + (i * ApplicationHelper::SLOT_DURATION).minutes + ApplicationHelper::SLOT_DURATION.minutes
reservations = a.slots.where(start_at: start_at).count
data = [
start_at.to_date,
print_slot(start_at, end_at),
a.spaces.first.name,
reservations,
a.nb_total_places
]
styles = [date, nil, nil, nil, nil]
types = [:date, :string, :string, :integer, :integer]
sheet.add_row data, :style => styles, :types => types
end
end
end
2017-03-02 13:05:29 +01:00
end
## Events availabilities
wb.add_worksheet(name: t('export_availabilities.events')) do |sheet|
## data table
# heading labels
columns = [t('export_availabilities.date'), t('export_availabilities.slot'), t('export_availabilities.event'),
t('export_availabilities.reservations'), t('export_availabilities.available_seats')]
sheet.add_row columns, :style => header
# data rows
@availabilities.where(available_type: 'event').order(:start_at).each do |a|
data = [
a.start_at.to_date,
print_slot(a.start_at, a.end_at),
a.event.title,
a.reservations.map(&:nb_reserve_places).reduce(:+) || 0,
a.nb_total_places
]
styles = [date, nil, nil, nil, nil]
types = [:date, :string, :string, :integer, :integer]
sheet.add_row data, :style => styles, :types => types
end
end