1
0
Fork 0
mirror of https://github.com/Findus23/invoices.git synced 2024-09-19 15:13:47 +02:00
invoices/main.py

174 lines
5.8 KiB
Python
Raw Permalink Normal View History

2019-02-21 20:38:48 +01:00
# noinspection PyUnresolvedReferences
import readline
2018-03-04 21:04:54 +01:00
import subprocess
2018-03-13 22:03:13 +01:00
import sys
2018-03-04 21:04:54 +01:00
import jinja2
2018-05-15 17:49:23 +02:00
from invoice import SingleInvoice, HourlyInvoice, Invoice
2018-03-13 22:03:13 +01:00
from utils import *
2018-03-04 21:04:54 +01:00
2018-03-13 22:03:13 +01:00
def create_invoice():
current_id = config["last_id"]
2018-05-15 17:49:23 +02:00
mode = ask("Mode", "set", set=["single", "hourly"], default="hourly")
if mode == "single":
invoice = SingleInvoice()
elif mode == "hourly":
invoice = HourlyInvoice()
else:
invoice = Invoice()
2018-03-13 22:03:13 +01:00
current_id += 1
config["last_id"] = current_id
2018-05-15 17:49:23 +02:00
invoice.locale = ask("locale", "set", set=["de", "en"], default="de")
invoice.id = ask("id", "int", default=current_id)
invoice.title = ask("title", default=config["title"])
2018-05-15 17:49:23 +02:00
invoice.recipient = ask("recipient", "set", set=get_possible_recipents(), default=config["default_recipient"])
invoice.date = ask("date", "date", default="today")
invoice.description = ask("description", default=config["description"])
invoice.range = ask("range", default=config["range"])
2018-05-15 17:49:23 +02:00
if invoice.mode == "single":
invoice.price = ask("price", "money")
elif invoice.mode == "hourly":
invoice.hours = ask("hours", "int", default=config["hours"])
invoice.minutes = ask("minutes", "int", default="0")
invoice.per_hour = ask("rate per hour", "money", default=config["default_hourly_rate"])
2018-05-15 17:49:23 +02:00
directory = invoice_dir + "/" + str(invoice.id)
2018-03-19 18:55:22 +01:00
if os.path.exists(directory):
if not ask("overwrite", "boolean"):
exit()
else:
os.mkdir(directory)
2018-05-15 17:49:23 +02:00
save_yaml(vars(invoice), directory + "/data.yaml")
2018-03-13 22:03:13 +01:00
save_yaml(config, "config.yaml")
2018-03-04 21:04:54 +01:00
2018-03-13 22:03:13 +01:00
def compile_invoice(id):
directory = invoice_dir + "/" + str(id)
2018-03-14 08:53:22 +01:00
if os.path.exists(directory + "/locked"):
print("The invoice has already been locked")
exit()
2018-05-15 17:49:23 +02:00
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)
2018-03-13 22:03:13 +01:00
env = jinja2.Environment(
block_start_string='\BLOCK{',
block_end_string='}',
variable_start_string='\VAR{',
variable_end_string='}',
comment_start_string='\#{',
comment_end_string='}',
line_statement_prefix='%#',
line_comment_prefix='%%',
trim_blocks=True,
autoescape=False,
loader=jinja2.FileSystemLoader(os.path.abspath('.'))
)
2019-10-09 20:21:13 +02:00
fromdata = load_yaml("from.yaml")
fromdata["country"] = fromdata["countryDE"] if invoice.locale == "de" else fromdata["countryEN"]
2018-03-13 22:03:13 +01:00
data = {
2019-10-09 20:21:13 +02:00
"from": fromdata,
2018-05-15 17:49:23 +02:00
"to": load_yaml("recipients/{id}.yaml".format(id=invoice.recipient)),
2018-05-05 13:04:30 +02:00
"invoice": invoice,
"config": config
2018-03-13 22:03:13 +01:00
}
2018-03-04 21:04:54 +01:00
2018-03-13 22:03:13 +01:00
strings = load_yaml("strings.yaml")
2018-03-04 21:04:54 +01:00
2018-03-13 22:03:13 +01:00
def translate(key):
if key in strings:
2018-05-15 17:49:23 +02:00
return strings[key][invoice.locale]
2018-03-13 22:03:13 +01:00
else:
print("Translation key for '{key}' is missing".format(key=key))
exit()
2018-03-04 21:04:54 +01:00
2018-03-13 22:03:13 +01:00
def format_digit(integer):
integer = integer / 100
string = "{0:.2f}".format(integer)
2018-05-15 17:49:23 +02:00
if invoice.locale == "de":
2018-03-13 22:03:13 +01:00
string = string.replace(".", ",")
return string
2018-03-04 21:04:54 +01:00
2018-03-26 13:38:10 +02:00
def format_date(date):
"""
:type date: datetime.datetime
"""
2018-05-15 17:49:23 +02:00
if invoice.locale == "de":
2018-03-26 13:38:10 +02:00
return date.strftime("%d. %m. %Y")
else:
return date.strftime("%Y-%m-%d")
2018-03-13 22:03:13 +01:00
env.filters['formatdigit'] = format_digit
2018-03-26 13:38:10 +02:00
env.filters['formatdate'] = format_date
2018-03-13 22:03:13 +01:00
env.filters['t'] = translate
with open(directory + "/{name}.tex".format(name=translate("invoice")), "w") as fh:
template = env.get_template('template.tex')
fh.write(template.render(section1='Long Form', section2='Short Form', **data))
os.chdir(directory)
2018-03-14 08:53:22 +01:00
for _ in range(2):
2020-09-22 14:01:19 +02:00
subprocess.check_call(['pdflatex', '-interaction=nonstopmode', '{name}.tex'.format(name=translate("invoice"))])
2018-03-13 22:03:13 +01:00
print(directory)
remove_tmp_files(translate("invoice"))
2018-03-04 21:04:54 +01:00
2018-03-26 13:38:10 +02:00
def sign_invoice(id):
directory = invoice_dir + "/" + str(id)
if os.path.exists(directory + "/locked"):
print("The invoice has already been locked")
exit()
if os.path.exists(directory + "/Rechnung.pdf"):
name = "Rechnung"
elif os.path.exists(directory + "/Invoice.pdf"):
name = "Invoice"
else:
print("Invoice not found")
name = ""
exit()
2018-05-19 15:55:14 +02:00
command = [
2018-12-19 12:38:47 +01:00
"/usr/local/PDF-Over/scripts/pdf-over_linux.sh",
2018-03-26 13:38:10 +02:00
"-i", "{dir}/{name}.pdf".format(dir=directory, name=name),
"-o", "{dir}/{name}_{signed}.pdf".format(
dir=directory, name=name, signed=("signiert" if name == "Rechnung" else "signed")
),
"-b", "LOCAL", # use local Bürgerkarte
"-a", # automatically position signature
2019-10-09 20:16:08 +02:00
"-v", "true" if name == "Rechnung" else "false", # set visibility
2018-03-26 13:38:10 +02:00
"-s" # save without asking
2018-05-19 15:55:14 +02:00
]
print(" ".join(command))
subprocess.check_call(command)
2018-03-26 13:38:10 +02:00
2018-03-13 22:03:13 +01:00
if __name__ == "__main__":
2018-03-26 13:38:10 +02:00
if len(sys.argv) == 1 or len(sys.argv) > 3 or sys.argv[1] not in ["create", "compile", "sign"]:
print("please use 'create', 'compile' or 'sign'")
2018-03-13 22:03:13 +01:00
exit()
config = load_yaml("config.yaml")
invoice_dir = config["invoice_dir"]
2018-03-04 21:04:54 +01:00
2018-03-13 22:03:13 +01:00
if sys.argv[1] == "create":
create_invoice()
2018-03-26 13:38:10 +02:00
if sys.argv[1] == "compile" or sys.argv[1] == "sign":
2018-03-13 22:03:13 +01:00
if len(sys.argv) == 3:
try:
2018-03-19 18:55:22 +01:00
invoice_id = int(sys.argv[2])
2018-03-13 22:03:13 +01:00
except ValueError:
invoice_id = False
print("invalid id")
exit()
else:
invoice_id = config["last_id"]
2018-03-26 13:38:10 +02:00
if sys.argv[1] == "compile":
compile_invoice(invoice_id)
else:
sign_invoice(invoice_id)