GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gncinvoicefkt.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 
4 ##@file
5 # @brief some help for working with invoices, used in \ref py_invoice_export
6 # @ingroup python_bindings_examples
7 # @author Christoph Holtermann (c.holtermann (at) gmx.de)
8 # @date 2014-11
9 #
10 # @details
11 # Credits to Tom Loft for the query to get_all_invoices
12 # as used in his REST-Api
13 #
14 # Issues:
15 # - get_all_invoices could be added as a method to book Class
16 # - get_all_customers should be a query like get_all_invoices
17 
18 try:
19  import gnucash
20  from gnucash.gnucash_business import Customer, Employee, Vendor, Job, \
21  Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \
22  GNC_DISC_PRETAX
23  import str_methods
24 except ImportError as import_error:
25  print "Problem importing modules."
26  print import_error
27  sys.exit(2)
28 
29 def get_all_lots(account):
30  """Return all lots in account and descendants"""
31  ltotal=[]
32  descs = account.get_descendants()
33  for desc in descs:
34  if type(desc).__name__ == 'SwigPyObject':
35  desc = gnucash.Account(instance=desc)
36  ll=desc.GetLotList()
37  ltotal+=ll
38  return ltotal
39 
40 def get_all_invoices_from_lots(account):
41  """Return all invoices in account and descendants
42 
43  This is based on lots. So invoices without lots will be missed."""
44 
45  lot_list=get_all_lots(account)
46  invoice_list=[]
47  for lot in lot_list:
48  if type(lot).__name__ == 'SwigPyObject':
49  lot = gnucash.GncLot(instance=lot)
50 
51  invoice=gnucash.gnucash_core_c.gncInvoiceGetInvoiceFromLot(lot.instance)
52  if invoice:
53  invoice_list.append(Invoice(instance=invoice))
54  return invoice_list
55 
56 def get_all_invoices(book, is_paid=None, is_active=None):
57  """Returns a list of all invoices in the book.
58 
59  posts a query to search for all invoices.
60 
61  arguments:
62  book the gnucash book to work with
63  keyword-arguments:
64  is_paid int 1 to search for invoices having been paid, 0 for not, None to ignore.
65  is_active int 1 to search for active invoices
66  """
67 
68  query = gnucash.Query()
69  query.search_for('gncInvoice')
70  query.set_book(book)
71 
72  if is_paid == 0:
73  query.add_boolean_match([gnucash.INVOICE_IS_PAID], False, gnucash.QOF_QUERY_AND)
74  elif is_paid == 1:
75  query.add_boolean_match([gnucash.INVOICE_IS_PAID], True, gnucash.QOF_QUERY_AND)
76  elif is_paid == None:
77  pass
78 
79  # active = JOB_IS_ACTIVE
80  if is_active == 0:
81  query.add_boolean_match(['active'], False, gnucash.QOF_QUERY_AND)
82  elif is_active == 1:
83  query.add_boolean_match(['active'], True, gnucash.QOF_QUERY_AND)
84  elif is_active == None:
85  pass
86 
87  # return only invoices (1 = invoices)
88  pred_data = gnucash.gnucash_core.QueryInt32Predicate(gnucash.QOF_COMPARE_EQUAL, 1)
89  query.add_term([gnucash.INVOICE_TYPE], pred_data, gnucash.QOF_QUERY_AND)
90 
91  invoice_list = []
92 
93  result = query.run()
94  for result in query.run():
95  invoice_list.append(Invoice(instance=result))
96 
97  query.destroy()
98 
99  return invoice_list
100 
101 def get_all_customers(book):
102  """Returns all customers in book.
103 
104  Counts IDs upwards. May miss customers with irregular IDs.
105  Should be replaced by query as in get_all_invoices."""
106 
107  customer_list = []
108  customer = True
109  customer_id = 0
110  while customer:
111  customer_id += 1
112  customer = book.CustomerLookupByID('%06d' % customer_id)
113  if customer:
114  customer_list.append(customer)
115 
116  return customer_list