mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-06 02:51:34 +00:00
Make DB migration generate summary by distributor
This commit is contained in:
@@ -49,6 +49,8 @@ class SimplifyVariantOverrideStockSettings < ActiveRecord::Migration
|
||||
update_on_demand_with_count_on_hand(csv)
|
||||
update_limited_stock_without_count_on_hand(csv)
|
||||
end
|
||||
|
||||
split_csv_by_distributor
|
||||
end
|
||||
|
||||
def down
|
||||
@@ -73,6 +75,10 @@ class SimplifyVariantOverrideStockSettings < ActiveRecord::Migration
|
||||
reports_path.join("changed_variant_overrides.csv")
|
||||
end
|
||||
|
||||
def distributor_csv_path(name, id)
|
||||
reports_path.join("changed_variant_overrides-#{name.parameterize('_')}-#{id}.csv")
|
||||
end
|
||||
|
||||
# When on_demand is nil but count_on_hand is set, force limited stock.
|
||||
def update_use_producer_stock_settings_with_count_on_hand(csv)
|
||||
variant_overrides = VariantOverride.where(on_demand: nil).with_count_on_hand
|
||||
@@ -137,4 +143,23 @@ class SimplifyVariantOverrideStockSettings < ActiveRecord::Migration
|
||||
|
||||
row + [variant_override.on_demand, variant_override.count_on_hand]
|
||||
end
|
||||
|
||||
def split_csv_by_distributor
|
||||
table = CSV.read(csv_path)
|
||||
distributor_ids = table[1..-1].map { |row| row[2] }.uniq # Don't use the header row.
|
||||
|
||||
distributor_ids.each do |distributor_id|
|
||||
distributor_data_rows = filter_data_rows_for_distributor(table[1..-1], distributor_id)
|
||||
distributor_name = distributor_data_rows.first[1]
|
||||
|
||||
CSV.open(distributor_csv_path(distributor_name, distributor_id), "w") do |csv|
|
||||
csv << table[0] # Header row
|
||||
distributor_data_rows.each { |row| csv << row }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def filter_data_rows_for_distributor(data_rows, distributor_id)
|
||||
data_rows.select { |row| row[2] == distributor_id }
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user