diff --git a/CHANGELOG.md b/CHANGELOG.md index 93791a192..3a3cd0d61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Fix a bug: unable to cancel a payment schedule - adds reservation context feature (for machine, training, space) +- adds coupon in statistic export (for subscription, machine, training, space, event, order) - [TODO DEPLOY] `rails db:seed` - [TODO DEPLOY] `rails fablab:es:build_stats` - [TODO DEPLOY] `rails fablab:maintenance:regenerate_statistics[2014,1]` diff --git a/app/helpers/excel_helper.rb b/app/helpers/excel_helper.rb index 75ede6bfc..9693c9262 100644 --- a/app/helpers/excel_helper.rb +++ b/app/helpers/excel_helper.rb @@ -70,6 +70,14 @@ module ExcelHelper types.push :float end + def add_coupon_cell(index, hit, data, styles, types) + return unless index.show_coupon? + + data.push hit['_source']['coupon'] + styles.push nil + types.push :text + end + ## # Retrieve an item in the given array of items # by default, the "id" is expected to match the given parameter but diff --git a/app/models/concerns/stat_reservation_concern.rb b/app/models/concerns/stat_reservation_concern.rb index b3727156d..077ac2c4d 100644 --- a/app/models/concerns/stat_reservation_concern.rb +++ b/app/models/concerns/stat_reservation_concern.rb @@ -9,5 +9,6 @@ module StatReservationConcern attribute :reservationContextId, Integer attribute :ca, Float attribute :name, String + attribute :coupon, String end end diff --git a/app/models/statistic_index.rb b/app/models/statistic_index.rb index ad45cd2c2..24b6ee0bf 100644 --- a/app/models/statistic_index.rb +++ b/app/models/statistic_index.rb @@ -9,4 +9,8 @@ class StatisticIndex < ApplicationRecord true end + + def show_coupon? + es_type_key.in? %w[subscription machine training event space order] + end end diff --git a/app/models/stats/order.rb b/app/models/stats/order.rb index 9e41e1af7..9b32caea2 100644 --- a/app/models/stats/order.rb +++ b/app/models/stats/order.rb @@ -10,4 +10,5 @@ class Stats::Order attribute :products, Array attribute :categories, Array attribute :ca, Float + attribute :coupon, String end diff --git a/app/models/stats/subscription.rb b/app/models/stats/subscription.rb index 2d60429c3..6eb6a5ccc 100644 --- a/app/models/stats/subscription.rb +++ b/app/models/stats/subscription.rb @@ -10,4 +10,5 @@ class Stats::Subscription attribute :subscriptionId, Integer attribute :invoiceItemId, Integer attribute :groupName, String + attribute :coupon, String end diff --git a/app/services/statistics/builder_service.rb b/app/services/statistics/builder_service.rb index 60fcc57d2..9e84eb697 100644 --- a/app/services/statistics/builder_service.rb +++ b/app/services/statistics/builder_service.rb @@ -17,7 +17,7 @@ class Statistics::BuilderService private def default_options - yesterday = 1.day.ago + yesterday = Time.current { start_date: yesterday.beginning_of_day, end_date: yesterday.end_of_day diff --git a/app/services/statistics/builders/reservations_builder_service.rb b/app/services/statistics/builders/reservations_builder_service.rb index cde74fe3f..ec6dba22f 100644 --- a/app/services/statistics/builders/reservations_builder_service.rb +++ b/app/services/statistics/builders/reservations_builder_service.rb @@ -18,7 +18,8 @@ class Statistics::Builders::ReservationsBuilderService ca: r[:ca], name: r["#{category}_name".to_sym], reservationId: r[:reservation_id], - reservationContextId: r[:reservation_context_id] + reservationContextId: r[:reservation_context_id], + coupon: r[:coupon] }.merge(user_info_stat(r))) stat[:stat] = (type == 'booking' ? 1 : r[:nb_hours]) stat["#{category}Id".to_sym] = r["#{category}_id".to_sym] diff --git a/app/services/statistics/builders/store_orders_builder_service.rb b/app/services/statistics/builders/store_orders_builder_service.rb index 302d24959..81a842d81 100644 --- a/app/services/statistics/builders/store_orders_builder_service.rb +++ b/app/services/statistics/builders/store_orders_builder_service.rb @@ -22,6 +22,7 @@ class Statistics::Builders::StoreOrdersBuilderService categories: o[:order_categories], orderId: o[:order_id], state: o[:order_state], + coupon: o[:coupon], stat: 1 }.merge(user_info_stat(o))) end end diff --git a/app/services/statistics/builders/subscriptions_builder_service.rb b/app/services/statistics/builders/subscriptions_builder_service.rb index 32f769931..2c1828ea2 100644 --- a/app/services/statistics/builders/subscriptions_builder_service.rb +++ b/app/services/statistics/builders/subscriptions_builder_service.rb @@ -16,6 +16,7 @@ class Statistics::Builders::SubscriptionsBuilderService planId: s[:plan_id], subscriptionId: s[:subscription_id], invoiceItemId: s[:invoice_item_id], + coupon: s[:coupon], groupName: s[:plan_group_name] }.merge(user_info_stat(s))) end end diff --git a/app/services/statistics/fetcher_service.rb b/app/services/statistics/fetcher_service.rb index cf6353547..9f5697c1e 100644 --- a/app/services/statistics/fetcher_service.rb +++ b/app/services/statistics/fetcher_service.rb @@ -34,6 +34,7 @@ class Statistics::FetcherService duration: p.find_statistic_type.key, subscription_id: sub.id, invoice_item_id: i.id, + coupon: i.invoice.coupon&.code, ca: ca }.merge(user_info(profile))) end result @@ -59,7 +60,8 @@ class Statistics::FetcherService slot_dates: r.slots.map(&:start_at).map(&:to_date), nb_hours: (r.slots.map(&:duration).map(&:to_i).reduce(:+) / 3600.0).to_f, ca: calcul_ca(r.original_invoice), - reservation_context_id: r.reservation_context_id + reservation_context_id: r.reservation_context_id, + coupon: r.original_invoice.coupon&.code }.merge(user_info(profile)) yield result end @@ -85,7 +87,8 @@ class Statistics::FetcherService slot_dates: r.slots.map(&:start_at).map(&:to_date), nb_hours: (r.slots.map(&:duration).map(&:to_i).reduce(:+) / 3600.0).to_f, ca: calcul_ca(r.original_invoice), - reservation_context_id: r.reservation_context_id + reservation_context_id: r.reservation_context_id, + coupon: r.original_invoice.coupon&.code }.merge(user_info(profile)) yield result end @@ -112,7 +115,8 @@ class Statistics::FetcherService training_date: slot.start_at.to_date, nb_hours: difference_in_hours(slot.start_at, slot.end_at), ca: calcul_ca(r.original_invoice), - reservation_context_id: r.reservation_context_id + reservation_context_id: r.reservation_context_id, + coupon: r.original_invoice&.coupon&.code }.merge(user_info(profile)) yield result end @@ -128,6 +132,7 @@ class Statistics::FetcherService .eager_load(:slots, :slots_reservations, :invoice_items, statistic_profile: [:group]) .find_each do |r| next unless r.reservable + next unless r.original_invoice profile = r.statistic_profile slot = r.slots.first @@ -141,6 +146,7 @@ class Statistics::FetcherService age_range: (r.reservable.age_range_id ? r.reservable.age_range.name : ''), nb_places: r.total_booked_seats, nb_hours: difference_in_hours(slot.start_at, slot.end_at), + coupon: r.original_invoice.coupon&.code, ca: calcul_ca(r.original_invoice) }.merge(user_info(profile)) yield result end @@ -215,7 +221,7 @@ class Statistics::FetcherService .where('order_activities.created_at >= :start_date AND order_activities.created_at <= :end_date', options) .group('orders.id') .find_each do |o| - result = { date: o.created_at.to_date, ca: calcul_ca(o.invoice) } + result = { date: o.created_at.to_date, ca: calcul_ca(o.invoice), coupon: o.invoice.coupon&.code } .merge(user_info(o.statistic_profile)) .merge(store_order_info(o)) yield result diff --git a/app/views/exports/statistics_current.xlsx.axlsx b/app/views/exports/statistics_current.xlsx.axlsx index 105fd0aff..97299c7e2 100644 --- a/app/views/exports/statistics_current.xlsx.axlsx +++ b/app/views/exports/statistics_current.xlsx.axlsx @@ -28,6 +28,7 @@ wb.add_worksheet(name: ExcelService.name_safe(index.label)) do |sheet| end columns.push t('export.reservation_context') if index.concerned_by_reservation_context? columns.push t('export.revenue') if index.ca + columns.push t('export.coupon') if index.show_coupon? sheet.add_row columns, style: header @@ -41,6 +42,7 @@ wb.add_worksheet(name: ExcelService.name_safe(index.label)) do |sheet| end add_hardcoded_cells(index, hit, data, styles, types) add_ca_cell(index, hit, data, styles, types) + add_coupon_cell(index, hit, data, styles, types) sheet.add_row data, style: styles, types: types end diff --git a/app/views/exports/statistics_global.xlsx.axlsx b/app/views/exports/statistics_global.xlsx.axlsx index 2b50393d7..a9b47ad10 100644 --- a/app/views/exports/statistics_global.xlsx.axlsx +++ b/app/views/exports/statistics_global.xlsx.axlsx @@ -20,6 +20,7 @@ indices.each do |index| end columns.push t('export.reservation_context') if index.concerned_by_reservation_context? columns.push t('export.revenue') if index.ca + columns.push t('export.coupon') if index.show_coupon? sheet.add_row columns, style: header # data rows @@ -39,6 +40,7 @@ indices.each do |index| add_hardcoded_cells(index, hit, data, styles, types) # proceed the 'ca' field if requested add_ca_cell(index, hit, data, styles, types) + add_coupon_cell(index, hit, data, styles, types) # finally, add the data row to the workbook's sheet sheet.add_row data, style: styles, types: types diff --git a/config/locales/en.yml b/config/locales/en.yml index 716ed08d3..f78c2203c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -506,6 +506,7 @@ en: female: "Woman" deleted_user: "Deleted user" reservation_context: "Reservation context" + coupon: "Coupon" #initial price's category for events, created to replace the old "reduced amount" property price_category: reduced_fare: "Reduced fare"