Exemplo n.º 1
0
    def add_event(
        self,
        account: models.Account,
        amount: decimal.Decimal,
        executed_at: datetime.datetime,
        event_type: models.EventType,
        position: Optional[models.Position] = None,
        withheld_taxes: Optional[decimal.Decimal] = None,
    ) -> Tuple[models.AccountEvent, bool]:

        if (event_type == models.EventType.DEPOSIT
                or event_type == models.EventType.DIVIDEND):
            assert amount > 0
        if event_type == models.EventType.WITHDRAWAL:
            assert amount < 0

        event, created = models.AccountEvent.objects.get_or_create(
            account=account,
            amount=amount,
            executed_at=executed_at,
            event_type=event_type,
            position=position,
            withheld_taxes=withheld_taxes or 0,
        )
        if created:
            balance_change = amount
            if withheld_taxes:
                balance_change -= withheld_taxes

            if event_type == models.EventType.DIVIDEND and position:
                position_currency = position.asset.currency
                account_currency = account.currency
                if position_currency != account_currency:
                    exchange_rate = prices.get_closest_exchange_rate(
                        date=executed_at.date(),
                        from_currency=position_currency,
                        to_currency=account_currency,
                    )
                    if exchange_rate is None:
                        raise ValueError(
                            f"Can't convert between currencies: "
                            f"{position_currency} and {account_currency}")
                    balance_change *= exchange_rate.value

            account.balance += balance_change
        account.save()
        return event, created
Exemplo n.º 2
0
    def add_crypto_income_event(
        self,
        account: models.Account,
        symbol: str,
        executed_at: datetime.datetime,
        quantity: decimal.Decimal,
        price: decimal.Decimal,
        local_value: decimal.Decimal,
        value_in_account_currency: decimal.Decimal,
        event_type: models.EventType,
    ) -> Tuple[models.AccountEvent, bool]:

        (
            transaction,
            _,
        ) = self.add_transaction_crypto_asset(
            account,
            symbol,
            executed_at,
            quantity,
            price,
            # Local value.
            local_value,
            value_in_account_currency,
            value_in_account_currency,
        )

        position = transaction.position

        event, created = models.AccountEvent.objects.get_or_create(
            account=account,
            amount=-value_in_account_currency,
            executed_at=executed_at,
            event_type=event_type,
            position=position,
            transaction=transaction,
            withheld_taxes=0,
        )

        if created:
            account.balance += -value_in_account_currency
        account.save()

        return event, created
Exemplo n.º 3
0
def add_account(request):
    if request.method == "POST":
        form = AddAccount(request.POST)
        if form.is_valid():
            data = {}
            data['name'] = form.cleaned_data['name']
            data['kind'] = form.cleaned_data['kind']
            account = Account(**data)
            account.save()
            savings = SavingTotal.objects.all()
            for saving in savings:
                data = {}
                data['saving_total'] = saving
                data['account'] = account
                new_saving = Saving(**data)
                new_saving.save()
            messages.success(request,
                             u"Рахунок %s успішно доданий" % account.name)
    url = reverse("home") + "?list=accounts"
    return HttpResponseRedirect(url)
Exemplo n.º 4
0
    def create_account(self, member, account_type):
        """
        account_type: debit/debt
        """
        if account_type == 'debit':
            name = 'Debit {}'.format(member.name)
            acc_type = Account.CHECKING
            balance = random.randint(300, 2000)
        else:
            name = 'Credit {}'.format(member.name)
            acc_type = Account.LOAN
            balance = -random.randint(50000, 100000)

        a = Account(member=member)
        a.guid = uuid.uuid4().hex
        a.uid = uuid.uuid4().hex
        a.name = name
        a.type = acc_type
        a.updated_at = timezone.now()
        a.balance = balance
        a.available_balance = a.balance
        a.save()