Created
July 14, 2013 04:55
-
-
Save maisaengineering/5993264 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 export_profile_data(recipient,profile_ids,season_year,json_template=nil,single=false) | |
require 'csv' | |
filename = "#{Rails.root}/tmp/export_profiles.csv" | |
File.delete(filename) if File.exist?(filename) | |
form_name = json_template.nil? ? 'Profiles' : json_template.form_name | |
season = seasons.find_by(season_year: season_year) | |
predefined_columns = {'Last Name'=>{:univ=>:lname}, 'First Name'=>{:univ=>:fname}, | |
'Preferred Name'=>{:univ=>:nickname}, 'KidsLink ID'=>{:univ=>:kids_id},'Application Date'=>{:univ=>:created_at}, | |
'Application Time'=>{:univ=>:created_at},'Status'=>{:seas=>:status}, 'Session'=>{:seas=>:age_group_and_school_days}, | |
'Birthdate'=>{:univ=>:birthdate}, 'Sex'=>{:univ=>:gender}, | |
'Father First Name'=>{:parent=>:fname},'Father last Name'=>{:parent=>:lname},'Father Phone 1'=>{:parent=>:contact},'Father phone 1 type'=>{:parent=>:type}, | |
'Father Phone 2'=>{:parent=>:contact},'Father phone 2 type'=>{:parent=>:type},'Father Phone 3'=>{:parent=>:contact},'Father phone 3 type'=>{:parent=>:type}, | |
'Father email'=>{:parent=>:email},'Mother First Name'=>{:parent=>:fname},'Mother Last Name'=>{:parent=>:lname},'Mother Phone 1'=>{:parent=>:contact}, | |
'Mother phone 1 type'=>{:parent=>:type},'Mother Phone 2'=>{:parent=>:contact},'Mother phone 2 type'=>{:parent=>:type},'Mother Phone 3'=>{:parent=>:contact}, | |
'Mother phone 3 type'=>{:parent=>:type},'Mother email'=>{:parent=>:email},'Address Line 1'=>{:univ=>:address1},'Address Line 2'=>{:univ=>:address2}, | |
'City'=>{:univ=>:city},'State'=>{:univ=>:state},'Zip'=>{:univ=>:zip},'Food allergies'=>{:univ=>:food_allergies},'Medical issues'=>{:univ=>:medical_issues}, | |
'Special needs'=>{:univ=>:special_needs},'Other concerns'=>{:univ=>:other_concerns},'Are you enrolling siblings'=>{:seas=>:are_you_enrolling_siblings}, | |
"Sibling's name"=>{:seas=>:sibling_name},"Sibling's age group"=>{:seas=>:sibling_age},"Sibling's day"=>{:seas=>:sibling_days},'Family currently enrolled?'=>{:seas=>:family_currently_enrolled}, | |
'Active members of Peachtree Presbyterian Church?'=>{:seas=>:active_member_of_ppc},'Admission agreement signature date'=>{:seas=>:ack_date}, | |
'Admission agreement signature user'=>{:seas=>:ack_signature},'Coupon code'=>{:seas=>:coupon_code},'Registration fee'=>{:seas=>:registration_fee}} | |
pre_univ_keys,pre_org_keys,pre_seas_keys =[] ,[],[] | |
#raise predefined_columns.inspect | |
predefined_columns.values.each do |v| | |
pre_univ_keys << v[:univ] unless v[:univ].nil? | |
pre_org_keys << v[:org] unless v[:org].nil? | |
pre_seas_keys << v[:seas] unless v[:seas].nil? | |
end | |
univ_columns,univ_keys = [],[] | |
org_columns,org_keys = [],[] | |
seas_columns,seas_keys = [],[] | |
application_template = season.json_templates.where(workflow: JsonTemplate::WORKFLOW_TYPES[:appl_form]).first | |
univ_keys_and_labels = application_template.keys_and_labels('univ') | |
org_keys_and_labels = application_template.keys_and_labels('org') | |
seas_keys_and_labels = application_template.keys_and_labels('seas') | |
univ_keys_and_labels.delete_if{|k,v| pre_univ_keys.include?(k.to_sym)} | |
org_keys_and_labels.delete_if{|k,v| pre_org_keys.include?(k.to_sym)} | |
seas_keys_and_labels.delete_if{|k,v| pre_seas_keys.include?(k.to_sym)} | |
univ_keys += univ_keys_and_labels.keys | |
univ_columns += univ_keys_and_labels.values | |
org_keys += org_keys_and_labels.keys | |
org_columns += org_keys_and_labels.values | |
seas_keys += seas_keys_and_labels.keys | |
seas_columns += seas_keys_and_labels.values | |
ack_date,ack_sig = [],[] | |
if json_template and json_template.id != application_template.id | |
univ_columns += json_template.keys_and_labels('univ').values | |
univ_keys += json_template.keys_and_labels('univ').keys | |
org_columns += json_template.keys_and_labels('org').values | |
org_keys += json_template.keys_and_labels('org').keys | |
seas_columns += json_template.keys_and_labels('seas').values | |
seas_keys += json_template.keys_and_labels('seas').keys | |
else | |
# for single profile export get only forms of belonging profile else get all the forms of season of organization | |
if single == true | |
json_template_ids = OrganizationMembership.where(profile_id: profile_ids.first,org_id: self.id).first.notifications.where(:json_template_id.ne => application_template.id,:json_template_id.exists=>true).map(&:json_template_id) | |
json_templates = JsonTemplate.in(id: json_template_ids) | |
else | |
json_templates = season.json_templates.where(:workflow.ne=>JsonTemplate::WORKFLOW_TYPES[:appl_form]) | |
end | |
json_templates.each_with_index do |json_template,index| | |
univ_columns += json_template.keys_and_labels('univ').values | |
univ_keys += json_template.keys_and_labels('univ').keys | |
org_columns += json_template.keys_and_labels('org').values | |
org_keys += json_template.keys_and_labels('org').keys | |
seas_columns += json_template.keys_and_labels('seas').values | |
seas_keys += json_template.keys_and_labels('seas').keys | |
ack_date << "#{index} - hello" | |
ack_sig << "#{index} - sig" | |
end | |
end | |
#for individual profile export | |
if single == true | |
csv_string = CSV.generate do |csv| | |
csv << predefined_columns.keys + univ_columns + org_columns + seas_columns | |
Profile.where(:id.in=>profile_ids).each do |profile| | |
csv << export_individual_profile_data(profile,application_template,season.season_year,univ_keys,org_keys,seas_keys,ack_date,ack_sig) | |
end | |
end | |
return csv_string | |
else | |
csv_string = CSV.open(filename,'w') do |csv| | |
csv << predefined_columns.keys + univ_columns + org_columns + seas_columns | |
Profile.where(:id.in=>profile_ids).each do |profile| | |
csv << export_individual_profile_data(profile,application_template,season.season_year,univ_keys,org_keys,seas_keys,ack_date,ack_sig) | |
end | |
end | |
Notifier.delay.export_form_details(recipient,name,form_name,season_year) | |
end | |
end | |
def export_individual_profile_data(profile,application_template,season_year,univ_keys,org_keys,seas_keys,ack_date,ack_sig) | |
organization_membership = profile.organization_membership(id) | |
applied_season = organization_membership.seasons.find_by(season_year: season_year) | |
kids_type = profile.kids_type | |
#grab notification for application form to fill acknowledgment details | |
notification = Notification.where(organization_membership_id: organization_membership.id,season_id: applied_season.id,json_template_id: application_template.id).first | |
predefined_values= predefined_profile_data(profile,notification,organization_membership,applied_season) | |
univ_values = kids_type.attributes.values_at(*univ_keys) | |
org_values = organization_membership.attributes.values_at(*org_keys) | |
seas_values = applied_season.attributes.values_at(*seas_keys) | |
duplicate_with_indexes = seas_keys.each_with_index.group_by(&:first).inject({}) do |result, (val,group)| | |
next result if group.length == 1 | |
result.merge val => group.map {|pair| pair[1]} | |
end | |
duplicate_with_indexes["ack_date"].each_with_index do |v,i| | |
seas_values[v] = ack_date[i] | |
end | |
duplicate_with_indexes["ack_signature"].each_with_index do |v,i| | |
seas_values[v] = ack_sig[i] | |
end | |
predefined_values + univ_values + org_values + seas_values | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment