1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

(feat) add adds coupon in statistic export

This commit is contained in:
Du Peng 2023-08-24 18:31:16 +02:00
parent 581e08ea1e
commit be69befb6b
14 changed files with 36 additions and 6 deletions

View File

@ -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]`

View File

@ -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

View File

@ -9,5 +9,6 @@ module StatReservationConcern
attribute :reservationContextId, Integer
attribute :ca, Float
attribute :name, String
attribute :coupon, String
end
end

View File

@ -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

View File

@ -10,4 +10,5 @@ class Stats::Order
attribute :products, Array
attribute :categories, Array
attribute :ca, Float
attribute :coupon, String
end

View File

@ -10,4 +10,5 @@ class Stats::Subscription
attribute :subscriptionId, Integer
attribute :invoiceItemId, Integer
attribute :groupName, String
attribute :coupon, String
end

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"