GnuCash c935c2f+
Loading...
Searching...
No Matches
Functions | Variables
new_book_with_opening_balances.py File Reference

Replicate the account structure of a book and apply basis opening balances from the original. More...

Go to the source code of this file.

Functions

 new_book_with_opening_balances.initialize_split (book, value, account, trans)
 
 new_book_with_opening_balances.record_opening_balance (original_account, new_account, new_book, opening_balance_per_currency, commodity_tuple)
 
 new_book_with_opening_balances.recursivly_build_account_tree (original_parent_account, new_parent_account, new_book, new_commodity_table, opening_balance_per_currency, account_types_to_open)
 
 new_book_with_opening_balances.reconstruct_account_name_with_mnemonic (account_tuple, mnemonic)
 
 new_book_with_opening_balances.find_or_make_account (account_tuple, root_account, book, currency)
 
 new_book_with_opening_balances.choke_on_none_for_no_account (opening_account, extra_string)
 
 new_book_with_opening_balances.create_opening_balance_transaction (commodtable, namespace, mnemonic, new_book_root, new_book, opening_trans, opening_amount, simple_opening_name_used)
 
 new_book_with_opening_balances.main ()
 

Variables

tuple new_book_with_opening_balances.OPENING_DATE = (1, 1, 2011)
 
 new_book_with_opening_balances.ACCOUNT_TYPES_TO_OPEN
 
tuple new_book_with_opening_balances.OPENING_BALANCE_ACCOUNT = ( 'Equity', 'Opening Balances')
 
tuple new_book_with_opening_balances.PREFERED_CURRENCY_FOR_SIMPLE_OPENING_BALANCE = ("CURRENCY", "CAD")
 

Detailed Description

Replicate the account structure of a book and apply basis opening balances from the original.

Author
Mark Jenkins, ParIT Worker Co-operative mark@.nosp@m.pari.nosp@m.t.ca

Definition in file new_book_with_opening_balances.py.

Function Documentation

◆ choke_on_none_for_no_account()

new_book_with_opening_balances.choke_on_none_for_no_account (   opening_account,
  extra_string 
)

Definition at line 245 of file new_book_with_opening_balances.py.

245def choke_on_none_for_no_account(opening_account, extra_string ):
246 if opening_account == None:
247 raise Exception("account currency and name mismatch, " + extra_string)
248

◆ create_opening_balance_transaction()

new_book_with_opening_balances.create_opening_balance_transaction (   commodtable,
  namespace,
  mnemonic,
  new_book_root,
  new_book,
  opening_trans,
  opening_amount,
  simple_opening_name_used 
)

Definition at line 249 of file new_book_with_opening_balances.py.

252 simple_opening_name_used):
253 currency = commodtable.lookup(namespace, mnemonic)
254 assert( currency.get_instance() != None )
255
256 if simple_opening_name_used:
257 account_pieces = reconstruct_account_name_with_mnemonic(
258 OPENING_BALANCE_ACCOUNT,
259 mnemonic)
260 opening_account = find_or_make_account(
261 account_pieces, new_book_root, new_book, currency )
262 choke_on_none_for_no_account(opening_account,
263 ', '.join(account_pieces) )
264 else:
265 opening_account = find_or_make_account(OPENING_BALANCE_ACCOUNT,
266 new_book_root, new_book,
267 currency )
268 simple_opening_name_used = True
269 if opening_account == None:
270 account_pieces = reconstruct_account_name_with_mnemonic(
271 OPENING_BALANCE_ACCOUNT,
272 mnemonic)
273 opening_account = find_or_make_account(
274 account_pieces, new_book_root, new_book, currency )
275 choke_on_none_for_no_account(opening_account,
276 ', '.join(account_pieces) )
277
278 # we don't need to use the opening balance account at all if all
279 # the accounts being given an opening balance balance out
280 if opening_amount.num() != 0:
281 initialize_split(new_book, opening_amount, opening_account,
282 opening_trans)
283
284 opening_trans.SetDate( *OPENING_DATE )
285 opening_trans.SetCurrency(currency)
286 opening_trans.SetDescription("Opening Balance")
287 opening_trans.CommitEdit()
288
289 return simple_opening_name_used
290

◆ find_or_make_account()

new_book_with_opening_balances.find_or_make_account (   account_tuple,
  root_account,
  book,
  currency 
)

Definition at line 222 of file new_book_with_opening_balances.py.

223 currency ):
224 current_account_name, account_path = account_tuple[0], account_tuple[1:]
225 current_account = root_account.lookup_by_name(current_account_name)
226 if current_account == None:
227 current_account = Account(book)
228 current_account.SetName(current_account_name)
229 current_account.SetCommodity(currency)
230 root_account.append_child(current_account)
231
232 if len(account_path) > 0:
233 return find_or_make_account(account_path, current_account, book,
234 currency)
235 else:
236 account_commod = current_account.GetCommodity()
237 if (account_commod.get_mnemonic(),
238 account_commod.get_namespace() ) == \
239 (currency.get_mnemonic(),
240 currency.get_namespace()) :
241 return current_account
242 else:
243 return None
244
STRUCTS.

◆ initialize_split()

new_book_with_opening_balances.initialize_split (   book,
  value,
  account,
  trans 
)

Definition at line 137 of file new_book_with_opening_balances.py.

137def initialize_split(book, value, account, trans):
138 split = Split(book)
139 split.SetValue(value)
140 split.SetAccount(account)
141 split.SetParent(trans)
142 return split
143
144

◆ main()

new_book_with_opening_balances.main ( )

Definition at line 291 of file new_book_with_opening_balances.py.

291def main():
292
293 if len(argv) < 3:
294 print('not enough parameters')
295 print('usage: new_book_with_opening_balances.py {source_book_url} {destination_book_url}')
296 print('examples:')
297 print("python3 new_book_with_opening_balances.py '/home/username/test.gnucash' 'sqlite3:///home/username/new_test.gnucash'")
298 print("python3 new_book_with_opening_balances.py '/home/username/test.gnucash' 'xml:///crypthome/username/finances/new_test.gnucash'")
299 return
300
301 #have everything in a try block to unable us to release our hold on stuff to the extent possible
302 try:
303 original_book_session = Session(argv[1], SessionOpenMode.SESSION_NORMAL_OPEN)
304 new_book_session = Session(argv[2], SessionOpenMode.SESSION_NEW_STORE)
305 new_book = new_book_session.get_book()
306 new_book_root = new_book.get_root_account()
307
308 commodtable = new_book.get_table()
309 # we discovered that if we didn't have this save early on, there would
310 # be trouble later
311 new_book_session.save()
312
313 opening_balance_per_currency = {}
314 recursivly_build_account_tree(
315 original_book_session.get_book().get_root_account(),
316 new_book_root,
317 new_book,
318 commodtable,
319 opening_balance_per_currency,
320 ACCOUNT_TYPES_TO_OPEN
321 )
322
323 (namespace, mnemonic) = PREFERED_CURRENCY_FOR_SIMPLE_OPENING_BALANCE
324 if (namespace, mnemonic) in opening_balance_per_currency:
325 opening_trans, opening_amount = opening_balance_per_currency[
326 (namespace, mnemonic)]
327 simple_opening_name_used = create_opening_balance_transaction(
328 commodtable, namespace, mnemonic,
329 new_book_root, new_book,
330 opening_trans, opening_amount,
331 False )
332 del opening_balance_per_currency[
333 PREFERED_CURRENCY_FOR_SIMPLE_OPENING_BALANCE]
334 else:
335 simple_opening_name_used = False
336
337 for (namespace, mnemonic), (opening_trans, opening_amount) in \
338 opening_balance_per_currency.items() :
339 simple_opening_name_used = create_opening_balance_transaction(
340 commodtable, namespace, mnemonic,
341 new_book_root, new_book,
342 opening_trans, opening_amount,
343 simple_opening_name_used )
344
345 new_book_session.save()
346 new_book_session.end()
347 original_book_session.end()
348 except:
349 if "original_book_session" in locals():
350 original_book_session.end()
351
352 if "new_book_session" in locals():
353 new_book_session.end()
354
355 raise
356
357

◆ reconstruct_account_name_with_mnemonic()

new_book_with_opening_balances.reconstruct_account_name_with_mnemonic (   account_tuple,
  mnemonic 
)

Definition at line 216 of file new_book_with_opening_balances.py.

216def reconstruct_account_name_with_mnemonic(account_tuple, mnemonic):
217 opening_balance_account_pieces = list(account_tuple)
218 opening_balance_account_pieces[
219 len(opening_balance_account_pieces) - 1 ] += " - " + mnemonic
220 return opening_balance_account_pieces
221

◆ record_opening_balance()

new_book_with_opening_balances.record_opening_balance (   original_account,
  new_account,
  new_book,
  opening_balance_per_currency,
  commodity_tuple 
)

Definition at line 145 of file new_book_with_opening_balances.py.

147 ):
148 # create an opening balance if the account type is right
149 if new_account.GetType() in ACCOUNT_TYPES_TO_OPEN:
150 final_balance = original_account.GetBalance()
151 if final_balance.num() != 0:
152 # if there is a new currency type, associate with the currency
153 # a Transaction which will be the opening transaction for that
154 # currency and a GncNumeric value which will be the opening
155 # balance account amount
156 if commodity_tuple not in opening_balance_per_currency:
157 trans = Transaction(new_book)
158 trans.BeginEdit()
159 opening_balance_per_currency[commodity_tuple] = (
160 trans, GncNumeric(0, 1) )
161 trans, total = opening_balance_per_currency[commodity_tuple]
162
163 new_total = total.sub(
164 final_balance,
165 GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT )
166
167 initialize_split(
168 new_book,
169 final_balance,
170 new_account, trans)
171 opening_balance_per_currency[commodity_tuple] = \
172 (trans, new_total )
173
The primary numeric class for representing amounts and values.

◆ recursivly_build_account_tree()

new_book_with_opening_balances.recursivly_build_account_tree (   original_parent_account,
  new_parent_account,
  new_book,
  new_commodity_table,
  opening_balance_per_currency,
  account_types_to_open 
)

Definition at line 174 of file new_book_with_opening_balances.py.

179 account_types_to_open ):
180
181 for child in original_parent_account.get_children():
182 original_account = child
183 new_account = Account(new_book)
184 # attach new account to its parent
185 new_parent_account.append_child(new_account)
186
187 # copy simple attributes
188 for attribute in ('Name', 'Type', 'Description', 'Notes',
189 'Code', 'TaxRelated', 'Placeholder'):
190 # new_account.SetAttribute( original_account.GetAttribute() )
191 getattr(new_account, 'Set' + attribute)(
192 getattr(original_account, 'Get' + attribute)() )
193
194 # copy commodity
195 orig_commodity = original_account.GetCommodity()
196 namespace = orig_commodity.get_namespace()
197 mnemonic = orig_commodity.get_mnemonic()
198 new_commodity = new_commodity_table.lookup(namespace, mnemonic)
199 if new_commodity == None:
200 new_commodity = orig_commodity.clone(new_book)
201 new_commodity_table.insert(new_commodity)
202 new_account.SetCommodity(new_commodity)
203
204 record_opening_balance( original_account, new_account,
205 new_book, opening_balance_per_currency,
206 (namespace, mnemonic),
207 )
208
209 recursivly_build_account_tree(original_account,
210 new_account,
211 new_book,
212 new_commodity_table,
213 opening_balance_per_currency,
214 account_types_to_open)
215

Variable Documentation

◆ ACCOUNT_TYPES_TO_OPEN

new_book_with_opening_balances.ACCOUNT_TYPES_TO_OPEN
Initial value:
1= set( (
2 ACCT_TYPE_BANK,
3 ACCT_TYPE_CASH,
4 ACCT_TYPE_CREDIT,
5 ACCT_TYPE_ASSET,
6 ACCT_TYPE_LIABILITY,
7 ACCT_TYPE_STOCK,
8 ACCT_TYPE_MUTUAL,
9 ACCT_TYPE_INCOME,
10 ACCT_TYPE_EXPENSE,
11 ACCT_TYPE_EQUITY,
12 ACCT_TYPE_RECEIVABLE,
13 ACCT_TYPE_PAYABLE,
14 ACCT_TYPE_TRADING,
15))

Definition at line 90 of file new_book_with_opening_balances.py.

◆ OPENING_BALANCE_ACCOUNT

tuple new_book_with_opening_balances.OPENING_BALANCE_ACCOUNT = ( 'Equity', 'Opening Balances')

Definition at line 130 of file new_book_with_opening_balances.py.

◆ OPENING_DATE

tuple new_book_with_opening_balances.OPENING_DATE = (1, 1, 2011)

Definition at line 87 of file new_book_with_opening_balances.py.

◆ PREFERED_CURRENCY_FOR_SIMPLE_OPENING_BALANCE

tuple new_book_with_opening_balances.PREFERED_CURRENCY_FOR_SIMPLE_OPENING_BALANCE = ("CURRENCY", "CAD")

Definition at line 135 of file new_book_with_opening_balances.py.