# Path: payway/accounts/querysets.py
# class TransactionQuerySet(ExtendedQuerySet):
# 
#     def sum_values(self):
#         return self.aggregate(Sum('money_amount'))['money_amount__sum'] or 0.0
# 
# class InvoiceQuerySet(ExtendedQuerySet):
# 
#     def get_or_create_invoice_with_other_money_amount(self, txn, money_amount):
#         invoice = self.get(uid=txn)
#         if invoice:
#             money_amount = Money(money_amount, invoice.money_amount.currency)
#             if invoice.money_amount != money_amount:
#                 self.__log_different_amount(invoice, invoice.money_amount, money_amount)
#                 invoice = self.create(
#                     account=invoice.account,
#                     money_amount=money_amount,
#                     money_amount_without_percent=invoice.money_amount_without_percent
#                 )
#         return invoice
# 
#     def __log_different_amount(self, invoice, should_be, given):
#         if should_be != given:
#             logging.warning(u"Got different money amount for {0} invoice. Amount should be {1} but was {2}".format(
#                 invoice,
#                 should_be,
#                 given
#             ))


from model_utils import Choices
from model_utils.models import  TimeStampedModel, InheritanceCastModel
from moneyed import CURRENCIES
from django.db import models
from django.contrib.auth.models import User
from django_simptools.managers import ChainableQuerySetManager
from django_simptools.money.models import fields
from django_simptools.models import RandomUIDAbstractModel
from django.utils.translation import ugettext_lazy as _
from moneyed.classes import Money, get_currency
from payway.accounts.querysets import TransactionQuerySet, InvoiceQuerySet
from simptools.money import round_down
import moneyed
        self.assert_correct_currency(money)

        if money < Money(0, self.currency):
            raise ValueError("You can't add a negative money amount")

        return Transaction.objects.create(
            account=self,
            money_amount=money,
        )

    def transfer(self, money, to_account):
        self.assert_correct_currency(money)

        if money < Money(0, self.currency):
            raise ValueError("You can't transfer a negative money amount")

        self.withdraw(money)

        return Transaction.objects.create(
            account=to_account,
            money_amount=money,
        )

    def assert_correct_currency(self, money):
        if money.currency != self.currency:
            raise ValueError("You can't add money with %s currency. Current currency %s" % (money.currency, self.currency))

class Transaction(TimeStampedModel):
    account = models.ForeignKey(Account, related_name='transactions', verbose_name=_('account'))
    money_amount = fields.MoneyField(_('money amount'), max_digits=MAX_MONEY_DIGITS, decimal_places=MAX_MONEY_PLACES)

# Path: payway/accounts/querysets.py
# class TransactionQuerySet(ExtendedQuerySet):
# 
#     def sum_values(self):
#         return self.aggregate(Sum('money_amount'))['money_amount__sum'] or 0.0
# 
# class InvoiceQuerySet(ExtendedQuerySet):
# 
#     def get_or_create_invoice_with_other_money_amount(self, txn, money_amount):
#         invoice = self.get(uid=txn)
#         if invoice:
#             money_amount = Money(money_amount, invoice.money_amount.currency)
#             if invoice.money_amount != money_amount:
#                 self.__log_different_amount(invoice, invoice.money_amount, money_amount)
#                 invoice = self.create(
#                     account=invoice.account,
#                     money_amount=money_amount,
#                     money_amount_without_percent=invoice.money_amount_without_percent
#                 )
#         return invoice
# 
#     def __log_different_amount(self, invoice, should_be, given):
#         if should_be != given:
#             logging.warning(u"Got different money amount for {0} invoice. Amount should be {1} but was {2}".format(
#                 invoice,
#                 should_be,
#                 given
#             ))


# Path: payway/accounts/models.py
from model_utils import Choices
from model_utils.models import  TimeStampedModel, InheritanceCastModel
from moneyed import CURRENCIES
from django.db import models
from django.contrib.auth.models import User
from django_simptools.managers import ChainableQuerySetManager
from django_simptools.money.models import fields
from django_simptools.models import RandomUIDAbstractModel
from django.utils.translation import ugettext_lazy as _
from moneyed.classes import Money, get_currency
from payway.accounts.querysets import TransactionQuerySet, InvoiceQuerySet
from simptools.money import round_down
import moneyed

        self.assert_correct_currency(money)

        if money < Money(0, self.currency):
            raise ValueError("You can't add a negative money amount")

        return Transaction.objects.create(
            account=self,
            money_amount=money,
        )

    def transfer(self, money, to_account):
        self.assert_correct_currency(money)

        if money < Money(0, self.currency):
            raise ValueError("You can't transfer a negative money amount")

        self.withdraw(money)

        return Transaction.objects.create(
            account=to_account,
            money_amount=money,
        )

    def assert_correct_currency(self, money):
        if money.currency != self.currency:
            raise ValueError("You can't add money with %s currency. Current currency %s" % (money.currency, self.currency))

class Transaction(TimeStampedModel):
    account = models.ForeignKey(Account, related_name='transactions', verbose_name=_('account'))
    money_amount = fields.MoneyField(_('money amount'), max_digits=MAX_MONEY_DIGITS, decimal_places=MAX_MONEY_PLACES)

    objects = ChainableQuerySetManager(TransactionQuerySet)