Ejemplo n.º 1
0
def test_insert_transaction(tmpdir):
    file_content = dedent("""
        2016-02-26 * "Uncle Boons" "Eating out alone"
            Liabilities:US:Chase:Slate                       -24.84 USD
            Expenses:Food:Restaurant                          24.84 USD
        ; FAVA-INSERT-MARKER
    """)
    samplefile = tmpdir.mkdir('fava_util_file3').join('example.beancount')
    samplefile.write(file_content)

    transaction = data.Transaction(
        None, datetime.date(2016, 1, 1), '*', 'payee', 'narr', None, None, [])

    insert_transaction(transaction, [str(samplefile)])
    assert samplefile.read() == dedent("""
        2016-02-26 * "Uncle Boons" "Eating out alone"
            Liabilities:US:Chase:Slate                       -24.84 USD
            Expenses:Food:Restaurant                          24.84 USD

        2016-01-01 * "payee" "narr"
        ; FAVA-INSERT-MARKER
    """)

    postings = [
        data.Posting('Liabilities:US:Chase:Slate',
                     amount.Amount(D('-10.00'), 'USD'),
                     None, None, None, None),
        data.Posting('Expenses:Food',
                     amount.Amount(D('10.00'), 'USD'),
                     None, None, None, None),
    ]

    transaction = data.Transaction(
        None, datetime.date(2016, 1, 1), '*',
        'new payee', 'narr', None, None, postings)

    insert_transaction(transaction, [str(samplefile)])
    assert samplefile.read() == dedent("""
        2016-02-26 * "Uncle Boons" "Eating out alone"
            Liabilities:US:Chase:Slate                       -24.84 USD
            Expenses:Food:Restaurant                          24.84 USD

        2016-01-01 * "payee" "narr"

        2016-01-01 * "new payee" "narr"
            Liabilities:US:Chase:Slate                    -10.00 USD
            Expenses:Food                                  10.00 USD
        ; FAVA-INSERT-MARKER
    """)
Ejemplo n.º 2
0
def api_add_transaction():
    json = request.get_json()

    postings = []
    for posting in json['postings']:
        if posting['account'] not in g.api.all_accounts_active:
            return _api_error('Unknown account: {}.'
                              .format(posting['account']))
        number_ = D(posting['number']) if posting['number'] else None
        amount_ = amount.Amount(number_, posting.get('currency'))
        postings.append(data.Posting(posting['account'], amount_,
                                     None, None, None, None))

    if not postings:
        return _api_error('Transaction contains no postings.')

    date = util.date.parse_date(json['date'])[0]
    transaction = data.Transaction(
        None, date, json['flag'], json['payee'],
        json['narration'], None, None, postings)

    insert_transaction(transaction, g.api.source_files())
    return _api_success(message='Stored transaction.')