diff --git a/.gitignore b/.gitignore
index a510a17..c918eb9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,6 +7,10 @@
bin/
obj/
+# SSDT / SQL database project caches (regenerated)
+*.dbmdl
+*.jfm
+
# NuGet
packages/
*.nupkg
diff --git a/Fuchs_Database/FuchsDatabase.slnx b/Fuchs_Database/FuchsDatabase.slnx
new file mode 100644
index 0000000..ce275a3
--- /dev/null
+++ b/Fuchs_Database/FuchsDatabase.slnx
@@ -0,0 +1,6 @@
+
+
+
+
+
+
diff --git a/Fuchs_Database/FuchsDatabase.sqlproj b/Fuchs_Database/FuchsDatabase.sqlproj
new file mode 100644
index 0000000..b9fbe26
--- /dev/null
+++ b/Fuchs_Database/FuchsDatabase.sqlproj
@@ -0,0 +1,424 @@
+
+
+
+ Debug
+ AnyCPU
+ FuchsDatabase
+ 2.0
+ 4.1
+ {c062672e-866d-4c74-b6de-8d660a42e885}
+ Microsoft.Data.Tools.Schema.Sql.Sql170DatabaseSchemaProvider
+ Database
+
+
+ FuchsDatabase
+ FuchsDatabase
+ 1033, CI
+ BySchemaAndSchemaType
+ True
+ v4.7.2
+ CS
+ Properties
+ False
+ True
+ True
+
+
+ bin\Release\
+ $(MSBuildProjectName).sql
+ False
+ pdbonly
+ true
+ false
+ true
+ prompt
+ 4
+
+
+ bin\Debug\
+ $(MSBuildProjectName).sql
+ false
+ true
+ full
+ false
+ true
+ true
+ prompt
+ 4
+
+
+ 11.0
+
+ True
+ 11.0
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Fuchs_Database/Security/fds_rwe.sql b/Fuchs_Database/Security/fds_rwe.sql
new file mode 100644
index 0000000..6a2b978
--- /dev/null
+++ b/Fuchs_Database/Security/fds_rwe.sql
@@ -0,0 +1,7 @@
+CREATE ROLE [fds_rwe]
+ AUTHORIZATION [dbo];
+
+
+GO
+ALTER ROLE [fds_rwe] ADD MEMBER [fuchs_fds];
+
diff --git a/Fuchs_Database/Security/fuchs_enc_1.sql b/Fuchs_Database/Security/fuchs_enc_1.sql
new file mode 100644
index 0000000..eee1734
--- /dev/null
+++ b/Fuchs_Database/Security/fuchs_enc_1.sql
@@ -0,0 +1,15 @@
+CREATE SYMMETRIC KEY [fuchs_enc_1]
+ AUTHORIZATION [dbo]
+ WITH ALGORITHM = AES_256
+ ENCRYPTION BY PASSWORD = N'lwppdri?q{jdzus&bkuaheklmsFT7_$!~
+
+ 10
+
+
+ Data Source=MSSQL4.NBG4.DOMAINXYZ.DE,10439;Initial Catalog=site_fuchs;Persist Security Info=False;User ID=ott1;Pooling=False;Multiple Active Result Sets=False;Connect Timeout=60;Encrypt=True;Trust Server Certificate=True;Command Timeout=0
+
+
+
+
+ {c062672e-866d-4c74-b6de-8d660a42e885}
+ FuchsDatabase
+
+
+
+
+
+ Version
+ 1
+
+
+
+
+ PlanGenerationType
+ SqlDeploymentOptions
+
+
+ AllowExistingModelErrors
+ False
+
+
+ AllowIncompatiblePlatform
+ False
+
+
+ AllowTableRecreation
+ True
+
+
+ BackupDatabaseBeforeChanges
+ False
+
+
+ IgnoreIndexesStatisticsOnEnclaveEnabledColumns
+ False
+
+
+ BlockOnPossibleDataLoss
+ True
+
+
+ BlockWhenDriftDetected
+ False
+
+
+ CompareUsingTargetCollation
+ False
+
+
+ CommentOutSetVarDeclarations
+ False
+
+
+ CreateNewDatabase
+ False
+
+
+ DeployDatabaseInSingleUserMode
+ False
+
+
+ DisableAndReenableDdlTriggers
+ True
+
+
+ DisableIndexesForDataPhase
+ True
+
+
+ DisableParallelismForEnablingIndexes
+ False
+
+
+ DoNotAlterChangeDataCaptureObjects
+ True
+
+
+ DoNotAlterReplicatedObjects
+ True
+
+
+ DropConstraintsNotInSource
+ True
+
+
+ DropDmlTriggersNotInSource
+ True
+
+
+ DropExtendedPropertiesNotInSource
+ True
+
+
+ DropIndexesNotInSource
+ True
+
+
+ DropPermissionsNotInSource
+ False
+
+
+ DropObjectsNotInSource
+ True
+
+
+ DropRoleMembersNotInSource
+ False
+
+
+ DropStatisticsNotInSource
+ True
+
+
+ GenerateSmartDefaults
+ False
+
+
+ HashObjectNamesInLogs
+ False
+
+
+ IgnoreDdlTriggerOrder
+ False
+
+
+ IgnoreDdlTriggerState
+ False
+
+
+ IgnoreObjectPlacementOnPartitionScheme
+ True
+
+
+ IgnoreAuthorizer
+ False
+
+
+ IgnoreDefaultSchema
+ False
+
+
+ IgnoreRouteLifetime
+ True
+
+
+ IgnoreCryptographicProviderFilePath
+ True
+
+
+ IgnoreComments
+ False
+
+
+ IgnoreWhitespace
+ True
+
+
+ IgnoreKeywordCasing
+ True
+
+
+ IgnoreSemicolonBetweenStatements
+ True
+
+
+ IgnorePartitionSchemes
+ False
+
+
+ IgnorePreDeployScript
+ False
+
+
+ IgnorePostDeployScript
+ False
+
+
+ IgnoreTablePartitionOptions
+ False
+
+
+ IgnoreWithNocheckOnCheckConstraints
+ False
+
+
+ IgnoreWithNocheckOnForeignKeys
+ False
+
+
+ IgnoreIdentitySeed
+ False
+
+
+ IgnoreIncrement
+ False
+
+
+ IgnoreFillFactor
+ True
+
+
+ IgnoreIndexPadding
+ True
+
+
+ IgnoreColumnCollation
+ False
+
+
+ IgnoreColumnOrder
+ False
+
+
+ IgnoreLockHintsOnIndexes
+ False
+
+
+ IgnoreTableOptions
+ False
+
+
+ IgnoreIndexOptions
+ False
+
+
+ IgnoreDmlTriggerOrder
+ False
+
+
+ IgnoreDmlTriggerState
+ False
+
+
+ IgnoreAnsiNulls
+ True
+
+
+ IgnoreQuotedIdentifiers
+ True
+
+
+ IgnoreUserSettingsObjects
+ False
+
+
+ IgnoreFilegroupPlacement
+ True
+
+
+ IgnoreFullTextCatalogFilePath
+ True
+
+
+ IgnoreFileAndLogFilePath
+ True
+
+
+ IgnoreLoginSids
+ True
+
+
+ IgnoreNotForReplication
+ False
+
+
+ IgnoreFileSize
+ True
+
+
+ IgnoreSensitivityClassifications
+ False
+
+
+ AllowUnsafeRowLevelSecurityDataMovement
+ False
+
+
+ IncludeCompositeObjects
+ False
+
+
+ IncludeTransactionalScripts
+ False
+
+
+ IsAlwaysEncryptedParameterizationEnabled
+ False
+
+
+ NoAlterStatementsToChangeCLRTypes
+ False
+
+
+ PopulateFilesOnFileGroups
+ True
+
+
+ PreserveIdentityLastValues
+ False
+
+
+ RegisterDataTierApplication
+ False
+
+
+ PerformIndexOperationsOnline
+ False
+
+
+ RebuildIndexesOfflineForDataPhase
+ False
+
+
+ RestoreSequenceCurrentValue
+ True
+
+
+ ScriptDatabaseCollation
+ False
+
+
+ ScriptDatabaseCompatibility
+ False
+
+
+ ScriptDatabaseOptions
+ False
+
+
+ ScriptDeployStateChecks
+ False
+
+
+ ScriptFileSize
+ False
+
+
+ ScriptNewConstraintValidation
+ True
+
+
+ ScriptRefreshModule
+ True
+
+
+ TargetDatabaseName
+ FuchsDatabase
+
+
+ TargetConnectionString
+ Integrated Security=True;Pooling=False;Connect Timeout=30
+
+
+ TreatVerificationErrorsAsWarnings
+ False
+
+
+ UnmodifiableObjectWarnings
+ True
+
+
+ VerifyCollationCompatibility
+ True
+
+
+ VerifyDeployment
+ True
+
+
+ RunDeploymentPlanExecutors
+ False
+
+
+ AllowDropBlockingAssemblies
+ False
+
+
+ DoNotEvaluateSqlCmdVariables
+ True
+
+
+ DoNotDropAggregates
+ False
+
+
+ DoNotDropApplicationRoles
+ False
+
+
+ DoNotDropAssemblies
+ False
+
+
+ DoNotDropAsymmetricKeys
+ False
+
+
+ DoNotDropAudits
+ False
+
+
+ DoNotDropBrokerPriorities
+ False
+
+
+ DoNotDropCertificates
+ False
+
+
+ DoNotDropClrUserDefinedTypes
+ False
+
+
+ DoNotDropColumnEncryptionKeys
+ False
+
+
+ DoNotDropColumnMasterKeys
+ False
+
+
+ DoNotDropContracts
+ False
+
+
+ DoNotDropCredentials
+ False
+
+
+ DoNotDropDatabaseScopedCredentials
+ False
+
+
+ DoNotDropCryptographicProviders
+ False
+
+
+ DoNotDropDatabaseAuditSpecifications
+ False
+
+
+ DoNotDropDatabaseRoles
+ False
+
+
+ DoNotDropDatabaseTriggers
+ False
+
+
+ IgnoreDatabaseWorkloadGroups
+ False
+
+
+ DoNotDropDatabaseWorkloadGroups
+ False
+
+
+ IgnoreWorkloadClassifiers
+ False
+
+
+ DoNotDropWorkloadClassifiers
+ False
+
+
+ DoNotDropDefaults
+ False
+
+
+ DoNotDropEndpoints
+ False
+
+
+ DoNotDropErrorMessages
+ False
+
+
+ DoNotDropEventNotifications
+ False
+
+
+ DoNotDropEventSessions
+ False
+
+
+ DoNotDropExtendedProperties
+ False
+
+
+ DoNotDropExternalDataSources
+ False
+
+
+ DoNotDropExternalFileFormats
+ False
+
+
+ DoNotDropExternalLanguages
+ False
+
+
+ DoNotDropExternalLibraries
+ False
+
+
+ DoNotDropExternalModels
+ False
+
+
+ DoNotDropExternalStreamingJobs
+ False
+
+
+ DoNotDropExternalTables
+ False
+
+
+ DoNotDropExternalStreams
+ False
+
+
+ DoNotDropFilegroups
+ False
+
+
+ DoNotDropFiles
+ False
+
+
+ DoNotDropFileTables
+ False
+
+
+ DoNotDropFullTextCatalogs
+ False
+
+
+ DoNotDropFullTextStoplists
+ False
+
+
+ DoNotDropTableValuedFunctions
+ False
+
+
+ DoNotDropLinkedServerLogins
+ False
+
+
+ DoNotDropLinkedServers
+ False
+
+
+ DoNotDropLogins
+ False
+
+
+ DoNotDropMessageTypes
+ False
+
+
+ DoNotDropPartitionFunctions
+ False
+
+
+ DoNotDropPartitionSchemes
+ False
+
+
+ DoNotDropPermissions
+ False
+
+
+ DoNotDropQueues
+ False
+
+
+ DoNotDropRemoteServiceBindings
+ False
+
+
+ DoNotDropRoleMembership
+ False
+
+
+ DoNotDropRoutes
+ False
+
+
+ DoNotDropRules
+ False
+
+
+ DoNotDropScalarValuedFunctions
+ False
+
+
+ DoNotDropSearchPropertyLists
+ False
+
+
+ DoNotDropSecurityPolicies
+ False
+
+
+ DoNotDropSequences
+ False
+
+
+ DoNotDropServerAuditSpecifications
+ False
+
+
+ DoNotDropServerRoleMembership
+ False
+
+
+ DoNotDropServerRoles
+ False
+
+
+ DoNotDropServerTriggers
+ False
+
+
+ DoNotDropServices
+ False
+
+
+ DoNotDropSignatures
+ False
+
+
+ DoNotDropStoredProcedures
+ False
+
+
+ DoNotDropSymmetricKeys
+ False
+
+
+ DoNotDropSynonyms
+ False
+
+
+ DoNotDropTables
+ False
+
+
+ DoNotDropUserDefinedDataTypes
+ False
+
+
+ DoNotDropUserDefinedTableTypes
+ False
+
+
+ DoNotDropUsers
+ False
+
+
+ DoNotDropViews
+ False
+
+
+ DoNotDropXmlSchemaCollections
+ False
+
+
+ ExcludeAggregates
+ False
+
+
+ ExcludeApplicationRoles
+ False
+
+
+ ExcludeAssemblies
+ False
+
+
+ ExcludeAsymmetricKeys
+ False
+
+
+ ExcludeAudits
+ True
+
+
+ ExcludeBrokerPriorities
+ False
+
+
+ ExcludeCertificates
+ False
+
+
+ ExcludeClrUserDefinedTypes
+ False
+
+
+ ExcludeColumnEncryptionKeys
+ False
+
+
+ ExcludeColumnMasterKeys
+ False
+
+
+ ExcludeContracts
+ False
+
+
+ ExcludeCredentials
+ True
+
+
+ ExcludeDatabaseScopedCredentials
+ True
+
+
+ ExcludeCryptographicProviders
+ True
+
+
+ ExcludeDatabaseAuditSpecifications
+ True
+
+
+ ExcludeDatabaseRoles
+ False
+
+
+ ExcludeDatabaseTriggers
+ False
+
+
+ ExcludeDefaults
+ False
+
+
+ ExcludeEndpoints
+ True
+
+
+ ExcludeErrorMessages
+ True
+
+
+ ExcludeEventNotifications
+ False
+
+
+ ExcludeExternalDataSources
+ False
+
+
+ ExcludeExternalFileFormats
+ False
+
+
+ ExcludeExternalLanguages
+ False
+
+
+ ExcludeExternalLibraries
+ False
+
+
+ ExcludeExternalModels
+ False
+
+
+ ExcludeExternalStreamingJobs
+ False
+
+
+ ExcludeExternalTables
+ False
+
+
+ ExcludeExternalStreams
+ False
+
+
+ ExcludeEventSessions
+ True
+
+
+ ExcludeFilegroups
+ False
+
+
+ ExcludeFiles
+ True
+
+
+ ExcludeFileTables
+ False
+
+
+ ExcludeFullTextCatalogs
+ False
+
+
+ ExcludeFullTextStoplists
+ False
+
+
+ ExcludeTableValuedFunctions
+ False
+
+
+ ExcludeLinkedServerLogins
+ True
+
+
+ ExcludeLinkedServers
+ True
+
+
+ ExcludeLogins
+ True
+
+
+ ExcludeMessageTypes
+ False
+
+
+ ExcludePartitionFunctions
+ False
+
+
+ ExcludePartitionSchemes
+ False
+
+
+ ExcludeQueues
+ False
+
+
+ ExcludeRemoteServiceBindings
+ False
+
+
+ ExcludeRoutes
+ True
+
+
+ ExcludeRules
+ False
+
+
+ ExcludeScalarValuedFunctions
+ False
+
+
+ ExcludeSearchPropertyLists
+ False
+
+
+ ExcludeSecurityPolicies
+ False
+
+
+ ExcludeSequences
+ False
+
+
+ ExcludeServerAuditSpecifications
+ True
+
+
+ ExcludeServerRoleMembership
+ True
+
+
+ ExcludeServerRoles
+ True
+
+
+ ExcludeServerTriggers
+ True
+
+
+ ExcludeServices
+ False
+
+
+ ExcludeSignatures
+ False
+
+
+ ExcludeStoredProcedures
+ False
+
+
+ ExcludeSymmetricKeys
+ False
+
+
+ ExcludeSynonyms
+ False
+
+
+ ExcludeTables
+ False
+
+
+ ExcludeUserDefinedDataTypes
+ False
+
+
+ ExcludeUserDefinedTableTypes
+ False
+
+
+ ExcludeUsers
+ False
+
+
+ ExcludeViews
+ False
+
+
+ ExcludeXmlSchemaCollections
+ False
+
+
+ AllowExternalLibraryPaths
+ False
+
+
+ AllowExternalLanguagePaths
+ False
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlServerDdlTrigger
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlRoute
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlLinkedServerLogin
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlEndpoint
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlErrorMessage
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlFile
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlLogin
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlLinkedServer
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlCredential
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlDatabaseCredential
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlDatabaseEncryptionKey
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlMasterKey
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlDatabaseAuditSpecification
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlServerAudit
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlServerAuditSpecification
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlCryptographicProvider
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlUserDefinedServerRole
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlEventSession
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlDatabaseOptions
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlEventNotification
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlServerRoleMembership
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlDatabaseEventSession
+ ExcludedType
+
+
+ Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlAssemblyFile
+ ExcludedType
+
+
+
+
+ 2
+ 100
+ Equals_Objects,Not_Supported_Deploy
+
+
+
+ dbo
+ 2023-10-06 10-15 fds__invoice_details
+
+
+ dbo
+ 2023-10-06 10-15 fds__invoice_items
+
+
+ dbo
+ 2023-10-06 10-15 fds__invoice_servicerequests
+
+
+ dbo
+ 2023-10-12 1709 fds__bankingtransactions
+
+
+ dbo
+ 2023-10-12 1709 fds__bankingtransactions_assigns
+
+
+ dbo
+ 2023-10-12 1709 fds__bankingtransactions_settings
+
+
+ dbo
+ 2024-02-22_fds__invoices
+
+
+ dbo
+ 2024-03-14__fds__invoices
+
+
+ dbo
+ 2024-04-29__fds__invoices
+
+
+ dbo
+ 2024-05-15 fds__invoices
+
+
+ dbo
+ 2024-07-29 1718 fds__invoice_servicerequests
+
+
+ dbo
+ 2024-11-13 fds__invoice_items
+
+
+ dbo
+ 2024-11-13 fds__invoices
+
+
+ dbo
+ 2024-11-16 fds__invoice_items
+
+
+ dbo
+ 2024-11-16 fds__invoices
+
+
+ dbo
+ 2025-12-03 0928 fds__invoice_items
+
+
+ dbo
+ 2025-12-03 0928 fds__invoices
+
+
+ dbo
+ 2025-12-18_1429__fds__invoice_items
+
+
+ dbo
+ 2025-12-18_1429__fds__invoices
+
+
+ dbo
+ 2026-01-08_1032 fds__invoice_details
+
+
+ dbo
+ 2026-01-08_1032 fds__invoice_items
+
+
+ dbo
+ 2026-01-08_1032 fds__invoices
+
+
+ dbo
+ 2026-01-28_1600 fds__invoice_details
+
+
+ dbo
+ 2026-01-28_1600 fds__invoice_items
+
+
+ dbo
+ 2026-01-28_1600 fds__invoices
+
+
+ dbo
+ 2026-01-28_1832 fds__invoice_details
+
+
+ dbo
+ 2026-01-28_1832 fds__invoice_items
+
+
+ dbo
+ 2026-01-28_1832 fds__invoices
+
+
+ dbo
+ 2026-03-02_1943 fds__invoice_details
+
+
+ dbo
+ 2026-03-02_1943 fds__invoice_items
+
+
+ dbo
+ 2026-03-02_1943 fds__invoices
+
+
+ dbo
+ 2026-04-21_1058 fds__invoice_details
+
+
+ dbo
+ 2026-04-21_1058 fds__invoice_items
+
+
+ dbo
+ 2026-04-21_1058 fds__invoices
+
+
+ dbo
+ 2026-05-18_1019 fds__invoice_details
+
+
+ dbo
+ 2026-05-18_1019 fds__invoice_items
+
+
+ dbo
+ 2026-05-18_1019 fds__invoices
+
+
+ dbo
+ bs_05d0e2b7c9124ec7bbcd613915ccfb88
+
+
+ dbo
+ bs_31ca83d0030b4669b67a46728b4b439e
+
+
+ dbo
+ bs_5310baa29e8d40c99a1f991964b8f141
+
+
+ dbo
+ bs_6188db7e852744658933d3a04f23e849
+
+
+ dbo
+ bs_ea44799b83844adcb5e634cb845786e9
+
+
+ dbo
+ email_list_2020
+
+
+ dbo
+ email_list_2021
+
+
+ dbo
+ t_8btst_mfr__steps
+
+
+ dbo
+ t_tqrcd_mfr__reports
+
+
+ dbo
+ t_ves6u_mfr__servicerequests
+
+
+ dbo
+ t_wo9hi_mfr__reports
+
+
+ dbo
+ ocms_archive__items
+
+
+ dbo
+ ocms_archive__properties
+
+
+ dbo
+ ocms_changelog
+
+
+ dbo
+ ocms_debug
+
+
+ dbo
+ ocms_items
+
+
+ dbo
+ ocms_items
+ ocms_items__ix0
+
+
+ dbo
+ ocms_items
+ ocms_items__ix1
+
+
+ dbo
+ ocms_log__properties
+
+
+ dbo
+ ocms_navigation
+
+
+ dbo
+ ocms_navigation
+ ocms_navigation__ix1
+
+
+ dbo
+ ocms_properties
+
+
+ dbo
+ ocms_settings
+
+
+ dbo
+ ocms_template_properties
+
+
+ dbo
+ ocms_template_properties
+ ocms_template_properties__ix1
+
+
+ dbo
+ ocms_templates
+
+
+ dbo
+ ocms_templates
+ ocms_templates__ix1
+
+
+ dbo
+ ocms_translations
+
+
+ dbo
+ ocms_useraccounts
+
+
+ dbo
+ ocms_useraccounts
+ ocms_useraccounts__ix1
+
+
+ dbo
+ ocms_admin_createUserAccount
+
+
+ dbo
+ ocms_admin_debug_log
+
+
+ dbo
+ ocms_cleanups
+
+
+ dbo
+ ocms_cloneItem
+
+
+ dbo
+ ocms_createPropertyItem
+
+
+ dbo
+ ocms_createView
+
+
+ dbo
+ ocms_deleteItem
+
+
+ dbo
+ ocms_getAvailableTemplates
+
+
+ dbo
+ ocms_getFullTree
+
+
+ dbo
+ ocms_getItem
+
+
+ dbo
+ ocms_getItem_byName
+
+
+ dbo
+ ocms_getView
+
+
+ dbo
+ ocms_getView_byName
+
+
+ dbo
+ ocms_merge_navigation
+
+
+ dbo
+ ocms_merge_properties
+
+
+ dbo
+ ocms_recoverItem
+
+
+ dbo
+ ocms_setItemName
+
+
+ dbo
+ ocms_setItemOrder
+
+
+ dbo
+ ocms_setViewItem_HiddenStatus
+
+
+ dbo
+ ocms_admin_getConfig
+
+
+ dbo
+ ocms_admin_authenticate
+
+
+ dbo
+ ocms_admin_getUserAccount
+
+
+ dbo
+ ocms_getFullItemTree
+
+
+ dbo
+ ocms_getFullViewTree
+
+
+ dbo
+ ocms_locales
+
+
+ dbo
+ ocms_admin_checkUserExists
+
+
+ dbo
+ ocms_currentaccount
+
+
+ dbo
+ ocms_currentuser
+
+
+ dbo
+ ocms_fn_generatePassword
+
+
+ dbo
+ ocms_fn_link_csv
+
+
+ dbo
+ ocms_fn_randbetween
+
+
+ dbo
+ ocms_fn_useraccount_id
+
+
+ dbo
+ ocms_type_itemtree
+
+
+ dbo
+ ocms_type_navigation_base
+
+
+ dbo
+ ocms_type_properties_base
+
+
+ dbo
+ ocms_type_viewtree_full
+
+
+ ocms_rwe
+
+
+
+
+ ocms_rwe
+
+
+
+ ocms_enc_1
+
+
+ Grant.Control.SymmetricKey
+ ocms_rwe
+ dbo
+ ocms_enc_1
+
+
+
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/AddBusinessDays.sql b/Fuchs_Database/dbo/Functions/AddBusinessDays.sql
new file mode 100644
index 0000000..6250e5a
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/AddBusinessDays.sql
@@ -0,0 +1,9 @@
+CREATE FUNCTION[dbo].[AddBusinessDays](@Date DATE,@n INT)
+RETURNS DATE AS
+BEGIN
+DECLARE @d INT,@f INT,@DW INT;
+SET @f=CAST(abs(1^SIGN(DATEPART(DW, @Date)-(7-@@DATEFIRST))) AS BIT)
+SET @DW=DATEPART(DW,@Date)-(7-@@DATEFIRST)*(@f^1)+@@DATEFIRST*(@f&1)
+SET @d=4-SIGN(@n)*(4-@DW);
+RETURN DATEADD(D,@n+((ABS(@n)+(@d%(8+SIGN(@n)))-2)/5)*2*SIGN(@n)-@d/7,@Date);
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/backup__fds__fn_InvoiceIdByName.sql b/Fuchs_Database/dbo/Functions/backup__fds__fn_InvoiceIdByName.sql
new file mode 100644
index 0000000..c975352
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/backup__fds__fn_InvoiceIdByName.sql
@@ -0,0 +1,27 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[backup__fds__fn_InvoiceIdByName]
+(
+ @nme varchar(255)
+)
+RETURNS varchar(20)
+AS
+BEGIN
+ DECLARE @Id varchar(20);
+
+ SET @nme = TRIM((SELECT TOP(1) [value] FROM string_split(@nme, '(')));
+
+ SET @Id = ISNULL(
+ (SELECT TOP(1) [Id] FROM (
+ SELECT TOP(1) [Id], [fds] = 1 FROM [dbo].[fds__invoices] WHERE [InvoiceId] = @nme
+ UNION
+ SELECT TOP(1) try_Cast([Id] as varchar(20)), [fds] = 0 FROM [dbo].[mfr__invoices] WHERE [InvoiceId] = @nme
+ )z ORDER BY [fds] DESC
+ ),'');
+
+ RETURN @id;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/backup__fds__fn_bankingtransactions.sql b/Fuchs_Database/dbo/Functions/backup__fds__fn_bankingtransactions.sql
new file mode 100644
index 0000000..b7bf5d6
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/backup__fds__fn_bankingtransactions.sql
@@ -0,0 +1,83 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[backup__fds__fn_bankingtransactions]
+(
+ @startdate date = NULL
+ ,@enddate date = NULL
+ ,@invoice_startdate date = NULL
+ ,@invoice_enddate date = NULL
+ ,@invoice_idlist [dbo].[fds__tt__idlist_vchar] READONLY
+)
+RETURNS @bankingtransactions TABLE
+(
+ [uid] bigint
+ ,[AccountIdentification] varchar(50)
+ ,[ValueDate] date
+ ,[Amount] numeric(9,2)
+ ,[AccountNumberOfPayer] varchar(30)
+ ,[NameOfPayer] nvarchar(60)
+ ,[SepaRemittanceInformation] varchar(150)
+ ,[EndToEndReference] varchar(50)
+ ,[manu] bit
+ ,[InvId] varchar(15)
+ ,[done_manually] bit
+ ,[fds] bit
+)
+AS
+BEGIN
+ -- Fill the table variable with the rows for your result set
+ DECLARE @firstever date = '1900-01-01', @tomorrow date = DATEADD(dAy, 1, GETDATE());
+ SELECT @invoice_startdate = ISNULL(@invoice_startdate, @firstever), @invoice_enddate = ISNULL(@invoice_enddate, @tomorrow);
+ DECLARE @idlistempty bit = IIF(EXISTS (SELECT 0 FROM @invoice_idlist) ,0,1);
+
+ with bt as (
+ SELECT [uid],[AccountIdentification],[ValueDate],[Amount],AccountNumberOfPayer,NameOfPayer, SepaRemittanceInformation, EndToEndReference
+ FROM [dbo].[fds__bankingtransactions] as bt_
+ where (@startdate is null OR bt_.ValueDate >= @startdate)
+ AND (@enddate is null or bt_.ValueDate <= @enddate)
+ ), bs0manu as (
+ SELECT bs.[banking_uid], [invid] = spl.[value], [manu] = cast(1 as bit)
+ FROM [dbo].[fds__bankingtransactions_settings] as bs
+ CROSS APPLY STRING_SPLIT(bs.assigned_invoice_id, ',') as spl
+ JOIN bt on bt.[uid] = bs.[banking_uid]
+ WHERE bs.assigned_invoice_id is not null
+ ), bs1 as (
+ SELECT * FROM bs0manu
+ UNION
+ SELECT bs.[banking_uid], [invid] = spl.[value], [manu] = cast(0 as bit)
+ FROM [dbo].[fds__bankingtransactions_settings] as bs
+ CROSS APPLY STRING_SPLIT(bs.auto_invoice_id, ',') as spl
+ JOIN bt on bt.[uid] = bs.[banking_uid]
+ WHERE bs.auto_invoice_id is not null
+ and bt.[uid] NOT IN (SELECT [banking_uid] FROM bs0manu)
+ ), bs as (
+ SELECT banking_uid, [invid], manu = CAST(MAX(CAST([manu] as int)) as bit)
+ from bs1
+ WHERE @idlistempty = 1 OR bs1.[invid] in (SELECT [id] FROM @invoice_idlist)
+ GROUP BY banking_uid, [invid]
+ )
+ INSERT INTO @bankingtransactions
+ SELECT
+ bt.[uid]
+ ,[AccountIdentification]
+ , bt.[ValueDate]
+ , [Amount]
+ ,bt.AccountNumberOfPayer
+ ,bt.NameOfPayer
+ ,bt.SepaRemittanceInformation
+ ,bt.EndToEndReference
+ ,[manu]
+ ,[invid]
+ ,[done_manually] = IIF(ISNULL(bs2.[done_manually],'') <> '', 1,0)
+ ,[fds] = IIF(mi.id is null, 1, 0)
+ FROM bt
+ JOIN bs ON bt.[uid] = bs.[banking_uid]
+ LEFT JOIN [dbo].[fds__bankingtransactions_settings] as bs2 ON bt.[uid] = bs2.[banking_uid]
+ LEFT JOIN [dbo].[fds__invoices] as fi on bs.[invid] = fi.[id] AND fi.[DateFinalized] BETWEEN @invoice_startdate AND @invoice_enddate
+ LEFT JOIN [dbo].[mfr__invoices] as mi on try_cast(bs.[invid] as bigint) = mi.[id] AND mi.[DateOfCreation] BETWEEN @invoice_startdate AND @invoice_enddate;
+
+ RETURN
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/backup__fds__fn_invoice_customerid.sql b/Fuchs_Database/dbo/Functions/backup__fds__fn_invoice_customerid.sql
new file mode 100644
index 0000000..2ab19dc
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/backup__fds__fn_invoice_customerid.sql
@@ -0,0 +1,26 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[backup__fds__fn_invoice_customerid]
+(
+ @InvId varchar(15)
+)
+RETURNS bigint
+AS
+BEGIN
+ DECLARE @ret bigint;
+
+ SELECT TOP(1) @ret = [customerid] FROM dbo.[fds__invoices] WHERE [id] = @InvId;
+
+ IF @ret is null
+ SELECT TOP(1) @ret = [customerid] FROM (SELECT TOP(1) [invid] = p.[PartnerId], s.[CustomerId], cy.[supportmail], cy.[Name]
+ FROM dbo.[mfr__*PartnerSet] as p
+ JOIN [dbo].[mfr__servicerequests] as s on p.[EntityId] = s.[Id] AND p.[Property] = 'ServiceRequest:Invoices'
+ JOIN [dbo].[mfr__companies] as cy on s.[CustomerId] = cy.id
+ WHERE p.PartnerId = TRY_CAST(@invId as bigint))z;
+
+ RETURN @ret;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/backup__fds__fn_unpaidInvoices.sql b/Fuchs_Database/dbo/Functions/backup__fds__fn_unpaidInvoices.sql
new file mode 100644
index 0000000..0c5739f
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/backup__fds__fn_unpaidInvoices.sql
@@ -0,0 +1,48 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[backup__fds__fn_unpaidInvoices]
+(
+
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ WITH inv as (
+ SELECT [Id] = cast([id] as varchar(15))
+ ,[Invoiceid]
+ ,[DateFinalized]
+ ,[InvoiceBalance]
+ ,[DocumentName]
+ ,[file]
+ ,[InvoiceTitle]
+ ,[customerid]
+ ,[dateSent]
+ ,[fds] = CAST(1 as bit)
+ FROM [dbo].[fds__invoices] as iv
+ WHERE [isfinal] = 1 and CAST(CASE WHEN ISNULL(iv.[IsCanceled],0) = 1 THEN 1 ELSE ISNULL([IsPayed],0) END as bit) = 0
+ and InvoiceBalance > 0
+ UNION
+ SELECT [Id] = cast([id] as varchar(15))
+ ,[Invoiceid]
+ ,[DateFinalized] = [DateOfCreation]
+ ,[InvoiceBalance]
+ ,[DocumentName]
+ ,[file] = NULL
+ ,[InvoiceTitle] = ''
+ ,[customerid] = [dbo].[fds__fn_invoice_customerid](mfri.[id])
+ ,[datesent] = NULL
+ ,[fds] = CAST(0 as bit)
+ FROM [dbo].[mfr__invoices] as mfri
+ LEFT JOIN [dbo].[fds__custom_invoiceinfo] as ivi on mfri.[id] = ivi.[invid]
+ WHERE [invoicestate] in ('eIsSent','eIsOpen')
+ AND ISNULL(ivi.isPayed,0) = 0
+ AND not [invoiceId] like 'preview'
+ and InvoiceBalance > 0
+ )
+ SELECT * /*[Rechnung] = [invoiceId], [Name] = [InvoiceTitle] + CHAR(10) + ' (' + (SELECT TOP(1) c.[name] FROM [dbo].[mfr__companies] as c WHERE inv.[customerid] = c.[id]) + ')', [Betrag (net)] = FORMAT([invoicebalance], '#0.00€', 'de'), [Datum] = FORMAT([DateFinalized], 'dd.MM.yy' , 'de'), [Zahlungen] = FORMAT([dbo].[fds__fn_InvoicePaymentAmount]([id]), '#0.00€', 'de'), [order] = ROW_NUMBER() OVER (ORDER BY [DateFinalized], [DateSent]) */
+ FROM inv
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/backup__fds__getInvoiceTreeIds.sql b/Fuchs_Database/dbo/Functions/backup__fds__getInvoiceTreeIds.sql
new file mode 100644
index 0000000..3bce719
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/backup__fds__getInvoiceTreeIds.sql
@@ -0,0 +1,33 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[backup__fds__getInvoiceTreeIds]
+(
+ @invid varchar(20)
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ with mfr_inv as (
+ SELECT [type] = 'invoice', [id] = [id] FROM [dbo].[mfr__invoices] as _mfri where _mfri.[id] = TRY_CAST(@invid as bigint)
+ ), mfr_srq as (
+ SELECT DISTINCT [type] = 'servicerequest', [id] = mfrs.[id] FROM mfr_inv
+ JOIN [dbo].[mfr__*PartnerSet] as ps on Property = 'Invoice:SourceServiceRequest' and ps.EntityId = mfr_inv.[id]
+ JOIN [dbo].[mfr__servicerequests] as mfrs on ps.[PartnerId] = mfrs.id
+ ), fds_srq as (
+ SELECT distinct [type] = 'servicerequest', [id] = mfrs.[id]
+ FROM [dbo].[fds__invoices] as _fi
+ JOIN [dbo].[fds__invoice_servicerequests] as _fs on _fi.Id = _fs.[InvId] and _fs.[mfr__servicerequest] is not null
+ JOIN [dbo].[mfr__servicerequests] as mfrs on _fs.[mfr__servicerequest] = mfrs.id
+ WHERE _fi.[Id] = @invid
+ )
+ SELECT * FROM mfr_inv
+ union
+ SELECT * FROM mfr_srq
+ union
+ SELECT * FROM fds_srq
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/backup__fds__r_getBalanceThisMonth.sql b/Fuchs_Database/dbo/Functions/backup__fds__r_getBalanceThisMonth.sql
new file mode 100644
index 0000000..82ec521
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/backup__fds__r_getBalanceThisMonth.sql
@@ -0,0 +1,42 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[backup__fds__r_getBalanceThisMonth](
+ @authuser varchar(100)
+)
+RETURNS varchar(25)
+BEGIN
+ DECLARE @today date = GETDATE();
+ DECLARE @tomorrow date = DATEADD(DAY,1,@today);
+ DECLARE @thismonthfirst date = [dbo].[date_monthfirst](@today);
+ DECLARE @ret varchar(25) = ''
+
+ IF [dbo].[fis_getModuleAuth]('fds_reports', @authuser) < 2
+ SET @ret = '';
+ ELSE
+ BEGIN
+
+ WITH inv as (
+ SELECT
+ i.[Dateofcreation]
+ ,i.[InvoiceBalance]
+ ,i.[InvoiceBalanceNetto]
+ FROM [dbo].[mfr__invoices] as i
+ where --i.[invoicestate] in('eIsSent','eIsPaid','eIsOpen') and
+ i.[FileType] in( 'PdfInvoice','PdfCancelInvoice','PdfPartialInvoice') and ISNULL(InvoiceId,'') not in ('Preview','')
+ AND [dbo].[date_monthfirst](i.[Dateofcreation]) = @thismonthfirst
+ UNION
+ SELECT
+ [Dateofcreation] = i.[DateFinalized]
+ ,i.[InvoiceBalance]
+ ,[InvoiceBalanceNetto] = i.[InvoiceBalance_net]
+ FROM [dbo].[fds__invoices] as i
+ WHERE i.[isFinal] = 1 AND [dbo].[date_monthfirst](i.[DateFinalized]) = @thismonthfirst
+ )
+ SELECT TOP(1) @ret = FORMAT( SUM( ISNULL(inv.[InvoiceBalanceNetto],0) ) * 0.001, '#,0 k€', 'de') FROM inv;
+ END
+
+ RETURN @ret;
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/bo_val.sql b/Fuchs_Database/dbo/Functions/bo_val.sql
new file mode 100644
index 0000000..68b9f4d
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/bo_val.sql
@@ -0,0 +1,22 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[bo_val]
+(
+ @price numeric(10,3)
+ ,@quantityhours numeric(10,3)
+ ,@discount numeric(10,3)
+ ,@VAT numeric(10,3)
+)
+RETURNS numeric(10,2)
+AS
+BEGIN
+ DECLARE @Ret numeric(10,2)
+
+ SET @RET = [dbo].[net_val](@price, @quantityhours, @discount) * (1 + (ISNULL(@vat, 19.0) * 0.01));
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/date_add.sql b/Fuchs_Database/dbo/Functions/date_add.sql
new file mode 100644
index 0000000..8faa2b1
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/date_add.sql
@@ -0,0 +1,58 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[date_add]
+(
+ @date date
+ ,@addition varchar(10)
+ ,@shift_to_first bit = 1
+)
+RETURNS date
+AS
+BEGIN
+ DECLARE @Ret date = @date;
+ SET @addition = LOWER(ISNULL(@addition,''));
+ DECLARE @negative bit = 0;
+ IF LEFT(@addition,1) = '-'
+ BEGIN
+ SET @negative = 1;
+ SET @addition = REPLACE(@addition, '-', '');
+ END
+
+ IF @addition like '[1-9]%[dwmqy]'
+ BEGIN
+ DECLARE @num int = TRY_CONVERT(int, LEFT(@addition, patindex('%[^0-9]%', @addition) - 1)) * IIF(@negative = 1, -1, 1), @unit varchar(2) = LOWER(RIGHT(@addition, LEN(@addition) - patindex('%[^0-9]%', @addition) + 1)) ;
+ if @num is not null
+ BEGIN
+
+ SET @ret = CASE WHEN ISNULL(@shift_to_first,1) = 1 THEN
+ CASE
+ WHEN @unit = 'y' THEN DATEADD(YEAR, @num - 1, dbo.date_nextFirst(@date, 'y'))
+ WHEN @unit = 'q' THEN DATEADD(QUARTER, @num - 1, dbo.date_nextFirst(@date, 'q'))
+ WHEN @unit = 'm' THEN DATEADD(Month, @num - 1, dbo.date_nextFirst(@date, 'm'))
+ WHEN @unit = 'w' THEN DATEADD(WEEK, @num - 1, dbo.date_nextFirst(@date, 'w'))
+ WHEN @unit = 'd' THEN DATEADD(DAY, @num - 1, dbo.date_nextFirst(@date, 'd'))
+ WHEN @unit in ('bd','wd') THEN [dbo].[AddBusinessDays](@date, @num)
+ ELSE @date
+ END
+ ELSE
+ CASE
+ WHEN @unit = 'y' THEN DATEADD(YEAR, @num, @date)
+ WHEN @unit = 'q' THEN DATEADD(QUARTER, @num, @date)
+ WHEN @unit = 'm' THEN DATEADD(Month, @num, @date)
+ WHEN @unit = 'w' THEN DATEADD(WEEK, @num, @date)
+ WHEN @unit = 'd' THEN DATEADD(DAY, @num, @date)
+ WHEN @unit in ('bd','wd') THEN [dbo].[AddBusinessDays](@date, @num)
+ ELSE @date
+ END
+ END;
+
+ END
+
+ END
+
+ RETURN @ret;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/date_addcustom.sql b/Fuchs_Database/dbo/Functions/date_addcustom.sql
new file mode 100644
index 0000000..c99e92f
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/date_addcustom.sql
@@ -0,0 +1,27 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[date_addcustom]
+(
+ @date date
+ ,@phrase varchar(10)
+)
+RETURNS date
+AS
+BEGIN
+ DECLARE @returndate date, @numpart integer = TRY_PARSE(left(@phrase, patindex('%[^0-9]%', @phrase + '.') - 1) as integer), @defpart varchar(10) = LOWER(RIGHT(@phrase, LEN(@phrase) - patindex('%[^0-9]%', @phrase + '.') + 1));
+
+ SET @returndate = CASE WHEN @numpart is null then null
+ WHEN @defpart = 'wd' THEN [dbo].[AddBusinessDays](@date, @numpart)
+ WHEN @defpart = 'd' THEN DATEADD(DAY, @numpart, @date)
+ WHEN @defpart = 'y' THEN DATEADD(YEAR, @numpart, @date)
+ WHEN @defpart = 'm' THEN DATEADD(Month, @numpart, @date)
+ WHEN @defpart = 'wk' THEN DATEADD(WEEK, @numpart, @date)
+ ELSE @date
+ END;
+
+ RETURN @returndate;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/date_monthend.sql b/Fuchs_Database/dbo/Functions/date_monthend.sql
new file mode 100644
index 0000000..7ee60c6
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/date_monthend.sql
@@ -0,0 +1,18 @@
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[date_monthend]
+(
+ @date date
+)
+RETURNS date
+AS
+BEGIN
+ DECLARE @monthend date = CASE WHEN @date is null THEN NULL ELSE DATEADD(DAY, -1, DATEADD(MONTH, 1, DATEFROMPARTS(YEAR(@date), MONTH(@date),1))) END;
+
+ return @monthend;
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/date_monthfirst.sql b/Fuchs_Database/dbo/Functions/date_monthfirst.sql
new file mode 100644
index 0000000..be8e384
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/date_monthfirst.sql
@@ -0,0 +1,18 @@
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[date_monthfirst]
+(
+ @date date
+)
+RETURNS date
+AS
+BEGIN
+ DECLARE @monthfirst date = CASE WHEN @date is null THEN NULL ELSE DATEFROMPARTS(YEAR(@date), MONTH(@date), 1) END;
+
+ return @monthfirst;
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/date_nextFirst.sql b/Fuchs_Database/dbo/Functions/date_nextFirst.sql
new file mode 100644
index 0000000..7dbd282
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/date_nextFirst.sql
@@ -0,0 +1,29 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[date_nextFirst]
+(
+ @date date
+ ,@unit varchar(1)
+)
+RETURNS date
+AS
+BEGIN
+ DECLARE @ret date;
+ SET @unit = LOWER(ISNULL(@unit, ''));
+
+ SET @ret = CASE
+ WHEN @unit = 'y' THEN DATEADD(YEAR, 1, DATEFROMPARTS(YEAR(@date), 1,1))
+ WHEN @unit = 'q' THEN DATEADD(MONTH, 4 - Month(@date) % 3, DATEFROMPARTS(YEAR(@date),MONTH(@date),1))
+ WHEN @unit = 'm' THEN DATEADD(MONTH, 1, DATEFROMPARTS(YEAR(@date), MONTH(@date),1))
+ WHEN @unit = 'w' THEN DATEADD(WEEK, 1, [dbo].[date_weekfirst](@date))
+ WHEN @unit = 'd' THEN DATEADD(DAY, 1, @date)
+ ELSE NULL
+ END;
+
+ RETURN @ret
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/date_weekend.sql b/Fuchs_Database/dbo/Functions/date_weekend.sql
new file mode 100644
index 0000000..d4e662f
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/date_weekend.sql
@@ -0,0 +1,17 @@
+-- =============================================
+-- Author: Dr. Stefan Ott
+-- Create date: 31.01.2013
+-- Description: first day of week
+-- =============================================
+CREATE FUNCTION [dbo].[date_weekend]
+(
+ @TargetDate date
+)
+RETURNS date
+AS
+BEGIN
+
+ DECLARE @Corretor smallint = -((@@DATEFIRST % 7) - 1);
+ DECLARE @ret date = CASE WHEN @TargetDate is null THEN null ELSE dateadd(d ,-((datepart(weekday,@TargetDate) - 1 - @Corretor)%7), @TargetDate) END;
+ return DATEADD(DAY, 6, @ret);
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/date_weekfirst.sql b/Fuchs_Database/dbo/Functions/date_weekfirst.sql
new file mode 100644
index 0000000..41bd5dc
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/date_weekfirst.sql
@@ -0,0 +1,19 @@
+
+
+-- =============================================
+-- Author: Dr. Stefan Ott
+-- Create date: 31.01.2013
+-- Description: first day of week
+-- =============================================
+CREATE FUNCTION [dbo].[date_weekfirst]
+(
+ @TargetDate date
+)
+RETURNS date
+AS
+BEGIN
+
+ DECLARE @Corretor smallint = -((@@DATEFIRST % 7) - 1);
+ DECLARE @ret date = CASE WHEN @TargetDate is null THEN null ELSE dateadd(d ,-((datepart(weekday,@TargetDate) - 1 - @Corretor)%7), @TargetDate) END;
+ return @ret;
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__admin_reminderSettings.sql b/Fuchs_Database/dbo/Functions/fds__admin_reminderSettings.sql
new file mode 100644
index 0000000..8ef7049
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__admin_reminderSettings.sql
@@ -0,0 +1,18 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__admin_reminderSettings]
+(
+
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ WITH rs as ( SELECT * FROM [dbo].[fds__admin_settings] WHERE [type] = 'reminder' )
+ SELECT [stage1] = (SELECT TOP(1) [value] FROM rs WHERE [key] = 1)
+ , [stage2] = (SELECT TOP(1) [value] FROM rs WHERE [key] = 2)
+ , [stage3] = (SELECT TOP(1) [value] FROM rs WHERE [key] = 3)
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_DocumentName.sql b/Fuchs_Database/dbo/Functions/fds__fn_DocumentName.sql
new file mode 100644
index 0000000..a589098
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_DocumentName.sql
@@ -0,0 +1,28 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_DocumentName]
+(
+ @InvoiceId varchar(10)
+ , @InvoiceType char(1)
+ , @InvoiceTitle varchar(100)
+ , @DateFinalized datetime
+)
+RETURNS varchar(50)
+AS
+BEGIN
+ DECLARE @doc varchar(50);
+
+ SET @doc = CASE WHEN @DateFinalized is null THEN ''
+ WHEN ISNULL(@InvoiceTitle,'') <> '' THEN @InvoiceTitle + ' ' + @InvoiceId
+ WHEN @InvoiceType = 'a' THEN 'Abschlagsrechnung ' + @InvoiceId
+ WHEN @InvoiceType = 'c' THEN 'Stornorechnung ' + @InvoiceId
+ WHEN @InvoiceType = 'f' THEN 'Schlussrechnung ' + @InvoiceId
+ ELSE 'Rechnung ' + @InvoiceId
+ END;
+
+ RETURN @doc + '.pdf';
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_IntermediateIsAllocatedToOther.sql b/Fuchs_Database/dbo/Functions/fds__fn_IntermediateIsAllocatedToOther.sql
new file mode 100644
index 0000000..df58125
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_IntermediateIsAllocatedToOther.sql
@@ -0,0 +1,22 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_IntermediateIsAllocatedToOther]
+(
+ @Intermediate_InvID varchar(10)
+ ,@Target_InvID varchar(10)
+
+)
+RETURNS bit
+AS
+BEGIN
+ DECLARE @IsA bit = CASE WHEN EXISTS (SELECT * FROM [dbo].[fds__invoices] as intermediates JOIN [dbo].[fds__invoice_details] as id on intermediates.[Id] = id.[InvId] and id.[AllocatedTo_InvId] is not null
+ JOIN [dbo].[fds__invoices] as non_intermediates ON id.[AllocatedTo_InvId] = non_intermediates.[Id] and id.[AllocatedTo_InvId] is not null
+ WHERE intermediates.[Id] = @Intermediate_InvID and (@Target_InvID is null or non_intermediates.[Id] <> @Target_InvID))
+ THEN 1 ELSE 0 END;
+
+ RETURN @isA;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_InvoiceIDs_mfr.sql b/Fuchs_Database/dbo/Functions/fds__fn_InvoiceIDs_mfr.sql
new file mode 100644
index 0000000..7ca2751
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_InvoiceIDs_mfr.sql
@@ -0,0 +1,30 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_InvoiceIDs_mfr]
+(
+ @invoiceid varchar(255),
+ @other varchar(1000)
+)
+RETURNS varchar(1000)
+AS
+BEGIN
+ DECLARE @RET varchar(1000) = REPLACE(LTRIM(ISNULL(@invoiceid,'')),'Preview','');
+
+ IF @RET <> ''
+ SET @RET = TRIM(REPLACE(@RET, ' ', ' '));
+
+ IF @RET <> ''
+ SET @RET = REPLACE(@RET, ' ',' (mfr)' + CHAR(10)) + ' (mfr)';
+
+ IF @RET <> '' AND ISNULL(@other, '') <> ''
+ SET @RET = @RET + CHAR(10);
+
+ SET @RET = @RET + ISNULL(@other, '');
+
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_InvoiceIdByName.sql b/Fuchs_Database/dbo/Functions/fds__fn_InvoiceIdByName.sql
new file mode 100644
index 0000000..eda1bcf
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_InvoiceIdByName.sql
@@ -0,0 +1,21 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_InvoiceIdByName]
+(
+ @nme varchar(255)
+)
+RETURNS varchar(20)
+AS
+BEGIN
+ DECLARE @Id varchar(20);
+
+ SET @nme = TRIM((SELECT TOP(1) [value] FROM string_split(@nme, '(')));
+
+ SET @Id = ISNULL((SELECT TOP(1) [Id] FROM [dbo].[fds__invoices] WHERE [InvoiceId] = @nme),'');
+
+ RETURN @id;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_InvoicePaymentAmount.sql b/Fuchs_Database/dbo/Functions/fds__fn_InvoicePaymentAmount.sql
new file mode 100644
index 0000000..4e66286
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_InvoicePaymentAmount.sql
@@ -0,0 +1,30 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_InvoicePaymentAmount]
+(
+ @InvID varchar(15)
+)
+RETURNS numeric(9,2)
+AS
+BEGIN
+ DECLARE @RES numeric(9,2);
+ DECLARE @invoice_idlist [dbo].[fds__tt__idlist_vchar];
+ INSERT INTO @invoice_idlist VALUES(@invid);
+
+ With bs as (
+ SELECT * FROM [dbo].[fds__fn_bankingtransactions](null,null, null, null,@invoice_idlist)
+ ), b as (
+ SELECT
+ [InvID] = @InvID
+ ,[amount] = SUM(ISNULL(bs.[amount], 0.0))
+ FROM bs
+ WHERE bs.[invid] = @InvID
+ )
+ SELECT TOP(1) @RES = [amount] FROM b;
+
+ RETURN ISNULL(@RES, 0.0);
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_InvoicePaymentAmount_full.sql b/Fuchs_Database/dbo/Functions/fds__fn_InvoicePaymentAmount_full.sql
new file mode 100644
index 0000000..76e12ed
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_InvoicePaymentAmount_full.sql
@@ -0,0 +1,37 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_InvoicePaymentAmount_full]
+(
+ @InvID varchar(15)
+)
+RETURNS numeric(9,2)
+AS
+BEGIN
+ DECLARE @RES numeric(9,2);
+ DECLARE @invoice_idlist [dbo].[fds__tt__idlist_vchar];
+ INSERT INTO @invoice_idlist VALUES(@invid);
+
+ With bs as (
+ SELECT * FROM [dbo].[fds__fn_bankingtransactions](null,null, null, null,@invoice_idlist)
+ ), ci as(
+ SELECT [InvId] = d.[StornoTo_InvId], [amount] = ISNULL(i.[InvoiceBalance],0.0) * -1 FROM [dbo].[fds__invoices] as i JOIN [dbo].[fds__invoice_details] as d on i.[Id] = d.[InvId] where d.[StornoTo_InvId] = @InvID AND i.[IsFinal] = 1
+ ), tp as (
+ SELECT [InvId], [amount] from bs where [invid] = @InvID
+ union
+ SELECT [InvId], [amount] from ci
+ ), b as (
+ SELECT
+ [InvID] = @InvID
+ ,[amount] = SUM(ISNULL(tp.[amount], 0.0))
+ FROM tp
+ WHERE tp.[invid] = @InvID
+ )
+ SELECT TOP(1) @RES = [amount] FROM b;
+
+ RETURN ISNULL(@RES, 0.0);
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_ReminderDocumentName.sql b/Fuchs_Database/dbo/Functions/fds__fn_ReminderDocumentName.sql
new file mode 100644
index 0000000..debd677
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_ReminderDocumentName.sql
@@ -0,0 +1,30 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_ReminderDocumentName]
+(
+ @ReminderType char(1)
+ , @ReminderTitle varchar(100)
+ , @DateFinalized datetime
+ , @InvoiceId varchar(10)
+
+)
+RETURNS varchar(50)
+AS
+BEGIN
+ DECLARE @doc varchar(50);
+
+ SET @doc = CASE WHEN @DateFinalized is null THEN ''
+ WHEN ISNULL(@ReminderTitle,'') <> '' THEN @ReminderTitle
+ WHEN @ReminderType = 'f' THEN 'Zahlungserinnerung ' + @InvoiceId
+ WHEN @ReminderType = 'c' THEN 'Mahnung ' + @InvoiceId
+ WHEN @ReminderType = 'f' THEN 'Mahnung ' + @InvoiceId
+ ELSE 'Zahlungserinnerung ' + @InvoiceId
+ END;
+
+ RETURN @doc + '.pdf';
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_bankingtransaction_id.sql b/Fuchs_Database/dbo/Functions/fds__fn_bankingtransaction_id.sql
new file mode 100644
index 0000000..f96dd97
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_bankingtransaction_id.sql
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+
+
+CREATE FUNCTION [dbo].[fds__fn_bankingtransaction_id] (
+)
+RETURNS varchar(10)
+AS
+BEGIN
+ DECLARE @NewAccount varchar(7) = [dbo].[ocms_fn_generatePassword] (8,0,1,0);
+
+ WHILE EXISTS( SELECT * FROM [dbo].[fds__bankingtransactions] WITH (SERIALIZABLE) WHERE [taID] = @NewAccount)
+ BEGIN
+ SET @NewAccount = [dbo].[ocms_fn_generatePassword] (8,0,1,0);
+ END
+
+ RETURN @NewAccount;
+END;
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_bankingtransactions.sql b/Fuchs_Database/dbo/Functions/fds__fn_bankingtransactions.sql
new file mode 100644
index 0000000..d16ffdb
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_bankingtransactions.sql
@@ -0,0 +1,88 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_bankingtransactions]
+(
+ @startdate date = NULL
+ ,@enddate date = NULL
+ ,@invoice_startdate date = NULL
+ ,@invoice_enddate date = NULL
+ ,@invoice_idlist [dbo].[fds__tt__idlist_vchar] READONLY
+)
+RETURNS @bankingtransactions TABLE
+(
+ [taID] varchar(10)
+ ,[AccountIdentification] varchar(50)
+ ,[ValueDate] date
+ ,[Amount] numeric(9,2)
+ ,[AccountNumberOfPayer] varchar(30)
+ ,[NameOfPayer] nvarchar(60)
+ ,[SepaRemittanceInformation] varchar(150)
+ ,[EndToEndReference] varchar(50)
+ ,[manu] bit
+ ,[InvId] varchar(15)
+ ,[done_manually] bit
+ ,[fds] bit
+)
+AS
+BEGIN
+ -- Fill the table variable with the rows for your result set
+ DECLARE @firstever date = '1900-01-01', @tomorrow date = DATEADD(dAy, 1, GETDATE());
+ SELECT @invoice_startdate = ISNULL(@invoice_startdate, @firstever), @invoice_enddate = ISNULL(@invoice_enddate, @tomorrow);
+ DECLARE @idlistempty bit = IIF(EXISTS (SELECT 0 FROM @invoice_idlist) ,0,1);
+
+ with bt as (
+ SELECT [taID],[AccountIdentification],[ValueDate],[Amount],AccountNumberOfPayer,NameOfPayer, SepaRemittanceInformation, EndToEndReference
+ FROM [dbo].[fds__bankingtransactions] as bt_
+ where (@startdate is null OR bt_.ValueDate >= @startdate)
+ AND (@enddate is null or bt_.ValueDate <= @enddate)
+ AND (bt_.[DebitCreditMark] in ('C')
+ AND NOT (ISNULL(bt_.[AccountNumberOfPayer],'') IN ('DE52301502000002091478') AND ISNULL(bt_.[SepaRemittanceInformation],'') like ('%umbuchung%'))
+ AND NOT (ISNULL(bt_.[AccountNumberOfPayer],'') IN ('DE23501108006161606386') AND ISNULL(bt_.[SepaRemittanceInformation],'') like ('%ebay%'))
+ AND NOT (ISNULL(bt_.[SepaRemittanceInformation],'') like ('%Umsatzsteuer%'))
+ AND ISNULL(bt_.AccountNumberOfPayer,'') <> ''
+ )
+ ), bs0manu as (
+ SELECT bs.[taID], [invid] = spl.[value], [manu] = cast(1 as bit)
+ FROM [dbo].[fds__bankingtransactions_settings] as bs
+ CROSS APPLY STRING_SPLIT(bs.assigned_invoice_id, ',') as spl
+ JOIN bt on bt.[taID] = bs.[taID]
+ WHERE bs.assigned_invoice_id is not null
+ ), bs1 as (
+ SELECT * FROM bs0manu
+ UNION
+ SELECT bs.[taID], [invid] = spl.[value], [manu] = cast(0 as bit)
+ FROM [dbo].[fds__bankingtransactions_settings] as bs
+ CROSS APPLY STRING_SPLIT(bs.auto_invoice_id, ',') as spl
+ JOIN bt on bt.[taID] = bs.[taID]
+ WHERE bs.auto_invoice_id is not null
+ and bt.[taID] NOT IN (SELECT [taID] FROM bs0manu)
+ ), bs as (
+ SELECT [taID], [invid], manu = CAST(MAX(CAST([manu] as int)) as bit)
+ from bs1
+ WHERE @idlistempty = 1 OR bs1.[invid] in (SELECT [id] FROM @invoice_idlist)
+ GROUP BY [taID], [invid]
+ )
+ INSERT INTO @bankingtransactions
+ SELECT
+ bt.[taID]
+ ,[AccountIdentification]
+ , bt.[ValueDate]
+ , [Amount]
+ ,bt.AccountNumberOfPayer
+ ,bt.NameOfPayer
+ ,bt.SepaRemittanceInformation
+ ,bt.EndToEndReference
+ ,[manu]
+ ,[invid]
+ ,[done_manually] = IIF(ISNULL(bs2.[done_manually],'') <> '', 1,0)
+ ,[fds] = IIF(fi.isExternal = 0, 1, 0)
+ FROM bt
+ JOIN bs ON bt.[taID] = bs.[taID]
+ LEFT JOIN [dbo].[fds__bankingtransactions_settings] as bs2 ON bt.[taID] = bs2.[taID]
+ LEFT JOIN [dbo].[fds__invoices] as fi on bs.[invid] = fi.[id] AND fi.[DateFinalized] BETWEEN @invoice_startdate AND @invoice_enddate
+
+ RETURN
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_bankingtransactions_perInvoice.sql b/Fuchs_Database/dbo/Functions/fds__fn_bankingtransactions_perInvoice.sql
new file mode 100644
index 0000000..30f2919
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_bankingtransactions_perInvoice.sql
@@ -0,0 +1,35 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_bankingtransactions_perInvoice]
+(
+ @startdate date = NULL
+ ,@enddate date = NULL
+)
+RETURNS @bankingtransactions TABLE
+(
+
+ [InvId] varchar(15)
+ ,[Amount] numeric(9,2)
+ ,[manu] bit
+)
+AS
+BEGIN
+ -- Fill the table variable with the rows for your result set
+
+ DECLARE @invoice_idlist [dbo].[fds__tt__idlist_vchar];
+
+ INSERT INTO @bankingtransactions
+ SELECT
+ [invid]
+ , [amount] = SUM(ISNULL([amount], 0.0))
+ , [manu] = CAST(MAX(ISNULL(CAST([manu] as int),0)) as bit)
+ FROM dbo.fds__fn_bankingtransactions(null, null, @startdate, @enddate, @invoice_idlist) -- start and enddates will filter by invoice date and not by banking transactions' value dates
+ GROUP BY [invid];
+
+
+ RETURN
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_combineAddress.sql b/Fuchs_Database/dbo/Functions/fds__fn_combineAddress.sql
new file mode 100644
index 0000000..9ac958a
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_combineAddress.sql
@@ -0,0 +1,27 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_combineAddress]
+(
+ @AddressString nvarchar(255)
+ ,@AddressString2 nvarchar(255)
+ ,@AddressString3 nvarchar(255)
+ ,@Postal nvarchar(255)
+ ,@City nvarchar(255)
+ ,@State nvarchar(255)
+ ,@Country nvarchar(255)
+)
+RETURNS nvarchar(1000)
+AS
+BEGIN
+ DECLARE @address nvarchar(1000) = STUFF( (SELECT (CHAR(10) + [t]) as [text()] FROM
+ (SELECT * FROM (VALUES(@AddressString), (@AddressString2), (@AddressString3), (LTRIM(RTRIM(ISNULL(@postal, '') + ' ' + ISNULL(@city, '')))), (@state), (@country)) z ([t]) WHERE ISNULL([t], '') <> '')y
+ FOR XML PATH
+ , TYPE).value('.[1]','nvarchar(1000)')
+ , 1, 1, '');
+
+ RETURN @address;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_creanupTransactionInfo.sql b/Fuchs_Database/dbo/Functions/fds__fn_creanupTransactionInfo.sql
new file mode 100644
index 0000000..5ea9c94
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_creanupTransactionInfo.sql
@@ -0,0 +1,19 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_creanupTransactionInfo]
+(
+ @info varchar(255)
+)
+RETURNS varchar(255)
+AS
+BEGIN
+ DECLARE @RET varchar(255);
+
+ SET @RET = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@info, 'R ', 'R'), 'Rg', 'R'), '2 0', '20'), CHAR(10),''), CHAR(13), ''), '/', ' ')
+
+ RETURN ISNULL(@RET, '');
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_invoice-srq_id.sql b/Fuchs_Database/dbo/Functions/fds__fn_invoice-srq_id.sql
new file mode 100644
index 0000000..05b3f36
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_invoice-srq_id.sql
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+CREATE FUNCTION [dbo].[fds__fn_invoice-srq_id] (
+)
+RETURNS varchar(10)
+AS
+BEGIN
+ DECLARE @NewID varchar(7) = [dbo].[ocms_fn_generatePassword] (7,0,1,0);
+
+ WHILE EXISTS( SELECT * FROM [dbo].[fds__invoice_servicerequests] WITH (SERIALIZABLE) WHERE [id] = @NewID)
+ BEGIN
+ SET @NewID = [dbo].[ocms_fn_generatePassword] (7,0,1,0);
+ END
+
+ RETURN @NewID;
+END;
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_invoice_customerid.sql b/Fuchs_Database/dbo/Functions/fds__fn_invoice_customerid.sql
new file mode 100644
index 0000000..222cd79
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_invoice_customerid.sql
@@ -0,0 +1,19 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_invoice_customerid]
+(
+ @InvId varchar(15)
+)
+RETURNS bigint
+AS
+BEGIN
+ DECLARE @ret bigint;
+
+ SELECT TOP(1) @ret = [customerid] FROM dbo.[fds__invoices] WHERE [id] = @InvId;
+
+ RETURN @ret;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_invoice_id.sql b/Fuchs_Database/dbo/Functions/fds__fn_invoice_id.sql
new file mode 100644
index 0000000..a2892b4
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_invoice_id.sql
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+CREATE FUNCTION [dbo].[fds__fn_invoice_id] (
+)
+RETURNS varchar(10)
+AS
+BEGIN
+ DECLARE @NewAccount varchar(5) = [dbo].[ocms_fn_generatePassword] (8,0,1,0);
+
+ WHILE EXISTS( SELECT * FROM [dbo].[fds__invoices] WITH (SERIALIZABLE) WHERE [id] = @NewAccount)
+ BEGIN
+ SET @NewAccount = [dbo].[ocms_fn_generatePassword] (8,0,1,0);
+ END
+
+ RETURN @NewAccount;
+END;
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_isInvoiceCancelled.sql b/Fuchs_Database/dbo/Functions/fds__fn_isInvoiceCancelled.sql
new file mode 100644
index 0000000..607e168
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_isInvoiceCancelled.sql
@@ -0,0 +1,28 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_isInvoiceCancelled]
+(
+ @InvId varchar(20)
+)
+RETURNS bit
+AS
+BEGIN
+ DECLARE @res bit;
+
+ --SET @res = CASE WHEN EXISTS (SELECT 0 FROM [dbo].[fds__invoices] as i2 JOIN [dbo].[fds__invoice_details] as id2 ON i2.[id] = id2.[InvId] where id2.[StornoTo_InvId] = @invId AND i2.[IsFinal] = 1 ) THEN 1 ELSE 0 END;
+
+ with storno as(
+ SELECT [amount] = SUM(ISNULL(ORIG.[invoicebalance],0.0)), [stornoamount] = SUM(ISNULL(i2.[invoicebalance],0.0) * -1.0)
+ FROM [dbo].[fds__invoices] as ORIG
+ LEFT JOIN [dbo].[fds__invoice_details] as id2 ON id2.StornoTo_InvId = ORIG.[id] and id2.StornoTo_InvId = @invId
+ LEFT JOIN [dbo].[fds__invoices] as i2 on i2.[id] = id2.[InvId] AND i2.[IsFinal] = 1
+ WHERE ORIG.[Id] = @invId
+ )
+ SELECT @res = CASE WHEN [amount] = [stornoamount] THEN 1 ELSE 0 END from storno;
+
+ RETURN cast(ISNULL(@res,0) as bit);
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_reminder_id.sql b/Fuchs_Database/dbo/Functions/fds__fn_reminder_id.sql
new file mode 100644
index 0000000..33e107a
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_reminder_id.sql
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+CREATE FUNCTION [dbo].[fds__fn_reminder_id] (
+)
+RETURNS varchar(8)
+AS
+BEGIN
+ DECLARE @NewAccount varchar(5) = [dbo].[ocms_fn_generatePassword] (8,0,1,0);
+
+ WHILE EXISTS( SELECT * FROM [dbo].[fds__reminder] WHERE [id] = @NewAccount)
+ BEGIN
+ SET @NewAccount = [dbo].[ocms_fn_generatePassword] (8,0,1,0);
+ END
+
+ RETURN @NewAccount;
+END;
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_requestsforinvoice.sql b/Fuchs_Database/dbo/Functions/fds__fn_requestsforinvoice.sql
new file mode 100644
index 0000000..4682dbd
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_requestsforinvoice.sql
@@ -0,0 +1,26 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_requestsforinvoice]
+(
+
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ SELECT
+ [Id]
+ ,sreq.[ExternalId]
+ ,sreq.[Name]
+ ,sreq.[workdoneat]
+ ,sreq.[ReleasedAt]
+ ,sreq.[CustomerId]
+ FROM [dbo].[mfr__servicerequests] as sreq
+ WHERE [state] not in ('Closed')
+ AND [type] in ('IsProject', 'IsMaintenance')
+ AND EXISTS (SELECT 0 FROM dbo.[mfr__*PartnerSet] as ps WHERE ps.[Property] = 'ServiceRequest:Tags' and ps.[PartnerId] = '16034758659' and sreq.[id] = ps.[EntityId])
+ AND NOT EXISTS( SELECT 0 FROM [dbo].[fds__invoice_servicerequests] as isr join [dbo].[fds__invoices] as inv on isr.[InvId] = inv.[id] and isr.[mfr__servicerequest] = sreq.[id])
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_unpaidInvoices.sql b/Fuchs_Database/dbo/Functions/fds__fn_unpaidInvoices.sql
new file mode 100644
index 0000000..b417fd8
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_unpaidInvoices.sql
@@ -0,0 +1,33 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_unpaidInvoices]
+(
+
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ WITH inv as (
+ SELECT [Id]
+ ,[Invoiceid]
+ ,[DateFinalized]
+ ,[InvoiceBalance]
+ ,[DocumentName]
+ --,[file]
+ ,[InvoiceTitle]
+ ,[customerid]
+ ,[dateSent]
+ ,[fds] = CAST(IIF(iv.isexternal = 0,1,0) as bit)
+ FROM [dbo].[fds__invoices] as iv
+ WHERE [isfinal] = 1
+ and CAST(CASE WHEN ISNULL(iv.[IsCanceled],0) = 1 THEN 1 ELSE ISNULL([IsPayed],0) END as bit) = 0
+ and InvoiceBalance > 0
+ AND NOT exists (SELECT 0 FROM [dbo].[fds__invoice_details] as d where d.[StornoTo_InvId] = iv.[id])
+ )
+ SELECT * /*[Rechnung] = [invoiceId], [Name] = [InvoiceTitle] + CHAR(10) + ' (' + (SELECT TOP(1) c.[name] FROM [dbo].[mfr__companies] as c WHERE inv.[customerid] = c.[id]) + ')', [Betrag (net)] = FORMAT([invoicebalance], '#0.00€', 'de'), [Datum] = FORMAT([DateFinalized], 'dd.MM.yy' , 'de'), [Zahlungen] = FORMAT([dbo].[fds__fn_InvoicePaymentAmount]([id]), '#0.00€', 'de'), [order] = ROW_NUMBER() OVER (ORDER BY [DateFinalized], [DateSent]) */
+ FROM inv
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__fn_vatfrombalances.sql b/Fuchs_Database/dbo/Functions/fds__fn_vatfrombalances.sql
new file mode 100644
index 0000000..6bbd7e4
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__fn_vatfrombalances.sql
@@ -0,0 +1,18 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__fn_vatfrombalances]
+(
+ @brutto numeric(10,3)
+ ,@netto numeric(10,3)
+)
+RETURNS numeric(5,2)
+AS
+BEGIN
+ DECLARE @vat numeric(5,2) = CASE WHEN ISNULL(@netto, 0) > 0 and ISNULL(@brutto, 0) > 0 THEN ROUND(( (@brutto / @netto) -1) * 100, 2) ELSE NULL END
+
+ RETURN @vat;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getCompanyAddress.sql b/Fuchs_Database/dbo/Functions/fds__getCompanyAddress.sql
new file mode 100644
index 0000000..43ed6e4
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getCompanyAddress.sql
@@ -0,0 +1,36 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__getCompanyAddress]
+(
+ @companyid bigint
+)
+RETURNS nvarchar(1000)
+AS
+BEGIN
+ DECLARE @locationid bigint, @address nvarchar(1000);
+
+ SELECT TOP(1) @locationid = cy.[Location#ID] from [dbo].[mfr__companies] as cy WHERE cy.id = @companyid;
+
+ IF @locationid is null
+ SELECT TOP(1) @locationid = l.[ID], @address = [dbo].[fds__fn_combineAddress] (l.[AddressString], null, null
+ ,l.[Postal]
+ ,l.[City]
+ ,l.[State]
+ ,l.[Country]) FROM [mfr__#locations] as l JOIN [dbo].[mfr__companies] as cy on l.[Property] = 'Company:Location' AND l.[EntityId] = cy.[Id] WHERE cy.[id] = @companyid;
+
+ -- Return the result of the function
+
+ IF @address is null
+ SELECT TOP(1) @address = [dbo].[fds__fn_combineAddress] (loc.[AddressString], null, null
+ ,loc.[Postal]
+ ,loc.[City]
+ ,loc.[State]
+ ,loc.[Country])
+ FROM [dbo].[mfr__#locations] as loc WHERE [id] = @locationid;
+
+ RETURN @address;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getCompanyContacts.sql b/Fuchs_Database/dbo/Functions/fds__getCompanyContacts.sql
new file mode 100644
index 0000000..5d31b1e
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getCompanyContacts.sql
@@ -0,0 +1,31 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__getCompanyContacts]
+(
+ @companyid bigint
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ WITH ps as (
+ SELECT [contactid] = p.[EntityId] from [mfr__*PartnerSet] as p WHERE p.property = 'Contact:Company' and p.[partnerId] = @companyid
+ UNION
+ SELECT [contactid] = p.[PartnerId] from [mfr__*PartnerSet] as p WHERE p.property = 'Company:Contacts' and p.[EntityId] = @companyid
+ )
+ SELECT ct.*
+ ,[isMainContact] = CAST( CASE WHEN cy.[MainContactId] is null then 0 WHEN cy.[MainContactId] = ct.[id] THEN 1 ELSE 0 END as bit)
+ FROM [dbo].[mfr__contacts] as ct
+ JOIN [dbo].[mfr__companies] as cy ON ct.[CompanyId] = cy.[id]
+ WHERE ct.CompanyId = @companyid
+ UNION
+ SELECT ct.*
+ ,[isMainContact] = CAST( CASE WHEN cy.[MainContactId] is null then 0 WHEN cy.[MainContactId] = ct.[id] THEN 1 ELSE 0 END as bit)
+ FROM [dbo].[mfr__contacts] as ct
+ JOIN ps ON ct.[id] = ps.[contactid]
+ JOIN [dbo].[mfr__companies] as cy ON cy.[id] = @companyid
+
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getCompanyEmail.sql b/Fuchs_Database/dbo/Functions/fds__getCompanyEmail.sql
new file mode 100644
index 0000000..cd451f0
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getCompanyEmail.sql
@@ -0,0 +1,16 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__getCompanyEmail]
+(
+ @companyid bigint
+)
+RETURNS varchar(255)
+AS
+BEGIN
+ DECLARE @email varchar(255) = (SELECT [supportmail] FROM [dbo].[mfr__companies] as cy WHERE cy.[id] = @companyid);
+
+ RETURN @email;
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getCompanyName.sql b/Fuchs_Database/dbo/Functions/fds__getCompanyName.sql
new file mode 100644
index 0000000..a43b01f
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getCompanyName.sql
@@ -0,0 +1,20 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__getCompanyName]
+(
+ @companyid bigint
+)
+RETURNS nvarchar(1000)
+AS
+BEGIN
+ DECLARE @name nvarchar(255);
+
+ SELECT TOP(1) @name = cy.[name] FROM [dbo].[mfr__companies] as cy WHERE cy.id = @companyid;
+
+ RETURN @name;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getCompanyNameAddress.sql b/Fuchs_Database/dbo/Functions/fds__getCompanyNameAddress.sql
new file mode 100644
index 0000000..f70e1e9
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getCompanyNameAddress.sql
@@ -0,0 +1,36 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__getCompanyNameAddress]
+(
+ @companyid bigint
+)
+RETURNS nvarchar(1000)
+AS
+BEGIN
+ DECLARE @locationid bigint, @address nvarchar(1000), @name nvarchar(255);
+
+ SELECT TOP(1) @locationid = cy.[Location#ID], @name = cy.[name] FROM [dbo].[mfr__companies] as cy WHERE cy.id = @companyid;
+
+ IF @locationid is null
+ SELECT TOP(1) @locationid = l.[ID], @address = [dbo].[fds__fn_combineAddress] (l.[AddressString], null, null
+ ,l.[Postal]
+ ,l.[City]
+ ,l.[State]
+ ,l.[Country]) FROM [mfr__#locations] as l JOIN [dbo].[mfr__companies] as cy on l.[Property] = 'Company:Location' AND l.[EntityId] = cy.[Id] WHERE cy.[id] = @companyid;
+
+ -- Return the result of the function
+
+ IF @address is null
+ SELECT TOP(1) @address = [dbo].[fds__fn_combineAddress] (loc.[AddressString], null, null
+ ,loc.[Postal]
+ ,loc.[City]
+ ,loc.[State]
+ ,loc.[Country])
+ FROM [dbo].[mfr__#locations] as loc WHERE [id] = @locationid;
+
+ RETURN @name + CHAR(10) + @address;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getCompanysContacts.sql b/Fuchs_Database/dbo/Functions/fds__getCompanysContacts.sql
new file mode 100644
index 0000000..045b421
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getCompanysContacts.sql
@@ -0,0 +1,36 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__getCompanysContacts]
+(
+ @companyids [dbo].[fds__tt__idlist] READONLY
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ WITH ps as (
+ SELECT [companyid] = p.[partnerId], [contactid] = p.[EntityId] from [mfr__*PartnerSet] as p WHERE p.property = 'Contact:Company' and p.[partnerId] IN (SELECT [Id] FROM @companyids)
+ UNION
+ SELECT [companyid] = p.[PartnerId], [contactid] = p.[PartnerId] from [mfr__*PartnerSet] as p WHERE p.property = 'Company:Contacts' and p.[EntityId] = (SELECT [Id] FROM @companyids)
+ )
+ SELECT
+ [CyId] = cyid.[Id]
+ ,ct.*
+ ,[isMainContact] = CAST( CASE WHEN cy.[MainContactId] is null then 0 WHEN cy.[MainContactId] = ct.[id] THEN 1 ELSE 0 END as bit)
+ FROM [dbo].[mfr__contacts] as ct
+ JOIN @companyids as cyid ON ct.[CompanyId] = cyid.[id]
+ JOIN [dbo].[mfr__companies] as cy ON cyid.[id] = cy.[id]
+ UNION
+ SELECT
+ [CyId] = cyid.[Id]
+ ,ct.*
+ ,[isMainContact] = CAST( CASE WHEN cy.[MainContactId] is null then 0 WHEN cy.[MainContactId] = ct.[id] THEN 1 ELSE 0 END as bit)
+ FROM [dbo].[mfr__contacts] as ct
+ JOIN ps ON ct.[id] = ps.[contactid]
+ JOIN @companyids as cyid ON ps.[CompanyId] = cyid.[id]
+ JOIN [dbo].[mfr__companies] as cy ON cyid.[id] = cy.[id]
+
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getInvoiceCredits.sql b/Fuchs_Database/dbo/Functions/fds__getInvoiceCredits.sql
new file mode 100644
index 0000000..c0bde9f
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getInvoiceCredits.sql
@@ -0,0 +1,129 @@
+
+CREATE function [dbo].[fds__getInvoiceCredits]
+(
+ @startdate date
+ ,@enddate date
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ with b0 as (
+ SELECT [taID]
+ --,[AccountIdentification]
+ ,[Amount]
+ --,[EntryDate]
+ ,[FundsCode]
+ ,[AccountNumberOfPayer]
+ ,[BankCodeOfPayer]
+
+ ,[EndToEndReference]
+ --,[JournalNumber]
+ ,[NameOfPayer]
+
+ ,'info' = [dbo].[fds__fn_creanupTransactionInfo]([SepaRemittanceInformation])
+ ,'e2e' = [dbo].[fds__fn_creanupTransactionInfo]([EndToEndReference])
+
+ --,[PayersReferenceParty]
+ --,[PostingText]
+ ,[SepaRemittanceInformation]
+ ,[TextKeyAddition]
+ ,[TransactionCode]
+
+ ,[DebitCreditMark]
+ ,[TransactionTypeIdCode]
+ ,[ValueDate]
+ FROM [dbo].[fds__bankingtransactions]
+ WHERE [DebitCreditMark] in ('C')
+ AND (@startdate is null or CAST([valuedate] AS DATE) >= @startdate)
+ AND (@enddate is null or CAST([valuedate] AS DATE) <= @enddate)
+ AND NOT (ISNULL([AccountNumberOfPayer],'') IN ('DE52301502000002091478') AND ISNULL([SepaRemittanceInformation],'') like ('%umbuchung%'))
+ AND NOT (ISNULL([AccountNumberOfPayer],'') IN ('DE23501108006161606386') AND ISNULL([SepaRemittanceInformation],'') like ('%ebay%'))
+ AND NOT (ISNULL([SepaRemittanceInformation],'') like ('%Umsatzsteuer%'))
+ )
+ , b as (
+ SELECT
+ *
+ ,'exact' = PATINDEX('%R202[0-9][- ][0-9][0-9][0-9][0-9]%',[info])
+ ,'exact2' = PATINDEX('%R202[0-9][- ][0-9][0-9][0-9][0-9]%',REPLACE([info], ' ', ''))
+ ,'test1' = PATINDEX('%202[0-9]%',[info])
+ ,'E2E_exact' = PATINDEX('%R202[0-9][- ][0-9][0-9][0-9][0-9]%',[e2e])
+ ,'E2E_exact2' = PATINDEX('%R202[0-9][- ][0-9][0-9][0-9][0-9]%',REPLACE([e2e], ' ', ''))
+ ,'E2E_test1' = PATINDEX('%202[0-9]%',[e2e])
+ from b0
+ ), b2 as (
+ SELECT
+ *
+ ,[info2] = REPLACE([info], ' ', '')
+ ,[info3] = CASE WHEN test1 > 0 THEN REPLACE(SUBSTRING([info], [test1] - 1, 10), ' ' , '-') ELSE '' END
+ ,[info4] = REPLACE(REPLACE([info], 'RE ', 'NO '), 'RE-NR. ', 'NO ')
+ ,[e2e_info2] = REPLACE([e2e], ' ', '')
+ ,[e2e_info3] = CASE WHEN [E2E_test1] > 0 THEN REPLACE(SUBSTRING([e2e], [E2E_test1] - 1, 10), ' ' , '-') ELSE '' END
+ FROM b
+ ), c as (
+ select
+ *
+ , 'lv' = CASE WHEN exact > 0 THEN 0
+ WHEN exact2 > 0 THEN 1
+ WHEN [test1] > 0 AND PATINDEX('%202[0-9][- ][0-9][0-9][0-9][0-9]%', [info3]) > 0 THEN 2
+ WHEN [test1] > 0 AND PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [info3]) > 0 THEN 3
+
+ WHEN [e2e_exact] > 0 THEN 10
+ WHEN [e2e_exact2] > 0 THEN 11
+ WHEN [e2e_test1] > 0 AND PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [e2e_info3]) > 0 THEN 12
+ WHEN [e2e_test1] > 0 AND PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [e2e_info3]) > 0 THEN 13
+
+ WHEN PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) > 0 AND EXISTS (SELECT * FROM [dbo].[fds__invoices] as i where RIGHT(i.[InvoiceId],4) = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) )
+ THEN 21
+ WHEN PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) > 0 AND EXISTS (SELECT * FROM [dbo].[mfr__invoices] as i where RIGHT(i.[InvoiceId],4) = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) )
+ THEN 22
+ ELSE 99 END
+ , 'Rg' =
+ CASe WHEN [exact] > 0 THEN REPLACE(SUBSTRING([info], [exact], 10), ' ' , '-')
+ WHEN [exact2] > 0 THEN REPLACE(SUBSTRING([info2], [exact2], 10), ' ' , '-')
+ WHEN [test1] > 0 AND PATINDEX('%202[0-9][- ][0-9][0-9][0-9][0-9]%', [info3]) > 0 THEN 'R' + SUBSTRING([info3], PATINDEX('%202[0-9][- ][0-9][0-9][0-9][0-9]%', [info3]) ,9)
+ WHEN [test1] > 0 AND PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [info3]) > 0 THEN 'R' + SUBSTRING([info3], PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [info3]) ,4) + '-' + + SUBSTRING([info3], PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [info3]) +4 ,4)
+ WHEN [e2e_exact] > 0 THEN REPLACE(SUBSTRING([e2e], [e2e_exact], 10), ' ' , '-')
+ WHEN [e2e_exact2] > 0 THEN REPLACE(SUBSTRING([e2e_info2], [e2e_exact2], 10), ' ' , '-')
+ WHEN [e2e_test1] > 0 AND PATINDEX('%202[0-9][- ][0-9][0-9][0-9][0-9]%', [e2e_info3]) > 0 THEN 'R' + SUBSTRING([e2e_info3], PATINDEX('%202[0-9][- ][0-9][0-9][0-9][0-9]%', [e2e_info3]) ,9)
+ WHEN [e2e_test1] > 0 AND PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [e2e_info3]) > 0 THEN 'R' + SUBSTRING([e2e_info3], PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [e2e_info3]) ,4) + '-' + + SUBSTRING([e2e_info3], PATINDEX('%202[0-9][0-9][0-9][0-9][0-9]%', [e2e_info3]) +4 ,4)
+
+ WHEN PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) > 0 AND EXISTS (SELECT * FROM [dbo].[fds__invoices] as i where RIGHT(i.[InvoiceId],4) = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) )
+ THEN (SELECT TOP(1) [invoiceid] FROM [dbo].[fds__invoices] as i where RIGHT(i.[InvoiceId],4) = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) )
+ WHEN PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) > 0 AND EXISTS (SELECT * FROM [dbo].[mfr__invoices] as i where RIGHT(i.[InvoiceId],4) = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) )
+ THEN (SELECT TOP(1) [invoiceid] FROM [dbo].[mfr__invoices] as i where RIGHT(i.[InvoiceId],4) = SUBSTRING([info4],PATINDEX('%NO [0-9][0-9][0-9][0-9] %', [info4]) + 3, 4) )
+
+
+
+ ELSE NULL
+ END
+ from b2
+ )
+ SELECT
+ [taID]
+ , [InvoiceId] = UPPER([rg])
+ , [info4]
+ , [Amount]
+ --,[EntryDate]
+ , [FundsCode]
+ , [AccountNumberOfPayer]
+ , [BankCodeOfPayer]
+
+ , [EndToEndReference]
+ --,[JournalNumber]
+ , [NameOfPayer]
+
+
+ --,[PayersReferenceParty]
+ --,[PostingText]
+ , [SepaRemittanceInformation]
+ , [TextKeyAddition]
+ , [TransactionCode]
+
+ , [DebitCreditMark]
+ , [TransactionTypeIdCode]
+ , [ValueDate]
+ --,ASCII(LEFT(RIGHT([info], 2),1))
+ FROM c
+ --where lv >90--Rg is null
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getInvoiceTreeIds.sql b/Fuchs_Database/dbo/Functions/fds__getInvoiceTreeIds.sql
new file mode 100644
index 0000000..ae1736d
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getInvoiceTreeIds.sql
@@ -0,0 +1,23 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__getInvoiceTreeIds]
+(
+ @invid varchar(20)
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ with fds_srq as (
+ SELECT distinct [type] = 'servicerequest', [id] = mfrs.[id]
+ FROM [dbo].[fds__invoices] as _fi
+ JOIN [dbo].[fds__invoice_servicerequests] as _fs on _fi.Id = _fs.[InvId] and _fs.[mfr__servicerequest] is not null
+ JOIN [dbo].[mfr__servicerequests] as mfrs on _fs.[mfr__servicerequest] = mfrs.id
+ WHERE _fi.[Id] = @invid
+ )
+ SELECT * FROM fds_srq
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getRequestTreeIds.sql b/Fuchs_Database/dbo/Functions/fds__getRequestTreeIds.sql
new file mode 100644
index 0000000..b403597
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getRequestTreeIds.sql
@@ -0,0 +1,18 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__getRequestTreeIds]
+(
+ @srqid bigint
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ with sp as (
+ SELECT DISTINCT [parentid] = CASE WHEN ISNULL([parentservicerequestid],0) = 0 THEN [id] ELSE [parentservicerequestid] END FROM [dbo].[mfr__servicerequests] as _s where _s.[Id] = @srqid OR _s.[ParentServiceRequestId] = @srqid
+ )
+ SELECT DISTINCT r.[Id] from [dbo].[mfr__servicerequests] as r JOIN sp on r.[Id] = sp.[parentid] OR r.[ParentServiceRequestId] = sp.[parentid] and ISNULL(sp.parentid,0) > 0
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getUpdateableRequests.sql b/Fuchs_Database/dbo/Functions/fds__getUpdateableRequests.sql
new file mode 100644
index 0000000..369ac77
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getUpdateableRequests.sql
@@ -0,0 +1,115 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__getUpdateableRequests]
+(
+)
+RETURNS @RET TABLE
+(
+ [entity_name] varchar(255)
+ ,[Id] bigint
+ ,[order] bigint
+)
+AS
+BEGIN
+ DECLARE @threshold_minutes_full int = 60, @threshold_minutes_short int = 8;
+
+ DECLARE @utcnow datetime = GETUTCDATE();
+ DECLARE @UTCTimeInMinutes int = DATEDIFF(MINUTE, CAST(CAST(@utcnow as date) as datetime), @utcnow);
+
+ DECLARe @AS XML = [dbo].[mfr__schema]();
+
+ DECLARE @EntityTypes TABLE([#] int, [typ] varchar(50), [name] varchar(255), [set] varchar(255), [EntitySet] varchar(255), [tablename] varchar(255), [url] varchar(500), [DateColumn] varchar(25));
+ WITH XMLNAMESPACES
+ (
+ 'http://schemas.microsoft.com/ado/2007/06/edmx' as edmx,
+ 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata' as m
+ ), et as (
+ SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) as '#'
+ , xD.value('local-name(.)', 'varchar(50)') as [type]
+ , xD.value('@Name', 'varchar(255)') as [EntityType] --, xD.query('.') as 'q'
+ , xS.value('@Name', 'varchar(255)') as [Set]
+ , o.[name] as [tablename]
+ from @as.nodes('/Schema[1]/EntityType') as xm(xD)
+ JOIN @as.nodes('/Schema/EntityContainer/EntitySet') as xn(xS) ON (xS.value('@EntityType', 'varchar(255)')) like ('MFR.%[.]' + (xD.value('@Name', 'varchar(255)')))
+ JOIN sys.objects as o ON o.[name] like ('mfr__' + (LOWER(xS.value('@Name', 'varchar(255)'))))
+ union
+ SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) as '#'
+ , xD.value('local-name(.)', 'varchar(50)') as [type]
+ , xD.value('@Name', 'varchar(255)') as [EntityType] --, xD.query('.') as 'q'
+ , '' as [Set]
+ , o.[name] as [tablename]
+ from @as.nodes('/Schema[1]/ComplexType') as xm(xD)
+ JOIN sys.objects as o ON o.[name] like ('mfr__[#]' + (LOWER(xD.value('@Name', 'varchar(255)'))) + 's')
+ )
+ INSERT INTO @EntityTypes SELECT [#],[type],[EntityType],[set] = LOWER([set]), [EntitySet] = [set], [tablename], null, null FROM et;
+
+ WITH tbl as(
+ SELECT
+ --QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.[name]) AS [TABLE_NAME]
+ sOBJ.[name] AS [TABLE_NAME]
+ , SUM(sPTN.Rows) AS [RowCount]
+ ,sOBJ.[create_date]
+ FROM
+ sys.objects AS sOBJ
+ INNER JOIN sys.partitions AS sPTN
+ ON sOBJ.object_id = sPTN.object_id
+ AND sOBJ.[name] like 'mfr__%'
+ WHERE
+ sOBJ.[type] = 'U'
+ AND sOBJ.is_ms_shipped = 0x0
+ AND index_id < 2 -- 0:Heap, 1:Clustered
+ GROUP BY
+ sOBJ.schema_id
+ , sOBJ.name
+ , sObj.[create_date]
+ ), pro as (SELECT *
+ FROM sys.procedures WHERE LEFT([name], 11) = 'mfr__updt__')
+ , lu as (SELECT [table], MAX([timestamp]) as 'lu' FROM [dbo].[fds__status] WHERE [action] like 'update%' group by [table])
+ , luf as (SELECT [table], MAX([timestamp]) as 'luf' FROM [dbo].[fds__status] WHERE [action] like 'update_full' group by [table])
+ , lur as (SELECT [table], MAX([timestamp]) as 'lur' FROM [dbo].[fds__status] WHERE [action] like 'update_reset' group by [table])
+ , luc as (SELECT lu.[table], [lu], [luf], [lur]
+ , DATEDIFF(MINUTE, [lu], @utcnow) as [dlu]
+ , DATEDIFF(MINUTE, [lur], @utcnow) as [dlr]
+ , DATEDIFF(MINUTE, [luf], @utcnow) as [dluf]
+ , CASE --WHEN [dbo].[ctm__getUpdateLockStatus] () = Cast(1 as bit) THEN 0
+ WHEN [lu] is null then 5
+ --WHEN DATEDIFF(MINUTE, [lu], @utcnow) > @threshold_minutes_full
+ -- OR DATEDIFF(MINUTE, [luf], @utcnow) > @threshold_minutes_full
+ -- THEN 2
+ WHEN DATEDIFF(MINUTE, [lu], @utcnow) > @threshold_minutes_short THEN 1
+ ELSE 0
+ END as 'need'
+ from lu LEFT JOIN luf on lu.[table] = luf.[table] LEFT JOIN lur on lu.[table] = lur.[table]
+ ), cols as (SELECT DISTINCT [TABLE_NAME] FROM INFORMATION_SCHEMA.COLUMNS
+ WHERE LEFT(TABLE_NAME, 5) = N'mfr__' AND ([column_name] COLLATE Latin1_General_CI_AS in ('DateModified', 'DateCreated', 'DateOfCreation')))
+ , updateabletables as (
+ SELECT DISTINCT [entity_name] = et.[name]
+ , 'table_name' = tbl.[table_name]
+ , 'temp_table' = pro.[name]
+ , 'locked' = 0 --ISNULL([dbo].[ctm__getUpdateLockStatus_tbl](REPLACE(tbl.[TABLE_NAME], 'mfr__', '')), 0)
+ , 'locked_reset' = 0 --ISNULL([dbo].[ctm__getUpdateLockStatus_reset_tbl](REPLACE(tbl.[TABLE_NAME], 'mfr__', '')), 0)
+ , 'updateneed' = CASE
+ --WHEN [dbo].[ctm__getUpdateLockStatus_tbl](REPLACE(tbl.[TABLE_NAME], 'mfr__', '')) = Cast(1 as bit) THEN 0
+ WHEN et.[name] COLLATE Latin1_General_CI_AS IN ('Attachment','Comment','Item','StepListTemplateInstance','Step') THEN 0
+ WHEN tbl.[RowCount] = 0 THEN 5
+ WHEN NOT EXISTS (SELECT * FROM cols WHERE tbl.[TABLE_NAME] = cols.[TABLE_NAME]) THEN 0
+ WHEN luc.need is null then 5
+ WHEN tbl.[TABLE_NAME] COLLATE Latin1_General_CI_AS in ('') AND ISNULL(luc.[dlr], 1440) >= 720 AND @UTCTimeInMinutes BETWEEN 45 AND 90 THEN 5 -- once every day, between 02:45 and 03:30 in the morning
+ ELSE luc.[need]--ISNULL(luc.[need], 0)
+ END
+ , 'datebased' = CASE WHEN EXISTS (SELECT * FROM cols WHERE tbl.[TABLE_NAME] = cols.[TABLE_NAME]) THEN 1 ELSE 0 END
+ FROM tbl
+ JOIN @EntityTypes as et on tbl.[TABLE_NAME] = et.[tablename]
+ JOIN pro on REPLACE(pro.[name], 'mfr__updt__', 'mfr__') = tbl.[TABLE_NAME]
+ LEFT JOIN luc on tbl.[table_name] = luc.[table]
+ )
+ insert into @RET
+ SELECT rq.[entity_name], rq.[Id], ROW_NUMBER() OVER (ORDER BY [date_requested])
+ FROM [dbo].[fds__mfr_updaterequests] as rq JOIN updateabletables ON rq.[entity_name] = updateabletables.[entity_name] AND rq.Id is not null
+ ;
+
+ RETURN
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__getUpdateableTables.sql b/Fuchs_Database/dbo/Functions/fds__getUpdateableTables.sql
new file mode 100644
index 0000000..052aa51
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__getUpdateableTables.sql
@@ -0,0 +1,114 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__getUpdateableTables]
+(
+)
+RETURNS @RET TABLE
+(
+ [entity_name] varchar(255)
+ ,[table_name] varchar(255)
+ ,[proc_name] varchar(255)
+ ,[locked] bit
+ ,[locked_reset] bit
+ ,[updateneed] tinyint
+ ,[datebased] bit
+)
+AS
+BEGIN
+ DECLARE @threshold_minutes_full int = 60, @threshold_minutes_short int = 8;
+
+ DECLARE @utcnow datetime = GETUTCDATE();
+ DECLARE @UTCTimeInMinutes int = DATEDIFF(MINUTE, CAST(CAST(@utcnow as date) as datetime), @utcnow);
+
+ DECLARe @AS XML = [dbo].[mfr__schema]();
+
+ DECLARE @EntityTypes TABLE([#] int, [typ] varchar(50), [name] varchar(255), [set] varchar(255), [EntitySet] varchar(255), [tablename] varchar(255), [url] varchar(500), [DateColumn] varchar(25));
+ WITH XMLNAMESPACES
+ (
+ 'http://schemas.microsoft.com/ado/2007/06/edmx' as edmx,
+ 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata' as m
+ ), et as (
+ SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) as '#'
+ , xD.value('local-name(.)', 'varchar(50)') as [type]
+ , xD.value('@Name', 'varchar(255)') as [EntityType] --, xD.query('.') as 'q'
+ , xS.value('@Name', 'varchar(255)') as [Set]
+ , o.[name] as [tablename]
+ from @as.nodes('/Schema[1]/EntityType') as xm(xD)
+ JOIN @as.nodes('/Schema/EntityContainer/EntitySet') as xn(xS) ON (xS.value('@EntityType', 'varchar(255)')) like ('MFR.%[.]' + (xD.value('@Name', 'varchar(255)')))
+ JOIN sys.objects as o ON o.[name] like ('mfr__' + (LOWER(xS.value('@Name', 'varchar(255)'))))
+ union
+ SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) as '#'
+ , xD.value('local-name(.)', 'varchar(50)') as [type]
+ , xD.value('@Name', 'varchar(255)') as [EntityType] --, xD.query('.') as 'q'
+ , '' as [Set]
+ , o.[name] as [tablename]
+ from @as.nodes('/Schema[1]/ComplexType') as xm(xD)
+ JOIN sys.objects as o ON o.[name] like ('mfr__[#]' + (LOWER(xD.value('@Name', 'varchar(255)'))) + 's')
+ )
+ INSERT INTO @EntityTypes SELECT [#],[type],[EntityType],[set] = LOWER([set]), [EntitySet] = [set], [tablename], null, null FROM et;
+
+ WITH tbl as(
+ SELECT
+ --QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.[name]) AS [TABLE_NAME]
+ sOBJ.[name] AS [TABLE_NAME]
+ , SUM(sPTN.Rows) AS [RowCount]
+ ,sOBJ.[create_date]
+ FROM
+ sys.objects AS sOBJ
+ INNER JOIN sys.partitions AS sPTN
+ ON sOBJ.object_id = sPTN.object_id
+ AND sOBJ.[name] like 'mfr__%'
+ WHERE
+ sOBJ.[type] = 'U'
+ AND sOBJ.is_ms_shipped = 0x0
+ AND index_id < 2 -- 0:Heap, 1:Clustered
+ GROUP BY
+ sOBJ.schema_id
+ , sOBJ.name
+ , sObj.[create_date]
+ ), pro as (SELECT *
+ FROM sys.procedures WHERE LEFT([name], 11) = 'mfr__updt__')
+ , lu as (SELECT [table], MAX([timestamp]) as 'lu' FROM [dbo].[fds__status] WHERE [action] like 'update%' group by [table])
+ , luf as (SELECT [table], MAX([timestamp]) as 'luf' FROM [dbo].[fds__status] WHERE [action] like 'update_full' group by [table])
+ , lur as (SELECT [table], MAX([timestamp]) as 'lur' FROM [dbo].[fds__status] WHERE [action] like 'update_reset' group by [table])
+ , luc as (SELECT lu.[table], [lu], [luf], [lur]
+ , DATEDIFF(MINUTE, [lu], @utcnow) as [dlu]
+ , DATEDIFF(MINUTE, [lur], @utcnow) as [dlr]
+ , DATEDIFF(MINUTE, [luf], @utcnow) as [dluf]
+ , CASE --WHEN [dbo].[ctm__getUpdateLockStatus] () = Cast(1 as bit) THEN 0
+ WHEN [lu] is null then 5
+ --WHEN DATEDIFF(MINUTE, [lu], @utcnow) > @threshold_minutes_full
+ -- OR DATEDIFF(MINUTE, [luf], @utcnow) > @threshold_minutes_full
+ -- THEN 2
+ WHEN DATEDIFF(MINUTE, [lu], @utcnow) > @threshold_minutes_short THEN 1
+ ELSE 0
+ END as 'need'
+ from lu LEFT JOIN luf on lu.[table] = luf.[table] LEFT JOIN lur on lu.[table] = lur.[table]
+ ), cols as (SELECT DISTINCT [TABLE_NAME] FROM INFORMATION_SCHEMA.COLUMNS
+ WHERE LEFT(TABLE_NAME, 5) = N'mfr__' AND ([column_name] COLLATE Latin1_General_CI_AS in ('DateModified', 'DateCreated', 'DateOfCreation')))
+ INSERT INTO @RET
+ SELECT DISTINCT [entity_name] = et.[name]
+ , 'table_name' = tbl.[table_name]
+ , 'temp_table' = pro.[name]
+ , 'locked' = 0 --ISNULL([dbo].[ctm__getUpdateLockStatus_tbl](REPLACE(tbl.[TABLE_NAME], 'mfr__', '')), 0)
+ , 'locked_reset' = 0 --ISNULL([dbo].[ctm__getUpdateLockStatus_reset_tbl](REPLACE(tbl.[TABLE_NAME], 'mfr__', '')), 0)
+ , 'updateneed' = CASE
+ --WHEN [dbo].[ctm__getUpdateLockStatus_tbl](REPLACE(tbl.[TABLE_NAME], 'mfr__', '')) = Cast(1 as bit) THEN 0
+ WHEN et.[name] COLLATE Latin1_General_CI_AS IN ('Attachment','Comment','Item','StepListTemplateInstance','Step') THEN 0
+ WHEN tbl.[RowCount] = 0 THEN 5
+ WHEN NOT EXISTS (SELECT * FROM cols WHERE tbl.[TABLE_NAME] = cols.[TABLE_NAME]) THEN 0
+ WHEN luc.need is null then 5
+ WHEN tbl.[TABLE_NAME] COLLATE Latin1_General_CI_AS in ('') AND ISNULL(luc.[dlr], 1440) >= 720 AND @UTCTimeInMinutes BETWEEN 45 AND 90 THEN 5 -- once every day, between 02:45 and 03:30 in the morning
+ ELSE luc.[need]--ISNULL(luc.[need], 0)
+ END
+ , 'datebased' = CASE WHEN EXISTS (SELECT * FROM cols WHERE tbl.[TABLE_NAME] = cols.[TABLE_NAME]) THEN 1 ELSE 0 END
+ FROM tbl
+ JOIN @EntityTypes as et on tbl.[TABLE_NAME] = et.[tablename]
+ JOIN pro on REPLACE(pro.[name], 'mfr__updt__', 'mfr__') = tbl.[TABLE_NAME]
+ LEFT JOIN luc on tbl.[table_name] = luc.[table];
+
+ RETURN
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__newInvoiceId.sql b/Fuchs_Database/dbo/Functions/fds__newInvoiceId.sql
new file mode 100644
index 0000000..96d8b4d
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__newInvoiceId.sql
@@ -0,0 +1,19 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__newInvoiceId]
+(
+ @year int
+)
+RETURNS varchar(25)
+AS
+BEGIN
+ DECLARE @lastid_int bigint;
+ SELECT @lastid_int = MAX( TRY_PARSE(RIGHT([InvoiceId], 4) as integer) ) FROM [dbo].[fds__invoices] WHERE LEFT(ISNULL([InvoiceId],''),5) = ('R' + CAST(@year as varchar(4)));
+ DECLARE @invoice_id varchar(25) = 'R' + CAST(@year as varchar(4)) + '-' + FORMAT(ISNULL( @lastid_int , CASE WHEN @year = 2021 THEN 1593 ELSE 0 END) + 1, '0000');
+
+ RETURN @invoice_id;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__r_getBalanceThisMonth.sql b/Fuchs_Database/dbo/Functions/fds__r_getBalanceThisMonth.sql
new file mode 100644
index 0000000..8a21d5c
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__r_getBalanceThisMonth.sql
@@ -0,0 +1,33 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fds__r_getBalanceThisMonth](
+ @authuser varchar(100)
+)
+RETURNS varchar(25)
+BEGIN
+ DECLARE @today date = GETDATE();
+ DECLARE @tomorrow date = DATEADD(DAY,1,@today);
+ DECLARE @thismonthfirst date = [dbo].[date_monthfirst](@today);
+ DECLARE @ret varchar(25) = ''
+
+ IF [dbo].[fis_getModuleAuth]('fds_reports', @authuser) < 2
+ SET @ret = '';
+ ELSE
+ BEGIN
+
+ WITH inv as (
+ SELECT
+ [Dateofcreation] = i.[DateFinalized]
+ ,i.[InvoiceBalance]
+ ,[InvoiceBalanceNetto] = i.[InvoiceBalance_net]
+ FROM [dbo].[fds__invoices] as i
+ WHERE i.[isFinal] = 1 AND [dbo].[date_monthfirst](i.[DateFinalized]) = @thismonthfirst
+ )
+ SELECT TOP(1) @ret = FORMAT( SUM( ISNULL(inv.[InvoiceBalanceNetto],0) ) * 0.001, '#,0 k€', 'de') FROM inv;
+ END
+
+ RETURN @ret;
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fds__updateNeed.sql b/Fuchs_Database/dbo/Functions/fds__updateNeed.sql
new file mode 100644
index 0000000..0f98931
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fds__updateNeed.sql
@@ -0,0 +1,25 @@
+CREATE FUNCTION [dbo].[fds__updateNeed]
+(
+ @threshold_minutes_full int
+ ,@threshold_minutes_short int
+)
+RETURNS tinyint
+AS
+BEGIN
+
+ --[Reset] = 5
+ -- [Full] = 2
+ -- [Short] = 1
+ -- [None] = 0
+
+
+
+ SET @threshold_minutes_full = 60;
+ SET @threshold_minutes_short = 8;
+
+ DECLARE @RET tinyint = 1
+ DECLARE @UpDate datetime
+ DECLARE @UpDateFull datetime
+
+ RETURN @RET;
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_authenticate.sql b/Fuchs_Database/dbo/Functions/fis_admin_authenticate.sql
new file mode 100644
index 0000000..afe4350
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_authenticate.sql
@@ -0,0 +1,33 @@
+
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_authenticate]
+(
+ @email varchar(255),
+ @password nvarchar(20)
+)
+RETURNS @RET TABLE ( [useraccount_id] varchar(5), [auth] smallint, [name] nvarchar(100) null, [firstname] nvarchar(100) null, [email] varchar(255) NULL, [mobile] varchar(50) null, [functions] varchar(1000), [language] varchar(2), [fullname] nvarchar(255) null, [fullname_rev] nvarchar(255) null)
+AS
+BEGIN
+ DECLARE @auth smallint = -1;
+ DECLARE @useraccount_id varchar(10), @name nvarchar(255), @mobile varchar(50), @functions varchar(1000) = '';
+
+ SELECT TOP(1) @useraccount_id = [useraccount_id]
+ FROM [dbo].[fis_useraccounts] as ua
+ WHERE ua.[DateDeleted] is null AND ua.[DateDisabled] is null
+ AND CAST(DECRYPTBYKEY(ua.[email_enc]) as varchar(255)) = @email
+ AND @email <> ''
+ AND CAST(DECRYPTBYKEY(ua.[password_enc]) as nvarchar(20)) = @password
+ AND @password <> ''
+ AND ua.[password_enc] is not null;
+
+ INSERT INTO @RET SELECT * FROM [dbo].[fis_admin_getUserAccount](@useraccount_id);
+
+ RETURN;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_authenticate_byID.sql b/Fuchs_Database/dbo/Functions/fis_admin_authenticate_byID.sql
new file mode 100644
index 0000000..fdbe73e
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_authenticate_byID.sql
@@ -0,0 +1,34 @@
+
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_authenticate_byID]
+(
+ @useraccount_id varchar(10),
+ @password nvarchar(20)
+)
+RETURNS @RET TABLE ( [useraccount_id] varchar(5), [auth] smallint, [name] nvarchar(100) null, [firstname] nvarchar(100) null, [email] varchar(255) NULL, [mobile] varchar(50) null, [functions] varchar(1000), [language] varchar(2), [fullname] nvarchar(255) null, [fullname_rev] nvarchar(255) null)
+AS
+BEGIN
+ DECLARE @auth smallint = -1;
+ DECLARE @name nvarchar(255), @mobile varchar(50), @functions varchar(1000) = '';
+
+ IF EXISTS( SELECT 0
+ FROM [dbo].[fis_useraccounts] as ua
+ WHERE ua.[DateDeleted] is null AND ua.[DateDisabled] is null
+ AND [useraccount_id] = @useraccount_id and ISNULL(@useraccount_id, '') <> ''
+ AND CAST(DECRYPTBYKEY(ua.[password_enc]) as nvarchar(20)) = @password
+ AND @password <> ''
+ AND ua.[password_enc] is not null
+ )
+ BEGIN
+ INSERT INTO @RET SELECT * FROM [dbo].[fis_admin_getUserAccount](@useraccount_id);
+ END
+
+ RETURN;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_checkUserExists.sql b/Fuchs_Database/dbo/Functions/fis_admin_checkUserExists.sql
new file mode 100644
index 0000000..50f7f8b
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_checkUserExists.sql
@@ -0,0 +1,20 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_checkUserExists]
+(
+ @useraccount_id varchar(5)
+)
+RETURNS bit
+AS
+BEGIN
+ DECLARE @RET bit;
+
+ SET @RET = IIF(EXISTS (SELECT * FROM [dbo].[fis_useraccounts] as ua WHERE [useraccount_id] = @useraccount_id AND ua.[DateDeleted] is null AND ua.[DateDisabled] is null), 1, 0);
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserAccount.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserAccount.sql
new file mode 100644
index 0000000..b409a59
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserAccount.sql
@@ -0,0 +1,55 @@
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserAccount]
+(
+ @useraccount_id varchar(5)
+)
+RETURNS @RET TABLE ( [useraccount_id] varchar(5), [auth] smallint, [name] nvarchar(100) null, [firstname] nvarchar(100) null, [email] varchar(255) NULL, [mobile] varchar(50) null, [functions] varchar(1000), [language] varchar(2), [fullname] nvarchar(255) null, [fullname_rev] nvarchar(255) null)
+AS
+BEGIN
+ DECLARE @auth smallint = -1;
+ DECLARE @email varchar(255), @name nvarchar(100), @firstname nvarchar(100), @mobile varchar(50), @functions varchar(1000) = '', @title varchar(50), @gender varchar(1), @password varchar(5), @language varchar(2), @fullname nvarchar(255), @fullname_rev nvarchar(255);
+
+ SELECT TOP(1) @useraccount_id = [useraccount_id]
+ ,@auth = CASE WHEN [useraccount_id] is null THEN -1 ELSE 1 END
+ ,@name = CAST(DECRYPTBYKEY(ua.[name_enc]) as nvarchar(100))
+ ,@firstname = CAST(DECRYPTBYKEY(ua.[firstname_enc]) as nvarchar(100))
+ ,@title = CAST(DECRYPTBYKEY(ua.[title_enc]) as varchar(50))
+ ,@gender = CAST(DECRYPTBYKEY(ua.[gender_enc]) as varchar(1))
+ --,@password = DECRYPTBYKEY(ua.[password_enc])
+ ,@email = CAST(DECRYPTBYKEY(ua.[email_enc]) as varchar(255))
+ ,@mobile = CAST(DECRYPTBYKEY(ua.[mobile_enc]) as varchar(50))
+ ,@language = ua.[language]
+ ,@fullname = [dbo].[fis_admin_getUserName](ua.[name_enc], ua.[firstname_enc], ua.[title_enc])
+ ,@fullname_rev = [dbo].[fis_admin_getUserName_rev](ua.[name_enc], ua.[firstname_enc], ua.[title_enc])
+ FROM [dbo].[fis_useraccounts] as ua
+ WHERE ua.[DateDeleted] is null AND ua.[DateDisabled] is null
+ AND ua.[useraccount_id] = @useraccount_id;
+
+ SET @auth = ISNULL(TRY_CAST((SELECT TOP(1) [value] FROM [fis_useraccounts_settings] WHERE [useraccount_id] = @useraccount_id and [key] = 'intranet_admin') as smallint), @auth);
+ SET @functions = STUFF(
+ (SELECT ',' + [value] as [text()] FROM [fis_useraccounts_settings] WHERE [useraccount_id] = @useraccount_id and [key] = 'intranet_function' FOR XML PATH(''), TYPE).value('.','nvarchar(max)')
+ , 1,1,'');
+
+ INSERT INTO @RET
+ VALUES(
+ @useraccount_id
+ ,IIF(@useraccount_id is null, -1, @auth)
+ ,IIF(@useraccount_id is null, null, @name)
+ ,IIF(@useraccount_id is null, null, @firstname)
+ ,IIF(@useraccount_id is null, null, @email)
+ ,IIF(@useraccount_id is null, null, @mobile)
+ ,IIF(@useraccount_id is null, null, @functions)
+ ,IIF(@useraccount_id is null, null, @language)
+ ,IIF(@useraccount_id is null, null, @fullname)
+ ,IIF(@useraccount_id is null, null, @fullname_rev)
+ );
+
+ RETURN;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserAccountList.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserAccountList.sql
new file mode 100644
index 0000000..e025aeb
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserAccountList.sql
@@ -0,0 +1,65 @@
+
+
+
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserAccountList]
+(
+ @authaccount varchar(5)
+ , @useraccount_id varchar(5)
+ , @usergroup_id varchar(5)
+ , @include_nonmembers as bit
+)
+RETURNS @RET TABLE ( [useraccount_id] varchar(5), [auth] smallint, [name] nvarchar(100) null, [firstname] nvarchar(100) null, [title] varchar(50) NULL, [gender] varchar(1), [email] varchar(255) NULL, [mobile] varchar(50) null, [functions] varchar(1000), [language] varchar(2), [disabled] bit, [nologin] bit, [member] bit, [order] int)
+AS
+BEGIN
+ DECLARE @auth smallint = [dbo].[fis_admin_getUserAuth](@authaccount);
+
+ IF @auth > 1 OR (ISNULL(@useraccount_id, '') <>'' AND @useraccount_id = @authaccount)
+ BEGIN
+ DECLARE @ungrouped bit = CASE WHEN @usergroup_id is null THEN 1 ELSE 0 END; /* if no usergroup is selected => ungrouped */
+ IF @ungrouped = 1 OR EXISTS(SELECT * FROM [dbo].[fis_usergroups] as ug where ug.[usergroup_id] = @usergroup_id AND ug.[DateDeleted] is null) /* Validate selected group, alternatively do not even look for accounts */
+ BEGIN
+ WITH SelectedGroupMembers as (
+ SELECT ug.[usergroup_id], ugm.[useraccount_id]
+ FROM [dbo].[fis_usergroups_members] as ugm
+ JOIN [dbo].[fis_usergroups] as ug ON ugm.[usergroup_id] = ug.[usergroup_id]
+ WHERE ug.[DateDeleted] is null
+ AND ugm.[DateDeleted] is null
+ AND ug.[usergroup_id] = @usergroup_id
+ )
+ INSERT INTO @RET
+ SELECT ua.[useraccount_id]
+ ,'auth' = ISNULL(TRY_CAST((SELECT TOP(1) [value] FROM [fis_useraccounts_settings] as s WHERE s.[useraccount_id] = ua.[useraccount_id] and s.[key] = 'intranet_admin') as smallint), CASE WHEN ua.[useraccount_id] is null THEN -1 WHEN [DateDisabled] is not null THEN 0 ELSE 1 END )
+ ,CAST(DECRYPTBYKEY(ua.[name_enc]) as nvarchar(100))
+ ,CAST(DECRYPTBYKEY(ua.[firstname_enc]) as nvarchar(100))
+ ,CAST(DECRYPTBYKEY(ua.[title_enc]) as varchar(50))
+ ,CAST(DECRYPTBYKEY(ua.[gender_enc]) as varchar(1))
+ ,CAST(DECRYPTBYKEY(ua.[email_enc]) as varchar(255))
+ ,CAST(DECRYPTBYKEY(ua.[mobile_enc]) as varchar(50))
+ ,'functions' = STUFF(
+ (SELECT ',' + [value] as [text()] FROM [fis_useraccounts_settings] as s WHERE s.[useraccount_id] = ua.[useraccount_id] and s.[key] = 'intranet_function' FOR XML PATH(''), TYPE).value('.','nvarchar(max)')
+ , 1,1,'')
+ ,ua.[language]
+ ,'disabled' = CASE WHEN ua.[DateDisabled] IS NULL THEN 0 ELSE 1 END
+ ,'nologin' = CAST(CASE WHEN ua.[password_enc] is null THEN 1 WHEN ISNULL(CAST(DECRYPTBYKEY(ua.[password_enc]) as varchar(20)),'') = '' THEN 1 ELSE 0 END as bit)
+ ,'member' = CASE WHEN @usergroup_id IS NULL THEN NULL WHEN sgm.[useraccount_id] is not null THEN 1 ELSE 0 END
+ ,'order' = ROW_NUMBER() OVER (ORDER BY CAST(DECRYPTBYKEY(ua.[name_enc]) as nvarchar(255)))
+ FROM [dbo].[fis_useraccounts] as ua
+ LEFT JOIN SelectedGroupMembers as sgm on sgm.[useraccount_id] = ua.[useraccount_id]
+ WHERE ua.[DateDeleted] is null /*never return deleted useraccounts */
+ AND (@useraccount_id is null OR (ua.[useraccount_id] = @useraccount_id)) /* if single useraccount is selected, return only this selected one */
+ AND (@ungrouped = 1 -- is no useraccount is selected
+ OR ISNULL(@include_nonmembers,0) = 1 -- or if useraccount is selected, but non-members should be included also
+ OR sgm.[useraccount_id] is not null) -- or if accounts are valid members
+ END
+ END
+
+ RETURN;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserAccount_Namelist.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserAccount_Namelist.sql
new file mode 100644
index 0000000..2f69c38
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserAccount_Namelist.sql
@@ -0,0 +1,36 @@
+
+
+
+
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserAccount_Namelist]
+(
+ @authaccount varchar(5)
+)
+RETURNS @RET TABLE ( [useraccount_id] varchar(5), [name] nvarchar(100) null, [firstname] nvarchar(100) null, [order] int)
+AS
+BEGIN
+ DECLARE @auth smallint = [dbo].[fis_admin_getUserAuth](@authaccount);
+
+ IF @auth > 1
+ BEGIN
+
+ INSERT INTO @RET
+ SELECT ua.[useraccount_id]
+ ,CAST(DECRYPTBYKEY(ua.[name_enc]) as nvarchar(100))
+ ,CAST(DECRYPTBYKEY(ua.[firstname_enc]) as nvarchar(100))
+ ,'order' = ROW_NUMBER() OVER (ORDER BY CAST(DECRYPTBYKEY(ua.[name_enc]) as nvarchar(255)))
+ FROM [dbo].[fis_useraccounts] as ua
+ WHERE ua.[DateDeleted] is null;
+
+ END
+
+ RETURN;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserAccount_byemail.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserAccount_byemail.sql
new file mode 100644
index 0000000..7c09c56
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserAccount_byemail.sql
@@ -0,0 +1,66 @@
+
+
+
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserAccount_byemail]
+(
+ @email varchar(255)
+ ,@include_password bit = 0
+)
+RETURNS @RET TABLE ( [useraccount_id] varchar(5), [auth] smallint, [name] nvarchar(100) null, [firstname] nvarchar(100) null, [title] varchar(50) null,
+ [gender] varchar(1) NULL, [email] varchar(255) NULL, [mobile] varchar(50) null,
+ [password] nvarchar(25) null, [functions] varchar(1000), [language] varchar(2)
+ , [fullname] nvarchar(255) null, [fullname_rev] nvarchar(255) null
+ )
+AS
+BEGIN
+ DECLARE @auth smallint = -1;
+ DECLARE @useraccount_id varchar(5), @name nvarchar(100), @firstname nvarchar(100), @mobile varchar(50), @functions varchar(1000) = '', @title varchar(50), @gender varchar(1), @password varchar(25), @language varchar(2), @fullname nvarchar(255), @fullname_rev nvarchar(255);
+
+ SELECT TOP(1) @useraccount_id = [useraccount_id]
+ ,@auth = CASE WHEN [useraccount_id] is null THEN -1 ELSE 1 END
+ ,@name = CAST(DECRYPTBYKEY(ua.[name_enc]) as nvarchar(100))
+ ,@firstname = CAST(DECRYPTBYKEY(ua.[firstname_enc]) as nvarchar(100))
+ ,@title = CAST(DECRYPTBYKEY(ua.[title_enc]) as varchar(50))
+ ,@gender = CAST(DECRYPTBYKEY(ua.[gender_enc]) as varchar(1))
+ ,@password = IIF(ISNULL(@include_password,0) = 1, CAST(DECRYPTBYKEY(ua.[password_enc]) as nvarchar(25)) , null)
+ ,@email = CAST(DECRYPTBYKEY(ua.[email_enc]) as varchar(255))
+ ,@mobile = CAST(DECRYPTBYKEY(ua.[mobile_enc]) as varchar(50))
+ ,@language = ua.[language]
+ ,@fullname = [dbo].[fis_admin_getUserName](ua.[name_enc], ua.[firstname_enc], ua.[title_enc])
+ ,@fullname_rev = [dbo].[fis_admin_getUserName_rev](ua.[name_enc], ua.[firstname_enc], ua.[title_enc])
+ FROM [dbo].[fis_useraccounts] as ua
+ WHERE ua.[DateDeleted] is null AND ua.[DateDisabled] is null
+ AND LOWER(RTRIM(CAST(DECRYPTBYKEY(ua.[email_enc]) as varchar(255)))) = LOWER(RTRIM(@email));
+
+ SET @auth = ISNULL(TRY_CAST((SELECT TOP(1) [value] FROM [fis_useraccounts_settings] WHERE [useraccount_id] = @useraccount_id and [key] = 'intranet_admin') as smallint), @auth);
+ SET @functions = STUFF(
+ (SELECT ',' + [value] as [text()] FROM [fis_useraccounts_settings] WHERE [useraccount_id] = @useraccount_id and [key] = 'intranet_function' FOR XML PATH(''), TYPE).value('.','nvarchar(max)')
+ , 1,1,'');
+
+ INSERT INTO @RET
+ VALUES(
+ @useraccount_id
+ ,IIF(@useraccount_id is null, -1, @auth)
+ ,IIF(@useraccount_id is null, null, @name)
+ ,IIF(@useraccount_id is null, null, @firstname)
+ ,IIF(@useraccount_id is null, null, @title)
+ ,IIF(@useraccount_id is null, null, @gender)
+ ,IIF(@useraccount_id is null, null, @email)
+ ,IIF(@useraccount_id is null, null, @mobile)
+ ,IIF(@useraccount_id is null, null, @password)
+ ,IIF(@useraccount_id is null, null, @functions)
+ ,IIF(@useraccount_id is null, null, @language)
+ ,IIF(@useraccount_id is null, null, @fullname)
+ ,IIF(@useraccount_id is null, null, @fullname_rev)
+ );
+
+ RETURN;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserAddressForm.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserAddressForm.sql
new file mode 100644
index 0000000..6e75842
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserAddressForm.sql
@@ -0,0 +1,33 @@
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserAddressForm]
+(
+ @name_enc varbinary(260)
+ ,@title_enc varbinary(50)
+ ,@gender_enc varbinary(100)
+ ,@language varchar(2)
+)
+RETURNS nvarchar(255)
+AS
+BEGIN
+ DECLARE @Username nvarchar(255);
+ DECLARE @l varchar(2) = LOWER(ISNULL(@language, 'de'));
+ DECLARE @g varchar(1) = LOWER(ISNULL(CAST(DECRYPTBYKEY(@gender_enc) as varchar(1)), 'm'));
+
+ SET @Username = CAST( CASE WHEN @l = 'de' THEN 'Sehr geehrte' + CASE WHEN @g = 'm' THEN 'r Herr ' ELSE ' Frau ' END
+ ELSE 'Dear ' + CASE WHEN @g = 'm' THEN 'Mr ' ELSE ' Ms ' END
+ END
+
+ + ISNULL(CASE WHEN @title_enc is null THEN '' ELSE CAST(DECRYPTBYKEY(@title_enc) as varchar(50)) + ' ' END,'')
+ + ISNULL(CASE WHEN @name_enc is null THEN '' ELSE CAST(DECRYPTBYKEY(@name_enc ) as nvarchar(100)) END, '')
+ as nvarchar(255)
+ );
+
+ RETURN @username;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserAuth.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserAuth.sql
new file mode 100644
index 0000000..b49ca50
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserAuth.sql
@@ -0,0 +1,23 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserAuth]
+(
+ @useraccount_id varchar(5)
+)
+RETURNS smallint
+AS
+BEGIN
+ DECLARE @auth smallint = -1;
+
+ SET @auth = ISNULL((SELECT TOP(1)
+ ISNULL(TRY_CAST((SELECT TOP(1) [value] FROM [fis_useraccounts_settings] as s WHERE s.[useraccount_id] = ua.[useraccount_id] and s.[key] = 'intranet_admin') as smallint), CASE WHEN [useraccount_id] is null THEN -1 WHEN [DateDisabled] is not null THEN 0 ELSE 1 END )
+ FROM [dbo].[fis_useraccounts] as ua
+ WHERE ua.[DateDeleted] is null
+ AND ua.[useraccount_id] = @useraccount_id), -1);
+
+ RETURN @auth;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserEmail_byID.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserEmail_byID.sql
new file mode 100644
index 0000000..9617f50
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserEmail_byID.sql
@@ -0,0 +1,24 @@
+
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserEmail_byID]
+(
+ @useraccount_id varchar(5)
+)
+RETURNS nvarchar(255)
+AS
+BEGIN
+ DECLARE @UserEmail varchar(255);
+
+ SELECT TOP(1) @UserEmail = ISNULL(CASE WHEN [email_enc] is null THEN '' ELSE CAST(DECRYPTBYKEY([email_enc] ) as varchar(255)) END, '')
+ FROM [dbo].[fis_useraccounts] as ua WHERE [useraccount_id] = @useraccount_id AND [DateDeleted] is null;
+
+
+ RETURN @userEmail;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserGroupList.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserGroupList.sql
new file mode 100644
index 0000000..71c0bd4
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserGroupList.sql
@@ -0,0 +1,33 @@
+
+
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserGroupList]
+(
+ @authaccount varchar(5)
+ , @usergroup_id varchar(5)
+)
+RETURNS @RET TABLE ( [usergroup_id] varchar(5), [name] nvarchar(255) null, [order] int)
+AS
+BEGIN
+ DECLARE @auth smallint = [dbo].[fis_admin_getUserAuth](@authaccount);
+
+ IF @auth > 1
+ BEGIN
+ INSERT INTO @RET
+ SELECT [usergroup_id]
+ ,CAST(DECRYPTBYKEY(ug.[name_enc]) as nvarchar(255))
+ ,'order' = ROW_NUMBER() OVER (ORDER BY CAST(DECRYPTBYKEY(ug.[name_enc]) as nvarchar(255)))
+ FROM [dbo].[fis_usergroups] as ug
+ WHERE ug.[DateDeleted] is null
+ AND (@usergroup_id is null OR ug.[usergroup_id] = @usergroup_id);
+ END
+
+ RETURN;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserName.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserName.sql
new file mode 100644
index 0000000..0d68873
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserName.sql
@@ -0,0 +1,27 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserName]
+(
+ @name_enc varbinary(260)
+ ,@firstname_enc varbinary(260)
+ ,@title_enc varbinary(50)
+)
+RETURNS nvarchar(255)
+AS
+BEGIN
+ DECLARE @Username nvarchar(255);
+
+ SET @Username = CAST(
+ ISNULL(CASE WHEN @title_enc is null THEN '' ELSE CAST(DECRYPTBYKEY(@title_enc) as varchar(50)) + ' ' END,'')
+ + ISNULL(CASE WHEN @firstname_enc is null THEN '' ELSE CAST(DECRYPTBYKEY(@firstname_enc) as nvarchar(100)) + ' ' END,'')
+ + ISNULL(CASE WHEN @name_enc is null THEN '' ELSE CAST(DECRYPTBYKEY(@name_enc ) as nvarchar(100)) END, '')
+ as nvarchar(255))
+
+
+ RETURN @username;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserName_byID.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserName_byID.sql
new file mode 100644
index 0000000..9202a6e
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserName_byID.sql
@@ -0,0 +1,23 @@
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserName_byID]
+(
+ @useraccount_id varchar(5)
+)
+RETURNS nvarchar(255)
+AS
+BEGIN
+ DECLARE @Username nvarchar(255);
+
+ SELECT TOP(1) @Username = [dbo].[fis_admin_getUserName]([name_enc],[firstname_enc],[title_enc])
+ FROM [dbo].[fis_useraccounts] as ua WHERE [useraccount_id] = @useraccount_id AND [DateDeleted] is null;
+
+
+ RETURN @username;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserName_rev.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserName_rev.sql
new file mode 100644
index 0000000..b2dcc9f
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserName_rev.sql
@@ -0,0 +1,28 @@
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserName_rev]
+(
+ @name_enc varbinary(260)
+ ,@firstname_enc varbinary(260)
+ ,@title_enc varbinary(50)
+)
+RETURNS nvarchar(255)
+AS
+BEGIN
+ DECLARE @Username nvarchar(255);
+
+ SET @Username = CAST(
+ ISNULL(CASE WHEN @name_enc is null THEN '' ELSE CAST(DECRYPTBYKEY(@name_enc ) as nvarchar(100)) END, '')
+ + N', ' + ISNULL(CASE WHEN @firstname_enc is null THEN '' ELSE CAST(DECRYPTBYKEY(@firstname_enc) as nvarchar(100)) + ' ' END,'')
+
+ as nvarchar(255))
+
+
+ RETURN @username;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserName_rev_byID.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserName_rev_byID.sql
new file mode 100644
index 0000000..3a5724d
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserName_rev_byID.sql
@@ -0,0 +1,24 @@
+
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserName_rev_byID]
+(
+ @useraccount_id varchar(5)
+)
+RETURNS nvarchar(255)
+AS
+BEGIN
+ DECLARE @Username nvarchar(255);
+
+ SELECT TOP(1) @Username = [dbo].[fis_admin_getUserName_rev]([name_enc],[firstname_enc],[title_enc])
+ FROM [dbo].[fis_useraccounts] as ua WHERE [useraccount_id] = @useraccount_id AND [DateDeleted] is null;
+
+
+ RETURN @username;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_getUserSetting.sql b/Fuchs_Database/dbo/Functions/fis_admin_getUserSetting.sql
new file mode 100644
index 0000000..857c127
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_getUserSetting.sql
@@ -0,0 +1,26 @@
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_getUserSetting]
+(
+ @useraccount_id varchar(5)
+ , @key varchar(25)
+)
+RETURNS varchar(255)
+AS
+BEGIN
+ DECLARE @ret varchar(25);
+
+ SET @ret = ISNULL((SELECT TOP(1)
+ ISNULL((SELECT TOP(1) [value] FROM [fis_useraccounts_settings] as s WHERE s.[useraccount_id] = ua.[useraccount_id] and s.[key] = @key), '')
+ FROM [dbo].[fis_useraccounts] as ua
+ WHERE ua.[DateDeleted] is null
+ AND ua.[useraccount_id] = @useraccount_id), '');
+
+ RETURN ISNULL(@ret,'');
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_admin_testGroupMembership.sql b/Fuchs_Database/dbo/Functions/fis_admin_testGroupMembership.sql
new file mode 100644
index 0000000..3c3bc99
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_admin_testGroupMembership.sql
@@ -0,0 +1,29 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_admin_testGroupMembership]
+(
+ @useraccount_id varchar(5)
+ ,@usergroup_id varchar(1000)
+)
+RETURNS bit
+AS
+BEGIN
+ DECLARE @groups TABLE([usergroup_id] varchar(5));
+ INSERT INTO @groups SELECT * from STRING_SPLIT(@usergroup_id,',');
+
+ DECLARE @isMember bit = CASE WHEN EXISTS (SELECT * FROM [dbo].[fis_usergroups] as g
+ JOIN @groups as groupstotest On groupstotest.[usergroup_id] = g.[usergroup_id]
+ JOIN [dbo].[fis_usergroups_members] as gm on g.[usergroup_id] = gm.[usergroup_id] and gm.[DateDeleted] is null AND g.[DateDeleted] is null
+ JOIN [dbo].[fis_useraccounts] as u on gm.[useraccount_id] = u.[useraccount_id]
+ WHERE u.[DateDeleted] is null AND u.[DateDisabled] is null
+ and u.[useraccount_id] = @useraccount_id
+ and g.[DateDeleted] is null
+ )
+ THEN 1 ELSE 0 END;
+
+ RETURN @isMember;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_fn_id.sql b/Fuchs_Database/dbo/Functions/fis_fn_id.sql
new file mode 100644
index 0000000..4c5ac55
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_fn_id.sql
@@ -0,0 +1,43 @@
+
+
+
+
+
+
+
+CREATE FUNCTION [dbo].[fis_fn_id] (
+ @table varchar(50)
+)
+RETURNS varchar(50)
+AS
+BEGIN
+ DECLARE @NewAccount varchar(10) = null;
+ SET @table = ISNULL(@table, '');
+
+ IF @table = 'fis_usergroups'
+ BEGIN
+ SELECT @NewAccount = [dbo].[ocms_fn_generatePassword] (5,0,1,0);
+ WHILE EXISTS( SELECT 0 FROM [dbo].[fis_usergroups] WITH (SERIALIZABLE) WHERE [usergroup_id] = @NewAccount)
+ BEGIN
+ SET @NewAccount = [dbo].[ocms_fn_generatePassword] (5,0,1,0);
+ END
+ END
+ ELSE IF @table = 'tmp__fds__invoice_servicerequests'
+ BEGIN
+ SELECT @NewAccount = [dbo].[ocms_fn_generatePassword] (7,0,1,0);
+ WHILE EXISTS( SELECT 0 FROM [dbo].[tmp__fds__invoice_servicerequests] WITH (SERIALIZABLE) WHERE [InvRqId] = @NewAccount)
+ BEGIN
+ SET @NewAccount = [dbo].[ocms_fn_generatePassword] (7,0,1,0);
+ END
+ END
+ ELSE IF @table = 'tmp__fds__bankingtransactions'
+ BEGIN
+ SELECT @NewAccount = [dbo].[ocms_fn_generatePassword] (7,0,1,0);
+ WHILE EXISTS( SELECT 0 FROM [dbo].[tmp__fds__bankingtransactions] WITH (SERIALIZABLE) WHERE [taID] = @NewAccount)
+ BEGIN
+ SET @NewAccount = [dbo].[ocms_fn_generatePassword] (7,0,1,0);
+ END
+ END
+
+ RETURN ISNULL(@NewAccount, [dbo].[ocms_fn_generatePassword] (10,0,1,0));
+END;
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_fn_useraccount_id.sql b/Fuchs_Database/dbo/Functions/fis_fn_useraccount_id.sql
new file mode 100644
index 0000000..5d8184b
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_fn_useraccount_id.sql
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+CREATE FUNCTION [dbo].[fis_fn_useraccount_id] (
+)
+RETURNS varchar(5)
+AS
+BEGIN
+ DECLARE @NewAccount varchar(5) = [dbo].[ocms_fn_generatePassword] (5,0,1,0);
+
+ WHILE EXISTS( SELECT * FROM [dbo].[fis_useraccounts] WHERE [useraccount_id] = @NewAccount)
+ BEGIN
+ SET @NewAccount = [dbo].[ocms_fn_generatePassword] (5,0,1,0);
+ END
+
+ RETURN @NewAccount;
+END;
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_getModuleAuth.sql b/Fuchs_Database/dbo/Functions/fis_getModuleAuth.sql
new file mode 100644
index 0000000..cfe6070
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_getModuleAuth.sql
@@ -0,0 +1,64 @@
+
+
+-- =============================================
+-- Author: Dr. Stefan Ott
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_getModuleAuth]
+(
+ @module varchar(50),
+ @useraccount_id varchar(10) -- length is 5; content would be cut to 5 is variable-length is 5 also
+)
+RETURNS int
+AS
+BEGIN
+ DECLARE @RET int, @TMP int, @module_base varchar(50), @module_qualifier varchar(50);
+ SET @module = ISNULL(@module,'');
+ SET @module_base = CASE WHEN CHARINDEX('-', @module) = 0 THEN @module ELSE LEFT(@module, CHARINDEX('-', @module) -1) END;
+ SET @module_qualifier = CASE WHEN CHARINDEX('-', @module) = 0 THEN '' ELSE RIGHT(@module, LEN(@module) - CHARINDEX('-', @module)) END;
+
+ SET @RET = CASE WHEN @module = '' OR LEFT(@module,1) ='-' -- OR ISNULL(@account, '') IN ('','*'))
+ THEN -3
+ WHEN @useraccount_id = 'sa'
+ THEN 10
+ WHEN NOT EXISTS (SELECT 0 FROM [dbo].[fis_useraccounts] as u WHERE u.[useraccount_id] = @useraccount_id and u.[DateDeleted] is null)
+ THEN -2
+ WHEN EXISTS (SELECT 0 FROM [dbo].[fis_useraccounts] as u WHERE u.[useraccount_id] = @useraccount_id and u.[DateDeleted] is null AND u.[DateDisabled] is not null)
+ THEN -1
+ WHEN @module <> 'fds-admin' AND NOT EXISTS (SELECT 0 from [dbo].[fis_module_auth] as a WHERE a.[module] = @module)
+ THEN 0
+ ELSE NULL END;
+
+
+ IF @RET is null
+ BEGIN
+ WITH maCTE as (
+ SELECT * FROM [dbo].[fis_module_auth] as a_
+ )
+ SELECT @RET = Max(a.[auth])
+ FROM maCTE as a
+ WHERE (
+ ((@module LIKE (a.[module] + '%') OR @module = a.[module]) AND @module <> '')
+ OR a.[module] = 'admin'
+ )
+ AND (
+ (a.[isgroup] = 0 AND a.[group_or_user] = @useraccount_id)
+ OR (a.[isgroup] = 1 AND [dbo].[fis_admin_testGroupMembership](@useraccount_id, a.[group_or_user]) = 1)
+ );
+ END
+
+
+ IF @module_qualifier not in ('manage','admin') AND @RET between -1 and 9 AND @RET between -1 and 9
+ SET @RET = [dbo].[ott_max](@RET, [dbo].[fis_getModuleAuth](@module_base + '-manage', @useraccount_id ));
+
+ IF @module <> 'fds-admin' AND ISNULL(@RET, -1) between -1 and 9
+ SET @RET = [dbo].[ott_max](@RET, [dbo].[fis_getModuleAuth]('fds-admin', @useraccount_id ));
+
+ --SET @RET = CASE WHEN @module = 'fds_inv' and @useraccount_id = 'J5KL8' THEN 1 ELSE @RET END;
+
+ SET @RET = ISNULL(@RET, -3);
+
+ RETURN @RET
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_getModuleAuthList.sql b/Fuchs_Database/dbo/Functions/fis_getModuleAuthList.sql
new file mode 100644
index 0000000..69335bc
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_getModuleAuthList.sql
@@ -0,0 +1,20 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_getModuleAuthList]
+(
+ @module varchar(100)
+ ,@authuser varchar(10)
+)
+RETURNS TABLE
+AS
+RETURN
+(
+ SELECT
+ [module] = [value]
+ , [auth] = [dbo].[fis_getModuleAuth]([value], @authuser)
+ FROM string_split(ISNULL(@module, ''),',')
+ WHERE TRIM([value]) <> ''
+)
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/fis_getONEPersonWidgets.sql b/Fuchs_Database/dbo/Functions/fis_getONEPersonWidgets.sql
new file mode 100644
index 0000000..b64e299
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/fis_getONEPersonWidgets.sql
@@ -0,0 +1,49 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[fis_getONEPersonWidgets]
+(
+ @useraccount_id varchar(10)
+)
+RETURNS @RET TABLE
+(
+ [short_name] [varchar](50) NOT NULL,
+ [account] [varchar](50) NOT NULL,
+ [type] [nvarchar](50) NOT NULL,
+ [name] [nvarchar](255) NOT NULL,
+ [description] [nvarchar](1000) NULL,
+ [sql] [nvarchar](2000) NULL,
+ [sql_admin_columns] [nvarchar](500) NULL,
+ [sql_datatypes] [nvarchar](255) NULL,
+ [url] [nvarchar](500) NULL,
+ [html] [nvarchar](max) NULL,
+ [visible_groups] [varchar](1000) NULL,
+ [visible] [tinyint] NOT NULL,
+ [auth_module] [varchar](50) NULL,
+ [rendering_options] [nvarchar](255) NULL,
+ [server_options] [nvarchar](2000) NULL,
+ [daycache] [bit] NOT NULL,
+ [personalized] [bit] NOT NULL,
+ [safemode] [bit] NOT NULL,
+ [variables] [varchar](1000) NULL,
+ [order] int
+)
+AS
+BEGIN
+ --DECLARE @account varchar(50) = ''; -- kept in function for reuse purposes
+
+
+ INSERT INTO @RET
+ SELECT w.*
+ , ROW_NUMBER() OVER (ORDER BY s.[display_order], s.[DateCreated] DESC, w.[name]) as 'order'
+ FROM [dbo].[fis_widgets] as w JOIN [dbo].[fis_widget_subscriptions] as s ON w.[short_name] = s.[widget_name]
+ WHERE --(s.[account] = @account OR s.[account] = '*')
+ (s.[person_guid] = @useraccount_id OR s.[person_guid] = '*')
+ AND (ISNULL(w.[visible],0) = 0 OR ([dbo].[fis_getModuleAuth](ISNULL(w.[auth_module], 'fis_widgets'), @useraccount_id) >= w.[visible]))
+ --UNION
+ -- SELECT * FROM [dbo].[fis_getONEPersonWidgets_ownevents](@account, @person_guid)
+
+ RETURN
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/json_compareObjectArrays.sql b/Fuchs_Database/dbo/Functions/json_compareObjectArrays.sql
new file mode 100644
index 0000000..16d2433
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/json_compareObjectArrays.sql
@@ -0,0 +1,154 @@
+
+CREATE FUNCTION [dbo].[json_compareObjectArrays] (@SourceJSON NVARCHAR(MAX), @TargetJSON NVARCHAR(MAX), @uniquekey varchar(50) )
+/**
+ Summary:
+ This function 'diffs' a source JSON document with a target JSON document and produces an
+ analysis of which properties are missing in either the source or target, or the values
+ of these properties that are different. It reports on the properties and values for
+ both source and target as well as the path that references that scalar value. The
+ path reference to the object's parent is exposed in the result to enable a query to
+ reference the value of any other object in the parent that is needed.
+ Author: Phil Factor
+ based on Author: Phil Factor (06/07/2020)
+ Date: 06/30/2023
+
+ Returns: >
+ equal: 1 = equal, 0 not equal
+ SideIndicator: ( == equal, <- not in target, -> not in source, <> not equal )
+ uid:
+ parent:
+ path: the JSON path used by the SQL JSON functions
+ key: the key field without the path
+ SourceValue: the value IN the SOURCE JSON document
+ TargetValue: the value IN the TARGET JSON document
+
+**/
+RETURNS @returntable TABLE
+ (
+ [equal] [bit],
+ [SideIndicator] CHAR(2), -- == means equal, <- means not in target, -> means not in source, <> means not equal
+ [UID] varchar(100),
+ [parent] VARCHAR(2000), -- the parent object
+ [path] VARCHAR(2000), -- the JSON path used by the SQL JSON functions
+ [key] VARCHAR(255), -- the key field without the path
+ [SourceValue] NVARCHAR(max), -- the value IN the SOURCE JSON document
+ [TargetValue] NVARCHAR(max) -- the value IN the TARGET JSON document
+ )
+AS
+ BEGIN
+
+ IF (IsJson(ISNULL(@SourceJSON, '{}')) = 1 AND IsJson(ISNULL(@TargetJSON, '{}')) = 1) --don't try anything if either json is invalid
+ BEGIN
+ DECLARE @map TABLE --these contain all properties or array elements with scalar values
+ (
+ iteration INT, --the number of times that more arrays or objects were found
+ SourceOrTarget CHAR(1), --is this the source 's' OR the target 't'
+ [UID] varchar(100),
+ [mparent] VARCHAR(2000), --the parent object
+ [mPath] VARCHAR(2000), -- the JSON path to the key/value pair or array element
+ [mOPath] NVARCHAR(2000),
+ [mKey] VARCHAR(255), --the key to the property
+ [mValue] NVARCHAR(MAX),-- the value
+ [mType] INT --the type of value it is
+ );
+ DECLARE @objects TABLE --this contains all the properties with arrays and objects
+ (
+ iteration INT,
+ SourceOrTarget CHAR(1),
+ [UID] varchar(100),
+ [oParent] VARCHAR(2000),
+ [oPath] VARCHAR(2000),
+ [oOPath] VARCHAR(2000),
+ [oKey] NVARCHAR(2000),
+ [oValue] NVARCHAR(MAX),
+ [oType] INT
+ );
+ DECLARE @depth INT = 1; --we start in shallow water
+ DECLARE @HowManyObjectsNext INT = 1, @SourceType INT, @TargetType INT;
+
+ SELECT --firstly, we try to work out if the source is an array or object
+ @SourceType =
+ CASE IsNumeric((SELECT TOP 1 [key] FROM OpenJson(@SourceJSON)))
+ WHEN 1 THEN 4 ELSE 5 END,
+ @TargetType= --and if the target is an array or object
+ CASE IsNumeric((SELECT TOP 1 [key] FROM OpenJson(@TargetJSON)))
+ WHEN 1 THEN 4 ELSE 5 END
+ --now we insert the base objects or arrays into the object table
+ INSERT INTO @objects (iteration, SourceOrTarget, [oParent], [oPath], [oOPath], [oKey], [oValue], [oType])
+ SELECT 0, 's' AS SourceOrTarget,'' AS [oParent], [oPath] = '$', [oOPath] = '$', [oKey] = '', @SourceJSON, @SourceType;
+ INSERT INTO @objects (iteration, SourceOrTarget, [oParent], [oPath], [oOPath], [oKey], [oValue], [oType])
+ SELECT 0, 't' AS SourceOrTarget, '' AS [oParent], [oPath] = '$', [oOPath] = '$', [oKey] = '', @TargetJSON, @TargetType;
+
+ --we now set the depth and how many objects are in the next iteration
+ SELECT @depth = 0, @HowManyObjectsNext = 2;
+
+ WHILE @HowManyObjectsNext > 0 AND @depth < 2
+ BEGIN
+ INSERT INTO @map --get the scalar values into the @map table
+ (iteration, SourceOrTarget, [UID], [mParent], [mPath], [mOPath], [mKey], [mValue], [mType])
+ SELECT --
+ [iteration] = o.[iteration] + 1
+ , SourceOrTarget
+ , [UID] = [UID]
+ , [mParent] = [oPath]
+ , [mPath] = [oPath] + CASE [otype] WHEN 4 THEN '[' + [Key] + ']' ELSE '.' + [key] END
+ , [mOPath] = [oOPath] + CASE [otype] WHEN 4 THEN '[' + [Key] + ']' ELSE '.' + [key] END
+ , [mkey] = [key]
+ , [mvalue] = [value]
+ , [mtype] = [type]
+ FROM @objects AS o
+ CROSS APPLY OpenJson([oValue]) as j
+ WHERE j.[type] IN (0, 1, 2, 3) AND o.[iteration] = @depth;
+
+ --now we do the same for the objects and arrays
+ INSERT INTO @objects (iteration, SourceOrTarget, [UID], [oParent], [oPath], [oOPath], [oKey], [oValue], [oType])
+ SELECT
+ [iteration] = o.[iteration] + 1
+ , [SourceOrTarget] = SourceOrTarget
+ , [UID] = JSON_VALUE(j.[Value], '$.' + @uniquekey)
+ , [oParent] = [oPath]
+ , [oPath] = [oPath] + CASE [oType] WHEN 4 THEN '[' + JSON_VALUE(j.[Value], '$.' + @uniquekey) + ']' ELSE '.' + [key] END
+ , [oOPath] = [oOPath] + CASE [oType] WHEN 4 THEN '[' + j.[key] + ']' ELSE '.' + j.[key] END
+ , [oKey] = [key]
+ , [oValue] = [value]
+ , [oType] = [type]
+ FROM @objects o
+ CROSS APPLY OpenJson([oValue]) as j
+ WHERE j.[type] IN (4,5) AND o.[iteration] = @depth;
+
+ SELECT @HowManyObjectsNext = @@RowCount --how many objects or arrays?
+ SELECT @depth = @depth + 1; --and so to the next depth maybe
+
+ END; --while
+
+ --now we just do a full join on the columns we are comparing and work out the comparison
+ INSERT INTO @returntable
+ SELECT
+ --first we work out the side-indicator that summarises the comparison
+ [equal] = CASE WHEN src.[UID] is null OR tgt.[UID] is null THEN 0
+ WHEN src.[mValue] is NULL AND tgt.[mValue] is null THEN 1
+ WHEN src.[mValue] = tgt.[mValue] THEN 1
+ ELSE 0
+ END
+ , [Sideindicator] = CASE WHEN src.[UID] is null AND tgt.[UID] IS NOT null THEN '->'
+ WHEN src.[UID] is NOT null AND tgt.[UID] IS null THEN '<-'
+ WHEN src.[mValue] is NULL AND tgt.[mValue] is null THEN '=='
+ WHEN src.[mValue] = tgt.[mValue] THEN '=='
+ ELSE IIF(src.[mPath] IS NULL, '-', '<') + IIF(tgt.[mPath] IS NULL, '-', '>')
+ END
+ --these columns could be in either table
+ , [UID] = Coalesce(src.[UID], tgt.[UID])
+ , [parent] = Coalesce(src.[mParent], tgt.[mParent])
+ , [path] = Coalesce(src.[mOPath], tgt.[mOPath])
+ , [key] = Coalesce(src.[mKey], tgt.[mKey])
+ , [sourceValue] = src.[mValue]
+ , [targetValue] = tgt.[mValue]
+ FROM
+ (SELECT [UID], [mParent], [mPath], [mOPath], [mKey], [mValue] FROM @map WHERE SourceOrTarget = 's') AS src -- the source scalar literals
+ FULL OUTER JOIN
+ (SELECT [UID], [mParent], [mPath], [mOPath], [mKey], [mValue] FROM @map WHERE SourceOrTarget = 't') AS tgt --the target scalar literals
+ ON src.[mPath] = tgt.[mPath]
+ ORDER BY [path];
+ END;
+ RETURN;
+ END;
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/json_diffObjectArrays.sql b/Fuchs_Database/dbo/Functions/json_diffObjectArrays.sql
new file mode 100644
index 0000000..ce8d8cf
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/json_diffObjectArrays.sql
@@ -0,0 +1,112 @@
+
+CREATE FUNCTION [dbo].[json_diffObjectArrays] (@SourceJSON NVARCHAR(MAX), @TargetJSON NVARCHAR(MAX), @uniquekey varchar(50) )
+/**
+Summary: >
+ This function 'diffs' a source JSON document with a target JSON document and produces an
+ analysis of which properties are missing in either the source or target, or the values
+ of these properties that are different. It reports on the properties and values for
+ both source and target as well as the path that references that scalar value. The
+ path reference to the object's parent is exposed in the result to enable a query to
+ reference the value of any other object in the parent that is needed.
+Author: Phil Factor
+Date: 06/07/2020
+Revised:
+ - mod: Added the parent reference to the difference report
+ - Date: 09/07/2020
+Database: PhilsRoutines
+Examples:
+ - SELECT * FROM dbo.Compare_JsonObject(@TheSourceJSON, @TheTargetJSON)
+ WHERE SideIndicator <> '==';
+ - SELECT *, Json_Value(@TheSourceJSON,TheParent+'.name')
+ FROM dbo.Compare_JsonObject(@TheSourceJSON, @TheTargetJSON)
+ WHERE SideIndicator <> '==';
+Returns: >
+ SideIndicator: ( == equal, <- not in target, -> not in source, <> not equal
+ ThePath: the JSON path used by the SQL JSON functions
+ TheKey: the key field without the path
+ TheSourceValue: the value IN the SOURCE JSON document
+ TheTargetValue: the value IN the TARGET JSON document
+
+**/
+RETURNS @returntable TABLE
+ (
+ [equal] [bit],
+ [action] smallint,
+ [uid] varchar(100),
+ [diff] nvarchar(max)
+ )
+AS
+ BEGIN
+
+ DECLARE @objects TABLE (
+ [uid] varchar(100),
+ [json] NVARCHAR(MAX)
+ )
+ INSERT INTO @objects ([uid], [json])
+ SELECT JSON_VALUE(j.[Value], '$.' + @uniquekey), [value]
+ FROM OpenJson(@TargetJSON) as j
+ WHERE type IN (4,5);
+
+ DECLARE @intermediate TABLE
+ (
+ [equal] [bit],
+ [SideIndicator] CHAR(2), -- == means equal, <- means not in target, -> means not in source, <> means not equal
+ [UID] varchar(100),
+ [parent] VARCHAR(2000), -- the parent object
+ [path] VARCHAR(2000), -- the JSON path used by the SQL JSON functions
+ [key] VARCHAR(255), -- the key field without the path
+ [SourceValue] NVARCHAR(max), -- the value IN the SOURCE JSON document
+ [TargetValue] NVARCHAR(max) -- the value IN the TARGET JSON document
+ )
+ INSERT INTO @intermediate
+ SELECT * FROM [dbo].[json_compareObjectArrays](@SourceJSON, @TargetJSON, @uniquekey);
+
+ WITH d as(
+ SELECT
+ [UID]
+ , [total] = SUM(1)
+ , [LEFT] = SUM(IIF([sideindicator] = '<-',1,0))
+ , [RIGHT] = SUM(IIF([sideindicator] = '->',1,0))
+ , [inequal] = SUM(IIF(i.[equal] = 1,0,1))
+ FROM @intermediate as i
+ GROUP BY [UID]
+ )
+ INSERT INTO @returntable
+ SELECT
+ [equal] = IIF([inequal] = 0, 1 ,0)
+ , CASE WHEN [total] = [RIGHT] THEN 2
+ WHEN [total] = [LEFT] THEN -1
+ WHEN [inequal] > 0 THEN 1
+ ELSE 0 END
+ , ISNULL(o.[uid], d.[UID])
+ , [j] = IIF([inequal] = 0, '{}' , --( SELECT [key] = im.[theKey], [val] = im.[TheTargetValue] FROM @intermediate as im WHERE im.[UID] = d.[UID] AND im.equal = 0 FOR JSON PATH, WITHOUT_ARRAY_WRAPPER )
+ o.[json]
+ )
+ FROM d
+ FULL OUTER JOIN @objects as o ON d.[UID] = o.[uid]
+
+ DECLARE @uid varchar(100), @key varchar(100);
+
+ DECLARE im_cursor CURSOR FOR
+ SELECT [uid], [key]
+ FROM @intermediate
+ WHERE [SideIndicator] in ('==');
+
+ OPEN im_cursor
+ FETCH NEXT FROM im_cursor INTO @uid, @key;
+
+ WHILE @@FETCH_STATUS = 0
+ BEGIN
+ UPDATE o
+ SET o.[diff] = JSON_MODIFY(o.[diff], '$.' + @key, null)
+ FROM @returntable as o
+ WHERE [uid] = @uid;
+
+ FETCH NEXT FROM im_cursor INTO @uid, @key;
+ END
+
+ CLOSE im_cursor ;
+ DEALLOCATE im_cursor;
+
+ RETURN;
+ END;
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/mfr__schema.sql b/Fuchs_Database/dbo/Functions/mfr__schema.sql
new file mode 100644
index 0000000..60161ca
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/mfr__schema.sql
@@ -0,0 +1,1188 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[mfr__schema]
+(
+
+)
+RETURNS XML
+AS
+BEGIN
+ DECLARe @AS XML;
+SET @AS = N'
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ';
+
+ RETURN @as;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/mfr_fn_dateSent_fromNotes.sql b/Fuchs_Database/dbo/Functions/mfr_fn_dateSent_fromNotes.sql
new file mode 100644
index 0000000..284bb80
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/mfr_fn_dateSent_fromNotes.sql
@@ -0,0 +1,18 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[mfr_fn_dateSent_fromNotes]
+(
+ @notes nvarchar(500)
+)
+RETURNS date
+AS
+BEGIN
+ DECLARE @DateSent date = TRY_PARSE((SELECT '20'+STRING_AGG(TRIM([string]), '-') WITHIN GROUP(ORDER BY [rowno] DESC) FROM dbo.string_SplitString_ordered((SELECT REPLACE(REPLACE(TRIM([value]), CHAR(10),''), CHAR(13),'') from string_split(@notes,':') where CHARINDEX('@',[value]) = 0 AND [value] like '%[-]__[-]21'),'-') WHERE ISNULL([string],'') <> '') as date)
+
+ RETURN @DateSent
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ne.sql b/Fuchs_Database/dbo/Functions/ne.sql
new file mode 100644
index 0000000..a09b7ec
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ne.sql
@@ -0,0 +1,77 @@
+
+
+-- =============================================
+-- Author: Dr. Stefan Ott
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ne]
+(
+ @value SQL_VARIANT
+ ,@alternative SQL_VARIANT
+)
+RETURNS SQL_VARIANT
+AS
+BEGIN
+ DECLARE @ret SQL_VARIANT;
+
+ IF SQL_VARIANT_PROPERTY(@value, 'BaseType') = 'varchar'
+ BEGIN
+
+ If SQL_VARIANT_PROPERTY(@value, 'MaxLength') <= 50
+ BEGIN
+ DECLARE @v1 varchar(50) = ISNULL(try_cast(@value as varchar(50)), '');
+ SET @ret = IIF( @v1 <> '', @v1, try_CAST(@alternative as varchar(50)));
+ END
+ ELSE If SQL_VARIANT_PROPERTY(@value, 'MaxLength') <= 255
+ BEGIN
+ DECLARE @v2 varchar(255) = ISNULL(try_cast(@value as varchar(255)), '');
+ SET @ret = IIF( @v2 <> '', @v2, try_CAST(@alternative as varchar(255)));
+ END
+ ELSE If SQL_VARIANT_PROPERTY(@value, 'MaxLength') <= 1000
+ BEGIN
+ DECLARE @v3 varchar(1000) = ISNULL(try_cast(@value as varchar(1000)), '');
+ SET @ret = IIF( @v2 <> '', @v2, try_CAST(@alternative as varchar(1000)));
+ END
+ ELSE If SQL_VARIANT_PROPERTY(@value, 'MaxLength') <= 4000
+ BEGIN
+ DECLARE @v4 varchar(4000) = ISNULL(try_cast(@value as varchar(4000)), '');
+ SET @ret = IIF( @v4 <> '', @v4, try_CAST(@alternative as varchar(4000)));
+ END
+ ELSE
+ BEGIN
+ DECLARE @v5 varchar(8000) = ISNULL(try_cast(@value as varchar(8000)), '');
+ SET @ret = IIF( @v5 <> '', @v4, try_CAST(@alternative as varchar(8000)));
+ END
+
+
+ END
+ ELSE IF SQL_VARIANT_PROPERTY(@value, 'BaseType') = 'nvarchar'
+ BEGIN
+
+ If SQL_VARIANT_PROPERTY(@value, 'MaxLength') <= 50
+ BEGIN
+ DECLARE @nv1 nvarchar(50) = ISNULL(try_cast(@value as nvarchar(50)), '');
+ SET @ret = IIF(@nv1 <> '', @nv1, try_CAST(@alternative as nvarchar(50)));
+ END
+ ELSE If SQL_VARIANT_PROPERTY(@value, 'MaxLength') <= 255
+ BEGIN
+ DECLARE @nv2 nvarchar(255) = ISNULL(try_cast(@value as nvarchar(255)), '');
+ SET @ret = IIF(@nv2 <> '',@nv2, try_CAST(@alternative as nvarchar(255)));
+ END
+ ELSE If SQL_VARIANT_PROPERTY(@value, 'MaxLength') <= 1000
+ BEGIN
+ DECLARE @nv3 nvarchar(1000) = ISNULL(try_cast(@value as nvarchar(1000)), '');
+ SET @ret = IIF(@nv2 <> '',@nv2, try_CAST(@alternative as nvarchar(1000)));
+ END
+ ELSE
+ BEGIN
+ DECLARE @nv4 nvarchar(4000) = ISNULL(try_cast(@value as nvarchar(4000)), '');
+ SET @ret = IIF(@nv4 <> '',@nv4, try_CAST(@alternative as nvarchar(4000)));
+ END
+
+
+ END
+
+ RETURN @ret
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/net_val.sql b/Fuchs_Database/dbo/Functions/net_val.sql
new file mode 100644
index 0000000..28e4b4a
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/net_val.sql
@@ -0,0 +1,21 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[net_val]
+(
+ @price numeric(10,3)
+ ,@quantityhours numeric(10,3)
+ ,@discount numeric(10,3)
+)
+RETURNS numeric(10,2)
+AS
+BEGIN
+ DECLARE @Ret numeric(10,2)
+
+ SET @RET = ISNULL(@Price,0) * ISNULL(@quantityhours,1) * (100 - ISNULL(@discount,0)) * 0.01;
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_20_randomStrings.sql b/Fuchs_Database/dbo/Functions/ott_20_randomStrings.sql
new file mode 100644
index 0000000..8e5297d
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_20_randomStrings.sql
@@ -0,0 +1,27 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_20_randomStrings]
+(
+ @digits tinyint
+)
+RETURNS @RET TABLE
+(
+ [s] varchar(5)
+)
+AS
+BEGIN
+
+ DECLARE @new_sessionid varchar(36), @trys int, @chars varchar(100), @UPPER int, @LOWER int;
+ SELECT @chars = '012KLMN34STUVWEFGHIJXYZ567OPQR89ABCD';
+ SELECT @UPPER = LEN(@chars), @LOWER = 1;
+
+ INSERT INTO @RET
+ SELECT * FROM (SELECT TOP(20) SUBSTRING(SUBSTRING(@chars, [dbo].[ott_random_int](@UPPER,@LOWER), 1) + SUBSTRING(@chars, [dbo].[ott_random_int](@UPPER,@LOWER), 1) + SUBSTRING(@chars, [dbo].[ott_random_int](@UPPER,@LOWER), 1) + SUBSTRING(@chars, [dbo].[ott_random_int](@UPPER,@LOWER), 1) + SUBSTRING(@chars, [dbo].[ott_random_int](@UPPER,@LOWER), 1) + SUBSTRING(@chars, [dbo].[ott_random_int](@UPPER,@LOWER), 1) + SUBSTRING(@chars, [dbo].[ott_random_int](@UPPER,@LOWER), 1) + SUBSTRING(@chars, [dbo].[ott_random_int](@UPPER,@LOWER), 1) + SUBSTRING(@chars, [dbo].[ott_random_int](@UPPER,@LOWER), 1) + SUBSTRING(@chars, [dbo].[ott_random_int](@UPPER,@LOWER), 1), 1, @digits) as id FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20)) vl(id))z WHERE LEN(id) = @digits;
+
+
+ RETURN
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_b10.sql b/Fuchs_Database/dbo/Functions/ott_b10.sql
new file mode 100644
index 0000000..651cc29
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_b10.sql
@@ -0,0 +1,43 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_b10]
+(
+ @string varchar(10)
+)
+RETURNS int
+AS
+BEGIN
+ DECLARE @RET int;
+
+
+ DECLARE @str VARCHAR(10) = REVERSE(LOWER(@string));
+ WITH Split(stpos,endpos) AS(
+ SELECT 1 AS stpos, 2 AS endpos
+ UNION ALL
+ SELECT endpos, endpos+1
+ FROM Split
+ WHERE endpos <= LEN(@str)
+ )
+ , c as (
+ SELECT
+ 'character' = SUBSTRING(@str,stpos,COALESCE(NULLIF(endpos,0),LEN(@str)+1)-stpos)
+ ,'charindex' = stpos
+ FROM Split
+ )
+ , s as (
+ SELECT
+ *
+ ,'num' = ASCII(character) - ASCII('a') + 1
+ ,'fac' = POWER(26,([charindex] -1))
+ ,'val' = POWER(26,([charindex] -1)) * (ASCII(character) - ASCII('a') + 1)
+ FROM c
+ )
+ select @RET = SUM([val]) FROM s;
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_b26.sql b/Fuchs_Database/dbo/Functions/ott_b26.sql
new file mode 100644
index 0000000..444f803
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_b26.sql
@@ -0,0 +1,38 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_b26]
+(
+ @number int
+)
+RETURNS varchar(10)
+AS
+BEGIN
+ DECLARE @RET varchar(10)
+
+ /*
+ SELECT
+ *,
+ LetterCode =
+ Coalesce((SELECT Char(65 + (N.Num - 475255) / 456976 % 26) WHERE N.Num >= 475255), '')
+ + Coalesce((SELECT Char(65 + (N.Num - 18279) / 17576 % 26) WHERE N.Num >= 18279), '')
+ + Coalesce((SELECT Char(65 + (N.Num - 703) / 676 % 26) WHERE N.Num >= 703), '')
+ + Coalesce((SELECT Char(65 + (N.Num - 27) / 26 % 26) WHERE N.Num >= 27), '')
+ + (SELECT Char(65 + (N.Num - 1) % 26))
+ FROM (VALUES(18931)) as N(Num)
+ ORDER BY N.Num
+ */
+
+ SET @RET = LOWER(
+ Coalesce((SELECT Char(65 + (@number - 475255) / 456976 % 26) WHERE @number >= 475255), '')
+ + Coalesce((SELECT Char(65 + (@number - 18279) / 17576 % 26) WHERE @number >= 18279), '')
+ + Coalesce((SELECT Char(65 + (@number - 703) / 676 % 26) WHERE @number >= 703), '')
+ + Coalesce((SELECT Char(65 + (@number - 27) / 26 % 26) WHERE @number >= 27), '')
+ + (SELECT Char(65 + (@number - 1) % 26))
+ );
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_csv_contains.sql b/Fuchs_Database/dbo/Functions/ott_csv_contains.sql
new file mode 100644
index 0000000..a696bc6
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_csv_contains.sql
@@ -0,0 +1,30 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_csv_contains]
+(
+ @text varchar(500)
+ ,@fragment varchar(500)
+)
+RETURNS bit
+AS
+BEGIN
+ DECLARE @RET bit;
+
+ IF ISNULL(@text, '') = '' OR ISNULL(@fragment, '') = ''
+ SET @REt = 0;
+ ELSE
+ BEGIN
+ -- Will be true
+ -- if any of the fragments exist in the list of text-items
+
+ WITH texts as (SELECT * FROM string_split(ISNULL(@text,''), ','))
+ ,fragments as (SELECT * FROM string_split(ISNULL(@fragment,''), ','))
+ SELECT @RET = CASE WHEN EXISTS (SELECT * FROM texts JOIN fragments ON texts.[value] = fragments.[value]) THEN 1 ELSE 0 END;
+ END;
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_jcontent.sql b/Fuchs_Database/dbo/Functions/ott_jcontent.sql
new file mode 100644
index 0000000..2ecf1f0
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_jcontent.sql
@@ -0,0 +1,36 @@
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_jcontent]
+(
+ @groupcode varchar(10)
+ ,@text varchar(4000)
+)
+RETURNS varchar(4000)
+AS
+BEGIN
+ DECLARE @RET varchar(4000);
+ DECLARE @textinput varchar(4000) = REPLACE(ISNULL(@text, ''), ' ', '');
+
+ IF (@textinput = '' OR @textinput = '{}')
+ BEGIN
+ SET @RET = 0;
+ END
+ ELSE
+ BEGIN
+
+ DECLARE @Pattern varchar(20) = '%"' + @groupcode + '":"%';
+ DECLARE @P0 int = PATINDEX(@pattern, @textinput);
+ DECLARE @P1 int = @P0 + LEN(@pattern) -2;
+ DECLARE @P2 int = CHARINDEX('"',@textinput,@p1);
+
+ SELECT @RET = CASE WHEN @P0 = 0 THEN '' ELSE SUBSTRING(@textinput, @P1, @P2 - @P1) END;
+ END
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_jcsv_contains.sql b/Fuchs_Database/dbo/Functions/ott_jcsv_contains.sql
new file mode 100644
index 0000000..c8dee5a
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_jcsv_contains.sql
@@ -0,0 +1,52 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_jcsv_contains]
+(
+ @groupcode varchar(10)
+ ,@text varchar(4000)
+ ,@fragment varchar(500)
+)
+RETURNS bit
+AS
+BEGIN
+ DECLARE @RET bit;
+ DECLARE @textinput varchar(4000) = REPLACE(ISNULL(@text, ''), ' ', '');
+
+ IF (@textinput = '' OR @textinput = '{}' OR @fragment = '')
+ BEGIN
+ SET @RET = 0;
+ END
+ ELSE
+ BEGIN
+
+ -- Will be true
+ -- if any of the fragments exist in the list of text-items
+
+ DECLARE @Pattern varchar(20) = '%"' + @groupcode + '":"%';
+ DECLARE @P0 int = PATINDEX(@pattern, @textinput);
+ DECLARE @P1 int = @P0 + LEN(@pattern) -2;
+ DECLARE @P2 int = CHARINDEX('"',@textinput,@p1);
+
+ IF @P0 = 0
+ BEGIN
+ SET @RET = 0;
+
+ END
+ ELSE
+ BEGIN
+ DECLARE @text2 varchar(4000) = SUBSTRING(@textinput, @P1, @P2 - @P1);
+
+ WITH texts as (SELECT * FROM string_split(ISNULL(@text2,''), ','))
+ ,fragments as (SELECT * FROM string_split(ISNULL(@fragment,''), ','))
+ SELECT @RET = CASE WHEN EXISTS (SELECT * FROM texts JOIN fragments ON texts.[value] = fragments.[value]) THEN 1 ELSE 0 END;
+ END
+
+ END
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_jcsv_containskey.sql b/Fuchs_Database/dbo/Functions/ott_jcsv_containskey.sql
new file mode 100644
index 0000000..59d6521
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_jcsv_containskey.sql
@@ -0,0 +1,38 @@
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_jcsv_containskey]
+(
+ @groupcode varchar(10)
+ ,@text varchar(4000)
+)
+RETURNS bit
+AS
+BEGIN
+ DECLARE @RET bit;
+ DECLARE @textinput varchar(4000) = REPLACE(ISNULL(@text, ''), ' ', '');
+
+ IF (@textinput = '' OR @textinput = '{}')
+ BEGIN
+ SET @RET = 0;
+ END
+ ELSE
+ BEGIN
+
+ -- Will be true
+ -- if any of the fragments exist in the list of text-items
+
+ DECLARE @Pattern varchar(20) = '%"' + @groupcode + '":"%';
+ DECLARE @P0 int = PATINDEX(@pattern, @textinput);
+
+ SET @RET = Case WHEN @P0 = 0 THEN 0 ELSE 1 END;
+
+ END
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_max.sql b/Fuchs_Database/dbo/Functions/ott_max.sql
new file mode 100644
index 0000000..dcb7856
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_max.sql
@@ -0,0 +1,28 @@
+
+-- =============================================
+-- Author: Dr. Stefan Ott
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_max]
+(
+ @val1 int,
+ @val2 int
+)
+RETURNS int
+AS
+BEGIN
+ DECLARE @RES int;
+
+ IF @val1 is null and @val2 is not null
+ SET @val1 = @val2
+ IF @val2 is null and @val1 is not null
+ SET @val2 = @val1
+ IF @val1 is null and @val2 is null
+ SELECT @val1 = 0, @val2 = 0;
+
+ SELECT @RES = CASE WHEN @val1 > @val2 THEN @val1 ELSE @val2 END;
+
+ RETURN @RES;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_max_float.sql b/Fuchs_Database/dbo/Functions/ott_max_float.sql
new file mode 100644
index 0000000..9f206fd
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_max_float.sql
@@ -0,0 +1,28 @@
+
+-- =============================================
+-- Author: Dr. Stefan Ott
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_max_float]
+(
+ @val1 float,
+ @val2 float
+)
+RETURNS float
+AS
+BEGIN
+ DECLARE @RES float;
+
+ IF @val1 is null and @val2 is not null
+ SET @val1 = @val2
+ IF @val2 is null and @val1 is not null
+ SET @val2 = @val1
+ IF @val1 is null and @val2 is null
+ SELECT @val1 = 0, @val2 = 0;
+
+ SELECT @RES = CASE WHEN @val1 > @val2 THEN @val1 ELSE @val2 END;
+
+ RETURN @RES;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_merge_csv.sql b/Fuchs_Database/dbo/Functions/ott_merge_csv.sql
new file mode 100644
index 0000000..7f5e56b
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_merge_csv.sql
@@ -0,0 +1,28 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_merge_csv]
+(
+ @inp1 varchar(2000)
+ ,@inp2 varchar(2000)
+)
+RETURNS varchar(4000)
+AS
+BEGIN
+ DECLARE @ret varchar(4000);
+
+ with sp as(
+ SELECT [value] FROM string_split(@inp1,',')
+ UNION
+ SELECT [value] FROM string_split(@inp2,',')
+ ), sp2 as (
+ SELECT DISTINCT [value] FROM sp where ISNULL([value], '') <> ''
+ )
+ SELECT @ret = ISNULL(STRING_AGG([value], ','),'')
+ FROM sp2;
+
+ RETURN @ret;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_min.sql b/Fuchs_Database/dbo/Functions/ott_min.sql
new file mode 100644
index 0000000..a39defe
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_min.sql
@@ -0,0 +1,29 @@
+
+
+-- =============================================
+-- Author: Dr. Stefan Ott
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_min]
+(
+ @val1 int,
+ @val2 int
+)
+RETURNS int
+AS
+BEGIN
+ DECLARE @RES int;
+
+ IF @val1 is null and @val2 is not null
+ SET @val1 = @val2
+ IF @val2 is null and @val1 is not null
+ SET @val2 = @val1
+ IF @val1 is null and @val2 is null
+ SELECT @val1 = 0, @val2 = 0;
+
+ SELECT @RES = CASE WHEN @val1 < @val2 THEN @val1 ELSE @val2 END;
+
+ RETURN @RES;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_min_date.sql b/Fuchs_Database/dbo/Functions/ott_min_date.sql
new file mode 100644
index 0000000..67a6d1c
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_min_date.sql
@@ -0,0 +1,28 @@
+
+
+
+-- =============================================
+-- Author: Dr. Stefan Ott
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_min_date]
+(
+ @val1 date,
+ @val2 date
+)
+RETURNS date
+AS
+BEGIN
+ DECLARE @RES date;
+
+ IF @val1 is null and @val2 is not null
+ SET @val1 = @val2
+ IF @val2 is null and @val1 is not null
+ SET @val2 = @val1;
+
+ SELECT @RES = CASE WHEN @val1 is null and @val2 is null THEN NULL WHEN @val1 < @val2 THEN @val1 ELSE @val2 END;
+
+ RETURN @RES;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_min_float.sql b/Fuchs_Database/dbo/Functions/ott_min_float.sql
new file mode 100644
index 0000000..dcd3965
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_min_float.sql
@@ -0,0 +1,29 @@
+
+
+-- =============================================
+-- Author: Dr. Stefan Ott
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_min_float]
+(
+ @val1 float,
+ @val2 float
+)
+RETURNS float
+AS
+BEGIN
+ DECLARE @RES float;
+
+ IF @val1 is null and @val2 is not null
+ SET @val1 = @val2
+ IF @val2 is null and @val1 is not null
+ SET @val2 = @val1
+ IF @val1 is null and @val2 is null
+ SELECT @val1 = 0, @val2 = 0;
+
+ SELECT @RES = CASE WHEN @val1 < @val2 THEN @val1 ELSE @val2 END;
+
+ RETURN @RES;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_quote.sql b/Fuchs_Database/dbo/Functions/ott_quote.sql
new file mode 100644
index 0000000..266a9b3
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_quote.sql
@@ -0,0 +1,19 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_quote]
+(
+ @txt as nvarchar(max)
+)
+RETURNS nvarchar(max)
+AS
+BEGIN
+ DECLARE @res nvarchar(max);
+
+ SET @res = CHAR(34) + REPLACE(ISNULL(@txt,''), CHAR(34), CHAR(34) + CHAR(34)) + CHAR(34);
+
+ RETURN @res;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_quote_255.sql b/Fuchs_Database/dbo/Functions/ott_quote_255.sql
new file mode 100644
index 0000000..f7ab489
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_quote_255.sql
@@ -0,0 +1,19 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_quote_255]
+(
+ @txt as nvarchar(255)
+)
+RETURNS nvarchar(255)
+AS
+BEGIN
+ DECLARE @res nvarchar(255);
+
+ SET @res = CHAR(34) + REPLACE(ISNULL(@txt,''), CHAR(34), CHAR(34) + CHAR(34)) + CHAR(34);
+
+ RETURN @res;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_random_int.sql b/Fuchs_Database/dbo/Functions/ott_random_int.sql
new file mode 100644
index 0000000..a6c0247
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_random_int.sql
@@ -0,0 +1,16 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_random_int]
+(@UPPER int, @LOWER int)
+RETURNS int
+AS
+BEGIN
+ DECLARE @RET INT = CAST(ROUND(((@UPPER - @LOWER - 1) * (SELECT TOP(1) [rn] FROM [dbo].[ocms_fn_rand]) + @LOWER), 0) as int);
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/ott_remove_csv.sql b/Fuchs_Database/dbo/Functions/ott_remove_csv.sql
new file mode 100644
index 0000000..9e7c06c
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/ott_remove_csv.sql
@@ -0,0 +1,27 @@
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[ott_remove_csv]
+(
+ @inp1 varchar(2000)
+ ,@fragment varchar(500)
+)
+RETURNS varchar(4000)
+AS
+BEGIN
+ DECLARE @ret varchar(4000);
+
+ with sp as(
+ SELECT [value] FROM string_split(@inp1,',')
+ ), sp2 as (
+ SELECT DISTINCT [value] FROM sp where ISNULL([value], '') <> '' AND ISNULL([value], '') <> @fragment
+ )
+ SELECT @ret = ISNULL(STRING_AGG([value], ','),'')
+ FROM sp2;
+
+ RETURN @ret;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/string_SplitString_ordered.sql b/Fuchs_Database/dbo/Functions/string_SplitString_ordered.sql
new file mode 100644
index 0000000..966f344
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/string_SplitString_ordered.sql
@@ -0,0 +1,30 @@
+
+CREATE FUNCTION [dbo].[string_SplitString_ordered]
+(
+ @String VARCHAR(MAX) ,
+ @Delimiter VARCHAR(10)
+)
+RETURNS @RetTable TABLE(
+ String varchar(MAX),
+ RowNo int
+)
+AS
+BEGIN
+ DECLARE @i INT ,
+ @j INT ,
+ @cnt INT
+ SELECT @i = 1
+ SET @cnt = 1
+ WHILE @i <= LEN(@String)
+ BEGIN
+ SELECT @j = CHARINDEX(@Delimiter, @String, @i)
+ IF @j = 0
+ BEGIN
+ SELECT @j = LEN(@String) + 1
+ END
+ INSERT INTO @RetTable SELECT SUBSTRING(@String, @i, @j - @i), @cnt
+ SET @cnt = @cnt + 1
+ SELECT @i = @j + LEN(@Delimiter)
+ END
+ RETURN
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/strings_encodeHTML.sql b/Fuchs_Database/dbo/Functions/strings_encodeHTML.sql
new file mode 100644
index 0000000..5b6b0c8
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/strings_encodeHTML.sql
@@ -0,0 +1,12 @@
+
+CREATE FUNCTION [dbo].[strings_encodeHTML](@inputText NVARCHAR(MAX))
+RETURNS NVARCHAR(MAX)
+AS
+BEGIN
+ DECLARE @encodedText NVARCHAR(MAX);
+
+ -- Use FOR XML PATH to escape special characters
+ SET @encodedText = CAST((SELECT @inputText AS [p] FOR XML PATH(''), TYPE) as nvarchar(max));
+
+ RETURN IIF(LEN(@encodedText) < 7, '', dbo.strings_replaceSpecialCharsInHTML(SUBSTRING(@encodedText, 4, LEN(@encodedText) -7)));
+END;
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/strings_removeLeading0.sql b/Fuchs_Database/dbo/Functions/strings_removeLeading0.sql
new file mode 100644
index 0000000..f80b32e
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/strings_removeLeading0.sql
@@ -0,0 +1,17 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[strings_removeLeading0]
+(
+ @Data NVARCHAR(MAX)
+)
+RETURNS nvarchar(max)
+AS
+BEGIN
+ DECLARE @ret nvarchar(max) = SUBSTRING(@Data, PATINDEX('%[^0]%', @Data), LEN(@Data));
+
+ RETURN @ret;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/strings_removeLeading0_255.sql b/Fuchs_Database/dbo/Functions/strings_removeLeading0_255.sql
new file mode 100644
index 0000000..b0d9cf3
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/strings_removeLeading0_255.sql
@@ -0,0 +1,17 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[strings_removeLeading0_255]
+(
+ @Data NVARCHAR(255)
+)
+RETURNS nvarchar(255)
+AS
+BEGIN
+ DECLARE @ret nvarchar(255) = SUBSTRING(@Data, PATINDEX('%[^0]%', @Data), LEN(@Data));
+
+ RETURN @ret;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/strings_replaceSpecialCharsInHTML.sql b/Fuchs_Database/dbo/Functions/strings_replaceSpecialCharsInHTML.sql
new file mode 100644
index 0000000..56c0a75
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/strings_replaceSpecialCharsInHTML.sql
@@ -0,0 +1,31 @@
+
+CREATE FUNCTION [dbo].[strings_replaceSpecialCharsInHTML]
+(
+ @inputHtml NVARCHAR(MAX)
+)
+RETURNS NVARCHAR(MAX)
+AS
+BEGIN
+ DECLARE @result NVARCHAR(MAX);
+
+ /* the order is important !! */
+
+
+ -- Replace ampersand ("&") with "&"
+ SET @inputHtml = REPLACE(@inputHtml, N'&', N'&');
+
+ -- Replace non-breaking space (ASCII 160) with " "
+ SET @inputHtml = REPLACE(@inputHtml, NCHAR(160), N' ');
+
+ -- Replace newline with "
"
+ SET @inputHtml = REPLACE(@inputHtml, NCHAR(10), N'
');
+
+ -- Replace copyright symbol ("©") with "©"
+ SET @inputHtml = REPLACE(@inputHtml, NCHAR(169), N'©');
+
+ -- Add more replacements for other special characters as needed
+
+ SET @result = @inputHtml;
+
+ RETURN @result;
+END;
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Functions/vat_val.sql b/Fuchs_Database/dbo/Functions/vat_val.sql
new file mode 100644
index 0000000..244fc62
--- /dev/null
+++ b/Fuchs_Database/dbo/Functions/vat_val.sql
@@ -0,0 +1,22 @@
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE FUNCTION [dbo].[vat_val]
+(
+ @price numeric(10,3)
+ ,@quantityhours numeric(10,3)
+ ,@discount numeric(10,3)
+ ,@VAT numeric(10,3)
+)
+RETURNS numeric(7,2)
+AS
+BEGIN
+ DECLARE @Ret numeric(7,2)
+
+ SET @RET = [dbo].[net_val](@price, @quantityhours, @discount) * ((ISNULL(@vat, 19.0) * 0.01));
+
+ RETURN @RET;
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Stored Procedures/_BackupAndClearInvoiceFile.sql b/Fuchs_Database/dbo/Stored Procedures/_BackupAndClearInvoiceFile.sql
new file mode 100644
index 0000000..f69149e
--- /dev/null
+++ b/Fuchs_Database/dbo/Stored Procedures/_BackupAndClearInvoiceFile.sql
@@ -0,0 +1,68 @@
+
+
+-- Example usage: EXEC dbo.usp_BackupAndClearInvoiceFile @InvoiceID = 'R2025-0057';
+
+CREATE PROCEDURE [dbo].[_BackupAndClearInvoiceFile]
+ @InvoiceID nvarchar(25)
+AS
+BEGIN
+ SET NOCOUNT ON;
+
+ DECLARE @invID varchar(10);
+ DECLARE @today datetime;
+ DECLARE @td nvarchar(max);
+ DECLARE @sql nvarchar(max) = '';
+ DECLARE @ErrorMessage nvarchar(500);
+
+ -- Get the invoice ID
+ SELECT TOP(1) @invID = [id]
+ FROM dbo.fds__invoices
+ WHERE InvoiceId = @InvoiceID;
+
+ -- Check if invoice exists
+ IF ISNULL(@invID, '') = ''
+ BEGIN
+ SET @ErrorMessage = 'Invoice not found: ' + @InvoiceID;
+ PRINT @ErrorMessage;
+ RETURN -1;
+ END
+
+ -- Generate backup table prefix with timestamp
+ SET @today = GETDATE();
+ SET @td = FORMAT(@today, 'yyyy-MM-dd_HHmm');
+
+ PRINT 'Backup table prefix: ' + @td;
+
+ BEGIN TRY
+ -- Build dynamic SQL for backup and update
+ SET @sql = @sql + N'SELECT * INTO [' + @td + ' fds__invoices] FROM dbo.fds__invoices WHERE Id = ''' + CAST(@invID as nvarchar(max)) + ''';';
+ SET @sql = @sql + N'SELECT * INTO [' + @td + ' fds__invoice_items] FROM dbo.fds__invoice_items WHERE InvId = ''' + CAST(@invID as nvarchar(max)) + ''';';
+ SET @sql = @sql + N'SELECT * INTO [' + @td + ' fds__invoice_details] FROM dbo.fds__invoice_details WHERE InvId = ''' + CAST(@invID as nvarchar(max)) + ''';';
+
+ -- Clear file column after successful backup
+ SET @sql = @sql + N'IF EXISTS (SELECT 1 FROM [dbo].[' + @td + ' fds__invoices])
+ BEGIN
+ UPDATE dbo.fds__invoices SET [file] = NULL WHERE [Id] = ''' + CAST(@invID as nvarchar(max)) + ''';
+ END;';
+
+ -- Execute the dynamic SQL
+ EXEC sp_executesql @sql;
+
+ PRINT 'SUCCESS: Backup created and file column cleared for Invoice: ' + @InvoiceID;
+
+
+
+ Print Formatmessage('https://sanitaerfuchs.de/intranet/do/req/idoc?id=%s&create=1', @invID);
+ Print Formatmessage('https://sanitaerfuchs.de/intranet/do/inv/rdoc?id=%s&create=1', @invID);
+
+
+ RETURN 0;
+
+ END TRY
+ BEGIN CATCH
+ SET @ErrorMessage = 'ERROR: ' + ERROR_MESSAGE();
+ PRINT @ErrorMessage;
+ RETURN -1;
+ END CATCH
+
+END
\ No newline at end of file
diff --git a/Fuchs_Database/dbo/Stored Procedures/backup__fds__createCredit_simple.sql b/Fuchs_Database/dbo/Stored Procedures/backup__fds__createCredit_simple.sql
new file mode 100644
index 0000000..92d34c5
--- /dev/null
+++ b/Fuchs_Database/dbo/Stored Procedures/backup__fds__createCredit_simple.sql
@@ -0,0 +1,264 @@
+
+
+-- =============================================
+-- Author:
+-- Create date:
+-- Description:
+-- =============================================
+CREATE PROCEDURE [dbo].[backup__fds__createCredit_simple]
+ @InvId varchar(20)
+ , @authuser varchar(100)
+AS
+BEGIN
+ SET NOCOUNT ON;
+
+ IF [dbo].[fis_getModuleAuth]('fds_inv', @authuser) < 2
+ THROW 60000, N'not authorized', 1;
+
+ INSERT INTO [dbo].[fds__admin_activity] ([activity] ,[authuser] ,[info])
+ VALUES ('fds__createCredit_simple' ,@authuser , 'invid: ' + ISNULL(@InvId, ''));
+
+ DECLARE @now datetime = GETDATE();
+ DECLARE @today date = CAST(@now as date);
+
+ DECLARE @newid varchar(10) = [dbo].[fds__fn_invoice_id]();
+
+
+ DECLARE @inv_DocumentName varchar(50), @inv_DateFinalized datetime, @inv_vat numeric(5,2), @inv_net numeric(10,3);
+
+ IF EXISTS (SELECT 0 FROM [dbo].[fds__invoices] WHERE [id] = @InvId AND ISNULL([isfinal],0) = 1)
+ BEGIN
+
+ SELECT @inv_DocumentName = [documentname], @inv_DateFinalized = [DateFinalized], @inv_vat = [InvoiceVAT_1], @inv_net = 1
+ FROM [dbo].[fds__invoices] WHERE [id] = @InvId
+
+ INSERT INTO [dbo].[fds__invoices]
+ ([Id]
+ ,[Version]
+ ,[InvoiceId]
+ ,[InvoiceType]
+ ,[InvoiceTitle]
+ ,[DocumentName]
+ ,[InvoiceBalance]
+ ,[InvoiceBalance_net]
+ ,[InvoiceVAT_net1]
+ ,[InvoiceVAT_1]
+ ,[InvoiceVAT_net2]
+ ,[InvoiceVAT_2]
+ ,[PaymentTerm]
+ ,[DueDate]
+ ,[CustomerId]
+ ,[SendToAddress]
+ ,[SendToEmail]
+ ,[ProvisionPeriod]
+ ,[ProvisionLocation]
+ ,[PaymentStatus]
+ ,[IsPayed]
+ ,[IsSent]
+ ,[Replaces_InvId]
+ ,[CustomValues]
+ ,[DateSent]
+ ,[UserSent]
+ ,[DateFinalized]
+ ,[UserFinalized]
+ ,[DateCancelled]
+ ,[UserCancelled]
+ ,[DateCreated]
+ ,[UserCreated]
+ ,[DateModified]
+ ,[UserModified])
+ SELECT
+ [Id] = @newid
+ ,[Version] = 0
+ ,[InvoiceId] = NULL
+ ,[InvoiceType] = 'g'
+ ,[InvoiceTitle] = 'Gutschrift'
+ ,[DocumentName] = NULL
+ ,[InvoiceBalance] = (@inv_net * -1) * (1 + 0.01 * ISNULL(@inv_vat,19))
+ ,[InvoiceBalance_net] = (@inv_net * -1)
+ ,[InvoiceVAT_net1] = (@inv_net * -1) * (0.01 * ISNULL(@inv_vat,19))
+ ,[InvoiceVAT_1] = @inv_net
+ ,[InvoiceVAT_net2] = null
+ ,[InvoiceVAT_2] = null
+ ,[PaymentTerm]
+ ,[DueDate] = @today
+ ,[CustomerId]
+ ,[SendToAddress]
+ ,[SendToEmail]
+ ,[ProvisionPeriod]
+ ,[ProvisionLocation]
+ ,[PaymentStatus] = NULL
+ ,[IsPayed] = 0
+ ,[IsSent] = 0
+ ,[Replaces_InvId] = @InvId
+ ,[CustomValues]
+ ,[DateSent] = NULL
+ ,[UserSent] = @authuser
+ ,[DateFinalized] = NULL
+ ,[UserFinalized] = NULL
+ ,[DateCancelled] = NULL
+ ,[UserCancelled] = NULL
+ ,[DateCreated] = @now
+ ,[UserCreated] = @authuser
+ ,[DateModified] = @now
+ ,[UserModified] = @authuser
+ FROM [dbo].[fds__invoices] WHERE [id] = @InvId AND ISNULL([isfinal],0) = 1;
+ END
+ ELSE IF EXISTS (SELECT 0 FROM [dbo].[mfr__invoices] as _i where TRY_CAST(_i.[Id] as varchar(20)) = @InvId AND [FileType] in ('PdfInvoice','PdfPartialInvoice'))
+ BEGIN
+
+ SELECT @inv_DocumentName = [documentname], @inv_DateFinalized = [DateOfCreation]
+ , @inv_vat = CASE WHEN ISNULL([InvoiceBalanceNetto], 0) <> 0 THEN (([InvoiceBalance] / [InvoiceBalanceNetto]) - 1)*100 ELSE (SELECT TOP(1) TRY_CAST([value] as numeric(5,2)) FROM [dbo].[fds__admin_settings] as st where st.[type] = 'defaults' and st.[key] = 'vat') END
+ , @inv_net = 1 --[InvoiceBalanceNetto]
+ FROM [dbo].[mfr__invoices] as _i where TRY_CAST(_i.[Id] as varchar(20)) = @InvId AND [FileType] in ('PdfInvoice','PdfPartialInvoice');
+
+ INSERT INTO [dbo].[fds__invoices]
+ ([Id]
+ ,[Version]
+ ,[InvoiceId]
+ ,[InvoiceType]
+ ,[InvoiceTitle]
+ ,[DocumentName]
+ ,[InvoiceBalance]
+ ,[InvoiceBalance_net]
+ ,[InvoiceVAT_net1]
+ ,[InvoiceVAT_1]
+ ,[InvoiceVAT_net2]
+ ,[InvoiceVAT_2]
+ ,[PaymentTerm]
+ ,[DueDate]
+ ,[CustomerId]
+ ,[SendToAddress]
+ ,[SendToEmail]
+ ,[ProvisionPeriod]
+ ,[ProvisionLocation]
+ ,[PaymentStatus]
+ ,[IsPayed]
+ ,[IsSent]
+ ,[Replaces_InvId]
+ ,[CustomValues]
+ ,[DateSent]
+ ,[UserSent]
+ ,[DateFinalized]
+ ,[UserFinalized]
+ ,[DateCancelled]
+ ,[UserCancelled]
+ ,[DateCreated]
+ ,[UserCreated]
+ ,[DateModified]
+ ,[UserModified])
+ SELECT
+ [Id] = @newid
+ ,[Version] = 0
+ ,[InvoiceId] = NULL
+ ,[InvoiceType] = 'g'
+ ,[InvoiceTitle] = 'Gutschrift'
+ ,[DocumentName] = NULL
+ ,[InvoiceBalance] = (@inv_net * -1) * (1 + 0.01 * ISNULL(@inv_vat,19))
+ ,[InvoiceBalance_net] = (@inv_net * -1)
+ ,[InvoiceVAT_net1] = (@inv_net * -1) * (0.01 * ISNULL(@inv_vat,19))
+ ,[InvoiceVAT_1] = @inv_vat
+ ,[InvoiceVAT_net2] = NULL
+ ,[InvoiceVAT_2] = NULL
+ ,[PaymentTerm] = '10wd'
+ ,[DueDate] = _i.DueDate
+ ,[CustomerId] = (SELECT TOP(1) [partnerid] from [mfr__*PartnerSet] as ps where ps.[Property] = 'Invoice:Customer' and ps.[EntityId] = _i.[id])
+ ,[SendToAddress] = NULL
+ ,[SendToEmail] = NULL
+ ,[ProvisionPeriod] = null
+ ,[ProvisionLocation] = NULL
+ ,[PaymentStatus] = NULL
+ ,[IsPayed] = 0
+ ,[IsSent] = 0
+ ,[Replaces_InvId] = @InvId
+ ,[CustomValues] = null
+ ,[DateSent] = NULL
+ ,[UserSent] = @authuser
+ ,[DateFinalized] = NULL
+ ,[UserFinalized] = NULL
+ ,[DateCancelled] = NULL
+ ,[UserCancelled] = NULL
+ ,[DateCreated] = @now
+ ,[UserCreated] = @authuser
+ ,[DateModified] = @now
+ ,[UserModified] = @authuser
+ FROM [dbo].[mfr__invoices] as _i where TRY_CAST(_i.[Id] as varchar(20)) = @InvId AND [FileType] in ('PdfInvoice','PdfPartialInvoice');
+ UPDATE [dbo].[fds__invoices]
+ SET [SendToAddress] = [dbo].[fds__getCompanyNameAddress]([CustomerId]), [SendToEmail] = [dbo].[fds__getCompanyEmail]([CustomerId])
+ WHERE [Id] = @newid;
+
+ END
+
+
+
+ -- continue creating storno
+
+ DECLARE @inv_srq [dbo].[fds__tt__invoice_servicerequests];
+ INSERT INTO [dbo].[fds__invoice_servicerequests]
+ ([InvId]
+ ,[mfr__servicerequest]
+ ,[title]
+ ,[value_net]
+ ,[SortOrder])
+ OUTPUT
+ inserted.* INTO @inv_srq
+ SELECT
+ [InvId] = [id]
+ ,NULL --
+ ,'Gutschrift'
+ ,(@inv_net * -1)
+ ,0
+ FROM [dbo].[fds__invoices] WHERE [id] = @newid;
+
+
+ INSERT INTO [dbo].[fds__invoice_items]
+ ([InvId]
+ ,[InvRqId]
+ ,[mfr__item]
+ ,[reference_InvId]
+ ,[Type]
+ ,[Position]
+ ,[Quantity]
+ ,[Text]
+ ,[value]
+ ,[value_total]
+ ,[vat]
+ ,[value_service]
+ ,[det]
+ ,[SortOrder])
+ SELECT
+ @newid --
+ ,[id]--