GnuCash 2.4.99
simple_business_create.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # simple_business_create.py -- Set up a set of books for business feature use
00004 #
00005 # Copyright (C) 2010 ParIT Worker Co-operative <transparency@parit.ca>
00006 # This program is free software; you can redistribute it and/or
00007 # modify it under the terms of the GNU General Public License as
00008 # published by the Free Software Foundation; either version 2 of
00009 # the License, or (at your option) any later version.
00010 #
00011 # This program is distributed in the hope that it will be useful,
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 # GNU General Public License for more details.
00015 #
00016 # You should have received a copy of the GNU General Public License
00017 # along with this program; if not, contact:
00018 # Free Software Foundation           Voice:  +1-617-542-5942
00019 # 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00020 # Boston, MA  02110-1301,  USA       gnu@gnu.org
00021 #
00022 # @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
00023 
00024 # Creates a new book file (or *overwrites* an existing one) that has elements
00025 # in it for business use -- intended as a demonstration program.
00026 # Syntax:
00027 # gnucash-env python simple_business_create.py \
00028 #                       sqlite3:///home/blah/blah.gnucash
00029 #
00030 # Specificically, this sets up a simple tree, creates a customer, job,
00031 # employee and vendor, creates an unposted invoice for each,
00032 # and posts the customer invoice with a few entries and a tax table.
00033 #
00034 # argv[1] should be the path the new or to overwrite gnucash file/database
00035 # for a file, simply pass the pathname prefixed with the requested data format
00036 # like:
00037 #   xml:///home/blah/blah.gnucash
00038 #   sqlite3:///home/blah/blah.gnucash
00039 # Paths can also be relative, for example:
00040 #   xml://from-here/to/there/blah.gnucash
00041 # For a database you can use these forms:
00042 #   mysql://user:password@host/dbname
00043 #   postgres://user:password@host[:port]/dbname (the port is optional)
00044 #
00045 # You may also want to look at simple_invoice_insert.py
00046 
00047 ##  @file
00048 #   @brief Set up a set of books for business feature use
00049 #   @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
00050 #   @ingroup python_bindings_examples
00051 
00052 from os.path import abspath
00053 from sys import argv, exit
00054 import datetime
00055 from datetime import timedelta
00056 from gnucash import Session, Account, GncNumeric
00057 from gnucash.gnucash_business import Customer, Employee, Vendor, Job, \
00058     Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \
00059     GNC_DISC_PRETAX    
00060 from gnucash.gnucash_core_c import \
00061     ACCT_TYPE_ASSET, ACCT_TYPE_RECEIVABLE, ACCT_TYPE_INCOME, \
00062     GNC_OWNER_CUSTOMER, ACCT_TYPE_LIABILITY
00063 
00064 if len(argv) < 2:
00065     print 'not enough parameters'
00066     print 'usage: simple_business_create.py {new_book_url}'
00067     print 'example:'
00068     print "gnucash-env python simple_business_create.py sqlite3:///home/blah/blah.gnucash"
00069     exit()
00070     
00071 
00072 s = Session(argv[1], is_new=True)
00073 
00074 try:
00075 
00076     book = s.book
00077     root = book.get_root_account()
00078     commod_table = book.get_table()
00079     CAD = commod_table.lookup('CURRENCY', 'CAD')
00080 
00081     a = Account(book)
00082     root.append_child(a)
00083     a.SetName('Assets')
00084     a.SetType(ACCT_TYPE_ASSET)
00085     a.SetCommodity(CAD)
00086 
00087     a2 = Account(book)
00088     a.append_child(a2)
00089     a2.SetName('Receivables')
00090     a2.SetType(ACCT_TYPE_RECEIVABLE)
00091     a2.SetCommodity(CAD)
00092 
00093     a3 = Account(book)
00094     root.append_child(a3)
00095     a3.SetName('Income')
00096     a3.SetType(ACCT_TYPE_INCOME)
00097     a3.SetCommodity(CAD)
00098 
00099     a4 = Account(book)
00100     root.append_child(a4)
00101     a4.SetName('Liabilities')
00102     a4.SetType(ACCT_TYPE_LIABILITY)
00103     a4.SetCommodity(CAD)
00104 
00105     a5 = Account(book)
00106     a4.append_child(a5)
00107     a5.SetName('Tax payable')
00108     a5.SetType(ACCT_TYPE_LIABILITY)
00109     a5.SetCommodity(CAD)
00110 
00111     a6 = Account(book)
00112     a.append_child(a6)
00113     a6.SetName('Bank')
00114     a6.SetType(ACCT_TYPE_ASSET)
00115     a6.SetCommodity(CAD)
00116 
00117     # name isn't required, ID and currency are
00118     new_customer = Customer(book, "1", CAD, "Bill & Bob Industries")
00119 
00120     # not required, but a good idea because the GUI insists on basic address info
00121     address = new_customer.GetAddr()
00122     address.SetName("Bill & Bob")
00123     address.SetAddr1("201 Nowhere street")
00124 
00125     new_employee = Employee(book, "2", CAD, "Reliable employee")
00126 
00127     new_vendor = Vendor(book, "3", CAD, "Dependable vendor")
00128 
00129     new_job = Job(book, "4", new_vendor, "Good clean, fun")
00130 
00131     # 7% tax
00132     tax_table = TaxTable(book, "good tax",
00133                          TaxTableEntry(a5, True, GncNumeric(700000, 100000) ) )
00134 
00135 
00136     invoice_customer = Invoice(book, "5", CAD, new_customer)
00137     customer_extract = invoice_customer.GetOwner()
00138     assert( isinstance(customer_extract, Customer) )
00139     assert( customer_extract.GetName() == new_customer.GetName() )
00140 
00141     invoice_employee = Invoice(book, "6", CAD, new_employee)
00142     employee_extract = invoice_employee.GetOwner()
00143     assert( isinstance(employee_extract, Employee) )
00144     assert( employee_extract.GetName() == new_employee.GetName() )
00145 
00146     invoice_vendor = Invoice(book, "7", CAD, new_vendor)
00147     vendor_extract = invoice_vendor.GetOwner()
00148     assert( isinstance(vendor_extract, Vendor) )
00149     assert( vendor_extract.GetName() == new_vendor.GetName() )
00150 
00151     invoice_job = Invoice(book, "8", CAD, new_job)
00152     job_extract = invoice_job.GetOwner()
00153     assert( isinstance(job_extract, Job) )
00154     assert( job_extract.GetName() == new_job.GetName() )
00155 
00156 
00157     invoice_entry = Entry(book, invoice_customer)
00158     invoice_entry.SetInvTaxTable(tax_table)
00159     invoice_entry.SetInvTaxIncluded(False)
00160     invoice_entry.SetDescription("excellent product")
00161     invoice_entry.SetQuantity( GncNumeric(1) )
00162     invoice_entry.SetInvAccount(a3)
00163     invoice_entry.SetInvPrice(GncNumeric(1) )
00164     invoice_entry.SetDateEntered(datetime.datetime.now())
00165 
00166     invoice_customer.PostToAccount(a2, datetime.date.today(), datetime.date.today(),
00167                                    "the memo", True)
00168 
00169     new_customer.ApplyPayment(None, None, a2, a6, GncNumeric(100,100),
00170                               GncNumeric(1), datetime.date.today(), "", "")
00171 
00172     invoice_customer.ApplyPayment(None, a6, GncNumeric(7,100),
00173                                   GncNumeric(1), datetime.date.today(), "", "")
00174 
00175     vendor_bill_returns = book.BillLoookupByID("7")
00176     assert( vendor_bill_returns.GetID() == "7" )
00177     vendor_extract = vendor_bill_returns.GetOwner()
00178     assert( vendor_extract.GetName() == new_vendor.GetName() )
00179     customer_invoice_returns = book.InvoiceLookupByID("5")
00180     assert( customer_invoice_returns.GetID() == "5" )
00181     customer_returns = book.CustomerLookupByID("1")
00182     assert( customer_returns.GetName() == new_customer.GetName() )
00183 
00184     s.save()
00185 
00186     s.end()
00187 except:
00188     if not s == None:
00189         s.end()
00190     raise
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines