Commits

Dan Connolly committed f355fbd

check for duplicate budget items; check for missing accounts

Comments (0)

Files changed (1)

 
     budget_load(open_read(gdoc_csv), engine)
 
-    raise NotImplementedError
+    budget_sync_accounts(engine, dry_run='--accounts' not in argv)
+    budget_sync_items(engine)
 
 
 def db_prepare(findcreds, create_engine, db,
                    Column('notes', String(200)))
 
 
+def budget_sync_accounts(conn, dry_run=True):
+    missing_acct = conn.execute('''
+select bi.* from (
+ select budget_name,
+        STR_TO_DATE(bi.t_lo,'%%m/%%d/%%Y') t_lo,
+        account_type, bi.code, parent, bi.name,
+        1 * replace(replace(budget, '$', ''), ',', '') budget
+ from budget_import bi
+ where bi.code > '') bi
+left join accounts a
+on a.code=bi.code
+where a.guid is null
+''').fetchall()
+
+    log.debug('missing_acct: %d', len(missing_acct))
+
+    if missing_acct:
+        log.warn('no such account code:\n%s',
+                 format_rows(missing_acct))
+
+    if dry_run:
+        return
+
+    raise NotImplemented
+
+
+def format_rows(rows):
+    return '\n'.join([str(row) for row in rows])
+
+
+def budget_sync_items(conn):
+    dups = conn.execute('''
+select count(*), budget_name, t_lo, code
+from budget_import
+where code > ''
+group by budget_name, t_lo, code
+having count(*) > 1''').fetchall()
+    log.debug('dups: %d', len(dups))
+
+    if dups:
+        log.error('duplicate keys in budget spreadsheet: %s',
+                  format_rows(dups))
+        raise IOError
+
+    raise NotImplementedError
+
+
 if __name__ == '__main__':
     def _initial_caps():
         from sys import argv