The invoice attached to Your email has already been worked in our system.
" + Dim oEmailData = MoveAndRenameEmailToRejected(args, oFileGroupId) + AddToEmailQueue(oFileGroupId, oBody, oEmailData) Catch ex As TooMuchFerdsException _logger.Error(ex) @@ -367,4 +400,24 @@ Public Class ImportZUGFeRDFiles Return oBody End Function + Private Function checkMD5(ByVal filename As String) As String + Try + Dim MD5 As New MD5CryptoServiceProvider + Dim Hash As Byte() + Dim Result As String = "" + Dim Tmp As String = "" + + Dim FN As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 8192) + MD5.ComputeHash(FN) + FN.Close() + + Hash = MD5.Hash + Result = Strings.Replace(BitConverter.ToString(Hash), "-", "") + Return Result + Catch ex As Exception + _logger.Error(ex) + Return "" + End Try + + End Function End Class diff --git a/Jobs/Exceptions.vb b/Jobs/Exceptions.vb index 9c326b51..f73d2d47 100644 --- a/Jobs/Exceptions.vb +++ b/Jobs/Exceptions.vb @@ -28,4 +28,11 @@ Public Class Exceptions MyBase.New("No ZUGFeRD documents found") End Sub End Class + Public Class MD5HashException + Inherits ApplicationException + + Public Sub New() + MyBase.New("There is already an identical invoice") + End Sub + End Class End Class diff --git a/Modules.Interfaces/ActiveDirectoryInterface/SyncUsers.MSSQL.vb b/Modules.Interfaces/ActiveDirectoryInterface/SyncUsers.MSSQL.vb index 6ded94d5..15f2874a 100644 --- a/Modules.Interfaces/ActiveDirectoryInterface/SyncUsers.MSSQL.vb +++ b/Modules.Interfaces/ActiveDirectoryInterface/SyncUsers.MSSQL.vb @@ -90,8 +90,15 @@ Namespace SyncUsers Private Sub AddUserToGroup(UserId As Integer, GroupId As Integer) Implements ISyncUsers.AddUserToGroup Try - Dim oSQL As String = $"INSERT INTO TBDD_GROUPS_USER (USER_ID, GROUP_ID, ADDED_WHO) VALUES ({UserId}, {GroupId}, '{ADDED_WHO}')" - Dim oResult = _mssql.NewExecutenonQuery(oSQL) + Dim oSQL = $"SELECT COUNT(*) FROM TBDD_GROUPS_USER WHERE USER_ID = {UserId} AND GROUP_ID = {GroupId}" + Dim oResult = True + If _mssql.NewExecuteScalar(oSQL) = 0 Then + oSQL = $"INSERT INTO TBDD_GROUPS_USER (USER_ID, GROUP_ID, ADDED_WHO) VALUES ({UserId}, {GroupId}, '{ADDED_WHO}')" + oResult = _mssql.NewExecutenonQuery(oSQL) + Else + _logger.Debug($"UserGroup-Relation [{UserId}/{GroupId}] already existing") + + End If If oResult = False Then Throw New Exception("Error while adding user to group!") @@ -104,7 +111,7 @@ Namespace SyncUsers Private Function GetGroupId(GroupName As String) As Integer Implements ISyncUsers.GetGroupId Try - Dim oSQL As String = $"SELECT GUID FROM TBDD_GROUPS WHERE NAME = '{GroupName}' AND AD_SYNC = 1 AND ACTIVE = 1" + Dim oSQL As String = $"SELECT GUID FROM TBDD_GROUPS WHERE UPPER(NAME) = UPPER('{GroupName}') AND AD_SYNC = 1 AND ACTIVE = 1" Dim oGroupId = _mssql.NewExecuteScalar(oSQL) If IsDBNull(oGroupId) OrElse oGroupId = 0 Then @@ -121,7 +128,7 @@ Namespace SyncUsers Private Function GetUserId(UserName As String) As Integer Implements ISyncUsers.GetUserId Try - Dim oSQL As String = $"SELECT GUID FROM TBDD_USER WHERE USERNAME = '{UserName}'" + Dim oSQL As String = $"SELECT GUID FROM TBDD_USER WHERE UPPER(USERNAME) = UPPER('{UserName}')" Dim oUserId = _mssql.NewExecuteScalar(oSQL) If IsDBNull(oUserId) OrElse oUserId = 0 Then @@ -137,15 +144,21 @@ Namespace SyncUsers Private Function CreateUser(User As ADUser) As Integer Implements ISyncUsers.CreateUser Try - Dim oSQL As String = $"INSERT INTO TBDD_USER (PRENAME, NAME, USERNAME, EMAIL, ADDED_WHO) VALUES ('{User.GivenName}', '{User.Surname}', '{User.samAccountName}', '{User.Email}', '{ADDED_WHO}')" - Dim oResult = _mssql.NewExecutenonQuery(oSQL) + Dim oUserId As Integer = GetUserId(User.samAccountName) + If oUserId = 0 Then + Dim oSQL As String = $"INSERT INTO TBDD_USER (PRENAME, NAME, USERNAME, EMAIL, ADDED_WHO) VALUES ('{User.GivenName}', '{User.Surname}', UPPER('{User.samAccountName}'), '{User.Email}', '{ADDED_WHO}')" + Dim oResult = _mssql.NewExecutenonQuery(oSQL) - If oResult = True Then - Dim oUserId = _mssql.NewExecuteScalar("SELECT MAX(GUID) FROM TBDD_USER") - Return oUserId + If oResult = True Then + oUserId = _mssql.NewExecuteScalar("SELECT MAX(GUID) FROM TBDD_USER") + Return oUserId + Else + Throw New Exception("Error while inserting user!") + End If Else - Throw New Exception("Error while inserting user!") + Return oUserId End If + Catch ex As Exception _logger.Error(ex) Throw ex