-
-
Save utkonos/1dad74716828c591c9839711fdb12f22 to your computer and use it in GitHub Desktop.
Insert Document Link into GnuCash SQLite3 Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
import sqlite3 | |
bills = [{'id': '000001', | |
'date_opened': '12/31/2018', | |
'owner_id': '000001', | |
'billingid': '123123123123', | |
'notes': '', | |
'date': '', | |
'desc': 'Widget', | |
'action': 'Material', | |
'account': 'Expenses:Office Supplies', | |
'quantity': 1, | |
'price': '1000.00', | |
'disc_type': '', | |
'disc_how': '', | |
'discount': '', | |
'taxable': '', | |
'taxincluded': '', | |
'tax_table': '', | |
'date_posted': '12/31/2018', | |
'due_date': '12/31/2018', | |
'account_posted': 'Liabilities:Accounts Payable', | |
'memo_posted': '', | |
'accu_splits': ''}, | |
{'id': '000001', | |
'date_opened': '01/31/2019', | |
'owner_id': '000001', | |
'billingid': '123123123126', | |
'notes': '', | |
'date': '', | |
'desc': 'Widget', | |
'action': 'Material', | |
'account': 'Expenses:Office Supplies', | |
'quantity': 1, | |
'price': '1000.00', | |
'disc_type': '', | |
'disc_how': '', | |
'discount': '', | |
'taxable': '', | |
'taxincluded': '', | |
'tax_table': '', | |
'date_posted': '01/31/2019', | |
'due_date': '01/31/2019', | |
'account_posted': 'Liabilities:Accounts Payable', | |
'memo_posted': '', | |
'accu_splits': ''}] | |
doclinks = { | |
'000001': { | |
'doclink': 'Path/To/Some1.pdf' | |
}, | |
'000002': { | |
'doclink': 'Path/To/Some2.pdf' | |
} | |
} | |
db_path = pathlib.Path('testfile.gnucash') | |
con = sqlite3.connect(db_path) | |
cur = con.cursor() | |
for bill in bills: | |
rows = cur.execute('SELECT * FROM invoices WHERE owner_type = 4 AND id = "{}"'.format(bill['id'])) | |
for row in rows: | |
doclinks[bill['id']]['obj_guid'] = row[0] | |
rows = cur.execute('SELECT max(id) FROM slots') | |
slotid = rows.fetchone()[0] + 1 | |
for doclink in doclinks.values(): | |
cur.execute("INSERT INTO slots VALUES ({},'{}','assoc_uri',4,0,'{}',NULL,'1970-01-01 00:00:00',NULL,0,1,NULL)".format(slotid, doclink['obj_guid'], doclink['doclink'])) | |
con.commit() | |
slotid += 1 | |
con.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment