#
#	ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:			dquermfile_cfdgdunzcount.txt
#	DESCRIPTION:	DQUERY definition for CCDB DQUERY "cfdgdunzcount"
#	HISTORY:
#		Date		Author		Description
#		20211202	WSC			use V_CCCFUNZ_ZUOOPTOUT instead of CCCFUNZ to include ZUO-Opt-Out Information in Download files
#
@querytitle			Anzahl von unzustellbaren Clearingfällen pro Träger und Berichtsdatum
@querydescription	Zeigt für jeden Träger und Berichtsdatum die Anzahl der ELDA-nicht-zustellbaren Clearingfälle an	
@group				SVCLCFSTATCFENZ
@querytype			dbselect
@sql				select repdatnum, traeger as "TRAEGER", count(*) as ANZAHL_ELDA_UNZUSTELLBAR, 'Download' as Download
					  from V_CCCFUNZ_ZUOOPTOUT
					 group by repdatnum,traeger
					order by repdatnum,traeger
@title												Anzahl unzustellbarer Clearingfälle per Träger und Berichtsdatum
@toptext			Diese Statistik wird periodisch 
					<a href="https://doku.seu.sozvers.at/confluence_secure/pages/viewpage.action?pageId=92081446">im Clearingsystem Confluence-Bereich</a> 
					veröffentlicht
# @decoration.decorate_metadata.0					Berichtsdatum
# @decoration.decorate_metadata.1					Träger
# @decoration.decorate_metadata.2					Anzahl unzustellbarer Clearingfälle
# @decoration.decorate_colcell.0.hyperlink.href		/dquery?dataname=cfdgdunzrepdat&repdat=@@row.repdatnum@@
# @decoration.decorate_colcell.0.hyperlink.title	Alle unzustellbaren Clearingfälle für Berichtsdatum @@row.repdatnum@@ anzeigen
# @decoration.decorate_colcell.1.hyperlink.href		/dquery?dataname=cfdgdunzsvt&svt=@@row.traeger@@&repdat=@@row.repdatnum@@
# @decoration.decorate_colcell.1.hyperlink.title	Alle unzustellbaren Clearingfälle für Träger @@row.traeger@@ und Berichtsdatum @@row.repdatnum@@ anzeigen
# @decoration.decorate_colcell.3.hyperlink.href		/dquery?dataname=SVCLDGCFUNZSVTDLD&repdat=@@row.repdatnum@@&svt=@@row.traeger@@
# @decoration.decorate_colcell.3.hyperlink.title	Alle unzustellbaren Clearingfälle für Träger @@row.traeger@@ und Berichtsdatum @@row.repdatnum@@ als Excel-Datei downloaden
@TLIST												05,11,12,13,14,15,16,17,18,19
@post_query
	proc: {
		let ptlres = aux.pivotizeResult(that.result,	{	pivotize:	{	vcolumns:		[ 'repdatnum' ],
																			hcolumns:		[ 'TRAEGER' ],
																			value_column:	'ANZAHL_ELDA_UNZUSTELLBAR',
																			vkeyformat:		"CONCAT",
																			hkeyformat:		"CONCAT",
																			vkeycolname:	"Berichtsdatum->Träger/Landesstelle"
																		}
														}
										);
		let tccn = ptlres.addMetaData({name: 'Total', class: 'vertical'});
		let prcn = ptlres.addMetaData({name: 'ProzentAnteilig', class: 'vertical' });
		let tis = that.query.TLIST.split(",");
		let tcns = new Array();
		let ti, tcn, totalcount, refcount, sc, cv, cav, row;
		if (this.eunz_total_refcount) {
			refcount = this.eunz_total_refcount;
		} else {
			refcount = 0;
		}
		for (ti=0; ti<tis.length; ti++) {
			tcn = ptlres.findColumn(tis[ti]);
			if (tcn<0) {
				that.errcoll.collect(null,"no column in result for TRAEGER \""+tis[ti]+"\"",ptlres);
				break proc;
			}
			tcns.push(tcn);
		}
		for (let ri=0; ri<ptlres.getRowCount(); ri++) {
			totalcount = 0;
			row = ptlres.getRowObject(ri);
			for (ti=0; ti<tcns.length; ti++) {
				cv = ptlres.getCellValue(ri,tcns[ti]);
				ptlres.setCellValue(ri,tcns[ti],"hyperlink",{
									href: "/dquery?dataname=SVCLDGCFUNZSVTDLD&repdat="+ptlres.getCellValue(ri,0)+"&svt="+tis[ti], 
									title: "Alle unzustellbaren Clearingfälle für Träger "+tis[ti]+" und Berichtsdatum "+ptlres.getCellValue(ri,0)+" als Excel-Datei downloaden"});
				totalcount += cv;
			}
			ptlres.setCellValue(ri,tccn,totalcount);
			if (!refcount) {
				if (ri==0) refcount = totalcount;
				if (refcount<1) refcount=0.1;
			}
			ptlres.setCellValue(ri,prcn,Math.floor((totalcount*100)/refcount)+" %");
		}
		ptlres.setResultAttribute("title","Anzahl ELDA-unzustellbarer Clearingfälle per Berichtsdatum per Träger und Total im Trend");
		that.result = ptlres;
	}