def __pd_balances_prep(self, company_id, account_ids, excl_contra=None, excl_interco=False, with_counterparties=None):
    if api_func('gl', 'company', company_id)['cmpy_type'] == 'CON':
            excl_interco = True

    cache = accountifie.gl.cache.get_cache(company_id)

    entries = cache.get_gl_entries(account_ids)

    if entries is None or entries.empty:
      return None

    if excl_contra:
      for contra in excl_contra:
        entries = entries[entries.contra_accts.map(lambda x: contra not in x.split(','))]

    if excl_interco:
      entries = entries[~entries.apply(self.__inter_co, axis=1)]

    if with_counterparties:
      entries = entries[entries['counterparty'] in with_counterparties]

    if entries.empty:
      return None
    clean_end = lambda row: row['date_end'] if row['date_end'] else row['date']
    entries['date_end'] = entries.apply(clean_end, axis=1)

    return entries
    def transactions(self, company_id, account_ids, from_date, to_date,
                     chunk_frequency, with_counterparties, excl_interco,
                     excl_contra):
        period_entries = []
        from_date = dateutil.parser.parse(from_date).date() if isinstance(
            from_date, basestring) else from_date
        to_date = dateutil.parser.parse(to_date).date() if isinstance(
            to_date, basestring) else to_date

        periods = [{'from': from_date, 'to': to_date}]
        if chunk_frequency == 'end-of-month':
            periods = self.monthly_chunk_periods(from_date, to_date)
        else:
            raise Exception('Unknown chunk frequency: %s' % chunk_frequency)

        cache = accountifie.gl.cache.get_cache(company_id)
        clean_end = lambda row: row['date_end'] if row['date_end'] else row[
            'date']
        clean_id = lambda row: str(row['transaction_id'])
        for period in periods:
            start = period['from']
            end = period['to']

            entries = cache.get_gl_entries(account_ids,
                                           from_date=start,
                                           to_date=end)

            if entries is not None and not entries.empty:
                if with_counterparties:
                    entries = entries[entries['counterparty'] in
                                      with_counterparties]

                if excl_contra and not entries.empty:
                    for contra in excl_contra:
                        entries = entries[entries.contra_accts.map(
                            lambda x: contra not in x.split(','))]

                if excl_interco and not entries.empty:
                    entries = entries[~entries.apply(self.__inter_co, axis=1)]

                if entries.empty:
                    break

                entries['transaction_id'] = entries.apply(clean_id, axis=1)
                entries['date_end'] = entries.apply(clean_end, axis=1)
                sub_entries = self.__depreciation_calcs(start, end, entries)

                sub_entries.reset_index(drop=False, inplace=True)
                sub_entries.rename(columns={'transaction_id': 'id'},
                                   inplace=True)
                period_entries.append(sub_entries)

        if len(period_entries) == 0:
            return None
        else:
            all_entries = pd.concat(period_entries, ignore_index=True)
            return all_entries.filter(items=[
                'date', 'id', 'comment', 'account_id', 'contra_accts',
                'counterparty', 'amount'
            ]).to_dict()
    def __pd_balances_prep(self,
                           company_id,
                           account_ids,
                           excl_contra=None,
                           excl_interco=False,
                           with_counterparties=None):
        if api_func('gl', 'company', company_id)['cmpy_type'] == 'CON':
            excl_interco = True

        cache = accountifie.gl.cache.get_cache(company_id)

        entries = cache.get_gl_entries(account_ids)

        if entries is None or entries.empty:
            return None

        if excl_contra:
            for contra in excl_contra:
                entries = entries[entries.contra_accts.map(
                    lambda x: contra not in x.split(','))]

        if excl_interco:
            entries = entries[~entries.apply(self.__inter_co, axis=1)]

        if with_counterparties:
            entries = entries[entries['counterparty'] in with_counterparties]

        if entries.empty:
            return None
        clean_end = lambda row: row['date_end'] if row['date_end'] else row[
            'date']
        entries['date_end'] = entries.apply(clean_end, axis=1)

        return entries
  def transactions(self, company_id, account_ids, from_date, to_date, chunk_frequency, with_counterparties, excl_interco, excl_contra):
    period_entries = []
    from_date = dateutil.parser.parse(from_date).date() if isinstance(from_date, basestring) else from_date
    to_date =   dateutil.parser.parse(to_date).date()   if isinstance(to_date, basestring) else to_date

    periods = [{'from': from_date, 'to': to_date}]
    if chunk_frequency == 'end-of-month':
      periods = self.monthly_chunk_periods(from_date, to_date)
    else:
      raise Exception('Unknown chunk frequency: %s' % chunk_frequency)

    cache = accountifie.gl.cache.get_cache(company_id)
    clean_end = lambda row: row['date_end'] if row['date_end'] else row['date']
    clean_id = lambda row: str(row['transaction_id'])
    for period in periods:
      start = period['from']
      end = period['to']

      entries = cache.get_gl_entries(account_ids, from_date=start, to_date=end)

      if entries is not None and not entries.empty:
        if with_counterparties:
          entries = entries[entries['counterparty'] in with_counterparties]

        if excl_contra and not entries.empty:
          for contra in excl_contra:
            entries = entries[entries.contra_accts.map(lambda x: contra not in x.split(','))]

        if excl_interco and not entries.empty:
          entries = entries[~entries.apply(self.__inter_co, axis=1)]

        if entries.empty:
          break

        entries['transaction_id'] = entries.apply(clean_id, axis=1)
        entries['date_end'] = entries.apply(clean_end, axis=1)
        sub_entries = self.__depreciation_calcs(start, end, entries)

        sub_entries.reset_index(drop=False, inplace=True)
        sub_entries.rename(columns={'transaction_id': 'id'}, inplace=True)
        period_entries.append(sub_entries)

    if len(period_entries) == 0:
      return None
    else:
      all_entries = pd.concat(period_entries, ignore_index=True)
      return all_entries.filter(items=['date', 'id', 'comment', 'account_id', 'contra_accts', 'counterparty', 'amount']).to_dict()