diff --git a/app/controllers/api/statistics_controller.rb b/app/controllers/api/statistics_controller.rb index e82603202..d80c3fdca 100644 --- a/app/controllers/api/statistics_controller.rb +++ b/app/controllers/api/statistics_controller.rb @@ -6,7 +6,7 @@ class API::StatisticsController < API::ApiController @statistics = StatisticIndex.all end - %w(account event machine project subscription training user).each do |path| + %w(account event machine project subscription training user space).each do |path| class_eval %{ def #{path} authorize :statistic, :#{path}? diff --git a/app/models/stats/space.rb b/app/models/stats/space.rb new file mode 100644 index 000000000..fda08fa18 --- /dev/null +++ b/app/models/stats/space.rb @@ -0,0 +1,9 @@ +module Stats + class Space + include Elasticsearch::Persistence::Model + include StatConcern + include StatReservationConcern + + attribute :spaceId, Integer + end +end diff --git a/app/policies/statistic_policy.rb b/app/policies/statistic_policy.rb index 617e6f931..d598bd792 100644 --- a/app/policies/statistic_policy.rb +++ b/app/policies/statistic_policy.rb @@ -1,5 +1,5 @@ class StatisticPolicy < ApplicationPolicy - %w(index account event machine project subscription training user scroll export_subscription export_machine + %w(index account event machine project subscription training user space scroll export_subscription export_machine export_training export_event export_account export_project export_global).each do |action| define_method "#{action}?" do user.is_admin? diff --git a/app/services/statistic_service.rb b/app/services/statistic_service.rb index 4fa428ac2..33d6ae3ae 100644 --- a/app/services/statistic_service.rb +++ b/app/services/statistic_service.rb @@ -39,6 +39,23 @@ class StatisticService end end + # space list + reservations_space_list(options).each do |r| + %w(booking hour).each do |type| + stat = Stats::Space.new({ + date: format_date(r.date), + type: type, + subType: r.space_type, + ca: r.ca, + spaceId: r.space_id, + name: r.space_name, + reservationId: r.reservation_id + }.merge(user_info_stat(r))) + stat.stat = (type == 'booking' ? 1 : r.nb_hours) + stat.save + end + end + # training list reservations_training_list(options).each do |r| %w(booking hour).each do |type| @@ -170,6 +187,27 @@ class StatisticService result end + + def reservations_space_list(options = default_options) + result = [] + Reservation + .where("reservable_type = 'Space' AND reservations.created_at >= :start_date AND reservations.created_at <= :end_date", options) + .eager_load(:slots, user: [:profile, :group], invoice: [:invoice_items]) + .each do |r| + u = r.user + result.push OpenStruct.new({ + date: options[:start_date].to_date, + reservation_id: r.id, + space_id: r.reservable.id, + space_name: r.reservable.name, + space_type: r.reservable.slug, + nb_hours: r.slots.size, + ca: calcul_ca(r.invoice) + }.merge(user_info(u))) if r.reservable + end + result + end + def reservations_training_list(options = default_options) result = [] Reservation diff --git a/config/routes.rb b/config/routes.rb index e00d33a11..1647643dd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -164,7 +164,7 @@ Rails.application.routes.draw do end end - %w(account event machine project subscription training user).each do |path| + %w(account event machine project subscription training user space).each do |path| post "/stats/#{path}/_search", to: "api/statistics##{path}" post "/stats/#{path}/export", to: "api/statistics#export_#{path}" end