# wb4.awk

#/*
#*************************************************************************
#**									*
#**	Copyright (C) 1992-2012 Trans-Mit Pty. Ltd. Melbourne Australia	*
#**	This software may not be loaned, resold, changed or copied	*
#**	in any way without the express written permission of author	*
#**	and Trans-Mit Pty. Ltd., Australia.				*
#**									*
#*************************************************************************
#**
#**	$Header: /usr/local/lib/cake/RCS/rcshead,v 1.11 2005/11/15 01:44:38 root Exp $
#**	$Author: root $
#**	$Date: 2005/11/15 01:44:38 $
#**	$Locker:  $
#**
#*************************************************************************
#*/

#######################################################

function fixdate(dt)
{
	# change /'s to -'s in  dd/mm/yyyy
	sub("/","-",dt)
	sub("/","-",dt)
	return dt
}

#----------------------------------------------------------
# needed because of rounding bug in awk (see tround1 script)

function mround2(cst)
{
	if ( match(cst,"\...5$") > 0 )
		cst += 0.001
	return sprintf("%0.2f", cst)
}

function mround4(cst)
{
	if ( match(cst,"\.....5$") > 0 )
		cst += 0.00001
	return sprintf("%0.4f", cst)
}


#----------------------------------------------------------

function sanetrim(s)
{
	sub(/^[ \t]*/,"",s)
	sub(/[ \t]*$/,"",s)
	gsub(/\047/,"`",s)
	gsub(/\"/,"",s)
	gsub(/\,/,";",s)
	return s
}

function trim5(s)
{
	sub(/^[ \t]*/,"",s)
	sub(/[ \t]*$/,"",s)
	gsub(/\,/,";",s)
	return substr(s,5)
}

function trim(s)
{
	sub(/^[ \t]*/,"",s)
	sub(/[ \t]*$/,"",s)
	gsub(/\,/,";",s)
	return s
}

function clip(s)
{
	sub(/[ \t]*$/,"",s)
	gsub(/\,/,";",s)
	return s
}

function fixforfname(s)
{
	gsub(/\//,"+",s)
	return s
}


function quote(s)
{
	gsub(/\"/,"\\\"",s)
	return "\"" s "\""
}

#----------------------------------------------------------

# convert MMYYYY MMM_YYYY
function MMYYYY_to_billperiod(MMYYYY,sep)
{
	mnthstr[1]  = "January"
	mnthstr[2]  = "February"
	mnthstr[3]  = "March"
	mnthstr[4]  = "April"
	mnthstr[5]  = "May"
	mnthstr[6]  = "June"
	mnthstr[7]  = "July"
	mnthstr[8]  = "August"
	mnthstr[9]  = "September"
	mnthstr[10] = "October"
	mnthstr[11] = "November"
	mnthstr[12] = "December"

	billmm = 0 + substr(MMYYYY,1,2)
	billyyyy = 0 + substr(MMYYYY,3,4)
	billsdd = 0 + substr(sdate,1,2)
	billedd = 0 + substr(edate,1,2)

	# for back billing output
	syyyymmdd = sprintf("%04s%02s%02s", billyyyy, billmm, billsdd)
	eyyyymmdd = sprintf("%04s%02s%02s", billyyyy, billmm, billedd)

	if ( sep != "" )
		sepchar = substr(sep,1,1)
	else
		sepchar = " "

	# create monthtag/billperiod
	billperiod = substr(mnthstr[billmm],1,3) sepchar billyyyy
	monthtag = substr(mnthstr[billmm],1,3) "_" billyyyy

	#for ( billmm = 1; billmm < 12; ++billmm ) {
	#	if ( substr(mnthstr[billmm],1,3) == substr(billperiod,1,3) )
	#		break
	#}
	if ( billmm > 12 ) {
		printf("FATAL ERROR: BAD billperiod [%s]\n", billperiod)
		exit 1
	}
	#billyyyy = 0 + substr(billperiod,5,4)
	#monthtag = sprintf("%s_%d", substr(mnthstr[billmm],1,3), billyyyy)

	print "MMYYYY = " MMYYYY "  billperiod = " billperiod

	# expanded bill period
	exbillperiod = sprintf("%s %d", mnthstr[billmm], billyyyy)
	printf("exbillperiod = %s\n", exbillperiod)

}



function get_weekday_arr(adate) {
	print "get_weekday_arr(" adate ")"
	mm = substr(adate,4,2)
	yyyy = substr(adate,7,4)
	calcmd = "cal " mm " " yyyy
	wdlncnt = 0
	while ( (calcmd | getline aline) > 0 ) {
		++wdlncnt
		if ( wdlncnt <= 2 )
			continue
		sun = sprintf("%d", substr(aline,1,2))
		mon = sprintf("%d", substr(aline,4,2))
		tue = sprintf("%d", substr(aline,7,2))
		wed = sprintf("%d", substr(aline,10,2))
		thu = sprintf("%d", substr(aline,13,2))
		fri = sprintf("%d", substr(aline,16,2))
		sat = sprintf("%d", substr(aline,19,2))
		if ( sun > 0 ) {
			aweekday = sprintf("%02d-%02d-%04d", sun, mm, yyyy)
			weekday_arr[aweekday] = "Sunday"
		}
		if ( mon > 0 ) {
			aweekday = sprintf("%02d-%02d-%04d", mon, mm, yyyy)
			weekday_arr[aweekday] = "Monday"
		}
		if ( tue > 0 ) {
			aweekday = sprintf("%02d-%02d-%04d", tue, mm, yyyy)
			weekday_arr[aweekday] = "Tuesday"
		}
		if ( wed > 0 ) {
			aweekday = sprintf("%02d-%02d-%04d", wed, mm, yyyy)
			weekday_arr[aweekday] = "Wednesday"
		}
		if ( thu > 0 ) {
			aweekday = sprintf("%02d-%02d-%04d", thu, mm, yyyy)
			weekday_arr[aweekday] = "Thursday"
		}
		if ( fri > 0 ) {
			aweekday = sprintf("%02d-%02d-%04d", fri, mm, yyyy)
			weekday_arr[aweekday] = "Friday"
		}
		if ( sat > 0 ) {
			aweekday = sprintf("%02d-%02d-%04d", sat, mm, yyyy)
			weekday_arr[aweekday] = "Saturday"
		}
	}
	close(calcmd)

	#for ( adate in weekday_arr ) {
	#	print "get weekday_arr[" adate "] = " weekday_arr[adate]
	#}
}


function weekday(adate) {
	aweekday = weekday_arr[adate]
	if ( aweekday == "" ) {
		# create weekday array for days in month
		get_weekday_arr(adate)
		aweekday = weekday_arr[adate]
	}
	return aweekday
}


function is_weekday(adate) {
	aweekday = weekday(adate)
	if ( aweekday == "Sunday" || aweekday == "Saturday" )
		return 0
	return 1
}


#####################################################
#####################################################
#rjs9
# Victrack custom

#--------------------------
# new tbsdata database


function ld_tbsdata_customer(customerfile) {
	printf("ld_tbsdata_customer(%s)\n", customerfile)

	#1|NXS|NEGA (SWANSTON TRAMS) PTY LTD|V6050884|
	# NATIONAL EXPRESS (SWANSTON) PTY LTD|
	# C/O MCGRATH NICOL - ATT: D PIENING|LEVEL 1; 161 COLLINS STREET|
	# MELBOURNE|VIC|3000||0|1|1|Dec 07 2004 12:00AM||robynd|1|

	while ( (getline aline < customerfile) > 0 ) {
		split(aline, a_arr, "|")
		##printf("aline=%s\n", aline)
		CustomerID = trim(a_arr[1])
		ShipTo = trim(a_arr[2])
		FullName = trim(a_arr[3])
		ShipToCode = trim(a_arr[4])
		Address01 = trim(a_arr[5])

		ShipTo = toupper(ShipTo)
		ShipToDesc = FullName

		CustomerDesc = ShipToDesc
		#if ( ShipTo == substr(ShipToDesc,1,length(ShipTo)) )
		#	CustomerDesc = substr(ShipToDesc,length(ShipTo)+1)
		if ( substr(CustomerDesc,1,1) == " " )
			CustomerDesc = substr(CustomerDesc,2)
		if ( CustomerDesc != "" )
			CustomerDesc = CustomerDesc " - " ShipTo
		else
			CustomerDesc = ShipTo

		ShipTo_to_ShipToDesc_arr[ShipTo] = ShipToDesc
		ShipTo_to_ShipToCode_arr[ShipTo] = ShipToCode
		ShipTo_to_CustomerDesc_arr[ShipTo] = CustomerDesc
		ShipTo_to_Address_arr[ShipTo] = Address01

		CustomerID_to_ShipTo_arr[CustomerID] = ShipTo

		print "    ShipTo = " ShipTo
		print "    ShipTo_to_ShipToDesc_arr[" ShipTo "] = " ShipTo_to_ShipToDesc_arr[ShipTo]
		print "    ShipTo_to_ShipToCode_arr[" ShipTo "] = " ShipTo_to_ShipToCode_arr[ShipTo]
		print "    CustomerID_to_ShipTo_arr[" CustomerID "] = " CustomerID_to_ShipTo_arr[CustomerID]
		print "    ShipTo_to_CustomerDesc_arr[" ShipTo "] = " ShipTo_to_CustomerDesc_arr[ShipTo]
	}
	close(customerfile)
}


function ld_tbsdata_servicetype(servicetypefile) {
	printf("ld_tbsdata_servicetype(%s)\n", servicetypefile)

	#99|XA|SMS MESSAGES|194|140|140|0||0|1|Dec 07 2004 12:00AM|
	# |1|0|0|0.0000|0|

	while ( (getline aline < servicetypefile) > 0 ) {
		split(aline, a_arr, "|")
		ServiceTypeID = trim(a_arr[1])
		ServiceTypeCode = trim(a_arr[2])
		SupplierID = trim(a_arr[4])
		RevenueElementID = trim(a_arr[5])
		SystemID = trim(a_arr[8])

		tascode = ServiceTypeCode
		tascode_to_SystemID_arr[tascode] = SystemID
		tascode_to_ElementID_arr[tascode] = RevenueElementID
		tascode_to_orgElementID_arr[tascode] = RevenueElementID
		tascode_to_SupplierID_arr[tascode] = SupplierID

		print "    tascode = " tascode
		print "    tascode_to_SystemID_arr[" tascode "] = " tascode_to_SystemID_arr[tascode]
		print "    tascode_to_ElementID_arr[" tascode "] = " tascode_to_ElementID_arr[tascode]
		print "    tascode_to_SupplierID_arr[" tascode "] = " tascode_to_SupplierID_arr[tascode]
	}
	close(servicetypefile)
}


function ld_tbsdata_element(elementfile) {
	printf("ld_tbsdata_element(%s)\n", elementfile)

      #1|1046|Telco Account Processing Fee||1|1|May 10 2005 12:00AM||499|10|0|

	while ( (getline aline < elementfile) > 0 ) {
		split(aline, a_arr, "|")
		##printf("aline=%s\n", aline)
		#printf("a_arr 1=%s 3=%s 4=%s\n", a_arr[1], a_arr[3], a_arr[4])
		ElementID = trim(a_arr[1])
		ElementCode = trim(a_arr[2])
		ElementCodeDesc = trim(a_arr[3])
		CustomerID = trim(a_arr[9])

		if ( CustomerID != "499" )	# only look at VRT entries
			continue

		SGID = 0
		SGIDdesc = "All"

		SEID = ElementCode
		SEIDdesc = ElementCodeDesc

		SGID_to_SGIDdesc_arr[SGID] = SGIDdesc

		SEID_to_SEIDdesc_arr[SEID] = SEIDdesc
		SEID_to_SGID_arr[SEID] = SGID

		ElementID_to_SEID_arr[ElementID] = SEID
		SEID_to_orgElementID_arr[SEID] = ElementID

		print "    SGID = " SGID "  SEID = " SEID
		print "    SEID_to_SGID_arr[" SEID "] = " SEID_to_SGID_arr[SEID]

		print "    SGID_to_SGIDdesc_arr[" SGID "] = " SGID_to_SGIDdesc_arr[SGID]
		print "    SEID_to_SEIDdesc_arr[" SEID "] = " SEID_to_SEIDdesc_arr[SEID]

		print "    ElementID_to_SEID_arr[" ElementID "] = " ElementID_to_SEID_arr[ElementID]
	}
	close(elementfile)
}


function ld_tbsdata_supplier(supplierfile) {
	printf("ld_tbsdata_supplier(%s)\n", supplierfile)

	#1|OPTUS COMMUNICATIONS|1||robynd|OPT001|1|OPTUS COMMUNICATIONS|
	# LEVEL 6|50 MILLER STREET|NORTH SYDNEY|NSW|2060|

	while ( (getline aline < supplierfile) > 0 ) {
		split(aline, a_arr, "|")
		##printf("aline=%s\n", aline)
		#printf("a_arr 1=%s 7=%s\n", a_arr[1], a_arr[7])

		SupplierID = trim(a_arr[1])
		SupplierName = trim(a_arr[2])

		split(SupplierName, SupplierName_arr, " ")
		supplier = toupper(SupplierName_arr[1])

		SupplierID_to_supplier_arr[SupplierID] = supplier

		print "      SupplierID = " SupplierID
		print "    SupplierName = " SupplierName
		print "        supplier = " supplier
		print "    SupplierID_to_supplier_arr[" SupplierID "] = " SupplierID_to_supplier_arr[SupplierID]
	}
	close(supplierfile)
}


function ld_tbsdata_person(personfile) {
	printf("ld_tbsdata_person(%s)\n", personfile)
	while ( (getline aline < personfile) > 0 ) {
		split(aline, a_arr, "|")
		#printf("aline=%s\n", aline)

		f = 0
		PersonID = sanetrim(a_arr[++f])
		Person = sanetrim(a_arr[++f])
		#StartDate = sanetrim(a_arr[++f])
		#EndDate = sanetrim(a_arr[++f])
		#Active = sanetrim(a_arr[++f])
		emailaddress = sanetrim(a_arr[++f])

		dirID = "Person" PersonID

		recordno = dirID
		master = "Y"
		t21surname = Person
		t21firstname = ""

		#staffid = emailaddress
		staffid = "P" PersonID

		# ??? see personservicelist
		#t21division = "ToBeSet"			# (ShipTo)
		#t21location = "ToBeSet"
		#t21department = "tbsdept ToBeSet"
		#t21site = "tbssite ToBeSet"

		extn = "tbsextn ToBeSet"

		if ( t21surname == "" )
			t21surname = "_blank_"
		if ( t21firstname == "" )
			t21firstname = "_blank_"

		if ( t21location == "" )
			t21location = "_blank_"

		t21name = t21surname
		if ( t21firstname != "_blank_")
			t21name = sprintf("%s %s", t21firstname, t21name)

		recordno_to_master_arr[recordno] = master

		recordno_to_t21name_arr[recordno] = t21name
		print " tPerson  recordno_to_t21name_arr[" recordno "] = [" recordno_to_t21name_arr[recordno] "]"

		recordno_to_staffid_arr[recordno] = staffid
		print " tPerson  recordno_to_staffid_arr[" recordno "] = [" recordno_to_staffid_arr[recordno] "]"

		recordno_to_email_arr[recordno] = emailaddress
		print " tPerson  recordno_to_email_arr[" recordno "] = [" recordno_to_email_arr[recordno] "]"

		email_to_tPrecordno_arr[emailaddress] = recordno
		print " tPerson  email_to_tPrecordno_arr[" emailaddress "] = [" email_to_tPrecordno_arr[emailaddress] "]"

		recordno_to_t21surname_arr[recordno] = t21surname
		recordno_to_t21firstname_arr[recordno] = t21firstname

		# ??? see personservicelist
		#recordno_to_t21location_arr[recordno] = t21location
		#recordno_to_t21division_arr[recordno] = t21division
		#recordno_to_t21department_arr[recordno] = t21department
		#recordno_to_t21site_arr[recordno] = t21site

		recordno_to_extn_arr[recordno] = extn

	}
	close(personfile)
}


#-----------------------------------------------------------------
# to replace info from Telmax21 Directory

function ld_tbsdata_personservicelist(personservicelistfile) {
	printf("ld_tbsdata_personservicelist(%s)\n", personservicelistfile)

	while ( (getline aline < personservicelistfile) > 0 ) {
		split(aline, a_arr, "|")
		#printf("aline=%s\n", aline)

		f = 0
		PersonID = sanetrim(a_arr[++f])
		Person = sanetrim(a_arr[++f])
		ServiceIDID = sanetrim(a_arr[++f])
		ServiceID = sanetrim(a_arr[++f])
		ShipTo = sanetrim(a_arr[++f])
		FullName = sanetrim(a_arr[++f])
		LocationID = sanetrim(a_arr[++f])
		LocationDesc = sanetrim(a_arr[++f])
		DepartmentID = sanetrim(a_arr[++f])
		DepartmentDesc = sanetrim(a_arr[++f])

		dirID = "Person" PersonID

		recordno = dirID

		t21division = ShipTo
		t21location = LocationDesc
		t21department = DepartmentDesc

		t21site = ShipTo

		recordno_to_t21division_arr[recordno] = t21division
		recordno_to_t21location_arr[recordno] = t21location
		recordno_to_t21department_arr[recordno] = t21department
                recordno_to_t21site_arr[recordno] = t21site

		print " tbspersonservicelist:  recordno_to_t21division_arr[" recordno "] = [" recordno_to_t21division_arr[recordno] "]"
		print " tbspersonservicelist:  recordno_to_t21location_arr[" recordno "] = [" recordno_to_t21location_arr[recordno] "]"
		print " tbspersonservicelist:  recordno_to_t21department_arr[" recordno "] = [" recordno_to_t21department_arr[recordno] "]"

		# a person in  tPerson can be against multiple serviceid's
		# so can't do this
		##recordno_to_eggrid_arr[recordno] = extgroupunique
		#recordno_to_eggrid_arr[recordno] = ServiceID
		#print " tbspersonservicelist  recordno_to_eggrid_arr[" recordno "] = [" recordno_to_eggrid_arr[recordno] "]"

		# (mobile to person done in gettbsmobconf script)
		#if ( match(ServiceID,/^04..-......$/) <= 0 ) 
		#	mobile = ""
		#else
		#	mobile = substr(ServiceID,1,2) substr(ServiceID,4)
		#mobile_to_directoryID_arr[mobile] = dirID
		#print "    mobile_to_directoryID_arr[" mobile "] = " mobile_to_directoryID_arr[mobile]

		# for m11cdr
		sid_to_directoryID_arr[ServiceID] = dirID
		print " tbspersonservicelist: sid_to_directoryID_arr[" ServiceID "]=[" sid_to_directoryID_arr[ServiceID] "]"

	}
	close(personservicelistfile)
}


#-----------------------------------------------------------------

#rjs9
#function ld_seidtranslate(seidtranslatefile) {
#	printf("ld_seidtranslate(%s)\n", seidtranslatefile)
#	while ( (getline aline < seidtranslatefile) > 0 ) {
#		split(aline, a_arr, ",")
#		##printf("aline=%s\n", aline)
#		#printf("a_arr 1=%s 7=%s\n", a_arr[1], a_arr[7])
#
#		systemid = trim(a_arr[1])
#		seid = trim(a_arr[2])
#
#		systemid_to_seid[systemid] = seid
#
#		print "systemid_to_seid[" systemid "] = " systemid_to_seid[systemid]
#	}
#	close(seidtranslatefile)
#}



#-----------------------------------------------------------------

function get_cdeflds(cdefile) {
	printf("get_cdeflds(%s)\n", cdefile)
	cdeflds = 0
	while ( (getline aline < cdefile) > 0 ) {
		gsub("\r","",aline)
		i = split(aline, a_arr, "|")
		cdeflds = i
		break;
	}
	close(cdefile)
	print "cdeflds=" cdeflds
}

function ld_cde(cdefile) {
	printf("ld_cde(%s)\n", cdefile)
	while ( (getline aline < cdefile) > 0 ) {
		gsub("\r","",aline)
		i = split(aline, a_arr, "|")
		print "ld_cde i=" i " aline:" aline
		if ( i != 29 && i != 30 ) {
			print "WARNING: bad cde record in file " cdefile " [" aline "]" " i=" i
			continue
		}
		print "got cde i=" i
		#print cdefile ": " aline
		##printf("aline=[%s]\n", aline)
		#printf("a_arr 1=%s\n", a_arr[1])
		f = 0
		shipto = trim(a_arr[++f])
		sundry = trim(a_arr[++f])
		rentcost = trim(a_arr[++f])
		callcost = trim(a_arr[++f])
		othercost = trim(a_arr[++f])
		admincost = trim(a_arr[++f])
		totalcost = trim(a_arr[++f])

		batchtypeid = trim(a_arr[++f])
		batchtypedesc = trim(a_arr[++f])

		reportgroupid = trim(a_arr[++f])
		reportgroupdesc = trim(a_arr[++f])
		reportorder = trim(a_arr[++f])

		tascode = trim(a_arr[++f])
		tbsSID = trim(a_arr[++f])
		location = trim(a_arr[++f])
		user = trim(a_arr[++f])

		tbscentreid = trim(a_arr[++f])
		tbscentre = trim(a_arr[++f])
		activity = trim(a_arr[++f])
		element = trim(a_arr[++f])
		subledger = trim(a_arr[++f])
		paytype = trim(a_arr[++f])

		deptid = trim(a_arr[++f])
		department = trim(a_arr[++f])
		servicedesc = trim(a_arr[++f])
		installdate = trim(a_arr[++f])
		std = trim(a_arr[++f])

		tbsperiod = trim(a_arr[++f])

		if ( cdeflds >= 30 )
			invoicedetailid = trim(a_arr[++f])

		#--------------------------

		SHIPTO = toupper(shipto)

		# cant have "/" in tbsSID (re: filenames)
		gsub("/",".",tbsSID)

		#print "SHIPTO=[" SHIPTO "]" " batchtypeid=[" batchtypeid "]" " batchtypedesc=[" batchtypedesc "]" " tbsSID=[" tbsSID "]"

		sid_sundry_to_batchtypeid_arr[tbsSID "|" sundry] = batchtypeid
		batchtypeid_to_batchtypedesc_arr[batchtypeid] = batchtypedesc

		# Testing
	        #if ( Testing >= 3 && index("|ANN|VLP|VLS|VLO|VLI|VNA|VTC|VNE|VSE|VSW|VRT|", "|" SHIPTO "|") <= 0 ) {
	        if ( Testing >= 3 && index("|A|D|K|S|V|", "|" substr(SHIPTO,1,1) "|") <= 0 ) {
			#print "CDE TESTING... SKIP  SHIPTO = " SHIPTO
			continue
		}

		# for Directory Description in no person assigned
		sid_sundry_to_servicedesc_arr[tbsSID "|" sundry] = servicedesc

                if ( sid_to_sundrylist_arr[tbsSID] != "" )
                        sid_to_sundrylist_arr[tbsSID] = sid_to_sundrylist_arr[tbsSID] "|"
                sid_to_sundrylist_arr[tbsSID] = sid_to_sundrylist_arr[tbsSID] sundry

		############################
		costcentre = sprintf("%-6.6s %-2.2s %-6.6s %-6.6s %-1.1s", tbscentre, activity, element, subledger, paytype)

		#ElementID = tascode_to_ElementID_arr[tascode]
		#print "        tascode_to_ElementID_arr[" tascode "] = [" tascode_to_ElementID_arr[tascode] "]"

		#itag = SHIPTO "|" sundry
		orgElementID = ElementID = tascode_to_orgElementID_arr[tascode]
		itag = SHIPTO "|" sundry "|" orgElementID
		if ( cdeflds < 30 ) {
			ididtag = SHIPTO "|" sundry "|" orgElementID
		}
		else {
			#ididtag = SHIPTO "|" sundry "|" orgElementID "|" invoicedetailid
			ididtag = SHIPTO "|" sundry "|" invoicedetailid
		}


 		platinv_batch = parent_sundry_to_platinv_batch_arr[ididtag]

		print "         itag=" itag "  platinv_batch = " platinv_batch
		print "      ididtag=" ididtag

		if ( platinv_batch == "" ) {
			if ( done_warn["cde_platinv:" SHIPTO "|" sundry] == "" ) {
				print "WARNING: Can't get Platinum Invoice batch for SHIPTO = " SHIPTO "  sundry = " sundry
				done_warn["cde_platinv:" SHIPTO "|" sundry] = "1"
			}
		}

		#if ShipTo code in tbs Customer is blank then set SHIPTO to VRT
		if ( ShipTo_to_ShipToCode_arr[SHIPTO] == "" ) {
			if ( done_warn["blank_shiptocode:" SHIPTO] == "" ) {
				print "WARNING: blank ShiptoCode, setting SHIPTO to VRT"
				done_warn["blank_shiptocode:" SHIPTO] = "1"
			}
			SHIPTO = "VRT"
			#itag = SHIPTO "|" sundry
			itag = SHIPTO "|" sundry "|" orgElementID
			platinv_batch = "VRTBATCHFILENAME"
			parent_sundry_to_platinv_batch_arr[ididtag] = platinv_batch
			invoiceno = "VRT999999"
			parent_sundry_to_platinv_invoicenumber_arr[ididtag] = invoiceno
		}
		# if ShipTo code in tbs Customer is VRT
		if ( ShipTo_to_ShipToCode_arr[SHIPTO] == "VRT" ) {
			#print "ShiptoCode = VRT, setting dummy platinv_batch"
			SHIPTO = "VRT"
			#itag = SHIPTO "|" sundry
			itag = SHIPTO "|" sundry "|" orgElementID
			platinv_batch = "VRTBATCHFILENAME"
			parent_sundry_to_platinv_batch_arr[ididtag] = platinv_batch
			invoiceno = "VRT999999"
			parent_sundry_to_platinv_invoicenumber_arr[ididtag] = invoiceno
		}

		SupplierID = tascode_to_SupplierID_arr[tascode]
		#old tassemap supplier = tascode_to_supplier_arr[tascode]
		supplier = SupplierID_to_supplier_arr[SupplierID]

		#old tassemap SEID = tascode_to_SEID_arr[tascode]
		SEID = ElementID_to_SEID_arr[ElementID]
		#print "        ElementID_to_SEID_arr[" ElementID "] = [" ElementID_to_SEID_arr[ElementID] "]"

		orgSEID = ElementID_to_SEID_arr[orgElementID]
		#print "orgSEID = " orgSEID

		# 05/12/2007
		# all internal and external network services for VRT
		# are now set to seid 2399 in tbsdata
		seidchange = 0
		actualSEID = SEID
		#if ( SEID == 2399 ) {
		# do for all (for VoiceMail in non VRT and future)

			# rjs9-
			## for mobile - translate seid using systemid
			#systemid = tascode_to_SystemID_arr[tascode]
			#newseid = systemid_to_seid[systemid]
			#if ( newseid != "" ) {
			#	print " translate SEID=" SEID "   translate newseid=" newseid "  systemid=" systemid
			#	SEID = newseid
			#	seidchange = 1
			#}	
			#else {
			#	# use tbsSID to determine Telmax21 data
			#	newseid = 101043
			#	if ( grid_to_grtype_arr[tmpsid] == "E" ) {
			#		print " SEID=" SEID "   newseid=" newseid "  tbsSID=" tbsSID
			#		SEID = newseid 
			#		seidchange = 1
			#	}
			#	else { # test for Switchboard or Voicemail
			#		if ( substr(tbsSID,1,2) == "SW" ) {
			#			tmpsid = substr(tbsSID,3)
			#			newseid = 101030
			#		}
			#		if ( substr(tbsSID,1,2) == "VM" ) {
			#			tmpsid = substr(tbsSID,3)
			#			newseid = 101047
			#		}
			#		if (grid_to_grtype_arr[tmpsid] == "E") {
			#			print " SEID=" SEID "   newseid=" newseid "  tbsSID=" tbsSID "  tmpsid=" tmpsid
			#			SEID = newseid
			#			seidchange = 1
			#		}
			#	}
			#}

			# to determine if its a mobile
			# match tbsSID with 04..-......
			if ( seidchange == 0 && match(tbsSID, "04[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]") ) {
				# if not
				#batchtypeid != 141 && # VT Call Diversion
				#batchtypeid != 304 && # Smartbus BGPRS
				#batchtypeid != 461 && # Kamco 
				#batchtypeid != 446 && # Smartbus Aggregate
				if ( batchtypeid != 141 && # VT Call Diversion
				     reportgroupid != 15 && # Smartbus GPRS
				     reportgroupid != 19 && # Smartbus Aggregate
				     1 == 1 ) {
					print " mobile - SEID=" SEID "  newseid=" newseid "  tbsSID=" tbsSID
					SEID = 101044
					seidchange = 1

					# set carrier code from supplier name
					carriercode = tolower(substr(supplier,1,1))
					mobile = substr(tbsSID,1,4)  substr(tbsSID,6,6)
					mobile_to_mob_carriercode_arr[mobile] = carriercode
					#print "setcarriercode: mobile_to_mob_carriercode_arr[" mobile "]=[" mobile_to_mob_carriercode_arr[mobile] "]"
				}
			}	

			## to determine if its T21 calls/switchboard/voicemail
			## use tascode & tbsSID
			#if ( seidchange == 0 && index("|TQ|TQ1|TQD|TQQ|TQS|VOI|VS1|VSW|VV1|", "|" tascode "|") > 0 ) {
			#	# T21 calls
			#	newseid = 101043
			#
			#	# T21 switchboard
			#	if ( substr(tbsSID,1,2) == "SW" )
			#		newseid = 101030
			#
			#	# T21 voicemail
			#	if ( substr(tbsSID,1,2) == "VM" )
			#		newseid = 101047
			#
			#	SEID = newseid
			#	seidchange = 1
			#	print " T21 - SEID=" SEID "  newseid=" newseid "  tbsSID=" tbsSID "  tascode=" tascode
			#}

			# to determine if it is T21 calls/switchboard/voicemail
			# use t21egrid lookup
			t21seid = t21egrid_to_seid_arr[tbsSID]
			#print "t21seid=[" t21seid "]"
			if (seidchange == 0 && t21seid != "" ) {
				newseid = t21seid
				SEID = newseid
				seidchange = 1
				print " T21 - SEID=" SEID "  newseid=" newseid "  tbsSID=" tbsSID "  tascode=" tascode
			}
			# -rjs9

		#}
		## SEID is still 2399 so,
		## this is to prevent hardcodes in reports from
		## creating drill down to t21 call/rent for
		## non drilldown services
		#if ( SEID == 2399 ) {
		#	SEID = 102399
		#	SEID_to_SEIDdesc_arr[SEID] = SEID_to_SEIDdesc_arr[2399]
		#	print " no translate for SEID=2399   setto=" SEID "  systemid=" systemid
		#	seidchange = 1
		#}

		# set elementid to seid lookup for replaced VRT 2399 seid
		if ( seidchange == 1 ) {
			# use replaced SEID+100000 as elementid
			newelementid = SEID + 100000
			tascode_to_ElementID_arr[tascode] = newelementid
			ElementID_to_SEID_arr[newelementid] = SEID

			SEID_to_orgElementID_arr[SEID] = ElementID

			print "new ElementID_to_SEID_arr[" newelementid "] = " ElementID_to_SEID_arr[newelementid]
			SEID_to_SGID_arr[SEID] = 0

			# create seid desc for new seid's set here
			# preserve switchboard seid desc
			SEID_to_SEIDdesc_arr[101030] = SEID_to_SEIDdesc_arr[1030]

			# VOICEMAIL has no entry in tElement
			SEID_to_SEIDdesc_arr[101047] = "Telco VoiceMail"

			# preserve mobile seid desc
			SEID_to_SEIDdesc_arr[101044] = SEID_to_SEIDdesc_arr[1044]

			# preserve T21 Rent and Calls seid desc
			SEID_to_SEIDdesc_arr[101043] = SEID_to_SEIDdesc_arr[1043]
		}

		SEIDdesc = SEID_to_SEIDdesc_arr[SEID]
		if ( SEIDdesc == "" )
			SEIDdesc = "Blank SEIDdesc"

		print "     use SEID = " SEID "  SEIDdesc = [" SEIDdesc "]"
		print "         tascode=" tascode "  supplier=" supplier

		# store sundry for T21 calls
		if ( tbsperiod == TBSBillingPeriod &&	# current period only
		     supplier == t21_supplier &&	# Victrack
		     SEID == 101043 ) {			# T21 calls 	
			# save TIMS calls sundry
			if ( t21_sundry != "" &&
			     t21_sundry != sundry ) {
				print "FATAL ERROR: multiple TIMS calls Sundrys t21_sundry = " t21_sundry " next sundry=" sundry
                                        exit 1
                        }
                        else {
                                # save TIMS calls sundry
                                t21_sundry = sundry
                                print "           t21_sundry = " t21_sundry
                        }
			if ( platinv_batch != "VRTBATCHFILENAME" ) {
				# save TIMS calls platinv_batch
				t21_platinv_batch[platinv_batch] = 1
				print "           t21 platinv_batch = " platinv_batch
			}
		}

		bmSERVTYPE = ""
		sedescextra = ""

		if ( SEID == 101043 ) {		# t21 calls
			bmSERVTYPE = 1
			sedescextra = "Calls"
		#	SERVTYPE = bmSERVTYPE
		#	# change desc to that of tbs
		#	#SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = SEIDdesc
		#	SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = servtypedesc
		}
		if ( SEID == 101030 ) {	# t21 switchboard calls
			bmSERVTYPE = SEID
			sedescextra = "Switchboard"
		#	SERVTYPE = bmSERVTYPE
		#	# use SEID for SERVTYPE
		#	#SERVTYPE = 1.1
		#	#SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = SEIDdesc
		#	SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = servtypedesc " - Switchboard"
		}
		if ( SEID == 101047 ) {	# t21 voicemail calls
			bmSERVTYPE = SEID
			sedescextra = "Voicemail"
		#	SERVTYPE = bmSERVTYPE
		#	# use SEID for SERVTYPE
		#	#SERVTYPE = 1.2
		#	#SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = SEIDdesc
		#	SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = servtypedesc " - Voicemail"
		}
		if ( SEID == 101044 ) {	# mobile
			bmSERVTYPE = 10
			#sedescextra = "Mobile"
		#	SERVTYPE = bmSERVTYPE
		#	# change desc to that of tbs
		#	#SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = SEIDdesc
		#	SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = servtypedesc
		}

		# reportgroup lookup
		sundryEGRID_to_reportgroupid_arr[sundry "|" tbsSID] = reportgroupid
		sundryEGRID_to_reportgroupdesc_arr[sundry "|" tbsSID] = reportgroupdesc
		sundryEGRID_to_reportorder_arr[sundry "|" tbsSID] = reportorder

		# tascode lookup
		sundryEGRID_to_tascode_arr[sundry "|" tbsSID] = tascode

		# invoiceno lookup
		sundryEGRID_to_invoiceno_arr[sundry "|" tbsSID] = invoiceno
	
		# location and user lookup
		sundryEGRID_to_location_arr[sundry "|" tbsSID] = location
		sundryEGRID_to_user_arr[sundry "|" tbsSID] = user

		# SEID lookup
		sundryEGRID_to_SEID_arr[sundry "|" tbsSID] = SEID


		if ( SID_to_sundry_arr[tbsSID] != "" && SID_to_sundry_arr[tbsSID] != sundry )
			print "LDCDEWARN: SID_to_sundry_arr[" tbsSID "]=[" SID_to_sundry_arr[tbsSID] "] set but not to sundry=[" sundry "]"
		else
			SID_to_sundry_arr[tbsSID] = sundry

		if ( SID_to_tascode_arr[tbsSID] != "" && SID_to_tascode_arr[tbsSID] != tascode )
			print "LSCDEWARN: SID_to_tascode_arr[" tbsSID "]=[" SID_to_tascode_arr[tbsSID] "] set but not to tascode=[" tascode "]"
		else
			SID_to_tascode_arr[tbsSID] = tascode

		if ( SID_to_invoiceno_arr[tbsSID] != "" && SID_to_invoiceno_arr[tbsSID] != invoiceno )
			print "LDCDEWARN: SID_to_invoiceno_arr[" tbsSID "]=["  SID_to_invoiceno_arr[tbsSID] "] set but not to invoiceno=[" invoiceno "]"
		else
			SID_to_invoiceno_arr[tbsSID] = invoiceno

		# only doing thisSHIPTO
		if ( thisSHIPTO != "all" && thisSHIPTO != SHIPTO )
			continue

		## rjs
		#if ( platinv_batch == "TBSAUG2004001134" ) {
		#	print "Warning 1 Skipping platinv_batch = " platinv_batch
		#	continue
		#}
		# only doing this platinumbatch
		if (platinumBATCH != "all" && platinumBATCH != platinv_batch)
			continue

		invoiceno = parent_sundry_to_platinv_invoicenumber_arr[ididtag]

		print "         itag=" itag "  invoiceno = " invoiceno

		if ( invoiceno == "" ) {
			print "WARNING: NO INVOICE for shipto = " SHIPTO " and sundry = " sundry "  tbsSID = " tbsSID "  tascode_to_orgElementID_arr[" tascode "] = " tascode_to_orgElementID_arr[tascode]
			#print "WARNING: NO INVOICE for shipto = " SHIPTO " and sundry = " sundry "  tbsSID = " tbsSID
			#platinv_batch = SHIPTO monthtag "NOINV"
			#parent_sundry_to_platinv_batch_arr[ididtag] = platinv_batch
			#invoiceno = SHIPTO "_" "NOINV"
			#parent_sundry_to_platinv_invoicenumber_arr[ididtag] = invoiceno
			continue
		}

		# flag parent / sundry in cde
		cde_parent_sundry_arr[itag] = 1
		print "cde_parent_sundry_arr[" itag "]=[" cde_parent_sundry_arr[itag] "]"

		#ElementID = tascode_to_ElementID_arr[tascode]
		##old tassemap SEID = tascode_to_SEID_arr[tascode]
		#SEID = ElementID_to_SEID_arr[ElementID]
		
		SGID = SEID_to_SGID_arr[SEID]

		DEPTdesc = department
		# rjs9 - deptid added to getcde script
		## assign unique departmnent id
		#DEPT = DEPTdesc_to_DEPT_arr[DEPTdesc]
		#if ( DEPT == 0 ) {
		#    DEPTdesc_to_DEPT_arr[DEPTdesc] = ++MAXDEPT
		#    DEPT = MAXDEPT
		#    DEPT_to_DEPTdesc_arr[DEPT] = DEPTdesc
		#    #printf("NEW   SHIPTO = %s DEPTdesc = (%d)%s    MAXDEPT = %d\n", SHIPTO, DEPT, DEPTdesc, MAXDEPT)
		#    pipecmd = "sort >" logdir "/" "DEPT_" batchtag ".lst"
		#    print SHIPTO "|" DEPT "|" DEPTdesc | pipecmd
		#}
		DEPT = "DEPT" deptid
		DEPT_to_DEPTdesc_arr[DEPT] = DEPTdesc

		GROUP = grid_to_parentgroupid_arr[DEPT]

		if ( GROUP == "" ) {
			GROUP = "GRPNOTFOUNDFOR" DEPT
			GROUPdesc = "No Group Found for DEPT=" DEPT
			grid_to_grname3_arr[GROUP] = GROUPdesc
			grid_to_parentgroupid_arr[DEPT] = GROUP
			grid_to_parentgroupid_arr[GROUP] = "VRT"
		}

		tbsCENTREID = "CENTRE" tbscentreid

		## use invoicedeatil desc for service typ desc
		#if ( platinv_particular != "" ) {
		#	print " using platinv_particular=[" platinv_particular "]" "  (sundry = " sundry ")"
		#	servtypedesc = SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]
		#	print "    to replace SERVTYPE=" SERVTYPE "  desc = [" servtypedesc "]"
		#	SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = platinv_particular
		#	if ( ++replaced_servdesc_count_arr[SERVTYPE] > 1 ) {
		#		print "    was already replaced"
		#	}
		#}

		## use batchtype desc for service typ desc
		##SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = batchtypedesc
		#if ( batchtypedesc != "" ) {
		#	print " using batchtypedesc=[" batchtypedesc "]" "  (sundry = " sundry ")"
		#	servtypedesc = SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]
		#	print "    to replace SERVTYPE=" SERVTYPE "  desc = [" servtypedesc "]"
		#	SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = batchtypedesc
		#	if ( ++replaced_servdesc_count_arr[SERVTYPE] > 1 ) {
		#		print "    was already replaced"
		#	}
		#}
		
		#batchtypeinfo = "(" batchtypeid ": " batchtypedesc ")"
		#print " from  batchtype [" batchtypeinfo "]"

		#reportgroupinfo  = "(" reportgroupid ": " reportgroupdesc ": " reportorder ")"
		#print " from reportgroup [" reportgroupinfo "]"

		platinv_particular = sundry_to_particular[sundry]

		#SERVTYPE = SEID
		#SERVTYPE = batchtypeid
		if ( bmSERVTYPE != "" ) {
			print " isbm"
			SERVTYPE = bmSERVTYPE
		}
		else {
			print " notbm"
			SERVTYPE = batchtypeid + 3000000
		}
		if ( SERVTYPE == 10 ) {	
			servtypedesc = SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]
		}
		else {
			if ( batchtypedesc != "" )
				servtypedesc = batchtypedesc
			else
				servtypedesc = SEIDdesc
			if ( sedescextra != "" )
				servtypedesc = servtypedesc " - " sedescextra
			SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] = servtypedesc
		}

		print " SERVTYPE_to_SERVTYPEdesc_arr[" SERVTYPE "]=[" SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE] "]"
	
		debuginfo = "RptGrpID:" reportgroupid
		debuginfo = debuginfo "(" supplier ";" invoiceno ";" sundry ")"
		debuginfo = debuginfo " " "(SEIDdesc:" SEIDdesc ";INVDETdesc:" platinv_particular ")"
		debuginfo = debuginfo  " " "[SERVTYPE:" SERVTYPE "(" (bmSERVTYPE != "" ? "isbm" : "notbm") "):" servtypedesc ";SEID:" orgSEID ";EID:" orgElementID "]"

		print "  debuginfo = " debuginfo
		#servtypedesc = servtypedesc " " debuginfo

		# moved from ld_t21groups()
		CENTRE = DEPT
		PROJECT = tbsCENTREID
	
		# for t21 calls etc
		grid_to_ccp_arr[tbsSID] = tbscentre
		grid_to_centre_arr[tbsSID] = CENTRE
		grid_to_project_arr[tbsSID] = PROJECT

                #print "grid_to_centre_arr[" tbsSID "]  = [" grid_to_centre_arr[tbsSID] "]"
                #print "grid_to_project_arr[" tbsSID "]  = [" grid_to_project_arr[tbsSID] "]"
		CENTREPROJECT_to_costcentre_arr[CENTRE "|" PROJECT] = costcentre

		CENTRE_to_ShipTo_arr[CENTRE] = SHIPTO
		#print "CENTRE_to_ShipTo_arr[" CENTRE "] = [" CENTRE_to_ShipTo_arr[CENTRE] "]"
		#CENTREPROJECT_to_ShipTo_arr[CENTRE "|" PROJECT] = SHIPTO
		#print "CENTREPROJECT_to_ShipTo_arr[" CENTRE "|" PROJECT "] = [" CENTREPROJECT_to_ShipTo_arr[CENTRE "|" PROJECT] "]"

		CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" tbsSID] = tbsSID
		#print "CENTREPROJECTSID_to_EGRID_arr[" CENTRE "|" PROJECT "|" tbsSID "] = [" CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" tbsSID] "]"

		#-rjs9

		# deptid lookup (used to get deptid fronm t21 dir entries)
		itag = SHIPTO "|" tbsSID
		# rjs. 22/11/2007
		#if ( parentSID_to_DEPT_arr[itag] != "" && parentSID_to_DEPT_arr[itag] != DEPT ) {
		#	# rjs skip multiple
		#	if ( tbsSID == "INV 2253177" )
		#		continue
		#	if ( tbsSID == "INV 2257030" )
		#		continue
		#	printf("FATAL ERROR: tbsSID=[%s] has multile deptid's for parent = %s\n", tbsSID, SHIPTO)
		#	exit 1
		#}
		parentSID_to_DEPT_arr[itag] = DEPT

		# X totals for report csv headers
		Xtottag = SERVTYPE "|" SHIPTO "|" GROUP "|" CENTRE "|" PROJECT "|" supplier "|" invoiceno "|" sundry "|" tbsSID
		Xrentcost_arr[Xtottag] = rentcost
		Xcallcost_arr[Xtottag] = callcost
		Xothercost_arr[Xtottag] = othercost
		Xadmincost_arr[Xtottag] = admincost
		Xtotalcost_arr[Xtottag] = totalcost
		#if ( tbsSID == "SW593WTW" )
		#	print "set Xtotalcost_arr[" Xtottag "]=[" Xtotalcost_arr[Xtottag] "]"

		#------------------------
		# chech values for building HIERIDx
		if (SERVTYPE == "" || SHIPTO == "" || GROUP == "" || CENTRE == "" || PROJECT == "" || SGID == "" || SEID == "" || DEPT == "" || tbsCENTREID == "" || tbsSID == "" || supplier == "" || tascode == "" || sundry == "" || invoiceno == "") {
			print "FATAL ERROR: BAD for set_HIERIDx() (1 or more blank fields)"
			print "           SERVTYPE = " SERVTYPE
			print "             SHIPTO = " SHIPTO
			print "              GROUP = " GROUP
			print "             CENTRE = " CENTRE
			print "            PROJECT = " PROJECT
			print "               SGID = " SGID
			print "               SEID = " SEID "     (ElementID = " ElementID ")"
			print "               DEPT = " DEPT
			print "        tbsCENTREID = " tbsCENTREID
			print "             tbsSID = " tbsSID
			print "           supplier = " supplier
			print "            tascode = " tascode
			print "           location = " location
			print "               user = " user
			print "         costcentre = " costcentre
			print "             sundry = " sundry
			print "          invoiceno = " invoiceno
			exit 1
		}

		got_cde_data = 1

		#------------------------
		if ( Testing > 1 ) {
			print "do ld_cde()"
			print "           SERVTYPE = " SERVTYPE
			print "             SHIPTO = " SHIPTO
			print "             CENTRE = " CENTRE
			print "            PROJECT = " PROJECT
			print "               SGID = " SGID
			print "               SEID = " SEID "     (ElementID = " ElementID ")"
			print "               DEPT = " DEPT
			print "        tbsCENTREID = " tbsCENTREID
			print "             tbsSID = " tbsSID
			print "           supplier = " supplier
			print "            tascode = " tascode
			print "           location = " location
			print "               user = " user
			print "         costcentre = " costcentre
			print "             sundry = " sundry
			print "          invoiceno = " invoiceno
		}

		rec_type = "T_CHARGE_SUM"

		# use tascode / servicedesc for chg_cat to show on distid pie
		chg_cat = tascode " " servicedesc

		# called also in ldt21detail code
		set_HIERIDx("cde", SERVTYPE,SHIPTO,GROUP,CENTRE,PROJECT,supplier,invoiceno,sundry,tbsSID,rec_type,chg_cat)

		print "HIERIDx = " HIERIDx

		HIERIDx_to_debuginfo_arr[HIERIDx] = debuginfo

		# add to totals
		chg_count = 0
		chg_duration = 0
		chg_cost = totalcost

		# don't add up cde data for these
		# as this is done by dodet code
		doadd = 1
		#if ( bmSERVTYPE == 1 )		# t21 calls
		#	doadd = 0
		#if ( bmSERVTYPE == 101030 )	# t21 switchboard
		#	doadd = 0
		#if ( bmSERVTYPE == 101047 )	# t21 voicemail
		#	doadd = 0
		#if ( bmSERVTYPE == 101044 )	# mobile
		#	doadd = 0
		if ( doadd == 1 )
			add_totals("TOTALTBS", SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, tbsSID, supplier, invoiceno, sundry, rec_type, chg_cat, chg_count, chg_duration, chg_cost)

		pipecmd = "sort -u >" logdir "/" "sundrys_" batchtag ".uniq"
		print sundry " " supplier | pipecmd

		pipecmd = "sort | uniq -d >" logdir "/" "SID_" batchtag ".dup"
		print tbsSID | pipecmd
	}
	close(cdefile)
}


function is_t21_platinv_batch(platinvbatchfilename)
{
	for ( t21batchfilename in t21_platinv_batch ) {
		if ( platinvbatchfilename == t21batchfilename )
			return 1
	}
	return 0
}


#----------------------------------------------------------
#----------------------------------------------------------

function process_tdetail_file(tdetailfile)
{
	#from getdetail6 tbs sql detail data
	print "process_tdetail_file(" tdetailfile ")"
	rows = 0
	if ( (getline aline < tdetailfile) < 0 ) {
		print "  - INDETNOTFOUND [" tdetailfile "]"
		close(tdetailfile)
		return
	}

	afound = 1
	while ( afound ) {
		#print "tdetail aline = " aline
		split(aline, a_arr, ",")

		if ( ++rows == 1 ) {	# skip header
			#print " tdetail skip header aline=[" aline "]"
			if ( (getline aline < tdetailfile) <= 0 )
				afound = 0
			continue
		}

		f = 0
		tdetail_tBatch_ID = trim5(a_arr[++f])
		tdetail_tBatch_BillingPeriod = trim5(a_arr[++f])
		tdetail_tBatchType_ID = trim5(a_arr[++f])
		tdetail_tBatchType_SupplierID = trim5(a_arr[++f])
		tdetail_tBatchType_Description = trim5(a_arr[++f])
		tdetail_tSupplier_SupplierName = trim5(a_arr[++f])
		tdetail_tServiceID_ServiceID = trim5(a_arr[++f])
		tdetail_tServiceID_Service = trim5(a_arr[++f])
		tdetail_tServiceType_Code = trim5(a_arr[++f])
		tdetail_tServiceType_Description = trim5(a_arr[++f])
		tdetail_tInvoice_InvoiceDate = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_TxnDate = trim5(a_arr[++f])
		tdetail_tReportGroupID = trim5(a_arr[++f])
		tdetail_tReportGroup_GroupDesc = trim5(a_arr[++f])
		tdetail_tInvoice_PlatinumFileName = trim5(a_arr[++f])
		tdetail_tInvoice_PlatinumInvoiceNo = trim5(a_arr[++f])
		tdetail_tCustomer_ShipTo = trim5(a_arr[++f])
		tdetail_tCustomer_FullName = trim5(a_arr[++f])
		tdetail_tCentre_Description = trim5(a_arr[++f])
		tdetail_tActivity_Description = trim5(a_arr[++f])
		tdetail_tElement_ElementCode = trim5(a_arr[++f])
		tdetail_tElement_Description = trim5(a_arr[++f])
		tdetail_tElement_CustEleCode = trim5(a_arr[++f])
		tdetail_tElement_CustEleDesc = trim5(a_arr[++f])
		tdetail_tElement_EleAdmCode = trim5(a_arr[++f])
		tdetail_tElement_EleAdmDesc = trim5(a_arr[++f])
		tdetail_tSubledger_CustSubLedger = trim5(a_arr[++f])
		tdetail_tDepartment_CustDept = trim5(a_arr[++f])
		tdetail_tLocation_Description = trim5(a_arr[++f])
		tdetail_tPerson_Person = trim5(a_arr[++f])

		tdetail_tApplication_ID = trim5(a_arr[++f])
		tdetail_tApplication_Description = trim5(a_arr[++f])

		tdetail_tOrigin_Description = trim5(a_arr[++f])
		tdetail_tDestination_Description = trim5(a_arr[++f])
		tdetail_tRateDescription_Description = trim5(a_arr[++f])
		tdetail_tRate_Cost = trim5(a_arr[++f])
		tdetail_tTransactionType_Description = trim5(a_arr[++f])
		tdetail_tTransactionGroup_Description = trim5(a_arr[++f])
		tdetail_tDialledNumber_Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Duration = trim5(a_arr[++f])

		tdetail_tFinalisedTransaction_AmountExGST = trim5(a_arr[++f])

		tdetail_tBatchType_Var01Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var01 = trim5(a_arr[++f])
		tdetail_tBatchType_Var02Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var02 = trim5(a_arr[++f])
		tdetail_tBatchType_Var03Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var03 = trim5(a_arr[++f])
		tdetail_tBatchType_Var04Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var04 = trim5(a_arr[++f])
		tdetail_tBatchType_Var05Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var05 = trim5(a_arr[++f])
		tdetail_tBatchType_Var06Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var06 = trim5(a_arr[++f])
		tdetail_tBatchType_Var07Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var07 = trim5(a_arr[++f])
		tdetail_tBatchType_Var08Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var08 = trim5(a_arr[++f])
		tdetail_tBatchType_Var09Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var09 = trim5(a_arr[++f])
		tdetail_tBatchType_Var10Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var10 = trim5(a_arr[++f])
		tdetail_tBatchType_Var11Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var11 = trim5(a_arr[++f])
		tdetail_tBatchType_Var12Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var12 = trim5(a_arr[++f])
		tdetail_tBatchType_Var13Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var13 = trim5(a_arr[++f])
		tdetail_tBatchType_Var14Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var14 = trim5(a_arr[++f])
		tdetail_tBatchType_Var15Description = trim5(a_arr[++f])
		tdetail_tFinalisedTransaction_Var15 = trim5(a_arr[++f])

#ZZZZ
		sid = tdetail_tServiceID_ServiceID
		centre = grid_to_centre_arr[sid]
		project = grid_to_project_arr[sid]

		# store ServiceTypeDesc for sid
		tbssid_to_servicetyedesc_arr[sid] = tdetail_tServiceType_Description
		print "tdet: tbssid_to_servicetyedesc_arr[" sid "] = [" tbssid_to_servicetyedesc_arr[sid] "]"

		# do ..to_REPORTGROUPfile()
		spit_data_to_REPORTGROUPfile()

#xyzzy
                # detimine if we should have other
                # than mobile call detail data
		batchtypeid = tdetail_tBatchType_ID
                if ( batchtypeid == 141 ||	# Victrack call diversion
                     SUPPLIER == "TELSTRA" ||	# suplier Telstra
                     SUPPLIER == "OPTUS" ||	# suplier Optus
                     SUPPLIER == "VODAFONE" ||	# suplier Vodafone
		     reportgroupid == 15 ||     # Smartbus GPRS
		     reportgroupid == 19 ) {    # Smartbus Aggregate

			# create symlinks to m11cdr files
			det_dname = sprintf("../../../data/m11cdr%s", SERVTYPE)
			det_file = sprintf("m11cdr_%s_%s_%s_%s.csv", centre, project, SERVTYPE, sid)
			sym_dname = sprintf("topdat/%s/%s/data/m11cdr%s", monthdir, DISTID, SERVTYPE)

			print "detm11sym: done_det_file_symlink_arr[" sym_dname "/" det_file "]=[" done_det_file_symlink_arr[sym_dname "/" det_file] "]"
			if ( done_det_file_symlink_arr[sym_dname "/" det_file] == "" ) {
				cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", sym_dname, sym_dname)
				#print "detm11mkdir: SERVTYPE=" SERVTYPE " [" cmd "]"
				system(cmd)
				cmd = sprintf("cd \"%s\"; [ ! -L \"%s\" ] && ln -s \"%s\" \"%s\"", sym_dname, det_file, det_dname "/" det_file, det_file)
				print "  detm11sym: SERVTYPE=" SERVTYPE " [" cmd "]"
				system(cmd)
				done_det_file_symlink_arr[sym_dname "/" det_file] == "1"
			}
		}

		if ( (getline aline < tdetailfile) <= 0 )
			afound = 0
	}
	close(tdetailfile)
}


function MMM_to_MM(MMM)
{
	mmmstr["Jan"] = 1
	mmmstr["Feb"] = 2
	mmmstr["Mar"] = 3
	mmmstr["Apr"] = 4
	mmmstr["May"] = 5
	mmmstr["Jun"] = 6
	mmmstr["Jul"] = 7
	mmmstr["Aug"] = 8
	mmmstr["Sep"] = 9
	mmmstr["Oct"] = 10
	mmmstr["Nov"] = 11
	mmmstr["Dec"] = 12
	return 0 + mmmstr[MMM]
}


#----------------------------------------------------------
#----------------------------------------------------------
# Voicemail Call Details


function create_vmlextnsum_file()
{
	vmlextnsum_fname = sprintf("topdat/%s/%s/data/vmlextnsum/vmlextnsum_%s_%s_%s_%s_%s_%s.csv", monthdir, DISTID, DISTID, SGID, SEID, CENTRE, SID, sundry);
	printf(">>>            vmlextnsum_fname = %s\n", vmlextnsum_fname);
	if ( (getline x < vmlextnsum_fname) < 0 ) {
		# create file and csv header
		newdir = sprintf("topdat/%s/%s/data/vmlextnsum", monthdir, DISTID)
		cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p %s", newdir, newdir)
		print "system 3 [" cmd "]"
		system(cmd)
		add_fname_to_fnamelist(vmlextnsum_fname)
		printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", "Extension Voicemail Breakdown", exbillperiod, "SDATE", "EDATE", CUSTOMERdesc, SHIPTO, SGID_to_SGIDdesc_arr[SGID], SGID, SEID_to_SEIDdesc_arr[SEID], SEID, CENTREdesc, CENTRE, SID, invoiceno, sundry, supplier, user, XRENT, XCALL, XOTHER, XADMIN, "Extension","Surname","FirstName","Section","Title","InQuantity","InDuration","InCost","OutQuantity","OutDuration","OutCost","RentCost","TotalCost:Float") >vmlextnsum_fname
		vmlextnsum_file_exists = 0
	}
	else {
		printf("WARNING %d: vmlextnsum_fname = %s exists.\n", NR, vmlextnsum_fname)
		vmlextnsum_file_exists = 1
	}
}


function spit_vmlextnsum_tovmlextnsumfile()
{
	# append vmlextnsum call record data to vmlextnsum file

	if ( vmlextnsum_file_exists ) {	# skip deatils if already exists
		if ( vmlextnsum_file_exists == 1 )
			print "vmlextnsum file exists - skipping vml extnsumail for " SID
		vmlextnsum_file_exists = 2
		return
	}

	durhh = int(vmlextntot_duration["in"] / 3600)
	remd = vmlextntot_duration["in"] - (durhh * 3600)
	durmm = int(remd / 60)
	remd = remd - (durmm * 60)
	durss = remd
	vml_in_durstr = sprintf("%03d:%02d:%02d", durhh, durmm, durss)

	durhh = int(vmlextntot_duration["out"] / 3600)
	remd = vmlextntot_duration["out"] - (durhh * 3600)
	durmm = int(remd / 60)
	remd = remd - (durmm * 60)
	durss = remd
	vml_out_durstr = sprintf("%03d:%02d:%02d", durhh, durmm, durss)

	vmlitag = last_vml_eggrid "|" last_vml_extn
 	vml_t21surname = egridextn_to_t21surname_arr[vmlitag]
 	vml_t21firstname = egridextn_to_t21firstname_arr[vmlitag]
 	vml_t21section = egridextn_to_t21section_arr[vmlitag]
 	vml_t21title = egridextn_to_t21title_arr[vmlitag]

	csvstr = sprintf(",,,,,,,,,,,,,,,,,,,,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", last_vml_extn, vml_t21surname, vml_t21firstname, vml_t21section, vml_t21title, vmlextntot_count["in"], vml_in_durstr, vmlextntot_cost["in"], vmlextntot_count["out"], vml_out_durstr, vmlextntot_cost["out"], VMLEXTN_XRENT, vmlextntot_cost["in"] + vmlextntot_cost["out"] + VMLEXTN_XRENT)
	#printf("VMLextnsum %s", csvstr)
	printf("%s", csvstr) >>vmlextnsum_fname
}



function create_vmldet_detail_file()
{
	vmldet_fname = sprintf("topdat/%s/%s/data/vmldet/vmldet_%s_%s_%s_%s_%s_%s_%s.csv", monthdir, DISTID, DISTID, SGID, SEID, CENTRE, SID, vml_extn, sundry);
	printf(">>>            vmldet_fname = %s\n", vmldet_fname);
	if ( (getline x < vmldet_fname) < 0 ) {
		# create file and csv header
		newdir = sprintf("topdat/%s/%s/data/vmldet", monthdir, DISTID)
		cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p %s", newdir, newdir)
		print "system 3 [" cmd "]"
		system(cmd)
		add_fname_to_fnamelist(vmldet_fname)
		printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", "Voicemail Call Detail", exbillperiod, "SDATE", "EDATE", CUSTOMERdesc, SHIPTO, SGID_to_SGIDdesc_arr[SGID], SGID, SEID_to_SEIDdesc_arr[SEID], SEID, CENTREdesc, CENTRE, SID, vml_extn, vml_name, invoiceno, sundry, supplier, user, VMLEXTN_XRENT, 0, 0, "Date","Time", "Dialled Number","Description","Duration","Cost:Float") >vmldet_fname
		vmldet_file_exists = 0
	}
	else {
		printf("WARNING %d: vmldet_fname = %s exists.\n", NR, vmldet_fname)
		vmldet_file_exists = 1
	}
}


function spit_sdrrec_tovmldetfile()
{
	# append vmldet call record data to vmldet file

	if ( vmldet_file_exists ) {	# skip deatils if already exists
		if ( vmldet_file_exists == 1 )
			print "vmldet file exists - skipping vml detail for " SID
		vmldet_file_exists = 2
		return
	}

	date = fixdate(vml_cdate)

	durhh = int(vml_duration / 3600)
	remd = vml_duration - (durhh * 3600)
	durmm = int(remd / 60)
	remd = remd - (durmm * 60)
	durss = remd
	vml_durstr = sprintf("%03d:%02d:%02d", durhh, durmm, durss)

	hh = substr(vml_ctime,1,2)
	mm = substr(vml_ctime,3,2)
	ss = substr(vml_ctime,5,2)
	ctimestr = sprintf("%2s:%2s:%2s",hh, mm, ss)

	dialledno = vml_dialled

	csvstr = sprintf(",,,,,,,,,,,,,,,,,,,,,,%s,%s,%s,%s,%s,%s\n", date, ctimestr, dialledno, vml_dirdesc, vml_durstr, vml_cost)
	#printf("VMLdet %s", csvstr)
	printf("%s", csvstr) >>vmldet_fname

}


function procvmlcalldetails(vmlcallsfile)
{
	printf("\n")
	printf("procvmlcalldetails(%s) %s %s %s %s)\n", vmlcallsfile, SID, invoiceno, supplier, sundry)

	vmlextnsum_fname = ""
	vmldet_fname = ""

	vmlextntot_count["in"] = 0
	vmlextntot_duration["in"] = 0
	vmlextntot_cost["in"] = 0
	vmlextntot_count["out"] = 0
	vmlextntot_duration["out"] = 0
	vmlextntot_cost["out"] = 0

	create_vmlextnsum_file()

	vml_eggrid = substr(SID,3)

	#tmpvmlcallsfile = sprintf("%s.tmp", vmlcallsfile)
	cmd = "basename " vmlcallsfile
	cmd | getline basevmlcallsfile
	tmpvmlcallsfile = sprintf("%s/%s.tmp", logdir, basevmlcallsfile)
	print "tmpvmlcallsfile = " tmpvmlcallsfile

	system("rm -f " tmpvmlcallsfile)
	cmdstr = sprintf("grep -iF \"%s,%s,\" %s >%s", SHIPTO, vml_eggrid, vmlcallsfile, tmpvmlcallsfile)
	print "RUN: " cmdstr
	system(cmdstr)
	#system("cat " tmpvmlcallsfile)
	
	# count extensions
	last_vml_extn = ""
	vml_extncount = 0
	while ( (getline aline < tmpvmlcallsfile) > 0 ) {
		#print "VMLEXTNCOUNT aline = " aline "
		split(aline, a_arr, ",")
		vml_extn = a_arr[3]
		if ( vml_extn != last_vml_extn)
			++vml_extncount
		last_vml_extn = vml_extn
		#print "VMLEXTNCOUNT vml_extn = " vml_extn "   vml_extncount = " vml_extncount
	}
	close(tmpvmlcallsfile)

	if ( vml_extncount != 0 )
		VMLEXTN_XRENT = sprintf("%0.2f", XRENT / vml_extncount)
	else
		VMLEXTN_XRENT = 0

	print "SID = " SID "  " "XRENT = " XRENT "  " "vml_extncount = " vml_extncount "  " "VMLEXTN_XRENT = " VMLEXTN_XRENT

	#ALA,ALA01PWS,18171,09/02/2004,104508,39,60033,out,19000,O,Outgoing Internal Network,396,0.0,
	#gggrid, eggrid, inletno, calldate, endtime, $cdrtable.siteid, inoutno, "out", dialledno,  callcategory, calltypedesc, $cdrtable.duration, $cdrtable.callcost

	last_vml_eggrid = ""
	last_vml_extn = ""

	while ( (getline aline < tmpvmlcallsfile) > 0 ) {
		#print "VMLDET aline = " aline
		split(aline, a_arr, ",")
		f = 0
		vml_shipto = a_arr[++f]
		vml_eggrid = a_arr[++f]
		vml_extn = a_arr[++f]
		vml_cdate = a_arr[++f]
		vml_ctime = a_arr[++f]
		vml_siteid = a_arr[++f]
		vml_inoutno = a_arr[++f]
		vml_direction = a_arr[++f]
		vml_dialled = a_arr[++f]
		vml_category = a_arr[++f]
		vml_calltypedesc = a_arr[++f]
		vml_duration = a_arr[++f]
		vml_cost = a_arr[++f]

		vml_cost = 0 + voicemailcallcost

		itag = vml_eggrid "|" vml_extn
		vml_name = egridextn_to_t21surname_arr[itag]

		vml_operator = ""
		vml_dirdesc = ""
		if ( vml_direction == "in" ) {
			vml_dirdesc = "Incomming Voicemail Call"
		}
		if ( vml_direction == "out" ) {
			vml_dirdesc = "Voicemail Facility"
		}

		#printf("VMLDET: %s %s %s %s %s %s %s %s %d %0.2f\n", vml_shipto, vml_eggrid, vml_extn, vml_name, vml_cdate, vml_ctime, vml_direction, vml_dialled, vml_duration, vml_cost)

		if ( vml_extn != last_vml_extn ) {
			if ( last_vml_extn != "" ) {
				if ( vmlextnsum_fname != "" ) {
					spit_vmlextnsum_tovmlextnsumfile()
				}
			}
			if ( vmldet_fname != "" ) {
				#print "close(" vmldet_fname ")"
				# close vml detail file
				close(vmldet_fname)
				vmldet_fname == ""
				print "     incount=" vmlextntot_count["in"] "    outcount=" vmlextntot_count["out"]
				if ( (vmlextntot_count["in"] + vmlextntot_count["out"]) == 0 ) {
					#no calls so delete detail file
					# (only X dummies)
					#print "remove empty vmldet_fname=" vmldet_fname
					system("rm -f " vmldet_fname)
				}
			}
			vmlextntot_count["in"] = 0
			vmlextntot_duration["in"] = 0
			vmlextntot_cost["in"] = 0
			vmlextntot_count["out"] = 0
			vmlextntot_duration["out"] = 0
			vmlextntot_cost["out"] = 0
			create_vmldet_detail_file()
		}

		# add call and spit if NOT dummy from directory
		if ( vml_category != "X" ) {
			vmlextntot_count[vml_direction] += 1
			vmlextntot_duration[vml_direction] += vml_duration
			vmlextntot_cost[vml_direction] += voicemailcallcost
			#print "vml_extn = " vml_extn "  vmlextntot_cost[" vml_direction "] = " vmlextntot_cost[vml_direction]

			spit_sdrrec_tovmldetfile()
		}

		last_vml_eggrid = vml_eggrid
		last_vml_extn = vml_extn
	}
	close(tmpvmlcallsfile)
	system("rm -f " tmpvmlcallsfile)

	if ( last_vml_extn != "" ) {
		if ( vmlextnsum_fname != "" ) {
			spit_vmlextnsum_tovmlextnsumfile()
		}
	}
	if ( vmldet_fname != "" ) {
		# close vml detail file
		close(vmldet_fname)
		vmldet_fname == ""
		print "     incount=" vmlextntot_count["in"] "    outcount=" vmlextntot_count["out"]
		if ( (vmlextntot_count["in"] + vmlextntot_count["out"]) == 0 ) {
			#no calls so delete detail file
			# (only X dummies)
			print "remove empty vmldet_fname=" vmldet_fname
			system("rm -f " vmldet_fname)
		}
	}

	# close vmlextnsum file
	close(vmlextnsum_fname)
	vmlextnsum_fname == ""
}



#----------------------------------------------------------
#----------------------------------------------------------
# Switchboard Call Details


function create_swbextnsum_file()
{
	swbextnsum_fname = sprintf("topdat/%s/%s/data/swbextnsum/swbextnsum_%s_%s_%s_%s_%s_%s.csv", monthdir, DISTID, DISTID, SGID, SEID, CENTRE, SID, sundry);
	printf(">>>            swbextnsum_fname = %s\n", swbextnsum_fname);
	if ( (getline x < swbextnsum_fname) < 0 ) {
		# create file and csv header
		newdir = sprintf("topdat/%s/%s/data/swbextnsum", monthdir, DISTID)
		cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p %s", newdir, newdir)
		print "system 3 [" cmd "]"
		system(cmd)
		add_fname_to_fnamelist(swbextnsum_fname)
		printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", "Extension Switchboard Breakdown", exbillperiod, "SDATE", "EDATE", CUSTOMERdesc, SHIPTO, SGID_to_SGIDdesc_arr[SGID], SGID, SEID_to_SEIDdesc_arr[SEID], SEID, CENTREdesc, CENTRE, SID, invoiceno, sundry, supplier, user, XRENT, XCALL, XOTHER, XADMIN, "Extension","InQuantity","InDuration","InCost","OutQuantity","OutDuration","OutCost","TotalCost:Float") >swbextnsum_fname
		swbextnsum_file_exists = 0
	}
	else {
		printf("WARNING %d: swbextnsum_fname = %s exists.\n", NR, swbextnsum_fname)
		swbextnsum_file_exists = 1
	}
}


function spit_swbextnsum_toswbextnsumfile()
{
	# append swbextnsum call record data to swbextnsum file

	if ( swbextnsum_file_exists ) {	# skip deatils if already exists
		if ( swbextnsum_file_exists == 1 )
			print "swbextnsum file exists - skipping swb extnsumail for " SID
		swbextnsum_file_exists = 2
		return
	}

	durhh = int(swbextntot_duration["in"] / 3600)
	remd = swbextntot_duration["in"] - (durhh * 3600)
	durmm = int(remd / 60)
	remd = remd - (durmm * 60)
	durss = remd
	swb_in_durstr = sprintf("%03d:%02d:%02d", durhh, durmm, durss)

	durhh = int(swbextntot_duration["out"] / 3600)
	remd = swbextntot_duration["out"] - (durhh * 3600)
	durmm = int(remd / 60)
	remd = remd - (durmm * 60)
	durss = remd
	swb_out_durstr = sprintf("%03d:%02d:%02d", durhh, durmm, durss)

	csvstr = sprintf(",,,,,,,,,,,,,,,,,,,,%s,%s,%s,%s,%s,%s,%s,%s\n", last_swb_extn, swbextntot_count["in"], swb_in_durstr, swbextntot_cost["in"], swbextntot_count["out"], swb_out_durstr, swbextntot_cost["out"], swbextntot_cost["in"] + swbextntot_cost["out"])
	#printf("SWBextnsum %s", csvstr)
	printf("%s", csvstr) >>swbextnsum_fname
}



function create_swbdet_detail_file()
{
	swbdet_fname = sprintf("topdat/%s/%s/data/swbdet/swbdet_%s_%s_%s_%s_%s_%s_%s.csv", monthdir, DISTID, DISTID, SGID, SEID, CENTRE, SID, swb_extn, sundry);
	printf(">>>            swbdet_fname = %s\n", swbdet_fname);
	if ( (getline x < swbdet_fname) < 0 ) {
		# create file and csv header
		newdir = sprintf("topdat/%s/%s/data/swbdet", monthdir, DISTID)
		cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p %s", newdir, newdir)
		print "system 3 [" cmd "]"
		system(cmd)
		add_fname_to_fnamelist(swbdet_fname)
		printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", "Switchboard Call Detail", exbillperiod, "SDATE", "EDATE", CUSTOMERdesc, SHIPTO, SGID_to_SGIDdesc_arr[SGID], SGID, SEID_to_SEIDdesc_arr[SEID], SEID, CENTREdesc, CENTRE, SID, swb_extn, swb_name, invoiceno, sundry, supplier, user, 0, 0, 0, "Date","Time","Operator", "Dialled Number","Description","Duration","Cost:Float") >swbdet_fname
		swbdet_file_exists = 0
	}
	else {
		printf("WARNING %d: swbdet_fname = %s exists.\n", NR, swbdet_fname)
		swbdet_file_exists = 1
	}
}


function spit_sdrrec_toswbdetfile()
{
	# append swbdet call record data to swbdet file

	if ( swbdet_file_exists ) {	# skip deatils if already exists
		if ( swbdet_file_exists == 1 )
			print "swbdet file exists - skipping swb detail for " SID
		swbdet_file_exists = 2
		return
	}

	date = fixdate(swb_cdate)

	durhh = int(swb_duration / 3600)
	remd = swb_duration - (durhh * 3600)
	durmm = int(remd / 60)
	remd = remd - (durmm * 60)
	durss = remd
	swb_durstr = sprintf("%03d:%02d:%02d", durhh, durmm, durss)

	hh = substr(swb_ctime,1,2)
	mm = substr(swb_ctime,3,2)
	ss = substr(swb_ctime,5,2)
	ctimestr = sprintf("%2s:%2s:%2s",hh, mm, ss)

	dialledno = swb_dialled

	csvstr = sprintf(",,,,,,,,,,,,,,,,,,,,,,%s,%s,%s,%s,%s,%s,%s\n", date, ctimestr, swb_operator, dialledno, swb_dirdesc, swb_durstr, swb_cost)
	#printf("SWBdet %s", csvstr)
	printf("%s", csvstr) >>swbdet_fname

}


function procswbcalldetails(swbcallsfile)
{
	printf("\n")
	printf("procswbcalldetails(%s) %s %s %s %s)\n", swbcallsfile, SID, invoiceno, supplier, sundry)

	swbextnsum_fname = ""
	swbdet_fname = ""

	swbextntot_count["in"] = 0
	swbextntot_duration["in"] = 0
	swbextntot_cost["in"] = 0
	swbextntot_count["out"] = 0
	swbextntot_duration["out"] = 0
	swbextntot_cost["out"] = 0

	create_swbextnsum_file()

	last_swb_extn = ""
	last_swb_name = ""

	swb_eggrid = substr(SID,3)

	#tmpswbcallsfile = sprintf("%s.tmp", swbcallsfile)
	cmd = "basename " swbcallsfile
	cmd | getline baseswbcallsfile
	tmpswbcallsfile = sprintf("%s/%s.tmp", logdir, baseswbcallsfile)
	print "tmpswbcallsfile = " tmpswbcallsfile

	system("rm -f " tmpswbcallsfile)
	cmdstr = sprintf("grep -iF \"%s,%s,\" %s >%s", SHIPTO, swb_eggrid, swbcallsfile, tmpswbcallsfile)
	print "RUN: " cmdstr
	system(cmdstr)
	#system("cat " tmpswbcallsfile)

	#ALA,ALA01PWS,18171,09/02/2004,104508,39,60033,out,19000,O,Outgoing Internal Network,396,0.0,
	#gggrid, eggrid, inletno, calldate, endtime, $cdrtable.siteid, inoutno, "out", dialledno,  callcategory, calltypedesc, $cdrtable.duration, $cdrtable.callcost

	while ( (getline aline < tmpswbcallsfile) > 0 ) {
		#print "SWBDET aline = " aline
		split(aline, a_arr, ",")
		f = 0
		swb_shipto = a_arr[++f]
		swb_eggrid = a_arr[++f]
		swb_extn = a_arr[++f]
		swb_cdate = a_arr[++f]
		swb_ctime = a_arr[++f]
		swb_siteid = a_arr[++f]
		swb_inoutno = a_arr[++f]
		swb_direction = a_arr[++f]
		swb_dialled = a_arr[++f]
		swb_category = a_arr[++f]
		swb_calltypedesc = a_arr[++f]
		swb_duration = a_arr[++f]
		swb_cost = a_arr[++f]

		swb_cost = 0 + switchboardcallcost

		itag = swb_eggrid "|" swb_extn
		swb_name = egridextn_to_t21surname_arr[itag]

		swb_operator = ""
		swb_dirdesc = ""
		if ( swb_direction == "in" ) {
			swb_operator = swb_inoutno
			swb_dirdesc = "Incomming Enquiry Call"
		}
		if ( swb_direction == "out" ) {
			swb_operator = ""	# not available in cdr
			swb_dirdesc = "Operator Assisted Call"
		}

		#printf("SWBDET: %s %s %s %s %s %s %s %s %d %0.2f\n", swb_shipto, swb_eggrid, swb_extn, swb_name, swb_cdate, swb_ctime, swb_direction, swb_dialled, swb_duration, swb_cost)

		if ( swb_extn != last_swb_extn ) {
			if ( last_swb_extn != "" ) {
				if ( swbextnsum_fname != "" ) {
					spit_swbextnsum_toswbextnsumfile()
				}
			}
			if ( swbdet_fname != "" ) {
				#print "close(" swbdet_fname ")"
				# close swb detail file
				close(swbdet_fname)
				swbdet_fname == ""
				print "     incount=" swbextntot_count["in"] "    outcount=" swbextntot_count["out"]
				if ( (swbextntot_count["in"] + swbextntot_count["out"]) == 0 ) {
					#no calls so delete detail file
					# (only X dummies)
					print "remove empty swbdet_fname=" swbdet_fname
					system("rm -f " swbdet_fname)
				}
			}
			swbextntot_count["in"] = 0
			swbextntot_duration["in"] = 0
			swbextntot_cost["in"] = 0
			swbextntot_count["out"] = 0
			swbextntot_duration["out"] = 0
			swbextntot_cost["out"] = 0
			create_swbdet_detail_file()
		}

		# add call and spit if NOT dummy from directory
		if ( swb_category != "X" ) {
			swbextntot_count[swb_direction] += 1
			swbextntot_duration[swb_direction] += swb_duration
			swbextntot_cost[swb_direction] += switchboardcallcost
			#print "swb_extn = " swb_extn "  swbextntot_cost[" swb_direction "] = " swbextntot_cost[swb_direction]

			spit_sdrrec_toswbdetfile()
		}

		last_swb_extn = swb_extn
		last_swb_name = swb_name
	}
	close(tmpswbcallsfile)
	system("rm -f " tmpswbcallsfile)

	if ( last_swb_extn != "" ) {
		if ( swbextnsum_fname != "" ) {
			spit_swbextnsum_toswbextnsumfile()
		}
	}
	if ( swbdet_fname != "" ) {
		# close swb detail file
		close(swbdet_fname)
		swbdet_fname == ""
		print "     incount=" swbextntot_count["in"] "    outcount=" swbextntot_count["out"]
		if ( (swbextntot_count["in"] + swbextntot_count["out"]) == 0 ) {
			#no calls so delete detail file
			# (only X dummies)
			print "remove empty swbdet_fname=" swbdet_fname
			system("rm -f " swbdet_fname)
		}
	}

	# close swbextnsum file
	close(swbextnsum_fname)
	swbextnsum_fname == ""
}


#----------------------------------------------------------
#----------------------------------------------------------

function ld_platinv(platinvfile)
{
	printf("ld_platinv(%s)\n", platinvfile)

	while ( (getline platinvline < platinvfile) > 0 ) {
		split(platinvline, platinvarr, "|");
		#printf("platinvline=%s\n", platinvline);

		f = 0
		tblInvoiceShipToCode = trim(platinvarr[++f])
		tblInvoicePlatinumFileName = trim(platinvarr[++f])
		tblInvoicePlatinumInvoiceNo = trim(platinvarr[++f])
		tblTransactionBatchID = trim(platinvarr[++f])
		tblSupplierSupplierName = trim(platinvarr[++f])
		tblInvoiceDetailElementID = trim(platinvarr[++f])
		tblElementDesciption = trim(platinvarr[++f])
		tblInvoiceDetailPeriod = trim(platinvarr[++f])
		tblTransactionInvoiceDetailID = trim(platinvarr[++f])
		tblInvoiceInvoiceDate = trim(platinvarr[++f])
		tblInvoiceDetailDescription = trim(platinvarr[++f])
		tblElementReportGroupID = trim(platinvarr[++f])
		tblInvoiceDetailAmount = trim(platinvarr[++f])

		#-----------------------------

		tblInvoiceShipToCode = toupper(tblInvoiceShipToCode)

		platinv_parent = tblInvoiceShipToCode
		platinv_batch = tblInvoicePlatinumFileName
		platinv_invoicenumber = tblInvoicePlatinumInvoiceNo
		platinv_sundry = tblTransactionBatchID
		platinv_supplier = tblSupplierSupplierName
		platinv_elementid = tblInvoiceDetailElementID
		platinv_reportgroupid = tblElementReportGroupID
		platinv_particular = tblInvoiceDetailDescription

		if ( platinv_parent == "" ) {
			print "FATAL ERROR: Blank parent (ShipTo) in platinum invoice data"
			exit 1
		}
		if ( platinv_batch == "" ) {
			print "FATAL ERROR: Blank PlatinumFileName in platinum invoice data"
			exit 1
		}
		if ( platinv_invoicenumber == "" ) {
			print "FATAL ERROR: Blank InvoiceNumber in platinum invoice data"
			exit 1
		}
		if ( platinv_sundry == "" ) {
			print "FATAL ERROR: Blank Sundry (var1) in platinum invoice data"
			exit 1
		}
		if ( platinv_particular == "" ) {
			print "FATAL ERROR: Blank particular (Detail Description) in platinum invoice data"
			exit 1
		}

		print ""
		print "        platinv_batch = " platinv_batch
		print "platinv_invoicenumber = " platinv_invoicenumber
		print "       platinv_sundry = " platinv_sundry
		print "    platinv_elementid = " platinv_elementid
		print "     platinv_supplier = " platinv_supplier
		print "       platinv_parent = " platinv_parent
		print "   platinv_particular = " platinv_particulay

		#if (length(inp_platinumBATCH) > 0 ) {
		#	if (length(inp_platinumBATCH) >= 7 && length(inp_platinumBATCH) <= 10) {
		#		if ( inp_platinumBATCH != platinv_invoicenumber ) {
		#			print "   skipping invoicenumber"
		#			continue			
		#		}
		#	}
		#	if (length(inp_platinumBATCH) > 10) {
		#		if ( inp_platinumBATCH != platinv_batch ) {
		#			print "   skipping batch"
		#			continue			
		#		}
		#	}
		#}

		sundry_to_particular[platinv_sundry] = platinv_particular

		htag = platinv_parent "|" platinv_invoicenumber "|" platinv_sundry
		printf("                htag = [%s]\n", htag)

		#itag = platinv_parent "|" platinv_sundry
		itag = platinv_parent "|" platinv_sundry "|" platinv_elementid
		printf("                itag = [%s]\n", itag)

		if ( cdeflds < 30 ) {
			ididtag = platinv_parent "|" platinv_sundry "|" platinv_elementid
		}
		else {
			#ididtag = platinv_parent "|" platinv_sundry "|" platinv_elementid "|" tblTransactionInvoiceDetailID
			ididtag = platinv_parent "|" platinv_sundry "|" tblTransactionInvoiceDetailID
		}
		printf("                ididtag = [%s]\n", ididtag)

		jtag = platinv_parent "|" platinv_invoicenumber
		printf("                jtag = [%s]\n", jtag)

		## rjs
		#if ( platinv_batch == "TBSAUG2004001134" ) {
		#	print "Warning 0 Skipping platinv_batch = " platinv_batch
		#	continue
		#}

		billingPeriod = tblInvoiceDetailPeriod
		parent_invoicenumber_sundry_to_period_arr[htag] = billingPeriod
		print "  parent_invoicenumber_sundry_to_period_arr[" htag "] = " parent_invoicenumber_sundry_to_period_arr[htag]

		## rjs 18/8/2008
		if ( parent_sundry_to_platinv_batch_arr[ididtag] != "" && parent_sundry_to_platinv_batch_arr[ididtag] != platinv_batch ) {
			print "FATAL ERROR: conflicting platinum invoice batch / sundry"
			print "       platinv_parent = " platinv_parent
			print "        platinv_batch = " platinv_batch
			print "platinv_invoicenumber = " platinv_invoicenumber
			print "       platinv_sundry = " platinv_sundry
			print "              ididtag = " ididtag
			print " parent_sundry_to_platinv_batch_arr[" ididtag "] = " parent_sundry_to_platinv_batch_arr[ididtag]
			exit 1
		}
		if ( parent_sundry_to_platinv_batch_arr[ididtag] == "" )
			parent_sundry_to_platinv_batch_arr[ididtag] = platinv_batch


		print "    parent_sundry_to_platinv_batch_arr[" ididtag "] = " parent_sundry_to_platinv_batch_arr[itag]

		## rjs 18/8/2008
		if ( parent_sundry_to_platinv_invoicenumber_arr[ididtag] != "" && parent_sundry_to_platinv_invoicenumber_arr[ididtag] != platinv_invoicenumber ) {
			print "FATAL ERROR: conflicting platinum invoice number / sundry"
			print "       platinv_parent = " platinv_parent
			print "        platinv_batch = " platinv_batch
			print "platinv_invoicenumber = " platinv_invoicenumber
			print "       platinv_sundry = " platinv_sundry
			print "              ididtag = " ididtag
			print " parent_sundry_to_platinv_invoicenumber_arr[" ididtag "] = " parent_sundry_to_platinvinvoicenumber_arr[ididtag]
			exit 1
		}
		if (parent_sundry_to_platinv_invoicenumber_arr[ididtag] == "")
			parent_sundry_to_platinv_invoicenumber_arr[ididtag] = platinv_invoicenumber
		print "    parent_sundry_to_platinv_invoicenumber_arr[" ididtag "] = " parent_sundry_to_platinv_invoicenumber_arr[ididtag]
		
		if ( parent_invoicenumber_to_platinv_batch_arr[jtag] != "" && parent_invoicenumber_to_platinv_batch_arr[jtag] != platinv_batch ) {
			print "FATAL ERROR: conflicting platinum invoice batch / invoicenumber"
			print "       platinv_parent = " platinv_parent
			print "        platinv_batch = " platinv_batch
			print "platinv_invoicenumber = " platinv_invoicenumber
			print "       platinv_sundry = " platinv_sundry
			print "                 jtag = " jtag
			print " parent_invoicenumber_to_platinv_batch_arr[" jtag "] = " parent_invoicenumber_to_platinv_batch_arr[jtag]
			exit 1
		}
		parent_invoicenumber_to_platinv_batch_arr[jtag] = platinv_batch
		print "    parent_invoicenumber_to_platinv_batch_arr[" jtag "] = " parent_invoicenumber_to_platinv_batch_arr[jtag]

		# flag parent / sundry in platinv
		platinv_parent_sundry_arr[itag] = 1
		print "platinv_parent_sundry_arr[" itag "]=[" platinv_parent_sundry_arr[itag] "]"

		# flag parent in platinv
		platinv_parent_arr[platinv_parent] = 1
	}
	close(platinvfile);

}


#----------------------------------------------------------
#----------------------------------------------------------

#end Victrack custom
#rjs9
#####################################################
#####################################################


#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------

function ld_acccodeconf(acccodeconffile) {
	printf("ld_acccodeconf(%s)\n", acccodeconffile)
	system("date")
	while ( (getline aline < acccodeconffile) > 0 ) {
		split(aline, a_arr, "|")
		#printf("aline=%s\n", aline)
		f = 0

		# from gett21acccode
		#101|1626|VM 0 OPTION TO MOBILE 0408939653|7950002|

		acc_desc_siteid = clip(a_arr[++f])
		acc_desc_account_code = clip(a_arr[++f])
		acc_desc_account_desc = clip(a_arr[++f])
		acc_grid_grid = fixforfname(clip(a_arr[++f]))

		if ( acc_desc_account_code == "" )
			acc_desc_account_code == "Account Code"

		acccode = acc_desc_account_code

		acccode_to_siteid_arr[acccode] = acc_desc_siteid
		acccode_to_desc_arr[acccode] = acc_desc_account_desc
		acccode_to_egrid_arr[acccode] = acc_grid_grid
		print "   acccode_to_egrid_arr[" acccode "] = " acccode_to_egrid_arr[acccode]

	}
	close(acccodeconffile)
}


function ld_mobconf(mobconffile) {
	printf("ld_mobconf(%s)\n", mobconffile)
	system("date")
	while ( (getline aline < mobconffile) > 0 ) {
		split(aline, a_arr, "|")
		printf("aline=%s\n", aline)
		f = 0

		## old mobconf
		#carriercode = clip(a_arr[++f])
		#mobile = clip(a_arr[++f])
		#surname = clip(a_arr[++f])
		#firstname = clip(a_arr[++f])
		#directoryID = clip(a_arr[++f])

		# from getSIDMAINTconf
		recordno = clip(a_arr[++f])
		mobconf_mobile = clip(a_arr[++f])
		sidmainttype = clip(a_arr[++f])
		mobconf_sdate = clip(a_arr[++f])
		mobconf_edate = clip(a_arr[++f])
		directoryID = clip(a_arr[++f])
		grid = fixforfname(clip(a_arr[++f]))
		datatype = clip(a_arr[++f])
		serviceproviderdir = clip(a_arr[++f])
		carriercode = clip(a_arr[++f])
		grname1 = clip(a_arr[++f])
		grname3 = fixforfname(clip(a_arr[++f]))
		surname = clip(a_arr[++f])
		firstname = clip(a_arr[++f])
		manufacturer = clip(a_arr[++f])
		model = clip(a_arr[++f])
		servicetype = clip(a_arr[++f])
		dataplan = clip(a_arr[++f])

		mobile = mobconf_mobile
		gsub(" ","",mobile)
		gsub("-","",mobile)

		#print "  mobile=[" mobile "]"

		if ( mobile == "" ) {
			mobile = "_blank_"
			print "ERROR: blank mobile in mobconf recordno = ["  recordno "]"
			directoryID = -2
		}
		if ( surname == "" )
			surname = "_blank_"
		if ( firstname == "" )
			firstname = "_blank_"
		name = surname
		if ( firstname != "_blank_")
			name = sprintf("%s %s", firstname, name)

		if ( directoryID == "" ) {
			directoryID = -1
			name = "Group Assigned"
		}

		if ( mobile != "_blank_" ) {
			mobsid = substr(mobile,1,4) "-" substr(mobile,5)
			mobile_to_sid_arr[mobile] = mobsid
			mobile_to_mob_carriercode_arr[mobile] = carriercode
			mobile_to_directoryID_arr[mobile] = directoryID
			mobile_to_grid_arr[mobile] = grid
			mobile_to_manufacturer_arr[mobile] = manufacturer
			mobile_to_model_arr[mobile] = model

			mobsid_to_directoryID_arr[mobsid] = directoryID

			if ( mobile_to_name_arr[mobile] == "" )
				mobile_to_name_arr[mobile] = name
			if ( mobile_to_name_arr[mobile] != name )
				mobile_to_name_arr[mobile] = "Multiple"

			print "mobconf: mobile_to_sid_arr[" mobile "] = " mobile_to_sid_arr[mobile]
			print "mobconf: mobile_to_directoryID_arr[" mobile "] = " mobile_to_directoryID_arr[mobile]
			print "mobconf: mobile_to_grid_arr[" mobile "] = " mobile_to_grid_arr[mobile]
			print "mobconf: mobile_to_name_arr[" mobile "] = " mobile_to_name_arr[mobile]
			print "mobconf: mobile_to_manufacturer_arr[" mobile "] = " mobile_to_manufacturer_arr[mobile]
			print "mobconf: mobile_to_model_arr[" mobile "] = " mobile_to_model_arr[mobile]
		}

	}
	close(mobconffile)
}


function ld_chargecat(chargecatfile) {
	printf("ld_chargecat(%s)\n", chargecatfile)
	system("date")
	while ( (getline aline < chargecatfile) > 0 ) {
		split(aline, a_arr, "|")
		#printf("aline=%s\n", aline)
		f = 0
		chargecatid = clip(a_arr[++f])
		chargecat = clip(a_arr[++f])
		importdatatypeid = clip(a_arr[++f])
		carriercode = clip(a_arr[++f])
		rectype = clip(a_arr[++f])
		chargeclasstype = clip(a_arr[++f])
		globalroaming = clip(a_arr[++f])

		Ctag = carriercode "|" rectype "|" chargeclasstype
		carriercode_chargeclasstype_to_chargecat_arr[Ctag] = chargecat

		print "  carriercode_chargeclasstype_to_chargecat_arr[" Ctag "] = " carriercode_chargeclasstype_to_chargecat_arr[Ctag]

		# global roaming flag
		if ( toupper(globalroaming) == "Y" ) {
			Ctag = carriercode "|" rectype "|" chargeclasstype
			gloroam_class_arr[Ctag] = 1
			print "  gloroam_class_arr[" Ctag "] = " gloroam_class_arr[Ctag]
		}

	}
	close(chargecatfile)
}




function ld_t21rt(t21rtfile) {
	printf("ld_t21rt(%s)\n", t21rtfile)
	system("date")
	while ( (getline aline < t21rtfile) > 0 ) {
		#print aline
		split(aline, a_arr, "|")
		f = 0

		rt_siteid = clip(a_arr[++f])
		rt_routeid = clip(a_arr[++f])
		rt_routetype = clip(a_arr[++f])
		rt_routedestination = clip(a_arr[++f])
		rt_routelocalid = clip(a_arr[++f])
		rt_shortholdingtime = clip(a_arr[++f])
		rt_trunkid = clip(a_arr[++f])
		rt_trunktype = clip(a_arr[++f])
		rt_trunklocalid = clip(a_arr[++f])
		rt_pulsetype = clip(a_arr[++f])
		rt_trunkdesc = clip(a_arr[++f])
		rt_altsiteid = clip(a_arr[++f])
		rt_ovrcar1 = clip(a_arr[++f])
		rt_ovrcar1siteid = clip(a_arr[++f])
		rt_ovrcar1acc = clip(a_arr[++f])
		rt_ovrcar2 = clip(a_arr[++f])
		rt_ovrcar2siteid = clip(a_arr[++f])
		rt_ovrcar2acc = clip(a_arr[++f])

		rte_siteid = clip(a_arr[++f])
		rte_routeid = clip(a_arr[++f])
		rte_trunklocalid = clip(a_arr[++f])
		rte_rt_remark = clip(a_arr[++f])
		rte_exch_a_end = clip(a_arr[++f])
		rte_exch_b_end = clip(a_arr[++f])
		rte_rt_code = clip(a_arr[++f])
		rte_tkid = clip(a_arr[++f])
		rte_len_no = clip(a_arr[++f])
		rte_naildn_len = clip(a_arr[++f])
		rte_cct_type = clip(a_arr[++f])
		rte_tk_remark = clip(a_arr[++f])
		rte_tk_status = clip(a_arr[++f])

		rti_siteid = clip(a_arr[++f])
		rti_routeid = clip(a_arr[++f])
		rti_trunklocalid = clip(a_arr[++f])
		rti_ipaddr = clip(a_arr[++f])
		rti_device = clip(a_arr[++f])

		site_sitenm1 = clip(a_arr[++f])

		rt_stripdd = rt_trunkid

		ROUTE = rt_siteid "_" rt_routeid

		ROUTE_to_ROUTEdesc_arr[ROUTE] = site_sitenm1 ": " rt_routedestination
		#print "ROUTE_to_ROUTEdesc_arr[" ROUTE "] = [" ROUTE_to_ROUTEdesc_arr[ROUTE] "]"

		ROUTE_to_SITEdesc_arr[ROUTE] = site_sitenm1

		site_trunk = rt_siteid "|" rt_trunklocalid

		site_trunk_to_stripdd[site_trunk] = rt_stripdd

		site_trunk_to_ovrcar1[site_trunk] = rt_ovrcar1
		site_trunk_to_ovrcar1acc[site_trunk] = rt_ovrcar1acc

		site_trunk_to_ovrcar2[site_trunk] = rt_ovrcar2
		site_trunk_to_ovrcar2acc[site_trunk] = rt_ovrcar2acc

	}
	close(t21rtfile)
}


function ld_t21extranges(t21extrangesfile) {
	printf("ld_t21extranges(%s)\n", t21extrangesfile)
	system("date")
	id = -1
	while ( (getline aline < t21extrangesfile) > 0 ) {
		++id
		split(aline, a_arr, "|")
		#printf("aline=%s\n", aline)
		f = 0
		siteid = clip(a_arr[++f])
		lorange = clip(a_arr[++f])
		hirange = clip(a_arr[++f])
		code1 = clip(a_arr[++f])
		code2 = clip(a_arr[++f])
		codedesc = clip(a_arr[++f])

		extrange_to_siteid_arr[lorange "|" hirange] = siteid
		#print "extrange_to_siteid_arr[" lorange "|" hirange "] = " siteid

		extrange_to_code1_arr[lorange "|" hirange "|" id] = code1
		extrange_to_code2_arr[lorange "|" hirange "|" id] = code2
		extrangecode1_to_code2_arr[lorange "|" hirange "|" code1 "|" id] = code2
	}
	close(t21extrangesfile)
}


function ld_t21dircsvfile(t21dircsvfile) {
	printf("ld_t21dircsvfile(%s)\n", t21dircsvfile)
	system("date")

        maxt21dirrecordno = 1

	t21dirreccnt = -1
	while ( (getline aline < t21dircsvfile) > 0 ) {
		++t21dirreccnt
		split(aline, a_arr, ",")
		printf("aline=%s\n", aline)
		#printf("a_arr 1=%s 2=%s 3=%s 4=%s 5=%s\n", a_arr[1], a_arr[2], a_arr[3], a_arr[4], a_arr[5])
		f = 0
		recordno = trim(a_arr[++f])
		t21surname = trim(a_arr[++f])
		t21firstname = trim(a_arr[++f])
		extn = clip(a_arr[++f])
		t21site = clip(a_arr[++f])
		t21section = clip(a_arr[++f])
		t21division = clip(a_arr[++f])
		t21title = clip(a_arr[++f])
		t21location = trim(a_arr[++f])
		extgroupunique = fixforfname(clip(a_arr[++f]))
		email = clip(a_arr[++f])
		tec = clip(a_arr[++f])
		t21mobile = clip(a_arr[++f])
		fax = clip(a_arr[++f])
		voicemail = clip(a_arr[++f])
		telephone = clip(a_arr[++f])

		# get largest recordno
                if ( (0 + recordno) > maxt21dirrecordno )
                        maxt21dirrecordno = recordno

		t21department = grid_to_grname1_arr[extgroupunique]
		staffid = "D" recordno
		master = "Y"

		# for mk_dircsvfile()
		print " recordno = [" recordno "]"
		recordno_to_aline_arr[recordno] = aline
		print "   recordno_to_aline_arr[" recordno "] = [" recordno_to_aline_arr[recordno] "]"
		t21dirreccnt_to_recordno_arr[t21dirreccnt] = recordno
		print "   t21dirreccnt_to_recordno_arr[" t21dirreccnt "] = [" t21dirreccnt_to_recordno_arr[t21dirreccnt] "]"

		recordno_to_master_arr[recordno] = master
		print "   recordno_to_master_arr[" recordno "] = [" recordno_to_master_arr[recordno] "]"

		if ( t21surname == "" )
			t21surname = "_blank_"
		if ( t21firstname == "" )
			t21firstname = "_blank_"

		if ( t21location == "" )
			t21location = "_blank_"

		gsub(" ","",t21mobile)
		gsub("-","",t21mobile)

		printf(" - extgroupunique = [%s]  extn = [%s]\n", extgroupunique, extn)
		printf("   t21surname = [%s]  t21firstname = [%s]\n", t21surname, t21firstname)
		printf("   t21site = [%s]  t21location = [%s]\n", t21site, t21location)
		printf("   t21division  = [%s]\n", t21division)
		printf("   t21department = [%s]\n", t21department)

		t21name = t21surname
		if ( t21firstname != "_blank_")
			t21name = sprintf("%s %s", t21firstname, t21name)
		recordno_to_t21name_arr[recordno] = t21name
		print "      recordno_to_t21name_arr[" recordno "] = [" recordno_to_t21name_arr[recordno] "]"

		recordno_to_staffid_arr[recordno] = staffid
		print "      recordno_to_staffid_arr[" recordno "] = [" recordno_to_staffid_arr[recordno] "]"

		recordno_to_email_arr[recordno] = email
		print "      recordno_to_email_arr[" recordno "] = [" recordno_to_email_arr[recordno] "]"

		recordno_to_extn_arr[recordno] = extn
		recordno_to_eggrid_arr[recordno] = extgroupunique
		recordno_to_t21site_arr[recordno] = t21site
		recordno_to_t21location_arr[recordno] = t21location

		# newbb1 BBBBBBB
		code1 = get_code1(extn)
		print "code1=[" code1 "]" "  extn=[" extn "]"
		if ( code1 != "" ) {
			if ( egrid_to_code1[extgroupunique] != "" && egrid_to_code1[extgroupunique] != code1 )
				print "ld_t21dircsv() extgroupunique=" extgroupunique " code1=" code1 "  already has egrid_to_code1[" extgroupunique "] set to [" egrid_to_code1[extgroupunique] "]"
			else 
				egrid_to_code1[extgroupunique] = code1
			print "egrid_to_code1[" extgroupunique "]=[" egrid_to_code1[extgroupunique] "]"
		}
		code2 = nget_code2(extn,code1)
		print "code2=[" code2 "]" "  extn=[" extn "]"

		# can't do this (code2 is NOT unique within extn group)
		#if ( code2 != "" ) {
		#	if ( egrid_to_code2[extgroupunique] != "" && egrid_to_code2[extgroupunique] != code2 )
		#		print "ld_t21dircsv() extgroupunique=" extgroupunique " code2=" code2 "  already has egrid_to_code2[" extgroupunique "] set to [" egrid_to_code2[extgroupunique] "]"
		#	else 
		#		egrid_to_code2[extgroupunique] = code2
		#	print "egrid_to_code2[" extgroupunique "]=[" egrid_to_code2[extgroupunique] "]"
		#}

		recordno_to_t21surname_arr[recordno] = t21surname
		recordno_to_t21firstname_arr[recordno] = t21firstname
		recordno_to_t21division_arr[recordno] = t21division
		recordno_to_t21department_arr[recordno] = t21department_college


		print "      recordno_to_extn_arr[" recordno "] = [" recordno_to_extn_arr[recordno] "]"
		print "      recordno_to_eggrid_arr[" recordno "] = [" recordno_to_eggrid_arr[recordno] "]"

                if ( t21mobile != "" ) {
			eetag = extgroupunique "|" extn
                        t21mobile_to_egridextn_arr[t21mobile] = eetag
                        #print "save t21mobile_to_egridextn_arr[" t21mobile "] = [" t21mobile_to_egridextn_arr[t21mobile] "]"
                }


		itag = extn
		printf(" itag = [%s]\n", itag)

		if ( master == "Y" ) {
			if ( dirextn_to_recordno_arr[itag] == "" )
				dirextn_to_recordno_arr[itag] = recordno
			if ( dirextn_to_t21name_arr[itag] == "" )
				dirextn_to_t21name_arr[itag] = t21name
			if ( dirextn_to_t21name_arr[itag] != t21name )
				dirextn_to_t21name_arr[itag] = "Multiple"
			print "    dirextn_to_t21name_arr[" itag "] = [" dirextn_to_t21name_arr[itag] "]"

			if ( dirextn_to_t21surname_arr[itag] == "" )
				dirextn_to_t21surname_arr[itag] = t21surname
			if ( dirextn_to_t21surname_arr[itag] != t21surname )
				dirextn_to_t21surname_arr[itag] = "Multiple"

			if ( dirextn_to_t21firstname_arr[itag] == "" )
				dirextn_to_t21firstname_arr[itag] = t21firstname
			if ( dirextn_to_t21firstname_arr[itag] != t21firstname )
				dirextn_to_t21firstname_arr[itag] = "Multiple"

			if ( dirextn_to_t21site_arr[itag] == "" )
				dirextn_to_t21site_arr[itag] = t21site
			if ( dirextn_to_t21site_arr[itag] != t21site )
				dirextn_to_t21site_arr[itag] = "Multiple"

			if ( dirextn_to_t21location_arr[itag] == "" )
				dirextn_to_t21location_arr[itag] = t21location
			if ( dirextn_to_t21location_arr[itag] != t21location )
				dirextn_to_t21location_arr[itag] = "Multiple"

			if ( dirextn_to_t21division_arr[itag] == "" )
				dirextn_to_t21division_arr[itag] = t21division
			if ( dirextn_to_t21division_arr[itag] != t21division )
				dirextn_to_t21division_arr[itag] = "Multiple"

			if ( dirextn_to_t21department_arr[itag] == "" )
				dirextn_to_t21department_arr[itag] = t21department
			if ( dirextn_to_t21department_arr[itag] != t21department )
				dirextn_to_t21department_arr[itag] = "Multiple"
			dirextn_to_eggrid_arr[itag] = extgroupunique
		}

		if ( egrid_to_t21site_arr[extgroupunique] == "" )
			egrid_to_t21site_arr[extgroupunique] = t21site
		if ( egrid_to_t21site_arr[extgroupunique] != t21site )
			egrid_to_t21site_arr[extgroupunique] = "Multiple"

                if ( extn_to_egridlist_arr[extn] ! "" )
                        extn_to_egridlist_arr[extn] = extn_to_egridlist_arr[extn] "|"
		extn_to_egridlist_arr[extn] = extn_to_egridlist_arr[extn] "|" extgroupunique
		grid_servtype_sid = extgroupunique "|" 1 "|" extn
		grid_servtype_sid_to_directoryID[grid_servtype_sid] = recordno

                if ( egrid_to_recordnolist_arr[extgroupunique] != "" )
                        egrid_to_recordnolist_arr[extgroupunique] = egrid_to_recordnolist_arr[extgroupunique] "|"
                egrid_to_recordnolist_arr[extgroupunique] = egrid_to_recordnolist_arr[extgroupunique] recordno
                #print "t21dir: egrid_to_recordnolist_arr[" extgroupunique "]=[" egrid_to_recordnolist_arr[extgroupunique] "]"

		if ( t21surname == ".INDIAL" ) {
			indial_nbr = gsub("X","[0-9]",telephone)
			indial_arr[telephone] = t21site
			print " indial_arr[" telephone "] = [" indial_arr[telephone] "]"
		}

	}
	close(t21dircsvfile)
}


function ld_t21groups4(t21groups4file) {
	printf("ld_t21groups4(%s)\n", t21groups4file)
	system("date")

	# loop first to look at G groups (to set grid_to_parentgroupid_arr)
	while ( (getline aline < t21groups4file) > 0 ) {
		split(aline, a_arr, "|")
		##printf("aline=%s\n", aline)
		#printf("a_arr 1=%s 2=%s 3=%s 4=%s\n", a_arr[1], a_arr[2], a_arr[3], a_arr[4])
		parentgroupid = fixforfname(clip(a_arr[1]))
		grid = fixforfname(clip(a_arr[2]))
		grtype = clip(a_arr[3])
		grname1 = clip(a_arr[4])
		grname2 = clip(a_arr[5])
		grname3 = fixforfname(clip(a_arr[6]))

		#if ( parentgroupid == "" )
		#	parentgroupid = "NOPAR"
		if ( grname1 == "" )
			grname1 = "BLANK"
		if ( grname2 == "" )
			grname2 = "BLANK"
		if ( grname3 == "" )
			grname3 = "BLANK"

		# look at G groups only
		if ( grtype != "G" )
			continue

		printf(" G - parentgroupid = [%s]  grid = [%s]  grtype = [%s]", parentgroupid, grid, grtype)
		printf("   grname1 = [%s]  grname2 = [%s]  grname3 = [%s]\n", grname1, grname2, grname3)

		parentchild_arr[parentgroupid "|" grid] = 1

		childcount_arr[parentgroupid] += 1

		grid_to_parentgroupid_arr[grid] = parentgroupid
		print "  grid_to_parentgroupid_arr[" grid "] = [" grid_to_parentgroupid_arr[grid] "]"

		grid_to_grtype_arr[grid] = grtype
		grid_to_grname1_arr[grid] = grname1
		grid_to_grname2_arr[grid] = grname2
		grid_to_grname3_arr[grid] = grname3

		## default CENTRE/PROJECT to grname1/00000
		#CENTRE = grname1
		CENTRE = parentgroupid

		#if ( grtype == "G" ) {
		if ( substr(grid,1,4) == "DEPT" ) {
			#rjs4
			# CENTRE = grname3
			CENTRE = grid
			CENTREdesc = grname2
			CENTREdesc1 = grname1
			CENTREdesc2 = grname2
			CENTREdesc3 = grname3
                 	if ( CENTRE == "" )
				CENTRE = grname1
                 	if ( CENTRE == "" )
				CENTRE = grid
                 	if ( CENTREdesc == "" )
				CENTREdesc = grname1
			if ( CENTREdesc == "" )
				 CENTREdesc = grid
			if ( CENTREdesc3 == "" )
				 CENTREdesc = grname1
			CENTRE_to_CENTREdesc_arr[CENTRE] = CENTREdesc
			CENTRE_to_CENTREdesc1_arr[CENTRE] = CENTREdesc1
			CENTRE_to_CENTREdesc2_arr[CENTRE] = CENTREdesc2
			CENTRE_to_CENTREdesc3_arr[CENTRE] = CENTREdesc3
			print "CENTRE_to_CENTREdesc_arr[" CENTRE "] = " CENTRE_to_CENTREdesc_arr[CENTRE]
		}

		printf("              CENTRE = [%s]\n", CENTRE)
		#rjs9 - set in ld_cde()
		#grid_to_centre_arr[grid] = CENTRE

                #print "grid_to_centre_arr[" grid "]  = [" grid_to_centre_arr[grid] "]"

		#-------------

		CCP = grname1
		centre_dot_project = 0

		## default CENTRE/PROJECT to grname1/00000
		#CENTRE = grname1
		#PROJECT = "00000"
		CENTRE = parentgroupid
		PROJECT = grid

		if ( substr(grid,1,6) == "CENTRE" ) {
			#rjs4
                        CENTRE = parentgroupid
                        PROJECT = grid
			CCP = grnmame3
                        centre_dot_project = 1
                        print "IS centre_dot_project"
			PROJECTdesc = grname2
			PROJECTdesc1 = grname1
			PROJECTdesc2 = grname2
			PROJECTdesc3 = grname3
			if ( PROJECTdesc == "" )
				PROJECTdesc = grid
			if ( PROJECTdesc3 == "" )
				PROJECTdesc3 = grname2
			if ( PROJECTdesc3 == "" )
				PROJECTdesc3 = grid

			CENTREPROJECT_to_PROJECTdesc_arr[CENTRE "|" PROJECT] = PROJECTdesc
			CENTREPROJECT_to_PROJECTdesc1_arr[CENTRE "|" PROJECT] = PROJECTdesc1
			CENTREPROJECT_to_PROJECTdesc2_arr[CENTRE "|" PROJECT] = PROJECTdesc2
			CENTREPROJECT_to_PROJECTdesc3_arr[CENTRE "|" PROJECT] = PROJECTdesc3

			print "0: CENTREPROJECT_to_PROJECTdesc_arr[" CENTRE "|" PROJECT "] = " CENTREPROJECT_to_PROJECTdesc_arr[CENTRE "|" PROJECT]

			#CENTRE_to_GGRID_arr[CENTRE] = parentgroupid
			CENTRE_to_GGRID_arr[CENTRE] = CENTRE
			print "CENTRE_to_GGRID_arr[" CENTRE "] = " CENTRE_to_GGRID_arr[CENTRE]
			# rjs9 centre|project has multiple egrids
			# see ld_cde/ldt21detail which sets CENTREPROJECTSID_to_EGRID_arr
			#CENTREPROJECT_to_EGRID_arr[CENTRE "|" PROJECT] = grid
			#print "CENTREPROJECT_to_EGRID_arr[" CENTRE "|" PROJECT "] = " CENTREPROJECT_to_EGRID_arr[CENTRE "|" PROJECT]

			is_CENTRE_dot_PROJECT_arr[grid] = centre_dot_project
		}

		printf("              CENTRE = [%s]  PROJECT = [%s]\n", CENTRE, PROJECT)
		#rjs9 - set in ld_cde()
		#grid_to_ccp_arr[grid] = CCP
		#grid_to_centre_arr[grid] = CENTRE
		#grid_to_project_arr[grid] = PROJECT

                #print "grid_to_centre_arr[" grid "]  = [" grid_to_centre_arr[grid] "]"
                #print "grid_to_project_arr[" grid "]  = [" grid_to_project_arr[grid] "]"

	}
	close(t21groups4file)


	# now look at non G groups to handle service
	while ( (getline aline < t21groups4file) > 0 ) {
		split(aline, a_arr, "|")
		##printf("aline=%s\n", aline)
		#printf("a_arr 1=%s 2=%s 3=%s 4=%s\n", a_arr[1], a_arr[2], a_arr[3], a_arr[4])
		parentgroupid = fixforfname(clip(a_arr[1]))
		grid = fixforfname(clip(a_arr[2]))
		grtype = clip(a_arr[3])
		grname1 = clip(a_arr[4])
		grname2 = clip(a_arr[5])
		grname3 = fixforfname(clip(a_arr[6]))

		#if ( parentgroupid == "" )
		#	parentgroupid = "NOPAR"
		if ( grname1 == "" )
			grname1 = "BLANK"
		if ( grname2 == "" )
			grname2 = "BLANK"
		if ( grname3 == "" )
			grname3 = "BLANK"

		# look at non G groups only
		if ( grtype == "G" )
			continue

		printf(" nonG - parentgroupid = [%s]  grid = [%s]  grtype = [%s]", parentgroupid, grid, grtype)
		printf("   grname1 = [%s]  grname2 = [%s]  grname3 = [%s]\n", grname1, grname2, grname3)

		parentchild_arr[parentgroupid "|" grid] = 1

		childcount_arr[parentgroupid] += 1

		grid_to_parentgroupid_arr[grid] = parentgroupid
		print "  grid_to_parentgroupid_arr[" grid "] = [" grid_to_parentgroupid_arr[grid] "]"

		grid_to_grtype_arr[grid] = grtype
		grid_to_grname1_arr[grid] = grname1
		grid_to_grname2_arr[grid] = grname2
		grid_to_grname3_arr[grid] = grname3

		#rjs9 - set in ld_cde()
		#grid_to_ccp_arr[grid] = CCP
		#grid_to_centre_arr[grid] = CENTRE
		#grid_to_project_arr[grid] = PROJECT

                #print "grid_to_centre_arr[" grid "]  = [" grid_to_centre_arr[grid] "]"
                #print "grid_to_project_arr[" grid "]  = [" grid_to_project_arr[grid] "]"
	}
	close(t21groups3file)


	# create entry for Unallocated Mobiles
	desc = "Unallocated Mobiles"
	grid = "UN_MOB"
	parentgroupid = "UNA"
	CENTRE = parentgroupid
	PROJECT = grid
	grid_to_grtype_arr[grid] = "E"
	grid_to_centre_arr[grid] = CENTRE
	grid_to_project_arr[grid] = PROJECT
	grid_to_ccp_arr[grid] = desc
	grid_to_grname1_arr[grid] = desc
	grid_to_grname2_arr[grid] = desc
	grid_to_grname3_arr[grid] = desc
	grid_to_parentgroupid_arr[grid] = parentgroupid
	#CENTRE_to_CENTREdesc_arr[CENTRE] = desc
	#CENTRE_to_CENTREdesc1_arr[CENTRE] = desc
	#CENTRE_to_CENTREdesc2_arr[CENTRE] = desc
	#CENTRE_to_CENTREdesc3_arr[CENTRE] = desc
	CENTREPROJECT_to_PROJECTdesc_arr[CENTRE "|" PROJECT] = desc
	CENTREPROJECT_to_PROJECTdesc1_arr[CENTRE "|" PROJECT] = desc
	CENTREPROJECT_to_PROJECTdesc2_arr[CENTRE "|" PROJECT] = desc
	CENTREPROJECT_to_PROJECTdesc3_arr[CENTRE "|" PROJECT] = desc
	CENTRE_to_GGRID_arr[CENTRE] = parentgroupid
	# rjs9 centre|project has multiple egrids
	# see ld_cde/ldt21detiail which sets CENTREPROJECTSID_to_EGRID_arr
	#CENTREPROJECT_to_EGRID_arr[CENTRE "|" PROJECT] = grid
	is_CENTRE_dot_PROJECT_arr[grid] = 0
	is_TENANT_arr[grid] = 0
}


function ld_t21WBDL(t21WBDLfile) {
	printf("ld_t21WBDL(%s)\n", t21WBDLfile)
	system("date")
	while ( (getline aline < t21WBDLfile) > 0 ) {
		split(aline, a_arr, "|")
		printf("aline=%s\n", aline)
		#printf("a_arr 1=%s 2=%s 3=%s 4=%s\n", a_arr[1], a_arr[2], a_arr[3], a_arr[4])
		f = 0
		# rjs9
		wbdconf_recordno = clip(a_arr[++f])
		wbdconf_distid = clip(a_arr[++f])
		wbdconf_distiddesc = clip(a_arr[++f])
		wbdconf_lastdist = clip(a_arr[++f])
		wbdconf_lastdistdate = clip(a_arr[++f])
		wbdconf_altemail = clip(a_arr[++f])
		wbdconf_altemailyn = clip(a_arr[++f])
		wbdconf_wbd_method = clip(a_arr[++f])
		wbdconf_wbd_type = clip(a_arr[++f])
		#rjs 9 (email no FORCE UPPER case)
		#wbdconf_userid = toupper(clip(a_arr[++f]))
		wbdconf_userid = clip(a_arr[++f])
		wbdconf_passwd = clip(a_arr[++f])
		wbdconf_sdate = clip(a_arr[++f])
		wbdconf_edate = clip(a_arr[++f])
		wbdlist_grid = fixforfname(clip(a_arr[++f]))
		dc_email = clip(a_arr[++f])

		if ( wbdconf_distid == "" ) {
			printf("FATAL ERROR: blank wbdconf_distid aline=[%s]\n", aline)
			exit 1
		}

		DISTID = wbdconf_distid


		# Testing
		if ( Testing >= 3 && ! (DISTID == "1001" || DISTID == "1002" || DISTID == "1003" || (DISTID >= "2000" && DISTID <= "2005") || DISTID == "2009" || DISTID == "2032" || DISTID == "2043") ) {
			#print "DISTID #TESTING... SKIP  DISTID = " DISTID
			continue
		}

		# skip PERSONAL type (used for passwords only)
		# virtual personal list entries will be created by 
		# create_virtual_user_dist_lists()
		if ( wbdconf_wbd_type == "PERSONAL" ) {
			continue;
		}

		dirID = "Person" wbdconf_recordno
		
                directoryID_to_distid_arr[dirID] = wbdconf_distid

		distid_to_directoryID_arr[wbdconf_distid] = dirID
		distid_to_distiddesc_arr[wbdconf_distid] = wbdconf_distiddesc

		t21name = recordno_to_t21name_arr[dirID]
		distid_to_t21name_arr[wbdconf_distid] = t21name
		distid_to_userid_arr[wbdconf_distid] = wbdconf_userid
		if ( distid_to_userid_arr[wbdconf_distid] == "" ) {
			email = recordno_to_email_arr[dirID]
			distid_to_userid_arr[wbdconf_distid] = email
		}

		distid_to_wbd_method_arr[wbdconf_distid] = wbdconf_wbd_method
		distid_to_wbd_type_arr[wbdconf_distid] = wbdconf_wbd_type
		print "distid_to_wbd_type_arr[" wbdconf_distid "]=[" distid_to_wbd_type_arr[wbdconf_distid] "]"

		distgrid = wbdconf_distid "|" wbdlist_grid
		distgrid_arr[distgrid] = 1

		print "distid_to_t21name_arr[" wbdconf_distid "]=[" distid_to_t21name_arr[wbdconf_distid] "]"
		print "distid_to_userid_arr[" wbdconf_distid "]=[" distid_to_userid_arr[wbdconf_distid] "]"


		###############
		# create virual management distribution lists
		mng_distid = "mng" wbdconf_distid
		mng_distiddesc = "Management Reports"
		mng_wbd_type = "MANAGEMENT"
		virtual_management_distid_arr[mng_distid] = wbdconf_distid
		distid_to_directoryID_arr[mng_distid] = dirID
		distid_to_distiddesc_arr[mng_distid] = mng_distiddesc
		distid_to_t21name_arr[mng_distid] = t21name
		distid_to_userid_arr[mng_distid] = wbdconf_userid
		if ( distid_to_userid_arr[mng_distid] == "" ) {
			email = recordno_to_staffid_arr[dirID]
			distid_to_userid_arr[mng_distid] = email
		}
		distid_to_wbd_method_arr[mng_distid] = wbdconf_wbd_method
		distid_to_wbd_type_arr[mng_distid] = mng_wbd_type

		distgrid = mng_distid "|" wbdlist_grid
		distgrid_arr[distgrid] = 1

	}
	close(t21WBDLfile)
}


function ld_t21wbdexclude(t21wbdexcludefile) {
	printf("ld_t21wbdexclude(%s)\n", t21wbdexcludefile)
	system("date")
	while ( (getline aline < t21wbdexcludefile) > 0 ) {
		split(aline, a_arr, "|")
		printf("aline=%s\n", aline)
		#printf("a_arr 1=%s 2=%s 3=%s 4=%s\n", a_arr[1], a_arr[2], a_arr[3], a_arr[4])
		f = 0
		wbdexclude_servicetypeid = clip(a_arr[++f])
		wbdexclude_siteid = clip(a_arr[++f])
		wbdexclude_grid = fixforfname(clip(a_arr[++f]))
		wbdexclude_extnlo = clip(a_arr[++f])
		wbdexclude_extnhi = clip(a_arr[++f])

		exclude_arr[wbdexclude_servicetypeid "|" wbdexclude_siteid "|" wbdexclude_grid "|" wbdexclude_extnlo "|" wbdexclude_extnhi] = 1
	}
	close(t21wbdexcludefile)
}



function ld_exwebconf(exwebconffile)
{
	printf("ld_exwebconf(%s)\n", exwebconffile)
	system("date")

	#noddsupfile = "noddsup.txt"
	#while ( (getline custline < noddsupfile) > 0 ) {
	#	split(custline, noddsupgggrid, " ")
	#	noddsuparr[noddsupgggrid[1]] = 1
	#}
	#close(noddsupfile)

	while ( (getline exwebconfline < exwebconffile) > 0 ) {
		split(exwebconfline, exwebconfarr, ",")
		#printf("exwebconfline=%s\n", exwebconfline)
		#printf("exwebconfarr 1=%s 2=%s 3=%s 4=%s\n", exwebconfarr[1], exwebconfarr[2], exwebconfarr[3], exwebconfarr[4])

		exwebconf_parent = exwebconfarr[1]
		exwebconf_dialxx = exwebconfarr[2]
		exwebconf_email = exwebconfarr[3]
		exwebconf_emailyn = exwebconfarr[4]
		exwebconf_status1 = exwebconfarr[5]
		exwebconf_lastemailed = exwebconfarr[6]
		exwebconf_lastemaileddate = exwebconfarr[7]
		exwebconf_print = exwebconfarr[8]
		exwebconf_cdgroup = exwebconfarr[9]

                # flag parent in exwebconf
                exwebconf_parent_arr[exwebconf_parent] = 1

		# rjs9
                #if ( platinv_parent_arr[exwebconf_parent] != 1 ) {
                #        print "WARNING: parent = " exwebconf_parent " found in xwebconf, but is not in platinum invoice data"
                #}

                #printf("noddsuparr[%s] = %d\n", exwebconf_parent, noddsuparr[ewebconf_parent]);
		if ( toupper(exwebconf_dialxx) == "N" )
                        noddsuparr[exwebconf_parent] = 1;
                else
                        noddsuparr[exwebconf_parent] = 0;

                #tmpgggrid = exwebconf_parent;
                #if ( noddsuparr[tmpgggrid] == 1 )
                #       printf("     NO DD SUPPRESS\n");
                #else
                #       printf("     DO DD SUPPRESS\n");

                # store parent grouping
                exwebconf_parent_to_cdgroup_arr[exwebconf_parent] = exwebconf_cgroup
	}
	close(exwebconffile)

}

#----------------------------------------------------------

function ld_t21callcallserviceids(t21callsfile,seid)
{
	printf("ld_t21callserviceids(%s)  seid=%s\n",t21callsfile,seid)
	while ( (getline cdrline < t21callsfile) > 0 ) {
		split(cdrline, cdr_arr, ",")
		##printf("cdrline=%s\n", cdrline)
		gggrid = fixforfname(trim(cdr_arr[1]))
		eggrid = fixforfname(trim(cdr_arr[2]))
		t21egrid_to_seid_arr[eggrid] = seid
	}
	close(t21callsfile)
	print "finished ld_t21callserviceids()"
}


function ld_t21swbcallsserviceids(t21swbcallsfile,seid)
{
	printf("ld_t21swbcallserviceids(%s)  seid=%s\n",t21callsfile,seid)
	while ( (getline aline < t21swbcallsfile) > 0 ) {
		split(aline, a_arr, ",")
		f = 0
		swb_shipto = a_arr[++f]
		swb_eggrid = a_arr[++f]
		t21egrid_to_seid_arr["SW" swb_eggrid] = seid
	}
	close(t21swbcallsfile)
	print "finished ld_t21swbcallserviceids()"
}


function ld_t21vmlcallsserviceids(t21vmlcallsfile,seid)
{
	printf("ld_t21vmlcallserviceids(%s)  seid=%s\n",t21callsfile,seid)
	while ( (getline aline < t21vmlcallsfile) > 0 ) {
		split(aline, a_arr, ",")
		f = 0
		vml_shipto = a_arr[++f]
		vml_eggrid = a_arr[++f]
		t21egrid_to_seid_arr["VM" vml_eggrid] = seid
	}
	close(t21vmlcallsfile)
	print "finished ld_t21vmlcallserviceids()"
}

#----------------------------------------------------------
# onnet ?

function is_onnet_mobile(nbr, i)
{
	i = index(nbr,"04")
	if ( i > 0 ) {
		realnbr = substr(nbr,i,10)
		if ( length(realnbr) == 10 ) {
			#print " is_onnet_mobile() 04  "  "nbr=[" nbr "]" "  mobile_to_sid_arr[" realnbr "] = [" mobile_to_sid_arr[realnbr] "]"
			if ( mobile_to_sid_arr[realnbr] != "" )
				return 1
		}
	}
	i = index(nbr,"614")
	if ( i > 0 ) {
		realnbr = "0" substr(nbr,i+2,9)
		if ( length(realnbr) == 10 ) {
			#print " is_onnet_mobile() 614 "  "nbr=[" nbr "]" "  mobile_to_sid_arr[" realnbr "] = [" mobile_to_sid_arr[realnbr] "]"
			if ( mobile_to_sid_arr[realnbr] != "" )
				return 1
		}
	}
	return 0
}


function is_onnet_fixed(nbr, i,tnbr)
{
	split(nbr, tmpnbr_arr, " ")
	tnbr = tmpnbr_arr[1]
	if ( i > 1 )
		tnbr = substr(nbr,i,10)
	# is an extn
	if ( dirextn_to_t21site_arr[tnbr] != "" ) {
		#print " is_onnet_fixed()"  "  dirextn_to_t21site_arr[" tnbr "] = [" dirextn_to_t21site_arr[tnbr] "]"
		return 1
	}
	for ( indial in indial_arr ) {
		if ( match(tnbr, indial) ) {
			#print " is_onnet_fixed()"  "  tnbr=[" tnbr "]" "  indial=[" indial "]"
			return 1
		}
		if ( match(tnbr, substr(indial,3)) ) {	# without area code
			#print " is_onnet_fixed()"  "  tnbr=[" tnbr "]" "  substr(" indial ",3)=[" substr(indial,3) "]"
			return 1
		}
	}
	return 0
}


function is_onnet(nbr,isonnet)
{
	isonnet = is_onnet_mobile(nbr) ? "ONNET Mobile" : ""
	if ( isonnet == "" )
		isonnet = is_onnet_fixed(nbr) ? "Extension" : ""
	return isonnet
}


#----------------------------------------------------------
# premium 19XXXXxx mobile / 19XXXX... fixed line service ?

function is_premium_service(nbr, servtype, i,tnbr)
{
	split(nbr, tmpnbr_arr, " ")
	tnbr = tmpnbr_arr[1]
	if ( substr(tnbr,1,1) == "+" )
		tndr = substr(tnbr,2)
	i = match(tnbr,"^19[0-9][0-9][0-9][0-9]")
	if ( i > 0  ) {
		# mobile premium 19XXXXxx
		if ( servtype == "10" && length(tnbr) <= 8 ) {
			#print " is_premium_service()"  "  nbr=[" nbr "]" "  servtype=[" servtype "]" "  tnbr=[" tnbr "]"
			return 1
		}
		# fixed line 19XXXX...
		if ( servtype == "1" ) {
			#print " is_premium_service()"  "  nbr=[" nbr "]" "  servtype=[" servtype "]" "  tnbr=[" tnbr "]"
			return 1
		}
	}
	return 0
}


#----------------------------------------------------------
# apply stripdd and ovrcars to dialled no

function rtdialno(siteid,trunklocalid,dialledno)
{
	#print "rtdialno(" siteid "," trunklocalid "," dialledno ")"

	if ( dialledno == "" )
		return dialledno

	new_dialno = dialledno

	site_trunk = siteid "|" trunklocalid

	stripdd = site_trunk_to_stripdd[site_trunk]
	ovrcar1 = site_trunk_to_ovrcar1[site_trunk]
	ovrcar1acc = site_trunk_to_ovrcar1acc[site_trunk]
	ovrcar2 = site_trunk_to_ovrcar2[site_trunk]
	ovrcar2acc = site_trunk_to_ovrcar2acc[site_trunk]

	if ( strippdd > 0 ) {
		new_dialno = substr(dialledno,stripdd + 1)
		#print "rtdialno stripdd=" stripdd "  new_dialno=[" new_dialno "]" "  dialledno=[" dialledno "]"
	}

	len_ovrcar1acc = length(ovrcar1acc)
	if ( len_ovrcar1acc > 0 ) {
		if ( substr(ovrcar1acc,length_ovrcar1acc) == "+" )
			--len_ovrcar1acc
		if ( ovrcar1 != "" && len_ovrcar1acc > 0 && substr(new_dialno,1,len_ovrcar1acc) == substr(ovrcar1acc,1,len_ovrcar1acc) ) {
			new_dialno = substr(new_dialno,len_ovrcar1acc + 1)
			#print "rtdialno ovrcar1acc=[" ovrcar1acc "]  new_dialno=[" new_dialno "]" "  dialledno=[" dialledno "]"
			return new_dialno
		}
	}

	len_ovrcar2acc = length(ovrcar2acc)
	if ( len_ovrcar2acc > 0 ) {
		if ( substr(ovrcar2acc,length_ovrcar2acc) == "+" )
			--len_ovrcar2acc
		if ( ovrcar2 != "" && len_ovrcar2acc > 0 && substr(new_dialno,1,len_ovrcar2acc) == substr(ovrcar2acc,1,len_ovrcar2acc) ) {
			new_dialno = substr(new_dialno,len_ovrcar2acc + 1)
			#print "rtdialno ovrcar2acc=[" ovrcar2acc "]  new_dialno=[" new_dialno "]" "  dialledno=[" dialledno "]"
			return new_dialno
		}
	}

	#print "rtdialno done new_dialno=[" new_dialno "]" "  dialledno=[" dialledno "]"

	return new_dialno
}



#----------------------------------------------------------

function create_virtual_user_dist_lists()
{
## Testing
#print "SKIPPING PERSONAL reports (needs LDAP auth)"
#return 0
	print "create_virtual_user_dist_lists()"
	system("date")
	# Mobile users...
	for ( mobile in mobile_to_directoryID_arr ) {
		directoryID = mobile_to_directoryID_arr[mobile]
		grid = mobile_to_grid_arr[mobile]
		mob_carrier = mobile_to_mob_carriercode_arr[mobile] 
		mob_name = mobile_to_name_arr[mobile]
		if ( mob_name == "_blank_" )
			mob_name = "BLANK NAME"
		sid = mobile_to_sid_arr[mobile]

		if ( (directoryID <= 0 || directoryID == "") && grid == "" ) {
			print "ERROR: create virtual user dist list: SKIPPING blank directoryID and grid for mobile=" mobile
			continue
		}

		# get email from tPerson
		email = fixforfname(recordno_to_email_arr[directoryID])

		# get staffid from tPerson
		staffid = recordno_to_staffid_arr[directoryID]

		# for personal assigned mobile
		# skip if no email (VT userid)
		if ( directoryID > 0 && email == "" ) {
			print "WARNING: create virtual user dist list: SKIPPING blank email for mobile=" mobile "  directoryID=" directoryID
			continue
		}

		# for personal assigned mobile
		# skip if no staffid
		if ( directoryID > 0 && staffid == "" ) {
			print "WARNING: create virtual user dist list: SKIPPING blank staffid for mobile=" mobile "  directoryID=" directoryID
			continue
		}

		# get billing grid for personal mobile
		## use extgroupunique from directory if mobconf grid is not set
		# t21 dir not used for mobiles - egrid is sid (see groups4)
		if ( grid == "" ) {
			#grid = recordno_to_eggrid_arr[directoryID]
			grid = sid
			if ( grid == "" ) {
				print "ERROR: create virtual user dist list: SKIPPING blank directory grid for mobile=" mobile "  directoryID=" directoryID
				continue
			}
		}

		Vdistid = staffid

		# Testing
		if ( Testing >= 3 &&
		     !(Vdistid == "P4668" || Vdistid == "D101715" ||
		       Vdistid == "P13536" || Vdistid == "D101715" ||
		       Vdistid == "P14684" || Vdistid == "D164044")) {
			#print "DISTID TESTING... SKIP  Vdistid = " Vdistid
			continue
		}

		if ( distid_to_distiddesc_arr[Vdistid] == "" ) {
			print "create Vdistid=[" Vdistid "]"
			distid_to_directoryID_arr[Vdistid] = directoryID
			Vdistid_to_directoryID_arr[Vdistid] = directoryID
			distid_to_distiddesc_arr[Vdistid] = "Personal Reports for " mob_name
			distid_to_t21name_arr[Vdistid] = mob_name
			distid_to_userid_arr[Vdistid] = email
			distid_to_wbd_method_arr[Vdistid] = "WEB"
			distid_to_wbd_type_arr[Vdistid] = "PERSONAL"

			print "  distid_to_distiddesc_arr[" Vdistid "]=[" distid_to_distiddesc_arr[Vdistid] "]"
		}
		#else {
		#	print "WARNING: Existing distid_to_distiddesc_arr[" Vdistid "] = " distid_to_distiddesc_arr[Vdistid]
		#}

		print "       mobile    Vdistid=[" Vdistid "]" "  email=[" email "]" "  staffid=[" staffid "]" "  directoryID=[" directoryID "]" "  grid=[" grid "]" "  sid=[" sid "]" "  mob_name=[" mob_name "]"  

		servtype = 10
		Stag = grid "|" servtype "|" sid

		if ( Vdistid_to_grid_servtype_sidlist_arr[Vdistid] != "" )
			Vdistid_to_grid_servtype_sidlist_arr[Vdistid] = Vdistid_to_grid_servtype_sidlist_arr[Vdistid] "~"
		Vdistid_to_grid_servtype_sidlist_arr[Vdistid] = Vdistid_to_grid_servtype_sidlist_arr[Vdistid] Stag

		print "          Vdistid_to_grid_servtype_sidlist_arr[" DISTID "]=[" Vdistid_to_grid_servtype_sidlist_arr[DISTID] "]"

		personal_egrid_servtype_SID_to_distid_arr[Stag] = Vdistid
		print "DEBUG: personal_egrid_servtype_SID_to_distid_arr[" Stag "] = " personal_egrid_servtype_SID_to_distid_arr[Stag]

		distgrid = Vdistid "|" Stag
		distgrid_arr[distgrid] = 1
	}


	# Fixed Line users...
	for ( dirextn in dirextn_to_recordno_arr ) {
		directoryID = dirextn_to_recordno_arr[dirextn]
		grid = dirextn_to_eggrid_arr[dirextn]
		t21_name = dirextn_to_t21name_arr[dirextn]
		if ( t21_name == "_blank_" )
			t21_name = "BLANK NAME"
		sid = dirextn

		if ( (directoryID <= 0 || directoryID == "") && grid == "" ) {
			print "ERROR: create virtual user dist list: SKIPPING blank directoryID and grid for dirextn=" dirextn
			continue
		}

		# get email from T21 dir entry
		email = recordno_to_email_arr[directoryID]

		# get tPerson recordno from T21 dir email
		tPrecordno = email_to_tPrecordno_arr[email]
		print " email_to_tPrecordno_arr[" email "]=[" email_to_tPrecordno_arr[email] "]"

		# get staffid from tPerson
		staffid = recordno_to_staffid_arr[tPrecordno]

		# skip if no email (VT userid)
		if ( email == "" ) {	# skip if no email (VT userid)
			print "WARNING: create virtual user dist list: SKIPPING blank email for dirextn=" dirextn "  directoryID=" directoryID
			continue
		}

		if ( staffid == "" ) {
			print "WARNING: create virtual user dist list: SKIPPING blank staffid for dirextn=" dirextn "  directoryID=" directoryID
			continue
		}

		if ( grid == "" ) {
			print "ERROR: create virtual user dist list: SKIPPING blank directory grid for dirextn=" dirextn "  directoryID=" directoryID
			continue
		}

		Vdistid = staffid

		# Testing
                if ( Testing >= 1 &&
                     !(Vdistid == "P4668" || Vdistid == "D101715" ||
                       Vdistid == "P13536" || Vdistid == "D101715" ||
                       Vdistid == "P14684" || Vdistid == "D164044")) {
			#print "DISTID TESTING... SKIP  Vdistid = " Vdistid
			continue
		}

		# must be fixed line only
		if ( distid_to_distiddesc_arr[Vdistid] == "" ) {
			print "create fixedline Vdistid=[" Vdistid "]"
			distid_to_directoryID_arr[Vdistid] = directoryID
			Vdistid_to_directoryID_arr[Vdistid] = directoryID
			distid_to_distiddesc_arr[Vdistid] = "Personal Fixed Line Reports for " t21_name
			distid_to_t21name_arr[Vdistid] = t21_name
			distid_to_userid_arr[Vdistid] = email
			distid_to_wbd_method_arr[Vdistid] = "WEB"
			distid_to_wbd_type_arr[Vdistid] = "PERSONAL"

			print "  distid_to_distiddesc_arr[" Vdistid "]=[" distid_to_distiddesc_arr[Vdistid] "]"
		}
		#else {
		#	print "WARNING: Existing distid_to_distiddesc_arr[" Vdistid "] = " distid_to_distiddesc_arr[Vdistid]
		#}

		print "       fixedline Vdistid=[" Vdistid "]" "  staffid=[" staffid "]" "  directoryID=[" directoryID "]" "  grid=[" grid "]" "  sid=[" sid "]" "  t21_name=[" t21_name "]"  

		servtype = 1
		Stag = grid "|" servtype "|" sid

		if ( Vdistid_to_grid_servtype_sidlist_arr[Vdistid] != "" )
			Vdistid_to_grid_servtype_sidlist_arr[Vdistid] = Vdistid_to_grid_servtype_sidlist_arr[Vdistid] "~"
		Vdistid_to_grid_servtype_sidlist_arr[Vdistid] = Vdistid_to_grid_servtype_sidlist_arr[Vdistid] Stag

		print "          Vdistid_to_grid_servtype_sidlist_arr[" Vdistid "]=[" Vdistid_to_grid_servtype_sidlist_arr[Vdistid] "]"

		personal_egrid_servtype_SID_to_distid_arr[Stag] = Vdistid
		print "DEBUG: personal_egrid_servtype_SID_to_distid_arr[" Stag "] = " personal_egrid_servtype_SID_to_distid_arr[Stag]

		distgrid = Vdistid "|" Stag
		distgrid_arr[distgrid] = 1
	}


}


#----------------------------------------------------------


function create_egrid_distidlist()
{
	print "create_egrid_distidlist()"
	system("date")
	for (grid in grid_to_grtype_arr ) {
		if ( grid_to_grtype_arr[grid] != "E" )
			continue
		for ( DISTID in distid_to_distiddesc_arr ) {
			# do all grids in distlist
			for ( dgstr in distgrid_arr ) {
				split(dgstr, dgstr_arr, "|")
				if ( DISTID != dgstr_arr[1] )
					continue
				distgrid = dgstr_arr[2]

				# keep a list of egrids in distids
				# (see addstar code below)
				if ( grid_to_grtype_arr[distgrid] == "E" && distgrid == grid ) {
					distid_egrid_arr[DISTID "|" grid] = 1
				}

				# Vdistid not added to distgrid_arr yet !
				#distservtype = dgstr_arr[3]
				#distsid = dgstr_arr[4]
				#if ( distservtype != "" || distsid != "" ) {
				#	# skip single servtype/sid
				#	# (personal Vdistid)
				#	continue
				#}

				#print "  DISTID=" DISTID "  distgrid=[" distgrid "]"

				if (test_grid1_belongs_to_grid2(grid, distgrid)) {
					if ( distid_to_wbd_type_arr[DISTID] == "MANAGEMENT" ) {
						if ( mng_egrid_distidlist_arr[grid] != "" )
							mng_egrid_distidlist_arr[grid] = mng_egrid_distidlist_arr[grid] "|" DISTID
						else
							mng_egrid_distidlist_arr[grid] = DISTID
					}
					else {
						if ( egrid_distidlist_arr[grid] != "" )
							egrid_distidlist_arr[grid] = egrid_distidlist_arr[grid] "|" DISTID
						else
							egrid_distidlist_arr[grid] = DISTID
					}
					distid_allegrid_arr[DISTID "|" grid] = 1

					#store a list of egrids for each distid
					if ( distid_to_egridlist_arr[DISTID] != "" )
						distid_to_egridlist_arr[DISTID] = distid_to_egridlist_arr[DISTID] "|"
					distid_to_egridlist_arr[DISTID] = distid_to_egridlist_arr[DISTID] grid

					#if ( DISTID == "mng1001" ) {
					#	print "SFDBG0:  set distid_allegrid_arr[" DISTID "|" grid "]=[" distid_allegrid_arr[DISTID "|" grid] "]"
					#}
				}

			}
		}
	}
}




#----------------------------------------------------------

function get_siteid(extn)
{
	for ( extrngstr in extrange_to_siteid_arr ) {
		split(extrngstr, extrngstr_arr, "|")
		#print "getsiteid extn = " extn "   extrngstr_arr[1] = " extrngstr_arr[1] "   extrngstr_arr[2] = " extrngstr_arr[2]
		if ( extn >= extrngstr_arr[1] && extn <= extrngstr_arr[2] ) {
			
			#print "get_siteid found siteid = " extrange_to_siteid_arr[extrngstr] "  for extn = " extn
			#print "      extrngstr_arr[1] = " extrngstr_arr[1] "   extrngstr_arr[2] = " extrngstr_arr[2]
			
			return extrange_to_siteid_arr[extrngstr]
		}
	}
	return -1
}


function get_code1(extn)
{
	for ( extrngstr in extrange_to_code1_arr ) {
		split(extrngstr, extrngstr_arr, "|")
		if ( extn >= extrngstr_arr[1] && extn <= extrngstr_arr[2] ) {
			
			#print "get_code1 found code1 = " extrange_to_code1_arr[extrngstr] "  for extn = " extn
			#print "      extrngstr_arr[1] = " extrngstr_arr[1] "   extrngstr_arr[2] = " extrngstr_arr[2]
			
			return extrange_to_code1_arr[extrngstr]
		}
	}
	return ""
}


function get_code2(extn)
{
	for ( extrngstr in extrange_to_code2_arr ) {
		split(extrngstr, extrngstr_arr, "|")
		if ( extn >= extrngstr_arr[1] && extn <= extrngstr_arr[2] ) {
			
			#print "get_code2 found code2 = " extrange_to_code2_arr[extrngstr] "  for extn = " extn
			#print "      extrngstr_arr[1] = " extrngstr_arr[1] "   extrngstr_arr[2] = " extrngstr_arr[2]
			
			return extrange_to_code2_arr[extrngstr]
		}
	}
	return ""
}


function nget_code2(extn,code1)
{
	for ( extrngcode1str in extrangecode1_to_code2_arr ) {
		split(extrngcode1str, extrngcode1str_arr, "|")
		#print "nget_code2 extn=[" extn "]" "  code1=[" code1 "]" "  extrngcode1str=[" extrngcode1str "]"
		#print "      extrngcode1str_arr[1] = " extrngcode1str_arr[1] "   extrngcode1str_arr[2] = " extrngcode1str_arr[2] "   extrngcode1str_arr[3] = " extrngcode1str_arr[3]
		if ( extn >= extrngcode1str_arr[1] && extn <= extrngcode1str_arr[2] && extrngcode1str_arr[3] == code1 ) {
			#print "nget_code2  found code2=[" extrangecode1_to_code2_arr[extrngcode1str] "]"
			return extrangecode1_to_code2_arr[extrngcode1str]
		}
	}
	return ""
}


#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------

##############################################################
# add extension totals to back billing output

# newbb1 - includes all service type
# newbb1 BBBBBBBB

function spit_newbb1(grid, sid, servtype)
{
	#print "spit_newbb1(" grid "," sid "," servtype ")"
	gn_ccpcode = grid_to_ccp_arr[grid]
	cc_pc = gn_ccpcode

	CENTRE = grid_to_centre_arr[grid]
	PROJECT = grid_to_project_arr[grid]
	## rjs9
	#pargrid = grid_to_parentgroupid_arr[grid]
	#CENTRE = grid_to_centre_arr[pargrid]
	#PROJECT = grid_to_project_arr[pargrid]

	SHIPTO = CENTRE_to_ShipTo_arr[CENTRE]
	GROUP = grid_to_parentgroupid_arr[CENTRE]

	newlast_inv_cc_pc = ""
	newlast_inv_grid = ""

	# total call cost
	tot_cost = totalcost_arr["TOTAL" "|" servtype "|" SHIPTO "|" GROUP "|" CENTRE "|" PROJECT "|" sid "|" "All" "|" "All" "|" "All" "|" "All"]

	# mobile GROUP ASSIGNED mobile
	#if ( servtype == "10" && sid == "" )

#ZZZZZ
#	# if no extn (mostly mobile GROUP ASSIGNED mobile)
#	if ( t21_extn == "" ) {
#		code1 = ""
#		code2 = ""
#	}
#	else {
#		# from extension range table get
#		code1 = get_code1(t21_extn)
#		code2 = get_code2(t21_extn)
#	}

#ZZZZZ
#	if ( servtype == "10" ) {
#		directoryID = mobsid_to_directoryID_arr[sid]
#		code1 = recordno_to_t21site_arr[directoryID]
#	}
#	else {
#		#code1 = dirextn_to_t21site_arr[t21extn]
#		code1 = get_code1(t21_extn)
#		#code2 = get_code2(t21_extn)
#	}
#	if ( code1 == "" )
#		code1 = egrid_to_t21site_arr[grid]
#
#	if ( t21_extn == "" ) {
#		#code1 = ""
#		code2 = ""
#	}
#	else {
#		# from extension range table get
#		#code1 = get_code1(t21_extn)
#		code2 = nget_code2(t21_extn,code1)
#	}

	if ( t21_extn == "" ) {
		code1 = ""
		code2 = ""
	}
	else {
		if ( servtype == "10"  && t21_extn == 0 ) {
			directoryID = mobsid_to_directoryID_arr[sid]
			code1 = recordno_to_t21site_arr[directoryID]
			code2 = nget_code2(t21_extn,code1)
		}
		else {
			# from extension range table get
			code1 = get_code1(t21_extn)
			code2 = get_code2(t21_extn)
		}
	}
#ZZZZZ

	# can't do this (code2 is NOT unique within extn group)
	#code1 = egrid_to_code1[grid]
	#code2 = egrid_to_code2[grid]
	if ( code2 == "" ) 
		code2 = "16"	# default

	# set output format
	outfmt = 1

	# Add campus code to centre.project for valid cost-centres only
	# and set correct output file extension
	if ( is_CENTRE_dot_PROJECT_arr[grid] ) {
		if ( code2 != "" ) {
			cc_pc = CENTRE "." PROJECT "." code2
		}
		else {
			cc_pc = CENTRE "." PROJECT
		}
		outfileext=".txt"
		delim="|"
		outfile = "TIMS_" eyyyymmdd outfileext
		outfmt = 1
	}
	else
	if ( is_TENANT_arr[grid] ) {
		outfileext="_invoice.csv"
		delim=","
		outfile = "TIMS_" eyyyymmdd outfileext
		newinv_delim = delim
		newinv_outfile = outfile
		newinv_tot_cost_exgst = sprintf("%.2f", mround2(newinv_tot_cost_exgst + tot_cost))
		newlast_inv_cc_pc = cc_pc
		newlast_inv_grid = grid
		outfmt = 2
	}
	else {
		outfileext=".other"
		delim="|"
		outfile = "TIMS_" eyyyymmdd outfileext
		outfmt =1
	}

	print "spit_newbb1(" grid "," sid "," servtype ")  sid=[" sid "]  cc_pc=[" cc_pc "]   CENTRE=[" CENTRE "]  PROJECT=[" PROJECT "]  code1=[" code1 "]  code2=[" code2 "]  directoryID=[" directoryID "]"

	set_project_displayinfo(CENTRE, PROJECT)

	if ( servtype != 10 ) {
		#itag = grid "|" sid
		itag = sid
		surname = dirextn_to_t21surname_arr[itag]
		firstname = dirextn_to_t21firstname_arr[itag]
		division = dirextn_to_t21division_arr[itag]
		department = dirextn_to_t21department_arr[itag]
		location = dirextn_to_t21location_arr[itag]
	}
	else {
		if ( directoryID != "" ) {
			surname = recordno_to_t21surname_arr[directoryID]
			firstname = recordno_to_t21firstname_arr[directoryID]
			division = recordno_to_t21division_arr[directoryID]
			department = recordno_to_t21department_arr[directoryID]
			location = recordno_to_t21location_arr[directoryID]
		}
		else {
			surname = ""
			firstname = ""
			#rjs9 division = ccpdesc
			division = ""
			department = ""
			location = ""
		}
	}

	if ( surname == "_blank_" )
		surname = ""
	if ( firstname == "_blank_" )
		firstname = ""
	if ( division == "_blank_" )
		division = ""
	if ( department == "_blank_" )
		department = ""
	if ( location == "_blank_" )
		location = ""

	name_sep = " "
	if ( firstname == "" || surname == "" )
		name_sep = ""
	dep_sep = " "
	if ( division == "" || division == "" )
		dep_sep = ""

	# SKIP 0 cost SPARE
	# test grid incase Master not set in Directory ie. extn not in T21E csv
	if ( servtype != 10 && outfileext == ".other" ) {
		if ( (surname == ".SPARE" || grid == "SPARE") && tot_cost == 0 )
			return
	}

	# do output
	if ( outfmt == 2 ) {	# output format 2, (no division)
		outstr1 = sprintf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%.2f", sid, delim, firstname, name_sep, surname, delim, department, delim, location, delim, cc_pc, delim, syyyymmdd, delim, eyyyymmdd, delim, servtype, delim, SERVTYPE_to_GLcode_arr[servtype], delim, SERVTYPE_to_SERVTYPEdesc_arr[servtype], delim, mround2(tot_cost))
	}
	else {			# output format 1, (default)
		outstr1 = sprintf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%.2f", sid, delim, firstname, name_sep, surname, delim, department, dep_sep, division, delim, location, delim, cc_pc, delim, syyyymmdd, delim, eyyyymmdd, delim, servtype, delim, SERVTYPE_to_GLcode_arr[servtype], delim, SERVTYPE_to_SERVTYPEdesc_arr[servtype], delim, mround2(tot_cost))
	}

	print outstr1 >> outfile
	close(outfile)
	#print "BB1 " outfile ": " outstr1

}


function spit_newbb1_inv_totGST()
{
	#print "spit_newbb1_inv_totGST()"
	# sid totals are ex gst
	outstr1 = sprintf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%.2f", "Total", newinv_delim, "Ex. GST", "", "", newinv_delim, grid_to_grname3[newlast_inv_grid], "", "", newinv_delim, "", newinv_delim, newlast_inv_cc_pc, newinv_delim, syyyymmdd, newinv_delim, eyyyymmdd, newinv_delim, last_SERVTYPE, newinv_delim, SERVTYPE_to_GLcode_arr[last_SERVTYPE], newinv_delim,SERVTYPE_to_tSERVTYPEdesc_arr[last_SERVTYPE], newinv_delim, mround2(newinv_tot_cost_exgst))

 print outstr1 >> newinv_outfile

	newinv_tot_cost_gst = sprintf("%.2f", mround2(10 * newinv_tot_cost_exgst / 100))

	newinv_tot_cost_incgst = sprintf("%.2f", newinv_tot_cost_exgst + newinv_tot_cost_gst)
	outstr1 = sprintf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%.2f", "Total", newinv_delim, "Inc. GST", "", "", newinv_delim, grid_to_grname3[newlast_inv_grid], "", "", newinv_delim, "", newinv_delim, newlast_inv_cc_pc, newinv_delim, syyyymmdd, newinv_delim, eyyyymmdd, newinv_delim, last_SERVTYPE, newinv_delim, SERVTYPE_to_GLcode_arr[last_SERVTYPE], newinv_delim,SERVTYPE_to_SERVTYPEdesc_arr[last_SERVTYPE], newinv_delim, mround2(newinv_tot_cost_incgst))
	print outstr1 >> newinv_outfile
	newlast_inv_cc_pc = ""
	newlast_inv_grid = ""
	newinv_tot_cost_exgst = 0
}


##############################################################


function spit_dircsv()
{
	#print "     do spit_dircsv() out_recordno=[" out_recordno "]"

	# from ecu
	#printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",
	#	 recordno, t21surname, t21firstname, out_extn, out_t21mobile,
	#	 t21mobile_carrier, out_centre, out_project, out_ccpdesc,
	#	 t21title, t21site, t21location) >>dircsvfile_fname

	# from wb3
	ostr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",		 out_recordno, out_name, out_extn,
#
		out_sid,
		out_servtype, out_servtypedesc,
		out_sundry, out_supplier, out_invoiceno,
		out_rptgrpid,
#
		out_shipto, out_customerdesc,
		out_group, out_groupdesc,
		out_centre, out_centredesc,
		out_project, out_projectdesc,
#
		out_section, out_location, out_title,
#
		out_tec,
		out_mobile,
		out_fax,
		out_voicemail,
		out_email)

#		out_replinkflag)

	#print "dircsvrec=[" ostr "]"
	print ostr >>dircsvfile_fname

}


function split_dir_aline(aline)
{
	if ( aline == "" ) {
		#print "WARNING: split_dir_aline BLANK aline DISTID = " DISTID
		tmprecno = 0
		t21surname = ""
		t21firstname = ""
		extn = ""
		t21site = ""
		t21section = ""
		t21divsion = ""
		t21title = ""
		t21location = ""
		extgroupunique = ""
		email = ""
		tec = ""
		t21mobile = ""
		fax = ""
		voicemail = ""
		return
	}

	split(aline, a_arr, ",")
	##printf("tdiraline=%s\n", aline)
	#printf("a_arr 1=%s 2=%s 3=%s 4=%s 5=%s\n", a_arr[1], a_arr[2], a_arr[3], a_arr[4], a_arr[5])
	f = 0
	tmprecono = trim(a_arr[++f])
	t21surname = trim(a_arr[++f])
	t21firstname = trim(a_arr[++f])
	extn = trim(a_arr[++f])
	t21site = trim(a_arr[++f])
	t21section = trim(a_arr[++f])
	t21division = trim(a_arr[++f])
	t21title = trim(a_arr[++f])
	t21location = trim(a_arr[++f])
	extgroupunique = trim(a_arr[++f])
	email = trim(a_arr[++f])
	tec = trim(a_arr[++f])
	t21mobile = trim(a_arr[++f])
	fax = trim(a_arr[++f])
	voicemail = trim(a_arr[++f])
}


function set_dircsv_recinfo(recno, extngroupid)
{
	out_recordno = recno

	# bilmax21 centre / project
	bmcentre = grid_to_centre_arr[extngroupid]
	bmproject = grid_to_project_arr[extngroupid]

	# VT group
	out_group = grid_to_parentgroupid_arr[bmcentre]
	out_groupdesc = grid_to_grname3_arr[out_group]

	# VT centre
	out_project = bmproject
        out_projectdesc = CENTREPROJECT_to_PROJECTdesc3_arr[bmcentre "|" bmproject]

	# rjs9
	#out_ccpdesc = CENTREPROJECT_to_PROJECTdesc_arr[bmcentre "|" bmproject]
	out_ccpdesc = ""
	
	#--- from wb3 -------------

	out_name = t21name
	out_extn = extn

	out_shipto = t21division
	out_customerdesc = ShipTo_to_ShipToDesc_arr[out_shipto]

	# VT department
	#out_centre = parentSID_to_DEPTID_arr[out_shipto "|" extngroupid]
	out_centre = bmcentre
        out_centredesc = CENTRE_to_CENTREdesc_arr[bmcentre]

# why is this in wb3
#	if ( out_shipto == "VRT" )
#		out_servtype = 101043

	out_servtype = servtype
	out_servtypedesc = servtypedesc

	out_sid = extngroupid

	out_sundry = sundry
	out_supplier = supplier
	out_invoiceno = invoiceno

	out_rptgrpid = rptgrpid

	out_section = t21section
	out_title = t21title
	out_location = t21location
	
# why is this in wb3
#	# if not doing TLM - skip TLM dir entries
#	if ( this_divgroup != "TLM" && t21div == "TLM" )
#		continue

	out_tec = tec

	tmp_mobile = t21mobile
	gsub(" ","",tmp_mobile)
	gsub("-","",tmp_mobile)
	if ( length(tmp_mobile) == 10 )
		mobsid = substr(tmp_mobile,1,4) "-" substr(tmp_mobile,5,6)
	else
		mobsid = tmp_mobile
	out_mobile = mobsid

	out_fax = fax
	out_voicemail = voicemail
	out_email = email

#	out_replinkflag = replinkflag

# why ?
	# if not real extn then clear these
	if ( out_extn == 0 ) {
		out_sundry = ""
		out_supplier = ""
		out_invoiceno = ""
		out_rptgrpid = ""
#		out_replinkflag = 0
	}

	#< from wb3 ----------------

}


function do_t21dir_entry(recordno)
{
	print "   do_t21dir_entry() recordno=[" recordno "]"
	aline = recordno_to_aline_arr[recordno]
	#print "        aline = [" aline "]"
	#print "  t21dirreccnt_to_recordno_arr[" reccnt "] = [" t21dirreccnt_to_recordno_arr[reccnt] "]"
	#print "  recordno = [" recordno "]" "  aline = [" aline "]"
	if ( aline == "" ) {
		print "WARNING: skipping BLANK aline DISTID = " DISTID "  egrid=[" egrid "]" "  recordno=[" recordno "]"
		return
	}
	split_dir_aline(aline)

	## skip non master
	#if ( recordno_to_master_arr[recordno] != "Y" ) {
	#	return
	#}

	pgrid = grid_to_parentgroupid_arr[extgroupunique]
	if ( pgrid == "SPARE" || pgrid == "UNPROG" )	# skip
		return

	t21name = t21firstname
	if ( t21name != "" && t21surname != "" )
		t21name = t21name " "
	t21name = t21name t21surname

	if ( extn > 0 ) {
		servtype = 1
		servtypedesc = SERVTYPE_to_SERVTYPEdesc_arr[servtype]
	}
	else {
		servtype = 0
		servtypedesc = "Off Site"
	}

	sundry = t21_sundry
	supplier = t21_supplier
	#invoiceno = sundryEGRID_to_invoiceno_arr[t21_sundry "|" extgroupunique]
	invoiceno = SID_to_invoiceno_arr[extgroupunique] 

	rptgrpid = 10

#	# only have services belonging to distid
#	replinkflag = 1

	set_dircsv_recinfo(recordno, extgroupunique)
	spit_dircsv()
}


function do_cdedir_entry(HIERIDxorder)
{
	print "   do_cdedir_entry() HIERIDxorder=[" HIERIDxorder "]"

	HIERIDx = HIERIDxorder_to_HIERIDx_arr[HIERIDxorder]
	get_HIERIDx_info(HIERIDx, 0)

	# skip if not from cde data
	if ( HIERIDxfrom != "cde" )
		return

	recordno = maxt21dirrecordno + HIERIDxorder

	t21surname = ""
	t21firstname = user

	# if user is blank then use ServiceID Description
	servicedesc = sid_sundry_to_servicedesc_arr[SID "|" SUNDRY]
	#print " sid_sundry_to_servicedesc_arr[" SID "|" SUNDRY "]=[" sid_sundry_to_servicedesc_arr[SID "|" SUNDRY] "]"
	if ( t21firstname == "" )
		t21firstname = servicedesc
	# if still blank use seid desc
	if ( t21firstname == "" )
		t21firstname = SEID_to_SEIDdesc_arr[SEID]
	t21name = t21firstname

	extn = "Service:"

	t21site = ""
	t21section = ""
	t21division = SHIPTO
	t21title = ""
	t21location = location
	extgroupunique = SID

	email = ""
	tec = ""
	t21mobile = ""
	fax = ""
	voicemail = ""

	servtype = SERVTYPE
	servtypedesc = SERVTYPEdesc

	### skip (already got this from the t21 directory)
	## t21 calls and rent rest                     VRT
	#if ( seid == 1043 || seid == 2399 ) {
	# seid now set here in ld_cde()
	if ( SEID == 101043 ) {
		##print "skipping t21 eggrid SID = [" SID "]"
		extn = "Service:-"
		##return
	}

	## mobile             rest                     VRT
	#if ( SEID == 1044 || SEID == 2405 ) {
	if ( servtype == 10 ) {
		### skip cde if mobile in t21 directory
		tmp_mobile = SID
		gsub(" ","",tmp_mobile)
		gsub("-","",tmp_mobile)
		#print "test t21mobile_to_egridextn_arr[" tmp_mobile "] = [" t21mobile_to_egridextn_arr[tmp_mobile] "]"
		if (t21mobile_to_egridextn_arr[tmp_mobile] != "") {
			##print "skipping mobile = [" SID "]"
			extn = "Service:--"
			##return
		}
		t21mobile = SID
	}
	else {
		t21mobile = ""
	}

	if ( servtype == 101047 )
		voicemail = "VOICEMAIL"
	else
		voicemail = ""

	sundry = SUNDRY
	supplier = SUPPLIER
	invoiceno = INVOICENO

	rptgrpid = reportgroupid

#		# only have services belonging to distid
#		replinkflag = 1


	set_dircsv_recinfo(recordno, extgroupunique)
	spit_dircsv()
}


function mk_dircsvfile() {
	printf("mk_dircsvfile() DISTID = %s\n", DISTID)
	system("date")

	dircsvfile_fname = sprintf("topdat/%s/%s/data/direnq/T21E_%s.csv", monthdir, DISTID, DISTID)
	print " dircsvfile_fname=[" dircsvfile_fname "]"
	cmd = sprintf("rm -f \"%s\"", dircsvfile_fname)
	system(cmd)


	#if ( virtual_management_distid_arr[DISTID] != "" ) {
	#	# vmng
	#	## dircsv file is sym link for virtual management lists
	#	#real_distid = virtual_management_distid_arr[DISTID]
	#	#real_dircsvfile_fname = sprintf("../../../%s/data/direnq/T21E_%s.csv", real_distid, real_distid)
	#	#cmd = "ln -s \"" real_dircsvfile_fname "\" \"" dircsvfile_fname "\""
	#	#print "virtual management distid dircsv symlink [" cmd "]"
	#	#system(cmd)
	#	return
	#}

	newdir = sprintf("topdat/%s/%s/data/direnq", monthdir, DISTID)
	cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
	system(cmd)
	add_fname_to_fnamelist(dircsvfile_fname)

	#----------------------------------------------------
	# do csv header

	reccnt = 0
	recordno = t21dirreccnt_to_recordno_arr[reccnt]
	aline = recordno_to_aline_arr[recordno]

	# -------- based on wb3 -------------
	out_recordno = recordno
	out_name = "}1Name/User/Description______________"
	out_extn = "#}eXtension_"

	out_sid = "^1}ServiceID_______________"
	out_servtype = "^0ServiceTypeID"
	out_servtypedesc = "^1}Batch_Type_______________________________"
	out_sundry = "^1}Sundry"
	out_supplier = "^1}Supplier____________"
	out_invoiceno = "^1}InvoiceNo_"
	out_rptgrpid = "^0ReportGroupID"

	out_shipto = "^2}ShipTo"
	out_customerdesc = "^2}Customer_Name______________________"

	out_group = "^0GroupID___"
	out_groupdesc = "^2}Group_____________________________"

	out_centre = "^0#DeptID____"		# VT Dept
	out_centredesc = "^2}Department_________________________"

	out_project = "^0Project___"		# VT Centre
	out_projectdesc = "^2}Centre____"
	#ccpdesc = "^2Centre_Project_Description______________"
	#out_ccpdesc = "^2Description_____________________________"

	out_section = "^3Section_______"
	out_location = "^3Location___________________________"
	out_title ="^3Title___________________________________"

	out_tec = "^4TEC______"
	out_mobile = "^4Mobile______"
	out_fax = "^4#Fax_Number__"
	out_voicemail = "^4Voicemail_"
	out_email = "^4Email_________________________________"

#	out_replinkflag = "^4#RepLinkFlag"
	#< wb3 -------------
	spit_dircsv()
	#--------------------


	#--------------------------------------------------
	# is a PERSONAL virtual distid
	# (Bilmax21 Directory only has personal user entrys)
	if ( distid_to_wbd_type_arr[DISTID] == "PERSONAL" ) { 
		print "   mk_dircsv PERSONAL Vdistid = DISTID"
		split(Vdistid_to_grid_servtype_sidlist_arr[DISTID], grid_servtype_sidlist_arr, "~")
		print "  Vdistid_to_grid_servtype_sidlist_arr[" DISTID "]=[" Vdistid_to_grid_servtype_sidlist_arr[DISTID] "]"
		for ( i in grid_servtype_sidlist_arr ) {
			grid_servtype_sid = grid_servtype_sidlist_arr[i]
			split(grid_servtype_sid, grid_servtype_sid_arr, "|")
			grid = grid_servtype_sid_arr[1]
			servtype = grid_servtype_sid_arr[2]
			sid = grid_servtype_sid_arr[3]

			print "   grid=[" grid "]" "  servtype=[" servtype "]" "  sid=[" sid "]"

			#---------------------------
			# do personal t21dir entries
			Vdistid_directoryID = grid_servtype_sid_to_directoryID[grid_servtype_sid]
			recordno = egrid_recordno_arr[j]
			print "        doing personal t21dir for grid=[" grid "]" "  servtype=[" servtype "]" "  sid=[" sid "]" "  Vdistid_directoryID = [" Vdistid_directoryID "]"
			do_t21dir_entry(Vdistid_directoryID)

			#---------------------------
			# do personal mobile cde entries
			if ( servtype == 10 ) {
				print "        doing personal cde for sid=[" sid "]..."
				print "          sid_to_sundrylist_arr[" sid "]=[" sid_to_sundrylist_arr[sid] "]"
				split(sid_to_sundrylist_arr[sid], sid_sundry_arr, "|")
				for ( j in sid_sundry_arr ) {
					sundry = sid_sundry_arr[j]
					HIERIDxorderlist = sid_sundry_to_HIERIDxorderlist_arr[sid "|" sundry]
					print "      sundry=[" sundry "]" "  HIERIDxorderlist=[" HIERIDxorderlist "]"
					split(HIERIDxorderlist, HIERIDxorder_arr, "|")
					for ( k in HIERIDxorder_arr ) {
						HIERIDxorder = HIERIDxorder_arr[k]
						do_cdedir_entry(HIERIDxorder)
					}
				}
			}
		}
		close(dircsvfile_fname)
		return
	}


	#-----------------------------------------------
	# Telmax21 dir entries for configured distid

	print ""
	print "   mk_dircsv CONFIGURED distid=["  DISTID "]"

	# configured distid directory contains all entries
	# belonging to the distribution list 

	split(distid_to_egridlist_arr[DISTID], egrid_arr, "|")
	for ( i in egrid_arr ) {
		egrid = egrid_arr[i]

		#------------------------------------
		# entries from Telmax21 Directory
		print "   doing t21dir for egrid=[" egrid "]"
                print "mkt21dir: egrid_to_recordnolist_arr[" egrid "]=[" egrid_to_recordnolist_arr[egrid] "]"
		split(egrid_to_recordnolist_arr[egrid],egrid_recordno_arr,"|")
		for ( j in egrid_recordno_arr ) {
			recordno = egrid_recordno_arr[j]
			do_t21dir_entry(recordno)
		}

		#------------------------------------
		# entries from cde summary data
		sid = egrid
		print "   doing cde for sid=[" sid "]..."
		print "      sid_to_sundrylist_arr[" sid "]=[" sid_to_sundrylist_arr[sid] "]"
		split(sid_to_sundrylist_arr[sid], sid_sundry_arr, "|")
		for ( j in sid_sundry_arr ) {
			sundry = sid_sundry_arr[j]
			HIERIDxorderlist = sid_sundry_to_HIERIDxorderlist_arr[sid "|" sundry]
			print "      sundry=[" sundry "]" "  HIERIDxorderlist=[" HIERIDxorderlist "]"
			split(HIERIDxorderlist, HIERIDxorder_arr, "|")
			for ( k in HIERIDxorder_arr ) {
				HIERIDxorder = HIERIDxorder_arr[k]
				do_cdedir_entry(HIERIDxorder)
			}
		}
	}

	close(dircsvfile_fname)
}


function ln_userhtml_index()
{
	t21name = distid_to_t21name_arr[DISTID]
	t21userid = distid_to_userid_arr[DISTID]
	t21wbd_method = distid_to_wbd_method_arr[DISTID]
	t21wbd_type = distid_to_wbd_type_arr[DISTID]
	if ( t21userid == "" )
		t21userid = distid_to_directoryID_arr[DISTID]
	userhtml_index_fname = sprintf("topdat/%s Click here to run your reports.html", t21userid)
	cmd = "rm -f " "\"" userhtml_index_fname "\"" "; ln -s ../img/user.html " "\"" userhtml_index_fname "\""
	print "ln_userhtml_index() cmd=[" cmd "]"
	system(cmd)
}

function mk_useridfile()
{
	print "mk_useridfile() t21userid=[" t21userid "]" "  DISTID=[" DISTID "]"
	t21name = distid_to_t21name_arr[DISTID]
	t21userid = distid_to_userid_arr[DISTID]
	t21wbd_method = distid_to_wbd_method_arr[DISTID]
	t21wbd_type = distid_to_wbd_type_arr[DISTID]
	if ( t21userid == "" )
		t21userid = distid_to_directoryID_arr[DISTID]
	usertag = fixforfname(sprintf("%s_%s", t21userid, monthtag))
	printf("usertag = %s\n", usertag)
	userid_fname = sprintf("topdat/%s/%s.txt", monthdir, usertag)

	# generate userid file
	printf("Generate userid (%s) for %s %s\n", userid_fname, t21name, monthdir)

	if ( (getline x < userid_fname) >= 0 ) { # exists (multiple dist lists)
		close(userid_fname)
		printf("\r\n") >>userid_fname
		printf("------\r\n") >>userid_fname
		printf("\r\n") >>userid_fname
	}
	else {					# create it
		printf("\r\n") >userid_fname
	}


	printf("Bilmax21 Web Billing Reports\r\n") >>userid_fname
	printf("For %s", t21name) >>userid_fname
	printf(" (userid: %s)", t21userid) >>userid_fname
	printf(" Period: %s\r\n", exbillperiod) >>userid_fname
	if ( retroBDL == "1" ) {
		printf("\r\n") >>userid_fname
		print "Retrospective Generation/Regeneration of Distribution List" >>userid_fname
	}
	printf("\r\n") >>userid_fname

	printf("Distribution Method: %s\r\n", t21wbd_method) >>userid_fname
	printf("          List Type: %s\r\n", t21wbd_type) >>userid_fname
	printf("\r\n") >>userid_fname
	add_fname_to_fnamelist(userid_fname)

	printf("%s - %s\r\n", DISTIDdesc, DISTID) >>userid_fname
	
	close(userid_fname)
}


function mk_distidfile()
{
	print "mk_distidfile() DISTID=[" DISTID "]"
	idtag = sprintf("%s_%s", DISTID, monthtag)
	#printf("idtag = %s\n", idtag)

	distidfile_dir = sprintf("topdat/%s/%s", monthdir, DISTID)
	distidfile_fname = sprintf("%s/%s.txt", distidfile_dir, idtag)

	cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", distidfile_dir, distidfile_dir)
	system(cmd)

	DISTIDdesc = distid_to_distiddesc_arr[DISTID]

	distid_t21name = distid_to_t21name_arr[DISTID]
	distid_t21userid = distid_to_userid_arr[DISTID]
	if ( distid_t21userid == "" )
		distid_t21userid = distid_to_directoryID_arr[DISTID]
	distid_t21wbd_method = distid_to_wbd_method_arr[DISTID]
	distid_t21wbd_type = distid_to_wbd_type_arr[DISTID]

	# generate ID file
	printf("Generate distidfile (%s) for %s %s\n", distidfile_fname, distid_t21name, monthdir)

	printf("") >distidfile_fname
	printf("%s   (%s)\r\n", distid_t21name, distid_t21userid) >>distidfile_fname
	printf("\r\n") >distidfile_fname
	printf("Welcome to your Bilmax21 reports for %s\r\n", exbillperiod) >>distidfile_fname
	printf("\r\n") >>distidfile_fname
	printf("%s - %s\r\n", DISTIDdesc, DISTID) >>distidfile_fname
	printf("\r\n") >>distidfile_fname

	printf("Distribution Method: %s\r\n", distid_t21wbd_method) >>distidfile_fname
	printf("          List Type: %s\r\n", distid_t21wbd_type) >>distidfile_fname
	printf("\r\n") >>distidfile_fname

	printf("The Cost Centres/Services included in your reports for this month are as follows,\r\n") >>distidfile_fname
	printf("\r\n") >>distidfile_fname

	# do all grids/sids in distlist
	for ( dgstr in distgrid_arr ) {
		#print "dgstr=[" dgstr "]"
		split(dgstr, dgstr_arr, "|")
		if ( DISTID != dgstr_arr[1] )
			continue
		grid = dgstr_arr[2]
		distservtype = dgstr_arr[3]
		distsid = dgstr_arr[4]

		print "mk_distidfile DISTID = " DISTID " (" DISTIDdesc ")" "  grid = " grid  "  distservtype=" distservtype "  distsid=" distsid

		printf("\r\n") >>distidfile_fname

		printf("%-8.8s : %s - %s\r\n", grid, grid_to_grname2_arr[grid], grid_to_grname3_arr[grid]) >>distidfile_fname

		if ( distservtype != "" ) { #only this servtype
			#print "DEBUGMOB: 3a DISTID=" DISTID "  distservtype=" distservtype
			printf("\tBatch Type %s\t - %s\r\n", distservtype, SERVTYPE_to_SERVTYPEdesc_arr[distservtype]) >>distidfile_fname
		}
		if ( distsid != "" ) {	# only this sid
			#print "DEBUGMOB: 3b DISTID=" DISTID "  distsid=" distsid
			printf("\t\tServiceID \t - %s\r\n", distsid) >>distidfile_fname
		}
		printf("\r\n") >>distidfile_fname
	}

	add_fname_to_fnamelist(distidfile_fname)
	close(distidfile_fname)
}


function distidfile_totals()
{
	#print "distidfile_totals()" " DISTID=[" DISTID "]" " SERVTYPE=[" SERVTYPE "]" " last_SERVTYPE=[" last_SERVTYPE "]" " CENTRE=[" CENTRE "]" " PROJECT=[" PROJECT "]"

	# create distid if not already created
	if ( distidfile_created[DISTID] != 1 ) {
		mk_distidfile()
		distidfile_created[DISTID] = 1
	}

	idtag = fixforfname(sprintf("%s_%s", DISTID, monthtag))

	# vmng
	svdistid = DISTID

	distidfile_fname = sprintf("topdat/%s/%s/%s.txt", monthdir, DISTID, idtag)

	if ( virtual_management_distid_arr[DISTID] != "" )
		DISTID = virtual_management_distid_arr[DISTID]
	# vmng
	DISTID = svdistid

	tot_cost = totalcost_arr["TOTAL" "|" SERVTYPE "|" SHIPTO "|" GROUP "|" CENTRE "|" PROJECT "|" SID "|" "All" "|" "All" "|" "All" "|" "All"]

	if ( SERVTYPE == 10 ) {	# mobile only

		if ( done_distidfile_servtype_arr[DISTID "|" SERVTYPE] == "" ) {
			printf("\r\n") >>distidfile_fname
			printf("-----------------------------------------------------\r\n") >>distidfile_fname
			printf("\r\n") >>distidfile_fname
			done_distidfile_servtype_arr[DISTID "|" SERVTYPE] = 1
		}

		if ( SERVTYPE != last_SERVTYPE || CENTRE != last_CENTRE || PROJECT != last_PROJECT ) {
			printf("\r\n") >>distidfile_fname
			CENTREdesc = CENTRE_to_CENTREdesc_arr[CENTRE]
			CCP = grid_to_ccp_arr[EGRID]
			printf("%s - GroupID: %-8.8s\r\n", SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE], EGRID) >>distidfile_fname
			printf("    %s - %s\r\n", CENTREdesc, CCP) >>distidfile_fname
			printf("\r\n") >>distidfile_fname
		}

		printf("        %-30.30s %-15.15s %5.2f\r\n", t21name, SID, mround2(tot_cost)) >>distidfile_fname

	}

	close(distidfile_fname)
}


##############################################################

function gen_top_csv()
{
	# generate top csv file
	top_fname = sprintf("topdat/%s/data/top_%s.csv", monthdir, CENTRE)
	printf("%s,%s,%s,%s,%s,%s\n", "Monthly Service Reports", exbillperiod, "SDATE", "EDATE", CENTREdesc, CENTRE) >top_fname
	close(top_fname)
}


function cpy_img(x)
{
	# test if image already done
	imgmarkerfile = sprintf("topdat/%s/%s/index.html", monthdir, CENTRE)
	printf("cpy_img(%d): testing for %s\n", x, imgmarkerfile)
	if ( (getline xs < imgmarkerfile) < 0 ) {
		printf("%d: Copy image files for %s/%s\n", x, monthdir, CENTRE)
		# copy image folder dirs and files
		cmd = sprintf("cpyimg topdat/%s/%s 2>&1", monthdir, CENTRE)
		system(cmd)
	}
	else {
		printf("%d: Image ALREADY exists for %s/%s\n", x, monthdir, CENTRE)
	}
	close(imgmarkerfile)
}



##############################################################


function detail_fnames_to_fnamelist(centreproject)
{
	for ( cpstr in fname_to_SERVTYPECENTREPROJECT_arr ) {
		if ( fname_to_SERVTYPECENTREPROJECT_arr[cpstr] != centreproject )
			continue
		print "detail_fnames: SERVTYPE=" SERVTYPE "  CENTRE=" CENTRE "  PROJECT=" PROJECT "  cpstr=" cpstr
		add_fname_to_fnamelist(cpstr)
	}
}

function add_fname_to_fnamelist(fname)
{
	# vmng
	fdistid = DISTID
	if ( virtual_management_distid_arr[DISTID] != "" )
		fdistid = virtual_management_distid_arr[DISTID]

	fnamelist_fname = sprintf("topdat/%s/%s/fnamelist.csv",monthdir,fdistid)
	fnamelist_fname = sprintf("topdat/%s/%s/fnamelist.csv",monthdir,fdistid)
	print fname >> fnamelist_fname
	close(fnamelist_fname)
}

#----------------------------------------------------------
# highest usage...

function do_highest_usage(distid,servtype,shipto,group,centre,project,egrid,sid)
{
	# fixed highest users
	if ( servtype == "1" ) {
		# rjs9
		supplier = "Fixed Line"
		supplier = t21_supplier " " supplier
		
		# rjs9 / rjs12 (removed)
		#group = grid_to_parentgroupid_arr[CENTRE]
		Wtag = "TOTAL" "|" servtype "|" shipto "|" group "|" centre "|" project "|" sid "|" supplier "|" "All" "|" "All"
		chg_cat = "All"

		chg_count = totalcount_arr[Wtag "|"  chg_cat]
		chg_duration = totalduration_arr[Wtag "|" chg_cat]
		chg_cost = totalcost_arr[Wtag "|" chg_cat]

		print "do_highest_usage1: Wtag=" Wtag "  chg_cat=" chg_cat
		print "        chg_count=[" chg_count "]"
		print "     chg_duration=[" chg_duration "]"
		print "         chg_cost=[" chg_cost "]"

		htag = distid "|" servtype
		hobj = centre "|" project "|" sid 
		hobj = hobj "|" chg_count "|" chg_duration "|" chg_cost 

		# by cost
		hval = chg_cost
		hi_in(htag, 20, hi_sid_by_cost_arr, hobj, hval, 0)

		# highest premium service users
		Stag = servtype "|" centre "|" project "|" sid
		if ( sid_premserv_count_arr[Stag] != "" ) {
			chg_count = sid_premserv_count_arr[Stag]
			chg_duration = sid_premserv_duration_arr[Stag]
			chg_cost = sid_premserv_cost_arr[Stag]
			htag = distid "|" servtype
			hobj = centre "|" project "|" sid
			hobj = hobj "|" chg_count "|" chg_duration "|" chg_cost 
			hval = chg_cost
			hi_in(htag, 20, hi_premserv_users_by_cost_arr, hobj, hval, 0)
		}
	}

	# mobile highest users
	if ( servtype == "10" ) {
		supplier = "All"

		# rjs9 rjs12 (removed)
		#group = grid_to_parentgroupid_arr[CENTRE]
		Wtag = "TOTAL" "|" servtype "|" shipto "|" group "|" centre "|" project "|" sid "|" supplier "|" "All" "|" "All"
		chg_cat = "All"
		
		chg_count = totalcount_arr[Wtag "|" "All_MOBILE_CDR"]
		chg_duration = totalduration_arr[Wtag "|" "All_MOBILE_CDR"]
		## r10 - mobile has NO CHARGE_DESC CALL totals
		chg_cost = totalcost_arr[Wtag "|" chg_cat]
		#chg_cost = totalcost_arr[Wtag "|" "All_MOBILE_CDR"]

		print "do_highest_usage10: Wtag=" Wtag "  chg_cat=" chg_cat
		print "        chg_count=[" chg_count "]"
		print "     chg_duration=[" chg_duration "]"
		print "         chg_cost=[" chg_cost "]"

		htag = distid "|" servtype
		hobj = centre "|" project "|" sid 
		hobj = hobj "|" chg_count "|" chg_duration "|" chg_cost 

		# by cost
		hval = chg_cost
		hi_in(htag, 20, hi_sid_by_cost_arr, hobj, hval, 0)

		###############
		# SMS
		supplier = "All"
		# rjs9 / rjs12 (removed)
		#group = grid_to_parentgroupid_arr[CENTRE]
		Wtag = "TOTAL" "|" servtype "|" shipto "|" group "|" centre "|" project "|" sid "|" supplier "|" "All" "|" "All"
		chg_cat = "Mobile Messaging"

		chg_count = totalcount_arr[Wtag "|" chg_cat]
		chg_duration = totalduration_arr[Wtag "|" chg_cat]
		chg_cost = totalcost_arr[Wtag "|" chg_cat]

		print "do_highest_usageSMS: Wtag=" Wtag "  chg_cat=" chg_cat
		print "        chg_count=[" chg_count "]"
		print "     chg_duration=[" chg_duration "]"
		print "         chg_cost=[" chg_cost "]"

		htag = distid "|" servtype
		hobj = centre "|" project "|" sid
		hobj = hobj "|" chg_count "|" chg_duration "|" chg_cost 

		# by mobile messaging by count
		hval = chg_count
		hi_in(htag, 20, hi_sid_mobile_messaging_by_count_arr, hobj, hval, 0)

		# by mobile messaging by cost
		hval = chg_cost
		hi_in(htag, 20, hi_sid_mobile_messaging_by_cost_arr, hobj, hval, 0)

		# global roaming users
		hobj = centre "|" project "|" sid
		chg_cost = gloroam_users_cost_arr[hobj]
		if ( chg_cost != "" ) {
			chg_count = gloroam_users_count_arr[hobj]
			chg_duration = gloroam_users_duration_arr[hobj]
			hobj = hobj "|" chg_count "|" chg_duration "|" chg_cost 
			#print " gloroam htag=[" htag "]" "  hobj=[" hobj "]" "  chg_cost=[" chg_cost "]"
			hval = chg_cost
			hi_in(htag, 2000, hi_gloroam_users_by_cost_arr, hobj, hval, 0)
		}

		# internet users
		hobj = centre "|" project "|" sid
		if ( internet_users_arr[hobj] == 1 ) {
			kbytes = internet_users_kbytes_arr[hobj]
			chg_duration = 0
			chg_cost = internet_users_cost_arr[hobj]
			hobj = hobj "|" kbytes "|" chg_duration "|" chg_cost 
			print " internet htag=[" htag "]" "  hobj=[" hobj "]" "  chg_cost=[" chg_cost "]"
			hval = chg_cost
			hi_in(htag, 20000, hi_internet_users_by_cost_arr, hobj, hval, 1)
		}

		# highest premium service users
		Stag = servtype "|" centre "|" project "|" sid
		if ( sid_premserv_count_arr[Stag] != "" ) {
			chg_count = sid_premserv_count_arr[Stag]
			chg_duration = sid_premserv_duration_arr[Stag]
			chg_cost = sid_premserv_cost_arr[Stag]
			htag = distid "|" servtype
			hobj = centre "|" project "|" sid
			hobj = hobj "|" chg_count "|" chg_duration "|" chg_cost 
			hval = chg_cost
			hi_in(htag, 20, hi_premserv_users_by_cost_arr, hobj, hval, 0)
		}
	}
}


		# screen out 0 values
	#print "hi_in() id=" id "  N_HIGH=" N_HIGH "  obj=" obj "  val=" val
		#print "        hi_arr[" id "|" i "]=" hi_arr[id "|" i]
	#print "        NEW i+1=" i+1
	# move i+1 --> i+2...
	# insert entry
function hi_in(id, N_HIGH, hi_arr, obj, val, include0vals)
{
	if (include0vals != 1 && val == 0)
		return
	j = N_HIGH - 1
	for ( i = j; i >= 0 ; --i ) {
		if ( hi_arr[id "|" i] == "" )
			continue
		split(hi_arr[id "|" i], hi_obj_val, "|")
		if ( val <= hi_obj_val[1] )
			break
	}
	if ( i == j )
		return
	k = i + 1
	while ( j > k ) {
		t = id "|" j
		hi_arr[t] = hi_arr[id "|" --j]
	}
	hi_arr[id "|" k] = val "|" obj
}


#----------------------------------------------------------
#


function add_totals(Xdistid, Xservtype, Xshipto, Xgroup, Xcentre, Xproject, Xsid, Xsupplier, Xinvoiceno, Xsundry, Xrectype, Xchg_cat, chg_count, chg_duration, chg_cost)
{
	##if ( Xservtype == "10" && Xrectype != "T_CHARGE_SUM" ) {
	#if ( Xservtype == "10" ) {
	#if ( Xservtype == "10" && Xsid == "0409-960966" ) {
	if ( Xservtype == "1" && Xrectype != "T_CHARGE_SUM" ) {
		print "add_totals:"
		print "      Xdistid=[" Xdistid "]"
		print "    Xservtype=[" Xservtype "]"
		print "      Xshipto=[" Xshipto "]"
		print "       Xgroup=[" Xgroup "]"
		print "      Xcentre=[" Xcentre "]"
		print "     Xproject=[" Xproject "]"
		print "         Xsid=[" Xsid "]"
		print "    Xsupplier=[" Xsupplier "]"
		print "   Xinvoiceno=[" Xinvoiceno "]"
		print "      Xsundry=[" Xsundry "]"
		print "     Xrectype=[" Xrectype "]"
		print "     Xchg_cat=[" Xchg_cat "]"
	}

	# don't add mobile CDR to All charge category totals
	# because the total call cost comes from the CHARGE_DESC CALL rec
	# (which contains any discounts)
	# as well as CHARGE_DESC_INFO (which duplicates the costs)
	# also cde totals
	if ( Xservtype == "10" ) {
		# All totals for tbs mobiles
		if ( Xrectype == "T_CHARGE_SUM" ) {
			XAll_chg_cat = "Allcde"
		}
		else {
			# All total for mobile charge desc 
			if ( Xrectype == "MOBILE_CHARGE_DESC" )
				XAll_chg_cat = "All"
			else
				XAll_chg_cat = "All_" Xrectype
		}
	}
	else {
		XAll_chg_cat = "All"
	}

	# ignore m11cdr detail for totals, (use cde)
	if ( substr(Xrectype,1,7) == "M11CDR_" ) {
			XAll_chg_cat = "AllM11CDR"
	}

	##if ( Xservtype == "10" && Xrectype != "T_CHARGE_SUM" ) {
	#if ( Xservtype == "10" ) {
	#if ( Xservtype == "10" && Xsid == "0409-960966" ) {
	if ( Xservtype == "1" && Xrectype != "T_CHARGE_SUM" ) {
		print "     XAll_chg_cat=[" XAll_chg_cat "]"
		print "        chg_count=[" chg_count "]"
		print "     chg_duration=[" chg_duration "]"
		print "         chg_cost=[" chg_cost "]"
	}

        doAllsid = 1
        # skip accountcode sid individualextn totals when doing All_sid tots
        if ( Xservtype == 5 ) {
                split(Xsid,xsid_arr,":")
                if ( xsid_arr[2] != "All" )
                        doAllsid = 0
        }

        if ( doAllsid == 1 ) {
                # total for ALLservtype/Allshipto/Allgroup/Allcentre/Allproject/Allsid
                add_tots(Xdistid, "All", "All", "All", "All", "All", "All", Xsupplier, Xinvoiceno, Xsundry, Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)

                # total for ALLservtype/Xshipto/Allgroup/Allcentre/Allproject/Allsid
                add_tots(Xdistid, "All", Xshipto, "All", "All", "All", "All", Xsupplier, Xinvoiceno, Xsundry, Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)

                # total for ALLservtype/Xshipto/Xgroup/Allcentre/Allproject/Allsid
                add_tots(Xdistid, "All", Xshipto, Xgroup, "All", "All", "All", Xsupplier, Xinvoiceno, Xsundry,  Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)

                # total for servtype/Xshipto/Allgroup/Allcentre/Allproject/Allsid
                add_tots(Xdistid, Xservtype, Xshipto, "All", "All", "All", "All", Xsupplier, Xinvoiceno, Xsundry, Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)
                # total for servtype/Xshipto/Xgroup/Allcentre/Allproject/Allsid
                add_tots(Xdistid, Xservtype, Xshipto, Xgroup, "All", "All", "All", Xsupplier, Xinvoiceno, Xsundry, Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)
        }

	# rjs
	## grand totals and centre totals (from detail data loading only)
	#if ( substr(Xdistid,1,5) == "TOTAL" ) {
                if ( doAllsid == 1 ) {
                        #total for Allservtype/Xshipto/Xgroup/Xcentre/Allproject/Allsid
                        add_tots(Xdistid, "All", Xshipto, Xgroup, Xcentre, "All", "All", Xsupplier, Xinvoiceno, Xsundry, Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)

                        # total for Allservtype/Xshipto/Xgroup/Xcentre/Xproject/Allsid
                        add_tots(Xdistid, "All", Xshipto, Xgroup, Xcentre, Xproject, "All", Xsupplier, Xinvoiceno, Xsundry, Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)

                        # total for Xservtype/Xshipto/Xgroup/Xcentre/Allproject/Allsid
                        add_tots(Xdistid, Xservtype, Xshipto, Xgroup, Xcentre, "All", "All", Xsupplier, Xinvoiceno, Xsundry, Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)

                        # total for Xservtype/Xshipto/Xgroup/Xcentre/Xproject/Allsid
                        add_tots(Xdistid, Xservtype, Xshipto, Xgroup, Xcentre, Xproject, "All", Xsupplier, Xinvoiceno, Xsundry, Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)
                }

		# total for Xservtype/Xshipto/Xgroup/Xcentre/Xproject/Xsid
		add_tots(Xdistid, Xservtype, Xshipto, Xgroup, Xcentre, Xproject, Xsid, Xsupplier, Xinvoiceno, Xsundry, Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)
	#}

	# rjs
	if ( substr(Xdistid,1,5) == "TOTAL" && Xdistid != "TOTALTBS" ) {
		nstag = "TOTAL" "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject
		if ( nsids_arr[nstag "|" Xsid] == "" ) {
			if ((Xservtype == 1 && chg_count > 0) || Xservtype != 1) {
				nsids_arr[nstag "|" Xsid] = "1"
				nsids_arr[nstag] += 1
				nsids_arr["TOTAL" "|" Xservtype "|" Xshipto "|" Xgroup] += 1
				nsids_arr["TOTAL" "|" Xservtype "|" Xshipto] += 1
				nsids_arr["TOTAL" "|" Xservtype] += 1
			}
		}
	}
}


function add_tots(Xdistid, Xservtype, Xshipto, Xgroup, Xcentre, Xproject, Xsid, Xsupplier, Xinvoiceno, Xsundry, Xchg_cat, XAll_chg_cat, chg_count, chg_duration, chg_cost)
{

	if ( Xservtype != "All" ) {
		# chg cat totals are for the DISTID file pie chart
		# supplier / invoiceno / sundry / charge category
		Xtag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" Xsupplier "|" Xinvoiceno "|" Xsundry "|" Xchg_cat
		#print "add_tots - Xtag=[" Xtag "] addcost = " chg_cost
		totalcount_arr[Xtag] += chg_count
		totalduration_arr[Xtag] += chg_duration
		totalcost_arr[Xtag] += chg_cost

		# supplier / invoiceno / sundry / All charge category
		Xtag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" Xsupplier "|" Xinvoiceno "|" Xsundry "|" XAll_chg_cat
		#print "add_tots - Xtag=[" Xtag "] addcost = " chg_cost
		totalcount_arr[Xtag] += chg_count
		totalduration_arr[Xtag] += chg_duration
		totalcost_arr[Xtag] += chg_cost

		# supplier / invoice / ALL_sundry / All charge category
		Xtag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" Xsupplier "|" Xinvoiceno "|" "All" "|" XAll_chg_cat
		#print "add_tots - Xtag=[" Xtag "] addcost = " chg_cost
		totalcount_arr[Xtag] += chg_count
		totalduration_arr[Xtag] += chg_duration
		totalcost_arr[Xtag] += chg_cost

		# supplier / ALL_invoice / ALL_sundry / All charge category
		Xtag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" Xsupplier "|" "All" "|" "All" "|" XAll_chg_cat
		#print "add_tots - Xtag=[" Xtag "] addcost = " chg_cost
		totalcount_arr[Xtag] += chg_count
		totalduration_arr[Xtag] += chg_duration
		totalcost_arr[Xtag] += chg_cost
	}

	# for SERVTYPE file project / sid totals / AVG
	#if ( Xservtype != "All" && Xcentre != "All" && Xsid  != "All" ) {
	if ( Xservtype != "All" ) {
		# All_supplier / ALL_invoice / ALL_sundry / charge category
		Xtag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" "All" "|" "All" "|" "All" "|" Xchg_cat
		#print "add_tots - Xtag=[" Xtag "] addcost = " chg_cost
		totalcount_arr[Xtag] += chg_count
		totalduration_arr[Xtag] += chg_duration
		totalcost_arr[Xtag] += chg_cost

		# All_supplier / ALL_invoice / ALL_sundry / All charge category
		Xtag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" "All" "|" "All" "|" "All" "|" XAll_chg_cat
		#print "add_tots - Xtag=[" Xtag "] addcost = " chg_cost
		totalcount_arr[Xtag] += chg_count
		totalduration_arr[Xtag] += chg_duration
		totalcost_arr[Xtag] += chg_cost
	}

	# for DISTID file All totals
	if ( Xservtype == "All" && Xsid  == "All" ) {
		# All_supplier / All_invoiceno / All_sundry / charge category
		Xtag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" "All" "|" "All" "|" "All" "|" XAll_chg_cat
		#print "add_tots - Xtag=[" Xtag "] addcost = " chg_cost
		totalcount_arr[Xtag] += chg_count
		totalduration_arr[Xtag] += chg_duration
		totalcost_arr[Xtag] += chg_cost

		# All_supplier / All_invoiceno / All_sundry / All charge category
		Xtag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" "All" "|" "All" "|" "All" "|" Xchg_cat
		#print "add_tots - Xtag=[" Xtag "] addcost = " chg_cost
		totalcount_arr[Xtag] += chg_count
		totalduration_arr[Xtag] += chg_duration
		totalcost_arr[Xtag] += chg_cost
	}
}


#--------------------

function print_totals(Xhieridxfrom, Xservtype, Xshipto, Xgroup,  Xcentre, Xproject, Xsid)
{
	# total for centre/project/servtype/sid
	# rjs9 / rjs12 (removed)
	#if ( Xcentre == "All" )
	#	Xgroup = "All"
	#else
	#	Xgroup = grid_to_parentgroupid_arr[Xcentre]

	if ( Xhieridxfrom == "cde" )
		Xtag = "TOTALTBS" "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" "All" "|" "All" "|" "All" "|" "All"
	else
		Xtag = "TOTAL" "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" "All" "|" "All" "|" "All" "|" "All"
	print "print_totals() - >>>> totalcost_arr[" Xtag "] = " totalcost_arr[Xtag]
}


#---------------------

function add_distid_totals()
{
	#print "add_distid_totals HIERIDx = [" HIERIDx "]"

	split(HIERIDx, HIERIDx_arr, "|")
	f = 0
	Xservtype = HIERIDx_arr[++f]
	Xshipto = HIERIDx_arr[++f]
	Xgroup = HIERIDx_arr[++f]
	Xcentre = HIERIDx_arr[++f]
	Xproject = HIERIDx_arr[++f]
	Xsupplier = HIERIDx_arr[++f]
	Xinvoiceno = HIERIDx_arr[++f]
	Xsundry = HIERIDx_arr[++f]
	Xsid = HIERIDx_arr[++f]
	Xrectype = HIERIDx_arr[++f]
	Xchg_cat = HIERIDx_arr[++f]
	Xhieridxfrom = HIERIDx_arr[++f]

	# rjs12
	#Xgroup = grid_to_parentgroupid_arr[Xcentre]

	#if ( Xhieridxfrom != "cde" )	# press cde enties only
	#	return
	## don't dobleup mobile sid totals (use detail tots to get chgcat)
	#if ( Xservtype == 10 && Xhieridxfrom == "cde" )
	#	return

	if ( Xhieridxfrom == "cde" ) 
		fromtotal = "TOTALTBS"
	else
		fromtotal = "TOTAL"

	Xtag = fromtotal "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject "|" Xsid "|" Xsupplier "|" Xinvoiceno "|" Xsundry "|" Xchg_cat

	chg_count = totalcount_arr[Xtag]
	chg_duration = totalduration_arr[Xtag]
	chg_cost = totalcost_arr[Xtag]

	print "add_distid_totals totalcost_arr[" Xtag "] = [" totalcost_arr[Xtag] "]"
	# rjs9 centre|project has multiple egrids
	#Xegrid = CENTREPROJECT_to_EGRID_arr[Xcentre "|" Xproject]
	Xegrid = CENTREPROJECTSID_to_EGRID_arr[Xcentre "|" Xproject "|" Xsid]
	#print "     Xegrid = [" Xegrid "]"

	if ( retroBDL != "1" ) {
	    if ( Xservtype == 1 || Xservtype == 10 ) {
		# do totals for virtual distids
		Stag = Xegrid "|" Xservtype "|" Xsid
		Vdistid = personal_egrid_servtype_SID_to_distid_arr[Stag]

		#print "add_distid_totals test Vdistid - personal_egrid_servtype_SID_to_distid_arr[" Stag "] = [" personal_egrid_servtype_SID_to_distid_arr[Stag] "]"
		if ( Vdistid != "" ) {
			#print "add_distid_totals PERSONAL Stag=[" Stag "]"
			Xdistid = "TOTAL_" Vdistid
			add_totals(Xdistid, Xservtype, Xshipto, Xgroup, Xcentre, Xproject, Xsid, Xsupplier, Xinvoiceno, Xsundry, Xrectype, Xchg_cat, chg_count, chg_duration, chg_cost)
		}
	    }
	}

	#print "add_distid_totals Xservtype = [" Xservtype "]"

	# do totals for each configured distid that has this egrid
	distidlist = egrid_distidlist_arr[Xegrid]
	#print "add_distid_totals distidlist=[" distidlist "]"
	split(distidlist, distid_arr, "|")
	for ( i in distid_arr ) {
		Xdistid = distid_arr[i]
		add_totals(Xdistid, Xservtype, Xgroup, Xcentre, Xproject, Xsid, Xsupplier, Xinvoiceno, Xsundry, Xrectype, Xchg_cat, chg_count, chg_duration, chg_cost)
		# add nsids for distid
		if ( Xservtype == 1 || Xservtype == 2 || Xservtype == 10 ) {
		    nsdistidtag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject

		    if ( nsids_arr[nsdistidtag "|" "DONE" ] != "1" ) {
			nsids_arr[nsdistidtag "|" "DONE" ] = "1"

			# get project sid count for servicetype
			nstag = "TOTAL" "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" Xproject
			nsids = nsids_arr[nstag]
			#print "nsids: get project nsids_arr[" nstag "]=[" nsids_arr[nstag] "]"

			# set distid/shipto sid count for servicetype 
			nstag = Xdistid "|" Xservtype "|" Xshipto "|" "All" "|" "All" "|" "All"
			nsids_arr[nstag] += nsids
			#print "nsids: set centre nsids_arr[" nstag "]=[" nsids_arr[nstag] "]"

			# set distid/group sid count for servicetype 
			nstag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" "All" "|" "All"
			nsids_arr[nstag] += nsids
			#print "nsids: set centre nsids_arr[" nstag "]=[" nsids_arr[nstag] "]"

			# set distid/centre sid count for servicetype 
			nstag = Xdistid "|" Xservtype "|" Xshipto "|" Xgroup "|" Xcentre "|" "All"
			nsids_arr[nstag] += nsids
			#print "nsids: set centre nsids_arr[" nstag "]=[" nsids_arr[nstag] "]"

			# set distid sids for servicetype
			nstag = Xdistid "|" Xservtype "|" Xshipto "|" "All" "|" "All" "|" "All"
			nsids_arr[nstag] += nsids
			#print "nsids: set distid nsids_arr[" nstag "]=[" nsids_arr[nstag] "]"
		    }
		}
	}
}


#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------



function handle_DISTID_datafile()
{
	print "handle_DISTID_datafile() DISTID=" DISTID "  HIERIDx=" HIERIDx

	DISTID_fname = sprintf("topdat/%s/%s/data/distid/distid_%s.csv", monthdir, DISTID, DISTID)

	#if ( REC_TYPE == "MOBILE_CHARGE_DESC" ) {
	#	if ( CHG_CAT != "Mobile Other" && CHG_CAT != "Mobile Misc" ) {
	#		print "    CHG_CAT skipping rec_type=[" REC_TYPE "]" "  CHG_CAT=[" CHG_CAT "]"
	#		return
	#	}
	#}

	if ( REC_TYPE == "MOBILE_CHARGE_DESC_INFO" ) {
		#if ( CHG_CAT != "Mobile Equipment" && CHG_CAT != "Mobile Adjustment" && CHG_CAT != "Mobile Recurring" ) {
			print "    CHG_CAT skipping rec_type=[" REC_TYPE "]" "  CHG_CAT=[" CHG_CAT "]"
			return
		#}
	}

	if ( done_DISTID_fname_arr[DISTID_fname] == "" ) {
		printf(">>>          DISTID_fname = %s\n", DISTID_fname)
		if ( (getline x < DISTID_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/%s/data/distid", monthdir, DISTID)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
			system(cmd)
			add_fname_to_fnamelist(DISTID_fname)
			spit_header_to_DISTIDfile()
		}
		else {
			printf("WARNING %d: DISTID_fname = %s exists.\n", NR, DISTID_fname)
		}
		done_DISTID_fname_arr[DISTID_fname] = "1"
	}

	# DISTID total for all groups and centres and projects and servicetypes and sids
	spit_allsid_totals_to_DISTIDfile("All", SHIPTO, "All", "All", "All")

	# group total for all centre and projects and servicetypes and sids
	spit_allsid_totals_to_DISTIDfile("All", SHIPTO, GROUP, "All", "All")

	# group - centre total for all projects and servicetypes and sids
	spit_allsid_totals_to_DISTIDfile("All", SHIPTO, GROUP, CENTRE, "All")

	# group - centre - project total for all servicetypes and sids
	spit_allsid_totals_to_DISTIDfile("All", SHIPTO, GROUP, CENTRE, PROJECT)


	# servicetype total for all groups and centres and projects and sids
	spit_allsid_totals_to_DISTIDfile(SERVTYPE, SHIPTO, "All", "All", "All")

	# servicetype group total for all centres and projects and sids
	spit_allsid_totals_to_DISTIDfile(SERVTYPE, SHIPTO, GROUP, "All", "All")

	# servicetype total for all projects and sids in each centre
	spit_allsid_totals_to_DISTIDfile(SERVTYPE, SHIPTO, GROUP, CENTRE, "All")

	# group - centre - project - servicetype total for all sids
	spit_allsid_totals_to_DISTIDfile(SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT)

	close(DISTID_fname)
}


function handle_SERVTYPE_datafile()
{
	print "handle_SERVTYPE_datafile() SERVTYPE= " SERVTYPE "  DISTID=" DISTID "  HIERIDx=" HIERIDx

	# rjs9
	# tbs rest - no drill below reportgroup
	if ( SERVTYPE >= 3000000 )
		return

	SERVTYPE_fname = sprintf("topdat/%s/%s/data/service%s/service_%s_%s_%s.csv", monthdir, DISTID, SERVTYPE, DISTID, CENTRE, SERVTYPE)

	if ( done_SERVTYPE_fname_header_arr[SERVTYPE_fname] == "" ) {
		printf(">>>          SERVTYPE_fname = %s\n", SERVTYPE_fname)
		if ( (getline x < SERVTYPE_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/%s/data/service%s", monthdir, DISTID, SERVTYPE)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
			system(cmd)
			add_fname_to_fnamelist(SERVTYPE_fname)
			spit_header_to_SERVTYPEfile()
		}
		else {
			printf("WARNING %d: SERVTYPE_fname = %s exists.\n", NR, SERVTYPE_fname)
		}
		done_SERVTYPE_fname_header_arr[SERVTYPE_fname] = "1"
	}

	# sid total
	if ( done_SERVTYPE_fname_SID_totals_arr[SERVTYPE_fname "|" PROJECT "|" SID "|" SUPPLIER] == "" ) {

                if ( SERVTYPE == 5 ) {  # account code summary extn totals
                        print "ACCCDEBUG: SID=[" SID "]"
                        for ( acccode_extn in acccode_extn_arr ) {
                                split(acccode_extn, accex_arr, "|")
                                if ( SID == accex_arr[1] ) {
                                        print "ACCCDEBUG: acccode_extn=[" acccode_extn "]"
                                        t21_extn = accex_arr[2]
                                        a_eggrid = dirextn_to_eggrid_arr[t21_extn]
					get_t21info(1,a_eggrid,t21_extn)
					spit_SIDtotals_to_SERVTYPEfile(SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID ":" t21_extn, SUPPLIER, INVOICENO, SUNDRY)
                                }
                        }
                        t21_extn = "All"
                        t21name = "All Extension"
                        t21surname = ""
                        t21firstname = ""
                        t21location = ""
                        t21site = ""
                        spit_SIDtotals_to_SERVTYPEfile(SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID ":" t21_extn, SUPPLIER, INVOICENO, SUNDRY)
                }
                else {
                        spit_SIDtotals_to_SERVTYPEfile(SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID, SUPPLIER, INVOICENO, SUNDRY)
                }

		# all supplier/invoiceno/sundry total
		#spit_SIDtotals_to_SERVTYPEfile(SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID, "All", "All", "All")


#xyzzy
		# create symlinks to call detail files
		# - for m11cdr detail data sym links
		# - see process_tdetail_file()
		det_dname = ""
		det_file = ""
		sym_dname = ""
                if ( SERVTYPE == 1 ||	# fixed line extn call detail
		     SERVTYPE == 2 ||	# equipment call detail
		     SERVTYPE == 3 ||	# miscellaneous call detail
		     SERVTYPE == 4 ) {	# overhead call detail
			det_dname = sprintf("../../../data/extn%s",  SERVTYPE)
			det_file = sprintf("extn_%s_%s_%s_%s.csv", CENTRE, PROJECT, SERVTYPE, SID)
			sym_dname = sprintf("topdat/%s/%s/data/extn%s", monthdir, DISTID, SERVTYPE)
		}
		if ( SERVTYPE == 5 ) {	# for account code call detail
			det_dname = sprintf("../../../data/acccodedet%s", SERVTYPE)
			det_file = sprintf("acccodedet_%s_%s_%s_%s.csv", CENTRE, PROJECT, SERVTYPE, SID)
			sym_dname = sprintf("topdat/%s/%s/data/acccodedet%s", monthdir, DISTID, SERVTYPE)
		}
                if ( SERVTYPE == 10 ) {	# mobile call detail
			det_dname = sprintf("../../../data/mobile%s", SERVTYPE)
			det_file = sprintf("mobile_%s_%s_%s_%s.csv", CENTRE, PROJECT, SERVTYPE, SID)
			sym_dname = sprintf("topdat/%s/%s/data/mobile%s", monthdir, DISTID, SERVTYPE)
		}
		if ( det_file != "" ) {
			 print "detsym: done_det_file_symlink_arr[" sym_dname "/" det_file "]=[" done_det_file_symlink_arr[sym_dname "/" det_file] "]"
			 if ( done_det_file_symlink_arr[sym_dname "/" det_file] == "" ) {	
				cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", sym_dname, sym_dname)
				#print "detmkdir: SERVTYPE=" SERVTYPE " [" cmd "]"
				system(cmd)
				cmd = sprintf("cd \"%s\"; [ ! -L \"%s\" ] && ln -s \"%s\" \"%s\"", sym_dname, det_file, det_dname "/" det_file, det_file)
				system(cmd)
				print "  detsym: SERVTYPE=" SERVTYPE " [" cmd "]"
				done_det_file_symlink_arr[sym_dname "/" det_file] == "1"
			}
		}
		else {
			 print "detsym: no det_file symlink"
		}


		done_SERVTYPE_fname_SID_totals_arr[SERVTYPE_fname "|" PROJECT "|" SID "|" SUPPLIER] = "1"

	}

	close(SERVTYPE_fname)
}


function handle_swb_datafiles()
{
	# switchboard
	if ( SERVTYPE == 101030 ) {
		if ( done_swb_arr[DISTID "|" SERVTYPE "|" CENTRE "|" PROJECT "|" SID] == "" ) {
			print "handle_swb_datafiles() SERVTYPE= " SERVTYPE "  DISTID=" DISTID "  HIERIDx=" HIERIDx
			procswbcalldetails(t21swcallsfile)
			done_swb_arr[DISTID "|" SERVTYPE "|" CENTRE "|" PROJECT "|" SID] = "1"
		}
	}
}


function handle_vml_datafiles()
{
	# voicemail
	if ( SERVTYPE == 101047 ) {
		if ( done_vml_arr[DISTID "|" SERVTYPE "|" CENTRE "|" PROJECT "|" SID] == "" ) {
			print "handle_vml_datafiles() SERVTYPE= " SERVTYPE "  DISTID=" DISTID "  HIERIDx=" HIERIDx
			procvmlcalldetails(t21vmcallsfile)
			done_vml_arr[DISTID "|" SERVTYPE "|" CENTRE "|" PROJECT "|" SID] = "1"
		}
	}
}



function handle_REPORTGROUP_datafile()
{
	print "handle_REPORTGROUP_datafile() DISTID=" DISTID "  reportgroupid=" reportgroupid "  INVOICENO=" INVOICENO "  SUNDRY=" SUNDRY "  SUPPLIER=" SUPPLIER "  SERVTYPE=" SERVTYPE
	print "  HIERIDx=" HIERIDx

	REPORTGROUP_fname = sprintf("topdat/%s/%s/data/reportgroup%s/reportgroup%s_%s_%s.csv", monthdir, DISTID, reportgroupid, reportgroupid, INVOICENO, SUNDRY)

	if ( done_REPORTGROUP_fname_header_arr[REPORTGROUP_fname] == "" ) {
		printf(">>>          REPORTGROUP_fname = %s\n", REPORTGROUP_fname)
		if ( (getline x < REPORTGROUP_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/%s/data/reportgroup%s", monthdir, DISTID, reportgroupid)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
			system(cmd)
			add_fname_to_fnamelist(REPORTGROUP_fname)
			spit_header_to_REPORTGROUPfile()
		}
		else {
			printf("WARNING %d: REPORTGROUP_fname = %s exists.\n", NR, REPORTGROUP_fname)
		}
		done_REPORTGROUP_fname_header_arr[REPORTGROUP_fname] = "1"
	}

	if ( reportgroupid == 10 || reportgroupid == 11 || reportgroupid == 13 ) {
		sid = SID
		centre = CENTRE
		project = PROJECT

		spit_data_to_REPORTGROUPfile()
	}

	if ( REPORTGROUP_fname != "" )
		close(REPORTGROUP_fname)
}


function handle_CUSTOMERIMPORT_datafile()
{
	print "handle_CUSTOMERIMPORT_datafile() SERVTYPE= " SERVTYPE "  DISTID=" DISTID "  HIERIDx=" HIERIDx

	# not from cde
	if ( REC_TYPE != "T_CHARGE_SUM" ) {
		#print "skipping REC_TYPE=[" REC_TYPE "]" "not from cde"
		return
	}

	#CUSTOMERIMPORT_fname = sprintf("topdat/%s/import_%s_%s_%s.csv", last_pardir, last_DIVID, monthtag, Xinvoiceno)

	CUSTOMERIMPORT_fname = sprintf("topdat/%s/%s/data/import/import_%s_%s_%s.csv", monthdir, DISTID, SHIPTO, monthtag, INVOICENO)

	printf("       CUSTOMERIMPORT_fname = %s\n", CUSTOMERIMPORT_fname);

	if ( done_CUSTOMERIMPORT_header[CUSTOMERIMPORT_fname] != "1" ) {
		# create file and csv header
		#newdir = sprintf("topdat/%s", monthdir);
		newdir = sprintf("topdat/%s/%s/data/import", monthdir, DISTID)
		cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
		system(cmd)
		add_fname_to_fnamelist(CUSTOMERIMPORT_fname)
		spit_header_to_CUSTOMERIMPORTfile();

		# add import file fname to customer import file list
		CUSTOMERIMPORTfilelist_fname = sprintf("topdat/%s/%s/data/import/importfilelist_%s.csv", monthdir, DISTID, DISTID)
		if ( done_CUSTOMERIMPORTfilelist[CUSTOMERIMPORTfilelist_fname] != "1" ) {
			add_fname_to_fnamelist(CUSTOMERIMPORTfilelist_fname)
			done_CUSTOMERIMPORTfilelist[CUSTOMERIMPORTfilelist_fname] = "1"
		}
		print CUSTOMERIMPORT_fname >>CUSTOMERIMPORTfilelist_fname
		close(CUSTOMERIMPORTfilelist_fname)

		done_CUSTOMERIMPORT_header[CUSTOMERIMPORT_fname] = "1"
	}

	# do customer import CSV data
	spit_SIDtotals_to_CUSTOMERIMPORTfile(SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID, SUPPLIER, INVOICENO, SUNDRY)

	close(CUSTOMERIMPORT_fname)
}



function handle_CUSTOMERIMPORT_detail_summary_datafile()
{
	print "handle_CUSTOMERIMPORT_detail_summary_datafile() SERVTYPE= " SERVTYPE "  DISTID=" DISTID "  HIERIDx=" HIERIDx

	# not from cde
	if ( REC_TYPE != "T_CHARGE_SUM" ) {
		print "skipping REC_TYPE=[" REC_TYPE "]" "not from cde"
		return
	}

	CUSTOMERIMPORTdetail_fname = sprintf("topdat/%s/%s/data/import/import_%s_%s_%s_detail.csv", monthdir, DISTID, SHIPTO, monthtag, INVOICENO)
	CUSTOMERIMPORTsummary_fname = sprintf("topdat/%s/%s/data/import/import_%s_%s_%s_summary.csv", monthdir, DISTID, SHIPTO, monthtag, INVOICENO)

	printf("       CUSTOMERIMPORTdetail_fname = %s\n", CUSTOMERIMPORTdetail_fname);
	printf("       CUSTOMERIMPORTsummary_fname = %s\n", CUSTOMERIMPORTsummary_fname);

	if ( done_CUSTOMERIMPORT_header[CUSTOMERIMPORTdetail_fname] != "1" ) {
		#print "         ---- CREATE files ---- "
		# create file and csv header
		#newdir = sprintf("topdat/%s", monthdir);
		newdir = sprintf("topdat/%s/%s/data/import", monthdir, DISTID)
		cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
		system(cmd)

		add_fname_to_fnamelist(CUSTOMERIMPORTdetail_fname)

		add_fname_to_fnamelist(CUSTOMERIMPORTsummary_fname)

		# add import files fname to customer import file list
		CUSROMETIMPORTfilelist_fname = sprintf("topdat/%s/%s/data/import/importfilelist_%s.csv", monthdir, DISTID, DISTID)
		if ( done_CUSTOMERIMPORTfilelist[CUSTOMERfilelist_fname] != "1" ) {
			add_fname_to_fnamelist(CUSTOMERIMPORTfilelist_fname)
			done_CUSTOMERIMPORTfilelist[CUSTOMERIMPORTfilelist_fname] = "1"
		}
		print CUSTOMERIMPORTdetail_fname >>CUSTOMERIMPORTfilelist_fname
		print CUSTOMERIMPORTsummary_fname >>CUSTOMERIMPORTfilelist_fname
		close(CUSTOMERIMPORTfilelist_fname)

		# init detail field data array
		f = 0
		mcsv_fldname_arr[++f] = "Element"
		mcsv_fldname_arr[++f] = "ServiceID"
		mcsv_fldname_arr[++f] = "Batch"
		mcsv_fldname_arr[++f] = "Supplier"
		mcsv_fldname_arr[++f] = "Location"
		mcsv_fldname_arr[++f] = "Person"
		mcsv_fldname_arr[++f] = "Centre"
		mcsv_fldname_arr[++f] = "Activity"
		mcsv_fldname_arr[++f] = "Element1"
		mcsv_fldname_arr[++f] = "SubLedger"
		mcsv_fldname_arr[++f] = "PayType"
		mcsv_fldname_arr[++f] = "ServiceType"
		mcsv_fldname_arr[++f] = "InvoiceDescription"
		mcsv_fldname_arr[++f] = "Period"
		mcsv_fldname_arr[++f] = "ShipTo"
		mcsv_fldname_arr[++f] = "Invoice"
		mcsv_fldname_arr[++f] = "ExGST"
		mcsv_fldname_arr[++f] = "GST"
		mcsv_fldname_arr[++f] = "IncGST"
		mcsv_fldname_arr[++f] = "TxnGrp"
		mcsv_fldname_arr[++f] = "Var01"
		mcsv_fldname_arr[++f] = "Var02"
		mcsv_fldname_arr[++f] = "Var03"
		mcsv_fldname_arr[++f] = "Var04"
		mcsv_fldname_arr[++f] = "Var05"
		mcsv_fldname_arr[++f] = "Var06"
		mcsv_fldname_arr[++f] = "Var07"
		mcsv_fldname_arr[++f] = "Var08"
		mcsv_fldname_arr[++f] = "Var09"
		mcsv_fldname_arr[++f] = "Var10"
		mcsv_fldname_arr[++f] = "Var11"
		mcsv_fldname_arr[++f] = "Var12"
		mcsv_fldname_arr[++f] = "Var13"
		mcsv_fldname_arr[++f] = "Var14"
		mcsv_fldname_arr[++f] = "Var15"
		mcsv_fldname_arr[++f] = "BatchType"
		mcsv_fldname_arr[++f] = "Group"
		mcsv_fldname_arr[++f] = "Dept"
		mcsv_fldname_arr[++f] = "Service"
		mcsv_fldname_arr[++f] = "CentreID"
		mcsv_nf = f

		# create distid import detail file with header
                if ( (getline x < CUSTOMERIMPORTdetail_fname) >= 0 ) {
			close(CUSTOMERIMPORTdetail_fname)
			print "WARNING: " CUSTOMERIMPORTdetail_fname " already  exists."
			system("rm -f \"" CUSTOMERIMPORTdetail_fname "\"")
		}
		f = 0
		for ( i = 1; i <= mcsv_nf; ++i ) {
			mcsv_fldname = mcsv_fldname_arr[i]
			if ( i > 1 ) 
				printf(",") >> CUSTOMERIMPORTdetail_fname
			printf("%s", mcsv_fldname) >> CUSTOMERIMPORTdetail_fname
		}
		printf("\n") >> CUSTOMERIMPORTdetail_fname
		done_CUSTOMERIMPORT_header[CUSTOMERIMPORTdetail_fname] = "1"

		# init summary data array
		for ( mcsv_atag in mcsv_atag_arr ) {
			delete mcsv_atag_arr[mcsv_atag]
			delete mcsv_ExGST_total_arr[mcsv_atag]
			delete mcsv_GST_total_arr[mcsv_atag]
			delete mcsv_IncGST_total_arr[mcsv_atag]
		}

		# input import detail file
		#mimportfile = sprintf("%s/import_%s_%s_%s_detail.csv", mimportcsvdir, SHIPTO, INVOICENO, SUNDRY)
		mimportfile = sprintf("%s/import_%s_%s_detail.csv", mimportcsvdir, SHIPTO, INVOICENO)
		printf("  mimportfile = %s\n", mimportfile);

		# read mcsv input file
                while ( (getline mcsvline < mimportfile) > 0 ) {
			#print "   read mcsvline=[" mcsvline "]"
                        mcsv_nf = split(mcsvline, mcsv_arr, ",")

			# load record
			for ( i = 1; i <= mcsv_nf; ++i ) {
				mcsv_fldname = mcsv_fldname_arr[i]
				mcsv_fldvalue = mcsv_arr[i]
				mcsv_fldvalue_arr[mcsv_fldname] = mcsv_fldvalue
			}

			mcsv_ShipTo = mcsv_fldvalue_arr["ShipTo"]
			mcsv_Invoice = mcsv_fldvalue_arr["Invoice"]
			mcsv_ServiceID = mcsv_fldvalue_arr["ServiceID"]
			mcsv_Service = mcsv_fldvalue_arr["Service"]
			mcsv_CentreID = mcsv_fldvalue_arr["CentreID"]
			mcsv_Centre = mcsv_fldvalue_arr["Centre"]

			##print "   mcsv_ShiptTo=[" mcsv_ShipTo "]" "   mcsv_Invoice=[" mcsv_Invoice "]"
			##print "    mcsv_CentreID=[" mcsv_CentreID "]" "   mcsv_Centre=[" mcsv_Centre "]"
			#print "   mcsv_ServiceID=[" mcsv_ServiceID "]" "   mcsv_Service=[" mcsv_Service "]"
			#print"    MCSV distid_allegrid_arr[" DISTID "|" mcsv_ServiceID "] = [" distid_allegrid_arr[DISTID "|" mcsv_ServiceID] "]"

			# check row belongs to distid and if so spit....
			if ( distid_allegrid_arr[DISTID "|" mcsv_ServiceID] == 1 ) {
				#print "    BELONGS"

				# spit customer detail import CSV data record
				for ( i = 1; i <= mcsv_nf; ++i ) {
					mcsv_fldname = mcsv_fldname_arr[i]
					mcsv_fldvalue = mcsv_fldvalue_arr[mcsv_fldname]
					if ( i > 1 ) 
						printf(",") >> CUSTOMERIMPORTdetail_fname
					printf("%s", mcsv_fldvalue) >> CUSTOMERIMPORTdetail_fname
					#print "       mcsv_fldvalue_arr[" mcsv_fldname "]=[" mcsv_fldvalue_arr[mcsv_fldname] "]"
				}
				printf("\n") >> CUSTOMERIMPORTdetail_fname
				close(CUSTOMERIMPORTdetail_fname)

				# create summary
				#The Monthly summary groups and shows the following fields from the above dataset:
				#.    Invoice (Platinum Invoice)
				#.    Cost Centre (tServiceID!CustomerCentre)
				#.    Cost Centre Description  (tDepartment.Description AS Dept)
				#.    Activity
				#.    Element1
				#.    SubLedger (tServiceID.CustomerSubLedgerID)
				#.    PayType
				#.    Sum of ExGST
				#.    Sum of GST
				#.    Sum of IncGST
				# ...
				mcsv_Dept = mcsv_fldvalue_arr["Dept"]
				mcsv_Activity = mcsv_fldvalue_arr["Activity"]
				mcsv_Element1 = mcsv_fldvalue_arr["Element1"]
				mcsv_SubLedger = mcsv_fldvalue_arr["SubLedger"]
				mcsv_PayType = mcsv_fldvalue_arr["PayType"]
				mcsv_ExGST = mcsv_fldvalue_arr["ExGST"]
				mcsv_GST = mcsv_fldvalue_arr["GST"]
				mcsv_IncGST = mcsv_fldvalue_arr["IncGST"]

				mcsv_atag = mcsv_ShipTo "|" mcsv_Invoice "|" mcsv_Centre "|" mcsv_Dept "|" mcsv_Activity "|" mcsv_Element1 "|" mcsv_SubLedger "|" mcsv_PayType

				mcsv_atag_arr[mcsv_atag] = 1
				mcsv_ExGST_total_arr[mcsv_atag] += mcsv_ExGST
				mcsv_GST_total_arr[mcsv_atag] += mcsv_GST
				mcsv_IncGST_total_arr[mcsv_atag] += mcsv_IncGST
				#print "           SUM: mcsv_IncGST_total_arr[" mcsv_atag "]=[" mcsv_IncGST_total_arr[mcsv_atag] "]"

			}
			#else {
			#	print "    DOES NOT BELONG"
			#}
                }
		close(mimportfile)
		close(CUSTOMERIMPORTdetail_fname)

		# create distid import summary file with header
		printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",
			"Invoice",
			"Centre",
			"Dept",
			"Activity",
			"Element",
			"SubLedger",
			"PayType",
			"ExGST",
			"GST",
			"IncGST") > CUSTOMERIMPORTsummary_fname

		# create temp sorted mcsv_atag file
		tmpmcsv_atagfile = "/tmp/getmcsvtmpmcsv_atag"
		system("rm -f " tmpmcsv_atagfile)
		mcsvsortcmd = "sort >" tmpmcsv_atagfile
		for ( mcsv_atag in mcsv_atag_arr ) {
			print mcsv_atag | mcsvsortcmd
		}
		close(mcsvsortcmd)

		# output distid import summary data
		while ( (getline mcsv_atag < tmpmcsv_atagfile) > 0 ) {
			#print "   SUM: mcsv_atag=[" mcsv_atag "]"
			split(mcsv_atag,mcsvsum_atag_arr,"|")
			f = 0
			mcsv_ShipTo = mcsvsum_atag_arr[++f]
			mcsv_Invoice = mcsvsum_atag_arr[++f]
			mcsv_Centre = mcsvsum_atag_arr[++f]
			mcsv_Dept = mcsvsum_atag_arr[++f]
			mcsv_Activity = mcsvsum_atag_arr[++f]
			mcsv_Element1 = mcsvsum_atag_arr[++f]
			mcsv_SubLedger = mcsvsum_atag_arr[++f]
			mcsv_PayType = mcsvsum_atag_arr[++f]

			mcsv_ExGST_total = mcsv_ExGST_total_arr[mcsv_atag]
			mcsv_GST_total = mcsv_GST_total_arr[mcsv_atag]
			mcsv_IncGST_total = mcsv_IncGST_total_arr[mcsv_atag]

			# output record
			printf("%s,%s,%s,%s,%s,%s,%s,%0.2f,%0.2f,%0.2f\n",
				mcsv_Invoice,
				mcsv_Centre,
				mcsv_Dept,
				mcsv_Activity,
				mcsv_Element1,
				mcsv_SubLedger,
				mcsv_PayType,
				mcsv_ExGST_total,
				mcsv_GST_total,
				mcsv_IncGST_total) >> CUSTOMERIMPORTsummary_fname
		}
		close(tmpmcsv_atagfile)
		system("rm -f " tmpmcsv_atagfile)

		close(CUSTOMERIMPORTsummary_fname)
	}

}



################################################
################################################

#-------
function spit_header_to_DISTIDfile()
{
	#print "spit_header_to_DISTIDfile() - " DISTID_fname
	csvstr = sprintf("%s,%s,%s,%s,%s,%s,CUSTOMERcode,CUSTOMERdesc,SHIPTO,groupdesc,group,centredesc,centre,projectdesc,project,projectfilter,ccpdesc,servicetypedesc,servicetypeID,reportgroupdesc,reportgroupid,supplier,invoiceno,sundry,chargecategory,totcount:Int,totduration:Int,totcost:Float\n", "V2 Cost Centre Summary", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID)

	#printf("%s", csvstr)
	printf("%s", csvstr) >>DISTID_fname
}


#-------

function spit_allsid_totals_to_DISTIDfile(Xservtype, Xshipto, Xgroup, Xcentre, Xproject)
{
	##XDISTIDtag = DISTID_fname "|" Xservtype "|" Xcentre "|" Xproject
	##if ( done_DISTID_fname_totals_arr[XDISTIDtag] == "" ) {
		#if ( spit_PERSONAL ) {
		#    #print "spit PERSONAL for XDISTIDtag=" XDISTIDtag
		#    spit_cat_totals_to_DISTIDfile(Xservtype,Xcentre,Xproject,"PERSONAL_" DISTID, SUPPLIER, INVOICENO, SUNDRY)
		#}
		#else {

			# for each servtype
			# supplier / invoiceno / sundry
			if ( Xservtype != "All" ) {
				spit_cat_totals_to_DISTIDfile(Xservtype,Xshipto,Xgroup,Xcentre,Xproject,"All", SUPPLIER, INVOICENO, SUNDRY)
			}

			# NO All combos for supplier / invoiceno / sundry
			# other than All_supplier / All_invoiceno / All_sundry
			spit_cat_totals_to_DISTIDfile(Xservtype,Xshipto,Xgroup,Xcentre,Xproject,"All", "All", "All", "All")

		#}
		##done_DISTID_fname_totals_arr[XDISTIDtag] = "1"
	##}
}

#-------

function spit_cat_totals_to_DISTIDfile(Xservtype, Xshipto, Xgroup, Xcentre, Xproject, Xsid, Xsupplier, Xinvoiceno, Xsundry)
{
	print "spit_cat_totals_to_DISTIDfile(Xservtype=" Xservtype ",Xshipto=" Xshipto ",Xgroup=" Xgroup ",Xcentre=" Xcentre ",Xproject=" Xproject ",Xsid=" Xsid ",Xsupplier=" Xsupplier ",Xinvoiceno=" Xinvoiceno ",Xsundry=" Xsundry ")"

	# All chg_cat totals
	spit_totals_to_DISTIDfile(DISTID,Xservtype,Xshipto,Xgroup,Xcentre,Xproject,Xsid,Xsupplier,Xinvoiceno,Xsundry,"All")

	## NO - charge category totals for All servtype tots
	#if ( Xservtype == "All" ) {
	#	return
	#}

	#  servtype/centre/project/sid totals for each charge cat
	spit_totals_to_DISTIDfile(DISTID,Xservtype,Xshipto,Xgroup,Xcentre,Xproject,Xsid,Xsupplier,Xinvoiceno,Xsundry,CHG_CAT)
}

#-------

function spit_totals_to_DISTIDfile(distid,servtype,shipto,group,centre,project,sid,supplier,invoiceno,sundry,chg_cat)
{
	#print "spit_totals_to_DISTIDfile() - " DISTID_fname

	Ztag = servtype "|" shipto "|" group "|" centre "|" project "|" sid "|" supplier "|" invoiceno "|" sundry "|" chg_cat

	if ( distid_to_wbd_type_arr[distid] == "PERSONAL" ) { # persernal list
		Xtag = "TOTAL_" distid "|" Ztag
	}
	else {
		# rjs
		#if ( centre != "All" ) {
		#	Xtag = "TOTAL" "|" Ztag
		#}
		#else {
			Xtag = distid "|" Ztag
		#}
	}

	XDISTIDtag = DISTID_fname "|" Xtag
	if ( done_DISTID_fname_totals_arr[XDISTIDtag] == "" ) {

		totalcount = totalcount_arr[Xtag]
		totalduration = totalduration_arr[Xtag]
		totalcost = totalcost_arr[Xtag]

		print "  spit_totals_to_DISTIDfile Xtag=" Xtag "  totalcost=" totalcost

		# if NOT All then use value set by  get_HIERIDx() 
		if ( group != "All" )
			groupdesc = GROUPdesc
		else
			groupdesc = "All Groups"

		set_project_displayinfo(centre, project)

		# if NOT All then use value set by  get_HIERIDx() 
		if ( servtype != "All" )
			servtypedesc = SERVTYPEdesc
		else
			servtypedesc = getSERVTYPEdesc(servtype,sid,sundry)

		if ( supplier == "All" && invoiceno == "All" && sundry == "All" && chg_cat == "All" ) {
		    if ( servtype == 1 || servtype == 2 || servtype == 10 ) {
			if ( centre != "All" && project != "All" )
				nstag = "TOTAL" "|" servtype "|" shipto "|" group "|" centre "|" project
			else
				nstag = distid "|" servtype "|" shipto "|" group "|" centre "|" project
			nsids = nsids_arr[nstag]
			#print "nsids: got nsids_arr[" nstag "]=[" nsids_arr[nstag] "]"
			if ( nsids != "" ) {
				if ( servtype == 1 )
					linecountdesc = "Active: " nsids
				if ( servtype == 2 )
					linecountdesc = "Lines: " nsids
				if ( servtype == 10 )
					linecountdesc = "Mobiles: " nsids
				#sub("EXTNCNT", " - " linecountdesc " ", servtypedesc)
				servtypedesc = servtypedesc " " linecountdesc
			}
		    }
		}

		## Testing	
		#if ( Testing >= 1 )
		#	ccpdesc = debuginfo

		csvstr = sprintf(",,,,,,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%d,%d,%0.2f\n", CUSTOMERcode, CUSTOMERdesc, SHIPTO, groupdesc, group, disp_centredesc, centre, disp_projectdesc, disp_project, projectfilter, ccpdesc, servtypedesc, servtype, reportgroupdesc, reportgroupid, supplier, invoiceno, sundry, chg_cat, totalcount, totalduration, mround2(totalcost))

		#printf("%s", csvstr)
		printf("%s", csvstr) >>DISTID_fname

		done_DISTID_fname_totals_arr[XDISTIDtag] = "1"
	}

}


#-------
# rjs9

function spit_header_to_REPORTGROUPfile()
{
	print "spit_header_to_REPORTGROUPfile() - " REPORTGROUP_fname

	if ( reportgroupid == 10 || reportgroupid == 11 || reportgroupid == 13 ) {
		# tbs sid summary header (modified wb3 DEPTID format from cde)
		csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,", "ServiceID Summary", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CUSTOMERdesc, SHIPTO, INVOICENO, SUNDRY, SUPPLIER)
		csvstr = csvstr sprintf("%s,%s,%s,%s,%s,%s,%s,%s,", "servtypedesc", "servtype", "groupdesc", "group", "centredesc", "centre", "projectdesc", "project");
		csvstr = csvstr sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s\n", "sid", "location", "user", "costcentre", "rentcost:Float", "callcost:Float", "othercost:Float", "admincost:Float", "totalcost:Float")

		#printf("%s", csvstr)
		printf("%s", csvstr) >>REPORTGROUP_fname
	}
	else {	# data taken from tdetail file

		csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,", "ServiceID Summary", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CUSTOMERdesc, SHIPTO, INVOICENO, SUNDRY, SUPPLIER)
		csvstr = csvstr sprintf("%s,%s,%s,%s,%s,%s,%s,%s,", "servtypedesc", "servtype", "groupdesc", "group", "centredesc", "centre", "projectdesc", "project");
		csvstr = csvstr sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,totalcost:Float\n",
				"tBatch_ID",
				"tBatch_BillingPeriod",
				"tBatchType_ID",
				"tBatchType_SupplierID",
				"tBatchType_Description",
				"tSupplier_SupplierName",
				"tServiceID_ServiceID",
				"tServiceID_Service",
				"tServiceType_Code",
				"tServiceType_Description",
				"tInvoice_InvoiceDate",
				"tTransactionTableYYYYMM_TxnDate",
				"tReportGroupID",
				"tReportGroup_GroupDesc",
				"tInvoice_PlatinumFileName",
				"tInvoice_PlatinumInvoiceNo",
				"tCustomer_ShipTo",
				"tCustomer_FullName",
				"tCentre_Description",
				"tActivity_Description",
				"tElement_ElementCode",
				"tElement_Description",
				"tElement_1_ElementCode",
				"tElement_1_Description",
				"tElement_2_ElementCode",
				"tElement_2_Description",
				"tSubledger_Description",
				"tDepartment_Description",
				"tLocation_Description",
				"tPerson_Person",

				"tApplication_ID",
				"tApplication_Description",

				"tOrigin_Description",
				"tDestination_Description",
				"tRateDescription_Description",
				"tRate_Cost:Float",
				"tTransactionType_Description",
				"tTransactionGroup_Description",
				"tDialledNumber_Description",
				"tTransactionTableYYYYMM_Duration:Int",

				"tTransactionTableYYYYMM_AmountExGST:Float",

				"tBatchType_Var01Description",
				"tTransactionTableYYYYMM_Var01:Int",
				"tBatchType_Var02Description",
				"tTransactionTableYYYYMM_Var02",
				"tBatchType_Var03Description",
				"tTransactionTableYYYYMM_Var03",
				"tBatchType_Var04Description",
				"tTransactionTableYYYYMM_Var04",
				"tBatchType_Var05Description",
				"tTransactionTableYYYYMM_Var05",
				"tBatchType_Var06Description",
				"tTransactionTableYYYYMM_Var06",
				"tBatchType_Var07Description",
				"tTransactionTableYYYYMM_Var07",
				"tBatchType_Var08Description",
				"tTransactionTableYYYYMM_Var08",
				"tBatchType_Var09Description",
				"tTransactionTableYYYYMM_Var09",
				"tBatchType_Var10Description",
				"tTransactionTableYYYYMM_Var10",
				"tBatchType_Var11Description",
				"tTransactionTableYYYYMM_Var11",
				"tBatchType_Var12Description",
				"tTransactionTableYYYYMM_Var12",
				"tBatchType_Var13Description",
				"tTransactionTableYYYYMM_Var13",
				"tBatchType_Var14Description",
				"tTransactionTableYYYYMM_Var14",
				"tBatchType_Var15Description",
				"tTransactionTableYYYYMM_Var15")

		#printf("%s", csvstr)
		printf("%s", csvstr) >>REPORTGROUP_fname

		# get all the data from tdetail
		tdetailfile = sprintf("%s/tdetail_%s_%s_%s_%s.unl", tdetailodir, SHIPTO, INVOICENO, SUNDRY, reportgroupid)
		process_tdetail_file(tdetailfile)

		# data done
		REPORTGROUP_fname != ""
	}

}


function spit_data_to_REPORTGROUPfile()
{
csvstr = ""
	print "spit_data_to_REPORTGROUPfile() - " REPORTGROUP_fname

	#XREPORTGROUPtag = REPORTGROUP_fname "|"
	#if ( done_REPORTGROUP_fname_totals_arr[XREPORTGROUPtag] == "" ) {
	#}

	set_project_displayinfo(centre, project)

	centredesc = CENTREdesc
	projectdesc = PROJECTdesc3
	costcentre = ccpdesc

	# tbs group
	group = grid_to_parentgroupid_arr[centre]
	groupdesc = grid_to_grname3_arr[group]

	print "  SERVTYPE=[" SERVTYPE "]" " sid=[" sid "]"
	print "    reportgroupid=[" reportgroupid "]"
	print "    batchtypeid=[" batchtypeid "]"
	print "    centre=[" centre "]  centredesc=[" centredesc "]"
	print "    project=[" project "] projectdesc=[" projectdesc "]"
	print "    costcentre=[" costcentre "]"
	print "    group=[" group "]" "  groupdesc=[" groupdesc "]"

	if ( reportgroupid == 10 || reportgroupid == 11 || reportgroupid == 13 ) {
		# tbs sid summary data
		# modified wb3 DEPTID format - one row per cde HIERIDx

		csvstr = sprintf(",,,,,,,,,,,")
		csvstr = csvstr sprintf("%s,%s,%s,%s,%s,%s,%s,%s,", SERVTYPEdesc, SERVTYPE, groupdesc, group, centredesc, centre, projectdesc, project);
		csvstr = csvstr sprintf("%s,%s,%s,%s,%0.2f,%0.2f,%0.2f,%0.2f,%0.2f\n", SID, location, user, costcentre, XRENT, XCALL, XOTHER, XADMIN, XTOTAL)

	}
	else {	# date taken from tdetail file
		totalcost = tdetail_tFinalisedTransaction_AmountExGST

		csvstr = sprintf(",,,,,,,,,,,")
		csvstr = csvstr sprintf("%s,%s,%s,%s,%s,%s,%s,%s,", SERVTYPEdesc, SERVTYPE, groupdesc, group, centredesc, centre, projectdesc, project);
		csvstr = csvstr sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",
			tdetail_tBatch_ID,
			tdetail_tBatch_BillingPeriod,
			tdetail_tBatchType_ID,
			tdetail_tBatchType_SupplierID,
			tdetail_tBatchType_Description,
			tdetail_tSupplier_SupplierName,
			tdetail_tServiceID_ServiceID,
			tdetail_tServiceID_Service,
			tdetail_tServiceType_Code,
			tdetail_tServiceType_Description,
			tdetail_tInvoice_InvoiceDate,
			tdetail_tFinalisedTransaction_TxnDate,
			tdetail_tReportGroupID,
			tdetail_tReportGroup_GroupDesc,
			tdetail_tInvoice_PlatinumFileName,
			tdetail_tInvoice_PlatinumInvoiceNo,
			tdetail_tCustomer_ShipTo,
			tdetail_tCustomer_FullName,
			tdetail_tCentre_Description,
			tdetail_tActivity_Description,
			tdetail_tElement_ElementCode,
			tdetail_tElement_Description,
			tdetail_tElement_CustEleCode,
			tdetail_tElement_CustEleDesc,
			tdetail_tElement_EleAdmCode,
			tdetail_tElement_EleAdmDesc,
			tdetail_tSubledger_CustSubLedger,
			tdetail_tDepartment_CustDept,
			tdetail_tLocation_Description,
			tdetail_tPerson_Person,

			tdetail_tApplication_ID,
			tdetail_tApplication_Description,

			tdetail_tOrigin_Description,
			tdetail_tDestination_Description,
			tdetail_tRateDescription_Description,
			tdetail_tRate_Cost,
			tdetail_tTransactionType_Description,
			tdetail_tTransactionGroup_Description,
			tdetail_tDialledNumber_Description,
			tdetail_tFinalisedTransaction_Duration,

			tdetail_tFinalisedTransaction_AmountExGST,

			tdetail_tBatchType_Var01Description,
			tdetail_tFinalisedTransaction_Var01,
			tdetail_tBatchType_Var02Description,
			tdetail_tFinalisedTransaction_Var02,
			tdetail_tBatchType_Var03Description,
			tdetail_tFinalisedTransaction_Var03,
			tdetail_tBatchType_Var04Description,
			tdetail_tFinalisedTransaction_Var04,
			tdetail_tBatchType_Var05Description,
			tdetail_tFinalisedTransaction_Var05,
			tdetail_tBatchType_Var06Description,
			tdetail_tFinalisedTransaction_Var06,
			tdetail_tBatchType_Var07Description,
			tdetail_tFinalisedTransaction_Var07,
			tdetail_tBatchType_Var08Description,
			tdetail_tFinalisedTransaction_Var08,
			tdetail_tBatchType_Var09Description,
			tdetail_tFinalisedTransaction_Var09,
			tdetail_tBatchType_Var10Description,
			tdetail_tFinalisedTransaction_Var10,
			tdetail_tBatchType_Var11Description,
			tdetail_tFinalisedTransaction_Var11,
			tdetail_tBatchType_Var12Description,
			tdetail_tFinalisedTransaction_Var12,
			tdetail_tBatchType_Var13Description,
			tdetail_tFinalisedTransaction_Var13,
			tdetail_tBatchType_Var14Description,
			tdetail_tFinalisedTransaction_Var14,
			tdetail_tBatchType_Var15Description,
			tdetail_tFinalisedTransaction_Var15,
			totalcost)
	}

	if ( csvstr != "" ) {
		#printf("%s", csvstr)
		printf("%s", csvstr) >>REPORTGROUP_fname
	}
}






#-------
function spit_header_to_SERVTYPEfile()
{
csvstr = ""
	#print "spit_header_to_SERVTYPEfile() - " SERVTYPE_fname

	if ( SERVTYPE == 1 ) {
		csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,", "Service Detail", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CENTREdesc, CENTRE, SERVTYPEdesc, SERVTYPE)
		csvstr = csvstr sprintf("extn,site,location,name,projectdesc,project,ccpdesc,tsiddesc,tsid,")
		csvstr = csvstr sprintf("Incoming Calls Ext:Int,Incoming Calls Int:Int,Outgoing Calls Free:Int,Outgoing Calls Local:Int,Outgoing Calls STD:Int,Outgoing Calls Mobile:Int,Outgoing Calls IDD:Int,Outgoing Calls Int:Int,Outgoing Call Costs Local:Float,Outgoing Call Costs STD:Float,Outgoing Call Costs Mobile:Float,Outgoing Call Costs IDD:Float,Total Cost:Float\n")
	}

	if ( SERVTYPE == 2 )
		csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,extn,site,location,name,projectdesc,project,ccpdesc,Total Cost:Float\n", "Service Detail", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CENTREdesc, CENTRE, SERVTYPEdesc, SERVTYPE)

	if ( SERVTYPE == 3 )
		csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,extn,site,location,name,projectdesc,project,ccpdesc,Total Cost:Float\n", "Service Detail", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CENTREdesc, CENTRE, SERVTYPEdesc, SERVTYPE)

	if ( SERVTYPE == 4 )
		csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,extn,site,location,name,projectdesc,project,ccpdesc,Total Cost:Float\n", "Service Detail", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CENTREdesc, CENTRE, SERVTYPEdesc, SERVTYPE)

        if ( SERVTYPE == 5 ) {
                csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,", "Service Detail", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CENTREdesc, CENTRE, SERVTYPEdesc, SERVTYPE)
                csvstr = csvstr sprintf("accountcode,accountcode_description,extn,name,projectdesc,project,ccpdesc,")

                csvstr = csvstr sprintf("Incoming Calls Ext:Int,Incoming Calls Int:Int,Outgoing Calls Free:Int,Outgoing Calls Local:Int,Outgoing Calls STD:Int,Outgoing Calls Mobile:Int,Outgoing Calls IDD:Int,Outgoing Calls Int:Int,Outgoing Call Costs Local:Float,Outgoing Call Costs STD:Float,Outgoing Call Costs Mobile:Float,Outgoing Call Costs IDD:Float,Total Cost:Float\n")
        }

	if ( SERVTYPE == 10 ) {
		sidcount = nsids_arr["TOTAL|10"]
		## r10
		#allchgcat = "All_MOBILE_CDR"
		allchgcat = "All"
		overallcost = totalcost_arr["TOTAL|10|All|All|All|All|All|All|All|All|" allchgcat]
		avg_cost = overallcost
		if ( mobdummy_cost > 0 ) {
			sidcount = sidcount - 1
			avg_cost = avg_cost - mobdummy_cost
			print "AVGDUMMY 1: mobdummy_cost=[" mobdummy_cost "]" "  avg_cost=[" avg_cost "]" "  sidcount=[" sidcount "]"
		}
		if ( sidcount > 0 )
			overallavgcost = sprintf("%0.2f", mround2(avg_cost / sidcount))
		else
			overallavgcost = 0
		print "AVGMOB: overallcost=" overallcost "  sidcount=" sidcount "  overallavgcost=" overallavgcost
		#csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,", "V2 Service Detail", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CENTREdesc, CENTRE, SERVTYPEdesc, SERVTYPE)
		csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,", "V2 Service Detail", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CENTREdesc, CENTRE, SERVTYPEdesc, SERVTYPE "~" overallavgcost)
		csvstr = csvstr sprintf("mobile,site,location,name,projectdesc,project,ccpdesc,supplier,")
		csvstr = csvstr sprintf("Onnet Count:Int,Onnet Duration:Int,Onnet Cost:Float,National Count:Int,National Duration:Int,National Cost:Float,International Count:Int,International Duration:Int,International Cost:Float,Commerce Count:Int,Commerce Duration:Int,Commerce Cost:Float,Messaging Count:Int,Messaging Duration:Int,Messaging Cost:Float,Misc Count:Int,Misc Duration:Int,Misc Cost:Float,")
		csvstr = csvstr sprintf("CDR Total Count:Int,CDR Total Duration:Int,CDR Total Cost:Float,")
		csvstr = csvstr sprintf("Equipment Cost:Float,Adjustment Cost:Float,Other Cost:Float,Recurring Cost:Float,Call Total Cost:Float,Total Cost:Float\n")
	}

	# rjs9
	# sw / vm
	if ( SERVTYPE >= 100000 && SERVTYPE <= 1999999 ) {
		csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,sid,site,location,name,projectdesc,project,ccpdesc,Total Cost:Float\n", "Service Detail", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CENTREdesc, CENTRE, SERVTYPEdesc, SERVTYPE)
	}
	# tbs rest
	if ( SERVTYPE >= 3000000 ) {
		# no data below report group
		return
		#csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,sid,site,location,name,projectdesc,project,ccpdesc,Total Cost:Float\n",
		#		 "Service Detail", exbillperiod, "SDATE", "EDATE", DISTIDdesc, DISTID, CENTREdesc, CENTRE, SERVTYPEdesc, SERVTYPE)
	}

	if ( csvstr != "" ) {
		#printf("%s", csvstr)
		printf("%s", csvstr) >>SERVTYPE_fname
	}
}


function spit_SIDtotals_to_SERVTYPEfile(servtype,shipto,group,centre,project,sid,supplier,invoiceno,sundry)
{
csvstr = ""
	print "spit_SIDtotals_to_SERVTYPEfile() - " SERVTYPE_fname

	## use t21 totals (except for mobile)
	## (need this for extn totals which aren't in cde data)
	#if ( servtype == 10 )
	#	tottag = "TOTALTBS"	# mobile totals from tbs cde
	#else
	#	tottag = "TOTAL"
	# use t21 totals
	tottag = "TOTAL"

	##Wtag = "TOTAL" "|" servtype "|" shipto "|" group "|" centre "|" project "|" sid "|" supplier "|" "All" "|" "All"
	#Wtag = "TOTALTBS" "|" servtype "|" shipto "|" group "|" centre "|" project "|" sid "|" supplier "|" invoiceno "|" sundry
	Wtag = tottag "|" servtype "|" shipto "|" group "|" centre "|" project "|" sid "|" supplier "|" invoiceno "|" sundry

	totalcost = totalcost_arr[Wtag "|All"]

	print "  SERVTYPE Wtag|All=" Wtag "|All  totalcost=" totalcost

	set_project_displayinfo(centre, project)

	if ( SERVTYPE == 1 ) {
	    P_cat = fixed_callcat_to_chg_cat["P"]
		#print "fixed P_cat=[" P_cat "]"
	    A_cat = fixed_callcat_to_chg_cat["A"]
		#print "fixed A_cat=[" A_cat "]"
	    N_cat = fixed_callcat_to_chg_cat["N"]
		#print "fixed N_cat=[" N_cat "]"
	    F_cat = fixed_callcat_to_chg_cat["F"]
		#print "fixed F_cat=[" F_cat "]"
	    L_cat = fixed_callcat_to_chg_cat["L"]
		#print "fixed L_cat=[" L_cat "]"
	    S_cat = fixed_callcat_to_chg_cat["S"]
		#print "fixed S_cat=[" S_cat "]"
	    E_cat = fixed_callcat_to_chg_cat["E"]
		#print "fixed E_cat=[" E_cat "]"
	    M_cat = fixed_callcat_to_chg_cat["M"]
		#print "fixed M_cat=[" M_cat "]"
	    I_cat = fixed_callcat_to_chg_cat["I"]
		#print "fixed I_cat=[" I_cat "]"
	    J_cat = fixed_callcat_to_chg_cat["J"]
		#print "fixed J_cat=[" J_cat "]"
	    O_cat = fixed_callcat_to_chg_cat["O"]
		#print "fixed O_cat=[" O_cat "]"

	    P_count = totalcount_arr[Wtag "|" P_cat]
	    A_count = totalcount_arr[Wtag "|" A_cat]
	    N_count = totalcount_arr[Wtag "|" N_cat]
	    F_count = totalcount_arr[Wtag "|" F_cat]
	    L_count = totalcount_arr[Wtag "|" L_cat]
	    S_count = totalcount_arr[Wtag "|" S_cat]
	    E_count = totalcount_arr[Wtag "|" E_cat]
	    M_count = totalcount_arr[Wtag "|" M_cat]
	    I_count = totalcount_arr[Wtag "|" I_cat]
	    J_count = totalcount_arr[Wtag "|" J_cat]
	    O_count = totalcount_arr[Wtag "|" O_cat]
	    L_cost = totalcost_arr[Wtag "|" L_cat]
	    S_cost = totalcost_arr[Wtag "|" S_cat]
	    E_cost = totalcost_arr[Wtag "|" E_cat]
	    M_cost = totalcost_arr[Wtag "|" M_cat]
	    I_cost = totalcost_arr[Wtag "|" I_cat]

	    egrid = CENTREPROJECTSID_to_EGRID_arr[centre "|" project "|" sid]
	    tsid = egrid
	    tsiddesc = grid_to_grname3_arr[egrid]

	    csvstr = sprintf(",,,,,,,,,,%s,%s,%s,%s,%s,%s,%s,%s,%s,%d,%d,%d,%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.2f,%0.2f\n", sid, t21site, t21location, t21name, PROJECTdesc3, disp_project, ccpdesc, tsiddesc, tsid, P_count, A_count + N_count, F_count, L_count, S_count + E_count, M_count, I_count, J_count + O_count, mround2(L_cost), mround2(S_cost + E_cost), mround2(M_cost), mround2(I_cost), mround2(totalcost))

	}

	if ( SERVTYPE == 2 )
		csvstr = sprintf(",,,,,,,,,,%s,%s,%s,%s,%s,%s,%s,%0.2f\n", sid, t21site, t21location, t21name, PROJECTdesc3, disp_project, ccpdesc, mround2(totalcost))

	if ( SERVTYPE == 3 )
		csvstr = sprintf(",,,,,,,,,,%s,%s,%s,%s,%s,%s,%s,%0.2f\n", sid, t21site, t21location, t21name, PROJECTdesc3, disp_project, ccpdesc, mround2(totalcost))

	if ( SERVTYPE == 4 )
		csvstr = sprintf(",,,,,,,,,,%s,%s,%s,%s,%s,%s,%s,%0.2f\n", sid, t21site, t21location, t21name, PROJECTdesc3, disp_project, ccpdesc, mround2(totalcost))


        if ( SERVTYPE == 5 ) {  # account code summary
            P_cat = fixed_callcat_to_chg_cat["P"]
            A_cat = fixed_callcat_to_chg_cat["A"]
            N_cat = fixed_callcat_to_chg_cat["N"]
            F_cat = fixed_callcat_to_chg_cat["F"]
            L_cat = fixed_callcat_to_chg_cat["L"]
            S_cat = fixed_callcat_to_chg_cat["S"]
            E_cat = fixed_callcat_to_chg_cat["E"]
            M_cat = fixed_callcat_to_chg_cat["M"]
            I_cat = fixed_callcat_to_chg_cat["I"]
            J_cat = fixed_callcat_to_chg_cat["J"]
            O_cat = fixed_callcat_to_chg_cat["O"]

            P_count = totalcount_arr[Wtag "|" P_cat]
            A_count = totalcount_arr[Wtag "|" A_cat]
            N_count = totalcount_arr[Wtag "|" N_cat]
            F_count = totalcount_arr[Wtag "|" F_cat]
            L_count = totalcount_arr[Wtag "|" L_cat]
            S_count = totalcount_arr[Wtag "|" S_cat]
            E_count = totalcount_arr[Wtag "|" E_cat]
            M_count = totalcount_arr[Wtag "|" M_cat]
            I_count = totalcount_arr[Wtag "|" I_cat]
            J_count = totalcount_arr[Wtag "|" J_cat]
            O_count = totalcount_arr[Wtag "|" O_cat]
            L_cost = totalcost_arr[Wtag "|" L_cat]
            S_cost = totalcost_arr[Wtag "|" S_cat]
            E_cost = totalcost_arr[Wtag "|" E_cat]
            M_cost = totalcost_arr[Wtag "|" M_cat]
            I_cost = totalcost_arr[Wtag "|" I_cat]

            csvstr = sprintf(",,,,,,,,,,%s,%s,%s,%s,%s,%s,%s,%d,%d,%d,%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.2f,%0.2f\n", t21_acccode, acccode_to_desc_arr[t21_acccode], t21_extn, t21name, PROJECTdesc3, disp_project, ccpdesc, P_count, A_count + N_count, F_count, L_count, S_count + E_count, M_count, I_count, J_count + O_count, mround2(L_cost), mround2(S_cost + E_cost), mround2(M_cost), mround2(I_cost), mround2(totalcost))

        }

	if ( SERVTYPE == 10 ) {	#mobile service10/call breakdown/charge summary
		ccat = "Mobile Onnet"
		mob_onnet_count = totalcount_arr[Wtag "|" ccat]
		mob_onnet_duration = totalduration_arr[Wtag "|" ccat]
		mob_onnet_cost = totalcost_arr[Wtag "|" ccat]
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		ccat = "Mobile National"
		mob_national_count = totalcount_arr[Wtag "|" ccat]
		mob_national_duration = totalduration_arr[Wtag "|" ccat]
		mob_national_cost = totalcost_arr[Wtag "|" ccat]
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		ccat = "Mobile International"
		mob_international_count = totalcount_arr[Wtag "|" ccat]
		mob_international_duration = totalduration_arr[Wtag "|" ccat]
		mob_international_cost = totalcost_arr[Wtag "|" ccat]
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		ccat = "Mobile Commerce"
		mob_commerce_count = totalcount_arr[Wtag "|" ccat]
		mob_commerce_duration = totalduration_arr[Wtag "|" ccat]
		mob_commerce_cost = totalcost_arr[Wtag "|" ccat]
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		ccat = "Mobile Messaging"
		mob_messaging_count = totalcount_arr[Wtag "|" ccat]
		mob_messaging_duration = totalduration_arr[Wtag "|" ccat]
		mob_messaging_cost = totalcost_arr[Wtag "|" ccat]
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		ccat = "Mobile Misc"
		mob_misc_count = totalcount_arr[Wtag "|" ccat]
		mob_misc_duration = totalduration_arr[Wtag "|" ccat]
		mob_misc_cost = totalcost_arr[Wtag "|" ccat]
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		ccat = "All_MOBILE_CDR"
		mob_cdr_totcount = totalcount_arr[Wtag "|" ccat]
		mob_cdr_totduration = totalduration_arr[Wtag "|" ccat]
		mob_cdr_totcost = totalcost_arr[Wtag "|" ccat]
		print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		#---
		#ccat = "Mobile Rent"
		#mob_rent_cost = totalcost_arr[Wtag "|" ccat]
		#
		# CALL charge (incl. discount)
		#ccat = "Mobile Call"
		#mob_call_cost = totalcost_arr[Wtag "|" ccat]
		#
		#ccat = "Mobile Call Discount"
		#mob_call_discount_total_cost = totalcost_arr[Wtag "|" ccat]
		#ccat = "Mobile Recurring Discount"
		#mob_recurring_discount_total_cost=totalcost_arr[Wtag "|" ccat]
		#---

		ccat = "Mobile Equipment"
		mob_equipment_cost = totalcost_arr[Wtag "|" ccat]
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		ccat = "Mobile Adjustment"
		mob_adjustment_cost = totalcost_arr[Wtag "|" ccat]
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		ccat = "Mobile Other"
		mob_other_cost = totalcost_arr[Wtag "|" ccat]
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		ccat = "Mobile Recurring"
		mob_recurring_cost = totalcost_arr[Wtag "|" ccat]
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		# CALL charge (NOT incl. discount)
		ccat = "Mobile Call Total"
		mob_call_total_cost = totalcost_arr[Wtag "|" ccat]
		ccat = "Mobile Internet"
		mob_call_total_cost += totalcost_arr[Wtag "|" ccat]
		## r10 - mobile m11 has no CALL_TOTAL
		#mob_call_total_cost = mob_onnet_cost + mob_national_cost + mob_international_cost + mob_commerce_cost + mob_messaging_cost + mob_misc_cost
		print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		#ccat = "All"
		#print "MOBDBG: totalcost_arr[" Wtag "|" ccat "] = [" totalcost_arr[Wtag "|" ccat] "]"

		csvstr = sprintf(",,,,,,,,,,")
		csvstr = csvstr sprintf("%s,%s,%s,%s,%s,%s,%s,%s,", sid, t21site, t21location, mobname, PROJECTdesc3, disp_project, ccpdesc, supplier)
		csvstr = csvstr sprintf("%d,%d,%0.2f,%d,%d,%0.2f,%d,%d,%0.2f,%d,%d,%0.2f,%d,%d,%0.2f,%d,%d,%0.2f,", mob_onnet_count, mob_onnet_duration, mround2(mob_onnet_cost), mob_national_count, mob_national_duration, mround2(mob_national_cost), mob_international_count, mob_international_duration, mround2(mob_international_cost), mob_commerce_count, mob_commerce_duration, mround2(mob_commerce_cost), mob_messaging_count, mob_messaging_duration, mround2(mob_messaging_cost), mob_misc_count, mob_misc_duration, mround2(mob_misc_cost))
		csvstr = csvstr sprintf("%d,%d,%0.2f,", mob_cdr_totcount, mob_cdr_totduration, mround2(mob_cdr_totcost))
		csvstr = csvstr sprintf("%0.2f,%0.2f,%0.2f,%0.2f,%0.2f,%0.2f\n", mround2(mob_equipment_cost), mround2(mob_adjustment_cost), mround2(mob_other_cost), mround2(mob_recurring_cost), mround2(mob_call_total_cost), mround2(totalcost))
	}

	# rjs9
	# sw / vm
	if ( SERVTYPE >= 100000 && SERVTYPE <= 1999999 ) {
		csvstr = sprintf(",,,,,,,,,,%s,%s,%s,%s,%s,%s,%s,%0.2f\n", sid, t21site, t21location, t21name, PROJECTdesc3, disp_project, ccpdesc, mround2(totalcost))
	}
	# tbs rest
	if ( SERVTYPE >= 3000000 ) {
		# no data below report group
		return
		#csvstr = sprintf(",,,,,,,,,,%s,%s,%s,%s,%s,%s,%s,%0.2f\n",
		#		 sid, t21site, t21location, t21name, PROJECTdesc3, disp_project, ccpdesc, mround2(totalcost))
	}

	if ( csvstr != "" ) {
		#printf("%s", csvstr)
		printf("%s", csvstr) >>SERVTYPE_fname
	}
}

#----------------------------------------------------------
#----------------------------------------------------------


function create_hi_sid_csv(hirepid, hirepdesc, N_HIGH, hi_in_arr, DISTID,fdistid, SERVTYPE, countcoldesc)
{
	HISID_fname = sprintf("topdat/%s/%s/data/mng/mngrep%s_%s.csv", monthdir, fdistid, hirepid, fdistid)

	itemdesc = "ServiceID"
	sitedesc = "Site"
	if ( SERVTYPE == "1" )
		itemdesc = "Extn"
	if ( SERVTYPE == "10" ) {
		itemdesc = "Mobile"
		#sitedesc = "ServiceType"
		sitedesc = "ShipTo"
	}

	if ( done_HISID_fname_header_arr[HISID_fname] == "" ) {
		printf(">>>          HISID_fname = %s\n", HISID_fname)
		if ( (getline x < HISID_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/%s/data/mng", monthdir, fdistid)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
			system(cmd)
			add_fname_to_fnamelist(HISID_fname)
			# spit header
			csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,Name,%s,Location,Description,Costcentre,%s,Cost:Float\n", hirepdesc, exbillperiod, "SDATE", "EDATE", hirepid, DISTIDdesc, DISTID, SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE], SERVTYPE, itemdesc, sitedesc, countcoldesc)
			#printf("%s", csvstr)
			printf("%s", csvstr) >>HISID_fname

		}
		else {
			printf("WARNING %d: HISID_fname = %s exists.\n", NR, HISID_fname)
		}
		done_HISID_fname_header_arr[HISID_fname] = "1"
	}

	# spit records
	for ( i = 0; i < N_HIGH; ++i ) {
		htag = DISTID "|" SERVTYPE
		hitag = htag "|" i
		val_obj = hi_in_arr[hitag]

		if ( val_obj == "" )
			continue

		split(val_obj, a_arr, "|")
		hi_val = a_arr[1]
		hi_centre = a_arr[2]
		hi_project = a_arr[3]
		hi_sid = a_arr[4]
		hi_count = a_arr[5]
		hi_duration = a_arr[6]
		hi_cost = a_arr[7]

		# rjs9 centre|project has multiple egrids
		#t21_EGRID = CENTREPROJECT_to_EGRID_arr[hi_centre "|" hi_project]
		t21_EGRID = CENTREPROJECTSID_to_EGRID_arr[hi_centre "|" hi_project "|" hi_sid]

		if ( SERVTYPE == "10" ) {
			get_mobileinfo(2,SERVTYPE,hi_sid,hi_centre)
			#t21site = tbssid_to_servicetyedesc_arr[hi_sid]
		}
		else {
			t21_extn = hi_sid
			get_t21info(SERVTYPE,t21_EGRID,t21_extn)
		}

		set_project_displayinfo(hi_centre, hi_project)

                if ( SERVTYPE == "1" )
			ccp = ccp "|" t21_EGRID
		else
			ccp = ccp "|"

		csvstr = sprintf(",,,,,,,,,%s,%s,%s,%s,%s,%s,%d,%0.2f\n", hi_sid, t21name, t21site, t21location, ccp, ccpdesc, hi_count, mround2(hi_cost))
		
		#printf("%s", csvstr)
		printf("%s", csvstr) >>HISID_fname
	}
	close(HISID_fname)
}



function create_hi_call_csv(hirepid, hirepdesc, N_HIGH, hi_in_arr, DISTID,fdistid, SERVTYPE)
{
	HICALL_fname = sprintf("topdat/%s/%s/data/mng/mngrep%s_%s.csv", monthdir, fdistid, hirepid, fdistid)

	itemdesc = "ServiceID"
	sitedesc = "Site"
	if ( SERVTYPE == "1" )
		itemdesc = "Extn"
	if ( SERVTYPE == "10" ) {
		itemdesc = "Mobile"
		#sitedesc = "ServiceType"
		sitedesc = "ShipTo"
	}

	if ( done_HICALL_fname_header_arr[HICALL_fname] == "" ) {
		printf(">>>          HICALL_fname = %s\n", HICALL_fname)
		if ( (getline x < HICALL_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/%s/data/mng", monthdir, fdistid)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
			system(cmd)
			add_fname_to_fnamelist(HICALL_fname)
			# spit header
			csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,Name,%s,Location,Description,Costcentre,Dialled/CLI,Duration,Cost:Float\n", hirepdesc, exbillperiod, "SDATE", "EDATE", hirepid, DISTIDdesc, DISTID, SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE], SERVTYPE, itemdesci, sitedesc)
			#printf("%s", csvstr)
			printf("%s", csvstr) >>HICALL_fname

		}
		else {
			printf("WARNING %d: HICALL_fname = %s exists.\n", NR, HICALL_fname)
		}
		done_HICALL_fname_header_arr[HICALL_fname] = "1"
	}

	# fixed
	#hi_call_by_cost_arr[mng1174|1|0]  = 38.72|23611|00000|1857|101|44018|out|05-10-2007|14:32:20|0414270400|M|Mobile Phone|123|38.72
	#hi_call_by_cost_arr[mng1174|10|0]  = 184.87|22107|00000|0420-946184|11|CDR|Mobile Commerce|MO|30-09-2007|17:22:22||vfint 18487Kb roam||123|187.87

	# mobile
	#hi_call_by_duration_arr[mng1174|1|11]  = 184184|20111|00000|5540|101|5257|out|22-10-2007|16:27:32|5257|J|Originating internal|321|0.0
	#hi_call_by_duration_arr[mng1174|10|0]  = 100219|23321|00000|0419-231473|169|CDR|Mobile Misc|PACKET DATA SESSIONS|08-09-2007|15:11:00|GIRRAWEEN|16 KB|BLACKBERRY.N|321|0.00

	# spit records
	for ( i = 0; i < N_HIGH; ++i ) {
		htag = DISTID "|" SERVTYPE
		hitag = htag "|" i
		val_obj = hi_in_arr[hitag]

		if ( val_obj == "" )
			continue

		split(val_obj, a_arr, "|")
		f = 0
		hi_val = a_arr[++f]
		hi_centre = a_arr[++f]
		hi_project = a_arr[++f]
		hi_sid = a_arr[++f]
		if ( SERVTYPE == "10" ) {
			mob_recno = a_arr[++f]
			mob_rectype = a_arr[++f]
			mob_cat = a_arr[++f]
			mob_recclass = a_arr[++f]
			date = a_arr[++f]
			ctimestr = a_arr[++f]
			mob_origin = a_arr[++f]
			expdialledno = a_arr[++f]
			mob_destination = a_arr[++f]
			duration = a_arr[++f]
			callcost = a_arr[++f]

			#chg_cat = mob_cat
			chg_duration = duration
			chg_cost = callcost
		}
		else {
			siteid = a_arr[++f]
			otherno = a_arr[++f]
			direction = a_arr[++f]
			date = a_arr[++f]
			endtimestr = a_arr[++f]
			expdialledno = a_arr[++f]
			callcat = a_arr[++f]
			calldesc = a_arr[++f]
			duration = a_arr[++f]
			callcost = a_arr[++f]

			#chg_cat = fixed_callcat_to_chg_cat[callcat]
			chg_duration = duration
			chg_cost = callcost
		}

		# rjs9 centre|project has multiple egrids
		#t21_EGRID = CENTREPROJECT_to_EGRID_arr[hi_centre "|" hi_project]
		t21_EGRID = CENTREPROJECTSID_to_EGRID_arr[hi_centre "|" hi_project "|" hi_sid]

		if ( SERVTYPE == "10" ) {
			get_mobileinfo(2,SERVTYPE,hi_sid,hi_centre)
			#t21site = tbssid_to_servicetyedesc_arr[hi_sid]
		}
		else {
			t21_extn = hi_sid
			get_t21info(SERVTYPE,t21_EGRID,t21_extn)
		}

		set_project_displayinfo(hi_centre, hi_project)

                if ( SERVTYPE == "1" )
			ccp = ccp "|" t21_EGRID
		else
			ccp = ccp "|"

		csvstr = sprintf(",,,,,,,,,%s,%s,%s,%s,%s,%s,%s,%s,%0.2f\n", hi_sid, t21name, t21site, t21location, ccp, ccpdesc, expdialledno, chg_duration, mround2(chg_cost))
		
		#printf("%s", csvstr)
		printf("%s", csvstr) >>HICALL_fname
	}
	close(HICALL_fname)
}



function create_hi_freqdial_csv(hirepid, hirepdesc, N_HIGH, hi_in_arr, DISTID,fdistid, SERVTYPE)
{
	HIFREQ_fname = sprintf("topdat/%s/%s/data/mng/mngrep%s_%s.csv", monthdir, fdistid, hirepid, fdistid)

	itemdesc = "ServiceID"
	if ( SERVTYPE == "1" )
		itemdesc = "Extn"
	if ( SERVTYPE == "10" )
		itemdesc = "Mobile"

	if ( done_HIFREQ_fname_header_arr[HIFREQ_fname] == "" ) {
		printf(">>>          HIFREQ_fname = %s\n", HIFREQ_fname)
		if ( (getline x < HIFREQ_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/%s/data/mng", monthdir, fdistid)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
			system(cmd)
			add_fname_to_fnamelist(HIFREQ_fname)
			# spit header
			csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,Dialled Number,Onnet,Total Number of Calls,Total Duration,Total Cost:Float,Most Frequent %s,Costcentre for %s,Count for %s\n", hirepdesc, exbillperiod, "SDATE", "EDATE", hirepid, DISTIDdesc, DISTID, SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE], SERVTYPE, itemdesc, itemdesc, itemdesc)
			#printf("%s", csvstr)
			printf("%s", csvstr) >>HIFREQ_fname

		}
		else {
			printf("WARNING %d: HIFREQ_fname = %s exists.\n", NR, HIFREQ_fname)
		}
		done_HIFREQ_fname_header_arr[HIFREQ_fname] = "1"
	}

	# fixed
	#hi_dist_freqdial_arr[mng1174|1|0]  = 4061|1800031037|947325|00000|9420|738
	# mobile
	#hi_dist_freqdial_arr[mng1174|10|0]  = 39006|Internet|20031|00000|0413-474744|2768

	htag = DISTID "|" SERVTYPE

	# spit records
	for ( i = 0; i < N_HIGH; ++i ) {
		hitag = htag "|" i
		val_obj = hi_in_arr[hitag]

		print "create_hi_freqdial_csv()  hi_in_arr[" hitag "] = [" hi_in_arr[hitag] "]"

		if ( val_obj == "" )
			continue

		split(val_obj, a_arr, "|")
		hi_val = a_arr[1]
		hi_dialno = a_arr[2]
		hi_sid_centre = a_arr[3]
		hi_sid_project = a_arr[4]
		hi_sid = a_arr[5]
		hi_sid_count = a_arr[6]

		onnet = (is_onnet(hi_dialno) != "") ? "Yes" : ""

		hi_count = hi_val
		hi_duration = dist_freq_numbers_duration_arr[htag "|" hi_dialno]
		hi_cost = dist_freq_numbers_cost_arr[htag "|" hi_dialno]

		# rjs9 centre|project has multiple egrids
		#hi_sid_EGRID = CENTREPROJECT_to_EGRID_arr[hi_sid_centre "|" hi_sid_project]
		hi_sid_EGRID = CENTREPROJECTSID_to_EGRID_arr[hi_sid_centre "|" hi_sid_project "|" hi_sid]

		#rjs4
		#hi_sid_costcentre = hi_sid_centre "-" hi_sid_project
		set_project_displayinfo(hi_sid_centre, hi_sid_project)

                if ( SERVTYPE == "1" )
			ccp = ccp "|" hi_sid_EGRID
		else
			ccp = ccp "|"

 		hi_sid_costcentre = ccp "|" ccpdesc

                if ( SERVTYPE == "10" ) {
			mobile = hi_sid
			gsub(" ","",mobile)
			gsub("-","",mobile)
			mobname = mobile_to_name_arr[mobile]
			dirID = mobile_to_directoryID_arr[mobile]
			if ( dirID <= 0 || dirID == "" ) {  # GROUP ASSIGNED
				hi_sid_name = ": " + mobname
			}
			else {
				hi_sid_name = recordno_to_t21name_arr[dirID]
			}
                }
                else {
                        hi_sid_name = dirextn_to_t21name_arr[hi_sid]
                }

		csvstr = sprintf(",,,,,,,,,%s,%s,%s,%s,%0.2f,%s,%s,%s\n", hi_dialno,onnet,hi_count,hi_duration,mround2(hi_cost), hi_sid "|" hi_sid_name, hi_sid_costcentre, hi_sid_count)
		
		#printf("%s", csvstr)
		printf("%s", csvstr) >>HIFREQ_fname
	}
	close(HIFREQ_fname)
}


function create_call_distr_by_date_csv(repid, repdesc, DISTID,fdistid, SERVTYPE)
{
	CALLDISTR_fname = sprintf("topdat/%s/%s/data/mng/mngrep%s_%s.csv", monthdir, fdistid, repid, fdistid)

	itemdesc = "ServiceID"
	if ( SERVTYPE == "1" )
		itemdesc = "Extn"
	if ( SERVTYPE == "10" )
		itemdesc = "Mobile"

	if ( done_CALLDISTR_fname_header_arr[CALLDISTR_fname] == "" ) {
		printf(">>>          CALLDISTR_fname = %s\n", CALLDISTR_fname)
		if ( (getline x < CALLDISTR_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/%s/data/mng", monthdir, fdistid)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
			system(cmd)
			add_fname_to_fnamelist(CALLDISTR_fname)
			# spit header
			csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,Day,Count,Duration,Cost:Float\n", repdesc, exbillperiod, "SDATE", "EDATE", repid, DISTIDdesc, DISTID, SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE], SERVTYPE)
			#printf("%s", csvstr)
			printf("%s", csvstr) >>CALLDISTR_fname

		}
		else {
			printf("WARNING %d: CALLDISTR_fname = %s exists.\n", NR, CALLDISTR_fname)
		}
		done_CALLDISTR_fname_header_arr[CALLDISTR_fname] = "1"
	}

	# get oldest and newest year
	oldyyyy = ""
	newyyyy = ""
	for ( Xtag in call_distribution_date_count_arr ) {
		split(Xtag, a_arr, "|")
		cdate = a_arr[3]
		cyyyy = 0 + substr(cdate,7,4)
		if ( oldyyyy == "" || cyyyy < oldyyyy )
			oldyyyy = cyyyy
		if ( newyyyy == "" || cyyyy > newyyyy )
			newyyyy = cyyyy
	}

	# spit records
	htag = DISTID "|" SERVTYPE
	for ( cyear = oldyyyy; cyear != "" && cyear <= newyyyy; ++cyear ) {
	    for ( cmonth = 1; cmonth <= 12; ++cmonth ) {
		for ( cday = 1; cday <= 31; ++cday ) {
			cddmmyyyy = sprintf("%02d-%02d-%04d",cday,cmonth,cyear);
			Xtag = htag "|" cddmmyyyy
			chg_count = call_distribution_date_count_arr[Xtag]
			if ( chg_count == "" )
				continue
			chg_duration = call_distribution_date_duration_arr[Xtag]
			chg_cost = call_distribution_date_cost_arr[Xtag]

			csvstr = sprintf(",,,,,,,,,%s,%d,%d,%0.2f\n", cddmmyyyy,chg_count,chg_duration,mround2(chg_cost))
			
			#printf("%s", csvstr)
			printf("%s", csvstr) >>CALLDISTR_fname
		}
	    }
	}
	close(CALLDISTR_fname)
}


function create_call_distr_by_hour_csv(repid, repdesc, DISTID,fdistid, SERVTYPE)
{
	CALLDISTR_fname = sprintf("topdat/%s/%s/data/mng/mngrep%s_%s.csv", monthdir, fdistid, repid, fdistid)

	itemdesc = "ServiceID"
	if ( SERVTYPE == "1" )
		itemdesc = "Extn"
	if ( SERVTYPE == "10" )
		itemdesc = "Mobile"

	if ( done_CALLDISTR_fname_header_arr[CALLDISTR_fname] == "" ) {
		printf(">>>          CALLDISTR_fname = %s\n", CALLDISTR_fname)
		if ( (getline x < CALLDISTR_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/%s/data/mng", monthdir, fdistid)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
			system(cmd)
			add_fname_to_fnamelist(CALLDISTR_fname)
			# spit header
			csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,Hour,Count,Duration,Cost:Float\n", repdesc, exbillperiod, "SDATE", "EDATE", repid, DISTIDdesc, DISTID, SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE], SERVTYPE)
			#printf("%s", csvstr)
			printf("%s", csvstr) >>CALLDISTR_fname

		}
		else {
			printf("WARNING %d: CALLDISTR_fname = %s exists.\n", NR, CALLDISTR_fname)
		}
		done_CALLDISTR_fname_header_arr[CALLDISTR_fname] = "1"
	}

	htag = DISTID "|" SERVTYPE

	#ndays = days_in_period
	# count days with data
	ndays = 0
	for ( Atag in call_distribution_byhourdate_arr ) {
		split(Atag, a_arr, "|")
		aservtype = a_arr[1]
		adate = a_arr[2]
		if ( aservtype == SERVTYPE )
			++ndays
	}

	print "create_call_distr_by_hour_csv(" repid "," repdesc ", " DISTID ", " SERVTYPE ")  ndays = " ndays

	if ( ndays <= 0 )
		ndays = 1

	# spit records
	for ( chour = 0; chour <= 23; ++chour ) {
		chh = sprintf("%02d", chour);
		Xtag = htag "|" chh

		chg_count = call_distribution_hour_count_arr[Xtag] / ndays
		chg_duration = call_distribution_hour_duration_arr[Xtag] / ndays
		chg_cost = call_distribution_hour_cost_arr[Xtag] / ndays

		csvstr = sprintf(",,,,,,,,,%d,%d,%d,%0.2f\n", chour+1,chg_count,chg_duration,mround2(chg_cost))
		
		#printf("%s", csvstr)
		printf("%s", csvstr) >>CALLDISTR_fname
	}
	close(CALLDISTR_fname)
}


function create_call_distr_by_category_csv(repid, repdesc, DISTID,fdistid, SERVTYPE)
{
	CALLDISTR_fname = sprintf("topdat/%s/%s/data/mng/mngrep%s_%s.csv", monthdir, fdistid, repid, fdistid)

	itemdesc = "ServiceID"
	if ( SERVTYPE == "1" )
		itemdesc = "Extn"
	if ( SERVTYPE == "10" )
		itemdesc = "Mobile"

	if ( done_CALLDISTR_fname_header_arr[CALLDISTR_fname] == "" ) {
		printf(">>>          CALLDISTR_fname = %s\n", CALLDISTR_fname)
		if ( (getline x < CALLDISTR_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/%s/data/mng", monthdir, fdistid)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", newdir, newdir)
			system(cmd)
			add_fname_to_fnamelist(CALLDISTR_fname)
			# spit header
			csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,Category,Count,Duration,Cost:Float\n", repdesc, exbillperiod, "SDATE", "EDATE", repid, DISTIDdesc, DISTID, SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE], SERVTYPE)
			#printf("%s", csvstr)
			printf("%s", csvstr) >>CALLDISTR_fname

		}
		else {
			printf("WARNING %d: CALLDISTR_fname = %s exists.\n", NR, CALLDISTR_fname)
		}
		done_CALLDISTR_fname_header_arr[CALLDISTR_fname] = "1"
	}

	htag = DISTID "|" SERVTYPE

	# fixed/mobile Intra-Organisational (has same data)
	if ( repid == "101" || repid == "102" ) {
		fixedhtag = DISTID "|" "1"
		spit_call_distr_by_category(fixedhtag,"Extension to Extension")
		spit_call_distr_by_category(fixedhtag,"Extension to ONNET Mobile")
		mobhtag = DISTID "|" "10"
		spit_call_distr_by_category(mobhtag,"ONNET Mobile to ONNET Mobile")
		spit_call_distr_by_category(mobhtag,"ONNET Mobile to Extension")
	}

	# fixed by call category
	if ( repid == "111" ) {
		#fixed_callcat_to_chg_cat["P"]
		#fixed_callcat_to_chg_cat["A"]
		#fixed_callcat_to_chg_cat["N"]
		spit_call_distr_by_category(htag, fixed_callcat_to_chg_cat["L"])
		spit_call_distr_by_category(htag, fixed_callcat_to_chg_cat["S"])
		spit_call_distr_by_category(htag, fixed_callcat_to_chg_cat["I"])
		spit_call_distr_by_category(htag, fixed_callcat_to_chg_cat["M"])
		spit_call_distr_by_category(htag, fixed_callcat_to_chg_cat["E"])
		spit_call_distr_by_category(htag, fixed_callcat_to_chg_cat["F"])
		#fixed_callcat_to_chg_cat["J"]
		#fixed_callcat_to_chg_cat["O"]
	}

	# mobile by call category
	if ( repid == "112" ) {
		spit_call_distr_by_category(htag, "Mobile National")
		spit_call_distr_by_category(htag, "Mobile International")
		spit_call_distr_by_category(htag, "Mobile Messaging")
		spit_call_distr_by_category(htag, "Mobile Commerce")
		spit_call_distr_by_category(htag, "Mobile Onnet")
		spit_call_distr_by_category(htag, "Mobile Misc")
		#"All_MOBILE_CDR"
		## "Mobile Rent"
		## "Mobile Call"
		## "Mobile Call Discount"
		##"Mobile Recurring Discount"
		#"Mobile Equipment"
		#"Mobile Adjustment"
		#"Mobile Other"
		#"Mobile Recurring"
		#"Mobile Call Total"
		##"All"
	}

	close(CALLDISTR_fname)
}

function spit_call_distr_by_category(htag,category)
{
	Xtag = htag "|" category

	chg_count = call_distribution_category_count_arr[Xtag]
	chg_duration = call_distribution_category_duration_arr[Xtag]
	chg_cost = call_distribution_category_cost_arr[Xtag]

	csvstr = sprintf(",,,,,,,,,%s,%d,%d,%0.2f\n", category,chg_count,chg_duration,mround2(chg_cost))
	
	#printf("%s", csvstr)
	printf("%s", csvstr) >>CALLDISTR_fname
}




#----------------------------------------------------------
#----------------------------------------------------------

function spit_header_to_wbttopfile()
{
	wbttop_fname = sprintf("topdat/%s/data/wbttop.csv", monthdir)

	if ( retroBDL != "1" ) {
		# create wbttop csv file
		print "spit_header_to_wbttopfile() - wbttop_fname = " wbttop_fname
		csvstr = sprintf("%s,%s,%s,%s,UserID_DESC,UserID,DistID_DESC,DistID,BDMethod,BDType,RealeaseDate,RegenDate,LastSentDate\n", "User List", exbillperiod, "SDATE", "EDATE")
		#printf("%s", csvstr)
		printf("%s", csvstr) >wbttop_fname
	}
}

function spit_USER_to_wbttopfile()
{
	print "spit_USER_to_wbttopfile() - wbttop_fname = " wbttop_fname
	DISTIDdesc = distid_to_distiddesc_arr[DISTID]
	t21name = distid_to_t21name_arr[DISTID]
	t21userid = distid_to_userid_arr[DISTID]
	if ( t21userid == "" )
		t21userid = distid_to_directoryID_arr[DISTID]
	t21wbd_method = distid_to_wbd_method_arr[DISTID]
	t21wbd_type = distid_to_wbd_type_arr[DISTID]
	bm_releasedate = ""	# release date in wbttop (set by user later)
	bm_regendate = (retroBDL == "1") ? cur_dd_mm_yyyy : "" # gen/regen date
	bm_lastsentdate = ""	# last emailed date

	if ( ENVIRON["AUTORELEASE"] == "1" ) {
		bm_releasedate = edate
		bm_lastsentdate = edate
	}

	csvstr = sprintf(",,,,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", t21name, t21userid, DISTIDdesc, DISTID, t21wbd_method, t21wbd_type, bm_releasedate, bm_regendate, bm_lastsentdate)

	if ( retroBDL == "1" ) {

		save_wbttop_fname = sprintf("topdat/%s/data/orig_wbttop.csv", monthdir)
		# if not already saved, save original wbttop csv file
		if ( (getline x < save_wbttop_fname) < 0 ) {
			system("cp " wbttop_fname " " save_wbttop_fname)
		}
		close(save_wbttop_fname)

		# users in wbttop csv file
		tmp_wbttop_fname = sprintf("/tmp/%s_%s_wbttop.csv", monthdir, DISTID)
		print "replace DISTID=" DISTID " in " wbttop_fname
		replaced = 0
		while ( (getline wbttopline < wbttop_fname) > 0 ) {
			split(wbttopline, wbttop_arr, ",")
			adistid = clip(wbttop_arr[8])
			#print "  adistid = [" adistid "]"
			if ( adistid == DISTID ) {
				replaced = 1
				print "   replaced [" csvstr "]"
				printf("%s", csvstr) >>tmp_wbttop_fname
			}
			else {
				printf("%s\n", wbttopline) >>tmp_wbttop_fname
			}
		}
		if ( !replaced ) {	# must be new - so append
			print "   appending new [" csvstr "]"
			printf("%s", csvstr) >>tmp_wbttop_fname
		}
		close(wbttop_fname)
		close(tmp_wbttop_fname)
		system("cp " tmp_wbttop_fname " " wbttop_fname)
		system("rm " tmp_wbttop_fname)
	}
	else {
		# append user to wbttop csv file
		#printf("%s", csvstr)
		printf("%s", csvstr) >>wbttop_fname
	}
}


#----------------------------------------------------------
#----------------------------------------------------------

function set_project_displayinfo(centre,project)
{
	if ( centre != "All" && project == "All" ) {
		#grid = CENTRE_to_GGRID_arr[CENTRE]
		#childcount = childcount_arr[grid]
		#if ( childcount == 1 )
		# rjs9
		#if ( CHILDCOUNT == 1 )
		#	disp_project = PROJECT
		#else
		#	disp_project = "All"
		disp_project = "All"
	}
	else {
		disp_project = project
	}
	if ( addstar != "" )
		disp_project = disp_project addstar

        if ( project == "All" ) {
                projectfilter = "All"
                #if ( centre == "All" )
                #       ccpdesc = "Your Cost Centres"
                #else
                #       ccpdesc = CENTRE_to_CENTREdesc_arr[centre]
		# rjs10
		#if ( addstar == "" ) {
		#	#rjsPTC ccpdesc = "All"	#rjs4 CENTRE_to_CENTREdesc_arr[centre]
		#	# rjs9
		#	#ccpdesc = CENTRE_to_CENTREdesc2_arr[CENTRE]
		#	ccpdesc = "" 
		#}
		#else {
		#	# rjs9
		#	#ccpdesc = CENTREPROJECT_to_PROJECTdesc_arr[centre "|" PROJECT]
		#	ccpdesc = "" 
		#}
		# rjs10
		ccpdesc = "All Centres"
	}
	else {
		projectfilter = "List"
		# rjs10
		## rjs9
		#ccpdesc = CENTREPROJECT_to_PROJECTdesc_arr[centre "|" project]
		##ccpdesc = "" 
		ccpdesc = CENTREPROJECT_to_costcentre_arr[centre "|" project]
	}

	#rjs4
	#ccp = centre "-" disp_project
	ccpid = centre "|" disp_project

	CENTREdesc = CENTRE_to_CENTREdesc_arr[centre]
	PROJECTdesc3 = CENTREPROJECT_to_PROJECTdesc3_arr[centre "|" project]

	if ( centre == "All" )
		disp_centredesc = "All"
	else
		disp_centredesc = CENTREdesc

	if ( disp_project == "All" )
		disp_projectdesc = "All"
	else
		disp_projectdesc = PROJECTdesc3
	if ( addstar != "" )
		disp_projectdesc = disp_projectdesc addstar

	#ccp = ccpid "|" disp_centredesc "-" disp_projectdesc
	ccp = ccpid "|" disp_centredesc "-" disp_projectdesc

	#DEPT2951|CENTRE4108|VRT NETWORK SALES-410
}


#----------------------------------------------------------
#----------------------------------------------------------

function finish_proc_dist_lists()
{
	print ""
	print "finish_proc_dist_lists()"
	system("date")

	# process distribution lists

	spit_header_to_wbttopfile()

	for ( DISTID in distid_to_distiddesc_arr ) {
		DISTIDdesc = distid_to_distiddesc_arr[DISTID]
		t21name = distid_to_t21name_arr[DISTID]
		t21userid = distid_to_userid_arr[DISTID]
		if ( t21userid == "" )
			t21userid = distid_to_directoryID_arr[DISTID]
		t21wbd_method = distid_to_wbd_method_arr[DISTID]
		t21wbd_type = distid_to_wbd_type_arr[DISTID]

		if ( done_DISTID_totals_arr[DISTID] != "" ) {
			print "finish DISTID=[" DISTID "]" "  t21name=[" t21name "]" "  t21userid=[" t21userid "]" "  t21wbd_type=[" t21wbd_type "]"

			spit_USER_to_wbttopfile()

			# skip virtual management
			if ( virtual_management_distid_arr[DISTID] != "" )
				continue

			## vmng
			#svdistid = DISTID
			#if ( virtual_management_distid_arr[DISTID] != "" )
			#	DISTID = virtual_management_distid_arr[DISTID]

			#spit_DISTIDtotals_to_TOTALSfile()
			#gen_top_csv()
			#cpy_img(0)

			# get invoice pdf files
			print "distid_to_shiptoinvoicenolist_arr[" DISTID "]=[" distid_to_shiptoinvoicenolist_arr[DISTID] "]"
			split(distid_to_shiptoinvoicenolist_arr[DISTID],shiptoinvoicenolist_arr,"|")
			for ( i in shiptoinvoicenolist_arr ) {
				shiptoinvoiceno = shiptoinvoicenolist_arr[i]
				print "    shiptoinvoiceno=[" shiptoinvoiceno "]"
				split(shiptoinvoiceno,shiptoinvoiceno_arr,"~")
				shipto = shiptoinvoiceno_arr[1]
				invoiceno = shiptoinvoiceno_arr[2]

				print "Get invoice pdf for DISTID=" DISTID "  shipto=[" shipto "]" "  invoiceno=[" invoiceno "]" "  exbillperiod=[" exbillperiod "]"
				invpdf_fname = sprintf("topdat/%s/%s/data/invoice_%s_%s_%s.pdf", monthdir,DISTID,monthdir,shipto, invoiceno)
				add_fname_to_fnamelist(invpdf_fname)
				cmd = "getinvpdf \"" exbillperiod "\" \"" shipto "\" \"" invoiceno "\" \"" invpdf_fname "\""
				print "cmd=[" cmd "]"
				system(cmd)
			}

			#if (  t21wbd_type != "PERSONAL" ) {
				mk_dircsvfile()
			#}

			add_fname_to_fnamelist(fnamelist_fname)

			add_fname_to_fnamelist(wbttop_fname)

			mk_useridfile()

			# create distid file
			# for PERSONAL virtual
			# (configured list are done from distidfile_totals)
			if ( Vdistid_to_directoryID_arr[DISTID] != "" )
				mk_distidfile()
		

			# create user html index file link
			if ( LinkUserHTMLindex )
				ln_userhtml_index()

			if ( retroBDL == "1" ) {
				# create index files
				cmd = sprintf("mkind \"topdat/%s/%s\"", monthdir, DISTID)
				print "run: cmd = " cmd
				system(cmd)
			}

			## vmng
			#DISTID = svdistid
		}
	}

	print ""
}


#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------

function get_t21info(servtype,egrid,sid)
{
        #if ( servtype == 5 ) { # account code
        #       t21name = ""
        #       t21surname = ""
        #       t21firstname = ""
        #       t21location = ""
        #       t21site = ""
        #       return
        #}

	#itag = egrid "|" sid
	itag = sid
	t21name = dirextn_to_t21name_arr[itag]
	t21surname = dirextn_to_t21surname_arr[itag]
	t21firstname = dirextn_to_t21firstname_arr[itag]
	t21location = dirextn_to_t21location_arr[itag]
	t21division = recordno_to_t21division_arr[recordno]
	t21site = dirextn_to_t21site_arr[itag]

	# now set in get_mobileinfo
	#if ( SERVTYPE == 10 && sid == "" ) {	# mobile GROUP ASSIGNED
	#	t21location = ""
	#	t21site = egrid_to_t21site_arr[egrid]
	#}

	if ( t21name == "_blank_" )
		t21name = ""
	if ( t21surname == "_blank_" )
		t21surname = ""
	if ( t21firstname == "_blank_" )
		t21firstname = ""
	if ( t21location == "_blank_" )
		t21location = ""
	if ( t21division == "_blank_" )
		t21division = ""

	if ( t21site == "" && sid != "All" ) {
		#print "BLANK SITE" "  extn_to_egridlist_arr[" sid "]=[" extn_to_egridlist_arr[sid] "]"
		if ( extn_to_egridlist_arr[sid] == "" )
			t21site = "NOT FOUND"
		else
			t21site = "MOVED"
	}

	#print "get_t21info() servtype=" servtype " egrid=" egrid " sid=" sid "  itag=" itag "  t21name=[" t21name "]  t21site=[" t21site "]"
}

#----------------------------------------------------------

function get_mobileinfo(a,servtype,sid,centre)
{
	mobile = sid
	gsub(" ","",mobile)
	gsub("-","",mobile)
	if ( mobile == "_blank_" )
		mobile = ""
	mobname = mobile_to_name_arr[mobile]
	if ( mob_name == "_blank_" )
		mob_name = "BLANK NAME"

	directoryID = mobile_to_directoryID_arr[mobile]
	if ( directoryID <= 0 || directoryID == "" ) {	# GROUP ASSIGNED
		t21_extn = ""
		siteid = ""
		t21surname = ""
		t21firstname = ""
		t21name = ""
		#t21site = egrid_to_t21site_arr[t21_EGRID]
		t21location = ""
		t21division = ""
	}
	else {
		t21_extn = recordno_to_extn_arr[directoryID]
		siteid = get_siteid(t21_extn)
		t21surname = recordno_to_t21surname_arr[recordno]
		t21firstname = recordno_to_t21firstname_arr[recordno]
		t21name = recordno_to_t21name_arr[directoryID]
		#t21site = recordno_to_t21site_arr[directoryID]
		t21location = recordno_to_t21location_arr[directoryID]
		t21division = recordno_to_t21division_arr[recordno]
	}

	t21site = CENTRE_to_ShipTo_arr[centre]

	if ( t21_extn == "_blank_" )
		t21_extn = ""
	if ( mobname == "_blank_" )
		mobname = ""
	if ( t21name == "_blank_" )
		t21name = ""
	if ( t21surname == "_blank_" )
		t21surname = ""
	if ( t21firstname == "_blank_" )
		t21firstname = ""
	if ( t21site == "_blank_" )
		t21site = ""
	if ( t21location == "_blank_" )
		t21location = ""
	if ( t21division == "_blank_" )
		t21division = ""

	if ( a == 2 )
		print a " " "get_mobileinfo() mobile=[" mobile "]  mobname=[" mobname "]  directoryID=[" directoryID "]  t21_extn=[" t21_extn "]   siteid=[" siteid "]"
}


#----------------------------------------------------------

function get_tbssidinfo(a,servtype,sid)
{
	directoryID = sid_to_directoryID_arr[sid]

	t21_extn = recordno_to_extn_arr[directoryID]
	#siteid = get_siteid(t21_extn)
	siteid = recordno_to_t21site_arr[directoryID]
	t21name = recordno_to_t21name_arr[directoryID]
	t21site = recordno_to_t21site_arr[directoryID]
	t21location = recordno_to_t21location_arr[directoryID]
	t21division = recordno_to_t21division_arr[directoryID]

	if ( a == 2 )
		print a " " "get_tbssidinfo() sid=[" sid "]" "  t21name=[" t21name "]" "  directoryID=[" directoryID "]" "  t21_extn=[" t21_extn "]" " siteid=[" siteid "]" "   t21site=[" t21site "]" "   t21location=[" t21location "]" "   t21division=[" t21division "]"
}



#----------------------------------------------------------

#rjs9 - called from ldt21detiail/ld_cde
function set_HIERIDx(from, servtype,shipto,group,centre,project,supplier,invoiceno,sundry,sid,rec_type,chg_cat)
{
	HIERID = servtype
	HIERID = HIERID "|" shipto
	HIERID = HIERID "|" group
	HIERID = HIERID "|" centre
	HIERID = HIERID "|" project
	HIERID = HIERID "|" supplier
	HIERID = HIERID "|" invoiceno
	HIERID = HIERID "|" sundry
	HIERID = HIERID "|" sid
	HIERID = HIERID "|" rec_type
	HIERID = HIERID "|" chg_cat
	HIERID = HIERID "|" from
	HIERIDxDUPcount = ++HIERIDxcount[HIERID]
	#HIERID = HIERID "|" HIERIDxDUPcount
	HIERIDx = HIERID "|"


	if ( Testing >= 3 ) {
		print "set HIERIDx " from " = " HIERIDx
	}

	if ( HIERIDxDUPcount > 1 ) {
		#print "FATAL: duplicate HIERIDx = " HIERIDx
		#exit 0
		#print "WARNING: skipping duplicate HIERIDx = " HIERIDx
		return
	}

	# for getting processing order
	#pipecmd = "sort >" logdir "/" "HIERIDx_" batchtag ".lst"
	#print HIERIDx | pipecmd
	HIERIDxsortcmd = "sort -u >" HIERIDxfile
	#print "HIERIDxsortcmd=[" HIERIDxsortcmd "]"
	print HIERIDx | HIERIDxsortcmd
}


function get_HIERIDx_info(HIERIDx,printflag)
{
	split(HIERIDx, HIERIDx_arr, "|")
	
	f = 0
	SERVTYPE = HIERIDx_arr[++f]
	SHIPTO = HIERIDx_arr[++f]
	GROUP = HIERIDx_arr[++f]
	CENTRE = HIERIDx_arr[++f]
	PROJECT = HIERIDx_arr[++f]
	SUPPLIER = HIERIDx_arr[++f]
	INVOICENO = HIERIDx_arr[++f]
	SUNDRY = HIERIDx_arr[++f]
	SID = HIERIDx_arr[++f]
	REC_TYPE = HIERIDx_arr[++f]
	CHG_CAT = HIERIDx_arr[++f]
	HIERIDxfrom = HIERIDx_arr[++f]
	#HIERIDxDUPcount = HIERIDx_arr[++f]
	HIERIDxDUPcount = "NODUPCOUNT"

	#SERVTYPEdesc = SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]
	SERVTYPEdesc = getSERVTYPEdesc(SERVTYPE,SID,SUNDRY)

	batchtypeid = sid_sundry_to_batchtypeid_arr[SID "|" SUNDRY]
	batchtypedesc = batchtypeid_to_batchtypedesc_arr[batchtypeid]

	supplier = SUPPLIER

	# tbs customer
	# (from t21groups4) SHIPTO = CENTRE_to_ShipTo_arr[CENTRE]
	CUSTOMERcode = ShipTo_to_ShipToCode_arr[SHIPTO]
	CUSTOMERdesc = ShipTo_to_CustomerDesc_arr[SHIPTO]

	# tbs group
	# (from t21groups4)GROUP = grid_to_parentgroupid_arr[CENTRE]
	GROUPdesc = grid_to_grname3_arr[GROUP]

	CENTREdesc = CENTRE_to_CENTREdesc_arr[CENTRE]
	CENTREdesc1 = CENTRE_to_CENTREdesc1_arr[CENTRE]
	CENTREdesc2 = CENTRE_to_CENTREdesc2_arr[CENTRE]
	CENTREdesc3 = CENTRE_to_CENTREdesc3_arr[CENTRE]

	PROJECTdesc = CENTREPROJECT_to_PROJECTdesc_arr[CENTRE "|" PROJECT]
	PROJECTdesc1 = CENTREPROJECT_to_PROJECTdesc1_arr[CENTRE "|" PROJECT]
	PROJECTdesc2 = CENTREPROJECT_to_PROJECTdesc2_arr[CENTRE "|" PROJECT]
	PROJECTdesc3 = CENTREPROJECT_to_PROJECTdesc3_arr[CENTRE "|" PROJECT]

	# rjs9 centre|project has multiple egrids
	#EGRID = CENTREPROJECT_to_EGRID_arr[CENTRE "|" PROJECT]
	EGRID = CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" SID]
	t21_EGRID = EGRID
	tbsSID = EGRID

	#GGRID = CENTRE_to_GGRID_arr[CENTRE]
	GGRID = grid_to_parentgroupid_arr[EGRID]
	t21_GGRID = GGRID

	t21_SID = SID
	#t21_SID = ""
	## t21 call/switchboard/voicemail
	#if ( SERVTYPE < 100 || SERVTYPE == 101030 || SERVTYPE == 101047 ) {
	#	t21_SID = SID
	#	if ( split(SID,SID_arr,":") == 2 ) {
	#		EGRID = SID_arr[1]
	#		t21_EGRID = EGRID
	#		t21_SID = SID_arr[2]
	#	}
	#}

	CHILDCOUNT = childcount_arr[GGRID]

	costcentre = CENTREPROJECT_to_costcentre_arr[CENTRE "|" PROJECT]

	reportgroupid = sundryEGRID_to_reportgroupid_arr[SUNDRY "|" EGRID]
	reportgroupdesc = sundryEGRID_to_reportgroupdesc_arr[SUNDRY "|" EGRID]
	reportorder = sundryEGRID_to_reportorder_arr[SUNDRY "|" EGRID]

	tascode = sundryEGRID_to_tascode_arr[SUNDRY "|" EGRID]
	invoiceno = sundryEGRID_to_invoiceno_arr[SUNDRY "|" EGRID]

	location = sundryEGRID_to_location_arr[SUNDRY "|" EGRID]
	user = sundryEGRID_to_user_arr[SUNDRY "|" EGRID]

	SEID = sundryEGRID_to_SEID_arr[SUNDRY "|" EGRID]
	if ( SEID == "" )
		SEID = SERVTYPE

	Xtottag = SERVTYPE "|" SHIPTO "|" GROUP "|" CENTRE "|" PROJECT "|" SUPPLIER "|" INVOICENO "|" SUNDRY "|" SID
	XRENT = Xrentcost_arr[Xtottag]
	XCALL = Xcallcost_arr[Xtottag]
	XOTHER = Xothercost_arr[Xtottag]
	XADMIN = Xadmincost_arr[Xtottag]
	XTOTAL = Xtotalcost_arr[Xtottag]

	debuginfo = HIERIDx_to_debuginfo_arr[HIERIDx]

	if ( HIERIDxfrom == "cde" )
		Xtag = "TOTALTBS" "|" SERVTYPE "|" SHIPTO "|" GROUP "|" CENTRE "|" PROJECT "|" SID "|" SUPPLIER "|" INVOICENO "|" SUNDRY "|" CHG_CAT
	else
		Xtag = "TOTAL" "|" SERVTYPE "|" SHIPTO "|" GROUP "|" CENTRE "|" PROJECT "|" SID "|" SUPPLIER "|" INVOICENO "|" SUNDRY "|" CHG_CAT

	totalcount = totalcount_arr[Xtag]
	totalduration = totalduration_arr[Xtag]
	totalcost = totalcost_arr[Xtag]

	#if ( 0 ) {
	if ( printflag > 1 ) {
		printf("\n")
		printf("get_HIERIDx_info()\n")
		printf("           HIERIDx = %s\n", HIERIDx)
		#printf("   HIERIDxDUPcount = %d\n", HIERIDxDUPcount)
		printf("           SERVTYPE = [(%s)%s]\n", SERVTYPE, SERVTYPEdesc)
		printf("              SEID = %s\n", SEID)
		printf("            SHIPTO = [(%s)(%s)%s]\n", SHIPTO, CUSTOMERcode, CUSTOMERdesc)
		printf("             GROUP = [(%s)%s]\n", GROUP, GROUPdesc)
		printf("            CENTRE = [(%s)%s]\n", CENTRE, CENTREdesc)
		printf("             GGRID = %s\n", GGRID)
		printf("         t21_GGRID = %s\n", t21_GGRID)
		printf("        CHILDCOUNT = %d\n", CHILDCOUNT)
		printf("           PROJECT = [(%s)%s]\n", PROJECT, PROJECTdesc)
		printf("             EGRID = %s\n", EGRID)
		printf("         t21_EGRID = %s\n", t21_EGRID)
		printf("               SID = %s\n", SID)
		printf("           t21_SID = %s\n", t21_SID)
		printf("          SUPPLIER = %s\n", SUPPLIER)
		printf("         INVOICENO = %s\n", INVOICENO)
		printf("            SUNDRY = %s\n", SUNDRY)
		printf("          REC_TYPE = %s\n", REC_TYPE)
		printf("           CHG_CAT = %s\n", CHG_CAT)
		printf("              Xtag = [%s]\n", Xtag)
		printf("        location   = [%s]\n", location)
		printf("        user       = [%s]\n", user)
		printf("        costcentre = [%s]\n", costcentre)
		printf("     reportgroupid = [%s]\n", reportgroupid)
		printf("   reportgroupdesc = [%s]\n", reportgroupdesc)
		printf("       reportorder = [%s]\n", reportorder)
		printf("       batchtypeid = [%s]\n", batchtypeid)
		printf("     batchtypedesc = [%s]\n", batchtypedesc)
		printf("        Xtottag    = [%s]\n", Xtottag)
		printf("        XRENT      = [%s]\n", XRENT)
		printf("        XCALL      = [%s]\n", XCALL)
		printf("        XOTHER     = [%s]\n", XOTHER)
		printf("        XADMIN     = [%s]\n", XADMIN)
		printf("        XTOTAL     = [%s]\n", XTOTAL)
		printf("        totalcount = [%s]\n", totalcount)
		printf("     totalduration = [%s]\n", totalduration)
		printf("         totalcost = [%s]\n", totalcost)
	}
	if ( printflag == 1 ) {
		printf("get_HIERIDx_info()\n");
		printf("%d|(%s)%s (%s)%s %s (%s)%s (%s)%s %s (%s)%s %s %s %s %s %s %s|%f|%f|%f|%f|TOTAL = %f\n", HIERIDxDUPcount, SHIPTO, CUSTOMERdesc, GROUP, GROUPdesc, GGRID, CENTRE, CENTREdesc, SERVTYPE, SERVTYPEdesc, EGRID, PROJECT, PROJECTdesc, SID, SUPPLIER, reportgroupid, invoiceno, sundry, CHG_CAT, XRENT, XCALL, XOTHER, XADMIN, totalcost)
	}
}


function getSERVTYPEdesc(servtype,sid,sundry,
batchtypeid,
batchtypedesc)
{
	servtypedesc = SERVTYPE_to_SERVTYPEdesc_arr[servtype]
	if ( servtype == 10 ) {
		batchtypeid = sid_sundry_to_batchtypeid_arr[sid "|" sundry]
                batchtypedesc = batchtypeid_to_batchtypedesc_arr[batchtypeid]
		servtypedesc = batchtypedesc
	}
	return servtypedesc
}


function test_grid1_belongs_to_grid2( grid1, grid2 )
{
	#print "    test_grid_belongs_to_grid(" grid1 "," grid2 ")"

	tstgrid = grid1

	#print "      testing tstgrid = [" tstgrid "]"
	#if ( tstgrid == grid2 )
	#	return 1

	while ( tstgrid != "" ) {
		#print "      testing tstgrid = [" tstgrid "]"
		if ( tstgrid == grid2 ) {
			#print "      matched tstgrid = [" tstgrid "]"
			return 1
		}
		tstgrid = grid_to_parentgroupid_arr[tstgrid]
	}

	#print "      NO MATCH for grid2 = [" grid2 "]"
	return 0
}


function test_exclude(servicetypeid, siteid, grid, extn)
{
	#print "test_exclude(" servicetypeid "," siteid "," grid "," extn ")"

	for ( exclude_tag in exclude_arr ) {
		#print "TEST exclude_arr[" exclude_tag "]=" exclude_arr[exclude_tag]

		do_exclude_servicetypeid = 0
		do_exclude_siteid = 0
		do_exclude_grid = 0
		do_exclude_extnlo = 0
		do_exclude_extnhi = 0
		do_exclude_extnrange = 0

		split(exclude_tag, exclude_entry_arr, "|")
		exclude_servicetypeid = exclude_entry_arr[1]
		exclude_siteid = exclude_entry_arr[2]
		exclude_grid = exclude_entry_arr[3]
		exclude_extnlo = exclude_entry_arr[4]
		exclude_extnhi = exclude_entry_arr[5]

		#print "      TEST exclude_servicetypeid=" "[" exclude_servicetypeid "]"
		#print "                  exclude_siteid=" "[" exclude_siteid "]"
		#print "                    exclude_grid=" "[" exclude_grid "]"
		#print "                  exclude_extnlo=" "[" exclude_extnlo "]" "  exclude_extnhi=" "[" exclude_extnhi "]"

		# not for this servicetype
		if ( exclude_servicetypeid != "" && exclude_servicetypeid == servicetypeid ) {
			print "      doexclude servicetype=" servicetype
			do_exclude_servicetypeid = 1
		}

		# Exclude if belongs to siteid exclude
		if ( exclude_siteid != "" && exclude_siteid == siteid ) {
			print "      doexclude siteid=" siteid
			do_exclude_siteid = 1
		}

		# Exclude if belongs to grid exclude
		if ( exclude_grid != "" && test_grid1_belongs_to_grid2(grid, exclude_grid) ) {
			print "      doexclude grid=" grid
			do_exclude_grid = 1
		}

		# Exclude if belongs to extn range excude
		excludeextn = 0
		if ( exclude_extnlo != "" && exclude_extnhi == "" && extn == exclude_extnlo ) {
			print "      doexclude exclude_extnlo=" exclude_extnlo
			do_exclude_extnlo = 1
		}
		if ( exclude_extnlo == "" && exclude_extnhi != "" && extn == exclude_extnhi ) {
			print "      doexclude exclude_extnhi=" exclude_extnhi
			do_exclude_extnhi = 1
		}
		if ( exclude_extnlo != "" && exclude_extnhi != "" && extn >= exclude_extnlo && extn <= exclude_extnhi ) {
			print "      doexclude extn=" extn "   BETWEEN " exclude_extnlo " and " exclude_extnhi
			do_exclude_extnrange = 1
		}

		#print " do_exclude_servicetypeid=" "[" do_exclude_servicetypeid "]"
		#print "        do_exclude_siteid=" "[" do_exclude_siteid "]"
		#print "          do_exclude_grid=" "[" do_exclude_grid "]"
		#print "        do_exclude_extnlo=" "[" do_exclude_extnlo "]"
		#print "        do_exclude_extnhi=" "[" do_exclude_extnhi "]"
		#print "     do_exclude_extnrange=" "[" do_exclude_extnrange "]"

		# if more than one condition in the same row is specified
		# then AND applies
		if ( (exclude_servicetypeid == "" || do_exclude_servicetypeid) && (exclude_siteid == "" || do_exclude_siteid) && (exclude_grid == "" || do_exclude_grid) && ((exclude_extnlo == "" || exclude_extnhi != "" ) || do_exclude_extnlo) && ((exclude_extnhi == "" || exclude_extnlo != "" ) || do_exclude_extnhi) && ((exclude_extnlo == "" && excludeextnhi == "") || do_exclude_extnrange)) {
			print "  DOEXCLUDE test_exclude(" servicetypeid "," siteid "," grid "," extn ")    exclude_arr[" exclude_tag "]=" exclude_arr[exclude_tag]
			return 1
		}
	}

	return 0
}

#----------------------------------------------------------
#----------------------------------------------------------

function get_ggname(gggrid) {
	# get group name
	ggname = grid_to_grname3_arr[gggrid]
	#printf("get_ggname(): gggrid = %s  ggname = %s\n", gggrid, ggname)
	return
}


function get_egname(eggrid) {
	# get group name
	egname = grid_to_grname3_arr[eggrid]
	egccp = grid_to_ccp_arr[eggrid]
	#printf("get_egname(): eggrid = %s  egname = %s  egccp = %s\n", eggrid, egname, egccp)
	return
}


function get_dirname(siteid,eggrid,extn) {
	# get name from directory
	#itag = eggrid "|" extn
	itag = extn
	t21surname = dirextn_to_t21surname_arr[itag]
	t21firstname = dirextn_to_t21firstname_arr[itag]
	#printf("get_dirname(" siteid "," eggrid "," extn "): eggrid=%s  extn=%s  t21surname = %s  t21firstname = %s\n", eggrid, extn, t21surname, t21firstname)
	t21name = dirextn_to_t21name_arr[itag]
	if ( t21name == "_blank_" )
		t21name = ""
	if ( t21surname == "_blank_" )
		t21surname = ""
	if ( t21firstname == "_blank_" )
		t21firstname = ""
	#printf("get_dirname(): siteid = %d  eggrid= %s extn = %s  t21name = %s\n", siteid, eggrid, extn, t21name)
	return t21name
}


function get_t21_upper_info(gggrid, eggrid)
{
	t21_SERVTYPE = SERVTYPE
	t21_SERVTYPEdesc = SERVTYPE_to_SERVTYPEdesc_arr[t21_SERVTYPE]

	#t21_CENTRE = gggrid
	t21_CENTRE = grid_to_centre_arr[eggrid]
	t21_EGRID = eggrid

	t21_CENTREdesc1 = CENTRE_to_CENTREdesc1_arr[t21_CENTRE]
	t21_CENTREdesc2 = CENTRE_to_CENTREdesc2_arr[t21_CENTRE]
	t21_CENTREdesc3 = CENTRE_to_CENTREdesc3_arr[t21_CENTRE]
	t21_CENTREdesc = CENTRE_to_CENTREdesc_arr[t21_CENTRE]

	t21_PROJECT = grid_to_project_arr[t21_EGRID]
	t21_PROJECTdesc = CENTREPROJECT_to_PROJECTdesc3_arr[t21_CENTRE "|" t21_PROJECT]
	t21_CCPdesc = CENTREPROJECT_to_PROJECTdesc_arr[t21_CENTRE "|" t21_PROJECT]


	# rjs10
	#t21_PROJECTdesc = grid_to_grname3_arr[t21_EGRID]
	#t21_CCPdesc = grid_to_grname2_arr[t21_EGRID]
	## rjs9
	#pargrid = grid_to_parentgroupid_arr[t21_EGRID]
	#t21_PROJECT = grid_to_project_arr[pargrid]
	#t21_PROJECTdesc = grid_to_grname3_arr[pargrid]
	#t21_CCPdesc = grid_to_grname2_arr[pargrid]

	## rjs9-
	## dept
	#t21_DEPT = t21_CENTRE
	#t21_DEPTdesc = t21_CENTREdesc
	## group 
	#t21_GROUP = grid_to_parentgroupid_arr[t21_DEPT]
	#t21_GROUPdesc = grid_to_grname3_arr[t21_GROUP]

	## shipto
	#t21_SHIPTO = CENTRE_to_ShipTo_arr[t21_CENTRE]
	#t21_ShipToDesc = ShipTo_to_ShipToDesc_arr[t21_SHIPTO]
	#t21_CustomerDesc = ShipTo_to_CustomerDesc_arr[t21_SHIPTO]

	printf("get_t21_upper_info(%s,%s): %s|%s|(%s) %s|(%s) %s\n", gggrid, eggrid, t21_EGRID, serviceprovider, t21_CENTRE, t21_CENTREdesc, t21_PROJECT, t21_PROJECTdesc)
	#printf("  %s|%s|(%s)%s|(%s)%s|(%s) %s,%s\n", SID, t21_supplier, t21_GROUP, t21_GROUPdesc, t21_DEPT, t21_DEPTdesc, t21_SHIPTO, t21_ShipToDesc, t21_CustomerDesc)
	#printf("  %s|%s\n, SID, t21_supplier)

	# -rjs9
}


#----------------------------------------------------------


function spit_header_to_CUSTOMERIMPORTfile()
{
	print "spit_header_to_CUSTOMERIMPORTfile() - " CUSTOMERIMPORT_fname
	printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", "Billing_Period", "CustomerName", "ShipTo", "SE_Description", "department", "ServiceID", "invoiceno", "sundry", "supplier", "location", "user", "centre", "activity", "element", "subledger", "paytype", "rentcost:Float", "callcost:Float", "othercost:Float", "admincost:Float",  "totalcost:Float") >CUSTOMERIMPORT_fname
}


function spit_SIDtotals_to_CUSTOMERIMPORTfile(servtype,shipto,group,centre,project,sid,supplier,invoiceno,sundry)
{
	print "spit_SIDtotals_to_CUSTOMERIMPORTfile(" servtype "," shipto "," group "," centre "," project "," sid "," supplier "," invoiceno "," sundry ")"

	print "HIERIDx=" HIERIDx

	Wtag = "TOTALTBS" "|" servtype "|" shipto "|" group "|" centre "|" project "|" sid "|" supplier "|" invoiceno "|" sundry

	# skip All totals
	if ( invoiceno == "All" ) {
		print "skip"
		return
	}
	if ( sundry == "All" || supplier == "All" ) {
		print "skip"
		return
	}

	print "XRENT = " XRENT "  XCALL = " XCALL "  XOTHER = " XOTHER "  XADMIN = " XADMIN

	totalcost = totalcost_arr[Wtag "|All"]
	print "  CUSTOMERIMPORT Wtag|All=" Wtag "|All  totalcost=" totalcost

	set_project_displayinfo(centre, project)

	print "        costcentre = [" costcentre "]"

	# CCCCCC AA EEEEEE SSSSSS P
	# 123456789012345678901234567
	#          1         2
	Xcentre = trim(substr(costcentre,1,6))
	Xactivity = trim(substr(costcentre,8,2))
	Xelement = trim(substr(costcentre,11,6))
	Xsubledger = trim(substr(costcentre,18,6))
	Xpaytype = trim(substr(costcentre,25,1))

	print "Xcentre=[" Xcentre "]"
	print "Xactivity=[" Xactivity "]"
	print "Xelement=[" Xelement "]"
	print "Xsubledger=[" Xsubledger "]"
	print "Xpaytype=[" Xpaytype "]"

	if ( invoiceno == "VRT999999" ) {
		extbsbillperiod = exbillperiod
		print "  extbsbillperiod=[" extbsbillperiod "]"
	}
	else {
		htag = SHIPTO "|" invoiceno "|" sundry
		tbsbillperiod = parent_invoicenumber_sundry_to_period_arr[htag]
		yyyy = 0 + substr(tbsbillperiod,1,4)
		mm = 0 + substr(tbsbillperiod,6,2)
		# expanded bill tbsbillperiod
		extbsbillperiod = sprintf("%s %d", mnthstr[mm], yyyy)
		print "  htag=[" htag "]" "  tbsbillperiod=[" tbsbillperiod "]" "  yyyy=[" yyyy "]" "  mm=[" mm "]" "  extbsbillperiod=[" extbsbillperiod "]"
	}

	csvstr = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%0.2f,%0.2f,%0.2f,%0.2f\n", extbsbillperiod, ShipTo_to_ShipToDesc_arr[SHIPTO], SHIPTO, SERVTYPEdesc, CENTRE_to_CENTREdesc_arr[CENTRE], sid, invoiceno, sundry, supplier, location, user, Xcentre, Xactivity, Xelement, Xsubledger, Xpaytype,  XRENT, XCALL, XOTHER, XADMIN, totalcost)

	printf("%s", csvstr)
	printf("%s", csvstr) >>CUSTOMERIMPORT_fname
}



#####################################

function spit_totals_csvdata()
{
	if ( last_SERVTYPE == 10 ) {
		#totcsvfile = totalscsvfile "_" last_SERVTYPE ".csv"
		totcsvfile = totalscsvfile "_" "mobiles" ".csv"
	}
	else {
		totcsvfile = totalscsvfile ".csv"
	}

	
	# rjs12
	#Xgroup = grid_to_parentgroupid_arr[last_CENTRE]

	Xsid = "All"
	Xsupplier = "All"
	Xcat = "All"

	one_line_per_servicetype = 0
	if ( last_SERVTYPE == 10 )
		one_line_per_servicetype = 1

	if ( one_line_per_servicetype ) {
		Xtotalcost = totalcost_arr["TOTALTBS" "|" last_SHIPTO "|" last_GROUP "|" last_SERVTYPE "|" Xgroup "|" last_CENTRE "|" last_PROJECT "|" Xsid "|" Xsupplier "|" Xcat]
		if ( Xtotalcost != 0 ) {
			print "spit_totals_csvdata() - totcsvfile=" totcsvfile " - for " last_SERVTYPE "  " last_SHIPTO "  " last_GROUP "  " last_CENTRE "  " last_PROJECT "  " totalcost_arr["TOTALTBS" "|" last_SERVTYPE "|" last_CENTRE "|" last_PROJECT "|" Xsid "|" Xsupplier "|" Xcat]

			printf("%s,%s,%s,%s,%s,%s,%s,%0.2f\r\n", SERVTYPE_to_SERVTYPEdesc_arr[last_SERVTYPE], CENTRE_to_CENTEdesc_arr[last_CENTRE], CENEPROJECT_to_PROJECTdesc3_arr[last_CENTRE "|" last_PROJECT], CENTREPROJECT_to_PROJECTdesc2_arr[last_CENTRE "|" last_PROJECT], CENTREPROJECT_to_PROJECTdesc3_arr[last_CENTRE "|" last_PROJECT], last_GGRID, last_EGRID, mround2(Xtotalcost)) >>totcsvfile
		}
	}
	else {
		# totals for servicetypes in separate cols
		if ( done_CENTRE_PROJECT_totcsv_arr[last_CENTRE "|" last_PROJECT] != 1 ) {
			done_CENTRE_PROJECT_totcsv_arr[last_CENTRE "|" last_PROJECT] = 1
			tot_overheads_cost = 0 + totalcost_arr["TOTALTBS" "|" "4" "|" Xshipto "|" Xgroup "|" last_CENTRE "|" last_PROJECT "|" "All" "|" "All" "|" "All"]
			tot_calls_cost = 0 + totalcost_arr["TOTALTBS" "|" "1" "|" Xshipto "|" Xgroup "|" last_CENTRE "|" last_PROJECT "|" "All" "|" "All" "|" "All"]
			tot_misc_cost = 0 + totalcost_arr["TOTALTBS" "|" "3" "|" Xshipto "|" Xgroup "|" last_CENTRE "|" last_PROJECT "|" "All" "|" "All" "|" "All"]
			#tot_cost = 0 + totalcost_arr["TOTALTBS" "|" "All" "|" Xshipto "|" Xgroup "|" last_CENTRE "|" last_PROJECT "|" "All" "|" "All" "|" "All"]
			Xtotalcost = tot_overheads_cost + tot_calls_cost + tot_misc_cost
			if ( Xtotalcost != 0 ) {
				print "spit_totals_csvdata() - " totcsvfile " - for " last_CENTRE "  " last_PROJECT "  " tot_overheads_cost "  " tot_calls_cost "  " tot_misc_cost "  " Xtotalcost

				# cost for ovheads/calls/misc and total
				printf("%s,%s,%s,%s,%s,%0.2f,%0.2f,%0.2f,%0.2f\r\n", CENTRE_to_CENTREdesc_arr[last_CENTRE], CENTREPROJECT_to_PROJECTdesc3_arr[last_CENTRE "|" last_PROJECT], CENTREPROJECT_to_PROJECTdesc2_arr[last_CENTRE "|" last_PROJECT], last_GGRID, last_EGRID, mround2(tot_overheads_cost), mround2(tot_calls_cost), mround2(tot_misc_cost), mround2(Xtotalcost)) >>totcsvfile
			}
		}

	}
}

###-------------

function spit_totals_rep_all()
{
	print "spit_totals_rep_all() - " totalsrepfile
	printf("\r\n\r\n") >>totalsrepfile
	printf("\r\n\r\n") >>totalsrepfile
	Xtag = "TOTALTBS" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All"
	printf("             Grand Total                                   $%9.2f\r\n", mround2(totalcost_arr[Xtag])) >>totalsrepfile
	printf("\r\n\r\n") >>totalsrepfile
	close(totalsrepfile)
}

function spit_totals_rep_servtype()
{
	print "spit_totals_rep_servtype() - " totalsrepfile
	printf("\r\n\r\n") >>totalsrepfile
	Xtag = "TOTALTBS" "|" last_SERVTYPE "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All"
	printf("    %-40.40s   Total        $%9.2f\r\n", SERVTYPE_to_SERVTYPEdesc_arr[last_SERVTYPE], mround2(totalcost_arr[Xtag])) >>totalsrepfile
	printf("\r\n\r\n") >>totalsrepfile
	printf("\r\n\r\n") >>totalsrepfile
	close(totalsrepfile)
}


function spit_totals_rep_centre()
{
	print "spit_totals_rep_centre() - " totalsrepfile
	Xtag = "TOTALTBS" "|" last_SERVTYPE "|" last_SHIPTO "|" last_GROUP "|" last_CENTRE "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All"
	printf("%-3.3s %-8.8s Total - %-40.40s   $%9.2f\r\n", last_SHIPTO, last_GGRID, CENTRE_to_CENTREdesc_arr[last_CENTRE], mround2(totalcost_arr[Xtag])) >>totalsrepfile
	printf("\r\n\r\n") >>totalsrepfile
	close(totalsrepfile)
}


function spit_totals_rep_project()
{
	print "spit_totals_rep_project() - " totalsrepfile
	Xtag = "TOTALTBS" "|" last_SERVTYPE "|" last_SHIPTO "|" last_GROUP "|" last_CENTRE "|" last_PROJECT "|" "All" "|" "All" "|" "All" "|" "All" "|" "All"
	printf("%-3.3s %-8.8s %-8.8s  %-40.40s $%9.2f\r\n", last_SHIPTO, last_GGRID, last_EGRID, CENTREPROJECT_to_PROJECTdesc_arr[last_CENTRE "|" last_PROJECT], mround2(totalcost_arr[Xtag])) >>totalsrepfile
	close(totalsrepfile)
}



#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------




# Mobile Call Details
#####################################################
#####################################################
#####################################################
#####################################################

function create_mobile_detail_file()
{
	if ( testmobdet == 1 || retroBDL != "1" ) {
		if ( mobile_fname != "" ) {	# close previous file
			close(mobile_fname)
			mobile_fname = ""
		}
	}
	mobile_fname = sprintf("topdat/%s/data/mobile%s/mobile_%s_%s_%s_%s.csv", monthdir, SERVTYPE, CENTRE, PROJECT, SERVTYPE, SID)
	fname_to_SERVTYPECENTREPROJECT_arr[mobile_fname] = SERVTYPE "|" CENTRE "|" PROJECT
	if ( testmobdet == 0 && retroBDL == "1" )
		return
	printf(">>>            mobile_fname = %s\n", mobile_fname)
	if ( (getline x < mobile_fname) < 0 ) {
		# create file and csv header
		newdir = sprintf("topdat/%s/data/mobile%s", monthdir, SERVTYPE)
		cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p %s", newdir, newdir)
		#print "system 3 [" cmd "]"
		system(cmd)

		CCPdesc = CENTREPROJECT_to_PROJECTdesc_arr[CENTRE "|" PROJECT]
		PROJECTdesc = CENTREPROJECT_to_PROJECTdesc3_arr[CENTRE "|" PROJECT]

		printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,", "ServiceID Detail", exbillperiod, "SDATE", "EDATE", CENTRE_to_CENTREdesc_arr[CENTRE], CENTRE, getSERVTYPEdesc(SERVTYPE,SID,sundry), SERVTYPE, PROJECTdesc, PROJECT, SID, serviceprovider, mobname "|" mobmanufacturer "|" mobmodel) >mobile_fname
		printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", "RecNo", "RecType", "ChargeCategory", "RecClass", "Date","Time","Origin","Dialled Number","Description","Duration","Cost:Float") >>mobile_fname
	}
	else {
		printf("WARNING %d: mobile_fname = %s exists.\n", NR, mobile_fname)
	}
}


function spit_mdrrec_tomobilefile()
{
	# append mobile call record data to mobile file

	if ( testmobdet == 1 || retroBDL != "1" ) {
		csvstr = sprintf(",,,,,,,,,,,,,%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%0.4f\n", mob_recno, mob_rectype, mob_cat "|" onnettype, mob_recclass, date, ctimestr, mob_origin, expdialledno, mob_destination, mob_durstr, mround4(mob_cost))

		#printf("%s", csvstr)
		printf("%s", csvstr) >>mobile_fname
	}

}


#################################################################

function fix_vt_mob_recclass(vt_mob_recclass)
{
	split(vt_mob_recclass, vt_mob_recclass_arr, "~")
	TransactionType_Desc = vt_mob_recclass_arr[1]
	RateDesc_Desc = vt_mob_recclass_arr[2]

	tmp_recclass = RateDesc_Desc
	if ( mob_carrier == "OPTUS" ) {
		tmp_recclass = TransactionType_Desc
		if ( match(RateDesc_Desc,/^Billed Messages/) > 0 )
			tmp_recclass = "Billed Messages"
		if ( match(RateDesc_Desc,/^Billed Events/) > 0 )
			tmp_recclass = "Billed Events"
		if ( match(RateDesc_Desc,/^Variable Rates/) > 0 )
			tmp_recclass = "Variable Rates"
		if ( match(RateDesc_Desc,/^Div-VoiceMail/) > 0 )
			tmp_recclass = "Div-VoiceMail"
		if ( match(RateDesc_Desc,/^Voicemail/) > 0 )
			tmp_recclass = "Voicemail"
		if ( match(RateDesc_Desc,/^Content Services/) > 0 )
			tmp_recclass = "Content Services"
		if ( match(RateDesc_Desc,/^informatel/) > 0 )
			tmp_recclass = "informatel"
		if ( match(RateDesc_Desc,/^Sensis 1234/) > 0 )
			tmp_recclass = "Sensis 1234"
		if ( match(RateDesc_Desc,/^Mobile All Call Types/) > 0 )
			tmp_recclass = "MOBCL M"
		if ( match(RateDesc_Desc,/^Mobile National Direct/) > 0 )
			tmp_recclass = "MOBCL M"
	}
	if ( mob_carrier == "TELSTRA" ) {
		if ( match(TransactionTypeDesc_Desc,/^CALLS MADE O\/S -DATA(GST FREE)/) > 0 )
			tmp_recclass = "CALLS MADE O/S -DATA(GST FREE)"
		gsub(/SESSIONS - .*$/,"SESSIONS", tmp_recclass)
		gsub(/ - .* ALERT$/,"", tmp_recclass)
		gsub(/ - .* MSGS$/,"", tmp_recclass)
		gsub(/ - .* ONDEMAN$/,"", tmp_recclass)
		gsub(/ - .* ITEM.*$/,"", tmp_recclass)
		gsub(/ - F$/,"", tmp_recclass)
		gsub(/ - O$/,"", tmp_recclass)
		gsub(/ - P$/,"", tmp_recclass)
		gsub(/ - S$/,"", tmp_recclass)
		gsub(/ - S O$/,"", tmp_recclass)
		if ( match(RateDesc_Desc,/^Mobile Originated SMS/) > 0 )
			tmp_recclass = "MOBILE ORIGINATED SMS"
		if ( match(RateDesc_Desc,/^Mobile Call Forwarding/) > 0 )
			tmp_recclass = "CALL FORWARDING CHARGES"
	}
	if ( mob_carrier == "VODAFONE" ) {
		split(tmp_recclass, vo_recclass_arr, " ")
		tmp_recclass = vo_recclass_arr[1]
		if ( match(RateDesc_Desc,/^Airtime Credit/) > 0 )
			tmp_recclass = "AIRTIME CREDIT"
	}
	if ( tmp_recclass == "" )
		tmp_recclass = TransactionType_Desc
	return tmp_recclass
}


#################################################################


function domobdetfile(mobcallsfile,servtype)
{
	printf("domobdet(%s)  servtype=%s\n", mobcallsfile, servtype)

	system("date")

	CENTRE = ""
	PROJECT = ""
	SID = ""

	mobile_fname = ""

	SERVTYPE = servtype
	SERVTYPEdesc = SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]
	print "SERVTYPE = " SERVTYPE "  SERVTYPEdesc = " SERVTYPEdesc

	last_gggrid = "First Time"
	last_eggrid = "First Time"
	last_extn = -1
	last_siteid = -1

	last_SERVTYPE = ""
	last_SHIPTO = ""
	last_GROUP = ""
	last_CENTRE = ""
	last_PROJECT = ""
	last_SID = ""
	last_directoryID = ""

	# rjs9
	#if ( substr(last_SID,5,1) == "-" )
	#	last_mobile = substr(last_SID,1,4) substr(last_SID,6)
	#else
	#	last_mobile = last_SID
	#printf("   last_mobile=[%s]\n", last_mobile)


	while ( (getline mobline < mobcallsfile) > 0 ) {
                #replace "," with ";"
                gsub(",",";",mobline)

		#print "MOBDET mobline = " mobline
		split(mobline, a_arr, "|")
		f = 0
		mob_batchtypeid = clip(a_arr[++f])
		mob_sundry = clip(a_arr[++f])
		mob_mobile = clip(a_arr[++f])
		mob_carrier = toupper(clip(a_arr[++f]))
		mob_rectype = clip(a_arr[++f])
		mob_recclass = clip(a_arr[++f])
		mob_billperiod = clip(a_arr[++f])
		mob_cdate = clip(a_arr[++f])
		mob_ctime = clip(a_arr[++f])
		mob_origin = clip(a_arr[++f])
		mob_destination = clip(a_arr[++f])
		mob_dialled = clip(a_arr[++f])
		mob_rate = clip(a_arr[++f])
		mob_duration = clip(a_arr[++f])
		mob_cost = clip(a_arr[++f])
		mob_newcost = clip(a_arr[++f])

		if ( mob_newcost != "" ) {
			mob_carrier_cost = mob_cost
			mob_cost = mob_newcost
		}

		# skip error recs
		if ( mob_rectype == "ERROR" ) {
			continue
		}

		# skip config validation recs
		if ( mob_rectype == "CONFIG" ) {
			continue
		}

		# skip invoice grand total rec
		if ( mob_rectype == "CHARGE_DESC_INFO" && mob_recclass == "TOTAL" ) {
			continue
		}

		# rjs9
		supplier = mob_carrier
		#supplier = t21_supplier " " supplier

		carriercode = mobile_to_mob_carriercode_arr[mob_mobile]
		#print "rjs mc: mob_mobile=[" mob_mobile "]" "  carriercode=[" carriercode "]"
		if ( carriercode == "" ) {
			if ( carriercode_err_arr[mob_mobile] == "" ) {
				if ( done_warn["carriercode:" mob_carrier] == "" ) {
					#print "ERROR: can't get carriercode for mob_mobile=[" mob_mobile "]" " ufid=[" mob_sundry "]" " carrier=[" mob_carrier "]"
					print "ERROR: can't get carriercode for mob_mobile=[" mob_mobile "]" " ufid=[" mob_sundry "]" " carrier=[" mob_carrier "]"
					done_warn["carriercode:" mob_carrier] = "1"
				}
				carriercode_err_arr[mob_mobile] = "1"

				# default lower cased first char of carrier
				carriercode = tolower(substr(mob_carrier,1,1))
				mobile_to_mob_carriercode_arr[mob_mobile] = carriercode
			}
			#continue
		}

		vt_mob_recclass = mob_recclass
		if ( mob_rectype == "CDR" ) {
			split(vt_mob_recclass, vt_mob_recclass_arr, "~")
			TransactionType_Desc = vt_mob_recclass_arr[1]
			RateDesc_Desc = vt_mob_recclass_arr[2]
			mob_recclass = fix_vt_mob_recclass(vt_mob_recclass)
		}

		Ctag = carriercode "|" mob_rectype "|" mob_recclass
		if ( mob_recclass == "X" ) {	# dummy cdr from config
			mob_cat = "X"
		}
		else {
			mob_cat = carriercode_chargeclasstype_to_chargecat_arr[Ctag]
		}
		if ( mob_cat == "" ) {
			mob_cat = "Mobile Misc"
			if ( done_warn["mob_cat_misc:" Ctag] == "" ) {
				print "WARNING: default mobile charge category " mob_cat " set for Ctag=[" Ctag "]" "  supplier = [" supplier "]" "  mob_cost = [" mob_cost "]" "  mob_mobile = [" mob_mobile "]" "  mob_recclass=[" mob_recclass "]" "  vt_mob_recclass=[" vt_mob_recclass "]"
				done_warn["mob_cat_misc:" Ctag] = "1"
			}
		}
		#print "mobcat: " mob_cat " set for Ctag=[" Ctag "]" "  supplier = [" supplier "]" "  mob_cost = [" mob_cost "]" "  mob_mobile = [" mob_mobile "]" "  mob_recclass=[" mob_recclass "]"
		#     "  vt_mob_recclass=[" vt_mob_recclass "]"

		rec_type = "MOBILE_" mob_rectype
		chg_cat = mob_cat
		if ( chg_cat == "X" )	# dummy categroy
			chg_count = 0
		else
			chg_count = 1
		chg_duration = mob_duration
		chg_cost = mob_cost

		#print "rec_type=[" rec_type "]  mob_recclass=[" mob_recclass "]"
		mobSERVTYPE = SERVTYPE

		mobSID = substr(mob_mobile,1,4) "-" substr(mob_mobile,5)

		mobSERVTYPEdesc = getSERVTYPEdesc(SERVTYPE,mobSID,mob_sundry)

		mobmanufacturer = mobile_to_manufacturer_arr[mob_mobile]
		mobmodel = mobile_to_model_arr[mob_mobile]

		#-----

		mobdirectoryID = mobile_to_directoryID_arr[mob_mobile]
		#print "mobile_to_directoryID_arr[" mob_mobile "] = [" mobile_to_directoryID_arr[mob_mobile] "]"

		# group assigned
		eggrid = mobile_to_grid_arr[mob_mobile]
		#print "mobile_to_grid_arr[" mob_mobile "]=[" mobile_to_grid_arr[mob_mobile] "]"
		if ( eggrid == "" ) {
			#personal assigned
			# rjs9 -  eggrid is mob sid 
			#eggrid = recordno_to_eggrid_arr[mobdirectoryID]
			#print "recordno_to_eggrid_arr[" mobdirectoryID "]=[" recordno_to_eggrid_arr[mobdirectoryID] "]"
			eggrid = mobSID
			if ( Testing >= 2 && eggrid != "" )
				print "PERSONAL ASSIGNED eggrid = [" eggrid "]"
		}
		else {
			if ( Testing >= 2 && eggrid != "" )
				print "GROUP ASSIGNED mobile_to_grid_arr[" mob_mobile "] = [" mobile_to_grid_arr[mob_mobile] "]"
		}

		if ( eggrid == "" ) {
			if ( done_warn["mob_not_assigned:" mob_mobile] == "" ){
				print "WARNING: mobile [" mob_mobile "] not assigned to directoryID or groupID"
				done_warn["mob_not_assigned:" mob_mobile] = "1"
			}
			eggrid = "UN_MOB"
			#continue
		}

		gggrid = grid_to_parentgroupid_arr[eggrid]
		#print "grid_to_parentgroupid_arr[" eggrid "] = [" grid_to_parentgroupid_arr[eggrid] "]"

		mobCENTRE = grid_to_centre_arr[eggrid]
		#print "grid_to_centre_arr[" eggrid "]=[" grid_to_centre_arr[eggrid] "]"
		mobPROJECT = grid_to_project_arr[eggrid]
		#print "grid_to_project_arr[" eggrid "]=[" grid_to_project_arr[eggrid] "]"
		## rjs9
		#mobCENTRE = grid_to_centre_arr[gggrid]
		#mobPROJECT = grid_to_project_arr[gggrid]

		# now set in get_mobileinfo()
		#extn = recordno_to_extn_arr[mobdirectoryID]
		##print "recordno_to_extn_arr[" mobdirectoryID "] = [" recordno_to_extn_arr[mobdirectoryID] "]"

		#print "MOBDETmobline = " mobline
		#printf("MOBDET: %s|%s|%s|%s|%s|%s|%s|%s|%s|%d|%0.4f\n", mobCENTRE, mobPROJECT, mobdirectoryID, mobSERVTYPE, mob_sundry, mobSID, mob_carrier, mob_cdate, mob_ctime, mob_duration, mround4(mob_cost))

		serviceprovider = mob_carrier
		#printf("   serviceprovider=%s\n", serviceprovider)

		######################

		if ( mobSID <= 0 ) {
			if ( done_warn["bad_mob:" mobSID] == "" ) {
				print "WARNING: MOBILE SKIPPING mobSID = " mobSID
				done_warn["bad_mob:" mobSID] = "1"
			}
			continue
		}

		# set servicetype
		SERVTYPE = servtype
		SERVTYPEdesc = getSERVTYPEdesc(SERVTYPE,mobSID)
		#print "SERVTYPE = " SERVTYPE "  SERVTYPEdesc = " SERVTYPEdesc

		if ( gggrid == "" )
			gggrid="XXX"

		if ( eggrid == "" )
			eggrid = "YYY"

		t21_GGRID = gggrid
		GGRID = t21_GGRID

		t21_EGRID = eggrid
		EGRID = t21_EGRID

		CENTRE = mobCENTRE
		PROJECT = mobPROJECT

		# get mobile info (name, extn and siteid)
		get_mobileinfo(0,SERVTYPE,mobSID,CENTRE)
		extn = t21_extn

		SID = mobSID

		#if ( SID != last_SID ) {
		#	print "Do mobdetfile eggrid = " eggrid "  CENTRE = " CENTRE "  PROJECT = "  PROJECT "  SID = " SID
		#}

		######################################
		# test if grids from summary data not in hierarchy
		if ( CENTRE == ""  || PROJECT == "" ) {
			print "WARNING: MOBILE SKIPPING BAD CENTRE/PROJECT" " eggrid = " eggrid "  CENTRE = " CENTRE "  PROJECT = "  PROJECT "  SID = " SID
			continue
		}

		# only doing thisCENTRE
		if ( thisCENTRE != "all" && gggrid != thisCENTRE )
			continue

		######################################
		SHIPTO = CENTRE_to_ShipTo_arr[CENTRE]
		GROUP = grid_to_parentgroupid_arr[CENTRE]
		#print "SHIPTO=[" SHIPTO "]" "  GROUP=[" GROUP "]"

		######################################
		if (SERVTYPE == "" || SHIPTO == "" || GROUP == "" || CENTRE == "" || PROJECT == "" || SID == "") {
			#print "WARNING: ERROR: MOBILE BAD HIERIDx = " HIERIDx
			print "WARNING: ERROR: MOBILE BAD"
			print "           SERVTYPE = " SERVTYPE
			print "             SHIPTO = " SHIPTO
			print "              GROUP = " GROUP
			print "             CENTRE = " CENTRE
			print "            PROJECT = " PROJECT
			print "                SID = " SID
			continue
		}


		######################

		# Testing
	        #if ( Testing >= 3 && index("|ANN|VLP|VLS|VLO|VLI|VNA|VTC|VNE|VSE|VSW|VRT|", "|" SHIPTO "|") <= 0 ) {
	        if ( Testing >= 3 && index("|A|D|K|S|V|", "|" substr(SHIPTO,1,1) "|") <= 0 ) {
			print "    TESTING... SKIPPING  SHIPTO=[" SHIPTO "]" "  gggrid=[" gggrid "]" "  eggrid=[" eggrid "]"
			continue
		}

		######################

		sundry = mob_sundry
		if ( sundry == 0 ) {
			if ( done_warn["bad_mob_sundry:" SID] == "" ) {
				print "DOMOBDETWARN: using SID_to_sundry_arr[" SID "]=[" SID_to_sundry_arr[SID] "]"
				done_warn["bad_mob_sundry:" SID] = "1"
			}			
			sundry = SID_to_sundry_arr[SID]
		}


		tascode = sundryEGRID_to_tascode_arr[sundry "|" eggrid]
		if ( tascode == "" ) {
			if ( done_warn["bad_mob_tascode:" SID] == "") {
				print "DOMOBDETWARN: using SID_to_tascode_arr[" SID "]=[" SID_to_tascode_arr[SID] "]"
				done_warn["bad_mob_tascode:" SID] = "1"
			}
			tascode = SID_to_tascode_arr[SID]
		}

		invoiceno = sundryEGRID_to_invoiceno_arr[sundry "|" eggrid]
		if ( invoiceno == "" ) {
			if ( done_warn["bad_mob_invoiceno:" SID] == "") {
				print "DOMOBDETWARN: using SID_to_invoiceno_arr[" SID "]=[" SID_to_invoiceno_arr[SID] "]"
				done_warn["bad_mob_invoiceno:" SID] = "1"
			}
			invoiceno = SID_to_invoiceno_arr[SID]
		}

		# rjs9 - called also in ld_cde()
		set_HIERIDx("t21mob", SERVTYPE,SHIPTO,GROUP,CENTRE,PROJECT,supplier,invoiceno,sundry,SID,rec_type,chg_cat)

		# now set in get_mobileinfo
		#get_t21info(SERVTYPE,t21_EGRID,extn,CENTRE)

		######################################
		# exclude data
		if ( test_exclude(SERVTYPE, siteid, eggrid, extn) )
			continue

		######################################
		#printf("mobline=[%s]\n", mobline)

		#handle_mobile_change()

		# new mobile
		if ( SID != last_SID || eggrid != last_eggrid || gggrid != last_gggrid ) {
			# rjs9
			CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" SID] = EGRID

			printf("T21 new mobile: eggrid=[%s]  SID=[%s]  mobname=[%s]  extn=[%s] t21name=[%s]\n", eggrid, SID, mobname, extn, t21name)
			if ( !dot21verify ) {
				create_mobile_detail_file()
			}
		}

		date = fixdate(mob_cdate)
		endtime = mob_ctime

		if (mob_duration == "") 
			mob_duration = 0

		durhh = int(mob_duration / 3600)
		remd = mob_duration - (durhh * 3600)
		durmm = int(remd / 60)
		remd = remd - (durmm * 60)
		durss = remd
		mob_durstr = sprintf("%03d:%02d:%02d", durhh, durmm, durss)

		hh = substr(endtime,1,2)
		mm = substr(endtime,3,2)
		ss = substr(endtime,5,2)
		ctimestr = sprintf("%2s:%2s:%2s",hh, mm, ss)

		# set dialled/description info
		if ( mob_dialled == mob_destination )
			mob_destination = ""
		# remove dialled number from start of destination
		if ( mob_dialled != "" ) {
			#sub(mob_dialled,"",mob_destination)
			if ( substr(mob_destination,1,length(mob_dialled)) == mob_dialled )
				mob_destination = substr(mob_destination,length(mob_dialled)+1)
		}
		# dialno_desc = sprintf("%s", mob_dialled)
		# if ( mob_destination != "" )
		# 	dialno_desc = sprintf("%s %s", dialno_desc, mob_destination)

		dialledno = mob_dialled
		#printf("noddsuparr[%s] = %d\n", gggrid, noddsuparr[gggrid])
		if ( 1 || noddsuparr[gggrid] == 0 ) {	# group has NO digit suppression
			expdialledno = dialledno
		}
		else {
			# xx for last 2 digits in dialledno if length >= 0
			## supress for these callcategories only
			#if ( index("JALMESIOF", callcat) ) {
				dnl = length(dialledno)
				dnpreflen = dnl - 2
				if ( dnpreflen < 0 )
					dnpreflen = 0
				if ( dnl >= 0 ) {
					dnpref = substr(dialledno, 1, dnpreflen)
					expdialledno = sprintf("%sxx", dnpref)
				}
			#}
			#else {
			#	expdialledno = dialledno
			#}
		}

		if ( mdrrec_recno[mobile_fname] == "" )
			mdrrec_recno[mobile_fname] = 1
		else
			mdrrec_recno[mobile_fname] += 1
		mob_recno = mdrrec_recno[mobile_fname]

		onnettype = ""
		new_expdialledno = expdialledno
		# rjs9
		if ( mob_rectype == "CDR" && chg_cat != "X") {
			#if ( substr(new_expdialledno,1,8) == "Internet" )
			#	new_expdialledno = "Internet"
			#if ( substr(new_expdialledno,1,10) == "Blackberry" )
			#	new_expdialledno = "Blackberry"
		#	# remove nKb from diall no
		#	gsub(/[0-9]+[kK][bB]/,"", new_expdialledno)
			onnettype = is_onnet(new_expdialledno)
		}

		if ( testmobdet == 1 || retroBDL != "1" ) {
			if ( !dot21verify ) {
				# skip dummy records (allows for 0 totals in summary data)
				if ( chg_cat != "X" && !dot21verify ) {
					#print "MOBDET: spit data for [" SID "]"
					spit_mdrrec_tomobilefile()
				}
				#else {
				#	print "skipping dummy mdrrec"
				#}
			}
		}

		######################
		# add to upper totals
		#print "addup rec_type=[" rec_type "]"
		#print "  mob_recclass=[" mob_recclass "]"
		#print "  SERVTYPE=[" SERVTYPE "]"
		#print "  CENTRE=[" CENTRE "]"
		#print "  PROJECT=[" PROJECT "]"
		#print "  supplier=[" supplier "]"
		#print "  chg_cat=[" chg_cat "]"
		#print "  SID=[" SID "]"
		#print "  mob_cost = " mob_cost

		# add up mobile 
		add_totals("TOTAL", SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID, supplier, invoiceno, sundry, rec_type, chg_cat, chg_count, chg_duration, chg_cost)

		# add up dummy mobile cost
		if ( SID == "0400-000000" ) {
			mobdummy_cost += chg_cost
		}

		##########################################
		# handle management reports for configured distribution lists
		if ( !dot21verify ) {
		    hsid = CENTRE "|" PROJECT "|" SID

		    # calls
		    if ( mob_rectype == "CDR" && chg_cat != "X") {

			if ( chg_count > 0 && new_expdialledno != "" ) {
				# most frequently dialled numbers for sid
				sid_freq_numbers_count_arr[SERVTYPE "|" hsid "|" new_expdialledno] += chg_count

				# premium service for sid
				if ( is_premium_service(new_expdialledno, SERVTYPE) ) {
					sid_premserv_count_arr[SERVTYPE "|" hsid] += chg_count
					sid_premserv_duration_arr[SERVTYPE "|" hsid] += chg_duration
					sid_premserv_cost_arr[SERVTYPE "|" hsid] += chg_cost
				}
			}

			# all global roaming mobile users
			Ctag = carriercode "|" mob_rectype "|" mob_recclass
			if ( gloroam_class_arr[Ctag] != "" ) {
				gloroam_users_count_arr[hsid] += chg_count
				gloroam_users_duration_arr[hsid] += chg_duration
				gloroam_users_cost_arr[hsid] += chg_cost
			}

			distidlist = mng_egrid_distidlist_arr[EGRID]
			#print "  distidlist=[" distidlist "]"
			split(distidlist, distid_arr, "|")
			for ( i in distid_arr ) {
				DISTID = distid_arr[i]

				htag = DISTID "|" SERVTYPE
				hobj = hsid "|" mob_recno "|" mob_rectype "|" mob_cat "|" mob_recclass "|" date "|" ctimestr "|" mob_origin "|" new_expdialledno "|" mob_destination "|" chg_duration "|" chg_cost

				# most expensiove call
				hval = chg_cost
				hi_in(htag, 20, hi_call_by_cost_arr, hobj, hval, 0)

				if ( chg_count > 0 && new_expdialledno != "" ) {
					# most frequently dialled numbers for distid
					dist_freq_numbers_count_arr[htag "|" new_expdialledno] += chg_count
					dist_freq_numbers_duration_arr[htag "|" new_expdialledno] += chg_duration
					dist_freq_numbers_cost_arr[htag "|" new_expdialledno] += chg_cost
					# Intra-Organisational Call Distribution
					if ( onnettype != "" ) {
						intracat = "ONNET Mobile to " onnettype
						Xtag = htag "|" intracat
						call_distribution_category_count_arr[Xtag] += chg_count
						call_distribution_category_duration_arr[Xtag] += chg_duration
						call_distribution_category_cost_arr[Xtag] += chg_cost
					}
				}

				# longest duration call
				hval = chg_duration
				hi_in(htag, 20, hi_call_by_duration_arr, hobj, hval, 0)
				# call distribution by date
				#cddmm = substr(date,1,5)
				Xtag = htag "|" date
				call_distribution_date_count_arr[Xtag] += chg_count
				call_distribution_date_duration_arr[Xtag] += chg_duration
				call_distribution_date_cost_arr[Xtag] += chg_cost

				if ( is_weekday(date) ) {
					# call distribution by hour of day
					chh = substr(endtime,1,2)
					Xtag = htag "|" chh
					call_distribution_hour_count_arr[Xtag] += chg_count
					call_distribution_hour_duration_arr[Xtag] += chg_duration
					call_distribution_hour_cost_arr[Xtag] += chg_cost
					call_distribution_byhourdate_arr[SERVTYPE "|" date] = 1
				}

				# call distribution by call category
				Xtag = htag "|" chg_cat
				call_distribution_category_count_arr[Xtag] += chg_count
				call_distribution_category_duration_arr[Xtag] += chg_duration
				call_distribution_category_cost_arr[Xtag] += chg_cost
			}
		    }

		    # all internet users
		    if ( mob_rectype == "CHARGE_DESC_INFO" && mob_recclass == "INTERNET" ) {
			internet_users_arr[hsid] = 1
			internet_users_kbytes_arr[hsid] += mob_rate
			print "mobdet: internet_users_kbytes_arr[" hsid "]=[" internet_users_kbytes_arr[hsid] "]"
			internet_users_cost_arr[hsid] += chg_cost
			print "mobdet: internet_users_cost_arr[" hsid "]=[" internet_users_cost_arr[hsid] "]"
		    }

		}

	
		##########################################
		# handle allmobiles csv for configured distribution lists
		# (skip dummy recs)
		if ( !dot21verify && chg_cat != "X" ) {
			distidlist = egrid_distidlist_arr[EGRID]
			#print "allmob: distidlist=[" distidlist "]"
			split(distidlist, distid_arr, "|")
			for ( i in distid_arr ) {
				DISTID = distid_arr[i]
				allmobiles_fname = allmobiles_fname_arr[DISTID]
				if ( allmobiles_fname == "" ) {
					# allmobiles csv header
					allmobiles_fname = sprintf("topdat/%s/%s/data/allmobiles_%s_%s.csv", monthdir, DISTID, DISTID, monthtag)
					print "allmobiles_fname=[" allmobiles_fname "]"
					allmobiles_fname_arr[DISTID] = allmobiles_fname
					# create file and csv header
					newdir = sprintf("topdat/%s/%s/data", monthdir, DISTID)
					cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p %s", newdir, newdir)
					system(cmd)
					add_fname_to_fnamelist(allmobiles_fname)


					outp = quote("ReportDesc") ","
					outp = outp quote("ExBillPeriod") ","
					outp = outp quote("ShipToDesc") ","
					#outp = outp quote("DIVID") ","
					outp = outp quote("ShipTo") ","
					outp = outp quote("BatchTypeDesc") ","
					outp = outp quote("department") ","
					#outp = outp quote("SID") ","
					outp = outp quote("ServiceID") ","
					outp = outp quote("InvoiceNo") ","
					outp = outp quote("Sundry") ","
					outp = outp quote("Supplier") ","
					outp = outp quote("User") ","
					outp = outp quote("Date") ","
					outp = outp quote("Time") ","
					outp = outp quote("Origin") ","
					outp = outp quote("DialledNumber") ","
					outp = outp quote("Description") ","
					outp = outp quote("Duration") ","
					outp = outp quote("Cost")

					print outp >allmobiles_fname
				}
				# allmobiles csv data

				# exclude CHARGE_DESC CALL call summary records
				# (keep RENT and OTHER and ADMIN)
				amDate = date
				amDialled_Number = expdialledno
				if ( amDate == "CHARGE_DESC" ) {
					if ( mob_origin == "CALL" )
						next
					amDate = billsdate
					Dialled_Number = ""
				}
				gsub("[a-zA-Z]","", Dialled_Number)

		#mdr
		#csvstr = sprintf(",,,,,,,,,,,,,%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%0.4f\n",
		#	 mob_recno, mob_rectype, mob_cat, mob_recclass,
		#	 date, ctimestr, mob_origin, expdialledno, mob_destination, mob_durstr, mround4(mob_cost))

				#CENTRE #(VT department)
				#CENTRE_to_CENTREdesc_arr[CENTRE]
				#SERVTYPEdesc,
				#PROJECTdesc,
				#PROJECT,
				#SID,
				#serviceprovider,
				#mobname "|" mobmanufacturer "|" mobmodel
				#supplier
				#invoiceno
				#sundry
				#tascode

				#print "allmob: ShipTo_to_ShipToDesc_arr[" SHIPTO"]=" ShipTo_to_ShipToDesc_arr[SHIPTO]

				ShipToDesc = ShipTo_to_ShipToDesc_arr[SHIPTO]
				ServTypeDesc = SERVTYPEdesc
				department = CENTRE_to_CENTREdesc_arr[CENTRE]

				outp = quote("ServiceID Detail") ","
				outp = outp quote(rfmtexbillperiod) ","
				outp = outp quote(ShipToDesc) ","
				outp = outp quote(SHIPTO) ","
				outp = outp quote(ServTypeDesc) ","
				outp = outp quote(department) ","
				outp = outp quote(SID) ","
				outp = outp quote(invoiceno) ","
				outp = outp quote(sundry) ","
				outp = outp quote(supplier) ","
				outp = outp quote(mobname) ","
				outp = outp quote(amDate) ","
				outp = outp quote(ctimestr) ","
				outp = outp quote(mob_origin) ","
				outp = outp quote(amDialled_Number) ","
				outp = outp quote(mob_destination) ","
				outp = outp quote(mob_durstr) ","
				outp = outp quote(mob_cost)

				print outp >>allmobiles_fname

			}
		}


		######################################
		last_gggrid = gggrid
		last_eggrid = eggrid
		last_extn = extn
		last_siteid = siteid
		last_SHIPTO = SHIPTO
		last_GROUP = GROUP
		last_SERVTYPE = SERVTYPE
		last_SHIPTO = SHIPTO
		last_GROUP = GROUP
		last_CENTRE = CENTRE
		last_PROJECT = PROJECT
		last_SID = SID
		last_directoryID = directoryID

	}
	close(mobcallsfile)

	###############################################
	# finish up
	print ""
	print "finished domobdetfile() - " mobcallsfile


}

# END Mobile Call Details
#####################################################
#####################################################


#----------------------------------------------------------


# m11cdr Call Details
# (from tbs getdetail6 - other than mobile or fixed line)
#####################################################
#####################################################
#####################################################
#####################################################

function create_m11cdr_detail_file()
{
	if ( retroBDL != "1" ) {
		if ( m11cdr_fname != "" ) {	# close previous file
			close(m11cdr_fname)
			m11cdr_fname = ""
		}
	}
	m11cdr_fname = sprintf("topdat/%s/data/m11cdr%s/m11cdr_%s_%s_%s_%s.csv", monthdir, SERVTYPE, CENTRE, PROJECT, SERVTYPE, SID)
	fname_to_SERVTYPECENTREPROJECT_arr[m11cdr_fname] = SERVTYPE "|" CENTRE "|" PROJECT
	if ( retroBDL == "1" )
		return
	printf(">>>            m11cdr_fname = %s\n", m11cdr_fname)
	if ( (getline x < m11cdr_fname) < 0 ) {
		# create file and csv header
		newdir = sprintf("topdat/%s/data/m11cdr%s", monthdir, SERVTYPE)
		cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p %s", newdir, newdir)
		#print "system 3 [" cmd "]"
		system(cmd)

		CCPdesc = CENTREPROJECT_to_PROJECTdesc_arr[CENTRE "|" PROJECT]
		PROJECTdesc = CENTREPROJECT_to_PROJECTdesc3_arr[CENTRE "|" PROJECT]

		printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,", "ServiceID Detail", exbillperiod, "SDATE", "EDATE", CENTRE_to_CENTREdesc_arr[CENTRE], CENTRE, getSERVTYPEdesc(SERVTYPE,SID,sundry), SERVTYPE, PROJECTdesc, PROJECT, SID, serviceprovider, t21name ) >m11cdr_fname
		printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", "RecNo", "RecType", "ChargeCategory", "RecClass", "Date","Time","Origin","Dialled Number","Description","Duration","Cost:Float") >>m11cdr_fname
	}
	else {
		printf("WARNING %d: m11cdr_fname = %s exists.\n", NR, m11cdr_fname)
	}
}


function spit_m11rec_tom11cdrfile()
{
	# append m11cdr call record data to m11cdr file

	if ( retroBDL != "1" ) {
		csvstr = sprintf(",,,,,,,,,,,,,%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%0.4f\n", m11cdr_recno, m11cdr_rectype, m11cdr_cat, m11cdr_recclass, date, ctimestr, m11cdr_origin, expdialledno, m11cdr_destination, m11cdr_durstr, mround4(m11cdr_cost))

		#printf("%s", csvstr)
		printf("%s", csvstr) >>m11cdr_fname
	}

}


#################################################################


function dom11cdrfile(m11cdrfile)
{
	printf("dom11cdr(%s)\n", m11cdrfile)

	system("date")

	CENTRE = ""
	PROJECT = ""
	SID = ""

	m11cdr_fname = ""

	last_gggrid = "First Time"
	last_eggrid = "First Time"
	last_extn = -1
	last_siteid = -1

	last_SERVTYPE = ""
	last_SHIPTO = ""
	last_GROUP = ""
	last_CENTRE = ""
	last_PROJECT = ""
	last_SID = ""
	last_directoryID = ""

	while ( (getline m11cdrline < m11cdrfile) > 0 ) {
		#print "M11CDR m11cdrline = " m11cdrline
		split(m11cdrline, a_arr, "|")
		f = 0
		m11cdr_batchtypeid = clip(a_arr[++f])
		m11cdr_sundry = clip(a_arr[++f])
		m11cdr_serviceid = clip(a_arr[++f])
		m11cdr_carrier = toupper(clip(a_arr[++f]))
		m11cdr_rectype = clip(a_arr[++f])
		m11cdr_recclass = clip(a_arr[++f])
		m11cdr_billperiod = clip(a_arr[++f])
		m11cdr_cdate = clip(a_arr[++f])
		m11cdr_ctime = clip(a_arr[++f])
		m11cdr_origin = clip(a_arr[++f])
		m11cdr_destination = clip(a_arr[++f])
		m11cdr_dialled = clip(a_arr[++f])
		m11cdr_rate = clip(a_arr[++f])
		m11cdr_duration = clip(a_arr[++f])
		m11cdr_cost = clip(a_arr[++f])
		m11cdr_newcost = clip(a_arr[++f])

		if ( m11cdr_newcost != "" ) {
			m11cdr_carrier_cost = m11cdr_cost
			m11cdr_cost = m11cdr_newcost
		}

		# skip error recs
		if ( m11cdr_rectype == "ERROR" ) {
			continue
		}

		# skip config validation recs
		if ( m11cdr_rectype == "CONFIG" ) {
			continue
		}

		# skip invoice grand total rec
		if ( m11cdr_rectype == "CHARGE_DESC_INFO" && m11cdr_recclass == "TOTAL" ) {
			continue
		}

		# rjs9
		supplier = m11cdr_carrier
		#supplier = t21_supplier " " supplier

		# lower cased first char of carrier
		carriercode = tolower(substr(m11cdr_carrier,1,1))

		vt_m11cdr_recclass = m11cdr_recclass
		if ( m11cdr_rectype == "CDR" ) {
			split(vt_m11cdr_recclass, vt_m11cdr_recclass_arr, "~")
			TransactionType_Desc = vt_m11cdr_recclass_arr[1]
			RateDesc_Desc = vt_m11cdr_recclass_arr[2]
			m11cdr_recclass = fix_vt_mob_recclass(vt_m11cdr_recclass)
		}

		Ctag = carriercode "|" m11cdr_rectype "|" m11cdr_recclass
		if ( m11cdr_recclass == "X" ) {	# dummy cdr from config
			m11cdr_cat = "X"
		}
		else {
			#m11cdr_cat = carriercode_chargeclasstype_to_chargecat_arr[Ctag]
			# use carriecode
			m11cdr_cat = carriercode
		}
		#if ( m11cdr_cat == "" ) {
		#	m11cdr_cat = "Mobile Misc"
		#	print "WARNING: default m11cdr charge category "
		#		 m11cdr_cat " set for Ctag=[" Ctag "]"
		#		 "  supplier = [" supplier "]"
		#		 "  m11cdr_cost = [" m11cdr_cost "]"
		#		 "  m11cdr_serviceid = [" m11cdr_serviceid "]"
		#		 "  m11cdr_recclass=[" m11cdr_recclass "]"
		#}
		##print "mobcat: " m11cdr_cat " set for Ctag=[" Ctag "]"
		##		 "  supplier = [" supplier "]"
		##		 "  m11cdr_cost = [" m11cdr_cost "]"
		##		 "  m11cdr_serviceid = [" m11cdr_serviceid "]"
		##		 "  m11cdr_recclass=[" m11cdr_recclass "]"

		rec_type = "M11CDR_" m11cdr_rectype
		chg_cat = m11cdr_cat
		if ( chg_cat == "X" )	# dummy categroy
			chg_count = 0
		else
			chg_count = 1
		chg_duration = m11cdr_duration
		chg_cost = m11cdr_cost

		#print "rec_type=[" rec_type "]  m11cdr_recclass=[" m11cdr_recclass "]" " vt_m11cdr_recclass=[" vt_m11cdr_recclass "]"
		m11cdrSID = m11cdr_serviceid

		#-----

		m11cdrdirectoryID = sid_to_directoryID_arr[ServiceID] 
		#print "sid_to_directoryID_arr[" m11cdr_serviceid "] = [" sid_to_directoryID_arr[m11cdr_serviceid] "]"

#----
#		# group assigned
#		eggrid = mobile_to_grid_arr{m11cdr_serviceid]
#		print "mobile_to_grid_arr[" m11cdr_serviceid "]=[" mobile_to_grid_arr[m11cdr_serviceid] "]"
#		if ( eggrid == "" ) {
#			#personal assigned
#			# rjs9 -  eggrid is mob sid 
#			#eggrid = recordno_to_eggrid_arr[mobdirectoryID]
#			#print "recordno_to_eggrid_arr[" mobdirectoryID "]=[" recordno_to_eggrid_arr[mobdirectoryID] "]"
#			eggrid = mobSID
#			if ( Testing >= 2 && eggrid != "" )
#				print "PERSONAL ASSIGNED eggrid = [" eggrid "]"
#		}
#		else {
#			if ( Testing >= 2 && eggrid != "" )
#				print "GROUP ASSIGNED mobile_to_grid_arr[" m11cdr_serviceid "] = [" mobile_to_grid_arr[m11cdr_serviceid] "]"
#		}

	
		# rjs9 -  eggrid is mob sid 
		eggrid = m11cdrSID
		#if ( eggrid == "" ) {
		#	print "WARNING: mobile [" m11cdr_serviceid "] not assigned to directoryID or groupID"
		#	eggrid = "UN_MOB"
		#	#continue
		#}

		gggrid = grid_to_parentgroupid_arr[eggrid]
		#print "grid_to_parentgroupid_arr[" eggrid "] = [" grid_to_parentgroupid_arr[eggrid] "]"

		m11cdrCENTRE = grid_to_centre_arr[eggrid]
		#print "grid_to_centre_arr[" eggrid "]=[" grid_to_centre_arr[eggrid] "]"
		m11cdrPROJECT = grid_to_project_arr[eggrid]
		#print "grid_to_project_arr[" eggrid "]=[" grid_to_project_arr[eggrid] "]"
		#print "M11CDRm11cdrline = " m11cdrline
		#printf("M11CDR: %s|%s|%s|%s|%s|%s|%s|%s|%s|%d|%0.4f\n",
		#	 m11cdrCENTRE, m11cdrPROJECT,
		#	 m11cdrdirectoryID, m11cdr_batchtypeid, m11cdr_sundry,
		#	 m11cdrSID, m11cdr_carrier, m11cdr_cdate,
		#	 m11cdr_ctime, m11cdr_duration, mround4(m11cdr_cost))

		serviceprovider = m11cdr_carrier
		#printf("   serviceprovider=%s\n", serviceprovider)

		######################

		if ( m11cdrSID <= 0 ) {
			print "WARNING: M11CDR SKIPPING m11cdrSID = " m11cdrSID
			continue
		}

		# set servicetype
		#SERVTYPE = servtype
		SERVTYPE = m11cdr_batchtypeid + 3000000
		SERVTYPEdesc = getSERVTYPEdesc(SERVTYPE,m11cdrSID,m11cdr_sundry)
		#print "SERVTYPE = " SERVTYPE "  SERVTYPEdesc = " SERVTYPEdesc

		if ( gggrid == "" )
			gggrid="XXX"

		if ( eggrid == "" )
			eggrid = "YYY"

		t21_GGRID = gggrid
		GGRID = t21_GGRID

		t21_EGRID = eggrid
		EGRID = t21_EGRID

		# get sid info (name, extn and siteid)
		get_tbssidinfo(0,SERVTYPE,m11cdrSID)
		extn = t21_extn

		CENTRE = m11cdrCENTRE
		PROJECT = m11cdrPROJECT

		SID = m11cdrSID

		#if ( SID != last_SID ) {
		#	print "Do m11cdrfile eggrid = " eggrid "  CENTRE = " CENTRE "  PROJECT = "  PROJECT "  SID = " SID
		#}

		######################################
		# test if grids from summary data not in hierarchy
		if ( CENTRE == ""  || PROJECT == "" ) {
			print "WARNING: M11CDR SKIPPING BAD CENTRE/PROJECT" " eggrid = " eggrid "  CENTRE = " CENTRE "  PROJECT = "  PROJECT "  SID = " SID
			continue
		}

		# only doing thisCENTRE
		if ( thisCENTRE != "all" && gggrid != thisCENTRE )
			continue

		######################################
		SHIPTO = CENTRE_to_ShipTo_arr[CENTRE]
		GROUP = grid_to_parentgroupid_arr[CENTRE]
		#print "SHIPTO=[" SHIPTO "]" "  GROUP=[" GROUP "]"

		######################################
		if (SERVTYPE == "" || SHIPTO == "" || GROUP == "" || CENTRE == "" || PROJECT == "" || SID == "") {
			#print "WARNING: ERROR: M11CDR BAD HIERIDx = " HIERIDx
			print "WARNING: ERROR: M11CDR BAD"
			print "           SERVTYPE = " SERVTYPE
			print "             SHIPTO = " SHIPTO
			print "              GROUP = " GROUP
			print "             CENTRE = " CENTRE
			print "            PROJECT = " PROJECT
			print "                SID = " SID
			continue
		}



		######################

		# Testing
	        #if ( Testing >= 3 && index("|ANN|VLP|VLS|VLO|VLI|VNA|VTC|VNE|VSE|VSW|VRT|", "|" SHIPTO "|") <= 0 ) {
	        if ( Testing >= 3 && index("|A|D|K|S|V|", "|" substr(SHIPTO,1,1) "|") <= 0 ) {
			print "    TESTING... SKIPPING  SHIPTO=[" SHIPTO "]" "  gggrid=[" gggrid "]" "  eggrid=[" eggrid "]"
			continue
		}

		######################

		sundry = m11cdr_sundry
		if ( sundry == 0 ) {
			print "DOM11CDRWARN: using SID_to_sundry_arr[" SID "]=[" SID_to_sundry_arr[SID] "]"
			sundry = SID_to_sundry_arr[SID]
		}


		tascode = sundryEGRID_to_tascode_arr[sundry "|" eggrid]
		if ( tascode == "") {
			print "DOM11CDRWARN: using SID_to_tascode_arr[" SID "]=[" SID_to_tascode_arr[SID] "]"
			tascode = SID_to_tascode_arr[SID]
		}

		invoiceno = sundryEGRID_to_invoiceno_arr[sundry "|" eggrid]
		if ( invoiceno == "" ) {
			print "DOM11CDRWARN: using SID_to_invoiceno_arr[" SID "]=[" SID_to_invoiceno_arr[SID] "]"
			invoiceno = SID_to_invoiceno_arr[SID]
		}

		# rjs9 - called also in ld_cde()
		set_HIERIDx("t21m11cdr", SERVTYPE,SHIPTO,GROUP,CENTRE,PROJECT,supplier,invoiceno,sundry,SID,rec_type,chg_cat)

		# set in get_tbsinfo
		#get_t21info(SERVTYPE,t21_EGRID,extn)

		######################################
		# exclude data
		if ( test_exclude(SERVTYPE, siteid, eggrid, extn) )
			continue

		######################################
		#printf("m11cdrline=[%s]\n", m11cdrline)

		#handle_m11cdr_change()

		# new sid
		if ( SID != last_SID || eggrid != last_eggrid || gggrid != last_gggrid ) {
#			# rjs9
#			CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" SID] = EGRID

			printf("T21 new m11cdr: eggrid=[%s]  SID=[%s]  extn=[%s] t21name=[%s]\n", eggrid, SID, extn, t21name)
			if ( !dot21verify ) {
				create_m11cdr_detail_file()
			}
		}

		date = fixdate(m11cdr_cdate)
		endtime = m11cdr_ctime

		if (m11cdr_duration == "") 
			m11cdr_duration = 0

		durhh = int(m11cdr_duration / 3600)
		remd = m11cdr_duration - (durhh * 3600)
		durmm = int(remd / 60)
		remd = remd - (durmm * 60)
		durss = remd
		m11cdr_durstr = sprintf("%03d:%02d:%02d", durhh, durmm, durss)

		hh = substr(endtime,1,2)
		mm = substr(endtime,3,2)
		ss = substr(endtime,5,2)
		ctimestr = sprintf("%2s:%2s:%2s",hh, mm, ss)

		# set dialled/description info
		if ( m11cdr_dialled == m11cdr_destination )
			m11cdr_destination = ""
		# remove dialled number from start of destination
		if ( m11cdr_dialled != "" ) {
			#sub(m11cdr_dialled,"",m11cdr_destination)
			if ( substr(m11cdr_destination,1,length(m11cdr_dialled)) == m11cdr_dialled )
				m11cdr_destination = substr(m11cdr_destination,length(m11cdr_dialled)+1)
		}
		# dialno_desc = sprintf("%s", m11cdr_dialled)
		# if ( m11cdr_destination != "" )
		# 	dialno_desc = sprintf("%s %s", dialno_desc, m11cdr_destination)

		dialledno = m11cdr_dialled
		#printf("noddsuparr[%s] = %d\n", gggrid, noddsuparr[gggrid])
		if ( 1 || noddsuparr[gggrid] == 0 ) {	# group has NO digit suppression
			expdialledno = dialledno
		}
		else {
			# xx for last 2 digits in dialledno if length >= 0
			## supress for these callcategories only
			#if ( index("JALMESIOF", callcat) ) {
				dnl = length(dialledno)
				dnpreflen = dnl - 2
				if ( dnpreflen < 0 )
					dnpreflen = 0
				if ( dnl >= 0 ) {
					dnpref = substr(dialledno, 1, dnpreflen)
					expdialledno = sprintf("%sxx", dnpref)
				}
			#}
			#else {
			#	expdialledno = dialledno
			#}
		}

		if ( m11rec_recno[m11cdr_fname] == "" )
			m11rec_recno[m11cdr_fname] = 1
		else
			m11rec_recno[m11cdr_fname] += 1
		m11cdr_recno = m11rec_recno[m11cdr_fname]

		onnettype = ""
		new_expdialledno = expdialledno
		# rjs9
		if ( mob_rectype == "CDR" && chg_cat != "X") {
			#if ( substr(new_expdialledno,1,8) == "Internet" )
			#	new_expdialledno = "Internet"
			#if ( substr(new_expdialledno,1,10) == "Blackberry" )
			#	new_expdialledno = "Blackberry"
		#	# remove nKb from diall no
		#	gsub(/[0-9]+[kK][bB]/,"", new_expdialledno)
			onnettype = is_onnet(new_expdialledno)
		}

		if ( retroBDL != "1" ) {
			if ( !dot21verify ) {
				# skip dummy records (allows for 0 totals in summary data)
				if ( chg_cat != "X" && !dot21verify ) {
				    #print "M11CDR: spit data for [" SID "]"
				    spit_m11rec_tom11cdrfile()
				}
				else {
				    print "skipping dummy m11rec"
				}
			}
		}

		######################
		# add to upper totals
		#print "addup rec_type=[" rec_type "]"
		#print "  m11cdr_recclass=[" m11cdr_recclass "]"
		#print "  SERVTYPE=[" SERVTYPE "]"
		#print "  CENTRE=[" CENTRE "]"
		#print "  PROJECT=[" PROJECT "]"
		#print "  supplier=[" supplier "]"
		#print "  chg_cat=[" chg_cat "]"
		#print "  SID=[" SID "]"
		#print "  m11cdr_cost = " m11cdr_cost

		# add up m11cdr 
		add_totals("TOTAL", SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID, supplier, invoiceno, sundry, rec_type, chg_cat, chg_count, chg_duration, chg_cost)

		## add up dummy m11cdr cost
		#if ( SID == "0400-000000" ) {
		#	mobdummy_cost += chg_cost
		#}


		##########################################
		# handle management reports for configured distribution lists
		if ( !dot21verify ) {
		    hsid = CENTRE "|" PROJECT "|" SID

		    # calls
		    if ( mob_rectype == "CDR" && chg_cat != "X") {

			if ( chg_count > 0 && new_expdialledno != "" ) {
			    // not an Internet call
			    if ( index("KB_Internet",new_expdialledno) <= 0 ) {
				# most frequently dialled numbers for sid
				sid_freq_numbers_count_arr[SERVTYPE "|" hsid "|" new_expdialledno] += chg_count

				# premium service for sid
				if ( is_premium_service(new_expdialledno, SERVTYPE) ) {
					sid_premserv_count_arr[SERVTYPE "|" hsid] += chg_count
					sid_premserv_duration_arr[SERVTYPE "|" hsid] += chg_duration
					sid_premserv_cost_arr[SERVTYPE "|" hsid] += chg_cost
				}
			    }
			}

			# all global roaming mobile users
			Ctag = carriercode "|" mob_rectype "|" mob_recclass
			if ( gloroam_class_arr[Ctag] != "" ) {
				gloroam_users_count_arr[hsid] += chg_count
				gloroam_users_duration_arr[hsid] += chg_duration
				gloroam_users_cost_arr[hsid] += chg_cost
			}

			distidlist = mng_egrid_distidlist_arr[EGRID]
			#print "  distidlist=[" distidlist "]"
			split(distidlist, distid_arr, "|")
			for ( i in distid_arr ) {
				DISTID = distid_arr[i]

				htag = DISTID "|" SERVTYPE
				hobj = hsid "|" mob_recno "|" mob_rectype "|" mob_cat "|" mob_recclass "|" date "|" ctimestr "|" mob_origin "|" new_expdialledno "|" mob_destination "|" chg_duration "|" chg_cost

				# most expensiove call
				hval = chg_cost
				hi_in(htag, 20, hi_call_by_cost_arr, hobj, hval, 0)

				if ( chg_count > 0 && new_expdialledno != "" ) {
					# most frequently dialled numbers for distid
					dist_freq_numbers_count_arr[htag "|" new_expdialledno] += chg_count
					dist_freq_numbers_duration_arr[htag "|" new_expdialledno] += chg_duration
					dist_freq_numbers_cost_arr[htag "|" new_expdialledno] += chg_cost
					# Intra-Organisational Call Distribution
					if ( onnettype != "" ) {
						intracat = "ONNET Mobile to " onnettype
						Xtag = htag "|" intracat
						call_distribution_category_count_arr[Xtag] += chg_count
						call_distribution_category_duration_arr[Xtag] += chg_duration
						call_distribution_category_cost_arr[Xtag] += chg_cost
					}
				}

				# longest duration call
				hval = chg_duration
				hi_in(htag, 20, hi_call_by_duration_arr, hobj, hval, 0)
				# call distribution by date
				#cddmm = substr(date,1,5)
				Xtag = htag "|" date
				call_distribution_date_count_arr[Xtag] += chg_count
				call_distribution_date_duration_arr[Xtag] += chg_duration
				call_distribution_date_cost_arr[Xtag] += chg_cost

				if ( is_weekday(date) ) {
					# call distribution by hour of day
					chh = substr(endtime,1,2)
					Xtag = htag "|" chh
					call_distribution_hour_count_arr[Xtag] += chg_count
					call_distribution_hour_duration_arr[Xtag] += chg_duration
					call_distribution_hour_cost_arr[Xtag] += chg_cost
					call_distribution_byhourdate_arr[SERVTYPE "|" date] = 1
				}

				# call distribution by call category
				Xtag = htag "|" chg_cat
				call_distribution_category_count_arr[Xtag] += chg_count
				call_distribution_category_duration_arr[Xtag] += chg_duration
				call_distribution_category_cost_arr[Xtag] += chg_cost
			}
		    }

		    # all internet users
		    if ( mob_rectype == "CHARGE_DESC_INFO" && mob_recclass == "INTERNET" ) {
			internet_users_arr[hsid] = 1
			internet_users_kbytes_arr[hsid] += mob_rate
			print "m11det: internet_users_kbytes_arr[" hsid "]=[" internet_users_kbytes_arr[hsid] "]"
			internet_users_cost_arr[hsid] += chg_cost
			print "m11det: internet_users_cost_arr[" hsid "]=[" internet_users_cost_arr[hsid] "]"
		    }

		}

	
		######################################
		last_gggrid = gggrid
		last_eggrid = eggrid
		last_extn = extn
		last_siteid = siteid
		last_SHIPTO = SHIPTO
		last_GROUP = GROUP
		last_SERVTYPE = SERVTYPE
		last_SHIPTO = SHIPTO
		last_GROUP = GROUP
		last_CENTRE = CENTRE
		last_PROJECT = PROJECT
		last_SID = SID
		last_directoryID = directoryID

	}
	close(m11cdrfile)

	###############################################
	# finish up
	print ""
	print "finished dom11cdrfile() - " m11cdrfile

}

# END m11cdr Call Details
#####################################################
#####################################################


#----------------------------------------------------------


# T21ACCOUNTCODECALLS
#####################################################
#####################################################
#####################################################
#####################################################

##############################################################

function spit_cdarec_to_acccodedetfile()
{
	# append call record data to acccodedet file
	printf(",,,,,,,,,,,,") >>acccodedet_fname
	printf("%s,%s,%s,%s,%s,%s,%s,%0.2f\n", date, endtimestr, t21_extn, t21name, expdialledno, calldesc, durstr, mround2(callcost)) >> acccodedet_fname
}


################################################
################################################


function handle_acccodedet_change()
{
	# start new acccodedet
	if ( acccode != last_acccode || eggrid != last_eggrid || gggrid != last_gggrid ) {
		printf("T21 acccodedet_change(): eggrid=%s  acccode=%s  extn=%s  t21surname=%s  t21firstname=%s  t21name=%s\n", eggrid, acccode, extn, t21surname, t21firstname, t21name)
		get_t21_upper_info(gggrid, eggrid)
		if ( acccodedet_fname != "" ) {	# close previous acccodedet file
			if ( !dot21verify ) {
				close(acccodedet_fname)
			}
			acccodedet_fname = ""
		}
		acccodedet_fname = sprintf("topdat/%s/data/acccodedet%s/acccodedet_%s_%s_%s_%s.csv", monthdir, SERVTYPE, CENTRE, PROJECT, SERVTYPE, acccode)
		fname_to_SERVTYPECENTREPROJECT_arr[acccodedet_fname] = SERVTYPE "|" CENTRE "|" PROJECT
		get_ggname(gggrid)
		get_egname(eggrid)
		get_dirname(siteid,eggrid,extn)
		if ( dot21verify )
			return
		printf("    >>> acccodedet_fname = %s\n", acccodedet_fname)
		if ( (getline x < acccodedet_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/data/acccodedet%s", monthdir, SERVTYPE)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p %s", newdir, newdir)
			system(cmd)

			printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,", "Account Code Call Details", exbillperiod, sdate, edate, t21_CENTREdesc, t21_CENTRE, t21_SERVTYPEdesc, t21_SERVTYPE, t21_PROJECTdesc, t21_PROJECT, acccode, acccode_to_desc_arr[acccode]) >>acccodedet_fname

			printf("Date,End Time,Extn,Name,Dialled Number,Description,Duration,Total:Float\n") >>acccodedet_fname
		}
		else {
			printf("WARNING %d: acccodedet_fname = %s exists.\n", NR, acccodedet_fname)
		}

	}
}



################################################################
# process T21 account code detail calls file

function dot21acccodecallsfile(t21acccodecallsfile,servtype)
{
	printf("dot21acccodecallsfile(%s)  servtype=%s\n", t21acccodecallsfile, servtype)
	system("date")

	serviceprovider = t21serviceprovider
	printf("   serviceprovider=%s\n", serviceprovider)

	CENTRE = ""
	PROJECT = ""
	SID = ""

	SERVTYPE = servtype
	SERVTYPEdesc = SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]
	print "SERVTYPE = " SERVTYPE "  SERVTYPEdesc = " SERVTYPEdesc

	tmpt21acccodecallsfile = t21acccodecallsfile

	ggname = ""
	egname = ""
	egccp = ""
	t21name = ""

	last_gggrid = "First Time"
	last_eggrid = "First Time"
	last_acccode = -1
	last_extn = -1
	last_siteid = -1

	last_ggname = ""
	last_egname = ""
	last_egccp = ""
	last_t21surname = ""
	last_t21firstname = ""
	last_t21name = ""

	acccodedet_fname = ""

	###############################################
	# process each T21 call

	while ( (getline cdaline < tmpt21acccodecallsfile) > 0 ) {
		split(cdaline, cda_arr, ",")
		##printf("cdaline=%s\n", cdaline)

		gggrid = fixforfname(trim(cda_arr[1]))
		eggrid = fixforfname(trim(cda_arr[2]))

		######################

		# Testing
		######################
	        #if ( Testing >= 3 && index("|ANN|VLP|VLS|VLO|VLI|VNA|VTC|VNE|VSE|VSW|VRT|", "|" gggrid "|") <= 0 ) {
	        if ( Testing >= 3 && index("|A|D|K|S|V|", "|" substr(gggrid,1,1) "|") <= 0 ) {
			#print "    TESTING... SKIPPING  gggrid = " gggrid "  eggrid = " eggrid
			continue
		}

		######################

		acccode = cda_arr[3]
		extn = cda_arr[3+1]
		date = cda_arr[4+1]
		endtime = cda_arr[5+1]
		siteid = cda_arr[6+1]
		otherno = cda_arr[7+1]
		direction = cda_arr[8+1]
		dialledno = cda_arr[9+1]
		callcat = cda_arr[10+1]
		calldesc = cda_arr[11+1]
		dur = cda_arr[12+1]
		callcost = cda_arr[13+1]

		# rjs9
		supplier = "Fixed Line"
		supplier = t21_supplier " " supplier

		rec_type = "FIXED_CDA"
		chg_cat = fixed_callcat_to_chg_cat[callcat]
		if ( chg_cat == "" )
			chg_cat = "Fixed Other"
		if ( chg_cat == "X" )	# dummy categroy
			chg_count = 0
		else
			chg_count = 1
		chg_duration = dur
		chg_cost = callcost

		if ( acccode <= 0 ) {
			print "WARNING: SKIPPING acccode = " acccode
			continue
		}

		if ( gggrid == "" )
			gggrid="XXX"

		if ( eggrid == "" )
			eggrid = "YYY"

		t21_GGRID = gggrid
		GGRID = t21_GGRID

		t21_EGRID = eggrid
		EGRID = t21_EGRID

		t21_SID = acccode
		SID = t21_SID
		#SID = t21_EGRID ":" t21_SID

		t21_extn = extn

		CENTRE = grid_to_centre_arr[eggrid]
		PROJECT = grid_to_project_arr[eggrid]
		## rjs9
		#pargrid = grid_to_parentgroupid_arr[eggrid]
		#CENTRE = grid_to_centre_arr[pargrid]
		#PROJECT = grid_to_project_arr[pargrid]

		#if ( SID != last_SID ) {
		#	print "Do t21acccodecallsfile  eggrid = " eggrid "  CENTRE = " CENTRE "  PROJECT = "  PROJECT "  SID = " SID
		#}

		######################################
		# test if grids from summary data not in hierarchy
		if ( CENTRE == ""  || PROJECT == "" ) {
			print "WARNING:     SKIPPING BAD CENTRE/PROJECT" " eggrid = " eggrid "  CENTRE = " CENTRE "  PROJECT = "  PROJECT "  acccode = " acccode
			continue
		}

		# only doing thisCENTRE
		if ( thisCENTRE != "all" && gggrid != thisCENTRE )
			continue

		######################################
		SHIPTO = CENTRE_to_ShipTo_arr[CENTRE]
		GROUP = grid_to_parentgroupid_arr[CENTRE]

		######################################
		if (SERVTYPE == "" || CENTRE == "" || PROJECT == "" || SID == "") {
			#print "WARNING: ERROR: BAD HIERIDx = " HIERIDx
			print "WARNING: ERROR: BAD"
			print "           SERVTYPE = " SERVTYPE
			print "             SHIPTO = " SHIPTO
			print "              GROUP = " GROUP
			print "             CENTRE = " CENTRE
			print "            project = " PROJECT
			print "                sid = " SID
			continue
		}

		sundry = t21_sundry
		tascode = sundryEGRID_to_tascode_arr[sundry "|" eggrid]
		invoiceno = sundryEGRID_to_invoiceno_arr[sundry "|" eggrid]

		# rjs9 - called also in ld_cde()
		set_HIERIDx("t21acccode", SERVTYPE,CENTRE,PROJECT,supplier,invoiceno,sundry,SID,rec_type,chg_cat)

		if ( acccode != last_acccode || eggrid != last_eggrid || gggrid != last_gggrid ) {
			CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" SID] = EGRID
			print "T21 new acccode: CENTREPROJECTSID_to_EGRID_arr[" CENTRE "|" PROJECT "|" SID "]=[" CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" SID] "]"
		}

		get_t21info(1,t21_EGRID,t21_extn)

		######################################
		# exclude data
		if ( test_exclude(SERVTYPE, siteid, eggrid, acccode) )
			continue

		######################################
		#printf("cdaline=[%s]\n", cdaline)

		# set correct call category for internal call
		if ( direction == "in" && callcat == "J" ) {
			callcat = "A"
			calldesc = "Incoming Internal"
		}

		# force dialledno to other extn for Answering Internal KB
		if ( callcat == "A" )
			dialledno = otherno
		# force dialledno to other extn for originating Internal KB
		if ( callcat == "J" )
			dialledno = otherno

		#if ( callcat == "O" ) {	# Outgoing N/W (Tie not shown)
		#	continue
		#}
		#if ( callcat != "F" ) {	# FREE not shown
		#	continue
		#}

		date = fixdate(date)

		durhh = int(dur / 3600)
		remd = dur - (durhh * 3600)
		durmm = int(remd / 60)
		remd = remd - (durmm * 60)
		durss = remd
		durstr = sprintf("%03d:%02d:%02d", durhh, durmm, durss)

		hh = substr(endtime,1,2)
		mm = substr(endtime,3,2)
		ss = substr(endtime,5,2)
		endtimestr = sprintf("%2s:%2s:%2s",hh, mm, ss)

		#printf("noddsuparr[%s] = %d\n", gggrid, noddsuparr[gggrid])
		if ( dot21verify || noddsuparr[gggrid] == 0 ) {
			# group has no digit suppression
			expdialledno = dialledno
		}
		else {			# xx for last 2 digits in dialledno if length >= 0
			# supress for these callcategories only
			if ( index("JALMESIOF", callcat) ) {
				dnl = length(dialledno)
				dnpreflen = dnl - 2
				if ( dnpreflen < 0 )
					dnpreflen = 0
				if ( dnl >= 0 ) {
					dnpref = substr(dialledno, 1, dnpreflen)
					expdialledno = sprintf("%sxx", dnpref)
				}
			}
			else {
				expdialledno = dialledno
			}
		}

		handle_acccodedet_change()

		# skip dummy records (allows for 0 totals in summary data)
		if ( callcat != "X" && !dot21verify )
			spit_cdarec_to_acccodedetfile()

		######################
		# add to upper totals
		if ( callcat != "X" ) { # ignore account code config recs
			# account code - extn totals
			add_totals("TOTAL", SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID ":" extn, supplier, invoiceno, sundry, rec_type, chg_cat, chg_count, chg_duration, chg_cost)

			# All extn totals
			add_totals("TOTAL", SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID ":" "All", supplier, invoiceno, sundry, rec_type, chg_cat, chg_count, chg_duration, chg_cost)

			# store list of extns for each account code
			acccode_extn_arr[SID  "|" extn] = 1
		}

		######################################
		last_gggrid = gggrid
		last_eggrid = eggrid
		last_extn = extn
		last_acccode = acccode
		last_siteid = siteid

		last_SID = SID
		last_ggname = ggname
		last_egname = egname
		last_egccp = egccp
		last_t21surname = t21surname
		last_t21firstname = t21firstname
		last_t21name = t21name

	}
	close(tmpt21acccodecallsfile)

	###############################################
	# finish up

	print ""
	print "finished dot21acccodecallsfile() - " tmpt21acccodecallsfile

}


# END T21ACCOUNTCODECALLS
#####################################################
#####################################################
#----------------------------------------------------------



# T21OVERHEADS
#####################################################
#####################################################
#####################################################
#####################################################

function spit_header_to_ovhextnfile()
{
	#print "spit_header_to_ovhextnfile() - " ovhextn_fname
	if ( SERVTYPE == 2 )
		reportdesc = "Equipment Details"
	if ( SERVTYPE == 3 )
		reportdesc = "Miscellaneous Details"
	if ( SERVTYPE == 4 )
		reportdesc = "Overhead Details"
	printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,Code,Item,Reference,Description,FromDate,ToDate,Total Cost:Float\n", reportdesc, exbillperiod, sdate, edate, t21_CENTREdesc, t21_CENTRE, t21_SERVTYPEdesc, t21_SERVTYPE, t21_PROJECTdesc, t21_PROJECT, extn, t21name, t21site, t21location) >>ovhextn_fname
}

function spit_odrrec_to_ovhextnfile()
{
	# append call record data to overheads extn file
	printf(",,,,,,,,,,,,,,%s,%s,%s,%s,%s,%s,%0.2f\n", ovhcode, ovhitem, ovhreference, ovhdescription, assosdate, assoedate, mround2(ovhcost)) >> ovhextn_fname
}


################################################
################################################

function handle_ovhextn_change()
{
	# start new extn
	if ( extn != last_extn || eggrid != last_eggrid || gggrid != last_gggrid || SERVTYPE != last_SERVTYPE ) {
		printf("T21 ovhextn_change(): eggrid=%s  extn=%s  t21surname=%s  t21firstname=%s  t21name=%s\n", eggrid, extn, t21surname, t21firstname, t21name)
		get_t21_upper_info(gggrid, eggrid)
		if ( retroBDL != "1" ) {
			if ( ovhextn_fname != "" ) {	# close previous extn file
				if ( !dot21verify ) {
					close(ovhextn_fname)
				}
				ovhextn_fname = ""
			}
		}
		ovhextn_fname = sprintf("topdat/%s/data/extn%s/extn_%s_%s_%s_%s.csv", monthdir, SERVTYPE, CENTRE, PROJECT, SERVTYPE, extn)
		fname_to_SERVTYPECENTREPROJECT_arr[ovhextn_fname] = SERVTYPE "|" CENTRE "|" PROJECT
		get_ggname(gggrid)
		get_egname(eggrid)
		get_dirname(siteid,eggrid,extn)

		if ( dot21verify )
			return
		if ( retroBDL == "1" )
			return
		printf("    >>> ovhextn_fname = %s\n", ovhextn_fname)
		if ( (getline x < ovhextn_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/data/extn%s", monthdir, SERVTYPE)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p %s", newdir, newdir)
			system(cmd)

			spit_header_to_ovhextnfile()
		}
		else {
			printf("WARNING %d: ovhextn_fname = %s exists.\n", NR, ovhextn_fname)
		}

	}

}


################################################################
# process T21 detail overheads file

function dot21ovhfile(t21ovhfile, Eservtype, Mservtype, Oservtype)
{
	printf("dot21ovhfile(%s)  Eservtype=%s  Mservtype=%s  Oservtype=%s \n", t21ovhfile, Eservtype, Mservtype, Oservtype)
	system("date")

	serviceprovider = t21serviceprovider
	printf("   serviceprovider=%s\n", serviceprovider)

	CENTRE = ""
	PROJECT = ""
	SID = ""

	tmpt21ovhfile = t21ovhfile

	ggname = ""
	egname = ""
	egccp = ""
	t21name = ""

	last_gggrid = "First Time"
	last_eggrid = "First Time"
	last_extn = -1
	last_siteid = -1

	last_ggname = ""
	last_egname = ""
	last_egccp = ""
	last_t21surname = ""
	last_t21firstname = ""
	last_t21name = ""
	last_SERVTYPE = ""
	last_SHIPTO = ""
	last_GROUP = ""

	ovhextn_fname = ""

	###############################################
	# process each T21 overhead

	while ( (getline ovhline < tmpt21ovhfile) > 0 ) {
		split(ovhline, ovh_arr, "|")
		##printf("ovhline=%s\n", ovhline)

		# ggrid|egrid|extn|siteid|ovhtype|ovhorder|ovhcode|ovhitem|ovhreference|ovhdescription|assosdate|assoedate|ovhcost|
		# 20001|CP001013|7801|101|E|10|STANDARD_CHARGE|Standard Charge|STANDARD|Standard Charge|01/09/2003||1.44|

		gggrid = fixforfname(trim(ovh_arr[1]))
		eggrid = fixforfname(trim(ovh_arr[2]))

		######################

		# Testing
	        #if ( Testing >= 3 && index("|ANN|VLP|VLS|VLO|VLI|VNA|VTC|VNE|VSE|VSW|VRT|", "|" gggrid "|") <= 0 ) {
	        if ( Testing >= 3 && index("|A|D|K|S|V|", "|" substr(gggrid,1,1) "|") <= 0 ) {
			#print "OVH TESTING... SKIP  gggrid = " gggrid "  eggrid = " eggrid
			continue
		}

		######################

		extn = ovh_arr[3]
		siteid = ovh_arr[4]
		ovhtype = ovh_arr[5]
		ovhorder = ovh_arr[6]
		ovhcode = ovh_arr[7]
		ovhitem = ovh_arr[8]
		ovhreference = ovh_arr[9]
		ovhdescription = ovh_arr[10]
		assosdate = ovh_arr[11]
		assoedate = ovh_arr[12]
		ovhcost = ovh_arr[13]

		# rjs9
		supplier = "Overheads"
		supplier = t21_supplier " " supplier

		rec_type = "OVHEXTN_CHARGE_DESC"
		chg_cat = ovhcode
		if ( chg_cat == "X" )	# dummy categroy
			chg_count = 0
		else
			chg_count = 1
		chg_duration = 0
		chg_cost = ovhcost

		#if ( extn <= 0 ) {
		#	print "WARNING: OVH SKIPPING extn = " extn
		#	continue
		#}

		# set servicetype from ovhtype and params
		if ( ovhtype == "E" )
			servtype = Eservtype
		if ( ovhtype == "M" )
			servtype = Mservtype
		if ( ovhtype == "O" )
			servtype = Oservtype

		SERVTYPE = servtype
		SERVTYPEdesc = SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]
		#print "SERVTYPE = " SERVTYPE "  SERVTYPEdesc = " SERVTYPEdesc

		if ( gggrid == "" )
			gggrid="XXX"

		if ( eggrid == "" )
			eggrid = "YYY"

		t21_GGRID = gggrid
		GGRID = t21_GGRID

		t21_EGRID = eggrid
		EGRID = t21_EGRID

		t21_SID = extn
		SID = t21_SID
		#SID = t21_EGRID ":" t21_SID

		CENTRE = grid_to_centre_arr[eggrid]
		PROJECT = grid_to_project_arr[eggrid]
		## rjs9
		#pargrid = grid_to_parentgroupid_arr[eggrid]
		#CENTRE = grid_to_centre_arr[pargrid]
		#PROJECT = grid_to_project_arr[pargrid]

		#if ( SID != last_SID ) {
		#	print "Do t21ovhfile eggrid = " eggrid "  CENTRE = " CENTRE "  PROJECT = "  PROJECT "  Extn = " extn
		#	print "       ovhtype=" ovhtype "  SERVTYPE=" SERVTYPE
		#}

		######################################
		# test if grids from summary data not in hierarchy
		if ( CENTRE == ""  || PROJECT == "" ) {
			print "WARNING: OVH SKIPPING BAD CENTRE/PROJECT" " eggrid = " eggrid "  CENTRE = " CENTRE "  PROJECT = "  PROJECT "  Extn = " extn
			continue
		}

		# only doing thisCENTRE
		if ( thisCENTRE != "all" && gggrid != thisCENTRE )
			continue

		######################################
		SHIPTO = CENTRE_to_ShipTo_arr[CENTRE]
		GROUP = grid_to_parentgroupid_arr[CENTRE]

		######################################
		if (SERVTYPE == "" || CENTRE == "" || PROJECT == "" || SID == "") {
			#print "WARNING: ERROR: OVH BAD HIERIDx = " HIERIDx
			print "WARNING: ERROR: OVH BAD"
			print "           SERVTYPE = " servtype
			print "             SHIPTO = " SHIPTO
			print "              GROUP = " GROUP
			print "             CENTRE = " centre
			print "            project = " project
			print "                sid = " sid
			continue
		}

		# rjs9 - called also in ld_cde()
		set_HIERIDx("t21ovh", SERVTYPE,SHIPTO,GROUP,CENTRE,PROJECT,supplier,invoiceno,sundry,SID,rec_type,chg_cat)

		sundry = t21_sundry
		tascode = sundryEGRID_to_tascode_arr[sundry "|" eggrid]
		invoiceno = sundryEGRID_to_invoiceno_arr[sundry "|" eggrid]

		if ( extn != last_extn || eggrid != last_eggrid || gggrid != last_gggrid || SERVTYPE != last_SERVTYPE ) {
			CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" SID] = EGRID
			print "T21 new ovhextn: CENTREPROJECTSID_to_EGRID_arr[" CENTRE "|" PROJECT "|" SID "]=[" CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" SID] "]"
		}

		get_t21info(SERVTYPE,t21_EGRID,extn)

		######################################
		# exclude data
		if ( test_exclude(SERVTYPE, siteid, eggrid, extn) )
			continue

		######################################
		#printf("ovhline=[%s]\n", ovhline)

		assosdate = fixdate(assosdate)
		assoedate = fixdate(assoedate)

		handle_ovhextn_change()

		if ( retroBDL != "1" ) {
			if ( !dot21verify )
				spit_odrrec_to_ovhextnfile()
		}

		######################
		# add to upper totals
		add_totals("TOTAL", SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID, supplier, invoiceno, sundry, rec_type, chg_cat, chg_count, chg_duration, chg_cost)

		######################################
		last_gggrid = gggrid
		last_eggrid = eggrid
		last_extn = extn
		last_siteid = siteid

		last_ggname = ggname
		last_egname = egname
		last_egccp = egccp
		last_t21surname = t21surname
		last_t21firstname = t21firstname
		last_t21name = t21name
		last_SERVTYPE = SERVTYPE
		last_SHIPTO = SHIPTO
		last_GROUP = GROUP
		last_SID = SID

	}
	close(tmpt21ovhfile)

	###############################################
	# finish up

	print ""
	print "finished dot21ovhfile() - " tmpt21ovhcallsfile

}


# END T21COVERHEADS
#####################################################
#####################################################
#----------------------------------------------------------




# T21CALLS
#####################################################
#####################################################
#####################################################
#####################################################

##############################################################

function spit_cdrrec_to_extnfile()
{ 
	# append call record data to extn file
	printf(",,,,,,,,,,,,,,,,") >>extn_fname
	printf("%s,%s,%s,%s,%s,%0.2f\n", date, endtimestr, expdialledno, calldesc, durstr, mround2(callcost)) >> extn_fname
}


################################################
################################################


function handle_extn_change()
{
	# start new extn
	if ( extn != last_extn || eggrid != last_eggrid || gggrid != last_gggrid ) {
		printf("T21 extn_change(): eggrid=%s  extn=%s  t21surname=%s  t21firstname=%s  t21name=%s\n", eggrid, extn, t21surname, t21firstname, t21name)
		get_t21_upper_info(gggrid, eggrid)
		if ( retroBDL != "1" ) {
			if ( extn_fname != "" ) {	# close previous extn file
				if ( !dot21verify ) {
					close(extn_fname)
				}
				extn_fname = ""
			}
		}
		extn_fname = sprintf("topdat/%s/data/extn%s/extn_%s_%s_%s_%s.csv", monthdir, SERVTYPE, CENTRE, PROJECT, SERVTYPE, extn)
		fname_to_SERVTYPECENTREPROJECT_arr[extn_fname] = SERVTYPE "|" CENTRE "|" PROJECT
		get_ggname(gggrid)
		get_egname(eggrid)
		get_dirname(siteid,eggrid,extn)

		if ( dot21verify )
			return
		if ( retroBDL == "1" )
			return
		#printf("    >>> extn_fname = %s\n", extn_fname)
		if ( (getline x < extn_fname) < 0 ) {
			# create file and csv header
			newdir = sprintf("topdat/%s/data/extn%s", monthdir, SERVTYPE)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p %s", newdir, newdir)
			system(cmd)

			printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,", "Extension Call Details", exbillperiod, sdate, edate, t21_CENTREdesc, t21_CENTRE, t21_SERVTYPEdesc, t21_SERVTYPE, t21_PROJECTdesc, t21_PROJECT, egname, eggrid, extn, t21name, t21site, t21location) >>extn_fname

			printf("Date,End Time,Dialled Number,Description,Duration,Total:Float\n") >>extn_fname
		}
		else {
			printf("WARNING %d: extn_fname = %s exists.\n", NR, extn_fname)
		}

	}


}


################################################################
# process T21 detail calls file

function dot21callsfile(t21callsfile,servtype)
{
	printf("dot21callsfile(%s)  servtype=%s\n", t21callsfile, servtype)
	system("date")

	serviceprovider = t21serviceprovider
	printf("   serviceprovider=%s\n", serviceprovider)

	CENTRE = ""
	PROJECT = ""
	SID = ""

	SERVTYPE = servtype
	SERVTYPEdesc = SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]
	print "SERVTYPE = " SERVTYPE "  SERVTYPEdesc = " SERVTYPEdesc

	tmpt21callsfile = t21callsfile

	ggname = ""
	egname = ""
	egccp = ""
	t21name = ""

	last_gggrid = "First Time"
	last_eggrid = "First Time"
	last_extn = -1
	last_siteid = -1

	last_ggname = ""
	last_egname = ""
	last_egccp = ""
	last_t21surname = ""
	last_t21firstname = ""
	last_t21name = ""

	extn_fname = ""

	###############################################
	# process each T21 call

	while ( (getline cdrline < tmpt21callsfile) > 0 ) {
		split(cdrline, cdr_arr, ",")
		##printf("cdrline=%s\n", cdrline)

		#NEG,2672SSA,1,12124,02/06/1996,124820, ,indial,536,0.0

		gggrid = fixforfname(trim(cdr_arr[1]))
		eggrid = fixforfname(trim(cdr_arr[2]))

		######################

		extn = cdr_arr[3]
		date = cdr_arr[4]
		endtime = cdr_arr[5]
		siteid = cdr_arr[6]
		otherno = cdr_arr[7]
		direction = cdr_arr[8]
		dialledno = cdr_arr[9]
		callcat = cdr_arr[10]
		calldesc = cdr_arr[11]
		dur = cdr_arr[12]
		callcost = cdr_arr[13]
                accesscode = trim(cdr_arr[14])

                # outgoing calls with account codes
                if ( direction == "out" && accesscode != "" ) {
                        calldesc = "ACODE " accesscode ":" callcost " " calldesc

			callcost = 0.0  # 0 cost - now in Account Code Charges
                        #continue       # skip
                }

		if ( callcat == "X" )	# dummy categroy
			chg_count = 0
		else
			chg_count = 1

		# rjs9
		supplier = "Fixed Line"
		supplier = t21_supplier " " supplier

		rec_type = "FIXED_CDR"
		chg_cat = fixed_callcat_to_chg_cat[callcat]
		if ( chg_cat == "" )
			chg_cat = "Fixed Other"
		chg_duration = dur
		chg_cost = callcost

		if ( extn <= 0 ) {
			if ( done_warn["bad_extn:" extn] == "") {
				print "WARNING: SKIPPING extn = " extn
				done_warn["extn:" extn] = "1"
			}
			continue
		}

		if ( gggrid == "" )
			gggrid="XXX"

		if ( eggrid == "" )
			eggrid = "YYY"

		t21_GGRID = gggrid
		GGRID = t21_GGRID

		t21_EGRID = eggrid
		EGRID = t21_EGRID

		t21_SID = extn
		SID = t21_SID
		#SID = t21_EGRID ":" t21_SID

		CENTRE = grid_to_centre_arr[eggrid]
		PROJECT = grid_to_project_arr[eggrid]
		## rjs9
		#CENTRE = grid_to_centre_arr[pargrid]
		#PROJECT = grid_to_project_arr[pargrid]

		pargrid = grid_to_parentgroupid_arr[eggrid]

		if ( Testing >= 3 && SID != last_SID ) {
			print " SID change eggrid = " eggrid "  pargrid = " pargrid "  CENTRE = " CENTRE "  PROJECT = "  PROJECT "  SID = " SID
		}

		######################################
		# test if grids from summary data not in hierarchy
		if ( CENTRE == ""  || PROJECT == "" ) {
			print "WARNING:     SKIPPING BAD CENTRE/PROJECT" " eggrid = " eggrid "  CENTRE = " CENTRE "  PROJECT = "  PROJECT "  Extn = " extn
			continue
		}

		# only doing thisCENTRE
		if ( thisCENTRE != "all" && gggrid != thisCENTRE )
			continue

		######################################
		SHIPTO = CENTRE_to_ShipTo_arr[CENTRE]
		GROUP = grid_to_parentgroupid_arr[CENTRE]
		#print "SHIPTO=[" SHIPTO "]" "  GROUP=[" GROUP "]"

		######################################
		if (SERVTYPE == "" || CENTRE == "" || PROJECT == "" || SID == "") {
			#print "WARNING: ERROR: BAD HIERIDx = " HIERIDx
			print "WARNING: ERROR: BAD"
			print "           SERVTYPE = " servtype
			print "             SHIPTO = " SHIPTO
			print "              GROUP = " GROUP
			print "             CENTRE = " centre
			print "            project = " project
			print "                sid = " sid
			continue
		}

		# Testing
		######################
	        #if ( Testing >= 3 && index("|ANN|VLP|VLS|VLO|VLI|VNA|VTC|VNE|VSE|VSW|VRT|", "|" SHIPTO "|") <= 0 ) {
	        if ( Testing >= 3 && index("|A|D|K|S|V|", "|" substr(SHIPTO,1,1) "|") <= 0 ) {
			#print "    TESTING... SKIPPING  SHIPTO=[" SHIPTO "]" "  gggrid=[" gggrid "]" "  eggrid=[" eggrid "]"
			continue
		}
		######################

		sundry = t21_sundry
		tascode = sundryEGRID_to_tascode_arr[sundry "|" eggrid]
		invoiceno = sundryEGRID_to_invoiceno_arr[sundry "|" eggrid]

		# rjs9 - called also in ld_cde()
		set_HIERIDx("t21calls", SERVTYPE,SHIPTO,GROUP,CENTRE,PROJECT,supplier,invoiceno,sundry,SID,rec_type,chg_cat)

		if ( extn != last_extn || eggrid != last_eggrid || gggrid != last_gggrid ) {
			CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" SID] = EGRID
			print "T21 new extn: CENTREPROJECTSID_to_EGRID_arr[" CENTRE "|" PROJECT "|" SID "]=[" CENTREPROJECTSID_to_EGRID_arr[CENTRE "|" PROJECT "|" SID] "]"
		}

		get_t21info(SERVTYPE,t21_EGRID,extn)

		######################################
		# exclude data
		if ( test_exclude(SERVTYPE, siteid, eggrid, extn) )
			continue

		######################################
		#printf("cdrline=[%s]\n", cdrline)

		# set correct call category for internal call
		if ( direction == "in" && callcat == "J" ) {
			callcat = "A"
			calldesc = "Incoming Internal"
		}

		# force dialledno to other extn for Answering Internal KB
		if ( callcat == "A" )
			dialledno = otherno
		# force dialledno to other extn for originating Internal KB
		if ( callcat == "J" )
			dialledno = otherno

		#if ( callcat == "O" ) {	# Outgoing N/W (Tie not shown)
		#	continue
		#}
		#if ( callcat != "F" ) {	# FREE not shown
		#	continue
		#}

		date = fixdate(date)

		durhh = int(dur / 3600)
		remd = dur - (durhh * 3600)
		durmm = int(remd / 60)
		remd = remd - (durmm * 60)
		durss = remd
		durstr = sprintf("%03d:%02d:%02d", durhh, durmm, durss)

		hh = substr(endtime,1,2)
		mm = substr(endtime,3,2)
		ss = substr(endtime,5,2)
		endtimestr = sprintf("%2s:%2s:%2s",hh, mm, ss)

		# remove routetrunk strippdd and ovrcars from dialledno
		origdialledno = dialledno
		if ( direction == "out" && dialledno != "" )
			dialledno = rtdialno(siteid,otherno,dialledno)

		#printf("noddsuparr[%s] = %d\n", gggrid, noddsuparr[gggrid])
		if ( dot21verify || noddsuparr[gggrid] == 0 ) {
			# group has no digit suppression
			expdialledno = dialledno
		}
		else {			# xx for last 2 digits in dialledno if length >= 0
			# supress for these callcategories only
			if ( index("JALMESIOF", callcat) ) {
				dnl = length(dialledno)
				dnpreflen = dnl - 2
				if ( dnpreflen < 0 )
					dnpreflen = 0
				if ( dnl >= 0 ) {
					dnpref = substr(dialledno, 1, dnpreflen)
					expdialledno = sprintf("%sxx", dnpref)
				}
			}
			else {
				expdialledno = dialledno
			}
		}

		handle_extn_change()

		if ( retroBDL != "1" ) {
			if ( !dot21verify ) {
				# skip dummy records (allows for 0 totals in summary data)
				if ( callcat != "X" && !dot21verify )
					spit_cdrrec_to_extnfile()
			}
		}

		######################
		# add to upper totals
		add_totals("TOTAL", SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID, supplier, invoiceno, sundry, rec_type, chg_cat, chg_count, chg_duration, chg_cost)

		##########################################
		# handle management reports for configured distribution lists

		if ( !dot21verify ) {
		    hsid = CENTRE "|" PROJECT "|" SID
		    new_expdialledno = expdialledno
		    if ( chg_count > 0 && new_expdialledno != "" ) {
			if ( callcat == "L" || callcat == "M" || callcat == "E" || callcat == "S" || callcat == "I" || callcat == "F" ) {
				# most frequently dialled numbers for sid
				sid_freq_numbers_count_arr[SERVTYPE "|" hsid "|" new_expdialledno] += chg_count

				# premium service for sid
				if ( is_premium_service(new_expdialledno, SERVTYPE) ) {
					sid_premserv_count_arr[SERVTYPE "|" hsid] += chg_count
					sid_premserv_duration_arr[SERVTYPE "|" hsid] += chg_duration
					sid_premserv_cost_arr[SERVTYPE "|" hsid] += chg_cost
				}
				# Intra-Organisational Call Distribution
				onnettype = is_onnet(new_expdialledno)
			}
			if ( callcat == "O" || callcat == "J" ) {
				# Intra-Organisational Call Distribution
				onnettype = is_onnet(new_expdialledno)
			}
		    }

		    distidlist = mng_egrid_distidlist_arr[EGRID]
		    #print "  distidlist=[" distidlist "]"
		    split(distidlist, distid_arr, "|")
		    for ( i in distid_arr ) {
			DISTID = distid_arr[i]

			htag = DISTID "|" SERVTYPE
			hobj = hsid "|" siteid "|" otherno "|" direction "|" date "|" endtimestr "|" expdialledno "|" callcat "|" calldesc "|" chg_duration "|" chg_cost

			if ( callcat == "L" || callcat == "M" || callcat == "E" || callcat == "S" || callcat == "I" || callcat == "F" ) {
				# most expensiove call
				hval = chg_cost
				hi_in(htag, 20, hi_call_by_cost_arr, hobj, hval, 0)

				if ( chg_count > 0 && new_expdialledno != "" ) {
					# most frequently dialled numbers for distid
					dist_freq_numbers_count_arr[htag "|" new_expdialledno] += chg_count
					dist_freq_numbers_duration_arr[htag "|" new_expdialledno] += chg_duration
					dist_freq_numbers_cost_arr[htag "|" new_expdialledno] += chg_cost
				}

				# call distribution by date
				#cddmm = substr(date,1,5)
				Xtag = htag "|" date
				call_distribution_date_count_arr[Xtag] += chg_count
				call_distribution_date_duration_arr[Xtag] += chg_duration
				call_distribution_date_cost_arr[Xtag] += chg_cost

				if ( is_weekday(date) ) {
					# call distribution by hour of day
					chh = substr(endtime,1,2)
					Xtag = htag "|" chh
					call_distribution_hour_count_arr[Xtag] += chg_count
					call_distribution_hour_duration_arr[Xtag] += chg_duration
					call_distribution_hour_cost_arr[Xtag] += chg_cost
					call_distribution_byhourdate_arr[SERVTYPE "|" date] = 1
				}

				# call distribution by call category
				Xtag = htag "|" chg_cat
				call_distribution_category_count_arr[Xtag] += chg_count
				call_distribution_category_duration_arr[Xtag] += chg_duration
				call_distribution_category_cost_arr[Xtag] += chg_cost

			}
			# Intra-Organisational Call Distribution
			if ( onnettype != "" ) {
				intracat = "Extension to " onnettype
				Xtag = htag "|" intracat
				call_distribution_category_count_arr[Xtag] += chg_count
				call_distribution_category_duration_arr[Xtag] += chg_duration
				call_distribution_category_cost_arr[Xtag] += chg_cost
			}

			# longest duration call
			hval = chg_duration
			hi_in(htag, 20, hi_call_by_duration_arr, hobj, hval, 0)
		    }
		}

		######################################
		last_gggrid = gggrid
		last_eggrid = eggrid
		last_extn = extn
		last_siteid = siteid

		last_SID = SID
		last_ggname = ggname
		last_egname = egname
		last_egccp = egccp
		last_t21surname = t21surname
		last_t21firstname = t21firstname
		last_t21name = t21name

	}
	close(tmpt21callsfile)

	###############################################
	# finish up

	print ""
	print "finished dot21callsfile() - " tmpt21callsfile

}


# END T21CALLS
#####################################################
#####################################################
#----------------------------------------------------------


#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------
# main


BEGIN {
	if ( logdir == "" )
		logdir = "./"
	Testing = 0
	testmobdet = 0
	LinkUserHTMLindex = 0

	# set bill period vars
	MMYYYY_to_billperiod(MMYYYY,"_")

	YYYYMM = substr(MMYYYY,3,4) substr(MMYYYY,1,2)

	TBSBillingPeriod = substr(MMYYYY,3,4) "/" substr(MMYYYY,1,2)

	YYYY = 0 + substr(YYYYMM,1,4)
	MM = 0 + substr(YYYYMM,5,2)
	rfmtexbillperiod = sprintf("%04d-%02d", YYYY, MM)

	print "Begin wb4 - dot21verify=" dot21verify "  thisCENTRE=" thisCENTRE
#rjs9
        print "          -            retroBDL = " retroBDL
        print "          -             RESTcde = " RESTcde
        print "          -       platinumBATCH = " platinumBATCH
        print "          - switchboardcallcost = " switchboardcallcost
        print "          -   voicemailcallcost = " voicemailcallcost
 	print "          -              MMYYYY = " MMYYYY
 	print "          -          billperiod = " billperiod
 	print "          -              YYYYMM = " YYYYMM

        if ( thisSHIPTO == "" ) {
		thisSHIPTO = "all"
	}

        if ( platinumBATCH == "" ) {
                platinumBATCH = "all"
        }

        inp_platinumBATCH = platinumBATCH
        #rjs9batchtag = billperiod "_" inp_platinumBATCH "_" thisSHIPTO
        #batchtag = billperiod "_" "all" "_" thisCENTRE
        #batchtag = "all" "_" thisCENTRE
        batchtag = thisCENTRE

	system("date")

	"date '+%d/%m/%Y'" | getline cur_dd_mm_yyyy

	#------------------------
	t21serviceprovider = "TELMAX21"
        t21_supplier = "VICTRACK"
        t21_sundry = ""
#rjs9

	#------------------------
	sdate = fixdate(sdate)
	edate = fixdate(edate)

	days_in_period = 0 + substr(edate,1,2)

	monthdir = monthtag
	printf("monthdir = %s\n", monthdir)


#rjs9
	if ( ! dot21verify ) {

		# ned to know how many fields in cde
		# before ld_platinv()
		get_cdeflds(cdefile)

		#------------------------
		# load platinv file
		ld_platinv(platinvfile)

		print "platinumBATCH = " platinumBATCH
		if ( platinumBATCH != "all" ) {
			# ShipTo and/or sundry specified
			if (thisSHIPTO == "all" && length(platinumBATCH) <= 6) {
				# get platinum BATCH for this sundry
				sundryBATCH = platinumBATCH
				platinumBATCH = ""
				print "  Get Platinum Batch for Specified Sundry = " sundryBATCH

				for (cstr in parent_sundry_to_platinv_batch_arr)
				{
					split(cstr, cstr_arr, "|")
					parent = cstr_arr[1]
					sundry = cstr_arr[2]
					elementid = cstr_arr[3]
					invoivedetailid = cstr_arr[4]

A
					if ( sundry != sundryBATCH )
						continue

					# ShipTo & sundry specified
					if ( thisSHIPTO != "all" && thisSHIPTO != parent )
						continue

					foundpB = parent_sundry_to_platinv_batch_arr[parent "|" sundry "|" elementid "|" invoicedetailid]
					print "  found ... parent_sundry_to_platinv_batch_arr[" parent "|" sundry "|" elementid "|" invoicedetailid"] = " parent_sundry_to_platinv_batch_arr[parent "|" sundry "|" elementid "|" invoicedetailid]
					if ( platinumBATCH != "" && platinumBATCH != foundpB ) {
						print "FATAL ERROR: Non Unique Platinum BATCH for Specified Sundry = " sundryBATCH
						exit 1
					}
					platinumBATCH = foundpB
				
				}
				if ( platinumBATCH == "" ) {
					print "FATAL ERROR: Can't get Platinum BATCH for Specified Sundry = " sundryBATCH
					exit 1
				}
			}

			# invoicenumber specified
			if (length(platinumBATCH) >= 7 && length(platinumBATCH) <= 10) {
				# get platinum BATCH for this invoicenumber
				invoicenumberBATCH = platinumBATCH
				platinumBATCH = ""
				print "  Get Platinum Batch for Specified Invoicenumber =  " invoicenumberBATCH
				for (cstr in parent_invoicenumber_to_platinv_batch_arr)
				{
					split(cstr, cstr_arr, "|")
					parent = cstr_arr[1]
					invoiceno = cstr_arr[2]
					if ( invoiceno != invoicenumberBATCH )
						continue
					foundpB = parent_invoicenumber_to_platinv_batch_arr[parent "|" invoiceno]
					print "  found ... parent_invoicenumber_to_platinv_batch_arr[" parent "|" invoiceno "] = " parent_invoicenumber_to_platinv_batch_arr[parent "|" invoiceno]
					if ( platinumBATCH != "" && platinumBATCH != foundpB ) {
						print "FATAL ERROR: Non Unique Platinum BATCH for Specified Invoicenumber = " invoicenumberBATCH
						exit 1
					}
					platinumBATCH = foundpB
				
				}
				if ( platinumBATCH == "" ) {
					print "FATAL ERROR: Can't get Platinum BATCH for Specified Invoicenumber = " invoicenumberBATCH
					exit 1
				}
			}

		}
	}

#rjs9
#	this_shiptogroup = exwebconf_parent_to_cdgroup_arr[SHIPTO]
#	if ( this_shiptogroup != "" )	# belongs to a cdgroup
#		act_targdir = "topdat" "/" billperiod "/" platinumBATCH "_" this_shiptogroup
#	else
#		act_targdir = "topdat" "/" billperiod "/" platinumBATCH "_" thisSHIPTO

# rjs9
#	if ( ! dot21verify ) {
#		lokfile = act_targdir ".lok"
#		if ( (getline x < lokfile) < 0 ) {
#			print "Running" > lokfile
#		}
#		else {
#			print "FATAL ERROR: already running lokfile = " lokfile
#			exit 1
#		}
#	}

#rjs9-
#	# make symlink if batch run with invoice number or shipto/sundry
#	if ( platinumBATCH != inp_platinumBATCH ) {
#		dirloc = "topdat" "/" monthtag
#		this_shiptogroup = exwebconf_parent_to_cdgroup_arr[thisSHIPTO]
#		if ( this_shiptogroup != "" ) {	# belongs to a cdgroup
#			relact_targdir = platinumBATCH "_" this_shiptogroup
#			symlink = inp_platinumBATCH "_" this_shiptogroup
#		}
#		else {
#			relact_targdir = platinumBATCH "_" thisSHIPTO
#			symlink = inp_platinumBATCH "_" thisSHIPTO
#		}
#		print "Make symlink  dirloc=" dirloc "  relact_targdir=" relact_targdir "  symlink=" symlink
#		cmd = "cd " dirloc ";" "ln -s \"" relact_targdir "\" \"" symlink "\""
#		system(cmd)
#	}
#-rjs9

	if ( platinumBATCH == "all" ) {
		print "INFO: Running for all platinum batch filenames"
	}
	else {
		print "INFO: Running for platinum batch filename = " platinumBATCH
	}
#rjs9



	#------------------------
        # load account code conf data
        ld_acccodeconf(acccodeconffile)

	#------------------------
	# load mobile conf data
	ld_mobconf(mobconffile)

	#------------------------
	# load chargecat data
	ld_chargecat(chargecatfile)

	#------------------------
	# load t21 route trunk data
	ld_t21rt(t21rtfile)

	#------------------------
	# load t21 extranges data
	ld_t21extranges(t21extrangesfile)

	#------------------------
	# load t21 groups data
	ld_t21groups4(t21groups4file)

	#------------------------
	# load t21 directory data
	ld_t21dircsvfile(t21dircsvfile)

	grid_to_parentgroupid_arr["XXX"] = "ZZZ"
	grid_to_grtype_arr["XXX"] = "G"
	grid_to_grname1_arr["XXX"] = "XXX Group Not Found"
	grid_to_grname2_arr["XXX"] = "XXX Group Not Found"
	grid_to_grname3_arr["XXX"] = "XXX Group Not Found"
	grid_to_ccp_arr["XXX"] = "XXXXX.XXXXX"
	grid_to_centre_arr["XXX"] = "XXXXX"
	grid_to_project_arr["XXX"] = "XXXXX"
	childcount_arr["XXX"] = 1

	grid_to_parentgroupid_arr["YYY"] = "XXX"
	grid_to_grtype_arr["YYY"] = "E"
	grid_to_grname1_arr["YYY"] = "YYY Group Not Found"
	grid_to_grname2_arr["YYY"] = "YYY Group Not Found"
	grid_to_grname3_arr["YYY"] = "YYY Group Not Found"
	grid_to_ccp_arr["YYY"] = "YYYYY.YYYYY"
	grid_to_centre_arr["YYY"] = "YYYYY"
	grid_to_project_arr["YYY"] = "YYYYY"

	#-----------------------------------------------------
	# setup service type desc array

	SERVTYPE_to_SERVTYPEdesc_arr["All"] = "All Batch Types"

	st = 10
	SERVTYPE_to_GLcode_arr[st] = 0
	SERVTYPE_to_SERVTYPEdesc_arr[st] = "Mobile Calls and Charges"
	if ( SERVTYPE_to_GLcode_arr[st] != 0 )
		SERVTYPE_to_SERVTYPEdesc_arr[st] = SERVTYPE_to_SERVTYPEdesc_arr[st] " (" SERVTYPE_to_GLcode_arr[st] ")"
	mobdummy_cost = 0

        st = 5
        SERVTYPE_to_GLcode_arr[st]  = 0
        SERVTYPE_to_SERVTYPEdesc_arr[st] = "Account Code Charges"
        if ( SERVTYPE_to_GLcode_arr[st] != 0 )
                SERVTYPE_to_SERVTYPEdesc_arr[st] = SERVTYPE_to_SERVTYPEdesc_arr[st] " (" SERVTYPE_to_GLcode_arr[st] ")"

	st = 4
	SERVTYPE_to_GLcode_arr[st]  = 0
	SERVTYPE_to_SERVTYPEdesc_arr[st] = "Overhead Charges"
	if ( SERVTYPE_to_GLcode_arr[st] != 0 )
		SERVTYPE_to_SERVTYPEdesc_arr[st] = SERVTYPE_to_SERVTYPEdesc_arr[st] " (" SERVTYPE_to_GLcode_arr[st] ")"

	st = 3
	SERVTYPE_to_GLcode_arr[st]  = 0
	SERVTYPE_to_SERVTYPEdesc_arr[st] = "Miscellaneous Charges"
	if ( SERVTYPE_to_GLcode_arr[st] != 0 )
		SERVTYPE_to_SERVTYPEdesc_arr[st] = SERVTYPE_to_SERVTYPEdesc_arr[st] " (" SERVTYPE_to_GLcode_arr[st] ")"

	st = 2
	SERVTYPE_to_GLcode_arr[st]  = 0
	SERVTYPE_to_SERVTYPEdesc_arr[st] = "Equipment and Service Charges"
	#SERVTYPE_to_SERVTYPEdesc_arr[st] = SERVTYPE_to_SERVTYPEdesc_arr[st] "EXTNCNT"
	if ( SERVTYPE_to_GLcode_arr[st] != 0 )
		SERVTYPE_to_SERVTYPEdesc_arr[st] = SERVTYPE_to_SERVTYPEdesc_arr[st] " (" SERVTYPE_to_GLcode_arr[st] ")"

	st = 1
	SERVTYPE_to_GLcode_arr[st]  = 0
	SERVTYPE_to_SERVTYPEdesc_arr[1] = "Fixed Line Call Charges"
	if ( SERVTYPE_to_GLcode_arr[st] != 0 )
		SERVTYPE_to_SERVTYPEdesc_arr[st] = SERVTYPE_to_SERVTYPEdesc_arr[st] " (" SERVTYPE_to_GLcode_arr[st] ")"

	#-----------------------------------------------------
	# tims fixed call categories
	fixed_callcat_to_chg_cat["J"] = "Fixed Originating Internal"
	fixed_callcat_to_chg_cat["A"] = "Fixed Answering Internal"
	fixed_callcat_to_chg_cat["L"] = "Fixed To Local"
	fixed_callcat_to_chg_cat["M"] = "Fixed To Mobile"
	fixed_callcat_to_chg_cat["E"] = "Fixed To Information"
	fixed_callcat_to_chg_cat["S"] = "Fixed To STD"
	fixed_callcat_to_chg_cat["I"] = "Fixed To IDD"
	fixed_callcat_to_chg_cat["N"] = "Fixed Incoming Network"
	fixed_callcat_to_chg_cat["P"] = "Fixed Incoming Public"
	fixed_callcat_to_chg_cat["O"] = "Fixed Outgoing Network"
	fixed_callcat_to_chg_cat["F"] = "Fixed Free"
	fixed_callcat_to_chg_cat["X"] = "Fixed From Directory"

	#------------------------------------------------
	system("date")

	totsprefix = "ex4_"
	if ( dot21verify ) {
		totsprefix = "ex4a_"
	}
	if ( retroBDL == "1" ) {
		totsprefix = "ex4r_"
	}
	totalscsvfile = totsprefix "totcsv_" thisCENTRE
	totalsrepfile = totsprefix "totrep_" thisCENTRE ".txt"

	cmd=sprintf("rm -f %s %s*.csv", totalsrepfile, totalscsvfile)
	print "removeing: cmd=" cmd
	system(cmd)

	printf("Bilmax21 Web Billing Extraction-4%s (%s)  Totals For %s\r\n\r\n", dot21verify ? "a (VERIFY)" : ((retroBDL == "1") ? "r (Retrospective)" : ""), thisCENTRE, exbillperiod) >>totalsrepfile
        #rjs9 printf("VICTRACK Web Billing3 (%s) %s Totals For %s\r\n\r\n", dot21verify ? "a (VERIFY)" : "", batchtag, exbillperiod) >>totalsrepfile
        if ( !dot21verify ) {
                printf("Invoice Batch: %s\r\n", platinumBATCH) >>totalsrepfile;
                printf("Switchboard Call Cost: %s\r\n", switchboardcallcost) >>totalsrepfile;
                printf("\r\n") >>totalsrepfile
                printf("CDE Data\r\n") >>totalsrepfile
                printf("\r\n") >>totalsrepfile
        }
	close(totalsrepfile)

	if ( !dot21verify ) {
		printf("Totals Report\r\n") >>totalsrepfile
		printf("=============\r\n") >>totalsrepfile
		printf("\r\n") >>totalsrepfile
	}
	else {
		printf("Verify Data Totals Report\r\n") >>totalsrepfile
		printf("=========================\r\n") >>totalsrepfile
		printf("\r\n") >>totalsrepfile
	}
	close(totalsrepfile)

	#-----------------------------------------------------------------
#rjs9
        if ( !dot21verify ) {
		# tbsdata database
		ld_tbsdata_customer(tbsCustomerfile)
		ld_tbsdata_servicetype(tbsServiceTypefile)
		ld_tbsdata_element(tbsElementfile)
		ld_tbsdata_supplier(tbsSupplierfile)
		ld_tbsdata_person(tbsPersonfile)

		#-----------------------------
		# tbs person service list data
		ld_tbsdata_personservicelist(tbsPersonServiceListfile)
	}

# rjs9
	## to translate VRT seid for drill down
	#ld_seidtranslate("indata/" monthtag "/seidtranslate.csv")

	#-----------------------------------------------------------------
	# load t21 Web Billing Distribution Lists
	ld_t21WBDL(t21WBDLfile)

	#------------------------
	# load t21 Web Billing Distribution site/grid/extn Eclusion List
	ld_t21wbdexclude(t21wbdexcludefile)

	####################################################################
	## generate t21 verification report and csv
	#system("date")
	if ( dot21verify ) {
#rjs9
		print ""
		print "Doing T21 Verification Report/CSV and Back Billing"
		print ""
		#dom11cdrfile(m11cdrfile)
		#domobdetfile(mobcallsfile,10)
                #dot21acccodecallsfile(t21acccodecallsfile,5)
		#dot21ovhfile(t21ovhfile,2,3,-1)
		dot21callsfile(t21callsfile,1)
		exit 0

	}

	#-----------------------------------------------------
	system("date")
	HIERIDx = ""
#rjs9
	if ( retroBDL != "1" )
		HIERIDxfile = "HIERIDx_" batchtag ".lst"
	else
		HIERIDxfile = "HIERIDx_" batchtag ".rlst"
        #system("rm -f HIERIDx_" batchtag ".lst")
        system("rm -f sundrys_" batchtag ".uniq")
        system("rm -f DEPT_" batchtag ".lst")


	####################################################################

	#-----------------------------------------------------------------
	# load exwebconffile - get groups with NO dialled digit suppression
	ld_exwebconf(exwebconffile)

	#-----------------------------------------------------------------
	# create t21 serviceid list from t21 call files
	ld_t21callcallserviceids(t21callsfile,   101043)
	ld_t21swbcallsserviceids(t21swcallsfile, 101030)
	ld_t21vmlcallsserviceids(t21vmcallsfile, 101047)

	#------------------------

	got_cde_data = 0

	# now data comes from tbsdata (see getcde scripts)
#rjs9
	ld_cde(cdefile)

	if ( got_cde_data != 1 ) {
		print "FATAL ERROR: NO CDE data loaded (error in CDE or NO matching invoice data)"
		exit 1
	}
#-rjs9

	####################################################################
	# create billperiod/data
	top_data_dir = sprintf("topdat/%s/data", monthdir)
	cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", top_data_dir, top_data_dir)
	system(cmd)

	####################################################################
	# clear releasedate and remove any old data for retrospective lists

	if ( !dot21verify ) {
	    if ( retroBDL == "1" ) {
		print "BMRETRO: Retrospective Generation/Regeneration of Distribution List(s)"
		print "BMRETRO: "
		for ( DISTID in distid_to_distiddesc_arr ) {
			DISTIDdesc = distid_to_distiddesc_arr[DISTID]
			t21name = distid_to_t21name_arr[DISTID]
			t21userid = distid_to_userid_arr[DISTID]
			print "BMRETRO: DISTID: " DISTID " " DISTIDdesc " for " t21name " (" t21userid ")"
			# clear releasedate
			spit_header_to_wbttopfile()
			spit_USER_to_wbttopfile()
			# remove data
			cmd=sprintf("rm -fr topdat/%s/%s", billperiod, DISTID)
			printf("Running [%s]\n", cmd)
			system(cmd)
		}
	    }
	}

	#-----------------------------------------------------------------
	if ( !dot21verify ) {
		# create egrid distid list array
		create_egrid_distidlist()

		print ""
		system("date")
		print "standard/admin egrid distid lists"
		for (egrid in egrid_distidlist_arr) {
			print "egrid_distidlist_arr[" egrid "]=[" egrid_distidlist_arr[egrid] "]"
		}

		print ""
		system("date")

		print "management egrid distid lists"
		for (egrid in mng_egrid_distidlist_arr) {
			print "mng_egrid_distidlist_arr[" egrid "]=[" mng_egrid_distidlist_arr[egrid] "]"
		}
	}

	#-----------------------------------------------------------------
	if ( !dot21verify ) {
		# create virtual user distribution list entries
		if ( retroBDL != "1" )
			create_virtual_user_dist_lists()
	}

	#-----------------------------------------------------
	#-----------------------------------------------------
	# process M11CDR detail calls file
	dom11cdrfile(m11cdrfile)

	#-----------------------------------------------------
	# process MOBILE carrier detail calls file
	domobdetfile(mobcallsfile, 10)

	#-----------------------------------------------------
	# process T21 access/account code calls file
	#dot21acccodecallsfile(t21acccodecallsfile,5)

	#-----------------------------------------------------
	# process T21 overheads file E/M/O
	#dot21ovhfile(t21ovhfile, 2, 3, 4)

	#-----------------------------------------------------
	# process T21 calls file
	dot21callsfile(t21callsfile, 1)

	#-----------------------------------------------------
	#-----------------------------------------------------
	if ( !dot21verify ) {
	    #-----------------------------------------------------
	    # process management highest usage frequently dialled number lists
	    # create freq dial numbers for each distid
	    for ( hdistdialtag in dist_freq_numbers_count_arr ) {
		dialcnt = dist_freq_numbers_count_arr[hdistdialtag]
		split(hdistdialtag, dfnc1_arr, "|")
		DISTID = dfnc1_arr[1]
		SERVTYPE = dfnc1_arr[2]
		dialledno = dfnc1_arr[3]

		htag = DISTID "|" SERVTYPE
		hobj = dialledno
		hval = dialcnt
		hi_in(htag, 20, hi_dist_freqdial_arr, hobj, hval, 0)
		#if ( DISTID == "mng1001" ) {
		#	print "SFDBG1:  set dist_freq_numbers_count_arr[" hdistdialtag "]=[" dist_freq_numbers_count_arr[hdistdialtag] "]"
		#}
	    }

	    # create list of servtype, freq dials
	    # to speed up next loop
	    for ( Htag in hi_dist_freqdial_arr ) {
		split(Htag, hdf1_arr, "|")
		distid = hdf1_arr[1]
		dist_servtype = hdf1_arr[2]
		hi_idx = hdf1_arr[3]
		split(hi_dist_freqdial_arr[Htag], hdf2_arr, "|")
		dist_dialcnt = hdf2_arr[1]
		dist_dialno = hdf2_arr[2]
		hi_freq_dial[dist_servtype "|" dist_dialno] = 1
		#if ( distid == "mng1001" ) {
		#	print "SFDBG2:  speedup hi_dist_freqdial_arr[" Htag "]=[" hi_dist_freqdial_arr[Htag] "]"
		#}
	    }

	    # get sid with highest usage for each distid's freq dial numbers
	    print "get sid for freq dial"
	    system("date")
	    for ( Stag in sid_freq_numbers_count_arr ) {
		split(Stag, sdfnc_arr, "|")
		servtype = sdfnc_arr[1]
		sid_centre = sdfnc_arr[2]
		sid_project = sdfnc_arr[3]
		sid = sdfnc_arr[4]
		dialno = sdfnc_arr[5]

		#print "SFDBG3: sid sid_freq_numbers_count_arr[" Stag "]=[" sid_freq_numbers_count_arr[Stag] "]"

		# speed up
		if ( hi_freq_dial[servtype "|" dialno] != 1 )
			continue

		dialcnt = sid_freq_numbers_count_arr[Stag]

		# rjs9 centre|project has multiple egrids
		#egrid = CENTREPROJECT_to_EGRID_arr[sid_centre "|" sid_project]
		egrid = CENTREPROJECTSID_to_EGRID_arr[sid_centre "|" sid_project "|" sid]
		##if ( dirextn_to_recordno_arr[sid] != "" ) {
		#	print "SFDBG4:  CENTREPROJECTSID_to_EGRID_arr[" sid_centre "|" sid_project "|" sid "]=[" CENTREPROJECTSID_to_EGRID_arr[sid_centre "|" sid_project "|" sid] "]"
		#	print "         egrid=[" egrid "]"
		##}

		for ( Htag in hi_dist_freqdial_arr ) {
			split(Htag, hdf3_arr, "|")
			distid = hdf3_arr[1]
			dist_servtype = hdf3_arr[2]
			hi_idx = hdf3_arr[3]

			#if ( distid == "mng1001" ) {
			#	print "SFDBG5:    hi_dist_freqdial_arr[" Htag "]=[" hi_dist_freqdial_arr[Htag] "]"
			#	print "           distid_allegrid_arr[" distid "|" egrid "]=[" distid_allegrid_arr[distid "|" egrid] "]"
			#}

			# test if egrid for sid belongs distid freq list
			if ( distid_allegrid_arr[distid "|" egrid] != 1 )
				continue

			split(hi_dist_freqdial_arr[Htag], hdf3val_arr, "|")
			dist_dialcnt = hdf3val_arr[1]
			dist_dialno = hdf3val_arr[2]
			dist_sid_centre = hdf3val_arr[3]
			dist_sid_project = hdf3val_arr[4]
			dist_sid = hdf3val_arr[5]
			dist_sid_dialcnt = hdf3val_arr[6]

			if ( dist_sid_dialcnt == "" )
				dist_sid_dialcnt = 0

			#if ( distid == "mng1001" ) {
			#	print "SFDBG6:      here2 dist_sid=[" dist_sid "]" "  dist_sid_dialcnt=[" dist_sid_dialcnt "]"
			#	print "             sid=[" sid "]" "  dialcnt=[" dialcnt "]"
			#	print "             servtype=[" servtype "]" "  dist_servtype=[" dist_servtype "]"
			#	print "             dialno=[" dialno "]" "  dist_dialno=[" dist_dialno "]"
			#	print "             dialcnt=[" dialcnt "]" "  dist_sid_dialcnt=[" dist_sid_dialcnt "]"
			#}

			if ( servtype == dist_servtype && ("x" dialno) == ("x" dist_dialno) ) {
				if ( dialcnt > dist_sid_dialcnt ) {
					hi_dist_freqdial_arr[Htag] = dist_dialcnt "|" dist_dialno "|" sid_centre "|" sid_project "|" sid "|" dialcnt
					#if ( distid == "mng1001" ) {
					#	print "SFDBG7:      hi_dist_freqdial_arr[" Htag "]=[" hi_dist_freqdial_arr[Htag] "]"
					#}

				}
			}
		}
	    }
	    system("date")
	}

	#-----------------------------------------------------
	#-----------------------------------------------------
	print "get processing order from sorted HIERIDx file"
	print "and generate distid totals"
	system("date")

	close(HIERIDxsortcmd)
	HIERIDxorder = 0
	while ( (getline HIERIDx < HIERIDxfile) > 0 ) {
		HIERIDxorder_to_HIERIDx_arr[++HIERIDxorder] = HIERIDx
		#print HIERIDxorder "  HIERIDx = " HIERIDx
		if ( !dot21verify ) {
			# generate distid totals
			add_distid_totals()

			# for cde directory entrie
			if ( Xhieridxfrom == "cde" ) {
				if ( sid_sundry_to_HIERIDxorderlist_arr[Xsid "|" Xsundry] != "" )
					sid_sundry_to_HIERIDxorderlist_arr[Xsid "|" Xsundry] = sid_sundry_to_HIERIDxorderlist_arr[Xsid "|" Xsundry] "|"
				sid_sundry_to_HIERIDxorderlist_arr[Xsid "|" Xsundry] = sid_sundry_to_HIERIDxorderlist_arr[Xsid "|" Xsundry] HIERIDxorder
			}
		}
	}
	close(HIERIDxfile)
	HIERIDxmaxorder = HIERIDxorder
	print "HIERIDxmaxorder = " HIERIDxmaxorder


	#-----------------------------------------------------
	print "generate upper data files"
	system("date")

	last_HIERIDxDUPcount = ""
	last_HIERIDx = ""
	last_HIERIDxfrom = ""
	last_serviceprovider = ""
	last_SERVTYPE = ""
	last_SHIPTO = ""
	last_GROUP = ""
	last_CENTRE = ""
	last_PROJECT = ""
	last_GGRID = ""
	last_EGRID = ""
	last_SID = ""

	disp_project = ""
	addstar = ""
	projectfilter = ""
	ccpdesc = ""

	#########################

	printf("\n")
	for ( HIERIDxorder = 1; HIERIDxorder <= HIERIDxmaxorder; ++HIERIDxorder ) {
		HIERIDx = HIERIDxorder_to_HIERIDx_arr[HIERIDxorder]

		print ""
		print "DO HIERIDx = " HIERIDx

		get_HIERIDx_info(HIERIDx, 1)

                # do this to set t21_extn, before call to get_t21info()
                if ( SERVTYPE == 10 )
                        get_mobileinfo(1,SERVTYPE,t21_SID,CENTRE)
                else
                if ( SERVTYPE == 5 ) {
                        t21_acccode = t21_SID
                        t21_extn = "All"
			get_t21info(SERVTYPE,t21_EGRID,t21_extn)
                }
                else {
                        t21_extn = t21_SID
			get_t21info(SERVTYPE,t21_EGRID,t21_extn)
                }


		if ( HIERIDx == last_HIERIDx ) {	# skip duplicates
			print "WARNING: duplicate HIERIDx: " HIERIDx
			continue
		}


		################

		print_totals(HIERIDxfrom, SERVTYPE, SHIPTO, GROUP, CENTRE, PROJECT, SID)

		if ( dot21verify && retroBDL != "1" ) {
		    # do extension totals to back billing
		    ###################################
		    # new bb1
		    # - includes all service types
		    # newbb1 BBBBBB

                    # except account codes
                    if ( SERVTYPE != "5" ) {
			# test CENTRE/PROJECT changed
			if ( last_SERVTYPE != SERVTYPE || last_CENTRE != CENTRE || last_PROJECT != PROJECT ) {
				# - spit invoice GST CENTRE totals
				if ( newlast_inv_grid != "" ) {
					spit_newbb1_inv_totGST()
				}
			}
			if ( !done_newbb1_egid_sid_servtype_arr[EGRID "|" SID "|" SERVTYPE] ) {
				# - spit sid total
				spit_newbb1(EGRID, SID, SERVTYPE)
				done_newbb1_egid_sid_servtype_arr[EGRID "|" SID "|" SERVTYPE] = 1
			}
		    }
		}


		##################
		
		# do project totals csv data and totals report
		if ( last_SERVTYPE != SERVTYPE || last_CENTRE != CENTRE || last_PROJECT != PROJECT ) {
			if ( last_PROJECT != "" ) {
				spit_totals_rep_project()
				spit_totals_csvdata()
				print_totals(last_HIERIDxfrom, last_SERVTYPE, last_SHIPTO, last_GROUP, last_CENTRE, last_PROJECT, "All")
			}
		}

		# do totals report for centre 
		if ( last_SERVTYPE != SERVTYPE || last_CENTRE != CENTRE ) {
			if ( last_CENTRE != "" ) {
				spit_totals_rep_centre()
				print_totals(last_HIERIDxfrom, last_SERVTYPE, last_SHIPTO, last_GROUP, last_CENTRE, "All", "All")
			}
		}

		# do totals report for servicetype 
		if ( last_SERVTYPE != SERVTYPE ) {
			if ( last_SERVTYPE != "" ) {
				spit_totals_rep_servtype()
				print_totals(last_HIERIDxfrom, last_SERVTYPE, last_SHIPTO, last_GROUP, last_CENTRE, "All", "All")
			}
			if ( SERVTYPE != "" ) {
				printf("\r\n", SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]) >>totalsrepfile
				printf("Batch Type: %s\r\n", SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]) >>totalsrepfile
				printf("======================================================\r\n", SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]) >>totalsrepfile
				printf("\r\n\r\n", SERVTYPE_to_SERVTYPEdesc_arr[SERVTYPE]) >>totalsrepfile
			}
		}

		################

		#for ( cstr in donetotal_arr ) {
		#	delete donetotals_arr[Xtotid]
		#}

		##############################

		if ( !dot21verify ) {

		    print ""
		    print "process configured distribution lists"
		    distidlist = egrid_distidlist_arr[EGRID]
		    print "  distidlist=[" distidlist "]"
		    split(distidlist, distid_arr, "|")
		    for ( i in distid_arr ) {
			DISTID = distid_arr[i]

			DISTIDdesc = distid_to_distiddesc_arr[DISTID]
			t21_dist_name = distid_to_t21name_arr[DISTID]
			t21userid = distid_to_userid_arr[DISTID]
			if ( t21userid == "" )
				t21userid = distid_to_directoryID_arr[DISTID]

			print "  HANDLE DISTID = " DISTID
			print "      GGRID = " GGRID "  EGRID = " EGRID
			print "      CHILDCOUNT = " CHILDCOUNT

			DISP_PROJECT = PROJECT
			addstar = ""

			# check if EGRID is in distid's distgrid list and
			# is 1 of many egrids(projects) in centre then addstar
 			if ( distid_egrid_arr[DISTID "|" EGRID] == 1 ) {
				if ( is_CENTRE_dot_PROJECT_arr[EGRID] ) {
					# more than 1 project in centre
					if ( CHILDCOUNT > 1 ) {
						addstar = "*"
						print "        ADDSTAR"
					}
				}
			}

			print "    PROJECT=" PROJECT "  addstar=[" addstar "]"

			# create distid dir
			distdir = sprintf("topdat/%s/%s/data/distid", monthdir, DISTID)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", distdir, distdir)
			system(cmd)

# no totals in distidfile
#			# sid totals to distidfile
#			if ( done_distidfile_totals_arr[DISTID "|" SERVTYPE "|" CENTRE "|" PROJECT "|" SID] == "" ) {
#				distidfile_totals()
#				done_distidfile_totals_arr[DISTID "|" SERVTYPE "|" CENTRE "|" PROJECT "|" SID] = 1
#			}

			if ( distid_to_wbd_type_arr[DISTID] == "MANAGEMENT" ) {
			    print "here1"
			    # don't look at TOTALTBS or Directory recs
			    #if ( substr(HIERIDxfrom,1,3) != "cde" && CHG_CAT != "X" ) {
			    if ( CHG_CAT != "X" ) {
				# do highest usage for distid
				if ( last_SERVTYPE != SERVTYPE || last_CENTRE != CENTRE || last_PROJECT != PROJECT || last_SID != SID) {
					# do highest user
					do_highest_usage(DISTID,SERVTYPE,SHIPTO,CENTRE,PROJECT,EGRID,SID)
				}
				done_DISTID_totals_arr[DISTID] = "1"
				continue
			    }
			}

			# for mobiles use t21 detail totals to get
			# chg cat totals for the DISTID file pie chart
			if ( SERVTYPE == 10 ) {
				if (substr(HIERIDxfrom,1,3) == "t21")
					handle_DISTID_datafile()
			}
			if ( HIERIDxfrom == "cde" ) {
				if ( SERVTYPE != 10 ) {
					handle_DISTID_datafile()
				}

				#handle_CENTRE_datafile()

				handle_REPORTGROUP_datafile()

				handle_CUSTOMERIMPORT_datafile()

				handle_CUSTOMERIMPORT_detail_summary_datafile()

				# store shipto/invoiceno list for distid
				if ( INVOICENO != "VRT999999" && done_distid_shiptoinvoiceno_arr[DISTID "|" SHIPTO "|" INVOICENO] == "" ) {
					if ( distid_to_shiptoinvoicenolist_arr[DISTID] != "" ) {
						distid_to_shiptoinvoicenolist_arr[DISTID] = distid_to_shiptoinvoicenolist_arr[DISTID] "|"
					}
					distid_to_shiptoinvoicenolist_arr[DISTID] = distid_to_shiptoinvoicenolist_arr[DISTID] SHIPTO "~" INVOICENO
					#print "store: distid_to_shiptoinvoicenolist_arr[" DISTID "]=[" distid_to_shiptoinvoicenolist_arr[DISTID] "]"
					done_distid_shiptoinvoiceno_arr[ DISTID "|" SHIPTO "|" INVOICENO] = "1"
				}

			}

			# handle SERVTYPE totals
			#if ( SERVTYPE < 100 ) {
			## mobile totals from cde
			#if ( SERVTYPE == 10 ) {
			#	if ( HIERIDxfrom == "cde" )
			#		handle_SERVTYPE_datafile()
			#}
			#else {
			#	if ( substr(HIERIDxfrom,1,3) == "t21" )
			#		handle_SERVTYPE_datafile()
			#}
			# from t21
			if ( substr(HIERIDxfrom,1,3) == "t21" )
				handle_SERVTYPE_datafile()

			if ( HIERIDxfrom == "cde" ) {

				handle_swb_datafiles()

				handle_vml_datafiles()
			}

			if ( last_SERVTYPE != SERVTYPE || last_CENTRE != CENTRE || last_PROJECT != PROJECT ) {
				detail_fnames_to_fnamelist(SERVTYPE "|" CENTRE "|" PROJECT)
			}

			done_DISTID_totals_arr[DISTID] = "1"
		    }
		    print ""
		}

		if ( !dot21verify ) {
		    print ""
		    print "process management distribution lists"
		    distidlist = mng_egrid_distidlist_arr[EGRID]
		    print "  distidlist=[" distidlist "]"
		    split(distidlist, distid_arr, "|")
		    for ( i in distid_arr ) {
			DISTID = distid_arr[i]

			DISTIDdesc = distid_to_distiddesc_arr[DISTID]
			t21_dist_name = distid_to_t21name_arr[DISTID]
			t21userid = distid_to_userid_arr[DISTID]
			if ( t21userid == "" )
				t21userid = distid_to_directoryID_arr[DISTID]

			print "  HANDLE MANANGEMENT DISTID = " DISTID
			print "      GGRID = " GGRID "  EGRID = " EGRID
			print "      CHILDCOUNT = " CHILDCOUNT

			DISP_PROJECT = PROJECT
			addstar = ""

			# check if EGRID is in distid's distgrid list and
			# is 1 of many egrids(projects) in centre then addstar
 			if ( distid_egrid_arr[DISTID "|" EGRID] == 1 ) {
				if ( is_CENTRE_dot_PROJECT_arr[EGRID] ) {
					# more than 1 project in centre
					if ( CHILDCOUNT > 1 ) {
						addstar = "*"
						print "        ADDSTAR"
					}
				}
			}

			print "    PROJECT=" PROJECT "  addstar=[" addstar "]"

			# create distid dir
			distdir = sprintf("topdat/%s/%s/data/distid", monthdir, DISTID)
			cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", distdir, distdir)
			system(cmd)

			# don't look at TOTALTBS or Directory recs
			if ( substr(HIERIDxfrom,1,3) != "cde" && CHG_CAT != "X" ) {
			#if ( CHG_CAT != "X" ) {
			    # do highest usage for distid
			    if ( last_SERVTYPE != SERVTYPE || last_CENTRE != CENTRE || last_PROJECT != PROJECT || last_SID != SID) {
				# do highest user
				do_highest_usage(DISTID,SERVTYPE,SHIPTO,CENTRE,PROJECT,EGRID,SID)
			    }
			}
			done_DISTID_totals_arr[DISTID] = "1"
		    }
		    print ""
		}


		#-------------------#

		if ( !dot21verify ) {
		    if ( retroBDL != "1" ) {
			print ""
			print "process virtual distribution list"
			Stag = EGRID "|" SERVTYPE "|" SID
			Vdistid = personal_egrid_servtype_SID_to_distid_arr[Stag]
			print "  personal_egrid_servtype_SID_to_distid_arr[" Stag "] = [" personal_egrid_servtype_SID_to_distid_arr[Stag] "]"
			if ( Vdistid != "" ) {
				DISTID = Vdistid
				DISTIDdesc = distid_to_distiddesc_arr[DISTID]
				t21_dist_name = distid_to_t21name_arr[DISTID]
				t21userid = distid_to_userid_arr[DISTID]
				if ( t21userid == "" )
					t21userid = distid_to_directoryID_arr[DISTID]

				print "  HANDLE PERSONAL DISTID = " DISTID
				print "      GGRID = " GGRID "  EGRID = " EGRID
				print "      CHILDCOUNT = " CHILDCOUNT

				addstar = "*"
				DISP_PROJECT = PROJECT addstar

				# create distid dir
				distdir = sprintf("topdat/%s/%s/data/distid", monthdir, DISTID)
				cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", distdir, distdir)
				system(cmd)

				# for PERSONAL fixedline there is no individual
				# extn cde data so use HIERIDx t21 entry
				# to genetate top level DISTID data
				if ( SERVTYPE == 1 ) {
					if (substr(HIERIDxfrom,1,3) == "t21")
						handle_DISTID_datafile()
				}
				# for mobiles use t21 detail totals to get
				# chg cat totals for the DISTID file pie chart
				if ( SERVTYPE == 10 ) {
					if (substr(HIERIDxfrom,1,3) == "t21")
						handle_DISTID_datafile()
				}
				if ( HIERIDxfrom == "cde" ) {
					if ( SERVTYPE != 1 && SERVTYPE != 10 ) {
						handle_DISTID_datafile()
					}

					#handle_CENTRE_datafile()

					handle_REPORTGROUP_datafile()

					handle_CUSTOMERIMPORT_datafile()

					handle_CUSTOMERIMPORT_detail_summary_datafile()
				}

				# handle SERVTYPE totals
				##if ( SERVTYPE < 100 ) {
				## mobile totals from cde
				#if ( SERVTYPE == 10 ) {
				#	if ( HIERIDxfrom == "cde" )
				#		handle_SERVTYPE_datafile()
				#}
				#else {
				#	if ( substr(HIERIDxfrom,1,3) == "t21" )
				#		handle_SERVTYPE_datafile()
				#}
				# from t21
				if ( substr(HIERIDxfrom,1,3) == "t21" )
					handle_SERVTYPE_datafile()

				if ( HIERIDxfrom == "cde" ) {
					handle_swb_datafiles()

					handle_vml_datafiles()
				}

				if ( last_SERVTYPE != SERVTYPE || last_CENTRE != CENTRE || last_PROJECT != PROJECT ) {
					detail_fnames_to_fnamelist(SERVTYPE "|" CENTRE "|" PROJECT)
				}

				done_DISTID_totals_arr[DISTID] = "1"
			}

			print ""
		    }
		}



		################

		last_HIERIDxDUPcount = HIERIDxDUPcount
		last_serviceprovider = serviceprovider
		last_SERVTYPE = SERVTYPE
		last_SHIPTO = SHIPTO
		last_GROUP = GROUP
		last_CENTRE = CENTRE
		last_PROJECT = PROJECT
		last_GGRID = GGRID
		last_EGRID = EGRID
		last_SID = SID
		last_HIERIDx = HIERIDx
		last_HIERIDxfrom = HIERIDxfrom

	}

	system("date")

	#########################
	# do last_ totals

	if ( dot21verify ) {
		# newbb1 BBBBBB
		# - spit invoice GST CENTRE totals
		if ( newlast_inv_grid != "" ) {
			spit_newbb1_inv_totGST()
		}
	}

	if ( last_PROJECT != "" ) {
		spit_totals_rep_project()
		spit_totals_csvdata()
		print_totals(last_HIERIDxfrom, last_SERVTYPE, last_SHIPTO, last_GROUP, last_CENTRE, last_PROJECT, "All")
	}
	if ( last_CENTRE != "" ) {
		spit_totals_rep_centre()
		print_totals(last_HIERIDxfrom, last_SERVTYPE, last_SHIPTO, last_GROUP, last_CENTRE, "All", "All")
	}
	if ( last_SERVTYPE != "" ) {
		spit_totals_rep_servtype()
		print_totals(last_HIERIDxfrom, last_SERVTYPE, last_SHIPTO, last_GROUP, last_CENTRE, "All", "All")
	}

	spit_totals_rep_all()
	print_totals(last_HIERIDxfrom, "All", "All", "All", "All", "All", "All")

	#-----------------------------------------------------
	# create network average csv
	if ( !dot21verify && retroBDL != 1 ) {
		avgcsv = "networkaverage.csv"
		# remove old
		networkavg_fname = sprintf("topdat/%s/data/%s", monthdir, avgcsv)
		cmd=sprintf("rm -f %s", networkavg_fname)
		system(cmd)
		# create header
		printf("%s,%s,%s,%s,servicetypeDESC,servicetypeID,ServiceidCount:Int,totcount:Int,totduration:Int,totcost:Float,avgcost:Float\n", "Network Average", exbillperiod, "SDATE", "EDATE") >networkavg_fname
		close(networkavg_fname)
		# for each servicetypeid
		for ( servtype in SERVTYPE_to_SERVTYPEdesc_arr ) {
			if ( servtype == "All" )
				continue
			servtypedesc = SERVTYPE_to_SERVTYPEdesc_arr[servtype]
			sidcount = nsids_arr["TOTAL|" servtype]
			## r10
			#if ( servtype == 10 )
			#	allchgcat = "All_MOBILE_CDR"
			#else
				allchgcat = "All"
			if ( servtype <= 20 )	# use t21 totals for t21 types
				totXtag = "TOTAL" "|" servtype "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" allchgcat
			else
				totXtag = "TOTALTBS" "|" servtype "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" "All" "|" allchgcat
			totcost = totalcost_arr[totXtag]
			totcount = totalcount_arr[totXtag]
			totduration = totalduration_arr[totXtag]
			avg_cost = totcost
			print "AVG: totXtag=[" totXtag "]" "  totcost=" totcost "  totcount=" totcount "  totduration=" totduration
			if ( servtype == "10" ) {
			    if ( mobdummy_cost > 0 ) {
				sidcount = sidcount - 1
				avg_cost = avg_cost - mobdummy_cost
				print "AVGDUMMY 0: mobdummy_cost=[" mobdummy_cost "]" "  avg_cost=[" avg_cost "]" "  sidcount=[" sidcount "]"
				totcost = avg_cost
			    }
			}
			
			if ( sidcount > 0 )
			    avgcost = sprintf("%0.2f", mround2(avg_cost / sidcount))
			else
			    avgcost = 0

			print "AVG: "
			print "AVG: servtypedesc=[" servtypedesc "]"
			print "AVG: servtype=[" servtype "]"
			print "AVG: sidcount=[" sidcount "]"
			print "AVG: totXtag=[" totXtag "]"
			print "AVG: totcount=[" totcount "]"
			print "AVG: totduration=[" totduration "]"
			print "AVG: totcost=[" totcost "]"
			print "AVG: avgcost=[" avgcost "]"

			printf(",,,,%s,%s,%d,%d,%d,%0.2f,%0.2f\n", servtypedesc, servtype, sidcount, totcount, totduration, mround2(totcost), mround2(avgcost)) >>networkavg_fname
		}
		close(networkavg_fname)
	}

	#-----------------------------------------------------
	#-----------------------------------------------------
	# create management distribution list data files

	if ( !dot21verify ) {
	    print "create management dist lists data files"
	    system("date")
	    for ( DISTID in distid_to_distiddesc_arr ) {
		DISTIDdesc = distid_to_distiddesc_arr[DISTID]
		t21name = distid_to_t21name_arr[DISTID]
		t21userid = distid_to_userid_arr[DISTID]
		if ( t21userid == "" )
			t21userid = distid_to_directoryID_arr[DISTID]
		t21wbd_method = distid_to_wbd_method_arr[DISTID]
		t21wbd_type = distid_to_wbd_type_arr[DISTID]

		# create management dist list data
		if ( distid_to_wbd_type_arr[DISTID] == "MANAGEMENT" ) {
			print "Create data for Management DISTID=[" DISTID "]" "  t21name=[" t21name "]" "  t21userid=[" t21userid "]" "  t21wbd_type=[" t21wbd_type "]"
			# vmng
			fdistid = DISTID
			if ( virtual_management_distid_arr[DISTID] != "" ) {
				# virtual managment list
				real_distid = virtual_management_distid_arr[DISTID]
				real_distiddir = sprintf("topdat/%s/%s", monthdir, real_distid)
				# vmng
				#cmd = sprintf("[ ! -d \"%s\" ] && mkdir -p \"%s\"", real_distiddir, real_distiddir)
				#system(cmd)
				## sym link to virtual managment distid dir
				#cmd = sprintf("cd \"topdat/%s\"; [ ! -L \"%s\" ] && ln -s \"%s\" \"%s\"", monthdir, DISTID, real_distid, DISTID)
				#print "mngsym: [" cmd "]"
				#system(cmd)
				fdistid = real_distid
			}

			print "generate data files from hi_sid_by_cost_arr"
			create_hi_sid_csv("011","Highest Fixed Line Users by Cost", 20, hi_sid_by_cost_arr,DISTID,fdistid,1,"Call Count")
			create_hi_sid_csv("012","Highest Mobile Users by Cost", 20, hi_sid_by_cost_arr,DISTID,fdistid,10,"Call Count")


			print "generate data files from hi_sid_mobile_messaging_by_count_arr"
			create_hi_sid_csv("052","Highest SMS Users by Number Sent", 20, hi_sid_mobile_messaging_by_count_arr,DISTID,fdistid,10,"SMS Count")


			print "generate data files from hi_sid_mobile_messaging_by_cost_arr"
			create_hi_sid_csv("062","Highest SMS Users by Cost", 20, hi_sid_mobile_messaging_by_cost_arr,DISTID,fdistid,10,"SMS Count")


			print "generate data files from hi_call_by_cost_arr"
			create_hi_call_csv("021","Most Expensive Fixed Line Calls", 20, hi_call_by_cost_arr,DISTID,fdistid,1)
			create_hi_call_csv("022","Most Expensive Mobile Calls", 20, hi_call_by_cost_arr,DISTID,fdistid,10)

			print "generate data files from hi_call_by_duration_arr"
			create_hi_call_csv("031","Longest Duration Fixed Line Calls", 20, hi_call_by_duration_arr,DISTID,fdistid,1)
			create_hi_call_csv("032","Longest Duration Mobile Calls", 20, hi_call_by_duration_arr,DISTID,fdistid,10)


			print "generate data files from hi_dist_freqdial_arr"
			create_hi_freqdial_csv("041","Most Frequently Dialled Numbers from Fixed Line Phones", 20, hi_dist_freqdial_arr,DISTID,fdistid,1)
			create_hi_freqdial_csv("042","Most Frequently Dialled Numbers from Mobile Phones", 20, hi_dist_freqdial_arr,DISTID,fdistid,10)


			print "generate data files from hi_gloroam_users_by_cost_arr"
			create_hi_sid_csv("072","All Global Roaming Mobile Users", 2000, hi_gloroam_users_by_cost_arr,DISTID,fdistid,10,"Call Count")


			print "generate data files from hi_internet_users_by_cost_arr"
			create_hi_sid_csv("132","All Internet Users", 20000, hi_internet_users_by_cost_arr,DISTID,fdistid,10,"KBytes")


			print "generate data files from hi_premserv_users_by_cost_arr"
			create_hi_sid_csv("121","Highest 19XXXX... Service Fixed Line Users By Cost", 20, hi_premserv_users_by_cost_arr,DISTID,fdistid,1,"Call Count")
			create_hi_sid_csv("122","Highest Premium 19XXXXxx Service Mobile Users By Cost", 20, hi_premserv_users_by_cost_arr,DISTID,fdistid,10,"Call Count")

			print "generate data files for call distribution by date"
			create_call_distr_by_date_csv("081","Fixed Line Call Distribution By Date", DISTID,fdistid,1)
			create_call_distr_by_date_csv("082","Mobile Call Distribution By Date", DISTID,fdistid,10)

			print "generate data files for call distribution by hour of day"
			create_call_distr_by_hour_csv("091","Fixed Line Call Distribution By Hour of the Day", DISTID,fdistid,1)
			create_call_distr_by_hour_csv("092","Mobile Call Distribution By Hour of the Day", DISTID,fdistid,10)

			print "generate data files for Intra-Organisational call distribution"
			create_call_distr_by_category_csv("101","Intra-Organisational Call Distribution", DISTID,fdistid,1)
			create_call_distr_by_category_csv("102","Intra-Organisational Call Distribution", DISTID,fdistid,10)

			print "generate data files for call distribution by category"
			create_call_distr_by_category_csv("111","Fixed Line Call Distribution By Call Type", DISTID,fdistid,1)
			create_call_distr_by_category_csv("112","Mobile Call Distribution By Call Type", DISTID,fdistid,10)


			done_DISTID_totals_arr[DISTID] = "1"
		}
	    }
	    print ""
	    system("date")
	}
			
	#-----------------------------------------------------
	#-----------------------------------------------------
	# finish up processing off distribution lists

	if ( !dot21verify )
		finish_proc_dist_lists()


	#-----------------------------------------------------
	#-----------------------------------------------------
	print ""
	system("date")
	for ( Xtag in totalcost_arr ) {
		print "DEBUGTOTALS: totalcost_arr[" Xtag "] = " totalcost_arr[Xtag] "  count=" totalcount_arr[Xtag]
	}
	print ""

	#-----------------------------------------------------
	#-----------------------------------------------------
	print ""
	print "highest usage debug..."
	system("date")
	print "hi_sid_by_cost_arr"
	for ( Htag in hi_sid_by_cost_arr ) {
		print "DEBUGHU: hi_sid_by_cost_arr[" Htag "]  = " hi_sid_by_cost_arr[Htag] 
	}
	print ""
	print "hi_sid_mobile_messaging_by_count_arr"
	for ( Htag in hi_sid_mobile_messaging_by_count_arr ) {
		print "DEBUGHU: hi_sid_mobile_messaging_by_count_arr[" Htag "]  = " hi_sid_mobile_messaging_by_count_arr[Htag] 
	}
	print ""
	print "hi_sid_mobile_messaging_by_cost_arr"
	for ( Htag in hi_sid_mobile_messaging_by_cost_arr ) {
		print "DEBUGHU: hi_sid_mobile_messaging_by_cost_arr[" Htag "]  = " hi_sid_mobile_messaging_by_cost_arr[Htag] 
	}
	print ""
	print "hi_call_by_duration_arr"
	for ( Htag in hi_call_by_duration_arr ) {
		print "DEBUGHU: hi_call_by_duration_arr[" Htag "]  = " hi_call_by_duration_arr[Htag] 
	}
	print ""
	print "hi_call_by_cost_arr"
	for ( Htag in hi_call_by_cost_arr ) {
		print "DEBUGHU: hi_call_by_cost_arr[" Htag "]  = " hi_call_by_cost_arr[Htag] 
	}
	print ""
	print "hi_dist_freqdial_arr"
	for ( Htag in hi_dist_freqdial_arr ) {
		print "DEBUGHU: hi_dist_freqdial_arr[" Htag "]  = " hi_dist_freqdial_arr[Htag] 
	}
	print ""
	print "sid_freq_numbers_count_arr"
	for ( Htag in sid_freq_numbers_count_arr ) {
		print "DEBUGHU: sid_freq_numbers_count_arr[" Htag "]  = " sid_freq_numbers_count_arr[Htag] 
	}
	print ""
	print "dist_freq_numbers_count_arr"
	for ( Htag in dist_freq_numbers_count_arr ) {
		print "DEBUGHU: dist_freq_numbers_count_arr[" Htag "]  = " dist_freq_numbers_count_arr[Htag] 
	}
	print ""
	print "gloroam_users_cost_arr"
	for ( Htag in gloroam_users_cost_arr ) {
		print "DEBUGHU: gloroam_users_cost_arr[" Htag "]  = " gloroam_users_cost_arr[Htag] 
	}
	print ""
	print "hi_gloroam_users_by_cost_arr"
	for ( Htag in hi_gloroam_users_by_cost_arr ) {
		print "DEBUGHU: hi_gloroam_users_by_cost_arr[" Htag "]  = " hi_gloroam_users_by_cost_arr[Htag] 
	}
	print ""
	print "internet_users_cost_arr"
	for ( Htag in internet_users_cost_arr ) {
		print "DEBUGHU: internet_users_cost_arr[" Htag "]  = " internet_users_cost_arr[Htag] 
	}
	print ""
	print "hi_internet_users_by_cost_arr"
	for ( Htag in hi_internet_users_by_cost_arr ) {
		print "DEBUGHU: hi_internet_users_by_cost_arr[" Htag "]  = " hi_internet_users_by_cost_arr[Htag] 
	}
	print ""
	print "sid_premserv_count_arr"
	for ( Htag in sid_premserv_count_arr ) {
		print "DEBUGHU: sid_premserv_count_arr[" Htag "]  = " sid_premserv_count_arr[Htag] 
	}
	print ""
	print "hi_premserv_users_by_cost_arr"
	for ( Htag in hi_premserv_users_by_cost_arr ) {
		print "DEBUGHU: hi_premserv_users_by_cost_arr[" Htag "]  = " hi_premserv_users_by_cost_arr[Htag] 
	}
	print ""

	print ""
	print "call_distribution_date_count_arr"
	for ( Htag in call_distribution_date_count_arr ) {
		print "DEBUGCALLDISTR: call_distribution_date_count_arr[" Htag "]  = " call_distribution_date_count_arr[Htag] "  duration  = " call_distribution_date_duration_arr[Htag] "  cost  = " call_distribution_date_cost_arr[Htag] 
	}
	print ""

	print ""
	print "call_distribution_hour_count_arr"
	for ( Htag in call_distribution_hour_count_arr ) {
		print "DEBUGCALLDISTR: call_distribution_hour_count_arr[" Htag "]  = " call_distribution_hour_count_arr[Htag] "  duration  = " call_distribution_hour_duration_arr[Htag] "  cost  = " call_distribution_hour_cost_arr[Htag] 
	}
	print ""

	print ""
	print "call_distribution_category_count_arr"
	for ( Htag in call_distribution_category_count_arr ) {
		print "DEBUGCALLDISTR: call_distribution_category_count_arr[" Htag "]  = " call_distribution_category_count_arr[Htag] "  duration  = " call_distribution_category_duration_arr[Htag] "  cost  = " call_distribution_category_cost_arr[Htag] 
	}
	print ""

	print ""
	print "nsids_arr"
	for ( Htag in nsids_arr ) {
		print "DEBUGNSIDS: nsids_arr[" Htag "] = " nsids_arr[Htag]
	}
	print ""

	print ""
	print "distid_to_egridlist_arr"
	for ( Dtag in distid_to_egridlist_arr ) {
		print "DEBUGDISTID: distid_to_egridlist_arr[" Dtag "] = " distid_to_egridlist_arr[Dtag]
	}
	print ""

	#-----------------------------------------------------
	#-----------------------------------------------------
	print ""
	print "Finish wb4 - " thisCENTRE
	system("date")

	# End BEGIN
	exit 0
}


END {
}