GnuCash  4.11-243-g1cac132214+
simple_business_create.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # simple_business_create.py -- Set up a set of books for business feature use
4 #
5 # Copyright (C) 2010 ParIT Worker Co-operative <transparency@parit.ca>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of
9 # the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, contact:
18 # Free Software Foundation Voice: +1-617-542-5942
19 # 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
20 # Boston, MA 02110-1301, USA gnu@gnu.org
21 #
22 # @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
23 
24 # Creates a new book file (or *overwrites* an existing one) that has elements
25 # in it for business use -- intended as a demonstration program.
26 # Syntax:
27 # python3 simple_business_create.py \
28 # sqlite3:///home/blah/blah.gnucash
29 #
30 # Specifically, this sets up a simple tree, creates a customer, job,
31 # employee and vendor, creates an unposted invoice for each,
32 # and posts the customer invoice with a few entries and a tax table.
33 #
34 # argv[1] should be the path the new or to overwrite gnucash file/database
35 # for a file, simply pass the pathname prefixed with the requested data format
36 # like:
37 # xml:///home/blah/blah.gnucash
38 # sqlite3:///home/blah/blah.gnucash
39 # Paths can also be relative, for example:
40 # xml://from-here/to/there/blah.gnucash
41 # For a database you can use these forms:
42 # mysql://user:password@host/dbname
43 # postgres://user:password@host[:port]/dbname (the port is optional)
44 #
45 # You may also want to look at simple_invoice_insert.py
46 
47 
51 
52 from os.path import abspath
53 from sys import argv, exit
54 import datetime
55 from datetime import timedelta
56 from gnucash import Session, Account, GncNumeric, SessionOpenMode
57 from gnucash.gnucash_business import Customer, Employee, Vendor, Job, \
58  Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \
59  GNC_DISC_PRETAX
60 from gnucash.gnucash_core_c import \
61  ACCT_TYPE_ASSET, ACCT_TYPE_RECEIVABLE, ACCT_TYPE_INCOME, \
62  GNC_OWNER_CUSTOMER, ACCT_TYPE_LIABILITY
63 
64 if len(argv) < 2:
65  print('not enough parameters')
66  print('usage: simple_business_create.py {new_book_url}')
67  print('example:')
68  print("python3 simple_business_create.py sqlite3:///home/blah/blah.gnucash")
69  exit()
70 
71 
72 try:
73  s = Session(argv[1], SessionOpenMode.SESSION_NEW_STORE)
74 
75  book = s.book
76  root = book.get_root_account()
77  commod_table = book.get_table()
78  CAD = commod_table.lookup('CURRENCY', 'CAD')
79 
80  a = Account(book)
81  root.append_child(a)
82  a.SetName('Assets')
83  a.SetType(ACCT_TYPE_ASSET)
84  a.SetCommodity(CAD)
85 
86  a2 = Account(book)
87  a.append_child(a2)
88  a2.SetName('Receivables')
89  a2.SetType(ACCT_TYPE_RECEIVABLE)
90  a2.SetCommodity(CAD)
91 
92  a3 = Account(book)
93  root.append_child(a3)
94  a3.SetName('Income')
95  a3.SetType(ACCT_TYPE_INCOME)
96  a3.SetCommodity(CAD)
97 
98  a4 = Account(book)
99  root.append_child(a4)
100  a4.SetName('Liabilities')
101  a4.SetType(ACCT_TYPE_LIABILITY)
102  a4.SetCommodity(CAD)
103 
104  a5 = Account(book)
105  a4.append_child(a5)
106  a5.SetName('Tax payable')
107  a5.SetType(ACCT_TYPE_LIABILITY)
108  a5.SetCommodity(CAD)
109 
110  a6 = Account(book)
111  a.append_child(a6)
112  a6.SetName('Bank')
113  a6.SetType(ACCT_TYPE_ASSET)
114  a6.SetCommodity(CAD)
115 
116  # name isn't required, ID and currency are
117  new_customer = Customer(book, "1", CAD, "Bill & Bob Industries")
118 
119  # not required, but a good idea because the GUI insists on basic address info
120  address = new_customer.GetAddr()
121  address.SetName("Bill & Bob")
122  address.SetAddr1("201 Nowhere street")
123 
124  new_employee = Employee(book, "2", CAD, "Reliable employee")
125 
126  new_vendor = Vendor(book, "3", CAD, "Dependable vendor")
127 
128  new_job = Job(book, "4", new_vendor, "Good clean, fun")
129 
130  # 7% tax
131  tax_table = TaxTable(book, "good tax",
132  TaxTableEntry(a5, True, GncNumeric(700000, 100000) ) )
133 
134 
135  invoice_customer = Invoice(book, "5", CAD, new_customer)
136  customer_extract = invoice_customer.GetOwner()
137  assert( isinstance(customer_extract, Customer) )
138  assert( customer_extract.GetName() == new_customer.GetName() )
139 
140  invoice_employee = Invoice(book, "6", CAD, new_employee)
141  employee_extract = invoice_employee.GetOwner()
142  assert( isinstance(employee_extract, Employee) )
143  assert( employee_extract.GetName() == new_employee.GetName() )
144 
145  invoice_vendor = Invoice(book, "7", CAD, new_vendor)
146  vendor_extract = invoice_vendor.GetOwner()
147  assert( isinstance(vendor_extract, Vendor) )
148  assert( vendor_extract.GetName() == new_vendor.GetName() )
149 
150  invoice_job = Invoice(book, "8", CAD, new_job)
151  job_extract = invoice_job.GetOwner()
152  assert( isinstance(job_extract, Job) )
153  assert( job_extract.GetName() == new_job.GetName() )
154 
155 
156  invoice_entry = Entry(book, invoice_customer)
157  invoice_entry.SetInvTaxTable(tax_table)
158  invoice_entry.SetInvTaxIncluded(False)
159  invoice_entry.SetDescription("excellent product")
160  invoice_entry.SetQuantity( GncNumeric(1) )
161  invoice_entry.SetInvAccount(a3)
162  invoice_entry.SetInvPrice(GncNumeric(1) )
163  invoice_entry.SetDateEntered(datetime.datetime.now())
164 
165  invoice_customer.PostToAccount(a2, datetime.date.today(), datetime.date.today(),
166  "the memo", True, False)
167 
168  new_customer.ApplyPayment(None, None, a2, a6, GncNumeric(100,100),
169  GncNumeric(1), datetime.date.today(), "", "", True)
170 
171  invoice_customer.ApplyPayment(None, a6, GncNumeric(7,100),
172  GncNumeric(1), datetime.date.today(), "", "")
173 
174  vendor_bill_returns = book.BillLookupByID("7")
175  assert( vendor_bill_returns.GetID() == "7" )
176  vendor_extract = vendor_bill_returns.GetOwner()
177  assert( vendor_extract.GetName() == new_vendor.GetName() )
178  customer_invoice_returns = book.InvoiceLookupByID("5")
179  assert( customer_invoice_returns.GetID() == "5" )
180  customer_returns = book.CustomerLookupByID("1")
181  assert( customer_returns.GetName() == new_customer.GetName() )
182 
183  s.save()
184 
185  s.end()
186 except:
187  if "s" in locals():
188  s.end()
189  raise
The primary numeric class for representing amounts and values.
Definition: gnc-numeric.hpp:59