Created
November 24, 2010 23:05
-
-
Save ltw/714600 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.generate_planned_simulation(model_package_id, start_date, finish_date, increment) | |
start_date = Date.strptime(start_date,'%B %d, %Y') | |
finish_date = Date.strptime(finish_date, '%B %d, %Y') | |
dates = get_dates_with_interval(start_date, finish_date, increment) | |
activities = Activity.all( | |
:conditions => [ | |
'(start_date <= :finish_date OR finish_date <= :finish_date) AND model_package_id = :model_package_id', | |
{ :finish_date => finish_date, :model_package_id => model_package_id } | |
], | |
:include => [:equipment_items, :work_status] | |
) | |
simulation_data = Array.new | |
last_date = activities.map{|a| [a.start_date, a.finish_date] }.flatten.compact.uniq.sort.first || Date.parse("1970-01-01") | |
dates.each do |d| | |
date_data = Hash.new | |
cad_ids_collection = [] | |
interval_dates = ((last_date + 1)..d).to_a.reverse | |
interval_dates.each do |date| | |
segmented_activities = activities.find_all{|a| (a.start_date == date) or (a.finish_date == date) } | |
segmented_activities.each do |a| | |
equipment_item_ids = a.equipment_items.map{|ei| | |
ei.cad_id unless cad_ids_collection.include?(ei.cad_id) | |
}.compact | |
cad_ids_collection += equipment_item_ids | |
if a.finish_date == date | |
suffix = "_finish" | |
else | |
suffix = "_start" | |
end | |
name = a.work_status.id.to_s + suffix | |
date_data[name] ||= [] | |
date_data[name] += equipment_item_ids.to_a | |
end | |
end | |
unless date_data.size == 0 | |
date_data.delete_if{|key, value| value.blank? } | |
simulation_data << {:date => d, :materials => date_data} | |
end | |
last_date = d | |
end | |
simulation_data.delete_if{|e| e[:materials].blank? } | |
return simulation_data | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment