GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnucash_simple.py
1 '''
2 
3 gnucash_simple.py -- A helper file to convert Gnucash objects into
4 dictionaries for easier conversion to JSON
5 
6 Copyright (C) 2013 Tom Lofts <[email protected]>
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, contact:
20 
21 Free Software Foundation Voice: +1-617-542-5942
22 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
23 Boston, MA 02110-1301, USA [email protected]
24 
25 @author Tom Lofts <[email protected]>
26 
27 '''
28 
29 import gnucash
30 from gnucash.gnucash_business import Entry, Split, Account
31 
32 def addressToDict(address):
33  if address is None:
34  return None
35  else:
36  simple_address = {}
37  simple_address['name'] = address.GetName();
38  simple_address['line_1'] = address.GetAddr1();
39  simple_address['line_2'] = address.GetAddr2();
40  simple_address['line_3'] = address.GetAddr3();
41  simple_address['line_4'] = address.GetAddr4();
42  simple_address['phone'] = address.GetPhone();
43  simple_address['fax'] = address.GetFax();
44  simple_address['email'] = address.GetEmail();
45 
46  return simple_address
47 
48 def vendorToDict(vendor):
49 
50  if vendor is None:
51  return None
52  else:
53  simple_vendor = {}
54  simple_vendor['name'] = vendor.GetName()
55  simple_vendor['id'] = vendor.GetID()
56  simple_vendor['guid'] = vendor.GetGUID().to_string()
57  simple_vendor['notes'] = vendor.GetNotes()
58  simple_vendor['active'] = vendor.GetActive()
59  simple_vendor['currency'] = vendor.GetCurrency().get_mnemonic()
60  simple_vendor['tax_table_override'] = vendor.GetTaxTableOverride()
61  simple_vendor['address'] = addressToDict(vendor.GetAddr())
62  simple_vendor['tax_included'] = vendor.GetTaxIncluded()
63 
64  return simple_vendor
65 
66 def customerToDict(customer):
67 
68  if customer is None:
69  return None
70  else:
71  simple_customer = {}
72  simple_customer['name'] = customer.GetName()
73  simple_customer['id'] = customer.GetID()
74  simple_customer['guid'] = customer.GetGUID().to_string()
75  simple_customer['notes'] = customer.GetNotes()
76  simple_customer['active'] = customer.GetActive()
77  simple_customer['discount'] = customer.GetDiscount().to_double()
78  simple_customer['credit'] = customer.GetCredit().to_double()
79  simple_customer['currency'] = customer.GetCurrency().get_mnemonic()
80  simple_customer['tax_table_override'] = customer.GetTaxTableOverride()
81  simple_customer['address'] = addressToDict(customer.GetAddr())
82  simple_customer['shipping_address'] = addressToDict(
83  customer.GetShipAddr())
84  simple_customer['tax_included'] = customer.GetTaxIncluded()
85 
86  return simple_customer
87 
88 def transactionToDict(transaction, entities):
89  if transaction is None:
90  return None
91  else:
92  simple_transaction = {}
93  simple_transaction['guid'] = transaction.GetGUID().to_string()
94  simple_transaction['num'] = transaction.GetNum()
95  simple_transaction['notes'] = transaction.GetNotes()
96  simple_transaction['is_closing_txn'] = transaction.GetIsClosingTxn()
97 
98  if 'splits' in entities:
99  simple_transaction['splits'] = []
100  for split in transaction.GetSplitList():
101  if type(split) != Split:
102  split=Split(instance=split)
103  simple_transaction['splits'].append(
104  splitToDict(split, ['account']))
105 
106  simple_transaction['count_splits'] = transaction.CountSplits()
107  simple_transaction['has_reconciled_splits'] = \
108  transaction.HasReconciledSplits()
109  simple_transaction['currency'] = transaction.GetCurrency(
110  ).get_mnemonic()
111  simple_transaction['imbalance_value'] = transaction.GetImbalanceValue(
112  ).to_double()
113  simple_transaction['is_balanced'] = transaction.IsBalanced()
114  simple_transaction['date'] = transaction.GetDate()
115  simple_transaction['date_posted'] = transaction.RetDatePostedTS(
116  ).strftime('%Y-%m-%d')
117  simple_transaction['date_entered'] = transaction.RetDateEnteredTS(
118  ).strftime('%Y-%m-%d')
119  simple_transaction['date_due'] = transaction.RetDateDueTS().strftime(
120  '%Y-%m-%d')
121  simple_transaction['void_status'] = transaction.GetVoidStatus()
122  simple_transaction['void_time'] = transaction.GetVoidTime().strftime(
123  '%Y-%m-%d')
124 
125  simple_transaction['description'] = transaction.GetDescription()
126 
127  return simple_transaction
128 
129 def splitToDict(split, entities):
130  if split is None:
131  return None
132  else:
133  simple_split = {}
134  simple_split['guid'] = split.GetGUID().to_string()
135  if 'account' in entities:
136  simple_split['account'] = accountToDict(split.GetAccount())
137  if 'transaction' in entities:
138  simple_split['transaction'] = transactionToDict(
139  split.GetParent(), [])
140  if 'other_split' in entities:
141  simple_split['other_split'] = splitToDict(
142  split.GetOtherSplit(), ['account'])
143  simple_split['amount'] = split.GetAmount().to_double()
144  simple_split['value'] = split.GetValue().to_double()
145  simple_split['balance'] = split.GetBalance().to_double()
146  simple_split['cleared_balance'] = split.GetClearedBalance().to_double()
147  simple_split['reconciled_balance'] = split.GetReconciledBalance(
148  ).to_double()
149 
150  return simple_split
151 def invoiceToDict(invoice):
152 
153  if invoice is None:
154  return None
155  else:
156  simple_invoice = {}
157  simple_invoice['id'] = invoice.GetID()
158  simple_invoice['type'] = invoice.GetType()
159  simple_invoice['date_opened'] = invoice.GetDateOpened().strftime(
160  '%Y-%m-%d')
161  if invoice.GetDatePosted().strftime('%Y-%m-%d') == '1970-01-01':
162  simple_invoice['date_posted'] = None
163  else:
164  simple_invoice['date_posted'] = invoice.GetDatePosted().strftime(
165  '%Y-%m-%d')
166  if invoice.GetDateDue().strftime('%Y-%m-%d') == '1970-01-01':
167  simple_invoice['date_due'] = None
168  else:
169  simple_invoice['date_due'] = invoice.GetDateDue().strftime(
170  '%Y-%m-%d')
171  simple_invoice['notes'] = invoice.GetNotes()
172  simple_invoice['active'] = invoice.GetActive()
173  simple_invoice['currency'] = invoice.GetCurrency().get_mnemonic()
174  simple_invoice['owner'] = vendorToDict(invoice.GetOwner())
175  simple_invoice['owner_type'] = invoice.GetOwnerType()
176  simple_invoice['billing_id'] = invoice.GetBillingID()
177  simple_invoice['to_charge_amount'] = invoice.GetToChargeAmount().to_double()
178  simple_invoice['posted_txn'] = transactionToDict(invoice.GetPostedTxn(), [])
179  simple_invoice['total'] = invoice.GetTotal().to_double()
180  simple_invoice['total_subtotal'] = invoice.GetTotalSubtotal(
181  ).to_double()
182  simple_invoice['total_tax'] = invoice.GetTotalTax().to_double()
183 
184  simple_invoice['entries'] = []
185  for n, entry in enumerate(invoice.GetEntries()):
186  if type(entry) != Entry:
187  entry=Entry(instance=entry)
188  simple_invoice['entries'].append(entryToDict(entry))
189 
190  simple_invoice['posted'] = invoice.IsPosted()
191  simple_invoice['paid'] = invoice.IsPaid()
192 
193  return simple_invoice
194 
195 def billToDict(bill):
196 
197  if bill is None:
198  return None
199  else:
200  simple_bill = {}
201  simple_bill['id'] = bill.GetID()
202  simple_bill['type'] = bill.GetType()
203  simple_bill['date_opened'] = bill.GetDateOpened().strftime('%Y-%m-%d')
204  if bill.GetDatePosted().strftime('%Y-%m-%d') == '1970-01-01':
205  simple_bill['date_posted'] = None
206  else:
207  simple_bill['date_posted'] = bill.GetDatePosted().strftime(
208  '%Y-%m-%d')
209  if bill.GetDateDue().strftime('%Y-%m-%d') == '1970-01-01':
210  simple_bill['date_due'] = None
211  else:
212  simple_bill['date_due'] = bill.GetDateDue().strftime('%Y-%m-%d')
213  simple_bill['notes'] = bill.GetNotes()
214  simple_bill['active'] = bill.GetActive()
215  simple_bill['currency'] = bill.GetCurrency().get_mnemonic()
216  simple_bill['owner'] = vendorToDict(bill.GetOwner())
217  simple_bill['owner_type'] = bill.GetOwnerType()
218  simple_bill['billing_id'] = bill.GetBillingID()
219  simple_bill['to_charge_amount'] = bill.GetToChargeAmount().to_double()
220  simple_bill['total'] = bill.GetTotal().to_double()
221  simple_bill['total_subtotal'] = bill.GetTotalSubtotal().to_double()
222  simple_bill['total_tax'] = bill.GetTotalTax().to_double()
223 
224  simple_bill['entries'] = []
225  for n, entry in enumerate(bill.GetEntries()):
226  if type(entry) != Entry:
227  entry=Entry(instance=entry)
228  simple_bill['entries'].append(entryToDict(entry))
229 
230  simple_bill['posted'] = bill.IsPosted()
231  simple_bill['paid'] = bill.IsPaid()
232 
233  return simple_bill
234 
235 def entryToDict(entry):
236 
237  if entry is None:
238  return None
239  else:
240 
241  simple_entry = {}
242  simple_entry['guid'] = entry.GetGUID().to_string()
243  simple_entry['date'] = entry.GetDate().strftime('%Y-%m-%d')
244  simple_entry['date_entered'] = entry.GetDateEntered().strftime(
245  '%Y-%m-%d')
246  simple_entry['description'] = entry.GetDescription()
247  simple_entry['action'] = entry.GetAction()
248  simple_entry['notes'] = entry.GetNotes()
249  simple_entry['quantity'] = entry.GetQuantity().to_double()
250  if entry.GetInvAccount() == None:
251  simple_entry['inv_account'] = {}
252  else:
253  simple_entry['inv_account'] = accountToDict(entry.GetInvAccount())
254  simple_entry['inv_price'] = entry.GetInvPrice().to_double()
255  simple_entry['discount'] = entry.GetInvDiscount().to_double()
256  simple_entry['discounted_type'] = entry.GetInvDiscountType()
257  simple_entry['discounted_how'] = entry.GetInvDiscountHow()
258  simple_entry['inv_taxable'] = entry.GetInvTaxable()
259  simple_entry['inv_tax_included'] = entry.GetInvTaxIncluded()
260  simple_entry['inv_tax_table_override'] = entry.GetInvTaxTable()
261  if entry.GetBillAccount() == None:
262  simple_entry['bill_account'] = {}
263  else:
264  simple_entry['bill_account'] = accountToDict(
265  entry.GetBillAccount())
266  simple_entry['bill_price'] = entry.GetBillPrice().to_double()
267  simple_entry['bill_taxable'] = entry.GetBillTaxable()
268  simple_entry['bill_tax_included'] = entry.GetBillTaxIncluded()
269  simple_entry['bill_tax_table'] = entry.GetBillTaxTable()
270  simple_entry['billable'] = entry.GetBillable()
271  simple_entry['bill_payment'] = entry.GetBillPayment()
272  simple_entry['is_open'] = entry.IsOpen()
273 
274  return simple_entry
275 
276 
277 def accountToDict(account):
278 
279  commod_table = account.get_book().get_table()
280  gbp = commod_table.lookup('CURRENCY', 'GBP')
281 
282  if account is None:
283  return None
284  else:
285  simple_account = {}
286  simple_account['name'] = account.GetName()
287  simple_account['type_id'] = account.GetType()
288  simple_account['description'] = account.GetDescription()
289  simple_account['guid'] = account.GetGUID().to_string()
290  if account.GetCommodity() == None:
291  simple_account['currency'] = ''
292  else:
293  simple_account['currency'] = account.GetCommodity().get_mnemonic()
294  simple_account['subaccounts'] = []
295  for n, subaccount in enumerate(account.get_children_sorted()):
296  simple_account['subaccounts'].append(accountToDict(subaccount))
297 
298  simple_account['balance'] = account.GetBalance().to_double()
299  simple_account['balance_gbp'] = account.GetBalanceInCurrency(
300  gbp, True).to_double()
301  simple_account['placeholder'] = account.GetPlaceholder()
302 
303  return simple_account
struct split_s Split
Split in Gnucash. A "split" is more commonly referred to as a "entry" in a "transaction". Each split belongs to one Account and one Transaction. The split is one out of several parts a Transaction is divided into.
Definition: gnc-engine.h:144