diff options
-rw-r--r-- | fracture/__main__.py | 20 | ||||
-rw-r--r-- | fracture/db.py | 4 | ||||
-rw-r--r-- | fracture/invoice.py | 19 |
3 files changed, 32 insertions, 11 deletions
diff --git a/fracture/__main__.py b/fracture/__main__.py index 5177a27..ee26bbe 100644 --- a/fracture/__main__.py +++ b/fracture/__main__.py @@ -139,9 +139,10 @@ def summarize(xlsx=False, year=None, quarter=None): wrtr.writerow(r) @command -def dump(id): - id, series, type = id - print(Invoice.load(int(id), int(series), type).to_json()) +def render(id, type=None): + invoice = Invoice.load_by_idrepr(id, type) + if invoice is not None: + print(invoice.to_json()) if __name__ == "__main__": load_config() @@ -174,11 +175,14 @@ if __name__ == "__main__": summary_parser.set_defaults(func=summarize) # Dump invoice - summary_parser = subparsers.add_parser("dump", aliases=["d"], - help="Display chosen template") - summary_parser.add_argument("id", nargs="+", type=str, - help="Invoice to render") - summary_parser.set_defaults(func=dump) + summary_parser = subparsers.add_parser("render", aliases=["rend", "r"], + help="Render chosen invoice in json format") + summary_parser.add_argument("id", nargs="?", type=str, + help="Invoice identification string") + summary_parser.add_argument("--type", type=str, + help="Invoice type", default="sent") + + summary_parser.set_defaults(func=render) # parse args = parser.parse_args() diff --git a/fracture/db.py b/fracture/db.py index 56a268d..1e90727 100644 --- a/fracture/db.py +++ b/fracture/db.py @@ -24,6 +24,7 @@ def create(dbname): # date is %Y-%m-%d c.execute('''CREATE TABLE invoices ( id INTEGER NOT NULL, + id_repr TEXT NOT NULL, type TEXT NOT NULL, series INTEGER NOT NULL, date TEXT NOT NULL, @@ -32,7 +33,8 @@ def create(dbname): customer_id TEXT, customer_name TEXT, customer_address TEXT, - PRIMARY KEY(type, series, id) + PRIMARY KEY(type, series, id), + UNIQUE (id_repr, type) )''') c.execute('''CREATE TABLE taxes diff --git a/fracture/invoice.py b/fracture/invoice.py index e973b8a..e56a44f 100644 --- a/fracture/invoice.py +++ b/fracture/invoice.py @@ -180,7 +180,20 @@ class Invoice: @classmethod - def load(cls, id, series, type): + def load_by_idrepr(cls, id_repr, type="sent"): + with sqlite3.connect(cls.DB_FILE) as conn: + conn.row_factory = sqlite3.Row + + c = conn.cursor() + c.execute("""SELECT id, series, type FROM invoices + WHERE id_repr = ? AND type = ?""", (id_repr, type)) + res = c.fetchone() + if res is None: + return None + return cls.load(res["id"], res["series"], type) + + @classmethod + def load(cls, id, series, type="sent"): with sqlite3.connect(cls.DB_FILE) as conn: conn.row_factory = sqlite3.Row @@ -291,15 +304,17 @@ class Invoice: type, series, id, + id_repr, date, notes, customer_id, customer_name, customer_address - ) VALUES (?,?,?,?,?,?,?,?)""", ( + ) VALUES (?,?,?,?,?,?,?,?,?)""", ( self.type, self.series, self.id, + self.format_id(), self.date.strftime("%Y-%m-%d"), self.notes, self.customer.id, |