Guest User

Untitled

a guest
Apr 25th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. begin
  2. # Taken from SM/icore_service.rb
  3. # This is made in order to run save_all_invoices with start_date of previous month without creating reservations
  4. # Ticket: jira.we.co/browse/BIT-603
  5.  
  6. def enabled_invoicing_accounts(icore_connection)
  7. total = icore_connection.member_invoicing_accounts
  8. enabled = total.select { |m| m['AccountStatus'] == 'Enabled' }
  9. Rails.logger.info("[iCore] found member accounts: total=#{total.count} enabled=#{enabled.count}")
  10. enabled
  11. end
  12.  
  13. def all_invoices(icore_connection,start_date, mias)
  14. icore_account_id2short_code = mias.map { |mia| [mia['InvoiceUserID'], [mia['MemberID'], mia['LocationUID']]] }
  15. icore_account_id2short_code = Hash[icore_account_id2short_code]
  16.  
  17. icore_invoices = icore_account_id2short_code.map do |icore_account_id, v|
  18. short_code, location_code = v
  19. begin
  20. invoices = icore_connection.invoices_by_icore_account(icore_account_id, start_date)
  21. Rails.logger.info("[iCore] found invoices by account: invoices=#{invoices.count} icore_account_id=#{icore_account_id} short_code=#{short_code} location_code=#{location_code}")
  22. [icore_account_id, invoices]
  23. rescue Exception => e
  24. IcoreService.log_error(e)
  25. Rails.logger.info("[iCore] couldn't get invoices by account: icore_account_id=#{icore_account_id} short_code=#{short_code} location_code=#{location_code}")
  26. nil
  27. end
  28. end
  29.  
  30. icore_invoices = Hash[icore_invoices.select(&:present?)]
  31. Rails.logger.info("[iCore] found #{icore_invoices.count} invoices")
  32.  
  33. icore_invoices.flat_map do |icore_account_id, invoices|
  34. short_code, location = icore_account_id2short_code[icore_account_id]
  35. invoices.map do |invoice|
  36. {
  37. icore_id: invoice['InvoiceID'],
  38. icore_account_id: icore_account_id,
  39. wework_location_code: location,
  40. wework_short_code: short_code,
  41. icore_invoice_date: Date.parse(invoice['InvoiceDate']),
  42. icore_usage: invoice['Usage'].to_d,
  43. icore_total: invoice['Total'].to_d,
  44. icore_tax: invoice['Tax'].to_d,
  45. icore_charge: invoice['Charge'].to_d,
  46. icore_invoice_url: invoice['InvoiceURL']
  47. }
  48. end
  49. end
  50. end
  51.  
  52. def save_invoice(icore_connection, invoice_hash)
  53. id = invoice_hash[:icore_id]
  54. if invoice_hash[:icore_invoice_url].blank?
  55. Rails.logger.info("invoice url NOT populated, making InvoiceDetailUrl call")
  56. url = safe_url(icore_connection, id)
  57. invoice_hash[:icore_invoice_url] = url if url
  58. else
  59. Rails.logger.info("invoice url already populated, skipping InvoiceDetailUrl call")
  60. end
  61.  
  62. invoice = IcoreInvoice.find_or_create_by(icore_id: id) do |i|
  63. i.update_attributes(invoice_hash)
  64. end
  65.  
  66. invoice
  67. end
  68.  
  69. def safe_url(icore_connection, icore_id)
  70. urls = icore_connection.invoice_url(icore_id)
  71. urls.count == 1 ? urls.first['InvoiceURL'] : nil
  72. rescue Exception => e
  73. IcoreService.log_error(e)
  74. nil
  75. end
  76.  
  77. icore_connection = IcoreConnection.new
  78. start_date = TimeProvider.today.prev_month.beginning_of_month
  79.  
  80. icore_accounts = enabled_invoicing_accounts(icore_connection)
  81. icore_invoices = all_invoices(icore_connection, start_date, icore_accounts)
  82.  
  83. icore_invoices.each do |invoice|
  84. begin
  85. account = Account.find_by(short_code: invoice[:wework_short_code])
  86.  
  87. raise "Tried creating IcoreInvoice for an account that does not exist: #{invoice[:wework_short_code]}" if account.nil?
  88.  
  89. raise "Tried creating IcoreInvoice for an inactive account: #{account}" unless account.active?
  90.  
  91. location = Location.find_by(code: invoice[:wework_location_code])
  92.  
  93. raise "Location info is weird so we couldn't switch to the correct tenant for #{invoice[:wework_location_code]} for account #{invoice[:wework_short_code]}" if location.nil?
  94.  
  95. TenantService.switch_tenant_with_location(location)
  96.  
  97. icore_invoice = save_invoice(icore_connection, invoice)
  98.  
  99. #no reservation saving:
  100. # reservation = create_reservation_from_icore_invoice(icore_invoice)
  101. rescue Exception => e
  102. IcoreService.log_error(e)
  103. end
  104. end
  105. end
Add Comment
Please, Sign In to add comment