# Path: payway/accounts/models.py
# MAX_MONEY_DIGITS = 20
# 
# MAX_MONEY_PLACES = 2
# 
# class Account(RandomUIDAbstractModel):
#     user = models.ForeignKey(User, related_name='accounts', verbose_name=_('user'))
#     currency_code = models.CharField(_('currency code'), max_length=3, choices=CURRENCY_CHOICES, default=moneyed.RUB)
# 
#     class Meta:
#         verbose_name = _('account')
#         verbose_name_plural = _('accounts')
#         permissions = (('can_view_account_report', 'Can view account report'),)
#         db_table = 'payway_accounts'
# 
#     @property
#     def currency(self):
#         currency = None
#         if self.currency_code:
#             currency = get_currency(self.currency_code)
#         return currency
# 
#     def __unicode__(self):
#         return u"{0} {1}".format(self.uid, self.currency)
# 
#     def get_balance(self):
#         sum = Transaction.objects.filter(account=self, money_amount_currency=self.currency).sum_values()
#         return round_down(Money(sum, self.currency))
# 
#     def withdraw(self, money, allow_overdraft=False):
#         self.assert_correct_currency(money)
# 
#         if money < Money(0, self.currency):
#             raise ValueError("You can't withdraw a negative amount")
# 
#         if not allow_overdraft and (self.get_balance() - money) < Money(0, self.currency):
#             raise Overdraft
# 
#         return Transaction.objects.create(
#             account=self,
#             money_amount=money * '-1.0',
#         )
# 
#     def add(self, money):
#         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))
# 
# Path: payway/merchants/models.py
# class Merchant(RandomUIDAbstractModel):
# 
#     URL_METHODS = Choices(
#         ('POST', 'POST'),
#         ('GET',  'GET'),
#     )
# 
#     user = models.ForeignKey(User, related_name='merchants', verbose_name=_('user'))
#     name = models.CharField('Имя', max_length=255)
#     secret_key = models.CharField(_('secret key'), max_length=50)
# 
#     result_url = models.URLField(_('result url'))
#     result_url_method = models.CharField(_('result url method'), max_length=4, choices=URL_METHODS)
# 
#     success_url = models.URLField(_('success url'))
#     fail_url = models.URLField(_('fail url'))
# 
#     class Meta:
#         verbose_name = 'продавец'
#         verbose_name_plural = 'продавцы'
#         db_table = 'payway_merchants'


from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_simptools.money.models import fields
from model_utils import Choices
from model_utils.models import TimeStampedModel
from django_simptools.managers import ChainableQuerySetManager
from django_simptools.querysets import ExtendedQuerySet
from payway.accounts.models import MAX_MONEY_DIGITS, MAX_MONEY_PLACES, Account
from payway.merchants.models import Merchant
# -*- coding: UTF-8 -*-

__author__ = 'Razzhivin Alexander'
__email__ = 'admin@httpbots.com'


class Order(TimeStampedModel):
    PAYMENT_STATUS = Choices(
        (False, 'NOT_PAID', _('not paid')),
        (True,  'PAID', _('paid')),
    )
    user = models.ForeignKey(User, related_name='orders', verbose_name=_('user'))
    merchant = models.ForeignKey(Merchant, related_name='orders', verbose_name=_('merchant'))
    uid = models.PositiveIntegerField(unique=False, editable=False)

# Path: payway/accounts/models.py
# MAX_MONEY_DIGITS = 20
# 
# MAX_MONEY_PLACES = 2
# 
# class Account(RandomUIDAbstractModel):
#     user = models.ForeignKey(User, related_name='accounts', verbose_name=_('user'))
#     currency_code = models.CharField(_('currency code'), max_length=3, choices=CURRENCY_CHOICES, default=moneyed.RUB)
# 
#     class Meta:
#         verbose_name = _('account')
#         verbose_name_plural = _('accounts')
#         permissions = (('can_view_account_report', 'Can view account report'),)
#         db_table = 'payway_accounts'
# 
#     @property
#     def currency(self):
#         currency = None
#         if self.currency_code:
#             currency = get_currency(self.currency_code)
#         return currency
# 
#     def __unicode__(self):
#         return u"{0} {1}".format(self.uid, self.currency)
# 
#     def get_balance(self):
#         sum = Transaction.objects.filter(account=self, money_amount_currency=self.currency).sum_values()
#         return round_down(Money(sum, self.currency))
# 
#     def withdraw(self, money, allow_overdraft=False):
#         self.assert_correct_currency(money)
# 
#         if money < Money(0, self.currency):
#             raise ValueError("You can't withdraw a negative amount")
# 
#         if not allow_overdraft and (self.get_balance() - money) < Money(0, self.currency):
#             raise Overdraft
# 
#         return Transaction.objects.create(
#             account=self,
#             money_amount=money * '-1.0',
#         )
# 
#     def add(self, money):
#         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))
# 
# Path: payway/merchants/models.py
# class Merchant(RandomUIDAbstractModel):
# 
#     URL_METHODS = Choices(
#         ('POST', 'POST'),
#         ('GET',  'GET'),
#     )
# 
#     user = models.ForeignKey(User, related_name='merchants', verbose_name=_('user'))
#     name = models.CharField('Имя', max_length=255)
#     secret_key = models.CharField(_('secret key'), max_length=50)
# 
#     result_url = models.URLField(_('result url'))
#     result_url_method = models.CharField(_('result url method'), max_length=4, choices=URL_METHODS)
# 
#     success_url = models.URLField(_('success url'))
#     fail_url = models.URLField(_('fail url'))
# 
#     class Meta:
#         verbose_name = 'продавец'
#         verbose_name_plural = 'продавцы'
#         db_table = 'payway_merchants'


# Path: payway/orders/models.py
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django_simptools.money.models import fields
from model_utils import Choices
from model_utils.models import TimeStampedModel
from django_simptools.managers import ChainableQuerySetManager
from django_simptools.querysets import ExtendedQuerySet
from payway.accounts.models import MAX_MONEY_DIGITS, MAX_MONEY_PLACES, Account
from payway.merchants.models import Merchant

# -*- coding: UTF-8 -*-

__author__ = 'Razzhivin Alexander'
__email__ = 'admin@httpbots.com'


class Order(TimeStampedModel):
    PAYMENT_STATUS = Choices(
        (False, 'NOT_PAID', _('not paid')),
        (True,  'PAID', _('paid')),
    )
    user = models.ForeignKey(User, related_name='orders', verbose_name=_('user'))
    merchant = models.ForeignKey(Merchant, related_name='orders', verbose_name=_('merchant'))
    uid = models.PositiveIntegerField(unique=False, editable=False)

    sum = fields.MoneyField(_('sum'), max_digits=MAX_MONEY_DIGITS, decimal_places=MAX_MONEY_PLACES)