#
#				ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILES:				dquerymfile_konvertpersonoegk.txt
#	DESCRIPTION:		DQUERY definition for DQUERY konvertpersonoegk
#						converts a operson's persondata (email, phone) from a GKK to OEGK
#
@querytitle				Person von GKK zu ÖGK konvertieren
@querydescription		Email und Telefonnummer der Person von GKK zu ÖGK übersetzen
@group					GROUPS
@attributenames			uskurzz:uskurzz
@querytype				function
@function				seqtrans.seqtrans

~query.tsteps

#
# <<TSTEPNUM:0>>: get person attributes for person and prepare new values
#
pre_qexpression
	this.uskurzz = this.query.uskurzz;
	this.gkklist = "wgkk,noegkk,bgkk,ooegkk,stgkk,kgkk,sgkk,tgkk,vgkk";
	this.gkka = this.gkklist.split(",");
	this.gkks = {};
	for (let gi=0; gi<this.gkka.length; gi++) {
		this.gkks[this.gkka[gi]] = gi+11;
	}
sql select ATTARCHTYP,ATTPARTYPE,ATTPARKURZ,ATTNAME,ATTVALUE 
      from CCATT 
	where ((ATTARCHTYP='USEMAIL' and ATTNAME='EMAIL')
	    or (ATTARCHTYP='USEMAIL' and ATTNAME='EMAIL_PRE_20200101')
	    or (ATTARCHTYP='PERSINFO' and ATTNAME='FON')
	    or (ATTARCHTYP='PERSINFO' and ATTNAME='TELEFON_PRE_20200101'))
	  and ATTPARKURZ=::uskurzz::
named_params	uskurzz
result_varname	ares
post_qexpression
	proc: {
		let ro;
		this.patts = {};
		for (let ri=0; ri<this.ares.getRowCount(); ri++) {
			ro = this.ares.getRowObject(ri);
			this.patts[ro.ATTNAME] = ro.ATTVALUE;
		}
		let missatts = new Array();
		let suatts = new Array();
		if (this.patts.hasOwnProperty("TELEFON_PRE_20200101")) suatts.push("TELEFON_PRE_20200101");
		if (this.patts.hasOwnProperty("EMAIL_PRE_20200101")) suatts.push("EMAIL_PRE_20200101");
		if (!this.patts.hasOwnProperty("FON")) missatts.push("FON");
		if (!this.patts.hasOwnProperty("EMAIL")) missatts.push("EMAIL");
		let etx = "";
		if (missatts.length>0) etx = "Person Attributes " + missatts.join(",") + " are missing";
		if (suatts.length>0) {
			if (etx) etx += " - ";
			etx += "Person Attributes " + suatts.join(",") + " are already there";
		}
		if (etx) {
			this.errcoll.collect(null,etx,ro);
			break proc;
		}
		let phrex = /\s([0-9]+)$/;
		let phmatch = phrex.exec(this.patts.FON);
		if (phmatch) {
			this.phext = phmatch[1];
		} else {
			this.errcoll.collect(null,"FON is malformed",this.patts);
			break proc;
		}
		let emrex = /^(.+)@(.+gkk).at/;
		let ematch = emrex.exec(this.patts.EMAIL);
		if (ematch) {
			this.pem = ematch[1];
			this.gkk = ematch[2];
		} else {
			this.errcoll.collect(null,"EMAIL is malformed",this.patts);
			break proc;
		}
		this.newfon = "+43-50766-"+(this.gkks[this.gkk])+" "+this.phext;
		this.newemail = this.pem + "@oegk.at";
		this.oldemail = this.patts.EMAIL;
		this.oldfon = this.patts.FON;
	}

#
# <<TSTEPNUM:1>>: create EMAIL_PRE_20200101
#
sql insert into CCATT(ATTARCHTYP,ATTPARTYPE,ATTPARKURZ,ATTNAME,ATTVALUE) 
           values('USEMAIL','CCUS',::uskurzz::,'EMAIL_PRE_20200101',::oldemail::)
named_params	uskurzz,oldemail

#
# <<TSTEPNUM:2>>: create TELEFON_PRE_20200101
#
sql insert into CCATT(ATTARCHTYP,ATTPARTYPE,ATTPARKURZ,ATTNAME,ATTVALUE) 
           values('PERSINFO','CCUS',::uskurzz::,'TELEFON_PRE_20200101',::oldfon::)
named_params	uskurzz,oldfon


#
# <<TSTEPNUM:3>>: update EMAIL
#
sql update CCATT set ATTVALUE=::newemail:: where ATTARCHTYP='USEMAIL' and ATTPARTYPE='CCUS' and ATTPARKURZ=::uskurzz:: and ATTNAME='EMAIL'
named_params	uskurzz,newemail


#
# <<TSTEPNUM:4>>: update FON
#
sql update CCATT set ATTVALUE=::newfon:: where ATTARCHTYP='PERSINFO' and ATTPARTYPE='CCUS' and ATTPARKURZ=::uskurzz:: and ATTNAME='FON'
named_params	uskurzz,newfon

#
# <<TSTEPNUM:5>>: chain query personinfo
#
qexpression
	this.result = new aux.Result({ resulttype: "plain", body: "Person "+this.uskurzz+" zu ÖGK konvertiert (FON, EMAIL)",
	                               chain_query: { dataname: 'personinfo', uskurzz: this.uskurzz } });

