import collections


def parse_billing_data(billing_data):
  """
  parse the billing data and store it in a hash

  args:
    billing_data:  CSV object of billing data

  returns:
    user_dict:  dict, keyed by AWS ID, containing name, user total for all
                services, and currency
    currency:   string, currency used (ie: USD)
    month:      billing month (for CSV output)
    year:       billing year  (for CSV output)
  """
  user_dict = collections.defaultdict(dict)
  currency = ''
  month = ''
  year = ''

  for row in billing_data:
    if len(row) < 4:
      continue
    if row[3] == 'AccountTotal':
      if not currency:
        currency = row[23]

      if not month or not year:
        date = row[6]
        month = date[5:7]
        year = date[0:4]

      acct_num = row[2]
      user_dict[acct_num]['name'] = row[9]
      user_dict[acct_num]['total'] = float(row[24])
      user_dict[acct_num]['currency'] = row[23]

  return user_dict, currency, month, year