
// Global d.3 libraries -----------------------------------------------------
import com.dvelop.d3.server.core.D3Interface;
import com.dvelop.d3.server.Document;
import com.dvelop.d3.server.User;
import com.dvelop.d3.server.DocumentType;
 
// Libraries to handle the different hook types -----------------------------
import com.dvelop.d3.server.ValueSet;
 
// Special libraries --------------------------------------------------------
import com.dvelop.d3.server.RepositoryField;



class ValueSetsCombined {

	@ValueSet( entrypoint = "WM_DAOberRegister" )
	def WM_DAOberRegister ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 3 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		def wm_typ = "";
		def unterreg = "";
		
		switch ( docType.id )
		{
			case "AUREG":
			case "AURG2":
			case "AURG3":
			case "AOREG":
			case "AREGS":
			case "DDAUE":
				wm_typ = "Dauerakte%";
				break;
			case "AJAOB":
			case "AJAAB":
			case "DABSC":
			case "AJAUR":
			case "AJAU2":
				wm_typ = "Jahresabschlussakte%";
				break;
			case "DBERA":
			case "ABERO":
			case "ABERA":
			case "ABEUR":
			case "ABEU2":
				wm_typ = "Beratungsakte%";
				break;
			case "DSTEU":
			case "ASTOR":
			case "ASTRA":
			case "ASTUR":
			case "ASTU2":
				wm_typ = "Steuerakte%";
				break;
			case "DBETR":
			case "ABEPA":
			case "ABEOR":
			case "ABPUR":
			case "ABPU2":
				wm_typ = "Betriebspruefungsakte%";
				break;
			case "DFIBU":
			case "AFIOR":
			case "AFIRA":
			case "AFIUR":
			case "AFIU2":
				wm_typ = "Fibuakte%";
				break;
			case "DLOHN":
			case "ALBOR":
			case "ALBRE":
			case "ALJUR":
			case "ALJR2":
				wm_typ = "Lohnbuchhaltungjahresakte%";
				break;
			case "DLSTD":
			case "ALBSO":
			case "ALORE":
			case "ALOUR":
			case "ALBU2":
				wm_typ = "Lohnbuchhaltungsstammdatenakte%";
				break;
			case "DDEBD":
			case "AAURR":
			case "AAURU":
			case "AAURA":
			case "AAUR2":
				wm_typ = "Ausgangsrechnungsakte%";
				break;
			case "AKROR":
			case "AKRER":
			case "AKREU":
			case "AKRU2":
			case "DKRED":
				wm_typ = "Kreditorenakte%";
				break;
			case "DUMMY":
			case "ADUMY":
			case "DDUMY":
				wm_typ = "%";
				break;
			
		}
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_2 from firmen_spezifisch where kue_dokuart = 'AVMNT' and dok_dat_feld_1 like '" + wm_typ + "' order by dok_dat_feld_2 ";
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_2 } );
		}
	}
	
	
	@ValueSet( entrypoint = "WM_DARegister" )
	def WM_DARegister ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 4 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		
		def wm_typ = "";
		
		switch ( docType.id )
		{
			case "AUREG":
			case "AURG2":
			case "AURG3":
			case "DDAUE":
				wm_typ = "Dauerakte%";
				break;
			case "AJAOB":
			case "AJAAB":
			case "DABSC":
			case "AJAUR":
			case "AJAU2":
				wm_typ = "Jahresabschlussakte%";
				break;
			case "DBERA":
			case "ABERO":
			case "ABERA":
			case "ABEUR":
			case "ABEU2":
				wm_typ = "Beratungsakte%";
				break;
			case "DSTEU":
			case "ASTOR":
			case "ASTRA":
			case "ASTUR":
			case "ASTU2":
				wm_typ = "Steuerakte%";
				break;
			case "DBETR":
			case "ABEPA":
			case "ABEOR":
			case "ABPUR":
			case "ABPU2":
				wm_typ = "Betriebspruefungsakte%";
				break;
			case "DFIBU":
			case "AFIOR":
			case "AFIRA":
			case "AFIUR":
			case "AFIU2":
				wm_typ = "Fibuakte%";
				break;
			case "DLOHN":
			case "ALBOR":
			case "ALBRE":
			case "ALJUR":
			case "ALJR2":
				wm_typ = "Lohnbuchhaltungjahresakte%";
				break;
			case "DLSTD":
			case "ALBSO":
			case "ALOUR":
			case "ALBU2":
				wm_typ = "Lohnbuchhaltungsstammdatenakte%";
				break;
			case "DDEBD":
			case "AAURR":
			case "AAURU":
			case "AAUR2":
				wm_typ = "Ausgangsrechnungsakte%";
				break;
			case "DKRED":
			case "AKROR":
				wm_typ = "Kreditorenakte%";
				break;
			case "DUMMY":
			case "AOREG":
			case "AREGS":
				wm_typ = "%";
				break;
			
		}
		
		def where = "";
		def where2 = "";
		
		if ( doc.field[ 3 ] != null )
		{
			where = " and dok_dat_feld_2 = '" + doc.field[ 3 ] + "'";
		}
		
		if ( doc.field[ 1 ] != null )
		{
			where2 = " and dok_dat_feld_1 = '" + doc.field[ 1 ] + "' ";
		}
		
		if ( doc.field[ 10 ] )
		{
			where2 = where2 + " and dok_dat_feld_10 = '" + doc.field[ 10 ] + "' ";
		}
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_3 as ddf3 from firmen_spezifisch where kue_dokuart = 'AVMNT' and dok_dat_feld_1 like '" + wm_typ + "' " + where + " union select distinct dok_dat_feld_4 as ddf3 from firmen_spezifisch where kue_dokuart in ('ABERA', 'ABEPA', 'AREGS', 'AFIRA', 'AJAAB', 'ALBRE', 'ALORE', 'ASTRA', 'AKRER' ) " + where2 + " and dok_dat_feld_4 != '' and dok_dat_feld_4 is not null and dok_dat_feld_4 not in (select dok_dat_feld_3 from firmen_spezifisch where kue_dokuart = 'AVMNT' and dok_dat_feld_3 is not null ) and dok_dat_feld_4 not in ( select dok_dat_feld_4 from firmen_spezifisch where kue_dokuart = 'AVMNT' and dok_dat_feld_4 is not null ) order by ddf3";
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.ddf3 } );
		}
		
	}
	
	
	@ValueSet( entrypoint = "WM_DAUnterRegister" )
	def WM_DAUnterRegister ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 23 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		
		// Es wird geprüft, ob das Register innerhalb des Mandanten (DDF1=>Mandant-Nr.) unterhalb des jetzt verwendeten Registers (DDF4)
		// angelegt wurde.
		
		def such_dokart, where = "";
		
		switch ( docType.id )
		{
			case "AUREG":
			case "AURG2":
			case "AURG3":
			case "DDAUE":
				such_dokart = "AUREG";
				break;
			case "DABSC":
			case "AJAUR":
			case "AJAU2":
				such_dokart = "AJAUR";
				break;
			case "DBERA":
			case "ABEUR":
			case "ABEU2":
				such_dokart = "ABEUR";
				break;
			case "DSTEU":
			case "ASTUR":
			case "ASTU2":
				such_dokart = "ASTUR";
				break;
			case "DBETR":
			case "ABPUR":
			case "ABPU2":
				such_dokart = "ABPUR";
				break;
			case "DFIBU":
			case "AFIUR":
			case "AFIU2":
				such_dokart = "AFIUR";
				break;
			case "DLOHN":
			case "ALJUR":
			case "ALJR2":
				such_dokart = "ALJUR";
				break;
			case "DLSTD":
			case "ALOUR":
			case "ALBU2":
				such_dokart = "ALOUR";
				break;
			case "DDEBD":
			case "AAURU":
			case "AAUR2":
				such_dokart = "AAURU";
				break;
			case "DKRED":
			case "AKREU":
			case "AKRU2":
				such_dokart = "ABEUR";
				break;
			
		}
		
		if ( doc.field[ 1 ] )
		{
			where = " and dok_dat_feld_1 = '" + doc.field[ 1 ] + "' ";
		}
		
		if ( doc.field[ 10 ] )
		{
			where = where + " and dok_dat_feld_10 = '" + doc.field[ 10 ] + "' ";
		}
		
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_23 from firmen_spezifisch \
							where kue_dokuart in ( '" + such_dokart + "' ) \
							and dok_dat_feld_23 != '' \
							and dok_dat_feld_4 = '" + doc.field[ 4 ] + "' " + where;
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_23 } );
		}
		
	}
	
	
	@ValueSet( entrypoint = "WM_DAUnterRegister2" )
	def WM_DAUnterRegister2( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 17 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		
		// Es wird geprüft, ob das Register innerhalb des Mandanten (DDF1=>Mandant-Nr.) unterhalb des jetzt verwendeten Registers (DDF4)
		// angelegt wurde.
		
		def such_dokart, where = "";
		
		switch ( docType.id )
		{
			case "AURG2":
			case "AURG3":
			case "DDAUE":
				such_dokart = "AURG2";
				break;
			case "DABSC":
			case "AJAU2":
				such_dokart = "AJAU2";
				break;
			case "DBERA":
			case "ABEU2":
				such_dokart = "ABEU2";
				break;
			case "DSTEU":
			case "ASTU2":
				such_dokart = "ASTU2";
				break;
			case "DBETR":
			case "ABPU2":
				such_dokart = "ABPU2";
				break;
			case "DFIBU":
			case "AFIU2":
				such_dokart = "AFIU2";
				break;
			case "DLOHN":
			case "ALJR2":
				such_dokart = "ALJR2";
				break;
			case "DLSTD":
			case "ALBU2":
				such_dokart = "ALBU2";
				break;
			case "DDEBD":
			case "AAUR2":
				such_dokart = "AAUR2";
				break;
			case "DKRED":
			case "AKRU2":
				such_dokart = "AKRU2";
				break;
			
		}
		
		
		if ( doc.field[ 1 ] )
		{
			where = " and dok_dat_feld_1 = '" + doc.field[ 1 ] + "' ";
		}
		
		if ( doc.field[ 10 ] )
		{
			where = where + " and dok_dat_feld_10 = '" + doc.field[ 10 ] + "' ";
		}
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_17 from firmen_spezifisch \
							where kue_dokuart in ( '" + such_dokart + "' ) \
							and dok_dat_feld_17 != '' \
							and dok_dat_feld_23 = '" + doc.field[ 23 ] + "' " + where;
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_17 } );
		}
		
	}
	
	
	@ValueSet( entrypoint = "WM_DAUnterRegister3" )
	def WM_DAUnterRegister3( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 32 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		// Es wird geprüft, ob das Register innerhalb des Mandanten (DDF1=>Mandant-Nr.) unterhalb des jetzt verwendeten Registers (DDF4)
		// angelegt wurde.
		
		def where = "";
		
		if ( doc.field[ 1 ] )
		{
			where = " and dok_dat_feld_1 = '" + doc.field[ 1 ] + "' ";
		}
		
		if ( doc.field[ 10 ] )
		{
			where = where + " and dok_dat_feld_10 = '" + doc.field[ 10 ] + "' ";
		}
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_32 from firmen_spezifisch \
							where kue_dokuart in ( 'AURG3' ) \
							and dok_dat_feld_32 != '' \
							and dok_dat_feld_17 = '" + doc.field[ 17 ] + "' " + where;
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_32 } );
		}
		
	}
	
	
	@ValueSet( entrypoint = "WM_Aktennummer" )
	def WM_Aktennummer ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 10 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		
		def where = "";
		def kuerzel = "";
		
		def mandant_oder_kreditor;
		def dokuarten;
		
		switch ( docType.id )
		{
			case "DDAUE": // Dauerakte
			case "AUREG":
			case "AURG2":
			case "AURG3":
			case "AOREG":
			case "AREGS":
			case "ADAUR":
				mandant_oder_kreditor = 0;
				kuerzel = "DA";
				break;
			case "DABSC": // Jahresabschlussakte
			case "AJAAA":
			case "AJAOB":
			case "AJAAB":
			case "AJAUR":
			case "AJAU2":
				mandant_oder_kreditor = 0;
				kuerzel = "JA";
				break;
			case "DBERA": // Beratungsakte
			case "ABERB":
			case "ABERO":
			case "ABERA":
			case "ABEUR":
			case "ABEU2":
				mandant_oder_kreditor = 0;
				kuerzel = "BE";
				break;
			case "DSTEU": // Steuerakte
			case "ASTEU":
			case "ASTOR":
			case "ASTRA":
			case "ASTUR":
			case "ASTU2":
				mandant_oder_kreditor = 0;
				kuerzel = "ST";
				break;
			case "DBETR": // Betriebspruefungsakte
			case "ABETR":
			case "ABEPA":
			case "ABEOR":
			case "ABPUR":
			case "ABPU2":
				mandant_oder_kreditor = 0;
				kuerzel = "BP";
				break;
			case "DFIBU": // Fibuakte
			case "AFIBA":
			case "AFIOR":
			case "AFIRA":
			case "AFIUR":
			case "AFIU2":
				mandant_oder_kreditor = 0;
				kuerzel = "FI";
				break;
			case "DLOHN": // Lohnbuchhaltungjahresakte
			case "ALOJA":
			case "ALBOR":
			case "ALBRE":
			case "ALJUR":
			case "ALJR2":
				mandant_oder_kreditor = 0;
				kuerzel = "LO";
				break;
			case "DLSTD": // Lohnbuchhaltungsstammdatenakte
			case "ALBSA":
			case "ALBSO":
			case "ALORE":
			case "ALOUR":
			case "ALBU2":
				mandant_oder_kreditor = 0;
				kuerzel = "LS";
				break;
			case "DDEBD": // Ausgangsrechnungsakte
			case "AAURE":
			case "AAURR":
			case "AAURA":
			case "AAURU":
			case "AAUR2":
				mandant_oder_kreditor = 0;
				kuerzel = "RE";
				break;
			case "AKRED": // Kreditorenakte
			case "AKROR":
			case "AKRER":
			case "AKREU":
			case "AKRU2":
			case "DKRED":
				mandant_oder_kreditor = 1;
				kuerzel = "KA";
				break;
			case "AEVAK": // Eigenverwaltungsakte
			case "AEVOR":
			case "AEVRE":
			case "DEVDO":
				mandant_oder_kreditor = 2;
				kuerzel = "EV";
				break;
		}
		
		switch ( mandant_oder_kreditor )
		{
			case 0:
				if ( doc.field[ 2 ] )
				{
					where = " and dok_dat_feld_2 = '" + doc.field[ 2 ] + "' ";
				}
				if ( doc.field[ 1 ] )
				{
					where = " and dok_dat_feld_1 = '" + doc.field[ 1 ] + "' ";
				}
				dokuarten = "'DBERA', 'DBETR', 'DDAUE', 'DDEBD', 'DFIBU', 'DABSC', 'DLOHN', 'DLSTD', 'DSTEU', 'ABERB', 'AAURE', 'ABETR', 'ADAUR', 'AFIBA', 'AJAAA', 'AKRED', 'ALOJA', 'ALBSA', 'ASTEU'";
				break;
			case 1:
				if ( doc.field[ 20 ] )
				{
					where = " and dok_dat_feld_20 = '" + doc.field[ 20 ] + "' ";
				}
				if ( doc.field[ 19 ] )
				{
					where = " and dok_dat_feld_19 = '" + doc.field[ 19 ] + "' ";
				}
				dokuarten = "'DKRED', 'AKRED'"
				break;
			case 2:
				dokuarten = "'AEVAK', 'DEVDO'"
				break;
			
		}
		
		if ( doc.field[ 21 ] )
		{
			where = where + " and dok_dat_feld_21 = '" + doc.field[ 21 ] + "' ";
		}
		
		where = where + " and dok_dat_feld_10 like '%" + kuerzel + "%' ";
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_10 from firmen_spezifisch where kue_dokuart in ( " + dokuarten + " ) " + where; 
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_10 } );
		}
	}
	
	
	@ValueSet( entrypoint = "WM_MandantNummer" )
	def WM_MandantNummer ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 1 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		def where = "";
		def sqland = "";
		
		if ( doc.field[ 2 ] )
		{
			where = " where clientname = '" + doc.field[ 2 ].toString().replace( "\'", "''" ) + "' ";
		}
		
		if ( doc.field[ 13 ] )
		{
			if ( where.length() > 0 )
			{
				sqland = " and ";
			}
			else
			{
				sqland = " where ";
			}
			where = where + sqland + " client_group_number = '" + doc.field[ 13 ] + "' ";
		}
		
		def sqlQuery = "select distinct top (10000) number from DatevClients " + where;
		
		//d3.log.error( "SQLQUERY WM " + sqlQuery );
		
		def sql = groovy.sql.Sql.newInstance( "jdbc:sqlserver://kp-sql01\\D3;databaseName=D3P_Datev", 'd3_master', 'Xbn8"Ep4', 'com.microsoft.sqlserver.jdbc.SQLServerDriver' );
		
		def rows = sql.rows( sqlQuery );
		
		if ( rows.size() > 0 )
		{
			reposField.provideValuesForValueSet( rows.collect{ it.number } );
		}
		
		sql.close();
	}
	
	
	@ValueSet( entrypoint = "WM_MandantName" )
	def WM_MandantName ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 2 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		
		def where = "";
		def sqland = "";
		
		if ( doc.field[ 1 ] )
		{
			where = " where number = '" + doc.field[ 1 ] + "' ";
		}
		
		
		if ( doc.field[ 13 ] )
		{
			if ( where.length() > 0 )
			{
				sqland = " and ";
			}
			else
			{
				sqland = " where ";
			}
			where = where + sqland + " client_group_number = '" + doc.field[ 13 ] + "' ";
		}
      
      
		def sqlQuery = "select distinct top (10000) clientname from DatevClients " + where;
		
		def sql = groovy.sql.Sql.newInstance( "jdbc:sqlserver://kp-sql01\\D3;databaseName=D3P_Datev", 'd3_master', 'Xbn8"Ep4', 'com.microsoft.sqlserver.jdbc.SQLServerDriver' );
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		def rows = sql.rows( sqlQuery );
		
		if ( rows.size() > 0 )
		{
			reposField.provideValuesForValueSet( rows.collect{ it.clientname } );
		}
		
		sql.close();
		
	}
	
	
	@ValueSet( entrypoint = "WM_MandantGruppenNummer" )
	def WM_MandantGruppenNummer ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 13 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		
		def where = "";
		
		if ( doc.field[ 1 ] )
		{
			where = " and dok_dat_feld_1 = '" + doc.field[ 1 ] + "' ";
		}
		
		if ( doc.field[ 10 ] )
		{
			def mandantnr = doc.field[ 10 ].substring( 0, 5 );
			where = " and dok_dat_feld_1 = '" + mandantnr + "' ";
		}
		
		// Der Mandantname wird nur mit einbezogen, wenn Mandantnummer oder Aktennummer (wo die Mandant-Nr. enthalten ist) nicht angegeben sind.
		if ( ( doc.field[ 2 ] ) && ( ! ( doc.field[ 1 ] || doc.field[ 10 ] ) ) )
		{
			where = where + " and dok_dat_feld_2 = '" + doc.field[ 2 ].toString().replace( "\'", "''" ) + "' ";
		}
		
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_13 from firmen_spezifisch where kue_dokuart in ( 'AMAND' ) " + where; 
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_13 } );
		}
      else
      {
         reposField.provideValuesForValueSet( ['<kein>'] );
      }
		
	}
	
	
	@ValueSet( entrypoint = "WM_AktenName" )
	def WM_AktenName ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 21 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		
		def where = "";
		def kuerzel = "";
		
		def mandant_oder_kreditor;
		def dokuarten;
		
		switch ( docType.id )
		{
			case "DDAUE": // Dauerakte
			case "AUREG":
			case "AURG2":
			case "AURG3":
			case "AOREG":
			case "AREGS":
			case "ADAUR":
				dokuarten = "'DDAUE', 'ADAUR'";
				mandant_oder_kreditor = 0;
				kuerzel = "DA";
				break;
			case "DABSC": // Jahresabschlussakte
			case "AJAAA":
			case "AJAOB":
			case "AJAAB":
			case "AJAUR":
			case "AJAU2":
				dokuarten = "'DABSC', 'AJAAA'";
				mandant_oder_kreditor = 0;
				kuerzel = "JA";
				break;
			case "DBERA": // Beratungsakte
			case "ABERB":
			case "ABERO":
			case "ABERA":
			case "ABEUR":
			case "ABEU2":
				dokuarten = "'DBERA', 'ABERB'";
				mandant_oder_kreditor = 0;
				kuerzel = "BE";
				break;
			case "DSTEU": // Steuerakte
			case "ASTEU":
			case "ASTOR":
			case "ASTRA":
			case "ASTUR":
			case "ASTU2":
				dokuarten = "'DSTEU', 'ASTEU'";
				mandant_oder_kreditor = 0;
				kuerzel = "ST";
				break;
			case "DBETR": // Betriebspruefungsakte
			case "ABETR":
			case "ABEPA":
			case "ABEOR":
			case "ABPUR":
			case "ABPU2":
				dokuarten = "'DBETR', 'ABETR'";
				mandant_oder_kreditor = 0;
				kuerzel = "BP";
				break;
			case "DFIBU": // Fibuakte
			case "AFIBA":
			case "AFIOR":
			case "AFIRA":
			case "AFIUR":
			case "AFIU2":
				dokuarten = "'DFIBU', 'AFIBA'";
				mandant_oder_kreditor = 0;
				kuerzel = "FI";
				break;
			case "DLOHN": // Lohnbuchhaltungjahresakte
			case "ALOJA":
			case "ALBOR":
			case "ALBRE":
			case "ALJUR":
			case "ALJR2":
				dokuarten = "'DLOHN', 'ALOJA'";
				mandant_oder_kreditor = 0;
				kuerzel = "LO";
				break;
			case "DLSTD": // Lohnbuchhaltungsstammdatenakte
			case "ALBSA":
			case "ALBSO":
			case "ALORE":
			case "ALOUR":
			case "ALBU2":
				dokuarten = "'DLSTD', 'ALBSA'";
				mandant_oder_kreditor = 0;
				kuerzel = "LS";
				break;
			case "DDEBD": // Ausgangsrechnungsakte
			case "AAURE":
			case "AAURR":
			case "AAURA":
			case "AAURU":
			case "AAUR2":
				dokuarten = "'DDEBD', 'AAURE'";
				mandant_oder_kreditor = 0;
				kuerzel = "RE";
				break;
			case "AKRED": // Kreditorenakte
			case "AKROR":
			case "AKRER":
			case "AKREU":
			case "AKRU2":
			case "DKRED":
				dokuarten = "'DKRED', 'AKRED'";
				mandant_oder_kreditor = 1;
				kuerzel = "KA";
				break;
			case "AEVAK": // Eigenverwaltungsakte
			case "AEVOR":
			case "AEVRE":
			case "DEVDO":
				dokuarten = "'AEVAK', 'DEVDO'";
				mandant_oder_kreditor = 2;
				kuerzel = "EV";
				break;
		}
		
		switch ( mandant_oder_kreditor )
		{
			case 0:
				if ( doc.field[ 2 ] )
				{
					where = " and dok_dat_feld_2 = '" + doc.field[ 2 ] + "' ";
				}
				if ( doc.field[ 1 ] )
				{
					where = " and dok_dat_feld_1 = '" + doc.field[ 1 ] + "' ";
				}
				break;
			case 1:
				if ( doc.field[ 20 ] )
				{
					where = " and dok_dat_feld_20 = '" + doc.field[ 20 ] + "' ";
				}
				if ( doc.field[ 19 ] )
				{
					where = " and dok_dat_feld_19 = '" + doc.field[ 19 ] + "' ";
				}
				break;
			case 2:
				if ( doc.field[ 21 ] )
				{
					where = " and dok_dat_feld_21 = '" + doc.field[ 21 ] + "' ";
				}
				break;
			
		}
		
		if ( doc.field[ 10 ] )
		{
			where = where + " and dok_dat_feld_10 = '" + doc.field[ 10 ] + "' ";
		}
		
		
		
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_21 from firmen_spezifisch where kue_dokuart in ( " + dokuarten  + " ) " + where; 
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_21 } );
		}
		
	}


	@ValueSet( entrypoint = "WM_Grundstueck" )
	def WM_Grundstueck ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{						  
		// Feststellen der Mandantennummer
		// Feststellen der Doku-ID der Akte "04.01 Grundstücke" zu diesem Mandanten
		// Ermitteln der Grundstückseinträge in dieser Akte
		// Anbieten als Wertemenge
		
		// Ohne Mandantennummer keine Werte!
		def mandantnr;
		
		if ( doc.field[ 1 ] )
		{
			mandantnr = doc.field[ 1 ];
		}
		else
		{
			mandantnr = doc.field[ 10 ].substring( 0, 5 );
		}
		//d3.log.info( "gefundene mandantnr " + mandantnr );
		if ( mandantnr )
		{
			def sqlQuery = "select doku_id from firmen_spezifisch where kue_dokuart = 'AREGS' and dok_dat_feld_4 like '04.01 Grundst_cke' and dok_dat_feld_1 = '" + doc.field[ 1 ] + "' order by doku_id desc";
			
			def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
			
			//d3.log.info( "SQLQUERY WM " + sqlQuery );
			
			//d3.log.info( "resultRows size: " + resultRows.size() );
			
			if ( resultRows.size() > 0 )
			{
				def doku_id = resultRows[ 0 ].doku_id;
				//d3.log.info( "gefundene Doku-ID " + doku_id );
				sqlQuery = "select top (10000) value_char from firm_spez_mult_val where doku_id = '" + doku_id + "' and field_no = 68 ";
				
				resultRows = d3.sql.executeAndGet( (String) sqlQuery );
				
				//d3.log.info( "SQLQUERY WM " + sqlQuery );
				
				if ( resultRows.size() > 0 )
				{
					reposField.provideValuesForValueSet( resultRows.collect{ it.value_char } );
				}
			}
		}
	}
	
	
	@ValueSet( entrypoint = "WM_KreditorName" )
	def WM_KreditorName ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 20 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		
		def where = "";
		
		if ( doc.field[ 19 ] )
		{
			where = " and dok_dat_feld_19 = '" + doc.field[ 19 ] + "' ";
		}
		
		if ( doc.field[ 10 ] )
		{
			def krednr = doc.field[ 10 ].substring( 0, 5 );
			where = where + " and dok_dat_feld_19 = '" + krednr + "' ";
		}
		
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_20 from firmen_spezifisch where kue_dokuart in ( 'AKRED' ) " + where; 
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_20 } );
		}
		
	}
	
	
	@ValueSet( entrypoint = "WM_KreditorNummer" )
	def WM_KreditorNummer ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 19 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		
		
		def where = "";
		
		if ( doc.field[ 20 ] )
		{
			where = " and dok_dat_feld_20 = '" + doc.field[ 20 ] + "' ";
		}
		
		if ( doc.field[ 10 ] )
		{
			def krednr = doc.field[ 10 ].substring( 0, 5 );
			where = where + " and dok_dat_feld_19 = '" + krednr + "' ";
		}
		
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_19 from firmen_spezifisch where kue_dokuart in ( 'AKRED' ) " + where; 
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_19 } );
		}
		
	}

	
	@ValueSet( entrypoint = "WM_EVOberregister" )
	def WM_EVOberregister ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 40 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		
		def where = "";
		
		if ( doc.field[ 10 ] )
		{
			where = " and dok_dat_feld_10 = '" + doc.field[ 10 ] + "' ";
		}
		
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_40 from firmen_spezifisch where kue_dokuart in ( 'AEVOR' ) " + where; 
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_40 } );
		}
		
	}
	
	
	@ValueSet( entrypoint = "WM_EVRegister" )
	def WM_EVRegister ( D3Interface d3, RepositoryField reposField, User user, DocumentType docType,
                          int rowNo, int validate, Document doc )
	{
		
		if ( user.id == "d3_server" || user.id == "d3_async" )
		{
            List<String> tmpList = [doc.field[ 39 ]]
            reposField.provideValuesForValueSet( tmpList );
            return 0;
        }
		
		def where = "";
		
		if ( doc.field[ 10 ] )
		{
			where = " and dok_dat_feld_10 = '" + doc.field[ 10 ] + "' ";
		}
		
		if ( doc.field[ 40 ] )
		{
			where = where + " and dok_dat_feld_40 = '" + doc.field[ 40 ] + "' ";
		}
		
		
		
		def sqlQuery = "select distinct top (10000) dok_dat_feld_39 from firmen_spezifisch where kue_dokuart in ( 'AEVRE' ) " + where; 
		
		//d3.log.info( "SQLQUERY WM " + sqlQuery );
		
		
		def resultRows = d3.sql.executeAndGet( (String) sqlQuery );
		
		if ( resultRows.size() > 0 )
		{
			reposField.provideValuesForValueSet( resultRows.collect{ it.dok_dat_feld_39 } );
		}
		
	}
	
	
	
	
	











}