11 from gnucash
import Session, Account, Transaction, Split
15 def get_transaction_list(account):
16 """Returns all transactions in account. 18 Splits are derived from account.GetSplitList(). 22 account: Account to get transactions from. 26 split_list=account.GetSplitList()
28 for split
in split_list:
29 if type(split) != Split:
30 split = Split(instance=split)
31 transaction=split.GetParent()
32 if not (transaction
in transaction_list):
33 transaction_list.append(transaction)
34 return transaction_list
37 def get_splits_without_lot(account=None,split_list=None):
38 """Returns a list of those Splits in split_list or account which do not have an according lot. 42 account: (optional) Account to search in. 43 split_list: (optional) List of Splits to search in. 45 one or the other has to be provided. 52 split_list=account.GetSplitList()
55 for split
in split_list:
56 if type(split).__name__ ==
'SwigPyObject':
57 split = Split(instance=split)
64 def find_account(account,name,account_list=None):
65 """Recursively searches full names of account and descendents 67 returns a list of accounts which contain name. 71 account: account to start search in. 72 name: name to search for. 73 account_list: (optional) list to append accounts to. 80 for child
in account.get_children():
81 if type(child) != Account:
83 account_list=find_account(child,name,account_list)
85 account_name=account.GetName()
86 if name
in account_name:
87 account_list.append(account)
92 def find_lot(lot_list,search_string):
93 """Searches lots in lot_list for search_string. 95 returns list of lots where title contains search_string. 99 lot_list: List of Lots to search in. 100 search_string: String to search for. 106 if type(lot).__name__ ==
'SwigPyObject':
107 lot = gnucash.GncLot(instance=lot)
108 ltitle=lot.get_title()
109 if search_string
in ltitle:
114 def find_split(split_list,search_string):
115 """Searches a list of splits for search_string 117 returns a list of splits that have search_string as part of 119 description of parent transaction. 123 split_list: List of Splits to search in. 124 search_string: String to search for. 129 for split
in split_list:
131 transaction_description=split.GetParent().GetDescription()
132 if (search_string
in memo)
or (search_string
in transaction_description):
137 def find_split_recursive(account, search_string):
138 """Searches account and descendants for Splits containing search_string 140 returns a list of splits that have search_string as part of 142 description of parent transaction. 146 account: Account to search in. 147 search_string: String to search for. 152 child_account_splits = []
155 for child
in account.get_children():
156 if type(child) != Account:
157 child =
Account(instance=child)
158 childsplits = find_split_recursive(child, search_string)
159 for split
in childsplits:
160 if type(split) != Split:
161 split = Split(instance=split)
162 child_account_splits += childsplits
165 splits=account.GetSplitList()
167 if type(split) != Split:
168 split = Split(instance=split)
169 basic_account_splits=find_split(splits,search_string)
171 rlist=child_account_splits+basic_account_splits
175 def find_transaction(account,name,ignore_case=True,transaction_list=None):
176 """Searches the transactions of an account for name. 178 Searches in description and in memo of each split. 179 returns a list of transactions that match criteria. 183 account: Account to search in. 184 name: String to search for. 185 ignore_case: (optional, default=True) Ignore case if True. 186 transaction_list: (optional) list of transactions to search in. 190 if not transaction_list:
191 transaction_list=get_transaction_list(account)
197 for transaction
in transaction_list:
200 desc=transaction.GetDescription()
207 sl=transaction.GetSplitList()
209 if type(split) != Split:
210 split=Split(instance=split)
212 memo = split.GetMemo()
220 ret.append(transaction)