diff --git a/CHANGELOG.md b/CHANGELOG.md index e0b957c36..950cd8961 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Updated shakapaker to 6.5.5 - Fix a bug: unable to create a recurrent event - Fix a bug: unable to create a non-rolling plan +- Fix a bug: invalid duration for machine/spaces reservations in statistics, when using slots of not 1 hour - Fix a bug: invalid month in date format - Fix a bug: do not show theme and age-range fields in event form if no options were set - Fix a bug: do not show catgory select in plan form if no options were set diff --git a/app/controllers/api/trainings_controller.rb b/app/controllers/api/trainings_controller.rb index 81b711ab3..a38ecd984 100644 --- a/app/controllers/api/trainings_controller.rb +++ b/app/controllers/api/trainings_controller.rb @@ -48,6 +48,7 @@ class API::TrainingsController < API::ApiController head :no_content end + # This endpoint is used to get a list of trainings to validate def availabilities authorize Training @training = Training.find(params[:id]) diff --git a/app/frontend/src/javascript/controllers/admin/statistics.js b/app/frontend/src/javascript/controllers/admin/statistics.js index bce3a669a..1676fd93c 100644 --- a/app/frontend/src/javascript/controllers/admin/statistics.js +++ b/app/frontend/src/javascript/controllers/admin/statistics.js @@ -647,7 +647,7 @@ Application.Controllers.controller('StatisticsController', ['$scope', '$state', } } - return angular.forEach($scope.selectedIndex.additional_fields, function (field) { + angular.forEach($scope.selectedIndex.additional_fields, function (field) { const filter = { key: field.key, label: field.label, values: [] }; switch (field.data_type) { case 'index': filter.values.push('input_number'); break; diff --git a/app/models/stats/machine.rb b/app/models/stats/machine.rb index 96aa3bd8b..e1c95d182 100644 --- a/app/models/stats/machine.rb +++ b/app/models/stats/machine.rb @@ -7,4 +7,5 @@ class Stats::Machine include StatReservationConcern attribute :machineId, Integer + attribute :machineDates, Array end diff --git a/app/models/stats/space.rb b/app/models/stats/space.rb index 62b1e6aa7..2146267da 100644 --- a/app/models/stats/space.rb +++ b/app/models/stats/space.rb @@ -7,4 +7,5 @@ class Stats::Space include StatReservationConcern attribute :spaceId, Integer + attribute :spaceDates, Array end diff --git a/app/services/statistics/builders/reservations_builder_service.rb b/app/services/statistics/builders/reservations_builder_service.rb index 6c6bea826..e8b4020fc 100644 --- a/app/services/statistics/builders/reservations_builder_service.rb +++ b/app/services/statistics/builders/reservations_builder_service.rb @@ -30,6 +30,8 @@ class Statistics::Builders::ReservationsBuilderService def add_custom_attributes(category, stat, reservation_data) stat = add_event_attributes(category, stat, reservation_data) + stat = add_machine_attributes(category, stat, reservation_data) + stat = add_space_attributes(category, stat, reservation_data) add_training_attributes(category, stat, reservation_data) end @@ -50,5 +52,21 @@ class Statistics::Builders::ReservationsBuilderService stat end + + def add_machine_attributes(category, stat, reservation_data) + return stat unless category == 'machine' + + stat[:machineDates] = reservation_data[:slot_dates] + + stat + end + + def add_space_attributes(category, stat, reservation_data) + return stat unless category == 'space' + + stat[:spaceDates] = reservation_data[:slot_dates] + + stat + end end end diff --git a/app/services/statistics/concerns/helpers_concern.rb b/app/services/statistics/concerns/helpers_concern.rb index e07b8c441..d851a4eb2 100644 --- a/app/services/statistics/concerns/helpers_concern.rb +++ b/app/services/statistics/concerns/helpers_concern.rb @@ -39,7 +39,7 @@ module Statistics::Concerns::HelpersConcern def difference_in_hours(start_at, end_at) if start_at.to_date == end_at.to_date - ((end_at - start_at) / 60 / 60).to_i + ((end_at - start_at) / 3600.0).to_i else end_at_to_start_date = end_at.change(year: start_at.year, month: start_at.month, day: start_at.day) hours = ((end_at_to_start_date - start_at) / 60 / 60).to_i diff --git a/app/services/statistics/fetcher_service.rb b/app/services/statistics/fetcher_service.rb index 9838a343b..26c1fbbc5 100644 --- a/app/services/statistics/fetcher_service.rb +++ b/app/services/statistics/fetcher_service.rb @@ -54,7 +54,8 @@ class Statistics::FetcherService machine_id: r.reservable.id, machine_type: r.reservable.friendly_id, machine_name: r.reservable.name, - nb_hours: r.slots.size, + slot_dates: r.slots.map(&:start_at).map(&:to_date), + nb_hours: (r.slots.map(&:duration).map(&:to_i).reduce(:+) / 3600.0).to_i, ca: calcul_ca(r.original_invoice) }.merge(user_info(profile))) end result @@ -75,7 +76,8 @@ class Statistics::FetcherService space_id: r.reservable.id, space_name: r.reservable.name, space_type: r.reservable.slug, - nb_hours: r.slots.size, + slot_dates: r.slots.map(&:start_at).map(&:to_date), + nb_hours: (r.slots.map(&:duration).map(&:to_i).reduce(:+) / 3600.0).to_i, ca: calcul_ca(r.original_invoice) }.merge(user_info(profile))) end result diff --git a/app/views/api/statistics/index.json.jbuilder b/app/views/api/statistics/index.json.jbuilder index d0f638d66..7a3371a1c 100644 --- a/app/views/api/statistics/index.json.jbuilder +++ b/app/views/api/statistics/index.json.jbuilder @@ -1,3 +1,5 @@ +# frozen_string_literal: true + json.array!(@statistics) do |s| json.extract! s, :id, :es_type_key, :label, :table, :ca json.additional_fields s.statistic_fields do |f| @@ -12,8 +14,10 @@ json.array!(@statistics) do |s| json.extract! st, :id, :key, :label end end - json.graph do - json.chart_type s.statistic_graph.chart_type - json.limit s.statistic_graph.limit - end if s.statistic_graph -end \ No newline at end of file + if s.statistic_graph + json.graph do + json.chart_type s.statistic_graph.chart_type + json.limit s.statistic_graph.limit + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 27d4cd98c..f0c89e4a4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -435,6 +435,8 @@ en: statistics: subscriptions: "Subscriptions" machines_hours: "Machines slots" + machine_dates: "Slots dates" + space_dates: "Slots dates" spaces: "Spaces" orders: "Orders" trainings: "Trainings" diff --git a/test/services/statistics/reservation_subscription_statistic_service_test.rb b/test/services/statistics/reservation_subscription_statistic_service_test.rb index e998f3a19..72f7f58b5 100644 --- a/test/services/statistics/reservation_subscription_statistic_service_test.rb +++ b/test/services/statistics/reservation_subscription_statistic_service_test.rb @@ -16,7 +16,7 @@ class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationT test 'build stats' do # Create a reservation to generate an invoice (2 days ago) machine = Machine.find(1) - slot = Availability.find(19).slots.first + machine_slot = Availability.find(19).slots.first travel_to(2.days.ago) post '/api/local_payment/confirm_payment', params: { customer_id: @user.id, @@ -27,7 +27,7 @@ class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationT reservable_type: machine.class.name, slots_reservations_attributes: [ { - slot_id: slot.id + slot_id: machine_slot.id } ] } @@ -72,8 +72,8 @@ class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationT }.to_json, headers: default_headers travel_back - # Crate another machine reservation (today) - slot = Availability.find(19).slots.last + # Create another machine reservation (today) + machine_slot2 = Availability.find(19).slots.last post '/api/local_payment/confirm_payment', params: { customer_id: @user.id, items: [ @@ -83,7 +83,7 @@ class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationT reservable_type: machine.class.name, slots_reservations_attributes: [ { - slot_id: slot.id + slot_id: machine_slot2.id } ] } @@ -106,6 +106,7 @@ class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationT { term: { type: 'booking' } }] } }).first assert_not_nil stat_booking assert_equal machine.friendly_id, stat_booking['subType'] + assert_equal 1, stat_booking['stat'] check_statistics_on_user(stat_booking) stat_hour = Stats::Machine.search(query: { bool: { must: [{ term: { date: 2.days.ago.to_date.iso8601 } }, @@ -113,6 +114,8 @@ class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationT assert_not_nil stat_hour assert_equal machine.friendly_id, stat_hour['subType'] + assert_equal (machine_slot.duration.to_i / 3600.0).to_i, stat_hour['stat'] + assert_includes stat_hour['machineDates'], machine_slot.start_at.to_date.iso8601 check_statistics_on_user(stat_hour) # second machine reservation (today) @@ -120,6 +123,7 @@ class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationT { term: { type: 'booking' } }] } }).first assert_not_nil stat_booking assert_equal machine.friendly_id, stat_booking['subType'] + assert_equal 1, stat_booking['stat'] check_statistics_on_user(stat_booking) stat_hour = Stats::Machine.search(query: { bool: { must: [{ term: { date: DateTime.current.to_date.iso8601 } }, @@ -127,6 +131,8 @@ class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationT assert_not_nil stat_hour assert_equal machine.friendly_id, stat_hour['subType'] + assert_equal (machine_slot2.duration.to_i / 3600.0).to_i, stat_hour['stat'] + assert_includes stat_hour['machineDates'], machine_slot2.start_at.to_date.iso8601 check_statistics_on_user(stat_hour) # training @@ -136,6 +142,7 @@ class ReservationSubscriptionStatisticServiceTest < ActionDispatch::IntegrationT { term: { type: 'booking' } }] } }).first assert_not_nil stat_training assert_equal training.friendly_id, stat_training['subType'] + assert_equal tr_slot.start_at.to_date.iso8601, stat_training['trainingDate'] check_statistics_on_user(stat_training) # subscription