# 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
    def _load(self, transaction):
        self.mainwindow.stop_editing()
        self.transaction = transaction.replicate()
        self.original = transaction
        self.view.refresh_for_multi_currency()
        self.split_table.refresh_initial()

    def _save(self):
        self.document.change_transaction(self.original, self.transaction)
        self.mainwindow.revalidate()

    # --- Public
    def mct_balance(self):
        """Balances the mct by using xchange rates.

        The currency of the new split is the currency of the currently selected split.
        """
        self.split_table.stop_editing()
        split = first(self._selected_splits)
        new_split_currency = self.document.default_currency
        if split is not None and split.amount != 0:
            new_split_currency = split.amount.currency_code
        self.transaction.mct_balance(new_split_currency)
        self.split_table.refresh_splits()

    def assign_imbalance(self):
        """Assigns remaining imbalance to the selected split.

        If the selected split is not an assigned split, does nothing.
        """

# 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

    def _load(self, transaction):
        self.mainwindow.stop_editing()
        self.transaction = transaction.replicate()
        self.original = transaction
        self.view.refresh_for_multi_currency()
        self.split_table.refresh_initial()

    def _save(self):
        self.document.change_transaction(self.original, self.transaction)
        self.mainwindow.revalidate()

    # --- Public
    def mct_balance(self):
        """Balances the mct by using xchange rates.

        The currency of the new split is the currency of the currently selected split.
        """
        self.split_table.stop_editing()
        split = first(self._selected_splits)
        new_split_currency = self.document.default_currency
        if split is not None and split.amount != 0:
            new_split_currency = split.amount.currency_code
        self.transaction.mct_balance(new_split_currency)
        self.split_table.refresh_splits()

    def assign_imbalance(self):
        """Assigns remaining imbalance to the selected split.

        If the selected split is not an assigned split, does nothing.
        """

        split = first(self._selected_splits)