Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- acceptable_service_account_balances = ["Imported Recurring Balance", "Imported Service Balance"]
- # TODO: Go back and fix whatever's left on these guys
- special_payment_reapply = [] # Already had the payee ar account code swapped, but need the A/R Rent credit fixed
- extra_payment_reapply = [] # Looks like these may have been split on imported A/R's? Need to double check
- 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)
- non_ar_rent_glli = []
- count = 0
- facility_count = 0
- ActiveRecord::Base.transaction do
- Facility.reformed.each do |facility|
- facility_count += 1
- puts "--------------- Updating #{facility.name} (#{facility_count} / #{Facility.reformed.count}) ---------------"
- 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')
- count += service_gllis.count
- service_gllis.each do |glli|
- account_balance = glli.account_balance
- ledger = account_balance.ledger
- puts "Updating #{ledger.tenant_name}"
- # Filter out the bad ones
- amount_on_day = ledger.gllis.where(internal_account_code: "1103").where('effective_on::date = ?', glli.effective_on.to_date).sum(:amount)
- if glli.amount != amount_on_day
- # This is a bad glli that's been offset already (large A/R Service credit from reformation that was later offset -- Thanks Donald)
- if ledger.gllis.where(internal_account_code: "1103").where('effective_on::date = ?', glli.effective_on.to_date).where('amount < 0').any?
- puts "Skipping GLLI of amount #{glli.amount}, the day should only be #{amount_on_day}"
- next
- end
- end
- # If the account balance we're on doesn't seem like the right one, let's see if there's a better one
- unless account_balance.description.in?(acceptable_service_account_balances)
- if better_ab = ledger.account_balances.that_are_payable.find_by(description: acceptable_service_account_balances)
- if account_balance.payers.none? && better_ab.payers.none?
- # this is good, and handles the majority case
- else
- # this is kinda crappy
- # TODO: Deal with these that are resolved in a later migration
- skippped_swapping_account_balances << "Ledger: #{ledger.id}, AB: #{account_balance.id}"
- next
- end
- puts "--------------------------------------------------------------------------------"
- puts "Swapping glli from #{account_balance.description} to #{better_ab.description}"
- puts "--------------------------------------------------------------------------------"
- # Swippity swap
- glli.update account_balance: better_ab
- account_balance = better_ab
- end
- end
- # Update the accountable's internal_accounts_receivable_account_code
- if account_balance.payment_amount?
- # These should all be resolved when we go through and fix their payees, we shouldn't have to do anything with them
- next
- else
- account_balance.accountable.update internal_accounts_receivable_account_code: DefaultAccountCodes.internal_code_by_name(:accounts_receivable_service)
- end
- # 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
- account_balance.payers.each do |payer|
- puts "Got a payer for #{account_balance.tenant.name}"
- gllis = account_balance.gllis.where('effective_on::date = ?', payer.resolved_on.to_date)
- if gllis.count != 1
- # Generally if a credit was given for the Imported Recurring/Service/Other, let's fix it later
- special_payment_reapply << "Ledger: #{ledger.id}, AB: #{account_balance.id}"
- next
- end
- glli = gllis.first
- if glli.internal_account_code == '1101'
- # We got some A/R, gonna put the thang down flip it and reverse it (and make it a service)
- new_glli = glli.dup
- new_glli.internal_account_code = DefaultAccountCodes.internal_code_by_name(:accounts_receivable_service)
- new_glli.save!
- glli.offset!
- else
- non_ar_rent_glli << "Ledger: #{ledger.id}, AB: #{account_balance.id}" unless glli.internal_account_code == '1103'
- end
- end
- ar_service = account_balance.gllis.where(internal_account_code: '1103').sum(:amount)
- extra_payment_reapply << "Ledger: #{ledger.id}, AB: #{account_balance.id}" if account_balance.amount_leftover != ar_service
- end
- end
- end
- completed = (count - (extra_payment_reapply.count + special_payment_reapply.count + skippped_swapping_account_balances.count)) / count.to_d * 100
- puts "We fixed #{completed.round(2)}%"
- puts "--------------------"
- puts "Specal"
- puts special_payment_reapply
- puts "--------------------"
- puts "Skipped cause swap"
- puts skippped_swapping_account_balances
- puts "--------------------"
- puts "May need extra care"
- puts extra_payment_reapply
- puts "--------------------"
- puts "Any non rent A/R?"
- puts non_ar_rent_glli
- puts 'done'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement