
| Current Path : /home/ift/52_procpy/dataninja/95__misc/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : //home/ift/52_procpy/dataninja/95__misc/ktoeditor.py |
# -*- coding: utf-8 -*-
import os
import re
import sys
class KTOEditor (object):
#*************************************************************************
def __init__ (self,sort_ukto):
self.sort_ukto = {}
zaehler = 0
for pattern in sort_ukto.split(","):
self.sort_ukto[pattern] = zaehler
zaehler = zaehler + 1
#*************************************************************************
def ktoformat (self,text,ukto=""):
'''
Outputs a kto database into a kto format
'''
buchungen = []
ktoamounts = {}
ktobez = {}
summe = 0
maxbetrag = 13 # formatting counts
maxktoa = 15 # Formatting KTOA
maxktob = 15 # Formatting KTOB
maxktoc = 15 # Formatting KTO for Bezeichnungen
maxbez = 15 # Formatting KTOBezeichnungen
maxsumme = 13 # Formatting Saldo
# 1. First run through all the Buchungssaetze
for line in text.split("\n"):
line1 = line.split(",")
if len(line1) < 2 or not line1[0] == "": # this is neither a Buchungssatz nor a Kto description
continue
if re.search(r"^\d+$",line1[1]): # This is a proper Buchungssatz
betrag = float(line1[2])
kto = [ re.sub(r"^-$","",line1[3].strip()), re.sub(r"^-$","",line1[4].strip()) ]
if not ukto == "":
for nr in (0,1):
kto[nr] = re.sub(r"^-"+ukto,"",kto[nr])
if kto[nr] == line1[nr+3]:
kto[nr] = re.sub(r"^-","",kto[nr])
if kto[nr] == line1[3]:
kto[nr] = ":" + kto[nr]
if self.sort_kto(kto[0],kto[1]) > 0: # switch kto's so that internal kto is first
kto.reverse()
betrag = (-1) * betrag
sumfaktor = 0
for nr in (0,1): # allocate the amounts for the teilkonten
teilkto = kto[nr]
if not re.search(r"^(-|$)",teilkto):
continue
sumfaktor = sumfaktor + (1-2*nr)
while (0 == 0):
if teilkto in ktoamounts:
ktoamounts[teilkto] = ktoamounts[teilkto] + (1-2*nr) * betrag
else:
ktoamounts[teilkto] = (1-2*nr) * betrag
if teilkto == "":
break
teilkto = re.sub(r"^(.*)(-.*)$","\\1",teilkto)
if not sumfaktor == 0:
summe = summe + sumfaktor * betrag
buchungs_key = kto[0] + "-" + kto[1] + str(betrag)
if not buchungs_key in buchungen:
buchungen[buchungs_key] = []
buchungen.[buchungs_key].append([
line1[1],
("%50.2f" % betrag).strip(),
re.sub(r"^$","-",kto[0]),
re.sub(r"^$","-",kto[1]),
("%50.2f" % summe).strip(),
line1[5].strip()
])
maxbetrag = max(maxbetrag,len(buchungen[-1][1]))
maxktoa = max(maxktoa ,len(buchungen[-1][2]))
maxktob = max(maxktob ,len(buchungen[-1][3]))
maxsumme = max(maxsumme ,len(buchungen[-1][4]))
elif line1[1] == "KTOBEZ": # store the ktobezeichungen
kto = None
if ukto == "":
kto = re.sub(r"^-","",line1[3].strip())
ktobez[kto] = re.sub(r"[ \-]","_",line1[2].strip(),99999999)
else:
m = re.search(r"^-"+ukto+r"($|-)(.*)",line1[3].strip())
if m:
kto = m.group(2)
ktobez[kto] = re.sub(r"[ \-]","_",line1[2].strip(),99999999)
if kto:
maxbez = max(maxbez,len(ktobez[kto]))
maxbetrag = "%" + str(maxbetrag) + "s"
maxktoc = "%-" + str(max(maxktoa,maxktob)) + "s"
maxktoa = "%-" + str(maxktoa) + "s"
maxktob = "%-" + str(maxktob) + "s"
maxsumme = "%" + str(maxsumme) + "s"
maxbez = "%-" + str(maxbez) + "s"
# 2. Second run
interne_buchungen = []
externe_buchungen = []
for buchung_set in buchungen:
buchungs_set.sort(self.sort_kto)
for buchung in buchungs_set:
line2 = ( buchung[0] + " " +
( maxbetrag % buchung[1] ) + " " +
( maxktoa % buchung[2] ) + " " +
( maxktob % buchung[3] ) + " " +
( maxsumme % buchung[4] ) + " " +
buchung[5]
)
if re.search(r"^-",buchung[2]) and re.search(r"^-",buchung[3]):
interne_buchungen.append(line2)
else:
externe_buchungen.append(line2)
interne_ktobezeichnungen = []
externe_ktobezeichnungen = []
ktolist = ktoamounts.keys()
ktolist.sort(self.sort_kto)
for kto in ktolist:
line3 = ""
if kto[1:] in ktobez:
line3 = ktobez[kto[1:]]
line3 = (
( maxktoc % kto[1:] ) +
( maxbez % line3 ) +
len(re.sub(r"[^-]","",kto[1:]))*" " +
( "%20.2f" % ktoamounts[kto] )
)
if kto == "":
externe_ktobezeichnungen.append(line3)
else:
interne_ktobezeichnungen.append(line3)
text = ( ukto + "\n" +
"\n".join(externe_ktobezeichnungen) + "\n\n" +
"\n".join(externe_buchungen) + "\n\n" +
"\n".join(interne_ktobezeichnungen) + "\n\n" +
"\n".join(interne_buchungen)
)
text = re.sub(r"\n\n\n+","\n\n",text,99999999,flags=re.DOTALL)
return(text)
#**********************************************************************
def csvformat (self,text,csv0):
# for zeile in text.split("\n"):
# if re.search(r"(\d\d\d\d\d\d\d\d
pass
#**********************************************************************
def sort_kto (self,a,b):
x = a.split("-")
y = b.split("-")
zaehler = 0
for k in x:
try:
k1 = y[zaehler]
except:
return(1)
if k == k1:
zaehler = zaehler + 1
else:
if k in self.sort_ukto:
if k1 in self.sort_ukto:
if self.sort_ukto[k] < self.sort_ukto[k1]:
return(-1)
else:
return(1)
else:
return(-1)
else:
if k1 in self.sort_ukto:
return(1)
else:
if k < k1:
return(-1)
else:
return(1)
return(-1)