Skip to content

Instantly share code, notes, and snippets.

@sudheeshcm
Created March 17, 2017 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudheeshcm/e804fcf3ae87348f1baee5b4b5e3120b to your computer and use it in GitHub Desktop.
Save sudheeshcm/e804fcf3ae87348f1baee5b4b5e3120b to your computer and use it in GitHub Desktop.
Dias <3 ruby
module Rfid::Exporter
class SalesStart < Rfid::ExporterBase
FILE_PREFIX = "sku_sales_period"
FILE_EXTENSION = "csv"
ROWS_PER_FILE = 300_000
AVAILABLE_STORE_TYPE = %i(flagship superlarge large standard small)
STORE_TYPES = {
flagship: [11],
superlarge: [12],
large: [13],
standard: [15,17],
small: [18]
}
def generate!
logger.info "Start process"
business_unit_name = BusinessUnitMaster.name_by_code(business_unit_code)
logger.info "File prefix will be #{export_file_prefix}"
logger.info "Export data for #{business_unit_name}"
@row = {}
fetch_recently_updated_items
export_items.each_slice(100) do |item_group|
item_ids = item_group.map(&:first)
items = ItemMaster.gu.where(ITM_CD: item_ids).index_by(&:ITM_CD)
skus_for_items = skus(item_ids)
item_group.each do |item_cd, sales_date_by_color|
next unless item = items[item_cd]
next unless sku_for_item = skus_for_items[item_cd]
assign_sku_to_colors!(sales_date_by_color, sku_for_item)
export_by_each_color(item, sales_date_by_color)
end
end
page_writer.close
logger.info "end process"
end
private
def export_items
@export_items ||= []
end
def add_export_item(item, type)
@export_items ||= {}
@export_items[item.item_cd] ||= {}
@export_items[item.item_cd][item.color_cd] ||= {}
AVAILABLE_STORE_TYPE.each do |store_type|
col_name = "DATE_#{store_type.upcase}".to_sym
next unless dt = item.read_attribute(col_name)
@export_items[item.item_cd][item.color_cd][store_type] ||= {}
@export_items[item.item_cd][item.color_cd][store_type][type] = dt.strftime("%Y/%m/%d") if dt
end
end
def fetch_recently_updated_items
raise "business_unit_code is missing" if business_unit_code.nil?
start_date_time_of_diff = @execution_date - 1.day
[::NewItemStart, ::ItemStockStart, ::ResaleStart].each do |item_type|
item_type.business_unit_code(business_unit_code).updated_after(start_date_time_of_diff).each do |item|
add_export_item(item, item_type)
end
end
end
def assign_sku_to_colors!(color_items, skus)
colors = color_items.keys
colors.delete("")
skus.each do |color_cd, sku|
if color = colors.detect{|color| color_cd.include?(color)}
color_items[color][:sku] ||= []
color_items[color][:sku] << sku.sku_cd
elsif color_items.key?("")
color_items[""][:sku] ||= []
color_items[""][:sku] << sku.sku_cd
end
end
end
def skus(item_ids)
sku_master = {}
SkuMaster.gu.where(ITM_CD: item_ids).each do |sku|
sku_master[sku.item_cd] ||= {}
sku_master[sku.item_cd][sku.color_cd] = sku
end
sku_master
end
def export_by_each_color(item, sales_date_by_color)
sales_date_by_color.each do |color_cd, sale_date_by_store_type|
AVAILABLE_STORE_TYPE.each do |store_type|
export_by_each_sku(item, sale_date_by_store_type, store_type)
end
end
end
def export_by_each_sku(item, sale_date_by_store_type, store_type)
return unless sales_dates = sale_date_by_store_type[store_type]
return unless sale_date_by_store_type[:sku]
sale_date_by_store_type[:sku].each do |sku|
STORE_TYPES[store_type].each do |storelineup|
page_writer.puts [
1,
sku,
item.department_code,
storelineup,
sales_dates[:new_item],
sales_dates[:stock_item],
sales_dates[:resale_item]
]
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment