# Path: auxi/core/reporting.py
# class Report(Object):
#     """
#     Base class for all auxi reports.
#     """
# 
#     def __init__(self, data_source, output_path=None):
#         self.data_source = data_source
#         self.output_path = output_path
# 
#     def _generate_table_(self):
#         return []
# 
#     def _render_matplotlib_(self, png=False):
#         pass
# 
#     def render(self, format=ReportFormat.printout):
#         """
#         Render the report in the specified format
# 
#         :param format: The format. The default format is to print
#           the report to the console.
# 
#         :returns: If the format was set to 'string' then a string
#           representation of the report is returned.
#         """
# 
#         table = self._generate_table_()
#         if format == ReportFormat.printout:
#             print(tabulate(table, headers="firstrow", tablefmt="simple"))
#         elif format == ReportFormat.latex:
#             self._render_latex_(table)
#         elif format == ReportFormat.txt:
#             self._render_txt_(table)
#         elif format == ReportFormat.csv:
#             self._render_csv_(table)
#         elif format == ReportFormat.string:
#             return str(tabulate(table, headers="firstrow", tablefmt="simple"))
#         elif format == ReportFormat.matplotlib:
#             self._render_matplotlib_()
#         elif format == ReportFormat.png:
#             if self.output_path is None:
#                 self._render_matplotlib_()
#             else:
#                 self._render_matplotlib_(True)
# 
#     def _render_latex_(self, table):
#         if self.output_path is not None:
#             with open(self.output_path + '.tex', 'w') as f:
#                 f.write(
#                     tabulate(table, headers="firstrow", tablefmt="latex"))
#         else:
#             print(tabulate(table, headers="firstrow", tablefmt="latex"))
# 
#     def _render_txt_(self, table):
#         if self.output_path is not None:
#             with open(self.output_path + '.txt', 'w') as f:
#                 f.write(
#                     tabulate(table, headers="firstrow", tablefmt="simple"))
#         else:
#             print(tabulate(table, headers="firstrow", tablefmt="simple"))
# 
#     def _render_csv_(self, table):
#         if self.output_path is not None:
#             with open(self.output_path + '.csv', 'w') as f:
#                 csv.writer(f, lineterminator='\n').writerows(table)
#         else:
#             with StringIO() as f:
#                 csv.writer(f, lineterminator='\n').writerows(table)
#                 print(f.getvalue())
# 
# Path: auxi/modelling/financial/des.py
# class AccountType(Enum):
#     """
#     Represents the type of general ledger account.
#     """
# 
#     asset = 1
#     equity = 2
#     expense = 3
#     liability = 4
#     revenue = 5


from datetime import datetime
from enum import Enum
from auxi.core.helpers import get_date
from auxi.core.reporting import Report as ReportBase
from auxi.modelling.financial.des import AccountType
    from auxi.modelling.reporting import GeneralLedgerStructureUnitTester
    from auxi.modelling.reporting import TransactionListUnitTester
    from auxi.modelling.reporting import BalanceSheetUnitTester
    from auxi.modelling.reporting import IncomeStatementUnitTester
    import unittest
    :param output_path: The path to write the report file to.
    """
    # TODO: Generate transaction for a component at a path only.

    def __init__(self, data_source, start=datetime.min.date(),
                 end=datetime.max.date(), component_path="", output_path=None):
        """
        """
        self.start_date = get_date(start)
        self.end_date = get_date(end)
        self.component_path = component_path
        super(IncomeStatement, self).__init__(data_source, output_path)

    def _sum_amounts_per_account_(self):
        gls = self.data_source.structure
        summedIncome = {}
        summedExpenses = {}
        for t in self.data_source.transactions:
            if not t.source.startswith(self.component_path):
                continue
            if self.start_date <= t.tx_date <= self.end_date:
                cr_account_type = gls.get_account(t.cr_account).account_type
                dt_account_type = gls.get_account(t.dt_account).account_type
                if not t.is_closing_cr_account and \
                   not t.is_closing_dt_account and t.cr_account != "":
                    if cr_account_type == AccountType.revenue:
                        if t.cr_account in summedIncome:
                            summedIncome[t.cr_account] += t.amount
                        else:
                            summedIncome[t.cr_account] = t.amount

# Path: auxi/core/reporting.py
# class Report(Object):
#     """
#     Base class for all auxi reports.
#     """
# 
#     def __init__(self, data_source, output_path=None):
#         self.data_source = data_source
#         self.output_path = output_path
# 
#     def _generate_table_(self):
#         return []
# 
#     def _render_matplotlib_(self, png=False):
#         pass
# 
#     def render(self, format=ReportFormat.printout):
#         """
#         Render the report in the specified format
# 
#         :param format: The format. The default format is to print
#           the report to the console.
# 
#         :returns: If the format was set to 'string' then a string
#           representation of the report is returned.
#         """
# 
#         table = self._generate_table_()
#         if format == ReportFormat.printout:
#             print(tabulate(table, headers="firstrow", tablefmt="simple"))
#         elif format == ReportFormat.latex:
#             self._render_latex_(table)
#         elif format == ReportFormat.txt:
#             self._render_txt_(table)
#         elif format == ReportFormat.csv:
#             self._render_csv_(table)
#         elif format == ReportFormat.string:
#             return str(tabulate(table, headers="firstrow", tablefmt="simple"))
#         elif format == ReportFormat.matplotlib:
#             self._render_matplotlib_()
#         elif format == ReportFormat.png:
#             if self.output_path is None:
#                 self._render_matplotlib_()
#             else:
#                 self._render_matplotlib_(True)
# 
#     def _render_latex_(self, table):
#         if self.output_path is not None:
#             with open(self.output_path + '.tex', 'w') as f:
#                 f.write(
#                     tabulate(table, headers="firstrow", tablefmt="latex"))
#         else:
#             print(tabulate(table, headers="firstrow", tablefmt="latex"))
# 
#     def _render_txt_(self, table):
#         if self.output_path is not None:
#             with open(self.output_path + '.txt', 'w') as f:
#                 f.write(
#                     tabulate(table, headers="firstrow", tablefmt="simple"))
#         else:
#             print(tabulate(table, headers="firstrow", tablefmt="simple"))
# 
#     def _render_csv_(self, table):
#         if self.output_path is not None:
#             with open(self.output_path + '.csv', 'w') as f:
#                 csv.writer(f, lineterminator='\n').writerows(table)
#         else:
#             with StringIO() as f:
#                 csv.writer(f, lineterminator='\n').writerows(table)
#                 print(f.getvalue())
# 
# Path: auxi/modelling/financial/des.py
# class AccountType(Enum):
#     """
#     Represents the type of general ledger account.
#     """
# 
#     asset = 1
#     equity = 2
#     expense = 3
#     liability = 4
#     revenue = 5


# Path: auxi/modelling/financial/reporting.py
from datetime import datetime
from enum import Enum
from auxi.core.helpers import get_date
from auxi.core.reporting import Report as ReportBase
from auxi.modelling.financial.des import AccountType
    from auxi.modelling.reporting import GeneralLedgerStructureUnitTester
    from auxi.modelling.reporting import TransactionListUnitTester
    from auxi.modelling.reporting import BalanceSheetUnitTester
    from auxi.modelling.reporting import IncomeStatementUnitTester
    import unittest

    :param output_path: The path to write the report file to.
    """
    # TODO: Generate transaction for a component at a path only.

    def __init__(self, data_source, start=datetime.min.date(),
                 end=datetime.max.date(), component_path="", output_path=None):
        """
        """
        self.start_date = get_date(start)
        self.end_date = get_date(end)
        self.component_path = component_path
        super(IncomeStatement, self).__init__(data_source, output_path)

    def _sum_amounts_per_account_(self):
        gls = self.data_source.structure
        summedIncome = {}
        summedExpenses = {}
        for t in self.data_source.transactions:
            if not t.source.startswith(self.component_path):
                continue
            if self.start_date <= t.tx_date <= self.end_date:
                cr_account_type = gls.get_account(t.cr_account).account_type
                dt_account_type = gls.get_account(t.dt_account).account_type
                if not t.is_closing_cr_account and \
                   not t.is_closing_dt_account and t.cr_account != "":
                    if cr_account_type == AccountType.revenue:
                        if t.cr_account in summedIncome:
                            summedIncome[t.cr_account] += t.amount
                        else:
                            summedIncome[t.cr_account] = t.amount

                    elif cr_account_type == AccountType.expense: