#
#		ITSV-GmbH
#		CCDB - Command and Control Database
#
#	FILE:			dquerymfile_remSelectedSOIDLSW
#	DESCRIPTION:	DQUERY definition file for SQUERY remSelectedLSWSOID
#
@querytitle			Entferne Zuordnung bestimmter SOIDs zu Lohnsoftware-Objekten
@querydescription	Entfernt die Zuordnung zu einer LSWID für alle Einträger in CCMVBRFAUSW, die dem Suchbegriff entsprechen
@group				SVCLMVBRFAUSWDATALOAD
@attributenames		pattern:string:{{patternoptions}}
@patternoptions		{"labeltext":"Suchbegriff (RegEx)","typedesc":"Regular Expression zur Auswahl der SOID-Werte, zu denen die Zuordnung entfernt werden soll"}
@querytype			function
@function			seqtrans.seqtrans
@sql_select			select distinct aw.SOID,aw.LSWID,lsw.LSWATTRIBS
					  from CCMVBRFAUSW aw
						left join SVCLRWLSW lsw on lsw.LSWID=aw.LSWID
					 where regexp_like(SOID,::pattern::)

~query.tsteps

#
# <<TSTEPNUM:0>>: initialize
#
qexpression
	proc: {
		copycvars(this,"SOID,LSWID,LSWATTRIBS");
		if (!this.init_done) {
			this.ppfx = this.fname+"("+this.query.dataname+")";
			this.recstoprocess = this.inputresult.getRowCount();
			ppush(this,[this.ppfx+"_STARTED","RECORDS_TO_PROCESS="+this.recstoprocess,"",""]);
			this.recsprocessed = 0;
			this.init_done = true;
		}
	}

#
# <<TSTEPNUM:1>>: if no LSWID, nothing to delete
#
if 		(!this.LSWID) 
goto	skip

#
# <<TSTEPNUM:2>>: first, unlink all applicable CCMVBRFAUSW entries from LSW-entry (after logging the current record parameters)
#
pre_qexpression
	ppush(this,[this.ppfx+"_PROCESS_RECORD","SOID="+this.SOID,"LSWID="+this.LSWID,"LSWATTRIBS=\""+this.LSWATTRIBS+"\""])
sql				update CCMVBRFAUSW set LSWID=null WHERE SOID=::SOID:: and LSWID=::LSWID::
named_params	SOID,LSWID
result_varname	unlinkres
post_qexpression
	ppush(this,[this.ppfx+"_SOID_UNLINKED","SOID="+this.SOID,"ROWS_AFFECTED="+this.unlinkres.resultobject.affectedRows,"DB_MESSAGE="+this.unlinkres.resultobject.message]);
	this.recsprocessed++;
	if (!this.soidsprocessed) {
		this.soidsprocessed = {};
	}
	if (!this.soidsprocessed[this.SOID]) {
		this.soidsprocessed[this.SOID] = { recsunlinked: 0 };
	}
	this.soidsprocessed[this.SOID].recsunlinked += this.unlinkres.resultobject.affectedRows;
	if (!this.lswsprocessed) {
		this.lswsprocessed = {};
	}
	if (!this.lswsprocessed[this.LSWID]) {
		this.lswsprocessed[this.LSWID] = { recsunlinked: 0, lswattribs: this.LSWIDATTRIBS };
	}
	this.lswsprocessed[this.LSWID].recsunlinked += this.unlinkres.resultobject.affectedRows;
		
	
	
#
# <<TSTEPNUM:3>>: delete the SOID record (as all references to this very SOID are unlinked by step 1)
#
sql				delete from SVCLRWSOID where SOID=::SOID::
named_params	SOID
result_varname	delsoidres
post_qexpression
	ppush(this,[this.ppfx+"_SOID_DELETED","DELETED_SOID="+this.SOID,"","DELETE_RESULT: "+aux.objTxt(this.delsoidres)]);


#
# <<TSTEPNUM:4>> check if the LSW-Object is referenced by more than one record still
#
sql				select count(*) from SVCLRWSOID where LSWID=::LSWID::
named_params	LSWID
result_varname	lswrefcntres
post_qexpression
	ppush(this,[this.ppfx+"_LSW_STILL_REF","LSWID="+this.LSWID,"REFERENCED_BY_SOIDS="+this.lswrefcntres.getCellValue(0,0),""]);

#
# <<TSTEPNUM:5>>: if more than one reference, skip deletion of LSW record
#
if (this.lswrefcntres.getCellValue(0,0)>1)
goto skip

#
# <<TSTEPNUM:6>>: only one reference (or only one left after previous unlinks), delete LSW record
#
sql				delete from SVCLRWLSW where LSWID=::LSWID::
named_params	LSWID
result_varname	dellswres
post_qexpression
	ppush(this,[this.ppfx+"_LSW_DELETED","SOID="+this.SOID,"LSW_DELETED="+this.LSWID,"DELETE_RESULT: "+aux.objTxt(this.dellswres)])

#
# <<TSTEPNUM:7>>: check if last record
#
skip:
if 		(!this.is_last_record)
goto	done

#
# <<TSTEPNUM:8>>: last record, finalize protocol
#
qexpression
	for (let soid in this.soidsprocessed) {
		ppush(this,[this.ppfx+"_SOIDPROCESSED","SOID_PROCESSED=\""+soid+"\"","RECORDS_UNLINKED="+this.soidsprocessed[soid].recsunlinked,""]);
	}
	for (let lswid in this.lswsprocessed) {
		ppush(this,[this.ppfx+"_LSWPROCESSED","LSWID_PROCESSED=\""+lswid+"\"","RECORDS_UNLINKED="+this.lswsprocessed[lswid].recsunlinked,"ATTRIBS=\""+this.lswsprocessed[lswid].lswattribs+"\""]);
	}
	ppush(this,[this.ppfx+"_FINISHED","RECORDS_PROCESSED="+this.recsprocessed,"",""]);

#
# <<TSTEPNUM:9>>:	record complete
#
done:
end
