flaschengeist/geruecht/model/creditList.py

322 lines
12 KiB
Python

from datetime import datetime
from geruecht import getLogger
LOGGER = getLogger(__name__)
def create_empty_data():
empty_data = {'id': 0,
'jan_guthaben': 0,
'jan_schulden': 0,
'feb_guthaben': 0,
'feb_schulden': 0,
'maer_guthaben': 0,
'maer_schulden': 0,
'apr_guthaben': 0,
'apr_schulden': 0,
'mai_guthaben': 0,
'mai_schulden': 0,
'jun_guthaben': 0,
'jun_schulden': 0,
'jul_guthaben': 0,
'jul_schulden': 0,
'aug_guthaben': 0,
'aug_schulden': 0,
'sep_guthaben': 0,
'sep_schulden': 0,
'okt_guthaben': 0,
'okt_schulden': 0,
'nov_guthaben': 0,
'nov_schulden': 0,
'dez_guthaben': 0,
'dez_schulden': 0,
'last_schulden': 0,
'year_date': datetime.now().year,
'user_id': 0}
return empty_data
class CreditList():
""" DataBase Object Credit List:
Attributes:
id: id in Database. Is the Primary Key
<month>_guthaben: Credit of the Month.
<month>_schulden: Debt of the Month.
last_schulden: Debt or Credit of last Year.
year: Year of all Credits and Debts.
user_id: id from the User.
"""
def __init__(self, data):
LOGGER.debug("Initialize Geruecht")
self.id = int(data['id'])
self.jan_guthaben = int(data['jan_guthaben'])
self.jan_schulden = int(data['jan_schulden'])
self.feb_guthaben = int(data['feb_guthaben'])
self.feb_schulden = int(data['feb_schulden'])
self.maer_guthaben = int(data['maer_guthaben'])
self.maer_schulden = int(data['maer_schulden'])
self.apr_guthaben = int(data['apr_guthaben'])
self.apr_schulden = int(data['apr_schulden'])
self.mai_guthaben = int(data['mai_guthaben'])
self.mai_schulden = int(data['mai_schulden'])
self.jun_guthaben = int(data['jun_guthaben'])
self.jun_schulden = int(data['jun_schulden'])
self.jul_guthaben = int(data['jul_guthaben'])
self.jul_schulden = int(data['jul_schulden'])
self.aug_guthaben = int(data['aug_guthaben'])
self.aug_schulden = int(data['aug_schulden'])
self.sep_guthaben = int(data['sep_guthaben'])
self.sep_schulden = int(data['sep_schulden'])
self.okt_guthaben = int(data['okt_guthaben'])
self.okt_schulden = int(data['okt_schulden'])
self.nov_guthaben = int(data['nov_guthaben'])
self.nov_schulden = int(data['nov_schulden'])
self.dez_guthaben = int(data['dez_guthaben'])
self.dez_schulden = int(data['dez_schulden'])
self.last_schulden = int(data['last_schulden'])
self.year = int(data['year_date'])
self.user_id = int(data['user_id'])
def getSchulden(self):
""" Get Schulden
This function calculate the total amount of them self.
From the Credit of the Month will the Amount of the Month subtract.
Finaly all Month will added together.
At Last the amount from last year will be subtract.
If the Return Value is negativ, the User has an Credit, else the User has an amount.
Returns:
double of the calculated amount
"""
LOGGER.debug("Calculate amount")
jan = self.jan_guthaben - self.jan_schulden
feb = self.feb_guthaben - self.feb_schulden
maer = self.maer_guthaben - self.maer_schulden
apr = self.apr_guthaben - self.apr_schulden
mai = self.mai_guthaben - self.mai_schulden
jun = self.jun_guthaben - self.jun_schulden
jul = self.jul_guthaben - self.jul_schulden
aug = self.aug_guthaben - self.aug_schulden
sep = self.sep_guthaben - self.sep_schulden
okt = self.okt_guthaben - self.okt_schulden
nov = self.nov_guthaben - self.nov_schulden
dez = self.dez_guthaben - self.dez_schulden
sum = jan + feb + maer + apr + mai + jun + jul + aug + sep + okt + nov + dez - self.last_schulden
LOGGER.debug("Calculated amount is {}".format(sum))
return sum
def getMonth(self, month=datetime.now().month):
""" Get Amount from month
This function returns the amount and credit of the month.
By default is month the actual month
Args:
month: which month you want to get the amount(1-12)
Returns:
double (credit, amount)
"""
LOGGER.debug("Get Credit and Amount from Month {}".format(month))
retValue = None
if month == 1:
retValue = (self.jan_guthaben, self.jan_schulden)
elif month == 2:
retValue = (self.feb_guthaben, self.feb_schulden)
elif month == 3:
retValue = (self.maer_guthaben, self.maer_schulden)
elif month == 4:
retValue = (self.apr_guthaben, self.apr_schulden)
elif month == 5:
retValue = (self.mai_guthaben, self.mai_schulden)
elif month == 6:
retValue = (self.jun_guthaben, self.jun_schulden)
elif month == 7:
retValue = (self.jul_guthaben, self.jul_schulden)
elif month == 8:
retValue = (self.aug_guthaben, self.aug_schulden)
elif month == 9:
retValue = (self.sep_guthaben, self.sep_schulden)
elif month == 10:
retValue = (self.okt_guthaben, self.okt_schulden)
elif month == 11:
retValue = (self.nov_guthaben, self.nov_schulden)
elif month == 12:
retValue = (self.dez_guthaben, self.dez_schulden)
LOGGER.debug("Credit and Amount is {}".format(retValue))
return retValue
def addAmount(self, amount, month=datetime.now().month):
""" Add Amount
This function add an amount to a month and returns the credit and amount of the month.
By default is month the actual month.
Args:
amount: the amount which is to add
month: in which month to add the amount (1-12)
Returns:
double (credit, amount)
"""
LOGGER.debug("Add Amount in Month {}".format(month))
if month == 1:
self.jan_schulden += amount
retValue = (self.jan_guthaben, self.jan_schulden)
elif month == 2:
self.feb_schulden += amount
retValue = (self.feb_guthaben, self.feb_schulden)
elif month == 3:
self.maer_schulden += amount
retValue = (self.maer_guthaben, self.maer_schulden)
elif month == 4:
self.apr_schulden += amount
retValue = (self.apr_guthaben, self.apr_schulden)
elif month == 5:
self.mai_schulden += amount
retValue = (self.mai_guthaben, self.mai_schulden)
elif month == 6:
self.jun_schulden += amount
retValue = (self.jun_guthaben, self.jun_schulden)
elif month == 7:
self.jul_schulden += amount
retValue = (self.jul_guthaben, self.jul_schulden)
elif month == 8:
self.aug_schulden += amount
retValue = (self.aug_guthaben, self.aug_schulden)
elif month == 9:
self.sep_schulden += amount
retValue = (self.sep_guthaben, self.sep_schulden)
elif month == 10:
self.okt_schulden += amount
retValue = (self.okt_guthaben, self.okt_schulden)
elif month == 11:
self.nov_schulden += amount
retValue = (self.nov_guthaben, self.nov_schulden)
elif month == 12:
self.dez_schulden += amount
retValue = (self.dez_guthaben, self.dez_schulden)
LOGGER.debug("Credit and Amount is {}".format(retValue))
return retValue
def addCredit(self, credit, month=datetime.now().month):
""" Add Credit
This function add an credit to a month and returns the credit and amount of the month.
By default is month the actual month.
Args:
credit: the credit which is to add
month: in which month to add the credit (1-12)
Returns:
double (credit, amount)
"""
LOGGER.debug("Add Credit in Month {}".format(month))
if month == 1:
self.jan_guthaben += credit
retValue = (self.jan_guthaben, self.jan_schulden)
elif month == 2:
self.feb_guthaben += credit
retValue = (self.feb_guthaben, self.feb_schulden)
elif month == 3:
self.maer_guthaben += credit
retValue = (self.maer_guthaben, self.maer_schulden)
elif month == 4:
self.apr_guthaben += credit
retValue = (self.apr_guthaben, self.apr_schulden)
elif month == 5:
self.mai_guthaben += credit
retValue = (self.mai_guthaben, self.mai_schulden)
elif month == 6:
self.jun_guthaben += credit
retValue = (self.jun_guthaben, self.jun_schulden)
elif month == 7:
self.jul_guthaben += credit
retValue = (self.jul_guthaben, self.jul_schulden)
elif month == 8:
self.aug_guthaben += credit
retValue = (self.aug_guthaben, self.aug_schulden)
elif month == 9:
self.sep_guthaben += credit
retValue = (self.sep_guthaben, self.sep_schulden)
elif month == 10:
self.okt_guthaben += credit
retValue = (self.okt_guthaben, self.okt_schulden)
elif month == 11:
self.nov_guthaben += credit
retValue = (self.nov_guthaben, self.nov_schulden)
elif month == 12:
self.dez_guthaben += credit
retValue = (self.dez_guthaben, self.dez_schulden)
LOGGER.debug("Credit and Amount is {}".format(retValue))
return retValue
def toJSON(self):
""" Create Dic to dump in JSON
Returns:
A Dic with static Attributes.
"""
dic = {
"jan": {
"credit": self.jan_guthaben,
"depts": self.jan_schulden},
"feb": {
"credit": self.feb_guthaben,
"depts": self.feb_schulden},
"maer": {
"credit": self.maer_guthaben,
"depts": self.maer_schulden},
"apr": {
"credit": self.apr_guthaben,
"depts": self.apr_schulden},
"mai": {
"credit": self.mai_guthaben,
"depts": self.mai_schulden},
"jun": {
"credit": self.jun_guthaben,
"depts": self.jun_schulden},
"jul": {
"credit": self.jul_guthaben,
"depts": self.jul_schulden},
"aug": {
"credit": self.aug_guthaben,
"depts": self.aug_schulden},
"sep": {
"credit": self.sep_guthaben,
"depts": self.sep_schulden},
"okt": {
"credit": self.okt_guthaben,
"depts": self.okt_schulden},
"nov": {
"credit": self.nov_guthaben,
"depts": self.nov_schulden},
"dez": {
"credit": self.dez_guthaben,
"depts": self.dez_schulden},
"last": self.last_schulden
}
return dic
def __repr__(self):
return "CreditList(year: {}, userID: {}, amount: {})".format(self.year, self.user_id, self.toJSON())