Class: Report::SourcesReport::SourcesReport

Inherits:
Object
  • Object
show all
Defined in:
app/services/report/sources_report/sources_report.rb

Overview

Service object: sources report.

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.comparison_date_range(period, start_date, end_date) ⇒ Object

Comparison date range.

Parameters:

  • period (Object)
  • start_date (Date)
  • end_date (Date)

Returns:

  • (Object)


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/services/report/sources_report/sources_report.rb', line 213

def self.comparison_date_range(period, start_date, end_date)
  case period
  when 1
    comparison_start_date = start_date - (end_date - start_date) - 1.day
    comparison_end_date = start_date - 1.day
  when 2
    comparison_start_date = start_date - 1.year
    comparison_end_date = end_date - 1.year
  else
    comparison_start_date = nil
    comparison_end_date = nil
  end

  { comparison_start_date:, comparison_end_date: }
end

.date_range(period) ⇒ Object

Date range.

Parameters:

  • period (Object)

Returns:

  • (Object)


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/services/report/sources_report/sources_report.rb', line 177

def self.date_range(period)
  case period
  when 1
    start_date = Date.current.beginning_of_month
    end_date = Date.current
  when 2
    start_date = Date.current.beginning_of_quarter
    end_date = Date.current
  when 3
    start_date = Date.current.beginning_of_year
    end_date = Date.current
  when 4
    start_date = 1.month.ago.beginning_of_month
    end_date = 1.month.ago.end_of_month
  when 5
    start_date = Date.current.last_quarter.beginning_of_quarter
    end_date = Date.current.last_quarter.end_of_quarter
  when 6
    start_date = 2.months.ago.beginning_of_month
    end_date = Date.current.end_of_month
  when 7
    start_date = 11.months.ago.beginning_of_month
    end_date = Date.current.end_of_month
  else
    start_date = nil
    end_date = nil
  end

  { start_date:, end_date: }
end

.result_report(options = {}) ⇒ Result

Returns sources report data.

Parameters:

  • options (Hash) (defaults to: {})

    report filter options

Options Hash (options):

  • baseline_period (Array<String>)

    first element is the baseline period identifier

  • comparison_period (Array<String>)

    first element is the comparison period identifier

  • period1_gteq (Date)

    start of the reporting period

  • period1_lteq (Date)

    end of the reporting period

  • source_id (String)

    source to report on

Returns:

  • (Result)

    sources report data



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/services/report/sources_report/sources_report.rb', line 69

def self.result_report(options = {})
  success = false
  baseline_period = options[:baseline_period].present? ? options[:baseline_period].first.to_i : nil
  comparison_period = options[:comparison_period].present? ? options[:comparison_period].first.to_i : nil

  if baseline_period.present?
    dates = date_range(baseline_period)
    start_date = dates[:start_date]
    end_date = dates[:end_date]
  else
    start_date = options[:period1_gteq]
    end_date = options[:period1_lteq]
  end

  if comparison_period.present?
    comparison_dates = comparison_date_range(comparison_period, start_date.to_date, end_date.to_date)
    comparison_start_date = comparison_dates[:comparison_start_date]
    comparison_end_date = comparison_dates[:comparison_end_date]
  end

  if start_date.present? && end_date.present?
    source_id = options[:source_id].presence
    parent_source = Source.find(source_id) if source_id.present? || !source_id.nil?
    main_sources = source_id.present? ? Source.where.any_of({ id: source_id }, { parent_id: source_id }).select("name, full_name, id").order(:name) : Source.where(parent_id: source_id).select("name, full_name, id").order(:name)
    sources = {}
    # Populate our sources
    main_sources.each do |s|
      descendants_ids = source_id.present? && source_id == s.id ? [source_id] : s.self_and_descendants_ids
      # revenue = Invoice.joins(:customer).sales_orders.where("parties.source_id" => descendants_ids).where(gl_date: start_date..end_date).sum(:revenue_consolidated)
      revenue = Invoice.sales_orders.where(source_id: descendants_ids).where(gl_date: start_date..end_date).sum(:revenue_consolidated)
      re_desc_ids = Invoice.sales_orders.where(source_id: descendants_ids).where(gl_date: start_date..end_date).select(:source_id).distinct.map { |r| r[:source_id] }

      customers = Customer.non_guests.where(source_id: descendants_ids).where(created_at: start_date..end_date).count(:id)
      c_desc_ids = Customer.non_guests.where(source_id: descendants_ids).where(created_at: start_date..end_date).select(:source_id).distinct.map { |r| r[:source_id] }

      opportunities = Opportunity.where(source_id: descendants_ids).where(created_at: start_date..end_date).group(:state).count(:id)
      op_desc_ids = Opportunity.where(source_id: descendants_ids).where(created_at: start_date..end_date).select(:source_id).distinct.map { |r| r[:source_id] }
      opps_created = opportunities.sum { |_k, v| v } || 0
      opps_won = opportunities['won'] || 0
      opps_conversion_ratio = opps_created.positive? ? ((opps_won.to_f / opps_created) * 100).round(1) : 0

      rooms = RoomConfiguration.joins(:opportunity).where(opportunities: { source_id: descendants_ids }).where(room_configurations: { created_at: start_date..end_date }).count("room_configurations.id")
      orders = Order.so_only.invoiced.where(shipped_date: start_date..end_date).where(source_id: descendants_ids).count(:id)
      or_desc_ids = Order.so_only.invoiced.where(shipped_date: start_date..end_date).where(source_id: descendants_ids).select(:source_id).distinct.map { |r| r[:source_id] }

      if comparison_start_date.present? && comparison_end_date.present?
        comparison_revenue = Invoice.sales_orders.where(source_id: descendants_ids).where(gl_date: comparison_start_date..comparison_end_date).sum(:revenue_consolidated)
        comp_re_desc_ids = Invoice.sales_orders.where(source_id: descendants_ids).where(gl_date: comparison_start_date..comparison_end_date).select(:source_id).distinct.map { |r| r[:source_id] }

        comparison_customers = Customer.non_guests.where(source_id: descendants_ids).where(created_at: comparison_start_date..comparison_end_date).count(:id)
        comp_c_desc_ids = Customer.non_guests.where(source_id: descendants_ids).where(created_at: comparison_start_date..comparison_end_date).select(:source_id).distinct.map { |r| r[:source_id] }

        comparison_opportunities = Opportunity.where(source_id: descendants_ids).where(created_at: comparison_start_date..comparison_end_date).group(:state).count(:id)
        comp_op_desc_ids = Opportunity.where(source_id: descendants_ids).where(created_at: comparison_start_date..comparison_end_date).select(:source_id).distinct.map { |r| r[:source_id] }
        comp_opps_created = comparison_opportunities.sum { |_k, v| v } || 0
        comp_opps_won = comparison_opportunities['won'] || 0
        comp_opps_conversion_ratio = comp_opps_created.positive? ? ((comp_opps_won.to_f / comp_opps_created) * 100).round(1) : 0

        comparison_rooms = RoomConfiguration.joins(:opportunity).where(opportunities: { source_id: descendants_ids }).where(room_configurations: { created_at: comparison_start_date..comparison_end_date }).count("room_configurations.id")
        comparison_orders = Order.so_only.invoiced.where(shipped_date: comparison_start_date..comparison_end_date).where(source_id: descendants_ids).count(:id)
        comp_or_desc_ids = Order.so_only.invoiced.where(shipped_date: comparison_start_date..comparison_end_date).where(source_id: descendants_ids).select(:source_id).distinct.map { |r| r[:source_id] }
      end

      if comparison_start_date.present? && comparison_end_date.present?
        descendants_ids_with_data = (re_desc_ids + c_desc_ids + op_desc_ids + or_desc_ids + comp_re_desc_ids + comp_c_desc_ids + comp_op_desc_ids + comp_or_desc_ids).compact.uniq
        sources[s] = {
                revenue: revenue,
                comparison_revenue: comparison_revenue,
                customers: customers,
                comparison_customers: comparison_customers,
                opps_created: opps_created,
                comparison_opps_created: comp_opps_created,
                opps_won: opps_won,
                comparison_opps_won: comp_opps_won,
                opps_conversion_ratio: opps_conversion_ratio,
                comparison_opps_conversion_ratio: comp_opps_conversion_ratio,
                rooms: rooms,
                comparison_rooms: comparison_rooms,
                orders: orders,
                comparison_orders: comparison_orders,
                descendants_ids: descendants_ids_with_data
        }
      else
        descendants_ids_with_data = (re_desc_ids + c_desc_ids + op_desc_ids + or_desc_ids).compact.uniq
        if revenue.positive? || customers.positive? || opps_created.positive? || opps_won.positive? || opps_conversion_ratio.positive? || orders.positive? || rooms > 0
          sources[s] = {
                  revenue: revenue,
                  customers: customers,
                  opps_created: opps_created,
                  opps_won: opps_won,
                  opps_conversion_ratio: opps_conversion_ratio,
                  rooms: rooms,
                  orders: orders,
                  descendants_ids: descendants_ids_with_data
          }
        end
      end
      success = true
    end
  end

  Result.new(success: success, start_date: start_date, end_date: end_date, baseline_period: [baseline_period].compact, comparison_period: [comparison_period].compact, comparison_start_date: comparison_start_date,
comparison_end_date: comparison_end_date, parent_source: parent_source, sources: sources)
end