GnuCash  5.6-150-g038405b370+
simple_invoice_insert.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # simple_invoice_insert.py -- Add an invoice to a set of books
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 # Opens a GnuCash book file and adds an invoice to it for a particular
25 # customer (by ID) with a specific ID and value
26 # Optionally also adds a payment for the invoice as well
27 #
28 # The account tree and tax tables are assumed to be the same as the ones
29 # created in simple_business_create.py, but you can edit that to adapt
30 # this to become an invoice importer for your own books
31 #
32 # Syntax:
33 # python3 simple_invoice_insert.py \
34 # /home/blah/blah.gnucash
35 # dda2ec8e3e63c7715097f852851d6b22 1001 'The Goods' 201.43
36 #
37 # argv[1] should be the path to an existing gnucash file/database
38 # for a file, simply pass the pathname, for a database you can use
39 # these forms:
40 # mysql://user:password@host/dbname
41 # postgres://user:password@host[:port]/dbname (the port is optional)
42 #
43 
44 
48 
49 from gnucash import Session, GUID, GncNumeric, SessionOpenMode
50 from gnucash.gnucash_business import Customer, Invoice, Entry
51 from gnucash.gnucash_core_c import string_to_guid
52 from os.path import abspath
53 from sys import argv
54 from decimal import Decimal
55 import datetime
56 
57 def gnc_numeric_from_decimal(decimal_value):
58  sign, digits, exponent = decimal_value.as_tuple()
59 
60  # convert decimal digits to a fractional numerator
61  # equivalent to
62  # numerator = int(''.join(digits))
63  # but without the wated conversion to string and back,
64  # this is probably the same algorithm int() uses
65  numerator = 0
66  TEN = int(Decimal(0).radix()) # this is always 10
67  numerator_place_value = 1
68  # add each digit to the final value multiplied by the place value
69  # from least significant to most significant
70  for i in range(len(digits)-1,-1,-1):
71  numerator += digits[i] * numerator_place_value
72  numerator_place_value *= TEN
73 
74  if decimal_value.is_signed():
75  numerator = -numerator
76 
77  # if the exponent is negative, we use it to set the denominator
78  if exponent < 0 :
79  denominator = TEN ** (-exponent)
80  # if the exponent isn't negative, we bump up the numerator
81  # and set the denominator to 1
82  else:
83  numerator *= TEN ** exponent
84  denominator = 1
85 
86  return GncNumeric(numerator, denominator)
87 
88 
89 s = Session(argv[1], SessionOpenMode.SESSION_NORMAL_OPEN)
90 
91 book = s.book
92 root = book.get_root_account()
93 commod_table = book.get_table()
94 CAD = commod_table.lookup('CURRENCY', 'CAD')
95 
96 my_customer = book.CustomerLookupByID(argv[2])
97 assert( my_customer != None )
98 assert( isinstance(my_customer, Customer) )
99 
100 assets = root.lookup_by_name("Assets")
101 receivables = assets.lookup_by_name("Receivables")
102 income = root.lookup_by_name("Income")
103 
104 invoice = Invoice(book, argv[3], CAD, my_customer )
105 description = argv[4]
106 invoice_value = gnc_numeric_from_decimal(Decimal(argv[5]))
107 tax_table = book.TaxTableLookupByName('good tax')
108 invoice_entry = Entry(book, invoice)
109 invoice_entry.SetInvTaxTable(tax_table)
110 invoice_entry.SetInvTaxIncluded(False)
111 invoice_entry.SetDescription(description)
112 invoice_entry.SetQuantity( GncNumeric(1) )
113 invoice_entry.SetInvAccount(income)
114 invoice_entry.SetInvPrice(invoice_value)
115 
116 invoice.PostToAccount(receivables, datetime.date.today(), datetime.date.today(),
117  "", True, False)
118 
119 s.save()
120 s.end()
The primary numeric class for representing amounts and values.
Definition: gnc-numeric.hpp:60