GnuCash  5.6-150-g038405b370+
gncinvoicefkt.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
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  for result in query.run():
94  invoice_list.append(Invoice(instance=result))
95 
96  query.destroy()
97 
98  return invoice_list
99 
100 def get_all_customers(book):
101  """Returns all customers in book.
102 
103  Posts a query to search for all customers.
104 
105  arguments:
106  book the gnucash book to work with
107  """
108 
109  query = gnucash.Query()
110  query.search_for('gncCustomer')
111  query.set_book(book)
112 
113  customer_list = []
114 
115  for result in query.run():
116  customer_list.append(Customer(instance=result))
117 
118  query.destroy()
119 
120  return customer_list