146 líneas
4,2 KiB
Python
146 líneas
4,2 KiB
Python
from datetime import datetime, timezone, timedelta
|
|
import time
|
|
import threading
|
|
import os
|
|
import sys
|
|
import os.path
|
|
import psycopg2
|
|
import pytz
|
|
import dateutil
|
|
from dateutil.parser import parse
|
|
from decimal import *
|
|
getcontext().prec = 2
|
|
|
|
###############################################################################
|
|
# get bills rows
|
|
###############################################################################
|
|
|
|
def print_bills():
|
|
|
|
try:
|
|
|
|
conn = psycopg2.connect(database = budget_db, user = budget_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
|
|
|
cur = conn.cursor()
|
|
|
|
cur.execute("SELECT datetime, domain, server, backup, fileserver, setup FROM bills")
|
|
|
|
rows = cur.fetchall()
|
|
|
|
for row in rows:
|
|
|
|
date = row[0].date().strftime('%d.%m.%Y')
|
|
print(date, ', domain: ' + str(row[1]), ', server: ' + str(row[2]), 'backup: ' + str(row[3]), ', fileserver: ' + str(row[4]), ' setup: ' + str(row[5]))
|
|
|
|
cur.close()
|
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
|
|
|
print(error)
|
|
|
|
finally:
|
|
|
|
if conn is not None:
|
|
|
|
conn.close()
|
|
|
|
###################################################################################
|
|
# add bills to database
|
|
###################################################################################
|
|
|
|
def insert_bills(billdate, domainbill, serverbill, backupbill, fileserverbill, setupbill):
|
|
|
|
sql = "INSERT INTO bills(datetime, domain, server, backup, fileserver, setup) VALUES(%s,%s,%s,%s,%s,%s)"
|
|
|
|
try:
|
|
|
|
conn = psycopg2.connect(database = budget_db, user = budget_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
|
|
|
cur = conn.cursor()
|
|
|
|
cur.execute(sql, (billdate, domainbill, serverbill, backupbill, fileserverbill, setupbill))
|
|
print("\n")
|
|
print("Updating bills...")
|
|
|
|
conn.commit()
|
|
|
|
cur.close()
|
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
|
|
|
print(error)
|
|
|
|
finally:
|
|
|
|
if conn is not None:
|
|
|
|
conn.close()
|
|
|
|
###############################################################################
|
|
# INITIALISATION
|
|
###############################################################################
|
|
|
|
# Returns the parameter from the specified file
|
|
def get_parameter( parameter, file_path ):
|
|
# Check if db_config.txt file exists
|
|
if not os.path.isfile(file_path):
|
|
if file_path == "config/db_config.txt":
|
|
print("File %s not found, exiting. Run db-setup.py."%file_path)
|
|
sys.exit(0)
|
|
|
|
# Find parameter in file
|
|
with open( file_path ) as f:
|
|
for line in f:
|
|
if line.startswith( parameter ):
|
|
return line.replace(parameter + ":", "").strip()
|
|
|
|
# Cannot find parameter, exit
|
|
print(file_path + " Missing parameter %s "%parameter)
|
|
print("Run setup.py")
|
|
sys.exit(0)
|
|
|
|
# Load configuration from config file
|
|
config_filepath = "config/db_config.txt"
|
|
budget_db = get_parameter("budget_db", config_filepath) # E.g., budget
|
|
budget_db_user = get_parameter("budget_db_user", config_filepath) # E.g., mastodon
|
|
|
|
###############################################################################
|
|
|
|
while True:
|
|
|
|
domainbill = input("Domain bill? (q to quit) ")
|
|
if domainbill == '':
|
|
domainbill = '0.00'
|
|
elif domainbill == 'q':
|
|
sys.exit("Bye")
|
|
domainbill = round(float(domainbill),2)
|
|
|
|
serverbill = input("Server bill? ")
|
|
if serverbill == '':
|
|
serverbill = '0.00'
|
|
serverbill = round(float(serverbill),2)
|
|
|
|
backupbill = input("Backup bill? ")
|
|
if backupbill == '':
|
|
backupbill = '0.00'
|
|
backupbill = round(float(backupbill),2)
|
|
|
|
fileserverbill = input("Fileserver bill? ")
|
|
if fileserverbill == '':
|
|
fileserverbill = 0.00
|
|
fileserverbill = round(float(fileserverbill),2)
|
|
|
|
setupbill = input("Setup bill? ")
|
|
if setupbill == '':
|
|
setupbill = 0.00
|
|
setupbill = round(float(setupbill),2)
|
|
|
|
billdate = datetime.strptime(input('Enter Bill date in the format d.m.yyyy '), '%d.%m.%Y')
|
|
|
|
now = datetime.now()
|
|
|
|
billdate = billdate.replace(hour=now.hour, minute=now.minute, second=now.second)
|
|
|
|
insert_bills(billdate, domainbill, serverbill, backupbill, fileserverbill, setupbill)
|
|
print_bills()
|