From 87e7b6a8469aea1433fb966364510697fcf975f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BD=C3=A1=C4=8Dek?= Date: Tue, 12 Apr 2022 16:23:08 +0200 Subject: [PATCH] init on git --- ucetnictvi_tm.py | 160 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 ucetnictvi_tm.py diff --git a/ucetnictvi_tm.py b/ucetnictvi_tm.py new file mode 100644 index 0000000..8bb67ea --- /dev/null +++ b/ucetnictvi_tm.py @@ -0,0 +1,160 @@ +# coding: utf8 + +import csv +from datetime import datetime +import sys +from pprint import pprint + +FILE = 'TMCZ202110.csv' + +usrmonth = input("Month to be processed: ") +usrmonth = int(usrmonth) + + +dictionary = {} + +def add_dict(id, key, value): + dict = dictionary.get(id) + value_ = float(value) + if key in dict: + dict[key] += value_ + else: + 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 + +print("Start") +with open(FILE, newline='', encoding="utf-8") as csvfile: + reader = csv.DictReader(csvfile, delimiter=';') + for row in reader: + + + + #print(row) + if row['Telefonní číslo'] == '': + continue #skip empty MSISDN + + if row['Začátek fakturačního období'] == '': + continue + + my_string = row['Začátek fakturačního období'] + #my_string = '2021-09-06' + print("start date: ",my_string) + + try: + my_date = datetime.strptime(my_string, "%d.%m.%Y") + except: + print("An exception occurred, current my_string is: ", my_string) + sys.exit(1) + + + #print(row['BILLED_PERIOD_START']) + #print(my_date.month) + + + if usrmonth != my_date.month: + print("skipping bad date") + continue #skip bad dates + + + + + MSISDN = int(row['Telefonní číslo']) + #ICCID = int(row['ICCID']) + price = row['Účtovaná částka'].replace(',','.') + + #price = round(price, 5) + + typ = None + + if row['Název destinace/služby'] == "Měsíční paušál za tarif": + typ = 'data' + elif row['Název destinace/služby'] == "SMS T-Mobile": + typ = "sms" + elif row['Název destinace/služby'] == "SMS ostatní sítě": + typ = "sms" + elif row['Název destinace/služby'] == "SMS v zahraničí - EU Roaming": + typ = "sms" + elif row['Název destinace/služby'] == "SMS mezinárodní": + typ = "sms" + elif row['Název destinace/služby'] == "SMS v zahraničí - T-Mobile Roam., zóna 2": typ = "sms" + elif row['Název destinace/služby'] == "Datový provoz v zahr. - T-Mobile Roam., zóna 2": + typ = "data" + elif row['Název destinace/služby'] == "Datový provoz (z tarifu, balíčků, navýšení datového limitu)": + typ = "data" + elif row['Název destinace/služby'] == "Datový provoz v zahr. - T-Mobile Roam., zóna 3": + typ = "data" + elif row['Název destinace/služby'] == "Datový roaming Evropa": + typ = "data" + elif row['Název destinace/služby'] == "Intranet v mobilu": + typ = "data" + else: + typ = 'other' + + + + if row['Jednotka'] == "ks": + + pricesms = int(row['Počet jednotek – volné']) + int(row['Počet jednotek – účtované']) + print("rowjednotka: ",row['Jednotka']) + print("sms price: ",pricesms,MSISDN) + + else: + pricesms = 0 + + #only if not exist create new record in dictionary for ID + if not MSISDN in dictionary: + print("Creating dictionary: ", MSISDN) + dictionary[MSISDN] = {'data':0.00000, 'sms':0.00000, 'other':0.00000, 'smscount':pricesms} + + if price != '': + print("Editing dictionary: ", MSISDN, "typ: ", typ, "cena: ", price) + + + add_dict(MSISDN, typ, price) + if row['Jednotka'] == "ks": + #print("pricesms: ", pricesms, " for unit: ", dictionary[MSISDN]) + add_dictsms(MSISDN, pricesms) + + + + #try: + #print(dictionary[436761890319959]) #one with both types + #except: + #print("Required unit is not in our set") + #sys.exit(1) + +#print(len(dictionary.keys())) + +#for key, value in dictionary.items(): + #print(key, ' : ', value) + + +print("Count of units: ", len(dictionary.keys())) + +with open('export_tm202110v1.csv', 'w') as f: + for key in dictionary.keys(): + f.write("%s,%s\n"%(key,dictionary[key])) + + + +csv_columns = ['takhleuztoneudelam', 'data', 'sms', 'other', 'smscount'] +try: + with open('export_tm_202110v1.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}") +