Files
vfcz/ucetnictvi_vfcz.py
2022-04-19 22:11:07 +02:00

187 lines
5.0 KiB
Python

# coding: utf8
import csv
from datetime import datetime
import sys
from pprint import pprint
import os
dirpath = "."
FILE = [filename for filename in os.listdir(dirpath) if filename.startswith("VFCZ_")]
FILE = FILE[0]
ext_date_from_filaname = FILE.split('_')[-1]
print(ext_date_from_filaname)
#usrmonth = input("Month to be processed: ")
#usrmonth = int(usrmonth)
usrmonth = int(5)
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
def add_dictdataamount(id, dataamount):
dict = dictionary.get(id)
dataamount = round(float(dataamount)/1024, 2)
print("increasing dataamount dict key for dict: ",dict," for :",dataamount)
dict["dataamount"] += dataamount
#MAIN
print("Start")
with open(FILE, newline='', encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile, delimiter=';',fieldnames=['faktura_c','phonenumber','direction','productgroup','productser','item','service','datetime','dialnum','time_taken','time_charged','dataamount','realcost','creditcost','discount' 'servicescost','chargedwovat','chargedwvat','privatecall'])
for row in reader:
print("Product serie: ",row['productser'])
print("Time taken ",row['time_taken'])
# sys.exit(1)
#print(row)
if row['phonenumber'] == '':
continue #skip empty phonenumber
# 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['phonenumber'])
#ICCID = int(row['ICCID'])
price = row['chargedwovat'].replace(',','.')
#price = round(price, 5)
typ = None
if row['productser'] == "Používání služeb ":
typ = 'data'
elif row['productser'] == "SMS ":
typ = "sms"
else:
print("groupvar: ",row['productser'])
typ = 'other'
if row['direction'] != "I" and row['productser'] == "SMS ":
pricesms = int(row['time_taken'])
print("sms price: ",pricesms,MSISDN)
print("got some countsms")
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, 'dataamount':0.00}
if price != '':
print("Editing dictionary: ", MSISDN, "typ: ", typ, "cena: ", price)
add_dict(MSISDN, typ, price)
add_dictdataamount(MSISDN, row['dataamount'])
if row['direction'] != "I" and row['productser'] == "SMS ":
print("pricesms: ", pricesms, " for unit: ", dictionary[MSISDN])
add_dictsms(MSISDN, pricesms)
else:
print("direction: ", row['direction'], " and productser: ", row['productser'])
#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('output_VFCZ_'+ext_date_from_filaname, 'w') as f:
writer = csv.writer(f)
writer.writerow(["MSISDN","ICCID","IMSI","DATA-PRICE","DATA-AMOUNT","SMS-PRICE","SMS-COUNT","OTHER-PRICE","SIM-STATUS"])
for key in dictionary.keys():
print(key)
print(dictionary[key])
data = dictionary[key]['data']
sms = dictionary[key]['sms']
other = dictionary[key]['other']
smscount = dictionary[key]['smscount']
dataamount = dictionary[key]['dataamount']
writer.writerow([key, "N/A", "N/A", data, dataamount, sms, smscount, other, "N/A"])
#csv_columns = ['takhleuztoneudelam', 'data', 'sms', 'other', 'smscount', 'dataamount']
#try:
# with open('export_vd_202109v1.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}")