2019-05-07 12:24:51 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-02 12:34:16 +01:00
|
|
|
wb = xlsx_package.workbook
|
|
|
|
|
2019-05-07 12:24:51 +02:00
|
|
|
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
|
2017-03-02 13:31:12 +01:00
|
|
|
columns = [t('export_availabilities.date'), t('export_availabilities.day_of_week'), t('export_availabilities.slot'),
|
|
|
|
t('export_availabilities.machine'), t('export_availabilities.reservations')]
|
2019-05-07 12:24:51 +02:00
|
|
|
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|
|
2020-04-15 18:08:02 +02:00
|
|
|
slot_duration = a.slot_duration || ApplicationHelper::SLOT_DURATION
|
2017-03-02 12:34:28 +01:00
|
|
|
a.machines.each do |m|
|
2020-04-15 18:08:02 +02:00
|
|
|
((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
|
2019-05-07 12:24:51 +02:00
|
|
|
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 = [
|
2019-05-07 12:24:51 +02:00
|
|
|
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
|
|
|
]
|
2017-03-02 13:31:12 +01:00
|
|
|
styles = [date, nil, nil, nil, nil]
|
2019-05-07 12:24:51 +02:00
|
|
|
types = %i[date string string string integer]
|
2017-03-02 12:34:28 +01:00
|
|
|
|
2019-05-07 12:24:51 +02: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
|
2017-03-02 13:31:12 +01:00
|
|
|
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')]
|
2019-05-07 12:24:51 +02:00
|
|
|
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 = [
|
2019-05-07 12:24:51 +02:00
|
|
|
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
|
|
|
]
|
2017-03-02 13:31:12 +01:00
|
|
|
styles = [date, nil, nil, nil, nil, nil]
|
2019-05-07 12:24:51 +02:00
|
|
|
types = %i[date string string string integer integer]
|
2017-03-02 12:44:59 +01:00
|
|
|
|
2019-05-07 12:24:51 +02: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
|
2017-03-02 13:31:12 +01:00
|
|
|
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')]
|
2019-05-07 12:24:51 +02:00
|
|
|
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|
|
2020-04-15 18:08:02 +02:00
|
|
|
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 = [
|
2019-05-07 12:24:51 +02:00
|
|
|
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
|
|
|
]
|
2017-03-02 13:31:12 +01:00
|
|
|
styles = [date, nil, nil, nil, nil, nil]
|
2019-05-07 12:24:51 +02:00
|
|
|
types = %i[date string string string integer integer]
|
2017-03-02 12:57:07 +01:00
|
|
|
|
2019-05-07 12:24:51 +02: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
|
2017-03-02 13:31:12 +01:00
|
|
|
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 = [
|
2019-05-07 12:24:51 +02:00
|
|
|
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
|
|
|
]
|
2017-03-02 13:31:12 +01:00
|
|
|
styles = [date, nil, nil, nil, nil, nil]
|
2019-05-07 12:24:51 +02:00
|
|
|
types = %i[date string string string integer integer]
|
2017-03-02 13:05:29 +01:00
|
|
|
|
2019-05-07 12:24:51 +02:00
|
|
|
sheet.add_row data, style: styles, types: types
|
2017-03-02 13:05:29 +01:00
|
|
|
end
|
|
|
|
end
|