Files
gnucash/bindings/python/example_scripts/gncinvoicefkt.py
T
Noah R 592b57ddbb [python-bindings] Fix missing return-type wrapping and clean up examples
Many Python binding methods silently returned raw SwigPyObject pointers
instead of proper Python wrapper objects because the methods_return_instance
dicts were incomplete. This adds the missing entries and activates the
DeprecationWarning in the SWIG typemap fallback path from PR 1.

Return-type wrapping fixes (gnucash_core.py):

  GncPrice (new dict — no wrapping existed before):
    get_commodity → GncCommodity, get_currency → GncCommodity,
    clone → GncPrice, get_value → GncNumeric

  GncPriceDB (added to existing dict):
    nth_price → GncPrice, lookup_day_t64 → GncPrice,
    convert_balance_nearest_before_price_t64 → GncNumeric
  GncPriceDB (new list wrapping):
    lookup_latest_any_currency → list[GncPrice],
    lookup_nearest_before_any_currency_t64 → list[GncPrice],
    lookup_nearest_in_time_any_currency_t64 → list[GncPrice]

  GncCommodity: obtain_twin → GncCommodity,
                get_namespace_ds → GncCommodityNamespace
  Account: get_currency_or_parent → GncCommodity,
           GetLotList → list[GncLot]
  GncLot: get_split_list → list[Split]

Example script cleanup:
  Remove type(x).__name__ == 'SwigPyObject' workarounds from
  gnc_convenience.py, gncinvoicefkt.py, str_methods.py — these are
  no longer needed now that methods return proper wrapper objects.

SWIG typemap (gnucash_core.i):
  Activate DeprecationWarning on the .instance fallback path, replacing
  the TODO placeholder from the typemap compatibility layer commit.

New test file (test_price_and_wrapping.py):
  18 tests covering all wrapping fixes and typemap compatibility,
  using in-repo test data (pricedb1.gml2, sample1.gnucash).
  Tests auto-skip when XML backend is unavailable.
2026-03-06 12:53:56 -08:00

116 lines
3.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
##@file
# @brief some help for working with invoices, used in \ref py_invoice_export
# @ingroup python_bindings_examples
# @author Christoph Holtermann (c.holtermann (at) gmx.de)
# @date 2014-11
#
# @details
# Credits to Tom Loft for the query to get_all_invoices
# as used in his REST-Api
#
# Issues:
# - get_all_invoices could be added as a method to book Class
# - get_all_customers should be a query like get_all_invoices
try:
import gnucash
from gnucash.gnucash_business import Customer, Employee, Vendor, Job, \
Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \
GNC_DISC_PRETAX
import str_methods
except ImportError as import_error:
print("Problem importing modules.")
print(import_error)
sys.exit(2)
def get_all_lots(account):
"""Return all lots in account and descendants"""
ltotal=[]
descs = account.get_descendants()
for desc in descs:
ll=desc.GetLotList()
ltotal+=ll
return ltotal
def get_all_invoices_from_lots(account):
"""Return all invoices in account and descendants
This is based on lots. So invoices without lots will be missed."""
lot_list=get_all_lots(account)
invoice_list=[]
for lot in lot_list:
invoice=gnucash.gnucash_core_c.gncInvoiceGetInvoiceFromLot(lot.instance)
if invoice:
invoice_list.append(Invoice(instance=invoice))
return invoice_list
def get_all_invoices(book, is_paid=None, is_active=None):
"""Returns a list of all invoices in the book.
Posts a query to search for all invoices.
arguments:
book the gnucash book to work with
keyword-arguments:
is_paid int 1 to search for invoices having been paid, 0 for not, None to ignore.
is_active int 1 to search for active invoices
"""
query = gnucash.Query()
query.search_for('gncInvoice')
query.set_book(book)
if is_paid == 0:
query.add_boolean_match([gnucash.INVOICE_IS_PAID], False, gnucash.QOF_QUERY_AND)
elif is_paid == 1:
query.add_boolean_match([gnucash.INVOICE_IS_PAID], True, gnucash.QOF_QUERY_AND)
elif is_paid == None:
pass
# active = JOB_IS_ACTIVE
if is_active == 0:
query.add_boolean_match(['active'], False, gnucash.QOF_QUERY_AND)
elif is_active == 1:
query.add_boolean_match(['active'], True, gnucash.QOF_QUERY_AND)
elif is_active == None:
pass
# return only invoices (1 = invoices)
pred_data = gnucash.gnucash_core.QueryInt32Predicate(gnucash.QOF_COMPARE_EQUAL, 1)
query.add_term([gnucash.INVOICE_TYPE], pred_data, gnucash.QOF_QUERY_AND)
invoice_list = []
for result in query.run():
invoice_list.append(Invoice(instance=result))
query.destroy()
return invoice_list
def get_all_customers(book):
"""Returns all customers in book.
Posts a query to search for all customers.
arguments:
book the gnucash book to work with
"""
query = gnucash.Query()
query.search_for('gncCustomer')
query.set_book(book)
customer_list = []
for result in query.run():
customer_list.append(Customer(instance=result))
query.destroy()
return customer_list