system("rm -f HIERIDx_" batchtag ".lst")
	system("rm -f sundrys_" batchtag ".uniq")
	system("rm -f DEPTID_" batchtag ".lst")
---
	#------------------------
	# empty out target topdat dir
	#if ( thisDIVID == "all" ) {
		print "INFO: REMOVE (and remake) act_targdir " act_targdir
		system("rm -fr " act_targdir)
		system("mkdir -p " act_targdir)
		#system("chown catcom " act_targdir)
		#system("chgrp catcom " act_targdir)
	#}
	#else {	# clean out parent only
	#	this_divgroup = exwebconf_parent_to_cdgroup_arr[thisDIVID]
	#	#rs 1
	#	if ( this_divgroup == "" )	# single div
	#		this_divgroup = thisDIVID # (force single to a divgroup)
	#	#rs 1
	#	if ( this_divgroup != "" )	# belongs to a cdgroup
	#		pardir = act_targdir "/" thisDIVID "/" thisDIVID
	#	else
	#		pardir = act_targdir "/" thisDIVID
	#	print "INFO: REMOVE pardir " pardir
	#	system("rm -fr " pardir)
	#}

	#------------------------
	init_totals("DIVID")
	init_totals("DIVID.SGID")
	init_totals("DIVID.SGID.SEID")
	init_totals("DIVID.SGID.SEIDDEPTID")
	#init_totals("DIVID.SGID.SEID.DEPTID")
	init_totals("DIVID.SGID.SEID.DEPTID.SID")

	#------------------------
	HIERIDxorder = 0
	MAXDEPTID = 0
	DEPTID = 0

	sundry = ""
	SID = ""
	DEPTID = ""
	SEID = ""
	SGID = ""
	DIVID = ""
	pardir = ""
	HIERIDx = ""

	last_HIERIDxDUPcount = ""
	last_invoiceno = ""
	last_sundry = ""
	last_costcentre = ""
	last_user = ""
	last_location = ""
	last_tascode = ""
	last_supplier = ""
	last_SID = "";	last_SIDdesc = ""
	last_DEPTID = ""
	last_SEID = ""
	last_SGID = ""
	last_DIVID = ""
	last_pardir = ""
	last_HIERIDx = ""

	DEPTID_fname = ""
	SEID_fname = ""
	SGID_fname = ""
	DIVID_fname = ""

	divid_file_exists = 0
	sgid_file_exists = 0
	seid_file_exists = 0
	deptid_file_exists = 0
	mobile_file_exists = 0
	swbextnsum_file_exists = 0
	swbdet_file_exists = 0

	#------------------------
	# old tbs database
	#ld_customer(tblCustomerfile)
	#ld_servicegroup(tblServiceGroupfile)
	#ld_serviceelements(tblServiceElementsfile)
	#ld_tassemap(tblTASSEMapfile)

	# service group report removed so set 1 default service group "All"
	SGID = 0
	SGIDdesc = "All"
	SGID_to_SGIDdesc_arr[SGID] = SGIDdesc

	# tbsdata database
	ld_tbsdata_customer(tbsCustomerfile)
	ld_tbsdata_servicetype(tbsServiceTypefile)
	ld_tbsdata_element(tbsElementfile)
	ld_tbsdata_supplier(tbsSupplierfile)

	# to translate VRT seid for drill down
	ld_seidtranslate("indata/" monthtag "/seidtranslate.csv")
	#------------------------

	got_cde_data = 0

	#getcdefiles("VODAFONE")
	#getcdefiles("OPTUS")
	#getcdefiles("TELSTRA")
	#getcdefiles("AAPT")
	#getcdefiles("VICTRACK")

	#if ( RESTcde == 1 ) {
	#	#getcdefiles("Electricity")
	#	#getcdefiles("Radiolandlines")
	#	#getcdefiles("SACC")
	#	getcdefiles("REST")
	#}
	#for ( cstr in cdefile_arr )
	#{
	#	split(cstr, cstr_arr, "|")
	#	cdedir = cstr_arr[1]
	#	cnt_cde = cstr_arr[2]
	#	print "Doing cde dir = " cdedir "  cnt= " cnt_cde "  cdefile = [" cdefile_arr[cstr] "]"
	#	ld_cde(cdefile_arr[cstr])
	#}

	# now data comes from tbsdata (see getcde3 script)
	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
	}

	close("sort >DEPTID_" batchtag ".lst")
	close("sort >HIERIDx_" batchtag ".lst")
	close("sort -u >sundrys_" batchtag ".uniq")
	close("sort | uniq -d >SID_" batchtag ".dup")

	print "MAXDEPTID = " MAXDEPTID

	DEPTID_to_department_arr["ALL"] = "All Departments"
	SEID_to_SEIDdesc_arr["ALL"] = "All Service Types"

	# get processing order from sorted file
	pipecmd = "sort >" logdir "/" "HIERIDx_" batchtag ".lst"
	close(pipecmd)
	infile = logdir "/" "HIERIDx_" batchtag ".lst"
	while ( (getline HIERIDx < infile) > 0 ) {
		HIERIDxorder_to_HIERIDx_arr[++HIERIDxorder] = HIERIDx
		#print HIERIDxorder "  HIERIDx = " HIERIDx
	}
	HIERIDxmaxorder = HIERIDxorder
	print "HIERIDxmaxorder = " HIERIDxmaxorder
	close(infile)

	#-----------------------------------------------------
	# check platinum invoice info has corresponding cde info

	if ( platinumBATCH == "all" ) {
		print "INFO: ---- CHECK Platinum invoice info has CDE data ----"
		for ( itag in platinv_parent_sundry_arr ) {
			print "itag=[" itag "]"
			split(itag, itag_arr, "|")
			parent = itag_arr[1]
			sundry = itag_arr[2]
			orgelementid = itag_arr[3]
			if ( orgelementid == 1 )	# Skip Admin Fee
				continue
			if ( cde_parent_sundry_arr[itag] == "" ) {
				print "WARNING: Platinum Invoice for parent=" parent " sundry=" sundry " orgelementid=" orgelementid " has NO CDE"
			}
	
		}
		print "INFO: --------------------------------------------------"
	}

	exwebconf_parent_arr[exwebconf_parent] = 1
	# check platinum invoice info has correesponding exwebconf info

	if ( platinumBATCH == "all" ) {
		print "INFO: ---- CHECK Platinum invoice info has exwebconf data ----"
		for ( parent in platinv_parent_arr ) {
			if ( exwebconf_parent_arr[parent] == "" ) {
				print "WARNING: Platinum Invoice for parent=" parent " has NO EXWEBCONF"
			}
		}
		print "INFO: --------------------------------------------------"
	}
	#-----------------------------------------------------
	# process gsm data

	printf("\n")
	for ( HIERIDxorder = 1; HIERIDxorder <= HIERIDxmaxorder; ++HIERIDxorder ) {
		HIERIDx = HIERIDxorder_to_HIERIDx_arr[HIERIDxorder]

		# # if doing all check if parent is in exwebconf
		# # and only do it if in exwebconf
		#split(HIERIDx, HIERIDx_arr, "|")
		#cde_parent = HIERIDx_arr[1];
		# if ( thisDIVID == "all" && !exwebconf_parent_arr[cde_parent] ) {
		# 	print "CDE SKIPPING parent = " cde_parent "  (NOT in exwebconf)"
		# 	continue
		# }
		#if ( SGID_to_SGIDdesc_arr[SGID] != "Network Services" )
		#	continue

		## Testing all
		#split(HIERIDx, HIERIDx_arr, "|")
		#cde_parent = HIERIDx_arr[1];
		#if ( cde_parent != "SPA" && cde_parent != "VRT" && cde_parent != "ALH" && cde_parent != "FRT" && cde_parent != "CGE" ) {
		#	if ( cde_parent != last_skip )
		#		print "Testing  - CDE Skipping " cde_parent
		#	last_skip = cde_parent
		#	continue
		#}


		get_HIERIDx_info(HIERIDx, 2)

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

		if ( HIERIDx == last_HIERIDx ) {	# skip duplicates
			print "WARNING: duplicate HIERIDx: " HIERIDx
			continue
		}

		handle_SID_change()
		handle_DEPTID_change()
		handle_SEID_change()
		handle_SGID_change()
		handle_DIVID_change()

		totid = "DIVID.SGID.SEID.DEPTID.SID"
		add_totals(totid,"","",invoiceno,sundry,supplier,rentcost,callcost,othercost,totalcost)

		#totid = "DIVID.SGID.SEID.DEPTID"
		#add_totals(totid,"","",invoiceno,sundry,supplier,rentcost,callcost,othercost,totalcost)

		totid = "DIVID.SGID.SEIDDEPTID"
		add_totals(totid,SEID,DEPTID,invoiceno,sundry,supplier,rentcost,callcost,othercost,totalcost)
		add_totals(totid,SEID,"ALL",invoiceno,sundry,supplier,rentcost,callcost,othercost,totalcost)
		add_totals(totid,"ALL",DEPTID,invoiceno,sundry,supplier,rentcost,callcost,othercost,totalcost)
		add_totals(totid,"ALL","ALL",invoiceno,sundry,supplier,rentcost,callcost,othercost,totalcost)

		totid = "DIVID.SGID.SEID"
		add_totals(totid,"","",invoiceno,sundry,supplier,rentcost,callcost,othercost,totalcost)

		totid = "DIVID.SGID"
		add_totals(totid,"","",invoiceno,sundry,supplier,rentcost,callcost,othercost,totalcost)

		totid = "DIVID"
		add_totals(totid,"","",invoiceno,sundry,supplier,rentcost,callcost,othercost,totalcost)

		last_HIERIDxDUPcount = HIERIDxDUPcount
		last_invoiceno = invoiceno
		last_sundry = sundry
		last_costcentre = costcentre
		last_user = user
		last_location = location
		last_tascode = tascode
		last_supplier = supplier
		last_SID = SID;	last_SIDdesc = SIDdesc
		last_DEPTID = DEPTID
		last_SEID = SEID
		last_SGID = SGID
		last_DIVID = DIVID
		last_pardir = pardir
		last_HIERIDx = HIERIDx

	}

	print ""
	print ""

	#####################################
	spit_SIDtotals_to_CUSTOMERIMPORTfile();
	#spit_DEPTIDtotals_to_SEIDfile();
	#spit_SEIDtotals_to_SGIDfile();
	spit_SEIDDEPTIDtotals_to_SGIDfile();
	spit_SGIDtotals_to_DIVIDfile();
	#spit_DIVIDtotals_to_TOTALSfile();
	spit_cde_SEIDDEPTID_totals_data();
	gen_cde_DIVID_totals_rep();

	close(DEPTID_fname);
	#close(SEID_fname);
	close(SGID_fname);
	close(DIVID_fname);

	#-----------------------------------------------------
	# NO do it above for for each new SID
	#procgsmdet(gsmcallsfile)

	#-----------------------------------------------------
	# process T21 calls file
	if (platinumBATCH == "all" || is_t21_platinv_batch(platinumBATCH) || platinv_batch == "VRTBATCHFILENAME"  ) {
		print "INFO: processing t21calls platinumBATCH = " platinumBATCH
		dot21callsfile(t21callsfile)
	}
	else {
		print "INFO: Skipping t21calls because platinumBATCH = " platinumBATCH
	}

	#-----------------------------------------------------
	#gen_t21_totals_rep();
	system("rm -f " tmptotalsrepfile);

	#-----------------------------------------------------
	if ( ! dot21verify ) {
		# remove lock file
		system("rm -f " lokfile)
	}

	#-----------------------------------------------------
	print ""
	print "Finish wb3 - " thisDIVID

	# End BEGIN
	exit 0
}