Esempio n. 1
0
    def delete(self, request, *args, **kwargs):
        model = ujson.loads(request.POST.get("model", '{}'))
        goal = get_object_or_404(Goal,
                                 pk=model["id"],
                                 account__primary_owner=self.client)
        goal.archived = True
        # remove from financial plan
        # remove automatic deposit and wdw
        #AutomaticWithdrawal.objects.filter(account=goal).delete()
        #AutomaticDeposit.objects.filter(account=goal).delete()

        # move shares
        if goal.total_balance > 0:
            if "transferAllTo" in model and model["transferAllTo"] != "bank":
                to_account = int(model["transferAllTo"])
                transfer_transaction = Transaction()
                transfer_transaction.type = "ACCOUNT_TRANSFER_FROM"
                transfer_transaction.from_account = goal
                transfer_transaction.to_account = get_object_or_404(Goal, pk=to_account,
                                                                    account__primary_owner=self.client)
                transfer_transaction.amount = goal.total_balance
                # transfer shares to the other account
                for p in Position.objects.filter(goal=transfer_transaction.from_account).all():
                    new_position, is_new = Position.objects.get_or_create(goal=transfer_transaction.to_account,
                                                                          ticker=p.ticker)
                    new_position.share += p.share
                    new_position.save()
                    p.delete()

                transfer_transaction.status = Transaction.STATUS_EXECUTED
                transfer_transaction.executed = now()
                transfer_transaction.save()

            else:
                # wdw all the money
                wdw_transaction = Transaction()
                wdw_transaction.account = goal
                wdw_transaction.type = Transaction.REASON_WITHDRAWAL
                wdw_transaction.amount = goal.total_balance
                wdw_transaction.save()
        goal.save()
        return HttpResponse('null', content_type="application/json")
Esempio n. 2
0
    def post(self, request, *args, **kwargs):
        model = ujson.loads(request.POST.get("model", '{}'))
        new_transaction = Transaction()
        if model['account']:
            new_transaction.account = get_object_or_404(
                Goal,
                pk=model['account'],
                account__primary_owner=self.client)

        new_transaction.from_account = None
        new_transaction.to_account = None
        new_transaction.type = model["type"]
        new_transaction.amount = model["amount"]

        if model["fromAccount"]:
            new_transaction.from_account = get_object_or_404(
                Goal,
                pk=model['fromAccount'],
                account__primary_owner=self.client)

        if model["toAccount"]:
            new_transaction.to_account = get_object_or_404(
                Goal,
                pk=model['toAccount'],
                account__primary_owner=self.client)

        new_transaction.save()

        if new_transaction.type == "ACCOUNT_TRANSFER_FROM":
            # transfer shares to the other account
            total_balance = new_transaction.from_account.total_balance
            transaction_amount = float(new_transaction.amount)
            for p in Position.objects.filter(goal=new_transaction.from_account).all():
                transfer_share_size = (p.value/total_balance) * transaction_amount / p.ticker.unit_price
                p.share -= transfer_share_size
                p.save()
                new_position, is_new = Position.objects.get_or_create(goal=new_transaction.to_account, ticker=p.ticker)
                new_position.share += transfer_share_size
                new_position.save()
            new_transaction.status = Transaction.STATUS_EXECUTED
            new_transaction.executed = now()
            new_transaction.save()

        if not new_transaction.account:
            return HttpResponse("null", content_type='application/json')

        nt_return = {
            "id": new_transaction.pk,
            "type": new_transaction.type,
            "amount": new_transaction.amount
        }
        if new_transaction.account:
            nt_return["account"] = new_transaction.account.pk

        if new_transaction.from_account:
            nt_return["fromAccount"] = new_transaction.from_account.pk

        if new_transaction.to_account:
            nt_return["toAccount"] = new_transaction.to_account.pk

        return HttpResponse(ujson.dumps(nt_return),
                            content_type='application/json')