Guest User

Untitled

a guest
Feb 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. from datetime import date
  2. import sqlite3
  3.  
  4. EPOCH_OFFSET = 978278400
  5.  
  6. conn = sqlite3.connect('banktivity.sql')
  7. conn.row_factory = sqlite3.Row
  8.  
  9. first_date = None
  10. accounts = set([])
  11.  
  12. txn_c = conn.cursor()
  13. for txn in txn_c.execute('''SELECT ZPLINEITEM,
  14. ZLINEITEMSOURCE.ZPDATE AS ZPDATE,
  15. ZPAMOUNT,
  16. ZPDETAILS,
  17. ZPACCOUNT,
  18. ZPTRANSACTION,
  19. ZPNOTE,
  20. ZPTITLE
  21. FROM ZLINEITEMSOURCE
  22. LEFT JOIN ZLINEITEM ON (ZLINEITEM.Z_PK=ZLINEITEMSOURCE.ZPLINEITEM)
  23. LEFT JOIN ZTRANSACTION ON (ZLINEITEM.ZPTRANSACTION=ZTRANSACTION.Z_PK)
  24. ORDER BY ZLINEITEMSOURCE.ZPDATE ASC'''):
  25. #WHERE ZLINEITEMSOURCE.Z_PK=2213
  26. # Now we have the main bank transaction
  27. txn_id = txn['ZPTRANSACTION']
  28. txn_date = date.fromtimestamp(txn['ZPDATE'] + EPOCH_OFFSET)
  29. txn_note = txn['ZPNOTE']
  30.  
  31. if not first_date:
  32. first_date = txn_date
  33.  
  34. if not txn_note:
  35. if txn['ZPDETAILS'].startswith(txn['ZPTITLE']) and len(txn['ZPDETAILS']) != len(txn['ZPTITLE']):
  36. txn_note = txn['ZPDETAILS'][len(txn['ZPTITLE']) + 3:]
  37. elif len(txn['ZPTITLE']) > len(txn['ZPDETAILS']):
  38. txn_note = txn['ZPTITLE']
  39. else:
  40. txn_note = txn['ZPDETAILS']
  41.  
  42. print('''{:%Y-%m-%d} * "{:s}"'''.format(txn_date, txn_note))
  43. print(''' ;{}'''.format(txn['ZPTITLE']))
  44. print(''' ;{}'''.format(txn['ZPNOTE']))
  45. print(''' ;{}'''.format(txn['ZPDETAILS']))
  46.  
  47.  
  48. # Get all associated entries
  49. entry_c = conn.cursor()
  50. entries = entry_c.execute('''SELECT *
  51. FROM ZLINEITEM
  52. LEFT JOIN ZACCOUNT ON (ZACCOUNT.Z_PK=ZLINEITEM.ZPACCOUNT)
  53. WHERE ZPTRANSACTION=?
  54. ORDER BY ZLINEITEM.Z_PK ASC''', (txn_id,)).fetchall()
  55. for entry in entries:
  56. account = entry['ZPFULLNAME'] or 'UnknownAccount'
  57. amount = entry['ZPTRANSACTIONAMOUNT']
  58. width = 51 - len(str(amount))
  59. print(''' {:<{width}s} {:.2f} CAD'''.format(account, amount, width=width))
  60. accounts.add(account)
  61.  
  62. print()
  63.  
  64. for account in accounts:
  65. print('''{:%Y-%m-%d} open {:s}'''.format(first_date, account))
Add Comment
Please, Sign In to add comment