Imports System.Data.SqlClient

Public Class cBuergschaften

    Public Shared Function GetNewOpenConnectionATLAS() As SqlConnection
        Dim cn As New SqlConnection()
        cn.ConnectionString = "Data Source=192.168.0.95\SQLATLAS;Initial Catalog=atlas;Integrated Security=false;User ID=AppUser;Password=yp/THDd?xM+pZ$;"
        cn.Open()
        Return cn
    End Function
    Public Shared Function GetNewOpenConnectionATLAS_SBG() As SqlConnection
        Dim cn As New SqlConnection()
        cn.ConnectionString = "Data Source=192.168.133.98;Initial Catalog=atlas;Integrated Security=false;User ID=AppUser;Password=yp/THDd?xM+pZ$;"
        cn.Open()
        Return cn
    End Function
    Public Shared Function GetNewOpenConnectionFMZOLL() As SqlConnection
        Return (VERAG_PROG_ALLGEMEIN.SQL).GetNewOpenConnectionFMZOLL()
        'Dim cn As New SqlConnection()
        'cn.ConnectionString = "Data Source=FMZOLL\SQLFMZOLL;Initial Catalog=VERAG;Integrated Security=false;User ID=AppUser;Password=yp/THDd?xM+pZ$;"
        '' cn.ConnectionString = "Data Source=DEVELOPER\DEVSQL;Initial Catalog=VERAG;Integrated Security=false;User ID=AppUser;Password=yp/THDd?xM+pZ$;"
        'cn.Open()
        'Return cn
    End Function
    Public Shared Function GetNewOpenConnectionBUCHHALTUNG() As SqlConnection
        Dim cn As New SqlConnection()
        cn.ConnectionString = "Data Source=BUCHHALTUNG\SQLEXPRESS;Initial Catalog=ezoll;Integrated Security=false;User ID=AppUser;Password=yp/THDd?xM+pZ$;"
        cn.Open()
        Return cn
    End Function

    Public Function doStuff_ATLAS(NL As String, HISTORY As Boolean) As Boolean ', Optional history As Boolean = False) As Boolean

        'MsgBox("start")
        Dim cnt As Integer = 0
        Dim dr As SqlDataReader
        Dim sql As String = ""
        If HISTORY Then
            '   sql = "SELECT  * FROM atlas.dbo.vwVERAG_NCTS_Datenbereitstellung_History  where veoant_andat > '01.10.2017'" 'alles nach dem Datum...
            sql = "SELECT  * FROM atlas.dbo.vwVERAG_NCTS_Datenbereitstellung " 'alles nach dem Datum...
        Else
            sql = "SELECT  * FROM atlas.dbo.vwVERAG_NCTS_DatenbereitstellungDATUM" 'letzten 10 Tage
            'sql = "SELECT  * FROM atlas.dbo.vwVERAG_NCTS_Datenbereitstellung" 'alles außer History
        End If
        Dim r As Boolean = False
        Dim sqlConn As SqlConnection = Nothing
        If NL = "SUB" Then
            sqlConn = GetNewOpenConnectionATLAS()
            setLabel(frmRoutineManager.lbBrgAtlasSuben, "einlesen ...")
        ElseIf NL = "SBG" Then
            sqlConn = GetNewOpenConnectionATLAS_SBG()
            setLabel(frmRoutineManager.lbBrgAtlasSalzburg, "einlesen ...")
        End If
        Using conn As SqlConnection = sqlConn
            Using cmd As New SqlCommand(sql, conn)

                Try
                    cmd.CommandTimeout = 600
                    dr = cmd.ExecuteReader()

                    While dr.Read

                        '  MsgBox("read")
                        '     [basman_nr], [basman_nl], [veoant_beznr], [veoant_lfdnr], [veoant_stat], [veoant_arbnr], [veoant_mrn], [veoant_abgdst], [veoant_bedst], [veoant_wgdat], [veoant_anzpos], [veoant_andat], [veoant_korant], [veoant_vrbdat], [veoant_sb], [veopos_posnr], [veopos_stat], [veopos_wbsch1], [veopos_rohmas], [veoerz_sicbsc], [veoerz_basbtg], [veoerz_sicbtg], [vegdat_erldat], [upsize_ts], [History]

                        Dim sql2 As String = " begin tran" &
                            " if exists (select * from [tblZabis_Atlas_NCTS] with (updlock,serializable) where [basman_nr]=@basman_nr AND [basman_nl]=@basman_nl AND [veoant_beznr]=@veoant_beznr AND [veopos_posnr]=@veopos_posnr) " &
                                   " begin " &
                                   " UPDATE [tblZabis_Atlas_NCTS] " &
                                   " SET [veoant_lfdnr]=@veoant_lfdnr, [veoant_stat]=@veoant_stat, [veoant_arbnr]=@veoant_arbnr, [veoant_mrn]=@veoant_mrn, [veoant_abgdst]=@veoant_abgdst, [veoant_bedst]=@veoant_bedst, [veoant_wgdat]=@veoant_wgdat, [veoant_anzpos]=@veoant_anzpos, [veoant_andat]=@veoant_andat, [veoant_korant]=@veoant_korant, [veoant_vrbdat]=@veoant_vrbdat, [veoant_sb]=@veoant_sb, [veopos_stat]=@veopos_stat, [veopos_wbsch1]=@veopos_wbsch1, [veopos_rohmas]=@veopos_rohmas, [veoerz_sicbsc]=@veoerz_sicbsc, [veoerz_basbtg]=@veoerz_basbtg, [veoerz_sicbtg]=@veoerz_sicbtg, [vegdat_erldat]=@vegdat_erldat, [History]=@History " &
                                   " WHERE [basman_nr]=@basman_nr AND [basman_nl]=@basman_nl AND [veoant_beznr]=@veoant_beznr AND [veopos_posnr]=@veopos_posnr " &
                            " End " &
                            " Else " &
                            " begin " &
                               " INSERT INTO [tblZabis_Atlas_NCTS] " &
                               " ( [basman_nr], [basman_nl], [veoant_beznr], [veoant_lfdnr], [veoant_stat], [veoant_arbnr], [veoant_mrn], [veoant_abgdst], [veoant_bedst], [veoant_wgdat], [veoant_anzpos], [veoant_andat], [veoant_korant], [veoant_vrbdat], [veoant_sb], [veopos_posnr], [veopos_stat], [veopos_wbsch1], [veopos_rohmas], [veoerz_sicbsc], [veoerz_basbtg], [veoerz_sicbtg], [vegdat_erldat], [History] ) VALUES " &
                               " ( @basman_nr, @basman_nl, @veoant_beznr, @veoant_lfdnr, @veoant_stat, @veoant_arbnr, @veoant_mrn, @veoant_abgdst, @veoant_bedst, @veoant_wgdat, @veoant_anzpos, @veoant_andat, @veoant_korant, @veoant_vrbdat, @veoant_sb, @veopos_posnr, @veopos_stat, @veopos_wbsch1, @veopos_rohmas, @veoerz_sicbsc, @veoerz_basbtg, @veoerz_sicbtg, @vegdat_erldat, @History) " &
                            "End " &
                            " commit tran "

                        Using conn2 As SqlConnection = GetNewOpenConnectionFMZOLL()
                            Using cmd2 As New SqlCommand(sql2, conn2)
                                cmd2.CommandTimeout = 600
 
                                '  cmd2.Parameters.AddWithValue("@ID_NCTS", dr.Item("ID_NCTS"))
                                cmd2.Parameters.AddWithValue("@basman_nr", dr.Item("basman_nr"))
                                cmd2.Parameters.AddWithValue("@basman_nl", dr.Item("basman_nl"))
                                cmd2.Parameters.AddWithValue("@veoant_beznr", dr.Item("veoant_beznr"))
                                cmd2.Parameters.AddWithValue("@veoant_lfdnr", dr.Item("veoant_lfdnr"))
                                cmd2.Parameters.AddWithValue("@veoant_stat", dr.Item("veoant_stat"))
                                cmd2.Parameters.AddWithValue("@veoant_arbnr", dr.Item("veoant_arbnr"))
                                cmd2.Parameters.AddWithValue("@veoant_mrn", dr.Item("veoant_mrn"))
                                cmd2.Parameters.AddWithValue("@veoant_abgdst", dr.Item("veoant_abgdst"))
                                cmd2.Parameters.AddWithValue("@veoant_bedst", dr.Item("veoant_bedst"))
                                cmd2.Parameters.AddWithValue("@veoant_wgdat", dr.Item("veoant_wgdat"))
                                cmd2.Parameters.AddWithValue("@veoant_anzpos", dr.Item("veoant_anzpos"))
                                cmd2.Parameters.AddWithValue("@veoant_andat", dr.Item("veoant_andat"))
                                cmd2.Parameters.AddWithValue("@veoant_korant", dr.Item("veoant_korant"))
                                cmd2.Parameters.AddWithValue("@veoant_vrbdat", dr.Item("veoant_vrbdat"))
                                cmd2.Parameters.AddWithValue("@veoant_sb", dr.Item("veoant_sb"))
                                cmd2.Parameters.AddWithValue("@veopos_posnr", dr.Item("veopos_posnr"))
                                cmd2.Parameters.AddWithValue("@veopos_stat", dr.Item("veopos_stat"))
                                cmd2.Parameters.AddWithValue("@veopos_wbsch1", dr.Item("veopos_wbsch1"))
                                cmd2.Parameters.AddWithValue("@veopos_rohmas", dr.Item("veopos_rohmas"))
                                cmd2.Parameters.AddWithValue("@veoerz_sicbsc", dr.Item("veoerz_sicbsc"))
                                cmd2.Parameters.AddWithValue("@veoerz_basbtg", dr.Item("veoerz_basbtg"))
                                cmd2.Parameters.AddWithValue("@veoerz_sicbtg", dr.Item("veoerz_sicbtg"))
                                cmd2.Parameters.AddWithValue("@vegdat_erldat", dr.Item("vegdat_erldat"))
                                'cmd2.Parameters.AddWithValue("@upsize_ts", dr.Item("upsize_ts"))
                                If history Then
                                    cmd2.Parameters.AddWithValue("@History", "1")
                                Else
                                    cmd2.Parameters.AddWithValue("@History", "0")
                                End If
                                Try
                                    cmd2.ExecuteNonQuery()
                                    cnt += 1
                                Catch ex As SqlException
                                    'MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Data")
                                    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodBase.GetCurrentMethod.Name, , "Fehler beim speichern")
                                End Try
                            End Using
                        End Using

                    End While

                    ' If NL = "SUB" Then
                    'MsgBox(cnt)
                    ' setLabel(frmRoutineManager.lblASUB_Ds, cnt)
                    ' ElseIf NL = "SBG" Then
                    'setLabel(frmRoutineManager.lblASBG_Ds, cnt)
                    '  End If
                    '
                    conn.Close()
                    conn.Dispose()

                    dr.Close()
                    dr = Nothing
                    Me.Finalize()
                    r = True
                Catch ex As Exception
                    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodBase.GetCurrentMethod.Name, , "DB-Fehler")
                End Try

            End Using
        End Using
        Return r

    End Function

    Public Function doStuff_EZOLL(history) As Boolean

        'MsgBox("start")
        Dim cnt As Integer = 0
        Dim dr As SqlDataReader
        Dim sql As String = "SELECT  * FROM  ezoll.dbo.vwVERAG_Zolaris_EZoll_NCTS_Datenübernahme"
        If Not history Then sql &= " WHERE [AnmeldungsDatum] > DATEADD(DAY, - 10, GETDATE()) AND CRN is not null " 'letzten 10 Tage
        If history Then sql &= " WHERE [AnmeldungsDatum] > DATEADD(MONTH, - 6, GETDATE()) AND CRN is not null " 'letzten 6 Monate

        Dim sqlConn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionEZOLL
        '    setLabel(frmAtlasAufschub.lbBrgAtlasSuben, "einlesen ...")

        Using conn As SqlConnection = sqlConn
            Using cmd As New SqlCommand(sql, conn)

                Try

                    dr = cmd.ExecuteReader()

                    While dr.Read

                        '  MsgBox("read")
                        '     [basman_nr], [basman_nl], [veoant_beznr], [veoant_lfdnr], [veoant_stat], [veoant_arbnr], [veoant_mrn], [veoant_abgdst], [veoant_bedst], [veoant_wgdat], [veoant_anzpos], [veoant_andat], [veoant_korant], [veoant_vrbdat], [veoant_sb], [veopos_posnr], [veopos_stat], [veopos_wbsch1], [veopos_rohmas], [veoerz_sicbsc], [veoerz_basbtg], [veoerz_sicbtg], [vegdat_erldat], [upsize_ts], [History]

                        Dim sql2 As String = " begin tran" &
                            " if exists (select * from [tblZolaris_EZoll_NCTS] with (updlock,serializable) where [CRN]=@CRN ) " &
                                   " begin " &
                                   " UPDATE [tblZolaris_EZoll_NCTS] " &
                                   " SET [AnmeldungsDatum]=@AnmeldungsDatum, [GarantieWert]=@GarantieWert, [AnmID]=@AnmID, [ErledigungsTypID]=@ErledigungsTypID, [Warenbezeichnung]=@Warenbezeichnung, [Absender]=@Absender, [Empfaenger]=@Empfaenger, [Gesamtrohmasse]=@Gesamtrohmasse, [Abgangszollstelle]=@Abgangszollstelle, [Bestimmungszollstelle]=@Bestimmungszollstelle, [Gestellungsfrist]=@Gestellungsfrist, [LRN]=@LRN, [DatumBestimmungErreicht]=@DatumBestimmungErreicht, [GRN]=@GRN, OperatorId=@OperatorId " &
                                   " WHERE [CRN]=@CRN " &
                            " End " &
                            " Else " &
                            " begin " &
                               " INSERT INTO [tblZolaris_EZoll_NCTS] " &
                               " ( [CRN], [AnmeldungsDatum], [GarantieWert], [AnmID], [ErledigungsTypID], [Warenbezeichnung], [Absender], [Empfaenger], [Gesamtrohmasse], [Abgangszollstelle], [Bestimmungszollstelle], [Gestellungsfrist], [LRN], [DatumBestimmungErreicht], [GRN], OperatorId ) VALUES " &
                               " ( @CRN, @AnmeldungsDatum, @GarantieWert, @AnmID, @ErledigungsTypID, @Warenbezeichnung, @Absender, @Empfaenger, @Gesamtrohmasse, @Abgangszollstelle, @Bestimmungszollstelle, @Gestellungsfrist, @LRN, @DatumBestimmungErreicht, @GRN, @OperatorId ) " &
                            "End " &
                            " commit tran "

                        Using conn2 As SqlConnection = GetNewOpenConnectionFMZOLL()
                            Using cmd2 As New SqlCommand(sql2, conn2)
                                'MsgBox(sql2 & vbNewLine & vbNewLine & dr.Item("CRN"))
                                '  cmd2.Parameters.AddWithValue("@ID_NCTS", dr.Item("ID_NCTS"))
                                cmd2.Parameters.AddWithValue("@CRN", dr.Item("CRN"))
                                cmd2.Parameters.AddWithValue("@AnmeldungsDatum", dr.Item("AnmeldungsDatum"))
                                cmd2.Parameters.AddWithValue("@GarantieWert", dr.Item("GarantieWert"))
                                cmd2.Parameters.AddWithValue("@AnmID", dr.Item("AnmID"))
                                cmd2.Parameters.AddWithValue("@ErledigungsTypID", dr.Item("ErledigungsTypID"))
                                cmd2.Parameters.AddWithValue("@Warenbezeichnung", dr.Item("Warenbezeichnung"))
                                cmd2.Parameters.AddWithValue("@Absender", dr.Item("Absender"))
                                cmd2.Parameters.AddWithValue("@Empfaenger", dr.Item("Empfaenger"))
                                cmd2.Parameters.AddWithValue("@Gesamtrohmasse", dr.Item("Gesamtrohmasse"))
                                cmd2.Parameters.AddWithValue("@Abgangszollstelle", dr.Item("Abgangszollstelle"))
                                cmd2.Parameters.AddWithValue("@Bestimmungszollstelle", dr.Item("Bestimmungszollstelle"))
                                cmd2.Parameters.AddWithValue("@Gestellungsfrist", dr.Item("Gestellungsfrist"))
                                cmd2.Parameters.AddWithValue("@LRN", dr.Item("LRN"))
                                cmd2.Parameters.AddWithValue("@DatumBestimmungErreicht", dr.Item("DatumBestimmungErreicht"))
                                cmd2.Parameters.AddWithValue("@GRN", dr.Item("GRN"))
                                cmd2.Parameters.AddWithValue("@OperatorId", dr.Item("OperatorId"))

                                Try
                                    cmd2.ExecuteNonQuery()

                                    cnt += 1

                                    ' setLabel(frmAtlasAufschub.lblCntBrg_ATLAS_SUB, cnt)

                                Catch ex As SqlException
                                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Data")
                                End Try
                            End Using
                        End Using

                    End While
                    '  setLabel(frmRoutineManager.lblEZOLL_Ds, cnt)

                    conn.Close()
                    conn.Dispose()
                    dr.Close()
                    dr = Nothing
                    Me.Finalize()
                    Return True
                Catch ex As Exception
                    VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodBase.GetCurrentMethod.Name, , "DB-Fehler")
                End Try
            End Using
        End Using
        Return False
    End Function


    'threadsicherer Aufruf
    Delegate Sub setLabelCallback(l As Label, t As String)
    Public Sub setLabel(l As Label, t As String)
        If frmRoutineManager.InvokeRequired Then
            Dim d As New setLabelCallback(AddressOf setLabel)
            frmRoutineManager.Invoke(d, New Object() {l, t})
        Else
            l.Text = t
        End If
    End Sub

End Class
