summaryrefslogtreecommitdiff
path: root/fracture
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2020-08-06 14:40:39 +0200
committerEkaitz Zarraga <ekaitz@elenq.tech>2020-08-06 16:13:25 +0200
commita4927a2a306253035dd4980943c97440e208191e (patch)
treea1b45edfd994e0d5c83f0b2569ca2bcc061b31c4 /fracture
parente74fb23d68d94150d8ee18c8e553307cb07ec6b6 (diff)
Make to_json and to_dict functions
Diffstat (limited to 'fracture')
-rw-r--r--fracture/__main__.py17
-rw-r--r--fracture/invoice.py49
2 files changed, 52 insertions, 14 deletions
diff --git a/fracture/__main__.py b/fracture/__main__.py
index c51c556..5177a27 100644
--- a/fracture/__main__.py
+++ b/fracture/__main__.py
@@ -1,4 +1,4 @@
-from invoice import Invoice, Tax, Product
+from invoice import Invoice, Tax, Product, Conf
import db
from csv import DictWriter
@@ -49,8 +49,8 @@ def load_config():
Invoice.ID_FORMAT = f
# CURRENCY
- Invoice.CURRENCY = conf["invoice"].get("currency", "€")
- Invoice.CURRENCY_DECIMAL = conf["invoice"].getint("currency_decimal", 2)
+ Conf.CURRENCY = conf["invoice"].get("currency", "€")
+ Conf.CURRENCY_DECIMAL = conf["invoice"].getint("currency_decimal", 2)
# TEMPLATE ?
# TODO
@@ -138,6 +138,10 @@ def summarize(xlsx=False, year=None, quarter=None):
for r in rows:
wrtr.writerow(r)
+@command
+def dump(id):
+ id, series, type = id
+ print(Invoice.load(int(id), int(series), type).to_json())
if __name__ == "__main__":
load_config()
@@ -169,6 +173,13 @@ if __name__ == "__main__":
help="Obtain the summary of the year")
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)
+
# parse
args = parser.parse_args()
args.func(args)
diff --git a/fracture/invoice.py b/fracture/invoice.py
index b3fa6b6..e973b8a 100644
--- a/fracture/invoice.py
+++ b/fracture/invoice.py
@@ -1,13 +1,24 @@
+import json
import sqlite3
from datetime import date, datetime, timedelta
from configparser import ConfigParser
from collections import OrderedDict
import io
+class Conf:
+ # MOVE ALL THE CONFIG HERE
+ CURRENCY = None
+ CURRENCY_DECIMAL = 0
+
class Tax:
def __init__(self, name="", ratio = 0.0):
self.name = name.upper()
self.ratio = ratio
+ def to_dict(self):
+ return {
+ "name": self.name,
+ "ratio": self.ratio,
+ }
class Product:
VATS = ()
@@ -27,6 +38,13 @@ class Product:
return self.price_unit * self.units
def calc_charged_vat(self):
return self.calc_base() * self.vat
+ def to_dict(self):
+ return {
+ "description": self.description,
+ "units": self.units,
+ "price-unit": round(self.price_unit, Conf.CURRENCY_DECIMAL),
+ "vat": self.vat
+ }
class Customer:
def __init__(self, name, id = "", address = ""):
@@ -40,8 +58,6 @@ class Invoice:
_TYPES = {"sent", "received"}
SERIES = {}
ID_FORMAT = None
- CURRENCY = None
- CURRENCY_DECIMAL = 0
DB_FILE = None
DEFAULT_TAXES = ()
@@ -327,10 +343,21 @@ class Invoice:
return self.id
- def format(self):
- # TODO
- # https://bugs.python.org/issue35111
- pass
+
+ def to_dict(self):
+ return {
+ "products": tuple(p.to_dict() for p in self.products),
+ "taxes": tuple(t.to_dict() for t in self.taxes),
+ "type": self.type,
+ "id": self.format_id(),
+ "date": self.date.strftime("%Y-%m-%d"),
+ "customer_id": self.customer.id,
+ "customer_name": self.customer.name,
+ "customer_address": self.customer.address,
+ "notes": self.notes,
+ }
+ def to_json(self):
+ return json.dumps(self.to_dict())
def to_row(self):
@@ -356,20 +383,20 @@ class Invoice:
for product in ps:
vat_base += product.calc_base()
charged_vat += product.calc_charged_vat()
- row[to_base_key(vat)] = round(vat_base, self.CURRENCY_DECIMAL)
- row[to_vat_key(vat)] = round(charged_vat, self.CURRENCY_DECIMAL)
+ row[to_base_key(vat)] = round(vat_base, Conf.CURRENCY_DECIMAL)
+ row[to_vat_key(vat)] = round(charged_vat, Conf.CURRENCY_DECIMAL)
total_base += vat_base
total_charged += vat_base + charged_vat
- row["base(TOTAL)"] = round(total_base, self.CURRENCY_DECIMAL)
+ row["base(TOTAL)"] = round(total_base, Conf.CURRENCY_DECIMAL)
to_tax_key = lambda tax: tax.name + "("+ str(tax.ratio*100) +"%)"
for i in self.taxes:
tax_amount = i.ratio * total_base
- row[to_tax_key(i)] = round(tax_amount, self.CURRENCY_DECIMAL)
+ row[to_tax_key(i)] = round(tax_amount, Conf.CURRENCY_DECIMAL)
total_charged += tax_amount
- row["TOTAL"] = round(total_charged, self.CURRENCY_DECIMAL)
+ row["TOTAL"] = round(total_charged, Conf.CURRENCY_DECIMAL)
return row