diff options
author | Ekaitz Zarraga <ekaitz@elenq.tech> | 2020-08-08 17:07:22 +0200 |
---|---|---|
committer | Ekaitz Zarraga <ekaitz@elenq.tech> | 2020-08-08 17:07:22 +0200 |
commit | 54e01779769bea931047c391070a985224107236 (patch) | |
tree | fa5da4613d08b358f56a200fe8be12d688f8ec5c /fracture | |
parent | 2ccbf8b690c4ea052ec3388bfabeb4b6943a74db (diff) |
Simplify summary, needs review
Diffstat (limited to 'fracture')
-rw-r--r-- | fracture/__main__.py | 10 | ||||
-rw-r--r-- | fracture/invoice.py | 44 |
2 files changed, 19 insertions, 35 deletions
diff --git a/fracture/__main__.py b/fracture/__main__.py index c99b0e9..44f2518 100644 --- a/fracture/__main__.py +++ b/fracture/__main__.py @@ -122,17 +122,11 @@ def summarize(xlsx=False, year=None, quarter=None): key=lambda x: x["type"]) keys = list(rows[0].keys()) + # FIXME all rows don't match and I'm putting them together in weird orders for r in rows: for k in r.keys(): if k not in keys: - # TODO: - # Inserts all the taxes at the end, but this is not cool - # because it needs information about how is the invoice - # arranged - # Maybe move this to a static function in invoices and call it - # summary and make it work with some logic like: create the - # keys first and then go setting them in order - keys.insert(-1, k) + keys.append(k) import sys wrtr = DictWriter(sys.stdout, keys) wrtr.writeheader() diff --git a/fracture/invoice.py b/fracture/invoice.py index d795340..295b7a8 100644 --- a/fracture/invoice.py +++ b/fracture/invoice.py @@ -1,5 +1,6 @@ import json import sqlite3 +from itertools import groupby from datetime import date, datetime, timedelta from configparser import ConfigParser from collections import OrderedDict @@ -386,41 +387,30 @@ class Invoice: def to_row(self): row = OrderedDict( - type = self.type, - id = self.id, - date = self.date.strftime("%Y-%m-%d"), - customer_id = self.customer.id, - customer_name = self.customer.name, + type = self.type, + id = self.id, + date = self.date.strftime("%Y-%m-%d"), + customer_id = self.customer.id, + customer_name = self.customer.name, + base_imp_total = Conf.round(self.base), + total = Conf.round(self.total), ) - total_base = 0 - total_charged = 0 - vats = sorted(Product.VATS) - to_base_key = lambda v: "base("+str(v*100)+"%)" - to_vat_key = lambda v: "VAT("+str(v*100)+"%)" - for vat in vats: - ps = filter(lambda p: p.vat == vat, self.products) + to_base_key = lambda v: "base("+str(v*100)+"%)" + to_vat_key = lambda v: "VAT("+str(v*100)+"%)" + pvat = lambda p: p.vat - vat_base = 0 - charged_vat = 0 - for product in ps: - vat_base += product.base - charged_vat += product.charged_vat - row[to_base_key(vat)] = round(vat_base, Conf.CURRENCY_DECIMAL) - row[to_vat_key(vat)] = round(charged_vat, Conf.CURRENCY_DECIMAL) + vs = groupby(sorted(self.products, key=pvat), key=pvat) - total_base += vat_base - total_charged += vat_base + charged_vat + for vat, ps in vs: + ps = tuple(ps) + row[to_base_key(vat)] = Conf.round(sum(p.base for p in ps)) + row[to_vat_key(vat)] = Conf.round(sum(p.charged_vat for p in ps)) - 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 + tax_amount = i.ratio * self.base row[to_tax_key(i)] = round(tax_amount, Conf.CURRENCY_DECIMAL) - total_charged += tax_amount - - row["TOTAL"] = round(total_charged, Conf.CURRENCY_DECIMAL) - return row |