Class: Report::CouponSalesReport::CouponSalesReport

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

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.get_coupon_vs_branch(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 410

def self.get_coupon_vs_branch(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  company_sql = company == 0 ? " " : (" and vsd.company_id = #{company} ")
  report_groupings_sql = report_groupings.empty? ? " " : " and vsd.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')}) "
  customer_ids_sql = customer_ids.empty? ? " " : " and vsd.customer_id in (#{customer_ids.map{|cu| cu }.join(',')})"
  coupon_ids_sql = coupon_ids.empty? ? " " : " and vsd.coupon_id in (#{coupon_ids.map{|cp| cp }.join(',')})"
  type_sql = type.present? ? " and vsd.type = '#{type}' " : " "

  # company_detail_sql =  company == 0 ? "'All Branches'" : "case when vss.company_id = 1 then 'USA' else 'Canada' end"
  company_sales_sql = company == 0 ? " " : (" and vss.company_id = #{company} ")
  report_groupings_sales_sql = report_groupings.empty? ? " " : " and vss.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')}) "
  customer_ids_sales_sql = customer_ids.empty? ? " " : " and vss.customer_id in (#{customer_ids.map{|cu| cu }.join(',')})"
  coupon_ids_sales_sql = coupon_ids.empty? ? " " : " and vss.customer_id in (select distinct vsd.customer_id from view_sales_discounts vsd where (gl_date between '#{start_date}' and '#{end_date}') and is_inactive = #{status} #{type_sql} #{company_sql}) "
  # group_by_sql =  company == 0 ? " " : " group by vss.company_id "

  sql = <<-SQL
    select
    case when vss.company_id = 1 then 'B-USA' else 'B-Canada' end as detail,
    sum(quantity * discounted_price * consolidated_exchange_rate) as sales
    from view_sales_shippings vss
    where (gl_date between '#{start_date}' and '#{end_date}')
    #{company_sales_sql}
    #{report_groupings_sales_sql}
    #{customer_ids_sales_sql}
    #{coupon_ids_sales_sql}
    group by vss.company_id
    union all
    select ('CP-' || vsd.code) as detail,sum(abs(discount_amount * vsd.consolidated_exchange_rate)) as sales
    from view_sales_discounts vsd
    where (gl_date between '#{start_date}' and '#{end_date}')
    and is_inactive = #{status}
    #{type_sql}
    #{company_sql}
    #{report_groupings_sql}
    #{customer_ids_sql}
    #{coupon_ids_sql}
    group by vsd.code;
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.get_coupon_vs_customer(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 327

def self.get_coupon_vs_customer(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  company_sql = company == 0 ? " " : (" and vsd.company_id = #{company} ")
  report_groupings_sql = report_groupings.empty? ? " " : " and vsd.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')}) "
  customer_ids_sql = customer_ids.empty? ? " " : " and vsd.customer_id in (#{customer_ids.map{|cu| cu }.join(',')})"
  coupon_ids_sql = coupon_ids.empty? ? " " : " and vsd.coupon_id in (#{coupon_ids.map{|cp| cp }.join(',')})"
  type_sql = type.present? ? " and vsd.type = '#{type}' " : " "

  customer_detail_sql = customer_ids.empty? ? "'All Customers'" : "('CU-' || cu.full_name)"
  company_sales_sql = company == 0 ? " " : (" and vss.company_id = #{company} ")
  report_groupings_sales_sql = report_groupings.empty? ? " " : " and vss.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')}) "
  customer_ids_sales_sql = customer_ids.empty? ? " " : " and vss.customer_id in (#{customer_ids.map{|cu| cu }.join(',')})"
  coupon_ids_sales_sql = coupon_ids.empty? ? " " : " and vss.customer_id in (select distinct vsd.customer_id from view_sales_discounts vsd where (gl_date between '#{start_date}' and '#{end_date}') and is_inactive = #{status} #{type_sql} #{coupon_ids_sql} ) "
  group_by_sql = customer_ids.empty? ? " " : " group by cu.full_name "

  sql = <<-SQL
    select
    #{customer_detail_sql} as detail,
    sum(quantity * discounted_price * consolidated_exchange_rate) as sales
    from view_sales_shippings vss
    inner join parties cu on vss.customer_id = cu.id
    where (gl_date between '#{start_date}' and '#{end_date}')
    #{company_sales_sql}
    #{report_groupings_sales_sql}
    #{customer_ids_sales_sql}
    #{coupon_ids_sales_sql}
    #{group_by_sql}
    union all
    select ('CP-' || vsd.code) as detail,sum(abs(discount_amount * vsd.consolidated_exchange_rate)) as sales
    from view_sales_discounts vsd
    where (gl_date between '#{start_date}' and '#{end_date}')
    and is_inactive = #{status}
    #{type_sql}
    #{company_sql}
    #{report_groupings_sql}
    #{customer_ids_sql}
    #{coupon_ids_sql}
    group by vsd.code;
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.get_coupon_vs_report_grouping(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 369

def self.get_coupon_vs_report_grouping(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  company_sql = company == 0 ? " " : (" and vsd.company_id = #{company} ")
  report_groupings_sql = report_groupings.empty? ? " " : " and vsd.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')}) "
  customer_ids_sql = customer_ids.empty? ? " " : " and vsd.customer_id in (#{customer_ids.map{|cu| cu }.join(',')})"
  coupon_ids_sql = coupon_ids.empty? ? " " : " and vsd.coupon_id in (#{coupon_ids.map{|cp| cp }.join(',')})"
  type_sql = type.present? ? " and vsd.type = '#{type}' " : " "

  # rg_detail_sql = report_groupings.empty? ? "'All Report Groupings'" : "report_grouping"
  company_sales_sql = company == 0 ? " " : (" and vss.company_id = #{company} ")
  report_groupings_sales_sql = report_groupings.empty? ? " " : " and vss.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')}) "
  customer_ids_sales_sql = customer_ids.empty? ? " " : " and vss.customer_id in (#{customer_ids.map{|cu| cu }.join(',')})"
  coupon_ids_sales_sql = coupon_ids.empty? ? " " : " and vss.customer_id in (select distinct vsd.customer_id from view_sales_discounts vsd where (gl_date between '#{start_date}' and '#{end_date}') and is_inactive = #{status} #{type_sql} #{report_groupings_sql}) "
  # group_by_sql = report_groupings.empty? ? " " : " group by vss.report_grouping "

  sql = <<-SQL
    select
    ('RG-' || report_grouping) as detail,
    sum(quantity * discounted_price * consolidated_exchange_rate) as sales
    from view_sales_shippings vss
    where (gl_date between '#{start_date}' and '#{end_date}')
    #{company_sales_sql}
    #{report_groupings_sales_sql}
    #{customer_ids_sales_sql}
    #{coupon_ids_sales_sql}
    group by vss.report_grouping
    union all
    select ('CP-' || vsd.code) as detail,sum(abs(discount_amount * vsd.consolidated_exchange_rate)) as sales
    from view_sales_discounts vsd
    where (gl_date between '#{start_date}' and '#{end_date}')
    and is_inactive = #{status}
    #{type_sql}
    #{company_sql}
    #{report_groupings_sql}
    #{customer_ids_sql}
    #{coupon_ids_sql}
    group by vsd.code;
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.get_coupons_by_month(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 207

def self.get_coupons_by_month(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  company_sql = company == 0 ? " " : (" and vsd.company_id = #{company} ")
  report_groupings_sql = report_groupings.empty? ? " " : " and vsd.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')}) "
  customer_ids_sql = customer_ids.empty? ? " " : " and vsd.customer_id in (#{customer_ids.map{|cu| cu }.join(',')})"
  coupon_ids_sql = coupon_ids.empty? ? " " : " and vsd.coupon_id in (#{coupon_ids.map{|cp| cp }.join(',')})"
  type_sql = type.present? ? " and vsd.type = '#{type}' " : " "

  sql = <<-SQL
    select
    vsd.coupon_id,
    vsd.code,
    sum(case when dt.month = 1 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as january,
    sum(case when dt.month = 2 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as february,
    sum(case when dt.month = 3 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as march,
    sum(case when dt.month = 4 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as april,
    sum(case when dt.month = 5 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as may,
    sum(case when dt.month = 6 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as june,
    sum(case when dt.month = 7 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as july,
    sum(case when dt.month = 8 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as august,
    sum(case when dt.month = 9 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as september,
    sum(case when dt.month = 10 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as october,
    sum(case when dt.month = 11 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as november,
    sum(case when dt.month = 12 then abs(discount_amount * consolidated_exchange_rate) else 0 end) as december,
    sum(abs(discount_amount * consolidated_exchange_rate)) as total
    from view_sales_discounts vsd
    inner join analytic_date_time_dimensions dt on vsd.gl_date = dt.date
    where (gl_date between '#{start_date}' and '#{end_date}')
    and vsd.is_inactive = #{status}
    #{type_sql}
    #{company_sql}
    #{report_groupings_sql}
    #{customer_ids_sql}
    #{coupon_ids_sql}
    group by vsd.coupon_id,vsd.code;
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.get_coupons_items(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 294

def self.get_coupons_items(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  company_sql = company == 0 ? " " : (" and vsd.company_id = #{company} ")
  report_groupings_sql = report_groupings.empty? ? " " : " and vsd.report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')}) "
  customer_ids_sql = customer_ids.empty? ? " " : " and vsd.customer_id in (#{customer_ids.map{|cu| cu }.join(',')})"
  coupon_ids_sql = coupon_ids.empty? ? " " : " and vsd.coupon_id in (#{coupon_ids.map{|cp| cp }.join(',')})"
  type_sql = type.present? ? " and vsd.type = '#{type}' " : " "

  sql = <<-SQL
    select
    vsd.coupon_id,
    vsd.code,
    count(vsd.coupon_id) as qty_coupons_used,
    sum(abs(discount_amount * vsd.consolidated_exchange_rate)) as discount_value,
    vss.item_id,
    i.sku,
    sum(vss.quantity) as qty_items_sold,
    sum(quantity * discounted_price * vss.consolidated_exchange_rate) as item_sold
    from view_sales_shippings vss
    inner join view_sales_discounts vsd on vss.line_item_id = vsd.line_item_id
    inner join items i on vss.item_id = i.id
    where (vss.gl_date between '#{start_date}' and '#{end_date}')
    and vsd.is_inactive = #{status}
    #{type_sql}
    #{company_sql}
    #{report_groupings_sql}
    #{customer_ids_sql}
    #{coupon_ids_sql}
    group by vsd.coupon_id,vsd.code,vss.item_id,i.sku;
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.get_items_by_month(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 246

def self.get_items_by_month(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  company_sql = company == 0 ? " " : (" and company_id = #{company} ")
  report_groupings_sql = report_groupings.empty? ? " " : " and report_grouping in (#{report_groupings.map{|rg| "'#{rg.sub("'","''")}'" }.join(',')}) "
  customer_ids_sql = customer_ids.empty? ? " " : " and customer_id in (#{customer_ids.map{|cu| cu }.join(',')})"
  coupon_ids_sql = coupon_ids.empty? ? " " : " and coupon_id in (#{coupon_ids.map{|cp| cp }.join(',')})"
  type_sql = type.present? ? " and type = '#{type}' " : " "

  sql = <<-SQL
    with items_discounted as (
        select distinct line_item_id
        from view_sales_discounts
        where (gl_date between '#{start_date}' and '#{end_date}')
        and is_inactive = #{status}
        #{type_sql}
        #{company_sql}
        #{report_groupings_sql}
        #{customer_ids_sql}
        #{coupon_ids_sql}

    ), sales_items as (
        select
        vss.item_id,
        i.sku,
        sum(case when dt.month = 1 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as january,
        sum(case when dt.month = 2 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as february,
        sum(case when dt.month = 3 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as march,
        sum(case when dt.month = 4 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as april,
        sum(case when dt.month = 5 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as may,
        sum(case when dt.month = 6 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as june,
        sum(case when dt.month = 7 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as july,
        sum(case when dt.month = 8 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as august,
        sum(case when dt.month = 9 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as september,
        sum(case when dt.month = 10 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as october,
        sum(case when dt.month = 11 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as november,
        sum(case when dt.month = 12 then quantity * discounted_price * consolidated_exchange_rate else 0 end) as december,
        sum(quantity * discounted_price * consolidated_exchange_rate) as total
        from view_sales_shippings vss
        inner join items_discounted itd on vss.line_item_id = itd.line_item_id
        inner join items i on vss.item_id = i.id
        inner join analytic_date_time_dimensions dt on vss.gl_date = dt.date
        group by vss.item_id,i.sku
    )
    select * from sales_items;
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.get_top_ten_coupons(cy_start_date, cy_end_date, lm_start_date, lm_end_date, type, status) ⇒ Object



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
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 96

def self.get_top_ten_coupons(cy_start_date,cy_end_date,lm_start_date,lm_end_date,type,status)
  type_sql = type.present? ? " and type = '#{type}' " : " "

  sql = <<-SQL
    with top_discounts as (
        select coupon_id,sum(discount_amount * consolidated_exchange_rate)
        from view_sales_discounts
        where (gl_date between '#{cy_start_date}' and '#{cy_end_date}')
        and is_inactive = #{status}
        #{type_sql}
        group by coupon_id
        order by 2 limit 10

    ), top_discounts_li as (
        select coupon_id,line_item_id,sum(discount_amount * consolidated_exchange_rate)
        from view_sales_discounts
        where gl_date between '#{cy_start_date}' and '#{cy_end_date}'
        and coupon_id in (select coupon_id from top_discounts)
        group by coupon_id,line_item_id

    ), sales as (
        select
        line_item_id,
        sum(case when gl_date between '#{cy_start_date}' and '#{cy_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end) as cy_sales_value,
        sum(case when gl_date between '#{lm_start_date}' and '#{lm_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end) as lm_sales_value
        from view_sales_shippings
        where (gl_date between '#{cy_start_date}' and '#{cy_end_date}' or gl_date between '#{lm_start_date}' and '#{lm_end_date}')
        and line_item_id in (select line_item_id from top_discounts_li)
        group by line_item_id

    ), discounts as (
        select
        vsd.coupon_id,
        vsd.code,
        line_item_id,
        abs(sum(case when gl_date between '#{cy_start_date}' and '#{cy_end_date}' then discount_amount * consolidated_exchange_rate else 0 end)) as cy_discount_value,
        abs(sum(case when gl_date between '#{lm_start_date}' and '#{lm_end_date}' then discount_amount * consolidated_exchange_rate else 0 end)) as lm_discount_value
        from view_sales_discounts vsd
        where (gl_date between '#{cy_start_date}' and '#{cy_end_date}' or gl_date between '#{lm_start_date}' and '#{lm_end_date}')
        and vsd.coupon_id in (select coupon_id from top_discounts)
        group by vsd.coupon_id,vsd.code,line_item_id

    )
    select d.coupon_id,code,
    sum(cy_discount_value) as cy_discount_value,sum(cy_sales_value) as cy_sales_value,case when sum(cy_sales_value) = 0 then 0 else (sum(cy_discount_value) / sum(cy_sales_value)) * 100 end as cy_ptg,
    sum(lm_discount_value) as lm_discount_value,sum(lm_sales_value) as lm_sales_value,case when sum(lm_sales_value) = 0 then 0 else (sum(lm_discount_value) / sum(lm_sales_value)) * 100 end as lm_ptg
    from sales s
    inner join discounts d on s.line_item_id = d.line_item_id
    group by d.coupon_id,code
    order by 3 desc;
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.get_top_ten_products(cy_start_date, cy_end_date, lm_start_date, lm_end_date, type, status) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 151

def self.get_top_ten_products(cy_start_date,cy_end_date,lm_start_date,lm_end_date,type,status)
  type_sql = type.present? ? " and type = '#{type}' " : " "

  sql = <<-SQL
    with top_discounts as (
        select item_id,sum(discount_amount * consolidated_exchange_rate)
        from view_sales_discounts
        where (gl_date between '#{cy_start_date}' and '#{cy_end_date}')
        and is_inactive = #{status}
        #{type_sql}
        group by item_id
        order by 2 limit 10

    ), top_discounts_li as (
        select item_id,line_item_id,sum(discount_amount * consolidated_exchange_rate)
        from view_sales_discounts
        where gl_date between '#{cy_start_date}' and '#{cy_end_date}'
        and item_id in (select item_id from top_discounts)
        group by item_id,line_item_id

    ), sales as (
        select
        line_item_id,
        sum(case when gl_date between '#{cy_start_date}' and '#{cy_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end) as cy_sales_value,
        sum(case when gl_date between '#{lm_start_date}' and '#{lm_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end) as lm_sales_value
        from view_sales_shippings
        where (gl_date between '#{cy_start_date}' and '#{cy_end_date}' or gl_date between '#{lm_start_date}' and '#{lm_end_date}')
        and line_item_id in (select line_item_id from top_discounts_li)
        group by line_item_id

    ), discounts as (
        select
        vsd.item_id,
        i.sku,
        line_item_id,
        abs(sum(case when gl_date between '#{cy_start_date}' and '#{cy_end_date}' then discount_amount * consolidated_exchange_rate else 0 end)) as cy_discount_value,
        abs(sum(case when gl_date between '#{lm_start_date}' and '#{lm_end_date}' then discount_amount * consolidated_exchange_rate else 0 end)) as lm_discount_value
        from view_sales_discounts vsd
        inner join items i on vsd.item_id = i.id
        where (gl_date between '#{cy_start_date}' and '#{cy_end_date}' or gl_date between '#{lm_start_date}' and '#{lm_end_date}')
        and vsd.item_id in (select item_id from top_discounts)
        group by vsd.item_id,i.sku,line_item_id

    )
    select d.item_id,sku,
    sum(cy_discount_value) as cy_discount_value,sum(cy_sales_value) as cy_sales_value,case when sum(cy_sales_value) = 0 then 0 else (sum(cy_discount_value) / sum(cy_sales_value)) * 100 end as cy_ptg,
    sum(lm_discount_value) as lm_discount_value,sum(lm_sales_value) as lm_sales_value,case when sum(lm_sales_value) = 0 then 0 else (sum(lm_discount_value) / sum(lm_sales_value)) * 100 end as lm_ptg
    from sales s
    inner join discounts d on s.line_item_id = d.line_item_id
    group by d.item_id,sku
    order by 3 desc;
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.get_total_by_country(cy_start_date, cy_end_date, lm_start_date, lm_end_date, type, status) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 52

def self.get_total_by_country(cy_start_date,cy_end_date,lm_start_date,lm_end_date,type,status)
  type_sql = type.present? ? " and type = '#{type}' " : " "

  sql = <<-SQL
    with sales as (
        select
        company_id,
        case when company_id = 1 then 'USA' else 'CANADA' end as company_name,
        sum(case when gl_date between '#{cy_start_date}' and '#{cy_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end) as cy_sales_value,
        sum(case when gl_date between '#{lm_start_date}' and '#{lm_end_date}' then quantity * discounted_price * consolidated_exchange_rate else 0 end) as lm_sales_value
        from view_sales_shippings
        where order_id in (
            select distinct order_id
            from view_sales_discounts
            where (gl_date between '#{cy_start_date}' and '#{cy_end_date}' or gl_date between '#{lm_start_date}' and '#{lm_end_date}')
            and is_inactive = #{status}
            #{type_sql}
        )
        group by company_id

    ), discounts as (
        select
        company_id,
        case when company_id = 1 then 'USA' else 'CANADA' end as company_name,
        abs(sum(case when gl_date between '#{cy_start_date}' and '#{cy_end_date}' then discount_amount * consolidated_exchange_rate else 0 end)) as cy_discount_value,
        abs(sum(case when gl_date between '#{lm_start_date}' and '#{lm_end_date}' then discount_amount * consolidated_exchange_rate else 0 end)) as lm_discount_value
        from view_sales_discounts
        where (gl_date between '#{cy_start_date}' and '#{cy_end_date}' or gl_date between '#{lm_start_date}' and '#{lm_end_date}')
        #{type_sql}
        and is_inactive = #{status}
        group by company_id

    )
    select s.company_id,s.company_name,
    cy_discount_value,cy_sales_value,case when cy_sales_value = 0 then 0 else (cy_discount_value / cy_sales_value) * 100 end as cy_ptg,
    lm_discount_value,lm_sales_value,case when lm_sales_value = 0 then 0 else (lm_discount_value / lm_sales_value) * 100 end as lm_ptg
    from sales s
    inner join discounts d on s.company_id = d.company_id
    order by 1;
  SQL

  results = ActiveRecord::Base.connection.execute(sql).to_a.map{ |r| r.symbolize_keys }
end

.result_report(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 8

def self.result_report(options = {})
  start_date = options[:period1_gteq]
  end_date = options[:period1_lteq]
  type = options[:type]
  status = options[:status]
  company = options[:company_ids].present? ? (options[:company_ids] == 'USA' ? 1 : 2) : 0
  report_groupings = options[:report_groupings].map(&:presence).compact
  customer_ids = options[:customer_ids].map(&:presence).compact
  coupon_ids = options[:coupon_ids].map(&:presence).compact

  cy_start_date = Date.current.beginning_of_year
  cy_end_date = Date.current - 1
  lm_start_date = cy_end_date.months_ago(1).beginning_of_month
  lm_end_date = lm_start_date.end_of_month

  total_by_country = get_total_by_country(cy_start_date,cy_end_date,lm_start_date,lm_end_date,type,status)
  top_ten_coupons = get_top_ten_coupons(cy_start_date,cy_end_date,lm_start_date,lm_end_date,type,status)
  top_ten_products = get_top_ten_products(cy_start_date,cy_end_date,lm_start_date,lm_end_date,type,status)

  coupons_by_month = get_coupons_by_month(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  items_by_month = get_items_by_month(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  coupons_items = get_coupons_items(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  coupon_vs_customer = get_coupon_vs_customer(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  coupon_vs_report_grouping = get_coupon_vs_report_grouping(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)
  coupon_vs_branch = get_coupon_vs_branch(start_date,end_date,company,report_groupings,customer_ids,coupon_ids,type,status)

  Result.new(success: true,
             total_by_country: total_by_country,
             top_ten_coupons: top_ten_coupons,
             top_ten_products: top_ten_products,
             coupons_by_month: coupons_by_month,
             items_by_month: items_by_month,
             coupons_items: coupons_items,
             coupon_vs_customer: coupon_vs_customer,
             coupon_vs_report_grouping: coupon_vs_report_grouping,
             coupon_vs_branch: coupon_vs_branch,
             start_date: start_date,
             end_date: end_date,
             cy_start_date: cy_start_date,
             cy_end_date: cy_end_date,
             lm_start_date: lm_start_date,
             lm_end_date: lm_end_date)
end