Files
DPM_PADM/DPM2016 - Kopie/Dokumente/clsDok.vb
2022-07-27 12:02:47 +02:00

404 lines
15 KiB
VB.net

Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.IO
Imports DevComponents.AdvTree
Public Class clsDok
Dim dbdokumente As New clsDB
Dim m_stream As MemoryStream
Property Documentstream As MemoryStream
Get
Return m_stream
End Get
Set(value As MemoryStream)
m_stream = value
End Set
End Property
#Region "Dokument Save Get"
Public Function Save_Document(ByVal Dokumentnr As Integer, ByVal Filename As String) As Boolean
Dim Connection As New SqlConnection()
Dim DA As New SqlDataAdapter("select * from dms_dokument where nreintrag =" + Str(Dokumentnr), Connection)
Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA)
Dim ds As New DataSet()
Dim fs As New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.Read)
Dim mydata(fs.Length) As Byte
Try
fs.Read(mydata, 0, fs.Length)
fs.Close()
Connection.ConnectionString = My.Settings.ConnectionString
Connection.Open()
DA.Fill(ds, "Dokument")
Dim myRow As DataRow
If ds.Tables(0).Rows.Count = 0 Then
Return False
Else
myRow = ds.Tables(0).Rows(0)
myRow.Item(14) = mydata
DA.Update(ds, "Dokument")
End If
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
Finally
fs = Nothing
cb = Nothing
ds = Nothing
DA = Nothing
Connection.Close()
Connection = Nothing
End Try
End Function
Public Function Save_Vorlage(ByVal Dokumentnr As Integer, ByVal Filename As String) As Boolean
Dim Connection As New SqlConnection()
Dim DA As New SqlDataAdapter("select * from dms_vorlage where nreintrag =" + Str(Dokumentnr), Connection)
Dim cb As SqlCommandBuilder = New SqlCommandBuilder(DA)
Dim ds As New DataSet()
Dim fs As New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.Read)
Dim mydata(fs.Length) As Byte
Try
fs.Read(mydata, 0, fs.Length)
fs.Close()
Connection.ConnectionString = My.Settings.ConnectionString
Connection.Open()
DA.Fill(ds, "Dokument")
Dim myRow As DataRow
If ds.Tables(0).Rows.Count = 0 Then
Return False
Else
myRow = ds.Tables(0).Rows(0)
myRow.Item(8) = mydata
DA.Update(ds, "Dokument")
End If
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
Finally
fs = Nothing
cb = Nothing
ds = Nothing
DA = Nothing
Connection.Close()
Connection = Nothing
End Try
End Function
Public Function Get_Dokument(ByVal DokumentNr As Integer, Optional destdir As String = "", Optional WithTimeStamp As Boolean = False, Optional DeleteDestFile As Boolean = False) As String
Dim Filename As String = My.Settings.TempPath
If destdir <> "" Then Filename = destdir
If Right(Filename, 1) <> "\" Then Filename = Filename + "\"
Dim connection As New SqlConnection()
Dim da As New SqlDataAdapter("Select * From dms_Dokument where nreintrag=" + Str(DokumentNr), connection)
Dim CB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()
Try
connection.ConnectionString = My.Settings.ConnectionString
connection.Open()
da.Fill(ds, "Dokument")
Dim myRow As DataRow
myRow = ds.Tables(0).Rows(0)
Dim MyData() As Byte
MyData = myRow.Item(14)
Dim K As Long
K = UBound(MyData)
If WithTimeStamp = True Then
Filename = Filename + Format(Now, "yyyyMMddHHmmss") + "_"
End If
Filename = Filename + System.IO.Path.GetFileName(myRow.Item(4))
If System.IO.File.Exists(Filename) Then
System.IO.File.Delete(Filename)
End If
Dim fs As New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.Write)
fs.Write(MyData, 0, K)
fs.Close()
fs = Nothing
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Return ""
Finally
connection.Close()
connection = Nothing
End Try
CB = Nothing
ds = Nothing
da = Nothing
Return Filename
End Function
Public Function Get_Vorlage(ByVal DokumentNr As Integer, ByVal Filename As String) As String
Dim connection As New SqlConnection()
Dim da As New SqlDataAdapter("Select * From dms_vorlage where nreintrag=" + Str(DokumentNr), connection)
Dim CB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()
Try
connection.ConnectionString = My.Settings.ConnectionString
connection.Open()
da.Fill(ds, "Dokument")
Dim myRow As DataRow
myRow = ds.Tables(0).Rows(0)
Dim MyData() As Byte
MyData = myRow.Item(8)
Dim K As Long
K = UBound(MyData)
If System.IO.File.Exists(Filename) Then
System.IO.File.Delete(Filename)
End If
Dim fs As New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.Write)
fs.Write(MyData, 0, K)
fs.Close()
fs = Nothing
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Return ""
Finally
connection.Close()
connection = Nothing
End Try
CB = Nothing
ds = Nothing
da = Nothing
Return Filename
End Function
Public Function Get_Dokument_stram(ByVal DokumentNr As Integer) As MemoryStream
Dim Filename As String = My.Settings.TempPath
If Right(Filename, 1) <> "\" Then Filename = Filename + "\"
Dim connection As New SqlConnection()
Dim da As New SqlDataAdapter("Select * From dms_Dokument where nreintrag=" + Str(DokumentNr), connection)
Dim CB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()
Try
connection.ConnectionString = My.Settings.ConnectionString
connection.Open()
da.Fill(ds, "Dokument")
Dim myRow As DataRow
myRow = ds.Tables(0).Rows(0)
Dim MyData() As Byte
MyData = myRow.Item(14)
Dim K As Long
K = UBound(MyData)
Dim ms As New MemoryStream(MyData)
Return ms
Filename = Filename + System.IO.Path.GetFileName(myRow.Item(4))
Dim fs As New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.Write)
fs.Write(MyData, 0, K)
fs.Close()
fs = Nothing
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Return Nothing
Finally
connection.Close()
connection = Nothing
End Try
CB = Nothing
ds = Nothing
da = Nothing
Return Nothing
End Function
#End Region
#Region "Tree"
Public Function LoadTree(ByRef Tree As DevComponents.AdvTree.AdvTree)
Dim db As New clsDB
db.Get_Tabledata("DMS_Struktur", "", "Select * from dms_struktur where aktiv=1")
Load_Treeview(db.dsDaten, Tree)
db.Dispose()
Tree.ExpandAll()
End Function
Private Sub Load_Treeview(ByVal oSourceData As DataSet, ByRef tree As AdvTree)
If Not (oSourceData Is Nothing) Then
Dim oView As DataView = oSourceData.Tables(0).DefaultView
Dim oTable As DataTable = oView.Table
Dim oDS As DataSet = New DataSet()
oDS.Tables.Add(oTable.Copy())
If oDS.Relations.Contains("SelfRefenceRelation") = False Then
oDS.Relations.Add("SelfRefenceRelation",
oDS.Tables(0).Columns("eintragnr"),
oDS.Tables(0).Columns("parentid"))
End If
oTable.Dispose()
oTable = Nothing
LoadTreeView(oDS, tree)
oDS.Dispose()
oDS = Nothing
End If
End Sub
''' <summary>
''' Tree aufbauen
''' </summary>
''' <param name="oDS"></param>
''' <param name="oTreeview"></param>
''' <remarks></remarks>
Private Sub LoadTreeView(ByVal oDS As DataSet, ByRef oTreeview As AdvTree)
'Dim oTreeView As TreeView = New TreeView()
Dim oDataRow As DataRow
For Each oDataRow In oDS.Tables(0).Rows
If Not oDataRow.IsNull("Parentid") Then
If oDataRow.Item("Parentid") = 0 Then
Dim oNode As New DevComponents.AdvTree.Node
oNode.Text = oDataRow("Bezeichnung").ToString()
oNode.Tag = oDataRow("eintragnr").ToString
oNode.ImageIndex = 0
oNode.ImageIndex = 0
oTreeview.Nodes.Add(oNode)
RecursivelyLoadTree(oDataRow, oNode)
End If
End If
Next oDataRow
oDS.Dispose()
oDS = Nothing
End Sub
''' <summary>
''' Child-Nodes hinzufügen
''' </summary>
''' <param name="oDataRow"></param>
''' <param name="oNode"></param>
''' <remarks></remarks>
Private Sub RecursivelyLoadTree(ByVal oDataRow As DataRow, ByRef oNode As DevComponents.AdvTree.Node)
Dim oChildRow As DataRow
For Each oChildRow In oDataRow.GetChildRows("SelfRefenceRelation")
Dim oChildNode As New DevComponents.AdvTree.Node()
oChildNode.Text = oChildRow("Bezeichnung").ToString()
oChildNode.Tag = oChildRow("eintragnr").ToString()
If oChildRow("eintragnr") = 0 Then
oChildNode.ImageIndex = 0
oChildNode.ImageIndex = 0
Else
oChildNode.ImageIndex = 1
oChildNode.ImageIndex = 1
End If
oNode.Nodes.Add(oChildNode)
RecursivelyLoadTree(oChildRow, oChildNode)
Next oChildRow
End Sub
#End Region
#Region "Datahandling"
Public Function Update_Grid(ByVal Strukturelement As Integer, ByVal Keyvalue As Integer, ByRef c1dokumente As C1.Win.C1TrueDBGrid.C1TrueDBGrid)
dbdokumente.Get_Tabledata("Dokumente", "", "Select * from dms_dokument where dms_strukturnr=" + Strukturelement.ToString + " and keyvalue=" + Keyvalue.ToString + " and aktiv=1 order by erstellt_am desc")
c1dokumente.DataSource = Nothing
c1dokumente.DataSource = dbdokumente.dsDaten.Tables(0)
c1dokumente.DataMember = dbdokumente.dsDaten.Tables(0).TableName
Dim spalten As New Tabellenspalte
spalten.Spaltentitel_aktualisieren(c1dokumente, "DMS_Dokument", dbdokumente.dsDaten.Tables(0))
End Function
Public Function Create_Doc(ByVal Strukturelement As Integer, ByVal Bezeichnung As String, ByVal Path As String, ByVal Partnernr As Integer, Optional nrfaktura As Integer = 0)
Dim dir As String
Dim db1 As New clsDB
db1.Get_Tabledata("DMS_Settings", "", "Select * from dms_settings where nreintrag=1")
Dim db2 As New clsDB
db2.Get_Tabledata("DMS_Settings", "", "Select * from dms_settings where nreintrag=2")
dir = db2.dsDaten.Tables(0).Rows(0).Item(2)
dbdokumente.Get_Tabledata("Dokumente", "", "Select * from dms_dokument where nreintrag=-1")
Dim dr As DataRow
dr = dbdokumente.dsDaten.Tables(0).NewRow
dr.Item(1) = Strukturelement
dr.Item(2) = Bezeichnung
dr.Item(3) = ""
dr.Item(4) = Path
dr.Item(5) = Now
dr.Item(6) = Now
dr.Item(7) = Globals.ActUser
dr.Item(8) = True
dr.Item(9) = db1.dsDaten.Tables(0).Rows(0).Item(2)
If UCase(db1.dsDaten.Tables(0).Rows(0).Item(2)) = "FALSE" Then
dr.Item(4) = dir + System.IO.Path.GetFileName(Path)
End If
dr.Item(10) = Now
dr.Item(11) = False
dr.Item(12) = ""
dr.Item(13) = Partnernr
dr.Item(15) = nrfaktura
dbdokumente.dsDaten.Tables(0).Rows.Add(dr)
dbdokumente.Update_Data()
If db1.dsDaten.Tables(0).Rows(0).Item(2) = "True" Then
dbdokumente.Get_Tabledata("Dokument", "", "Select top 1 * from dms_dokument order by nreintrag desc")
Dim dok As New clsDok
dok.Save_Document(dbdokumente.dsDaten.Tables(0).Rows(0).Item(0), Path)
Else
If Not System.IO.Directory.Exists(dir) Then
System.IO.Directory.CreateDirectory(dir)
End If
System.IO.File.Copy(Path, dir + System.IO.Path.GetFileName(Path))
End If
End Function
Public Sub Delete_Doks(ByVal nrbehandlung As String)
Dim db As New clsDB
Dim db1 As New clsDB
Try
Dim Hauptfaktura As Integer
db.Get_Tabledata("Doks", "", "Select nrhauptfaktura from faktura where nrbehandlung=" + nrbehandlung.ToString + " order by erstellt_am desc")
Hauptfaktura = db.dsDaten.Tables(0).Rows(0).Item(0)
db.Get_Tabledata("Doks", "", "Select * from dms_dokument where nrfaktura=" + Hauptfaktura.ToString + " and aktiv=1")
If db.dsDaten.Tables(0).Rows.Count > 0 Then
If MsgBox("Möchten Sie die vorhandenen Dokumente löschen?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
db.Get_Tabledata("fakturen", "", "Select nrfaktura from faktura where nrhauptfaktura=" + Hauptfaktura.ToString + " order by erstellt_am desc")
For Each r As DataRow In db.dsDaten.Tables(0).Rows
db1.Get_Tabledata("doks", "", "Select * from dms_dokument where nrfaktura=" + r.Item(0).ToString)
Delete_Dok(db1.dsDaten.Tables(0).Rows(0).Item(0))
Next
End If
Catch
Finally
db.Dispose()
End Try
End Sub
Public Sub Delete_Dok(ByVal Dokumentid As String)
Dim db As New clsDB
Dim db1 As New clsDB
db.Get_Tabledata("DMS_Dokument", "where nreintrag=" + Dokumentid, "")
db.dsDaten.Tables(0).Rows(0).Item("Aktiv") = 0
db.dsDaten.Tables(0).Rows(0).Item("mutiert_am") = Now
db.dsDaten.Tables(0).Rows(0).Item("mutierer") = Globals.ActUser
db.Update_Data()
db1.Get_Tabledata("DMS_Settings", "", "Select * from dms_settings where nreintrag=1")
If UCase(db1.dsDaten.Tables(0).Rows(0).Item(2)) = "FALSE" Then
Try
System.IO.File.Delete(db.dsDaten.Tables(0).Rows(0).Item("Pfad"))
Catch ex As Exception
MsgBox("Die Datei '" + db.dsDaten.Tables(0).Rows(0).Item("Pfad") + " konnte nicht gelöscht werden.", vbExclamation)
End Try
End If
db.Dispose()
db1.Dispose()
End Sub
#End Region
End Class