summaryrefslogtreecommitdiff
path: root/fracture/invoice.py
diff options
context:
space:
mode:
Diffstat (limited to 'fracture/invoice.py')
-rw-r--r--fracture/invoice.py61
1 files changed, 37 insertions, 24 deletions
diff --git a/fracture/invoice.py b/fracture/invoice.py
index f0ab2f7..4ea9d71 100644
--- a/fracture/invoice.py
+++ b/fracture/invoice.py
@@ -57,6 +57,7 @@ class Invoice:
self.taxes = taxes or Invoice.DEFAULT_TAXES
def set_series(self, series):
+ """ Series is an integer """
if series not in Invoice.SERIES.keys():
raise ValueError ("Not valid series for Invoice. Valid are %s"
% Invoice.SERIES)
@@ -100,12 +101,13 @@ class Invoice:
return strf.getvalue()
- def from_config(self, config):
+ @classmethod
+ def from_config(cls, config):
cfg = ConfigParser()
cfg.read_string(config)
# PRODUCTS SECTIONS
- self.products = ()
+ products = ()
for s in cfg.sections():
if not s.startswith("product"):
continue
@@ -113,32 +115,41 @@ class Invoice:
un = float(cfg[s]["units"])
pu = float(cfg[s]["price_unit"])
vat = float(cfg[s]["vat"])
- self.products += ( Product(desc,un,pu,vat) ,)
- if cfg[s]["description"] == Invoice.DEFAULT_PRODUCT_DESC:
+ products += ( Product(desc,un,pu,vat) ,)
+ if cfg[s]["description"] == cls.DEFAULT_PRODUCT_DESC:
raise ValueError("Product name not set")
- if len(self.products) == 0:
+ if len(products) == 0:
raise ValueError("No products assigned")
# TAXES SECTION
- self.taxes = ()
+ taxes = ()
if cfg.has_section("taxes"):
for x in cfg["taxes"]:
- self.taxes += ( Tax(x, float(cfg["taxes"][x])) ,)
+ taxes += ( Tax(x, float(cfg["taxes"][x])) ,)
# INVOICE SECTION
if not cfg.has_section("invoice"):
raise ValueError("[invoice] section needed")
- i = cfg["invoice"]
- self.set_series(int(i["series"]))
- self.date = datetime.strptime(i["date"], "%Y-%m-%d").date()
- self.notes = i["notes"]
- self.set_type(i["type"])
- self.customer= Customer(i["customer-name"],
- i["customer-address"],
- i["customer-id"])
- if i["customer-name"] == Invoice.DEFAULT_CUSTOMER_DESC:
+ i = cfg["invoice"]
+ series = int(i["series"])
+ date = datetime.strptime(i["date"], "%Y-%m-%d").date()
+ notes = i["notes"]
+ type = i["type"]
+ customer = Customer( i["customer-name"],
+ i["customer-address"],
+ i["customer-id"] )
+ if i["customer-name"] == cls.DEFAULT_CUSTOMER_DESC:
raise ValueError("Customer name not set")
+ return cls(
+ type,
+ series,
+ date,
+ notes,
+ products,
+ customer,
+ taxes)
+
def persist(self):
conn = sqlite3.connect(Invoice.DB_FILE)
c = conn.cursor()
@@ -185,17 +196,16 @@ class Invoice:
conn.close()
return self.id
- def load(id):
- with sqlite3.connect(Invoice.DB_FILE) as conn:
+ @classmethod
+ def load(cls, id):
+ with sqlite3.connect(cls.DB_FILE) as conn:
conn.row_factory = sqlite3.Row
- invoice = Invoice()
-
c = conn.cursor()
# PRODUCTS
products = ()
- c.execute("""SELECT * FROM products WHERE invoice_id = ?""", (id,))
+ c.execute("SELECT * FROM products WHERE invoice_id = ?", (id,))
res = c.fetchone()
while res is not None:
desc = res["description"]
@@ -207,16 +217,16 @@ class Invoice:
# TAXES
taxes = ()
- c.execute("""SELECT * FROM taxes WHERE invoice_id = ?""", (id,))
+ c.execute("SELECT * FROM taxes WHERE invoice_id = ?", (id,))
res = c.fetchone()
while res is not None:
taxes += (Tax(res["name"], res["ratio"]) ,)
res = c.fetchone()
# INVOICE
- c.execute("""SELECT * FROM invoices WHERE id = ?""", (id,))
+ c.execute("SELECT * FROM invoices WHERE id = ?", (id,))
res = c.fetchone()
- return Invoice(
+ return cls(
res["type"],
res["series"],
res["date"],
@@ -229,4 +239,7 @@ class Invoice:
def format(self):
# TODO
+ # https://bugs.python.org/issue35111
pass
+
+