# Path: core/util.py
# def first(iterable):
#     """Returns the first item of ``iterable``.
#     """
#     try:
#         return next(iter(iterable))
#     except StopIteration:
#         return None
# 
# Path: core/const.py
# class AccountType:
#     """Enum of all possible account types.
# 
#     * ``Asset``
#     * ``Liability``
#     * ``Income``
#     * ``Expense``
# 
#     Special values:
# 
#     * ``All``: set containing all account types
#     * ``InOrder``: all account type in "sort" order.
#     """
#     Asset = 'asset'
#     Liability = 'liability'
#     Income = 'income'
#     Expense = 'expense'
#     InOrder = [Asset, Liability, Income, Expense]
#     All = set(InOrder)


import weakref
from datetime import date
from core.util import first
from ..const import AccountType
from ..model._ccore import AccountList, Transaction
from .base import GUIPanel
from .split_table import SplitTable
from .completable_edit import CompletableEdit
# Copyright 2019 Virgil Dupras
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.gnu.org/licenses/gpl-3.0.html




class PanelWithTransaction(GUIPanel):
    """Base class for panels working with a transaction"""
    def __init__(self, mainwindow):
        GUIPanel.__init__(self, mainwindow)
        self.transaction = Transaction(date.today())
        self._selected_splits = []
        # Place to store temporarily created accounts during the editing of the
        # transaction.
        self._tmpaccounts = AccountList(self.document.default_currency)
        # completable_edit has to be set before split_table is created because split table fetches
        # our completable edit on __init__ (for Qt).
        self.completable_edit = CompletableEdit(mainwindow)
        self.split_table = SplitTable(weakref.proxy(self))

    def change_split(self, split, account_name, amount, memo):
        if split is None:
            split = self.transaction.new_split()
        if account_name:
            if split.account:
                account_type = split.account.type
            else:

# Path: core/util.py
# def first(iterable):
#     """Returns the first item of ``iterable``.
#     """
#     try:
#         return next(iter(iterable))
#     except StopIteration:
#         return None
# 
# Path: core/const.py
# class AccountType:
#     """Enum of all possible account types.
# 
#     * ``Asset``
#     * ``Liability``
#     * ``Income``
#     * ``Expense``
# 
#     Special values:
# 
#     * ``All``: set containing all account types
#     * ``InOrder``: all account type in "sort" order.
#     """
#     Asset = 'asset'
#     Liability = 'liability'
#     Income = 'income'
#     Expense = 'expense'
#     InOrder = [Asset, Liability, Income, Expense]
#     All = set(InOrder)


# Path: core/gui/transaction_panel.py
import weakref
from datetime import date
from core.util import first
from ..const import AccountType
from ..model._ccore import AccountList, Transaction
from .base import GUIPanel
from .split_table import SplitTable
from .completable_edit import CompletableEdit

# Copyright 2019 Virgil Dupras
#
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.gnu.org/licenses/gpl-3.0.html




class PanelWithTransaction(GUIPanel):
    """Base class for panels working with a transaction"""
    def __init__(self, mainwindow):
        GUIPanel.__init__(self, mainwindow)
        self.transaction = Transaction(date.today())
        self._selected_splits = []
        # Place to store temporarily created accounts during the editing of the
        # transaction.
        self._tmpaccounts = AccountList(self.document.default_currency)
        # completable_edit has to be set before split_table is created because split table fetches
        # our completable edit on __init__ (for Qt).
        self.completable_edit = CompletableEdit(mainwindow)
        self.split_table = SplitTable(weakref.proxy(self))

    def change_split(self, split, account_name, amount, memo):
        if split is None:
            split = self.transaction.new_split()
        if account_name:
            if split.account:
                account_type = split.account.type
            else:

                account_type = AccountType.Expense if split.amount < 0 else AccountType.Income