Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- report = []
- headers = %w(client portfolio_id opportunity_id fund_id donor_disbursement_field_added_to_fund)
- Client.on_each do |client|
- next unless client.engage_only?
- post_acceptance_opportunities = Opportunity::PostAcceptance.not_archived.all
- post_acceptance_opportunities.each do |opportunity|
- portfolio = opportunity.portfolio
- fund = portfolio.fund
- # get the form_field id's from questions on the opportunity
- opportunity_form_field_ids = opportunity.questions.pluck(:form_field_id)
- # get the form field id's form disbursement fields on the fund
- donor_disbursement_field_ids = fund.disbursement_fields_funds.pluck(:disbursement_field_id)
- donor_disbursement_form_field_ids = donor_disbursement_field_ids.collect { |id| Donor::Disbursement::Field.find(id).form_field.id }
- # get the difference, if any, between the form_fields on the opportunity and those on the fund
- difference_in_form_field_ids = opportunity_form_field_ids - donor_disbursement_form_field_ids
- next if difference_in_form_field_ids.none?
- donor_disbursement_fields_to_add_to_fund = []
- # gather disbursement fields that need to be added to the fund. Create a report.
- difference_in_form_field_ids.each do |id|
- row = []
- existing_donor_disbursement_field = Donor::Disbursement::Field.find_by(import_header: "form_field_#{id}")
- row << client
- row << portfolio.id
- row << opportunity.id
- row << fund.id
- # collect existing disbursement fields
- if existing_donor_disbursement_field
- donor_disbursement_fields_to_add_to_fund << existing_donor_disbursement_field
- row << existing_donor_disbursement_field.id
- end
- report << row
- end
- # add donor disbursement fields from opportunity, that aren't on the fund, to the disb_fields_funds table so they show up on the fund.
- donor_disbursement_fields_to_add_to_fund.each do |donor_disbursement_field|
- donor_disbursement_fields_fund = Donor::DisbursementFieldsFund.new(
- fund_id: fund.id,
- disbursement_field_id: donor_disbursement_field.id,
- contact_visible: donor_disbursement_field.available_on_funds,
- )
- donor_disbursement_fields_fund.save!
- end
- end
- # upload report to each clients files
- report.easy_csv("disbursement_fields_sync", headers, {direct_upload: true})
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement