Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- begin
- # Taken from SM/icore_service.rb
- # This is made in order to run save_all_invoices with start_date of previous month without creating reservations
- # Ticket: jira.we.co/browse/BIT-603
- def enabled_invoicing_accounts(icore_connection)
- total = icore_connection.member_invoicing_accounts
- enabled = total.select { |m| m['AccountStatus'] == 'Enabled' }
- Rails.logger.info("[iCore] found member accounts: total=#{total.count} enabled=#{enabled.count}")
- enabled
- end
- def all_invoices(icore_connection,start_date, mias)
- icore_account_id2short_code = mias.map { |mia| [mia['InvoiceUserID'], [mia['MemberID'], mia['LocationUID']]] }
- icore_account_id2short_code = Hash[icore_account_id2short_code]
- icore_invoices = icore_account_id2short_code.map do |icore_account_id, v|
- short_code, location_code = v
- begin
- invoices = icore_connection.invoices_by_icore_account(icore_account_id, start_date)
- 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}")
- [icore_account_id, invoices]
- rescue Exception => e
- IcoreService.log_error(e)
- Rails.logger.info("[iCore] couldn't get invoices by account: icore_account_id=#{icore_account_id} short_code=#{short_code} location_code=#{location_code}")
- nil
- end
- end
- icore_invoices = Hash[icore_invoices.select(&:present?)]
- Rails.logger.info("[iCore] found #{icore_invoices.count} invoices")
- icore_invoices.flat_map do |icore_account_id, invoices|
- short_code, location = icore_account_id2short_code[icore_account_id]
- invoices.map do |invoice|
- {
- icore_id: invoice['InvoiceID'],
- icore_account_id: icore_account_id,
- wework_location_code: location,
- wework_short_code: short_code,
- icore_invoice_date: Date.parse(invoice['InvoiceDate']),
- icore_usage: invoice['Usage'].to_d,
- icore_total: invoice['Total'].to_d,
- icore_tax: invoice['Tax'].to_d,
- icore_charge: invoice['Charge'].to_d,
- icore_invoice_url: invoice['InvoiceURL']
- }
- end
- end
- end
- def save_invoice(icore_connection, invoice_hash)
- id = invoice_hash[:icore_id]
- if invoice_hash[:icore_invoice_url].blank?
- Rails.logger.info("invoice url NOT populated, making InvoiceDetailUrl call")
- url = safe_url(icore_connection, id)
- invoice_hash[:icore_invoice_url] = url if url
- else
- Rails.logger.info("invoice url already populated, skipping InvoiceDetailUrl call")
- end
- invoice = IcoreInvoice.find_or_create_by(icore_id: id) do |i|
- i.update_attributes(invoice_hash)
- end
- invoice
- end
- def safe_url(icore_connection, icore_id)
- urls = icore_connection.invoice_url(icore_id)
- urls.count == 1 ? urls.first['InvoiceURL'] : nil
- rescue Exception => e
- IcoreService.log_error(e)
- nil
- end
- icore_connection = IcoreConnection.new
- start_date = TimeProvider.today.prev_month.beginning_of_month
- icore_accounts = enabled_invoicing_accounts(icore_connection)
- icore_invoices = all_invoices(icore_connection, start_date, icore_accounts)
- icore_invoices.each do |invoice|
- begin
- account = Account.find_by(short_code: invoice[:wework_short_code])
- raise "Tried creating IcoreInvoice for an account that does not exist: #{invoice[:wework_short_code]}" if account.nil?
- raise "Tried creating IcoreInvoice for an inactive account: #{account}" unless account.active?
- location = Location.find_by(code: invoice[:wework_location_code])
- 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?
- TenantService.switch_tenant_with_location(location)
- icore_invoice = save_invoice(icore_connection, invoice)
- #no reservation saving:
- # reservation = create_reservation_from_icore_invoice(icore_invoice)
- rescue Exception => e
- IcoreService.log_error(e)
- end
- end
- end
Add Comment
Please, Sign In to add comment