Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. acceptable_service_account_balances = ["Imported Recurring Balance", "Imported Service Balance"]
  2.  
  3. # TODO: Go back and fix whatever's left on these guys
  4. special_payment_reapply = [] # Already had the payee ar account code swapped, but need the A/R Rent credit fixed
  5. extra_payment_reapply = [] # Looks like these may have been split on imported A/R's? Need to double check
  6. skippped_swapping_account_balances = [] # These need to be swapped either to or from resolved ABs, need to make sure the right thing happens (19 of em)
  7. non_ar_rent_glli = []
  8.  
  9. count = 0
  10.  
  11. facility_count = 0
  12. ActiveRecord::Base.transaction do
  13. Facility.reformed.each do |facility|
  14. facility_count += 1
  15. puts "--------------- Updating #{facility.name} (#{facility_count} / #{Facility.reformed.count}) ---------------"
  16. service_gllis = GeneralLedgerLineItem.where(facility_id: facility.id, internal_account_code: "1103").where("effective_on::date = ?", facility.import_facility.reformation_started_at.to_date - 1.day).where('amount > 0')
  17. count += service_gllis.count
  18.  
  19. service_gllis.each do |glli|
  20. account_balance = glli.account_balance
  21. ledger = account_balance.ledger
  22. puts "Updating #{ledger.tenant_name}"
  23.  
  24. # Filter out the bad ones
  25. amount_on_day = ledger.gllis.where(internal_account_code: "1103").where('effective_on::date = ?', glli.effective_on.to_date).sum(:amount)
  26. if glli.amount != amount_on_day
  27. # This is a bad glli that's been offset already (large A/R Service credit from reformation that was later offset -- Thanks Donald)
  28. if ledger.gllis.where(internal_account_code: "1103").where('effective_on::date = ?', glli.effective_on.to_date).where('amount < 0').any?
  29. puts "Skipping GLLI of amount #{glli.amount}, the day should only be #{amount_on_day}"
  30. next
  31. end
  32. end
  33.  
  34. # If the account balance we're on doesn't seem like the right one, let's see if there's a better one
  35. unless account_balance.description.in?(acceptable_service_account_balances)
  36. if better_ab = ledger.account_balances.that_are_payable.find_by(description: acceptable_service_account_balances)
  37. if account_balance.payers.none? && better_ab.payers.none?
  38. # this is good, and handles the majority case
  39. else
  40. # this is kinda crappy
  41. # TODO: Deal with these that are resolved in a later migration
  42. skippped_swapping_account_balances << "Ledger: #{ledger.id}, AB: #{account_balance.id}"
  43. next
  44. end
  45.  
  46. puts "--------------------------------------------------------------------------------"
  47. puts "Swapping glli from #{account_balance.description} to #{better_ab.description}"
  48. puts "--------------------------------------------------------------------------------"
  49.  
  50. # Swippity swap
  51. glli.update account_balance: better_ab
  52. account_balance = better_ab
  53. end
  54. end
  55.  
  56.  
  57. # Update the accountable's internal_accounts_receivable_account_code
  58. if account_balance.payment_amount?
  59. # These should all be resolved when we go through and fix their payees, we shouldn't have to do anything with them
  60. next
  61. else
  62. account_balance.accountable.update internal_accounts_receivable_account_code: DefaultAccountCodes.internal_code_by_name(:accounts_receivable_service)
  63. end
  64.  
  65. # If they've been paid at all, we gotta go find the A/R Rent credit that was written and swap it with A/R Service
  66. account_balance.payers.each do |payer|
  67. puts "Got a payer for #{account_balance.tenant.name}"
  68. gllis = account_balance.gllis.where('effective_on::date = ?', payer.resolved_on.to_date)
  69. if gllis.count != 1
  70. # Generally if a credit was given for the Imported Recurring/Service/Other, let's fix it later
  71. special_payment_reapply << "Ledger: #{ledger.id}, AB: #{account_balance.id}"
  72. next
  73. end
  74.  
  75. glli = gllis.first
  76. if glli.internal_account_code == '1101'
  77. # We got some A/R, gonna put the thang down flip it and reverse it (and make it a service)
  78. new_glli = glli.dup
  79. new_glli.internal_account_code = DefaultAccountCodes.internal_code_by_name(:accounts_receivable_service)
  80. new_glli.save!
  81. glli.offset!
  82. else
  83. non_ar_rent_glli << "Ledger: #{ledger.id}, AB: #{account_balance.id}" unless glli.internal_account_code == '1103'
  84. end
  85. end
  86. ar_service = account_balance.gllis.where(internal_account_code: '1103').sum(:amount)
  87. extra_payment_reapply << "Ledger: #{ledger.id}, AB: #{account_balance.id}" if account_balance.amount_leftover != ar_service
  88. end
  89. end
  90. end
  91.  
  92. completed = (count - (extra_payment_reapply.count + special_payment_reapply.count + skippped_swapping_account_balances.count)) / count.to_d * 100
  93. puts "We fixed #{completed.round(2)}%"
  94. puts "--------------------"
  95. puts "Specal"
  96. puts special_payment_reapply
  97. puts "--------------------"
  98. puts "Skipped cause swap"
  99. puts skippped_swapping_account_balances
  100. puts "--------------------"
  101. puts "May need extra care"
  102. puts extra_payment_reapply
  103. puts "--------------------"
  104. puts "Any non rent A/R?"
  105. puts non_ar_rent_glli
  106. puts 'done'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement