mirror of
https://github.com/Findus23/invoices.git
synced 2024-09-19 15:13:47 +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
|
import jinja2
|
||||||
|
|
||||||
|
from invoice import SingleInvoice, HourlyInvoice, Invoice
|
||||||
from utils import *
|
from utils import *
|
||||||
|
|
||||||
|
|
||||||
def create_invoice():
|
def create_invoice():
|
||||||
current_id = config["last_id"]
|
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
|
current_id += 1
|
||||||
config["last_id"] = current_id
|
config["last_id"] = current_id
|
||||||
invoice = {
|
invoice.locale = ask("locale", "set", set=["de", "en"], default="de")
|
||||||
"locale": ask("locale", "set", set=["de", "en"], default="de"),
|
invoice.id = ask("id", "int", default=current_id)
|
||||||
"id": ask("id", "int", default=current_id),
|
invoice.title = ask("title")
|
||||||
"title": ask("title"),
|
invoice.recipient = ask("recipient", "set", set=get_possible_recipents(), default=config["default_recipient"])
|
||||||
"recipient": ask("recipient", "set", set=get_possible_recipents(), default=config["default_recipient"]),
|
invoice.date = ask("date", "date", default="today")
|
||||||
"date": ask("date", "date", default="today"),
|
invoice.description = ask("description")
|
||||||
"mode": ask("Mode", "set", set=["single", "hourly"], default="hourly"),
|
invoice.range = ask("range")
|
||||||
"description": ask("description"),
|
|
||||||
"range": ask("range"),
|
if invoice.mode == "single":
|
||||||
}
|
invoice.price = ask("price", "money")
|
||||||
if invoice["mode"] == "single":
|
|
||||||
single = {
|
elif invoice.mode == "hourly":
|
||||||
"price": ask("price", "money")
|
invoice.hours = ask("hours", "int")
|
||||||
}
|
invoice.minutes = ask("minutes", "int")
|
||||||
invoice.update(single)
|
invoice.per_hour = ask("per hour", "money", default=config["default_hourly_rate"])
|
||||||
elif invoice["mode"] == "hourly":
|
directory = invoice_dir + "/" + str(invoice.id)
|
||||||
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"])
|
|
||||||
if os.path.exists(directory):
|
if os.path.exists(directory):
|
||||||
if not ask("overwrite", "boolean"):
|
if not ask("overwrite", "boolean"):
|
||||||
exit()
|
exit()
|
||||||
else:
|
else:
|
||||||
os.mkdir(directory)
|
os.mkdir(directory)
|
||||||
print(invoice)
|
save_yaml(vars(invoice), directory + "/data.yaml")
|
||||||
save_yaml(invoice, directory + "/data.yaml")
|
|
||||||
save_yaml(config, "config.yaml")
|
save_yaml(config, "config.yaml")
|
||||||
|
|
||||||
|
|
||||||
|
@ -49,7 +48,15 @@ def compile_invoice(id):
|
||||||
if os.path.exists(directory + "/locked"):
|
if os.path.exists(directory + "/locked"):
|
||||||
print("The invoice has already been locked")
|
print("The invoice has already been locked")
|
||||||
exit()
|
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(
|
env = jinja2.Environment(
|
||||||
block_start_string='\BLOCK{',
|
block_start_string='\BLOCK{',
|
||||||
block_end_string='}',
|
block_end_string='}',
|
||||||
|
@ -63,12 +70,9 @@ def compile_invoice(id):
|
||||||
autoescape=False,
|
autoescape=False,
|
||||||
loader=jinja2.FileSystemLoader(os.path.abspath('.'))
|
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 = {
|
data = {
|
||||||
"from": load_yaml("from.yaml"),
|
"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,
|
"invoice": invoice,
|
||||||
"config": config
|
"config": config
|
||||||
}
|
}
|
||||||
|
@ -77,7 +81,7 @@ def compile_invoice(id):
|
||||||
|
|
||||||
def translate(key):
|
def translate(key):
|
||||||
if key in strings:
|
if key in strings:
|
||||||
return strings[key][invoice["locale"]]
|
return strings[key][invoice.locale]
|
||||||
else:
|
else:
|
||||||
print("Translation key for '{key}' is missing".format(key=key))
|
print("Translation key for '{key}' is missing".format(key=key))
|
||||||
exit()
|
exit()
|
||||||
|
@ -85,7 +89,7 @@ def compile_invoice(id):
|
||||||
def format_digit(integer):
|
def format_digit(integer):
|
||||||
integer = integer / 100
|
integer = integer / 100
|
||||||
string = "{0:.2f}".format(integer)
|
string = "{0:.2f}".format(integer)
|
||||||
if invoice["locale"] == "de":
|
if invoice.locale == "de":
|
||||||
string = string.replace(".", ",")
|
string = string.replace(".", ",")
|
||||||
return string
|
return string
|
||||||
|
|
||||||
|
@ -94,7 +98,7 @@ def compile_invoice(id):
|
||||||
|
|
||||||
:type date: datetime.datetime
|
:type date: datetime.datetime
|
||||||
"""
|
"""
|
||||||
if invoice["locale"] == "de":
|
if invoice.locale == "de":
|
||||||
return date.strftime("%d. %m. %Y")
|
return date.strftime("%d. %m. %Y")
|
||||||
else:
|
else:
|
||||||
return date.strftime("%Y-%m-%d")
|
return date.strftime("%Y-%m-%d")
|
||||||
|
|
|
@ -105,7 +105,7 @@
|
||||||
\VAR{invoice.description} &
|
\VAR{invoice.description} &
|
||||||
\EUR{\VAR{invoice.per_hour | formatdigit }} &
|
\EUR{\VAR{invoice.per_hour | formatdigit }} &
|
||||||
\VAR{invoice.hours}:\VAR{invoice.minutes} &
|
\VAR{invoice.hours}:\VAR{invoice.minutes} &
|
||||||
\EUR{\VAR{invoice.hourtotal | formatdigit }} \\
|
\EUR{\VAR{invoice.hourtotal() | formatdigit }} \\
|
||||||
|
|
||||||
\BLOCK{if invoice.bank_fee}
|
\BLOCK{if invoice.bank_fee}
|
||||||
\VAR{"bank_fee"|t} & & & \EUR{\VAR{config.bank_fee| formatdigit}} \\
|
\VAR{"bank_fee"|t} & & & \EUR{\VAR{config.bank_fee| formatdigit}} \\
|
||||||
|
@ -115,7 +115,7 @@
|
||||||
\midrule %\cmidrule{4-4}
|
\midrule %\cmidrule{4-4}
|
||||||
|
|
||||||
|
|
||||||
& & & \EUR{\VAR{invoice.total | formatdigit }} \\
|
& & & \EUR{\VAR{invoice.total() | formatdigit }} \\
|
||||||
\end{tabularx}
|
\end{tabularx}
|
||||||
|
|
||||||
\BLOCK{endif}
|
\BLOCK{endif}
|
||||||
|
|
2
utils.py
2
utils.py
|
@ -41,7 +41,7 @@ def ask(question, validator=None, default=None, set=None):
|
||||||
continue
|
continue
|
||||||
if validator == "money":
|
if validator == "money":
|
||||||
try:
|
try:
|
||||||
answer = int(answer) * 100
|
answer = int(answer * 100)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue
|
continue
|
||||||
if validator == "int":
|
if validator == "int":
|
||||||
|
|
Loading…
Reference in a new issue