daily pastebin goal
75%
SHARE
TWEET

Untitled

a guest Nov 8th, 2018 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ActiveRecord::Base.transaction do
  2.   facilities = Facility.where(id: [64,196,200,257,4119,331,285])
  3.   tracker_array = []
  4.  
  5.   facilities.each do |facility|
  6.     tracker_hash = {facility_id: facility.id}
  7.     account_balance_ids = facility.account_balances.joins(:general_ledger_line_items).where("#{GeneralLedgerLineItem.quoted_table_name}.internal_account_code IN ('1103','4150') AND #{GeneralLedgerLineItem.quoted_table_name}.ledger_id IS NULL").uniq.pluck(:id)
  8.  
  9.     gllis = GeneralLedgerLineItem.where(account_balance_id: account_balance_ids).to_a
  10.     ar_service_gllis = gllis.select { |glli| glli.internal_account_code == "1103"}
  11.     service_revenue_gllis = gllis.select { |glli| glli.internal_account_code == "4150"}
  12.  
  13.     puts "Offsetting A/R Services GLLIs for Facility Store Number #{facility.store_number}"
  14.  
  15.     amount = ar_service_gllis.sum(&:amount)
  16.     if amount != 0
  17.       ar_glli = ar_service_gllis.first.dup
  18.       ar_glli.effective_on = Date.today.in_time_zone(facility.time_zone)
  19.       ar_glli.amount = -amount
  20.       ar_glli.transaction_type = "gl_adjustment"
  21.       if ar_glli.save
  22.         tracker_hash[:ar_glli_id] = ar_glli.id
  23.         ar_service_gllis << ar_glli
  24.       else
  25.         raise "Couldn't offset A/R Services for AB #{ab.id}"
  26.       end
  27.  
  28.       revenue_glli = service_revenue_gllis.first.dup
  29.       revenue_glli.effective_on = Date.today.in_time_zone(facility.time_zone)
  30.       revenue_glli.amount = amount
  31.       revenue_glli.transaction_type = "gl_adjustment"
  32.      
  33.       raise "Couldn't offset A/R revenue for AB #{ab.id}" unless revenue_glli.save
  34.       tracker_hash[:revenue_glli_id] = revenue_glli.id
  35.       tracker_array << tracker_hash
  36.     else
  37.       raise "Amount #{amount} for account balance #{ab.id} in facility #{facility.id} is fine"
  38.     end
  39.  
  40.     ar_amount = ar_service_gllis.sum(&:amount)
  41.     raise "Oustanding AR in the amount of #{ar_amount} for facility #{facility.id}" unless ar_amount == 0
  42.     puts "Phantom A/R Services for facility #{facility.id} GOT BLASTED"
  43.   end
  44.  
  45.   puts "Created GLLIs:"
  46.   puts tracker_array
  47. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top