Class: Report::CouponSalesReport::CouponSalesReport
- Inherits:
-
Object
- Object
- Report::CouponSalesReport::CouponSalesReport
- Defined in:
- app/services/report/coupon_sales_report/coupon_sales_report.rb
Overview
Service object: coupon sales report.
Defined Under Namespace
Classes: Result
Class Method Summary collapse
-
.get_coupon_vs_branch(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get coupon vs branch.
-
.get_coupon_vs_customer(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get coupon vs customer.
-
.get_coupon_vs_report_grouping(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get coupon vs report grouping.
-
.get_coupons_by_month(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get coupons by month.
-
.get_coupons_items(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get coupons items.
-
.get_items_by_month(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get items by month.
-
.get_top_ten_coupons(cy_start_date, cy_end_date, lm_start_date, lm_end_date, type, status) ⇒ Object
Get top ten coupons.
-
.get_top_ten_products(cy_start_date, cy_end_date, lm_start_date, lm_end_date, type, status) ⇒ Object
Get top ten products.
-
.get_total_by_country(cy_start_date, cy_end_date, lm_start_date, lm_end_date, type, status) ⇒ Object
Get total by country.
-
.result_report(options = {}) ⇒ Result
Coupon sales data.
Class Method Details
.get_coupon_vs_branch(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get coupon vs branch.
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 534 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| ActiveRecord::Base.lease_connection.quote(rg) }.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| ActiveRecord::Base.lease_connection.quote(rg) }.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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_coupon_vs_customer(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get coupon vs customer.
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 431 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| ActiveRecord::Base.lease_connection.quote(rg) }.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| ActiveRecord::Base.lease_connection.quote(rg) }.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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_coupon_vs_report_grouping(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get coupon vs report grouping.
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 483 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| ActiveRecord::Base.lease_connection.quote(rg) }.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| ActiveRecord::Base.lease_connection.quote(rg) }.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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_coupons_by_month(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get coupons by month.
281 282 283 284 285 286 287 288 289 290 291 292 293 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 |
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 281 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| ActiveRecord::Base.lease_connection.quote(rg) }.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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_coupons_items(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get coupons items.
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 388 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| ActiveRecord::Base.lease_connection.quote(rg) }.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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_items_by_month(start_date, end_date, company, report_groupings, customer_ids, coupon_ids, type, status) ⇒ Object
Get items by month.
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 368 369 370 371 372 373 374 375 376 |
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 330 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| ActiveRecord::Base.lease_connection.quote(rg) }.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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_top_ten_coupons(cy_start_date, cy_end_date, lm_start_date, lm_end_date, type, status) ⇒ Object
Get top ten coupons.
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 152 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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_top_ten_products(cy_start_date, cy_end_date, lm_start_date, lm_end_date, type, status) ⇒ Object
Get top ten products.
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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 215 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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.get_total_by_country(cy_start_date, cy_end_date, lm_start_date, lm_end_date, type, status) ⇒ Object
Get total by country.
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 |
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 100 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.squish 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 ActiveRecord::Base.lease_connection.execute(sql).to_a.map(&:symbolize_keys) end |
.result_report(options = {}) ⇒ Result
Returns coupon sales data.
44 45 46 47 48 49 50 51 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 |
# File 'app/services/report/coupon_sales_report/coupon_sales_report.rb', line 44 def self.result_report( = {}) start_date = [:period1_gteq] end_date = [:period1_lteq] type = [:type] status = [:status] company = if [:company_ids].present? [:company_ids] == 'USA' ? 1 : 2 else 0 end report_groupings = [:report_groupings].filter_map(&:presence) customer_ids = [:customer_ids].filter_map(&:presence) coupon_ids = [:coupon_ids].filter_map(&:presence) 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 |