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

(feat) report store orders in the stats

This commit is contained in:
Sylvain 2022-10-12 11:42:15 +02:00
parent 33238e2ea5
commit 0016f248d8
6 changed files with 45 additions and 18 deletions

View File

@ -7,7 +7,7 @@ class Order < PaymentDocument
belongs_to :coupon
belongs_to :invoice
has_many :order_items, dependent: :destroy
has_one :payment_gateway_object, as: :item
has_one :payment_gateway_object, as: :item, dependent: :destroy
has_many :order_activities, dependent: :destroy
ALL_STATES = %w[cart paid payment_failed refunded in_progress ready canceled delivered].freeze

View File

@ -7,17 +7,23 @@ class Statistics::Builders::StoreOrdersBuilderService
class << self
def build(options = default_options)
# project list
Statistics::FetcherService.store_orders_list(options).each do |o|
Stats::Order.create({ date: format_date(o[:date]),
type: 'order',
subType: 'store',
ca: o[:ca],
products: o[:order_products],
categories: o[:order_categories],
orderId: o[:order_id],
state: o[:order_state],
stat: 1 }.merge(user_info_stat(o)))
states = {
'paid-processed': %w[paid in_progress ready delivered],
aborted: %w[payment_failed refunded canceled]
}
# orders list
states.each do |sub_type, order_states|
Statistics::FetcherService.store_orders_list(order_states, options).each do |o|
Stats::Order.create({ date: format_date(o[:date]),
type: 'store',
subType: sub_type,
ca: o[:ca],
products: o[:order_products],
categories: o[:order_categories],
orderId: o[:order_id],
state: o[:order_state],
stat: 1 }.merge(user_info_stat(o)))
end
end
end
end

View File

@ -183,12 +183,14 @@ class Statistics::FetcherService
result
end
def store_orders_list(options = default_options)
def store_orders_list(states, options = default_options)
result = []
Order.includes(order_items: [:orderable])
.joins(:order_items, :order_activities)
.where("order_items.orderable_type = 'Product'")
.where(order_items: { orderable_type: 'Product' })
.where(orders: { state: states })
.where('order_activities.created_at >= :start_date AND order_activities.created_at <= :end_date', options)
.group('orders.id')
.each do |o|
result.push({ date: o.created_at.to_date, ca: calcul_ca(o.invoice) }
.merge(user_info(o.statistic_profile))

View File

@ -1258,6 +1258,7 @@ en:
export_is_running_you_ll_be_notified_when_its_ready: "Export is running. You'll be notified when it's ready."
create_plans_to_start: "Start by creating new subscription plans."
click_here: "Click here to create your first one."
average_cart: "Average cart:"
#statistics graphs
stats_graphs:
statistics: "Statistics"

View File

@ -455,6 +455,8 @@ en:
project_publication: "Project publication"
duration: "Duration"
store: "Boutique"
paid-processed: "Paid and/or processed"
aborted: "Aborted"
#statistics exports to the Excel file format
export:
entries: "Entries"

View File

@ -1029,10 +1029,26 @@ end
unless StatisticIndex.find_by(es_type_key: 'order')
index = StatisticIndex.create!(es_type_key: 'order', label: I18n.t('statistics.orders'))
StatisticType.create!([
{ statistic_index_id: index.id, key: 'store', label: I18n.t('statistics.store'),
graph: true, simple: true }
])
type = StatisticType.create!([
{ statistic_index_id: index.id, key: 'store', label: I18n.t('statistics.store'),
graph: true, simple: true }
])
StatisticSubType.create!([
{ key: 'paid-processed', label: I18n.t('statistics.paid-processed'), statistic_types: [type] },
{ key: 'aborted', label: I18n.t('statistics.aborted'), statistic_types: [type] }
])
# average cart price for orders
average_cart = StatisticCustomAggregation.new(
statistic_type_id: type.id,
es_index: 'stats',
es_type: 'order',
field: 'average_cart',
query: '{"size":0, "aggregations":{"%{aggs_name}":{"avg":{"field":"ca", ' \
'"script":"BigDecimal.valueOf(_value).setScale(1, RoundingMode.HALF_UP)", "missing": 0}}}, ' \
'"query":{"bool":{"must":[{"range": {"date":{"gte":"%{start_date}", "lte":"%{end_date}"}}}]}}}'
)
average_cart.save!
end
ProfileCustomField.find_or_create_by(label: 'N° SIRET')