How to use the piecash._common.GncValidationError function in piecash

To help you get started, we’ve selected a few piecash examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sdementen / piecash / piecash / core / transaction.py View on Github external
def __init__(self,
                 currency,
                 description="",
                 notes=None,
                 splits=None,
                 enter_date=None,
                 post_date=None,
                 num="",
                 ):

        if not (enter_date is None or type(enter_date) is datetime.datetime):
            raise GncValidationError("enter_date should be a datetime object")
        if not (post_date is None or type(post_date) is datetime.date):
            raise GncValidationError("post_date should be a date object")

        self.currency = currency
        self.description = description
        self.enter_date = (enter_date
                           if enter_date
                           else datetime.datetime.now()).replace(microsecond=0)
        self.post_date = (post_date if post_date else datetime.date.today())
        self.num = num
        if notes is not None:
            self.notes = notes
        if splits:
            self.splits = splits
github sdementen / piecash / piecash / core / transaction.py View on Github external
if '_quantity_num' in old or '_value_num' in old:
            self.transaction._recalculate_balance = True

        if self.transaction_guid is None:
            raise GncValidationError("The split is not linked to a transaction")

        if self.transaction.currency == self.account.commodity:
            if self.quantity != self.value:
                raise GncValidationError("The split has a quantity diffeerent from value "
                                         "while the transaction currency and the account commodity is the same")
        else:
            if self.quantity is None:
                raise GncValidationError(
                    "The split quantity is not defined while the split is on a commodity different from the transaction")
            if self.quantity.is_signed() != self.value.is_signed():
                raise GncValidationError("The split quantity has not the same sign as the split value")

        # everything is fine, let us normalise the value with respect to the currency/commodity precisions
        self._quantity_denom_basis = self.account.commodity_scu
        self._value_denom_basis = self.transaction.currency.fraction

        if self.transaction.currency != self.account.commodity:
            # let us also add a Price
            from piecash import Price

            value = (self.value / self.quantity).quantize(Decimal("0.000001"))
            try:
                # find existing price if any and if so, do nothing
                pr = self.book.prices(commodity=self.account.commodity,
                                      currency=self.transaction.currency,
                                      date=self.transaction.post_date,
                                      )
github sdementen / piecash / piecash / core / transaction.py View on Github external
def validate(self):
        old = self.get_all_changes()

        if old["STATE_CHANGES"][-1] == "deleted":
            return

        if self.currency.namespace != "CURRENCY":
            raise GncValidationError("You are assigning a non currency commodity to a transaction")

        # check all accounts related to the splits of the transaction are not placeholder(=frozen)
        for sp in self.splits:
            if sp.account.placeholder != 0:
                raise GncValidationError("Account '{}' used in the transaction is a placeholder".format(sp.account))

        # check same currency
        if "currency" in old and old["currency"] is not NEVER_SET:
            raise GncValidationError("You cannot change the currency of a transaction once it has been set")

        # validate the splits
        if hasattr(self, "_recalculate_balance"):
            del self._recalculate_balance
            value_imbalance, quantity_imbalances = self.calculate_imbalances()
            if value_imbalance:
                # raise exception instead of creating an imbalance entry as probably an error
                # (in the gnucash GUI, another decision taken because need of "save unfinished transaction")
                raise GncImbalanceError("The transaction {} is not balanced on its value".format(self))

            if any(quantity_imbalances.values()) and self.book.use_trading_accounts:
                self.normalize_trading_accounts()
github sdementen / piecash / piecash / _common.py View on Github external
from .sa_extra import DeclarativeBase, _Date


class GnucashException(Exception):
    pass


class GncNoActiveSession(GnucashException):
    pass


class GncValidationError(GnucashException):
    pass


class GncImbalanceError(GncValidationError):
    pass


class GncConversionError(GnucashException):
    pass


class Recurrence(DeclarativeBase):
    """
    Recurrence information for scheduled transactions

    Attributes:
        obj_guid (str): link to the parent ScheduledTransaction record.
        recurrence_mult (int): Multiplier for the period type. Describes how many times
            the period repeats for the next occurrence.
        recurrence_period_type (str): type or recurrence (monthly, daily).
github sdementen / piecash / piecash / core / transaction.py View on Github external
if old["STATE_CHANGES"][-1] == "deleted":
            return

        if '_quantity_num' in old or '_value_num' in old:
            self.transaction._recalculate_balance = True

        if self.transaction_guid is None:
            raise GncValidationError("The split is not linked to a transaction")

        if self.transaction.currency == self.account.commodity:
            if self.quantity != self.value:
                raise GncValidationError("The split has a quantity diffeerent from value "
                                         "while the transaction currency and the account commodity is the same")
        else:
            if self.quantity is None:
                raise GncValidationError(
                    "The split quantity is not defined while the split is on a commodity different from the transaction")
            if self.quantity.is_signed() != self.value.is_signed():
                raise GncValidationError("The split quantity has not the same sign as the split value")

        # everything is fine, let us normalise the value with respect to the currency/commodity precisions
        self._quantity_denom_basis = self.account.commodity_scu
        self._value_denom_basis = self.transaction.currency.fraction

        if self.transaction.currency != self.account.commodity:
            # let us also add a Price
            from piecash import Price

            value = (self.value / self.quantity).quantize(Decimal("0.000001"))
            try:
                # find existing price if any and if so, do nothing
                pr = self.book.prices(commodity=self.account.commodity,
github sdementen / piecash / piecash / core / transaction.py View on Github external
def validate(self):
        old = self.get_all_changes()

        if old["STATE_CHANGES"][-1] == "deleted":
            return

        if '_quantity_num' in old or '_value_num' in old:
            self.transaction._recalculate_balance = True

        if self.transaction_guid is None:
            raise GncValidationError("The split is not linked to a transaction")

        if self.transaction.currency == self.account.commodity:
            if self.quantity != self.value:
                raise GncValidationError("The split has a quantity diffeerent from value "
                                         "while the transaction currency and the account commodity is the same")
        else:
            if self.quantity is None:
                raise GncValidationError(
                    "The split quantity is not defined while the split is on a commodity different from the transaction")
            if self.quantity.is_signed() != self.value.is_signed():
                raise GncValidationError("The split quantity has not the same sign as the split value")

        # everything is fine, let us normalise the value with respect to the currency/commodity precisions
        self._quantity_denom_basis = self.account.commodity_scu
        self._value_denom_basis = self.transaction.currency.fraction

        if self.transaction.currency != self.account.commodity:
            # let us also add a Price
            from piecash import Price
github sdementen / piecash / piecash / core / transaction.py View on Github external
old = self.get_all_changes()

        if old["STATE_CHANGES"][-1] == "deleted":
            return

        if self.currency.namespace != "CURRENCY":
            raise GncValidationError("You are assigning a non currency commodity to a transaction")

        # check all accounts related to the splits of the transaction are not placeholder(=frozen)
        for sp in self.splits:
            if sp.account.placeholder != 0:
                raise GncValidationError("Account '{}' used in the transaction is a placeholder".format(sp.account))

        # check same currency
        if "currency" in old and old["currency"] is not NEVER_SET:
            raise GncValidationError("You cannot change the currency of a transaction once it has been set")

        # validate the splits
        if hasattr(self, "_recalculate_balance"):
            del self._recalculate_balance
            value_imbalance, quantity_imbalances = self.calculate_imbalances()
            if value_imbalance:
                # raise exception instead of creating an imbalance entry as probably an error
                # (in the gnucash GUI, another decision taken because need of "save unfinished transaction")
                raise GncImbalanceError("The transaction {} is not balanced on its value".format(self))

            if any(quantity_imbalances.values()) and self.book.use_trading_accounts:
                self.normalize_trading_accounts()
github sdementen / piecash / piecash / core / transaction.py View on Github external
def validate(self):
        old = self.get_all_changes()

        if old["STATE_CHANGES"][-1] == "deleted":
            return

        if self.currency.namespace != "CURRENCY":
            raise GncValidationError("You are assigning a non currency commodity to a transaction")

        # check all accounts related to the splits of the transaction are not placeholder(=frozen)
        for sp in self.splits:
            if sp.account.placeholder != 0:
                raise GncValidationError("Account '{}' used in the transaction is a placeholder".format(sp.account))

        # check same currency
        if "currency" in old and old["currency"] is not NEVER_SET:
            raise GncValidationError("You cannot change the currency of a transaction once it has been set")

        # validate the splits
        if hasattr(self, "_recalculate_balance"):
            del self._recalculate_balance
            value_imbalance, quantity_imbalances = self.calculate_imbalances()
            if value_imbalance:
                # raise exception instead of creating an imbalance entry as probably an error
github sdementen / piecash / piecash / core / transaction.py View on Github external
def validate(self):
        old = self.get_all_changes()

        if old["STATE_CHANGES"][-1] == "deleted":
            return

        if '_quantity_num' in old or '_value_num' in old:
            self.transaction._recalculate_balance = True

        if self.transaction_guid is None:
            raise GncValidationError("The split is not linked to a transaction")

        if self.transaction.currency == self.account.commodity:
            if self.quantity != self.value:
                raise GncValidationError("The split has a quantity diffeerent from value "
                                         "while the transaction currency and the account commodity is the same")
        else:
            if self.quantity is None:
                raise GncValidationError(
                    "The split quantity is not defined while the split is on a commodity different from the transaction")
            if self.quantity.is_signed() != self.value.is_signed():
                raise GncValidationError("The split quantity has not the same sign as the split value")

        # everything is fine, let us normalise the value with respect to the currency/commodity precisions
        self._quantity_denom_basis = self.account.commodity_scu
        self._value_denom_basis = self.transaction.currency.fraction
github sdementen / piecash / piecash / core / transaction.py View on Github external
def __init__(self,
                 currency,
                 description="",
                 notes=None,
                 splits=None,
                 enter_date=None,
                 post_date=None,
                 num="",
                 ):

        if not (enter_date is None or type(enter_date) is datetime.datetime):
            raise GncValidationError("enter_date should be a datetime object")
        if not (post_date is None or type(post_date) is datetime.date):
            raise GncValidationError("post_date should be a date object")

        self.currency = currency
        self.description = description
        self.enter_date = (enter_date
                           if enter_date
                           else datetime.datetime.now()).replace(microsecond=0)
        self.post_date = (post_date if post_date else datetime.date.today())
        self.num = num
        if notes is not None:
            self.notes = notes
        if splits:
            self.splits = splits