Skip to content

Commit

Permalink
fix: spreadsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
mewthu2 committed Apr 16, 2024
1 parent 2c2968e commit 0581cd5
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions app/jobs/product_ranking_spreadsheet_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ class ProductRankingSpreadsheetJob < ApplicationJob
queue_as :default

def perform(year)
product_sales = ProductSale.where(year_refference: year)
workbook = RubyXL::Workbook.new
product_sales = ProductSale.includes(:product)
.where(year_refference: year)
.group_by { |sale| sale&.product_id }

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

Expand All @@ -23,23 +25,21 @@ def perform(year)
'November',
'December']

header.each.with_index(0) { |data, row| tab.add_cell(0, row, data) }

product_sales.each.with_index(1) do |product_sale, col|
tab.add_cell(col, 0, product_sale.product.item_name)
tab.add_cell(col, 1, product_sale.product.seller_sku)
tab.add_cell(col, 2, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'January')&.unit_count)
tab.add_cell(col, 3, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'February')&.unit_count)
tab.add_cell(col, 4, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'March')&.unit_count)
tab.add_cell(col, 5, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'April')&.unit_count)
tab.add_cell(col, 6, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'May')&.unit_count)
tab.add_cell(col, 7, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'June')&.unit_count)
tab.add_cell(col, 8, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'July')&.unit_count)
tab.add_cell(col, 9, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'August')&.unit_count)
tab.add_cell(col, 10, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'September')&.unit_count)
tab.add_cell(col, 11, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'October')&.unit_count)
tab.add_cell(col, 12, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'November')&.unit_count)
tab.add_cell(col, 13, ProductSale.find_by(product_id: product_sale.product_id, month_refference: 'December')&.unit_count)
header.each_with_index { |data, row| tab.add_cell(0, row, data) }

row_index = 1

product_sales.each_value do |sales|
product = sales.first.product
tab.add_cell(row_index, 0, product.item_name)
tab.add_cell(row_index, 1, product.seller_sku)

sales.each do |sale|
month_index = Date::MONTHNAMES.index(sale.month_refference)
tab.add_cell(row_index, month_index, sale.unit_count)
end

row_index += 1
end

workbook.stream.read
Expand Down

0 comments on commit 0581cd5

Please sign in to comment.