{
 "metadata": {
  "name": "",
  "signature": "sha256:bd67b7b484c91394b3b133ddde99ff9dc16989c4cedd102bf8ab990afa054d54"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import piecash\n",
      "import datetime"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# open a SQLAlchemy session linked to the test.gnucash file (as sqlite3 saved Book)\n",
      "s = piecash.connect_to_gnucash_book(\"test.gnucash\", readonly=False).get_session()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# retrieve the single Book object from the session (this is a sqlalchemy standard call)\n",
      "book = s.query(piecash.Book).one()\n",
      "# retrieve the EUR currency\n",
      "EUR = s.query(piecash.Commodity).one()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# from the book, retrieve the root account and display its children accounts\n",
      "book.root_account.children"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 4,
       "text": [
        "[Account<Assets>,\n",
        " Account<Liabilities>,\n",
        " Account<Income>,\n",
        " Account<Expenses>,\n",
        " Account<Equity>]"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# retrieve the standard 3 default assets accounts (checking account, saving account, cash in wallet)\n",
      "curacc, savacc, cash = book.root_account.children[0].children[0].children"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# check splits (they should be empty if the GnuCash book was an empty Book)\n",
      "savacc.splits, curacc.splits"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "([], [])"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# create a transaction of 45 \u20ac from the saving  account to the checking account\n",
      "tr = piecash.Transaction.single_transaction(datetime.date.today(),datetime.date.today(), \"transfer of money\", 45, EUR, savacc, curacc)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# check some attributes of the transaction\n",
      "tr.description, tr.splits"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "('transfer of money',\n",
        " [<Split Account<Assets:Current Assets:Savings Account> -45>,\n",
        "  <Split Account<Assets:Current Assets:Checking Account> 45>])"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# check the splits from the accounts point of view\n",
      "savacc.splits, curacc.splits"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "([<Split Account<Assets:Current Assets:Savings Account> -45>],\n",
        " [<Split Account<Assets:Current Assets:Checking Account> 45>])"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# rollback the session (i.e. undo all changes)\n",
      "s.rollback()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 10
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# check splits after the rollback (they should be unchanged)\n",
      "savacc.splits, curacc.splits"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 11,
       "text": [
        "([], [])"
       ]
      }
     ],
     "prompt_number": 11
    }
   ],
   "metadata": {}
  }
 ]
}