mirror of
https://github.com/Findus23/invoices.git
synced 2024-09-18 14:03:48 +02:00
more OOP
This commit is contained in:
parent
7204ec2b4a
commit
50ab1d0abb
4 changed files with 80 additions and 37 deletions
39
invoice.py
Normal file
39
invoice.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
from datetime import datetime
|
||||
|
||||
|
||||
class Invoice(object):
|
||||
|
||||
def __init__(self, id: int = None, locale: int = None, title: str = None, recipient: object = None,
|
||||
date: datetime = None, description: str = None, range: str = None, bank_fee: int = None):
|
||||
self.id = id
|
||||
self.locale = locale
|
||||
self.title = title
|
||||
self.recipient = recipient
|
||||
self.date = date
|
||||
self.description = description
|
||||
self.range = range
|
||||
self.locale = locale
|
||||
|
||||
|
||||
class SingleInvoice(Invoice):
|
||||
|
||||
def __init__(self, price: int = None, **kwargs):
|
||||
super(SingleInvoice, self).__init__(**kwargs)
|
||||
self.mode = "single"
|
||||
self.price = price
|
||||
|
||||
|
||||
class HourlyInvoice(Invoice):
|
||||
|
||||
def __init__(self, hours: int = None, minutes: int = None, per_hour: int = None, **kwargs):
|
||||
super(HourlyInvoice, self).__init__(**kwargs)
|
||||
self.mode = "hourly"
|
||||
self.hours = hours
|
||||
self.minutes = minutes
|
||||
self.per_hour = per_hour
|
||||
|
||||
def hourtotal(self):
|
||||
return self.per_hour * (self.hours + self.minutes / 60)
|
||||
|
||||
def total(self):
|
||||
return self.hourtotal()
|
72
main.py
72
main.py
|
@ -3,44 +3,43 @@ import sys
|
|||
|
||||
import jinja2
|
||||
|
||||
from invoice import SingleInvoice, HourlyInvoice, Invoice
|
||||
from utils import *
|
||||
|
||||
|
||||
def create_invoice():
|
||||
current_id = config["last_id"]
|
||||
mode = ask("Mode", "set", set=["single", "hourly"], default="hourly")
|
||||
if mode == "single":
|
||||
invoice = SingleInvoice()
|
||||
elif mode == "hourly":
|
||||
invoice = HourlyInvoice()
|
||||
else:
|
||||
invoice = Invoice()
|
||||
current_id += 1
|
||||
config["last_id"] = current_id
|
||||
invoice = {
|
||||
"locale": ask("locale", "set", set=["de", "en"], default="de"),
|
||||
"id": ask("id", "int", default=current_id),
|
||||
"title": ask("title"),
|
||||
"recipient": ask("recipient", "set", set=get_possible_recipents(), default=config["default_recipient"]),
|
||||
"date": ask("date", "date", default="today"),
|
||||
"mode": ask("Mode", "set", set=["single", "hourly"], default="hourly"),
|
||||
"description": ask("description"),
|
||||
"range": ask("range"),
|
||||
}
|
||||
if invoice["mode"] == "single":
|
||||
single = {
|
||||
"price": ask("price", "money")
|
||||
}
|
||||
invoice.update(single)
|
||||
elif invoice["mode"] == "hourly":
|
||||
hourly = {
|
||||
"hours": ask("hours", "int"),
|
||||
"minutes": ask("hours", "int"),
|
||||
"per_hour": ask("per hour", "money", default=config["default_hourly_rate"])
|
||||
}
|
||||
invoice.update(hourly)
|
||||
invoice["bank_fee"] = ask("bank_fee", "boolean", default=False)
|
||||
directory = invoice_dir + "/" + str(invoice["id"])
|
||||
invoice.locale = ask("locale", "set", set=["de", "en"], default="de")
|
||||
invoice.id = ask("id", "int", default=current_id)
|
||||
invoice.title = ask("title")
|
||||
invoice.recipient = ask("recipient", "set", set=get_possible_recipents(), default=config["default_recipient"])
|
||||
invoice.date = ask("date", "date", default="today")
|
||||
invoice.description = ask("description")
|
||||
invoice.range = ask("range")
|
||||
|
||||
if invoice.mode == "single":
|
||||
invoice.price = ask("price", "money")
|
||||
|
||||
elif invoice.mode == "hourly":
|
||||
invoice.hours = ask("hours", "int")
|
||||
invoice.minutes = ask("minutes", "int")
|
||||
invoice.per_hour = ask("per hour", "money", default=config["default_hourly_rate"])
|
||||
directory = invoice_dir + "/" + str(invoice.id)
|
||||
if os.path.exists(directory):
|
||||
if not ask("overwrite", "boolean"):
|
||||
exit()
|
||||
else:
|
||||
os.mkdir(directory)
|
||||
print(invoice)
|
||||
save_yaml(invoice, directory + "/data.yaml")
|
||||
save_yaml(vars(invoice), directory + "/data.yaml")
|
||||
save_yaml(config, "config.yaml")
|
||||
|
||||
|
||||
|
@ -49,7 +48,15 @@ def compile_invoice(id):
|
|||
if os.path.exists(directory + "/locked"):
|
||||
print("The invoice has already been locked")
|
||||
exit()
|
||||
invoice = load_yaml(directory + "/data.yaml")
|
||||
invoicedata = load_yaml(directory + "/data.yaml")
|
||||
mode = invoicedata["mode"]
|
||||
del invoicedata["mode"]
|
||||
if mode == "single":
|
||||
invoice = SingleInvoice(**invoicedata)
|
||||
elif mode == "hourly":
|
||||
invoice = HourlyInvoice(**invoicedata)
|
||||
else:
|
||||
invoice = Invoice(**invoicedata)
|
||||
env = jinja2.Environment(
|
||||
block_start_string='\BLOCK{',
|
||||
block_end_string='}',
|
||||
|
@ -63,12 +70,9 @@ def compile_invoice(id):
|
|||
autoescape=False,
|
||||
loader=jinja2.FileSystemLoader(os.path.abspath('.'))
|
||||
)
|
||||
if invoice["mode"] == "hourly":
|
||||
invoice["hourtotal"] = invoice["per_hour"] * (invoice["hours"] + invoice["minutes"] / 60)
|
||||
invoice["total"] = invoice["hourtotal"] + config["bank_fee"]
|
||||
data = {
|
||||
"from": load_yaml("from.yaml"),
|
||||
"to": load_yaml("recipients/{id}.yaml".format(id=invoice["recipient"])),
|
||||
"to": load_yaml("recipients/{id}.yaml".format(id=invoice.recipient)),
|
||||
"invoice": invoice,
|
||||
"config": config
|
||||
}
|
||||
|
@ -77,7 +81,7 @@ def compile_invoice(id):
|
|||
|
||||
def translate(key):
|
||||
if key in strings:
|
||||
return strings[key][invoice["locale"]]
|
||||
return strings[key][invoice.locale]
|
||||
else:
|
||||
print("Translation key for '{key}' is missing".format(key=key))
|
||||
exit()
|
||||
|
@ -85,7 +89,7 @@ def compile_invoice(id):
|
|||
def format_digit(integer):
|
||||
integer = integer / 100
|
||||
string = "{0:.2f}".format(integer)
|
||||
if invoice["locale"] == "de":
|
||||
if invoice.locale == "de":
|
||||
string = string.replace(".", ",")
|
||||
return string
|
||||
|
||||
|
@ -94,7 +98,7 @@ def compile_invoice(id):
|
|||
|
||||
:type date: datetime.datetime
|
||||
"""
|
||||
if invoice["locale"] == "de":
|
||||
if invoice.locale == "de":
|
||||
return date.strftime("%d. %m. %Y")
|
||||
else:
|
||||
return date.strftime("%Y-%m-%d")
|
||||
|
|
|
@ -105,7 +105,7 @@
|
|||
\VAR{invoice.description} &
|
||||
\EUR{\VAR{invoice.per_hour | formatdigit }} &
|
||||
\VAR{invoice.hours}:\VAR{invoice.minutes} &
|
||||
\EUR{\VAR{invoice.hourtotal | formatdigit }} \\
|
||||
\EUR{\VAR{invoice.hourtotal() | formatdigit }} \\
|
||||
|
||||
\BLOCK{if invoice.bank_fee}
|
||||
\VAR{"bank_fee"|t} & & & \EUR{\VAR{config.bank_fee| formatdigit}} \\
|
||||
|
@ -115,7 +115,7 @@
|
|||
\midrule %\cmidrule{4-4}
|
||||
|
||||
|
||||
& & & \EUR{\VAR{invoice.total | formatdigit }} \\
|
||||
& & & \EUR{\VAR{invoice.total() | formatdigit }} \\
|
||||
\end{tabularx}
|
||||
|
||||
\BLOCK{endif}
|
||||
|
|
2
utils.py
2
utils.py
|
@ -41,7 +41,7 @@ def ask(question, validator=None, default=None, set=None):
|
|||
continue
|
||||
if validator == "money":
|
||||
try:
|
||||
answer = int(answer) * 100
|
||||
answer = int(answer * 100)
|
||||
except ValueError:
|
||||
continue
|
||||
if validator == "int":
|
||||
|
|
Loading…
Reference in a new issue