Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. report = []
  2.  
  3. headers = %w(client portfolio_id opportunity_id fund_id donor_disbursement_field_added_to_fund)
  4.  
  5. Client.on_each do |client|
  6.  
  7. next unless client.engage_only?
  8.  
  9. post_acceptance_opportunities = Opportunity::PostAcceptance.not_archived.all
  10.  
  11. post_acceptance_opportunities.each do |opportunity|
  12. portfolio = opportunity.portfolio
  13. fund = portfolio.fund
  14.  
  15. # get the form_field id's from questions on the opportunity
  16. opportunity_form_field_ids = opportunity.questions.pluck(:form_field_id)
  17.  
  18. # get the form field id's form disbursement fields on the fund
  19. donor_disbursement_field_ids = fund.disbursement_fields_funds.pluck(:disbursement_field_id)
  20. donor_disbursement_form_field_ids = donor_disbursement_field_ids.collect { |id| Donor::Disbursement::Field.find(id).form_field.id }
  21.  
  22. # get the difference, if any, between the form_fields on the opportunity and those on the fund
  23. difference_in_form_field_ids = opportunity_form_field_ids - donor_disbursement_form_field_ids
  24.  
  25. next if difference_in_form_field_ids.none?
  26.  
  27. donor_disbursement_fields_to_add_to_fund = []
  28.  
  29. # gather disbursement fields that need to be added to the fund. Create a report.
  30. difference_in_form_field_ids.each do |id|
  31. row = []
  32. existing_donor_disbursement_field = Donor::Disbursement::Field.find_by(import_header: "form_field_#{id}")
  33.  
  34. row << client
  35. row << portfolio.id
  36. row << opportunity.id
  37. row << fund.id
  38.  
  39. # collect existing disbursement fields
  40. if existing_donor_disbursement_field
  41. donor_disbursement_fields_to_add_to_fund << existing_donor_disbursement_field
  42. row << existing_donor_disbursement_field.id
  43. end
  44.  
  45. report << row
  46. end
  47.  
  48. # 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.
  49. donor_disbursement_fields_to_add_to_fund.each do |donor_disbursement_field|
  50. donor_disbursement_fields_fund = Donor::DisbursementFieldsFund.new(
  51. fund_id: fund.id,
  52. disbursement_field_id: donor_disbursement_field.id,
  53. contact_visible: donor_disbursement_field.available_on_funds,
  54. )
  55.  
  56. donor_disbursement_fields_fund.save!
  57. end
  58. end
  59.  
  60. # upload report to each clients files
  61. report.easy_csv("disbursement_fields_sync", headers, {direct_upload: true})
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement