|
GnuCash 2.4.99
|
00001 #!/usr/bin/env python 00002 00003 # simple_invoice_insert.py -- Add an invoice to a set of books 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 # Opens a GnuCash book file and adds an invoice to it for a particular 00025 # customer (by GUID) with a specific ID and value 00026 # 00027 # The account tree and tax tables are assumed to be the same as the ones 00028 # created in simple_business_create.py, but you can edit that to adapt 00029 # this to become an invoice imported for your own books 00030 # 00031 # Syntax: 00032 # gnucash-env python simple_invoice_insert.py \ 00033 # /home/blah/blah.gnucash 00034 # dda2ec8e3e63c7715097f852851d6b22 1001 'The Goods' 201.43 00035 # 00036 # argv[1] should be the path to an existing gnucash file/database 00037 # for a file, simply pass the pathname, for a database you can use 00038 # these forms: 00039 # mysql://user:password@host/dbname 00040 # postgres://user:password@host[:port]/dbname (the port is optional) 00041 # 00042 00043 ## @file 00044 # @brief Add an invoice to a set of books 00045 # @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca> 00046 # @ingroup python_bindings_examples 00047 00048 from gnucash import Session, GUID, GncNumeric 00049 from gnucash.gnucash_business import Customer, Invoice, Entry 00050 from gnucash.gnucash_core_c import string_to_guid 00051 from os.path import abspath 00052 from sys import argv 00053 from decimal import Decimal 00054 import datetime 00055 00056 def gnc_numeric_from_decimal(decimal_value): 00057 sign, digits, exponent = decimal_value.as_tuple() 00058 00059 # convert decimal digits to a fractional numerator 00060 # equivlent to 00061 # numerator = int(''.join(digits)) 00062 # but without the wated conversion to string and back, 00063 # this is probably the same algorithm int() uses 00064 numerator = 0 00065 TEN = int(Decimal(0).radix()) # this is always 10 00066 numerator_place_value = 1 00067 # add each digit to the final value multiplied by the place value 00068 # from least significant to most sigificant 00069 for i in xrange(len(digits)-1,-1,-1): 00070 numerator += digits[i] * numerator_place_value 00071 numerator_place_value *= TEN 00072 00073 if decimal_value.is_signed(): 00074 numerator = -numerator 00075 00076 # if the exponent is negative, we use it to set the denominator 00077 if exponent < 0 : 00078 denominator = TEN ** (-exponent) 00079 # if the exponent isn't negative, we bump up the numerator 00080 # and set the denominator to 1 00081 else: 00082 numerator *= TEN ** exponent 00083 denominator = 1 00084 00085 return GncNumeric(numerator, denominator) 00086 00087 00088 s = Session(argv[1], is_new=False) 00089 # this seems to make a difference in more complex cases 00090 s.save() 00091 00092 book = s.book 00093 root = book.get_root_account() 00094 commod_table = book.get_table() 00095 CAD = commod_table.lookup('CURRENCY', 'CAD') 00096 00097 my_customer = book.LookupByID(arg[2]) 00098 assert( my_customer != None ) 00099 assert( isinstance(my_customer, Customer) ) 00100 00101 assets = root.lookup_by_name("Assets") 00102 recievables = assets.lookup_by_name("Recievables") 00103 income = root.lookup_by_name("Income") 00104 00105 invoice = Invoice(book, argv[3], CAD, my_customer ) 00106 description = argv[4] 00107 invoice_value = gnc_numeric_from_decimal(Decimal(argv[5])) 00108 tax_table = book.TaxTableLookupByName('good tax') 00109 invoice_entry = Entry(book, invoice) 00110 invoice_entry.SetInvTaxTable(tax_table) 00111 invoice_entry.SetInvTaxIncluded(False) 00112 invoice_entry.SetDescription(description) 00113 invoice_entry.SetQuantity( GncNumeric(1) ) 00114 invoice_entry.SetInvAccount(income) 00115 invoice_entry.SetInvPrice(invoice_value) 00116 00117 invoice.PostToAccount(recievables, datetime.date.today(), datetime.date.today(), 00118 "", True) 00119 00120 s.save() 00121 s.end()
1.7.4