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

137 lines
4.8 KiB
Plaintext
Raw Normal View History

# frozen_string_literal: true
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:16 +01:00
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
columns = [t('export_availabilities.date'), t('export_availabilities.day_of_week'), t('export_availabilities.slot'),
t('export_availabilities.machine'), t('export_availabilities.reservations')]
sheet.add_row columns, style: header
2017-03-02 12:34:16 +01:00
# data rows
2017-03-02 12:34:28 +01:00
@availabilities.where(available_type: 'machines').order(:start_at).each do |a|
slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION
2017-03-02 12:34:28 +01:00
a.machines.each do |m|
((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i|
start_at = a.start_at + (i * slot_duration).minutes
end_at = a.start_at + (i * slot_duration).minutes + slot_duration.minutes
2017-03-02 12:34:28 +01:00
reservations = 0
if a.slots&.map(&:start_at)&.include? start_at
reservations = Reservation.where(reservable: m).includes(:slots).where('slots.id' => a.slots, 'slots.start_at' => start_at).count
2017-03-02 12:34:28 +01:00
end
2017-03-02 12:34:16 +01:00
2017-03-02 12:34:28 +01:00
data = [
start_at.to_date,
I18n.l(start_at, format: '%A').capitalize,
print_slot(start_at, end_at),
m.name,
reservations
2017-03-02 12:34:28 +01:00
]
styles = [date, nil, nil, nil, nil]
types = %i[date string string string integer]
2017-03-02 12:34:28 +01:00
sheet.add_row data, style: styles, types: types
2017-03-02 12:34:28 +01:00
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.day_of_week'), t('export_availabilities.slot'),
t('export_availabilities.training'), t('export_availabilities.reservations'),
t('export_availabilities.available_seats')]
sheet.add_row columns, style: header
2017-03-02 12:44:59 +01:00
# data rows
@availabilities.where(available_type: 'training').order(:start_at).each do |a|
data = [
a.start_at.to_date,
I18n.l(a.start_at, format: '%A').capitalize,
print_slot(a.start_at, a.end_at),
a.trainings.first.name,
a.reservations.count,
a.nb_total_places
2017-03-02 12:44:59 +01:00
]
styles = [date, nil, nil, nil, nil, nil]
types = %i[date string string string integer integer]
2017-03-02 12:44:59 +01:00
sheet.add_row data, style: styles, types: types
2017-03-02 12:44:59 +01:00
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.day_of_week'), t('export_availabilities.slot'),
t('export_availabilities.space'), t('export_availabilities.reservations'),
t('export_availabilities.available_seats')]
sheet.add_row columns, style: header
2017-03-02 12:57:07 +01:00
# data rows
@availabilities.where(available_type: 'space').order(:start_at).each do |a|
slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION
((a.end_at - a.start_at) / slot_duration.minutes).to_i.times do |i|
start_at = a.start_at + (i * slot_duration).minutes
end_at = a.start_at + (i * slot_duration).minutes + slot_duration.minutes
2017-03-02 12:57:07 +01:00
reservations = a.slots.where(start_at: start_at).count
data = [
start_at.to_date,
I18n.l(start_at, format: '%A').capitalize,
print_slot(start_at, end_at),
a.spaces.first.name,
reservations,
a.nb_total_places
2017-03-02 12:57:07 +01:00
]
styles = [date, nil, nil, nil, nil, nil]
types = %i[date string string string integer integer]
2017-03-02 12:57:07 +01:00
sheet.add_row data, style: styles, types: types
2017-03-02 12:57:07 +01:00
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.day_of_week'), t('export_availabilities.slot'),
t('export_availabilities.event'), t('export_availabilities.reservations'),
t('export_availabilities.available_seats')]
2017-03-02 13:05:29 +01:00
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,
I18n.l(a.start_at, format: '%A').capitalize,
print_slot(a.start_at, a.end_at),
a.event.title,
a.reservations.map(&:nb_reserve_places).reduce(:+) || 0,
a.nb_total_places
2017-03-02 13:05:29 +01:00
]
styles = [date, nil, nil, nil, nil, nil]
types = %i[date string string string integer integer]
2017-03-02 13:05:29 +01:00
sheet.add_row data, style: styles, types: types
2017-03-02 13:05:29 +01:00
end
end