Diverses Zugferd

This commit is contained in:
Digital Data - Marlon Schreiber 2019-05-09 11:25:39 +02:00
parent 9e330bd52e
commit 809f8e2b43
9 changed files with 155 additions and 13 deletions

View File

@ -92,7 +92,7 @@ Partial Class frmWorkflowStep
Me.Name = "frmWorkflowStep"
Me.Ribbon = Me.RibbonControl
Me.StatusBar = Me.RibbonStatusBar
Me.Text = "frmWorkflowStep"
Me.Text = "View WorkflowStep"
CType(Me.RibbonControl, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.LayoutControl1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.LayoutControlGroupMain, System.ComponentModel.ISupportInitialize).EndInit()

View File

@ -29,6 +29,9 @@
<setting name="FB_PASS" serializeAs="String">
<value>dd</value>
</setting>
<setting name="MD5Cheksum" serializeAs="String">
<value />
</setting>
</ZUGFeRDTest.My.MySettings>
</userSettings>
<runtime>

View File

@ -27,6 +27,8 @@ Partial Class Form1
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.Button2 = New System.Windows.Forms.Button()
Me.Button3 = New System.Windows.Forms.Button()
Me.Button4 = New System.Windows.Forms.Button()
Me.txtMD5Checksum = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'OpenFileDialog1
@ -68,11 +70,31 @@ Partial Class Form1
Me.Button3.Text = "Load Single File"
Me.Button3.UseVisualStyleBackColor = True
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(12, 112)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(221, 23)
Me.Button4.TabIndex = 4
Me.Button4.Text = "MD5Chcksum"
Me.Button4.UseVisualStyleBackColor = True
'
'txtMD5Checksum
'
Me.txtMD5Checksum.DataBindings.Add(New System.Windows.Forms.Binding("Text", Global.ZUGFeRDTest.My.MySettings.Default, "MD5Cheksum", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))
Me.txtMD5Checksum.Location = New System.Drawing.Point(12, 141)
Me.txtMD5Checksum.Name = "txtMD5Checksum"
Me.txtMD5Checksum.Size = New System.Drawing.Size(360, 20)
Me.txtMD5Checksum.TabIndex = 5
Me.txtMD5Checksum.Text = Global.ZUGFeRDTest.My.MySettings.Default.MD5Cheksum
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(916, 435)
Me.Controls.Add(Me.txtMD5Checksum)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.ListBox1)
@ -80,6 +102,7 @@ Partial Class Form1
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
@ -88,4 +111,6 @@ Partial Class Form1
Friend WithEvents ListBox1 As ListBox
Friend WithEvents Button2 As Button
Friend WithEvents Button3 As Button
Friend WithEvents Button4 As Button
Friend WithEvents txtMD5Checksum As TextBox
End Class

View File

@ -1,5 +1,6 @@
Imports System.IO
Imports System.Reflection
Imports System.Security.Cryptography
Imports System.Text.RegularExpressions
Imports System.Xml
Imports DigitalData.Modules
@ -100,4 +101,29 @@ Public Class Form1
Console.WriteLine()
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
My.Settings.Save()
Dim result = checkMD5(txtMD5Checksum.Text)
MsgBox("Done")
End Sub
Public 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
Return Nothing
End Try
End Function
End Class

View File

@ -95,6 +95,18 @@ Namespace My
Me("FB_PASS") = value
End Set
End Property
<Global.System.Configuration.UserScopedSettingAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Configuration.DefaultSettingValueAttribute("")> _
Public Property MD5Cheksum() As String
Get
Return CType(Me("MD5Cheksum"),String)
End Get
Set
Me("MD5Cheksum") = value
End Set
End Property
End Class
End Namespace

View File

@ -14,5 +14,8 @@
<Setting Name="FB_PASS" Type="System.String" Scope="User">
<Value Profile="(Default)">dd</Value>
</Setting>
<Setting Name="MD5Cheksum" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

View File

@ -2,6 +2,7 @@
Imports System.Data
Imports System.IO
Imports System.Linq
Imports System.Security.Cryptography
Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Interfaces
@ -225,6 +226,7 @@ Public Class ImportZUGFeRDFiles
Dim oFileGroupFiles As List(Of FileInfo) = oFileGroup.Value
Dim oFileGroupId As String = oFileGroup.Key
Dim oMissingProperties As New List(Of String)
Dim oMD5CheckSum As String
_logger.NewBlock($"Message Id {oFileGroupId}")
_logger.Info("Start processing file group {0}", oFileGroupId)
@ -251,6 +253,23 @@ Public Class ImportZUGFeRDFiles
_logger.Warn("File is not a valid ZUGFeRD document! Skipping.")
Continue For
End Try
oMD5CheckSum = checkMD5(oFile.FullName)
If oMD5CheckSum <> "" Then
Dim oCheckCommand = $"SELECT * FROM TBEDM_ZUGFERD_HISTORY_IN WHERE UPPER(MD5HASH) = UPPER('{oMD5CheckSum}')"
Dim oMD5DT As DataTable = _firebird.GetDatatable(oCheckCommand, Firebird.TransactionMode.ExternalTransaction)
If Not IsNothing(oMD5DT) Then
If oMD5DT.Rows.Count = 1 Then
'Hier muss noch gepüft werden ob die Rechnung schon mal abgelehnt wurde?!
Throw New MD5HashException()
End If
Else
_logger.Info("Be careful: oExistsDT is nothing!")
End If
Else
_logger.Info("Be careful: oMD5CheckSum is nothing!")
End If
' Check if there are more than one ZUGFeRD files
If oZUGFeRDCount = 1 Then
@ -301,8 +320,22 @@ Public Class ImportZUGFeRDFiles
Throw New NoFerdsException()
End If
' If no errors occurred, commit the transaction
'If no errors occurred...
'Log the History
If oMD5CheckSum <> "" Then
Dim oInsertCommand = $"INSERT INTO TBEDM_ZUGFERD_HISTORY_IN (MESSAGE_ID, MD5HASH) VALUES ('{oFileGroupId}', '{oMD5CheckSum}')"
_firebird.ExecuteNonQueryWithConnection(oInsertCommand, oConnection, Firebird.TransactionMode.ExternalTransaction, oTransaction)
End If
'commit the transaction
oTransaction.Commit()
Catch ex As MD5HashException
_logger.Error(ex)
oMoveDirectory = args.ErrorDirectory
Dim oBody = "<p>The invoice attached to Your email has already been worked in our system.</p>"
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

View File

@ -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

View File

@ -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 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")
oUserId = _mssql.NewExecuteScalar("SELECT MAX(GUID) FROM TBDD_USER")
Return oUserId
Else
Throw New Exception("Error while inserting user!")
End If
Else
Return oUserId
End If
Catch ex As Exception
_logger.Error(ex)
Throw ex