GnuCash c935c2f+
Loading...
Searching...
No Matches
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
49from gnucash import Session, GUID, GncNumeric, SessionOpenMode
50from gnucash.gnucash_business import Customer, Invoice, Entry
51from gnucash.gnucash_core_c import string_to_guid
52from os.path import abspath
53from sys import argv
54from decimal import Decimal
55import datetime
56
57def 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
89s = Session(argv[1], SessionOpenMode.SESSION_NORMAL_OPEN)
90
91book = s.book
92root = book.get_root_account()
93commod_table = book.get_table()
94CAD = commod_table.lookup('CURRENCY', 'CAD')
95
96my_customer = book.CustomerLookupByID(argv[2])
97assert( my_customer != None )
98assert( isinstance(my_customer, Customer) )
99
100assets = root.lookup_by_name("Assets")
101receivables = assets.lookup_by_name("Receivables")
102income = root.lookup_by_name("Income")
103
104invoice = Invoice(book, argv[3], CAD, my_customer )
105description = argv[4]
106invoice_value = gnc_numeric_from_decimal(Decimal(argv[5]))
107tax_table = book.TaxTableLookupByName('good tax')
108invoice_entry = Entry(book, invoice)
109invoice_entry.SetInvTaxTable(tax_table)
110invoice_entry.SetInvTaxIncluded(False)
111invoice_entry.SetDescription(description)
112invoice_entry.SetQuantity( GncNumeric(1) )
113invoice_entry.SetInvAccount(income)
114invoice_entry.SetInvPrice(invoice_value)
115
116invoice.PostToAccount(receivables, datetime.date.today(), datetime.date.today(),
117 "", True, False)
118
119s.save()
120s.end()
The primary numeric class for representing amounts and values.