Example #1
0
def make_transaction(user: User, source_account_id: int,
                     target_account_id: int, amount: float, currency_id: int,
                     description: str, date: datetime) -> Transaction:

    transaction = Transaction(
        amount=amount,
        currency_id=currency_id,
        source_account_id=source_account_id,
        target_account_id=target_account_id,
        user=user,
        date=date,
        description=description,
    )
    db.session.add(transaction)

    source_record = Record(
        amount=0 - amount if amount > 0 else amount,
        record_type=Record.RECORD_TYPE_EXPENSE,
        currency_id=currency_id,
        account_id=source_account_id,
        user=user,
        transaction=transaction,
        date=date,
        description=description,
    )
    db.session.add(source_record)

    target_record = Record(
        amount=0 - amount if amount < 0 else amount,
        record_type=Record.RECORD_TYPE_INCOME,
        currency_id=currency_id,
        account_id=target_account_id,
        user=user,
        transaction=transaction,
        date=date,
        description=description,
    )
    db.session.add(target_record)

    db.session.commit()

    return transaction
Example #2
0
def edit_record(record_id):
    record = Record.query.filter(
        Record.id == record_id, Record.user_id == current_user.id
    ).first_or_404()

    record_schema = Record.from_json(json.loads(request.data.decode('utf-8')), partial=True)

    if record_schema.errors:
        return {'errors': record_schema.errors}, 400

    if record.transaction_id:
        return {'errors': {'record': 'Can not change transactional record.'}}, 400

    record_data = record_schema.data

    if record_data['record_type'] == Record.RECORD_TYPE_EXPENSE and record_data['amount'] > 0:
        record_data['amount'] = 0 - record_data['amount']

    if record_data['record_type'] == Record.RECORD_TYPE_INCOME and record_data['amount'] < 0:
        record_data['amount'] = 0 - record_data['amount']

    if 'account_id' in record_data:
        account = Account.query.filter(Account.id == record_data['account_id']).first()

        if not account:
            return {'errors': {'account': 'Account with this id does not exist'}}, 400

    if 'category_id' in record_data:
        category = GroupCategory.query.filter(
            GroupCategory.id == record_data['category_id']
        ).first()

        if not category:
            return {'errors': {'category': 'GroupCategory with this id does not exist'}}, 400

    if 'currency_id' in record_data:
        currency = GroupCurrency.query.filter(
            GroupCurrency.id == record_data['currency_id']
        ).first()

        if not currency:
            return {'errors': {'currency': 'Group currency with this id does not exist'}}, 400

    for field, value in record_data.items():
        if hasattr(record, field):
            setattr(record, field, value)

    record.user = current_user

    db.session.commit()

    return record, 200
Example #3
0
def add_record():
    record_schema = Record.from_json(json.loads(request.data.decode('utf-8')))

    if record_schema.errors:
        return {'errors': record_schema.errors}, 400

    record_data = record_schema.data
    if record_data['record_type'] == Record.RECORD_TYPE_EXPENSE and record_data['amount'] > 0:
        record_data['amount'] = 0 - record_data['amount']

    if record_data['record_type'] == Record.RECORD_TYPE_INCOME and record_data['amount'] < 0:
        record_data['amount'] = 0 - record_data['amount']

    account = Account.query.filter(Account.id == record_data['account_id']).first()

    if not account:
        return {'errors': {'account': 'Account with this id does not exist'}}, 400

    category = GroupCategory.query.filter(
        GroupCategory.id == record_data['category_id']
    ).first()

    if not category:
        return {'errors': {'category': 'GroupCategory with this id does not exist'}}, 400

    currency = GroupCurrency.query.filter(
        GroupCurrency.id == record_data['currency_id']
    ).first()

    if not currency:
        return {'errors': {'currency': 'Group currency with this id does not exist'}}, 400

    record = Record(**record_data)
    record.user = current_user

    db.session.add(record)
    db.session.commit()

    return record, 201