////////////////////////////////////////////////////////////////////////////////
// Hook for dbs | invoice
// => Sets for user in d.3 admin
// File-Encoding: UTF-8 without BOM( Test: ü,ä,ö )
//------------------------------------------------------------------------------
// Created/Edited       | Description
//------------------------------------------------------------------------------
// 08.2017              | dbs | invoice workflow 1.1.0
////////////////////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------
// Dynamic value sets
/*------------------------------------------------------------------------------
// - GetCurrency 
// - GetCreditorNo
// - GetCreditorName
// - GetCompanyCode
// - GetClient
//------------------------------------------------------------------------------
*/
proc GetCurrency( pReposId, pUser, pDocTypeShort, pRowNo ) 
{
   vars lFromClause
   //add scheme to from clause if set
   if(gGMF_SCHEME_NAME != "")
   {
      lFromClause = ":(gGMF_SCHEME_NAME).:(gGMF_TABLE_CURRENCIES)"
   }
   else
   {
      lFromClause = ":gGMF_TABLE_CURRENCIES"
   } 
   
	DBMS ALIAS d3server_value_char_allowed, d3server_repos_id_allowed
	DBMS SQL SELECT DISTINCT iso, \
			         :pReposId \
		        FROM :lFromClause \
		    ORDER BY iso
	DBMS ALIAS
}//end of GetCurrency

proc GetCreditorNo( pReposId, pUser, pDocTypeShort, pRowNo ) 
{
	vars lWhereClause = "", lContactId = "", lClient = "", lCompanyCode = "", lContactName = "", lFromClause
	
   lContactId = dok_dat_feld[ gDDF_CONTACT_ID ]
	
	// add wildcard
	lContactId = lContactId ## "%"
	lWhereClause = "WHERE creditor_no LIKE :+lContactId"
	
	// if company code is known add to where-clause
   lCompanyCode = dok_dat_feld[ gDDF_COMPANY_CODE ]
	if ( lCompanyCode != "" )
	{
		lWhereClause = lWhereClause ## " AND company_code = :+lCompanyCode "  
	}
   
   // if name is known add to where-clause
   lContactName = dok_dat_feld[ gDDF_CONTACT_NAME ]
	if ( lContactName != "" )
	{
      lContactName = lContactName ## "%"
		lWhereClause = lWhereClause ## " AND name LIKE :+lContactName "  
	}
	
	// if client is known add to where-clause
   lClient = dok_dat_feld[ gDDF_CLIENT ]
	if ( lClient != "" )
	{
		lWhereClause = lWhereClause ## " AND client = :+lClient "  
	}
	
	//add scheme to from clause if set
   if(gGMF_SCHEME_NAME != "")
   {
      lFromClause = ":(gGMF_SCHEME_NAME).:(gGMF_TABLE_CREDITORS)"
   }
   else
   {
      lFromClause = ":gGMF_TABLE_CREDITORS"
   } 
	
	DBMS ALIAS d3server_value_char_allowed, d3server_repos_id_allowed
	DBMS SQL SELECT DISTINCT creditor_no, \
			          :pReposId \
		        FROM :lFromClause :lWhereClause \
		    ORDER BY creditor_no
	DBMS ALIAS
}//end of GetCreditorNo

proc GetCreditorName( pReposId, pUser, pDocTypeShort, pRowNo ) 
{
	vars lWhereClause = "", lContactName = "", lClient = "", lCompanyCode = "", lContactId = "", lFromClause
   
   lContactName = dok_dat_feld[ gDDF_CONTACT_NAME ]
	
	// add wildcard
	lContactName = lContactName ## "%"
	lWhereClause = "WHERE name LIKE :+lContactName"
	
	// if company code is known add to where-clause
   lCompanyCode = dok_dat_feld[ gDDF_COMPANY_CODE ]
	if ( lCompanyCode != "" )
	{
		lWhereClause = lWhereClause ## " AND company_code = :+lCompanyCode "  
	}
	
	// if client is known add to where-clause
   lClient = dok_dat_feld[ gDDF_CLIENT ]
	if ( lClient != "" )
	{
		lWhereClause = lWhereClause ## " AND client = :+lClient "  
	}
	
	// if creditor_no is known add to where-clause
   lContactId = dok_dat_feld[ gDDF_CONTACT_ID ]
	if ( lContactId != "" )
	{
		lWhereClause = lWhereClause ## " AND creditor_no = :+lContactId "  
	}
	
	//add scheme to from clause if set
   if(gGMF_SCHEME_NAME != "")
   {
      lFromClause = ":(gGMF_SCHEME_NAME).:(gGMF_TABLE_CREDITORS)"
   }
   else
   {
      lFromClause = ":gGMF_TABLE_CREDITORS"
   } 
	
	DBMS ALIAS d3server_value_char_allowed, d3server_repos_id_allowed
	DBMS SQL SELECT DISTINCT name, \
			          :pReposId \
		        FROM :lFromClause :lWhereClause \
		    ORDER BY name
	DBMS ALIAS
}//end of GetCreditorName

proc GetClient( pReposId, pUser, pDocTypeShort, pRowNo ) 
{  
   vars lFromClause
   
   //add scheme to from clause if set
   if(gGMF_SCHEME_NAME != "")
   {
      lFromClause = ":(gGMF_SCHEME_NAME).:(gGMF_TABLE_COMPANY_CODE)"
   }
   else
   {
      lFromClause = ":gGMF_TABLE_COMPANY_CODE"
   } 
	DBMS ALIAS d3server_value_char_allowed, d3server_repos_id_allowed
	DBMS SQL SELECT DISTINCT client_id, \
			          :pReposId \
		        FROM :lFromClause \
		    ORDER BY client_id
	DBMS ALIAS
}//end of GetClient

proc GetCompanyCode( pReposId, pUser, pDocTypeShort, pRowNo ) 
{
	//CompanyCode depends on Client
   vars lFromClause, lWhereClause, lClient
   
   lWhereClause = ""
   lClient      = dok_dat_feld[ gDDF_CLIENT ]
   
	if ( lClient != "" )
	{
		lWhereClause = "WHERE client_id = :+lClient"  
	}
	
	//add scheme to from clause if set
	if(gGMF_SCHEME_NAME != "")
   {
      lFromClause = ":(gGMF_SCHEME_NAME).:(gGMF_TABLE_COMPANY_CODE)"
   }
   else
   {
      lFromClause = ":gGMF_TABLE_COMPANY_CODE"
   }
   
   DBMS ALIAS d3server_value_char_allowed, d3server_repos_id_allowed
	DBMS SQL SELECT DISTINCT company_id, \
			          :pReposId \
		        FROM :lFromClause :lWhereClause \
		    ORDER BY company_id
	DBMS ALIAS
}//end of GetCompanyCode
