Skip to content

Commit

Permalink
feat: implementing new way to print informations on spreadsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
mewthu2 committed May 2, 2024
1 parent cb94486 commit 29a457e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
43 changes: 27 additions & 16 deletions app/jobs/product_ranking_spreadsheet_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,22 @@ def generate_thirty_days_data(year)
def generate_seven_days_data(year)
product_sales = ProductSale.includes(:product)
.where(year_refference: year, kind: 'seven_days')
.group_by { |sale| [sale.product_id, sale.week_refference] }
.group_by { |sale| sale&.product_id }

workbook = RubyXL::Workbook.new
tab = workbook.worksheets[0]
tab.sheet_name = 'Product Sales Spreadsheet'

header = ['Product Name',
'SKU',
'Fulfillment Channel',
'ASIN1',
'Month',
'Week',
'Units Sold']
header = ['Product Name', 'SKU', 'Fulfillment Channel', 'ASIN1']

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
weeks = (1..5).map { |week| "Week #{week}" }

months.each do |month|
weeks.each do |week|
header << "#{month} #{week}"
end
end

header.each_with_index { |data, col| tab.add_cell(0, col, data) }

Expand All @@ -87,18 +90,26 @@ def generate_seven_days_data(year)
product_sales.each_value do |sales|
product = sales.first.product

monthly_sales = Hash.new { |h, k| h[k] = Hash.new(0) }

sales.each do |sale|
tab.add_cell(row_index, 0, product.item_name)
tab.add_cell(row_index, 1, product.seller_sku)
tab.add_cell(row_index, 2, product.fulfillment_channel)
tab.add_cell(row_index, 3, product.asin1)
month_week = "#{sale.month_refference} #{weeks[sale.week_refference.to_i - 1]}"
monthly_sales[sale.month_refference][month_week] += sale.unit_count.to_i
end

tab.add_cell(row_index, 4, sale.month_refference)
tab.add_cell(row_index, 5, sale.week_refference)
tab.add_cell(row_index, 6, sale.unit_count)
tab.add_cell(row_index, 0, product.item_name)
tab.add_cell(row_index, 1, product.seller_sku)
tab.add_cell(row_index, 2, product.fulfillment_channel)
tab.add_cell(row_index, 3, product.asin1)

row_index += 1
months.each_with_index do |month, month_index|
weeks.each_with_index do |week, week_index|
month_week = "#{month} #{week}"
tab.add_cell(row_index, 4 + (month_index * weeks.length) + week_index, monthly_sales[month][month_week])
end
end

row_index += 1
end

workbook.stream.read
Expand Down
16 changes: 8 additions & 8 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 29a457e

Please sign in to comment.