diff options
Diffstat (limited to 'fracture/invoice.py')
-rw-r--r-- | fracture/invoice.py | 49 |
1 files changed, 38 insertions, 11 deletions
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 |