1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/views/exports/availabilities_index.xlsx.axlsx
2020-06-10 11:52:54 +02:00

137 lines
4.7 KiB
Plaintext

# frozen_string_literal: true
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
## Machines slots
wb.add_worksheet(name: t('export_availabilities.machines')) do |sheet|
## 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
# data rows
@availabilities.where(available_type: 'machines').order(:start_at).each do |a|
slot_duration = a.slot_duration || Setting.get('slot_duration').to_i
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
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
end
data = [
start_at.to_date,
I18n.l(start_at, format: '%A').capitalize,
print_slot(start_at, end_at),
m.name,
reservations
]
styles = [date, nil, nil, nil, nil]
types = %i[date string string string integer]
sheet.add_row data, style: styles, types: types
end
end
end
end
## Trainings availabilities
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
# 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
]
styles = [date, nil, nil, nil, nil, nil]
types = %i[date string string string integer integer]
sheet.add_row data, style: styles, types: types
end
end
## Spaces slots
if Setting.get('spaces_module')
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
# data rows
@availabilities.where(available_type: 'space').order(:start_at).each do |a|
slot_duration = a.slot_duration || Setting.get('slot_duration').to_i
((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
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
]
styles = [date, nil, nil, nil, nil, nil]
types = %i[date string string string integer integer]
sheet.add_row data, style: styles, types: types
end
end
end
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')]
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
]
styles = [date, nil, nil, nil, nil, nil]
types = %i[date string string string integer integer]
sheet.add_row data, style: styles, types: types
end
end