is alright

This commit is contained in:
2022-03-15 10:10:46 +01:00
parent a307d054f7
commit 4f8e61a3fe

132
tmgdsp.py
View File

@@ -2,10 +2,11 @@ import csv
from datetime import datetime from datetime import datetime
import sys import sys
FILE = 'TMGDSP202110.CSV' FILE = 'TMGDSP202112.CSV'
usrmonth = input("Month to be processed: ") #usrmonth = input("Month to be processed: ")
countofnotsms = int(1)
dictionary = {} dictionary = {}
@@ -13,10 +14,26 @@ dictionary = {}
def add_dict(id, key, value): def add_dict(id, key, value):
dict = dictionary.get(id) dict = dictionary.get(id)
if key in dict: if key in dict:
print("increasing notsms dict key for: ",value,"key IS: ",key)
dict[key] += value dict[key] += value
print("end state: ",dict[key])
dict[key] = round(dict[key],5)
print("key: ",key)
else: else:
print("Never should happen", id) print("Never should happen", id)
def add_dictsms(id, pricesms):
dict = dictionary.get(id)
print("increasing sms dict key for dict: ",dict," for :",pricesms)
dict["smscount"] += pricesms
#MAIN #MAIN
@@ -25,38 +42,39 @@ with open(FILE, newline='', encoding='utf-8') as csvfile:
for row in reader: for row in reader:
#print(row) #print(row)
if row['MSISDN'] == '': if row['MSISDN'] == '':
continue #skip empty MSISDN continue #skip empty MSISDN
#print(row)
if row['ICCID'] == '':
continue #skip empty ICCID
if row['BILLED_PERIOD_START'] == '': #if row['BILLED_PERIOD_START'] == '':
continue #continue
my_string = row['BILLED_PERIOD_START'] #my_string = row['BILLED_PERIOD_START']
#my_string = '2021-09-06' #my_string = '2021-09-06'
print(my_string) #print(my_string)
try: #try:
my_date = datetime.strptime(my_string, "%Y-%m-%d") #my_date = datetime.strptime(my_string, "%Y-%m-%d")
except: #except:
print("An exception occurred, current my_string is: ", my_string) #print("An exception occurred, current my_string is: ", my_string)
sys.exit(1) #sys.exit(1)
#print(row['BILLED_PERIOD_START']) #print(row['BILLED_PERIOD_START'])
#print(my_date.month) #print(my_date.month)
if my_date.month != '5': #if my_date.month != '5':
continue #skip bad dates #continue #skip bad dates
MSISDN = int(row['MSISDN']) MSISDN = int(row['MSISDN'])
ICCID = int(row['ICCID']) ICCID = int(row['ICCID'])
print("charge_net: ",row['CHARGE_NET'])
price = float(row['CHARGE_NET'].replace(',','.')) price = float(row['CHARGE_NET'].replace(',','.'))
print("before conditions price: ",price, "for: ",ICCID)
typ = None typ = None
if row['CHARGE_TYPE'] == "Pravidelné měsíční poplatky": if row['CHARGE_TYPE'] == "Pravidelné měsíční poplatky":
@@ -72,27 +90,77 @@ with open(FILE, newline='', encoding='utf-8') as csvfile:
else: else:
typ = 'other' typ = 'other'
if row['INCREMENT'] == "SMS":
pricesms = int(row['RATED_AMOUNT'])
else:
pricesms = 0
#only if not exist create new record in dictionary for ID #only if not exist create new record in dictionary for ID
if not MSISDN in dictionary: if not MSISDN in dictionary:
dictionary[MSISDN] = {'ICCID':ICCID,'data':0.0, 'sms':0.0, 'other':0.0}
dictionary[MSISDN] = {'ICCID':ICCID,'data':0.00000, 'sms':0.00000, 'other':0.00000, 'smscount':pricesms}
print("Creating new dictionary: ", dictionary[MSISDN])
if dictionary[MSISDN] == "436761890300016":
print("creating dictionary for our unit")
input()
print("Adding: ", price, " for: ", dictionary[MSISDN], "TYP: ",typ)
add_dict(MSISDN, typ, price) add_dict(MSISDN, typ, price)
if MSISDN == 436761890301205:
print("created dictionary for our unit")
#try:
if row['INCREMENT'] == "SMS":
print("pricesms: ",pricesms," for unit: ",dictionary[MSISDN])
add_dictsms(MSISDN, pricesms)
if MSISDN == 436761890301205:
print("increasing pricesms")
#ptry:
#print(dictionary[436761890319959]) #one with both types #print(dictionary[436761890319959]) #one with both types
#except: #pexcept:
#print("Required unit is not in our set") #pprint("Required unit is not in our set")
#sys.exit(1) #psys.exit(1)
print(len(dictionary.keys())) #pprint(len(dictionary.keys()))
for key, value in dictionary.items():
print(key, ' : ', value) #for key, value in dictionary.items():
#print(key, ' : ', value)
print("Count of units: ", len(dictionary.keys())) print("Count of units: ", len(dictionary.keys()))
#print("Count of sms: ",countofsms)
#for dict_value in dictionary:
# type(dict_value)
# #for k, v in dict_value.items():
with open('TMGDSP_export_202110.csv', 'w') as f: #with open('fakturace_tmgdsp_export_20211110v2.csv', 'w') as f:
for key in dictionary.keys(): # for key in dictionary.keys():
f.write("%s,%s\n"%(key,dictionary[key])) # print(key,dictionary[key])
# f.write("%s,%s\n"%(key,dictionary[key]))
csv_columns = ['takhleuztoneudelam', 'ICCID', 'data', 'sms', 'other', 'smscount']
try:
with open('output_tmgdsp_202112.csv', 'w') as csvfile:
w = csv.DictWriter(sys.stdout, csv_columns)
for key, val in sorted(dictionary.items()):
row = {'takhleuztoneudelam': key}
row.update(val)
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writerow(row)
except IOError:
print(f"I/O error: {IOError}")
csvfile.close()