

Imports javax.xml.bind.annotation
Imports VERAG_PROG_ALLGEMEIN.agsCustomsExchange

Public Class usrCntlCBAM

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
    Public kdnr = -1
    Dim AD As VERAG_PROG_ALLGEMEIN.cAdressen = Nothing
    Dim KD As VERAG_PROG_ALLGEMEIN.cKunde = Nothing

    Sub New()
        InitializeComponent()
    End Sub

    Sub New(kdNr)
        InitializeComponent()
        Me.kdnr = kdNr
    End Sub


    Private Sub btnCBAMPruef_DAKOSY_Click(sender As Object, e As EventArgs) Handles btnCBAMPruef_DAKOSY.Click
        txtCBAMPruef_DAKOSY.Text = "-"
        Try
            Dim EORI = txtEori.Text
            If cbxVERAG.Checked Then EORI = "VERAG"
            If EORI = "" Then MsgBox("EORI eingeben!") : Exit Sub
            If txtVon._value = "" Then MsgBox("Datum von eingeben!") : Exit Sub
            If txtBis._value = "" Then MsgBox("Datum bis eingeben!") : Exit Sub
            Me.Cursor = Cursors.WaitCursor

            'isnull( eza_anmeldedatum, eza_Trans_DatumZeit)
            Dim SQLSTR = "
                SELECT  COUNT(*) FROM [tblDakosy_EZA] inner join tblDakosy_EZA_Warenposition on ezaWP_EzaId=eza_Id
                  where  eza_anmeldedatum between '" & txtVon._value & "' and '" & txtBis._value & "'
                  and 
                  (
                  left(ezaWP_WarennummerEZT,4) IN (2523,2716,2808,2814,3102,3105,7201,7203,7318,7326,7601,7616)
                  OR   left(ezaWP_WarennummerEZT,4)  between 7301 and 7311
                  OR   left(ezaWP_WarennummerEZT,4)  between 7205 and 7229
                  OR   left(ezaWP_WarennummerEZT,4)  between 7603 and 7614
                  OR   ezaWP_WarennummerEZT like '25070080%'
                  OR   ezaWP_WarennummerEZT like '283421%'
                  OR   ezaWP_WarennummerEZT like '260112%'
                  OR   ezaWP_WarennummerEZT like '720211%'
                  OR   ezaWP_WarennummerEZT like '720219%'
                  OR   ezaWP_WarennummerEZT like '720241%'
                  OR   ezaWP_WarennummerEZT like '720249%'
                  OR   ezaWP_WarennummerEZT like '720260%'
                  OR   ezaWP_WarennummerEZT like '280410%'
                     )
                  and ezaWP_WarennummerEZT not like '310560%'
                    and (eza_status between 50  and 60 or [eza_ANR] LIKE 'ATC%')
            "

            If cbxVERAG.Checked Then
                SQLSTR &= " 
                            and eza_VertretungsVerhaeltnisCode=2
                          "
            Else
                SQLSTR &= " 
                            and (select   isnull([ezaAd_NameFirma1],[ezaAd_TeilnehmerEORI]) FROM tblDakosy_EZA_Adressen WHERE ezaAd_EZAId=eza_Id and [ezaAd_AdressTyp]='CN')='" & EORI & "' 
                          "
            End If

            Dim dt = SQL.loadDgvBySql(SQLSTR, "FMZOLL", 600)
            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                txtCBAMPruef_DAKOSY.Text = dt.Rows(0)(0) & " Einträge"
                Me.Cursor = Cursors.Default
                Exit Sub
            End If

            txtCBAMPruef_DAKOSY.Text = "0"


        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnCBAMPruef_ALLE_Click(sender As Object, e As EventArgs) Handles btnCBAMPruef_ALLE.Click
        btnCBAMPruef_DAKOSY.PerformClick()
        btnCBAMPruef_DHF_VERAG.PerformClick()
        btnCBAMPruef_DHF_UNISPED.PerformClick()
        btnCBAMPruef_TELOTEC.PerformClick()
    End Sub

    Private Sub btnCBAMPruef_DHF_VERAG_Click(sender As Object, e As EventArgs) Handles btnCBAMPruef_DHF_VERAG.Click
        txtCBAMPruef_DHF_VERAG.Text = "-"
        Try
            Dim EORI = txtEori.Text
            If cbxVERAG.Checked Then EORI = "VERAG"
            If EORI = "" Then MsgBox("EORI eingeben!") : Exit Sub
            If txtVon._value = "" Then MsgBox("Datum von eingeben!") : Exit Sub
            If txtBis._value = "" Then MsgBox("Datum bis eingeben!") : Exit Sub
            Me.Cursor = Cursors.WaitCursor

            Dim SQLSTR = "
                 select COUNT(*) from zsAnmRefs 
				 inner join zsAnmDHFRefs on zsAnmRefs.LizenzNr=zsAnmDHFRefs.LizenzNr and zsAnmRefs.AnmID=zsAnmDHFRefs.AnmID and zsAnmRefs.VorgangID=zsAnmDHFRefs.VorgangID and zsAnmRefs.OperatorID=zsAnmDHFRefs.OperatorID
			     inner join zsAnmGdsitem  on zsAnmRefs.LizenzNr=zsAnmGdsitem.LizenzNr and zsAnmRefs.AnmID=zsAnmGdsitem.AnmID and zsAnmRefs.VorgangID=zsAnmGdsitem.VorgangID and zsAnmRefs.OperatorID=zsAnmGdsitem.OperatorID
				 inner join zsAnmHea on zsAnmHea.LizenzNr=zsAnmGdsitem.LizenzNr and zsAnmHea.AnmID=zsAnmGdsitem.AnmID and zsAnmHea.VorgangID=zsAnmGdsitem.VorgangID and zsAnmHea.OperatorID=zsAnmGdsitem.OperatorID
				 inner join zsAnmDeclarant on zsAnmHea.LizenzNr=zsAnmDeclarant.LizenzNr and zsAnmHea.AnmID=zsAnmDeclarant.AnmID and zsAnmHea.VorgangID=zsAnmDeclarant.VorgangID and zsAnmHea.OperatorID=zsAnmDeclarant.OperatorID
				 inner join zsAnmTransp on zsAnmHea.LizenzNr=zsAnmTransp.LizenzNr and zsAnmHea.AnmID=zsAnmTransp.AnmID and zsAnmHea.VorgangID=zsAnmTransp.VorgangID and zsAnmHea.OperatorID=zsAnmTransp.OperatorID
					 	where DecDT between '" & txtVon._value & "' and '" & txtBis._value & "'
                  and 
                  (
                  left(ComCd,4) IN (2523,2716,2808,2814,3102,3105,7201,7203,7318,7326,7601,7616)
                  OR   left(ComCd,4)  between 7301 and 7311
                  OR   left(ComCd,4)  between 7205 and 7229
                  OR   left(ComCd,4)  between 7603 and 7614
                  OR   ComCd like '25070080%'
                  OR   ComCd like '283421%'
                  OR   ComCd like '260112%'
                  OR   ComCd like '720211%'
                  OR   ComCd like '720219%'
                  OR   ComCd like '720241%'
                  OR   ComCd like '720249%'
                  OR   ComCd like '720260%'
                  OR   ComCd like '280410%'  
                  ) 
                  and ComCd not like '310560%' 
                  and crn is not null 
                  and zsAnmDHFRefs.ErledigungsTypID like 'F%'
                   "

            If cbxVERAG.Checked Then
                SQLSTR &= " 
                                and DecRep IN(3)
                              
                          "
            Else
                SQLSTR &= " 
                          and (SELECT TIN FROM zsAnmConeeTra WHERE (zsAnmConeeTra.LizenzNr =  zsAnmHea.LizenzNr ) And (zsAnmConeeTra.OperatorID =zsAnmHea.OperatorID ) And (zsAnmConeeTra.VorgangID = zsAnmHea.VorgangID ) And (zsAnmConeeTra.AnmID = zsAnmHea.AnmID )) = '" & EORI & "' 
                            "
            End If

            Dim dt = SQL.loadDgvBySql(SQLSTR, "EZOLL", 600)
            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                txtCBAMPruef_DHF_VERAG.Text = dt.Rows(0)(0) & " Einträge"
                Me.Cursor = Cursors.Default
                Exit Sub
            End If

            txtCBAMPruef_DHF_VERAG.Text = "0"

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnCBAMPruef_DHF_UNISPED_Click(sender As Object, e As EventArgs) Handles btnCBAMPruef_DHF_UNISPED.Click
        txtCBAMPruef_DHF_UNISPED.Text = "-"
        Try
            Dim EORI = txtEori.Text
            If cbxVERAG.Checked Then EORI = "VERAG"
            If EORI = "" Then MsgBox("EORI eingeben!") : Exit Sub
            If txtVon._value = "" Then MsgBox("Datum von eingeben!") : Exit Sub
            If txtBis._value = "" Then MsgBox("Datum bis eingeben!") : Exit Sub
            Me.Cursor = Cursors.WaitCursor

            Dim SQLSTR = "
                 select COUNT(*) from zsAnmRefs 
				  inner join zsAnmDHFRefs on zsAnmRefs.LizenzNr=zsAnmDHFRefs.LizenzNr and zsAnmRefs.AnmID=zsAnmDHFRefs.AnmID and zsAnmRefs.VorgangID=zsAnmDHFRefs.VorgangID and zsAnmRefs.OperatorID=zsAnmDHFRefs.OperatorID
			       inner join zsAnmGdsitem  on zsAnmRefs.LizenzNr=zsAnmGdsitem.LizenzNr and zsAnmRefs.AnmID=zsAnmGdsitem.AnmID and zsAnmRefs.VorgangID=zsAnmGdsitem.VorgangID and zsAnmRefs.OperatorID=zsAnmGdsitem.OperatorID
				 inner join zsAnmHea on zsAnmHea.LizenzNr=zsAnmGdsitem.LizenzNr and zsAnmHea.AnmID=zsAnmGdsitem.AnmID and zsAnmHea.VorgangID=zsAnmGdsitem.VorgangID and zsAnmHea.OperatorID=zsAnmGdsitem.OperatorID
				 inner join zsAnmDeclarant on zsAnmHea.LizenzNr=zsAnmDeclarant.LizenzNr and zsAnmHea.AnmID=zsAnmDeclarant.AnmID and zsAnmHea.VorgangID=zsAnmDeclarant.VorgangID and zsAnmHea.OperatorID=zsAnmDeclarant.OperatorID
				 inner join zsAnmTransp on zsAnmHea.LizenzNr=zsAnmTransp.LizenzNr and zsAnmHea.AnmID=zsAnmTransp.AnmID and zsAnmHea.VorgangID=zsAnmTransp.VorgangID and zsAnmHea.OperatorID=zsAnmTransp.OperatorID
					 	where DecDT between '" & txtVon._value & "' and '" & txtBis._value & "'
                  and 
                  (
                  left(ComCd,4) IN (2523,2716,2808,2814,3102,3105,7201,7203,7318,7326,7601,7616)
                  OR   left(ComCd,4)  between 7301 and 7311
                  OR   left(ComCd,4)  between 7205 and 7229
                  OR   left(ComCd,4)  between 7603 and 7614
                  OR   ComCd like '25070080%'
                  OR   ComCd like '283421%'
                  OR   ComCd like '260112%'
                  OR   ComCd like '720211%'
                  OR   ComCd like '720219%'
                  OR   ComCd like '720241%'
                  OR   ComCd like '720249%'
                  OR   ComCd like '720260%'
                  OR   ComCd like '280410%'  
                  ) 
                  and ComCd not like '310560%' 
                 and crn is not null 
                 and zsAnmDHFRefs.ErledigungsTypID like 'F%'
            "


            If cbxVERAG.Checked Then
                SQLSTR &= " 
                                and DecRep IN(3)
                                    "
            Else
                SQLSTR &= " 
                          and (SELECT TIN FROM zsAnmConeeTra WHERE (zsAnmConeeTra.LizenzNr =  zsAnmHea.LizenzNr ) And (zsAnmConeeTra.OperatorID =zsAnmHea.OperatorID ) And (zsAnmConeeTra.VorgangID = zsAnmHea.VorgangID ) And (zsAnmConeeTra.AnmID = zsAnmHea.AnmID )) = '" & EORI & "' 
                            "
            End If


            Dim dt = SQL.loadDgvBySql(SQLSTR, "EZOLL_UNISPED", 600)
            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                txtCBAMPruef_DHF_UNISPED.Text = dt.Rows(0)(0) & " Einträge"
                Me.Cursor = Cursors.Default
                Exit Sub
            End If

            txtCBAMPruef_DHF_UNISPED.Text = "0"

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnCBAMPruef_TELOTEC_Click(sender As Object, e As EventArgs) Handles btnCBAMPruef_TELOTEC.Click
        txtCBAMPruef_TELOTEC.Text = "nicht verfügbar"
        Exit Sub
        Try
            Dim EORI = txtEori.Text
            If cbxVERAG.Checked Then EORI = "VERAG"
            If EORI = "" Then MsgBox("EORI eingeben!") : Exit Sub
            Me.Cursor = Cursors.WaitCursor
            Dim SQLSTR = "
                

         
             "

            Dim dt = SQL.loadDgvBySql(SQLSTR, "FMZOLL", 600)
            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                txtCBAMPruef_TELOTEC.Text = dt.Rows(0)(0) & " Einträge"
                Me.Cursor = Cursors.Default
                Exit Sub
            End If

            txtCBAMPruef_TELOTEC.Text = "0"

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Me.Cursor = Cursors.Default

    End Sub

    Private Sub usrCntlCBAM_Load(sender As Object, e As EventArgs) Handles Me.Load
        txtJahr.Text = Now.Year


        If Now.Month < 3 Then
            txtJahr.Text = Now.Year - 1
            cboQuartal.Text = "Q4"
        ElseIf Now.Month < 6 Then : cboQuartal.Text = "Q1"
        ElseIf Now.Month < 9 Then : cboQuartal.Text = "Q2"
        ElseIf Now.Month < 12 Then : cboQuartal.Text = "Q3"
        End If

        If kdnr > 0 Then
            AD = New VERAG_PROG_ALLGEMEIN.cAdressen(kdnr)
            KD = New VERAG_PROG_ALLGEMEIN.cKunde(kdnr)
        End If
        If KD IsNot Nothing Then
            txtEori.Text = KD.EORITIN
        End If

    End Sub

    Private Sub cboQuartal_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboQuartal.SelectedIndexChanged
        If cboQuartal.Text <> "" And IsNumeric(txtJahr.Text) AndAlso txtJahr.Text.Length = 4 Then

            Select Case cboQuartal.Text
                Case "Q1" : txtVon._value = CDate("01.01." & txtJahr.Text) : txtBis._value = CDate(txtVon._value).AddMonths(3).AddDays(-1)
                Case "Q2" : txtVon._value = CDate("01.04." & txtJahr.Text) : txtBis._value = CDate(txtVon._value).AddMonths(3).AddDays(-1)
                Case "Q3" : txtVon._value = CDate("01.07." & txtJahr.Text) : txtBis._value = CDate(txtVon._value).AddMonths(3).AddDays(-1)
                Case "Q4" : txtVon._value = CDate("01.10." & txtJahr.Text) : txtBis._value = CDate(txtVon._value).AddMonths(3).AddDays(-1)

            End Select


        End If
    End Sub

    Private Sub btnCBAM_DS_DAKOSY_Click(sender As Object, e As EventArgs) Handles btnCBAM_DS_DAKOSY.Click
        btnCBAM_DS_DAKOSY_Deteil.Enabled = False
        btnCBAM_DS_DAKOSY_Copy.Enabled = False
        btnCBAM_DS_DAKOSY_Deteil.Tag = ""

        Try
            Dim EORI = txtEori.Text
            If cbxVERAG.Checked Then EORI = "VERAG"
            If EORI = "" Then MsgBox("EORI eingeben!") : Exit Sub
            If txtVon._value = "" Then MsgBox("Datum von eingeben!") : Exit Sub
            If txtBis._value = "" Then MsgBox("Datum bis eingeben!") : Exit Sub
            Me.Cursor = Cursors.WaitCursor

            Dim SQLSTR = "
               SELECT  
                      [eza_ANR] RegistriernummerATLAS 
	                  ,ezaWP_PositionsNummer PositionsNummer
                      ,isnull( eza_anmeldedatum, eza_Trans_DatumZeit) Anmeldedatum 
                      ,[eza_ObjectName] BezugsNr 
                      ,[eza_VertretungsVerhaeltnisCode] VertretungsVerhaeltnis 
                      ,ezaWP_WarennummerEZT Tarifnummer
	                  ,ezaWP_Eigenmasse Eigenmasse 
                      ,[eza_VersendungsLandCode] VersendungsLand
                      ,[eza_Bestimmungsland] Bestimmungsland
                      ,[ezaWP_Artikelpreis]Rechnungspreis
                      ,[ezaWP_ArtikelpreisWaehrung] Rechnungswaehrung
	                  ,(select   isnull([ezaAd_NameFirma1],[ezaAd_TeilnehmerEORI]) FROM tblDakosy_EZA_Adressen WHERE ezaAd_EZAId=eza_Id and [ezaAd_AdressTyp]='CN') as Empfänger
	                  ,(select   isnull([ezaAd_NameFirma1],[ezaAd_TeilnehmerEORI]) FROM tblDakosy_EZA_Adressen WHERE ezaAd_EZAId=eza_Id and [ezaAd_AdressTyp]='CZ') as Absender
	  
                  FROM [VERAG].[dbo].[tblDakosy_EZA]
                  inner join tblDakosy_EZA_Warenposition on ezaWP_EzaId=eza_Id 
                  where  
                  eza_anmeldedatum between '" & txtVon._value & "' and '" & txtBis._value & "'
                  and 
                  (
                  left(ezaWP_WarennummerEZT,4) IN (2523,2716,2808,2814,3102,3105,7201,7203,7318,7326,7601,7616)
                  OR   left(ezaWP_WarennummerEZT,4)  between 7301 and 7311
                  OR   left(ezaWP_WarennummerEZT,4)  between 7205 and 7229
                  OR   left(ezaWP_WarennummerEZT,4)  between 7603 and 7614
                  OR   ezaWP_WarennummerEZT like '25070080%'
                  OR   ezaWP_WarennummerEZT like '283421%'
                  OR   ezaWP_WarennummerEZT like '260112%'
                  OR   ezaWP_WarennummerEZT like '720211%'
                  OR   ezaWP_WarennummerEZT like '720219%'
                  OR   ezaWP_WarennummerEZT like '720241%'
                  OR   ezaWP_WarennummerEZT like '720249%'
                  OR   ezaWP_WarennummerEZT like '720260%'
                  OR   ezaWP_WarennummerEZT like '280410%' 
                  )
                  and ezaWP_WarennummerEZT not like '310560%' 
                     and (eza_status between 50  and 60 or [eza_ANR] LIKE 'ATC%')
                  
            "


            If cbxVERAG.Checked Then
                SQLSTR &= " 
                            and eza_VertretungsVerhaeltnisCode=2
                          "
            Else
                SQLSTR &= " 
                            and (select   isnull([ezaAd_NameFirma1],[ezaAd_TeilnehmerEORI]) FROM tblDakosy_EZA_Adressen WHERE ezaAd_EZAId=eza_Id and [ezaAd_AdressTyp]='CN')='" & EORI & "' 
                          "
            End If

            SQLSTR &= "  order by Absender,eza_ANR, tblDakosy_EZA_Warenposition.ezaWP_PositionsNummer"


            Dim dt = SQL.loadDgvBySql(SQLSTR, "FMZOLL", 600)
            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                Dim path = VERAG_PROG_ALLGEMEIN.cProgramFunctions.genExcelFromDT_NEW(dt,,,,, False, EORI & "_Detailbericht_" & txtVon._value & "-" & txtBis._value)
                btnCBAM_DS_DAKOSY_Deteil.Tag = path
                btnCBAM_DS_DAKOSY_Deteil.Enabled = True
                btnCBAM_DS_DAKOSY_Copy.Enabled = True
                Me.Cursor = Cursors.Default

            End If



        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Me.Cursor = Cursors.Default


        '-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


        btnCBAM_DS_DAKOSY_Summe.Enabled = False
        btnCBAM_DS_DAKOSY_Summe.Tag = ""

        Try
            Dim EORI = txtEori.Text
            If cbxVERAG.Checked Then EORI = "VERAG"
            If EORI = "" Then MsgBox("EORI eingeben!") : Exit Sub
            If txtVon._value = "" Then MsgBox("Datum von eingeben!") : Exit Sub
            If txtBis._value = "" Then MsgBox("Datum bis eingeben!") : Exit Sub
            Me.Cursor = Cursors.WaitCursor

            Dim SQLSTR = "
                SELECT  
                     left(ezaWP_WarennummerEZT,8) Tarifnummer,sum(ezaWP_Eigenmasse)Eigenmasse,sum(ezaWP_Rohmasse)Rohmasse,sum(ezaWP_Artikelpreis)Rechnungspreis,eza_VersendungsLandCode VersendungsLand ,isnull(ezaWP_AnmeldeVErfahren,'') Verfahren,isnull([ezaWP_VerfahrensCodeVorangegangenesVerfahren],'') VorangegangenesVerfahren
                        
                    FROM [VERAG].[dbo].[tblDakosy_EZA]
                  inner join tblDakosy_EZA_Warenposition on ezaWP_EzaId=eza_Id
                   where  
                    eza_anmeldedatum between '" & txtVon._value & "' and '" & txtBis._value & "'
                  and 
                  (
                  left(ezaWP_WarennummerEZT,4) IN (2523,2716,2808,2814,3102,3105,7201,7203,7318,7326,7601,7616)
                  OR   left(ezaWP_WarennummerEZT,4)  between 7301 and 7311
                  OR   left(ezaWP_WarennummerEZT,4)  between 7205 and 7229
                  OR   left(ezaWP_WarennummerEZT,4)  between 7603 and 7614
                  OR   ezaWP_WarennummerEZT like '25070080%'
                  OR   ezaWP_WarennummerEZT like '283421%'
                  OR   ezaWP_WarennummerEZT like '260112%'
                  OR   ezaWP_WarennummerEZT like '720211%'
                  OR   ezaWP_WarennummerEZT like '720219%'
                  OR   ezaWP_WarennummerEZT like '720241%'
                  OR   ezaWP_WarennummerEZT like '720249%'
                  OR   ezaWP_WarennummerEZT like '720260%'
                  OR   ezaWP_WarennummerEZT like '280410%'
   
                  )
                    and ezaWP_WarennummerEZT not like '310560%'
                        and (eza_status between 50  and 60 or [eza_ANR] LIKE 'ATC%')
                    
            "

            If cbxVERAG.Checked Then
                SQLSTR &= " 
                            and eza_VertretungsVerhaeltnisCode=2
                          "
            Else
                SQLSTR &= " 
                            and (select   isnull([ezaAd_NameFirma1],[ezaAd_TeilnehmerEORI]) FROM tblDakosy_EZA_Adressen WHERE ezaAd_EZAId=eza_Id and [ezaAd_AdressTyp]='CN')='" & EORI & "' 
                          "
            End If

            SQLSTR &= "   group by eza_VersendungsLandCode,left(ezaWP_WarennummerEZT,8),ezaWP_AnmeldeVErfahren,ezaWP_VerfahrensCodeVorangegangenesVerfahren"


            Dim dt = SQL.loadDgvBySql(SQLSTR, "FMZOLL", 600)
            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                Dim path = VERAG_PROG_ALLGEMEIN.cProgramFunctions.genExcelFromDT_NEW(dt,,,,, False, EORI & "_Summenbericht_" & txtVon._value & "-" & txtBis._value)
                btnCBAM_DS_DAKOSY_Summe.Tag = path
                btnCBAM_DS_DAKOSY_Summe.Enabled = True
                btnCBAM_DS_DAKOSY_Copy.Enabled = True

                Me.Cursor = Cursors.Default

            End If



            ''----- XML ----------------------------------------------------------------------------------------------------------------------------------------

            'btnCBAM_DS_DAKOSY_XML.Enabled = False
            'btnCBAM_DS_DAKOSY_XML.Tag = ""

            'If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
            '    '---------------------------------------------------------------------------------------------------------------------------------------------
            '    doCBAM_XML(dt, EORI)
            '    '---------------------------------------------------------------------------------------------------------------------------------------------

            'End If


            ''---------------------------------------------------------------------------------------------------------------------------------------------

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try





        Me.Cursor = Cursors.Default

    End Sub

    Private Sub ButtonXML_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button2.Enabled = False
        btnCBAM_DS_DAKOSY_XML.Enabled = False
        btnCBAM_DS_DAKOSY_XML.Tag = ""


        '-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


        Try
            Dim EORI = txtEori.Text
            If cbxVERAG.Checked Then EORI = "VERAG"
            If EORI = "" Then MsgBox("EORI eingeben!") : Exit Sub
            If txtVon._value = "" Then MsgBox("Datum von eingeben!") : Exit Sub
            If txtBis._value = "" Then MsgBox("Datum bis eingeben!") : Exit Sub
            Me.Cursor = Cursors.WaitCursor

            Dim SQLSTR = "
                SELECT  
                     left(ezaWP_WarennummerEZT,8) Tarifnummer,sum(ezaWP_Eigenmasse)Eigenmasse,sum(ezaWP_Rohmasse)Rohmasse,sum(ezaWP_Artikelpreis)Rechnungspreis,eza_VersendungsLandCode VersendungsLand ,isnull(ezaWP_AnmeldeVErfahren,'') Verfahren,isnull([ezaWP_VerfahrensCodeVorangegangenesVerfahren],'') VorangegangenesVerfahren
                   ,isnull(ezaAd_NameFirma1,'') as Empfänger,isnull(ezaAd_TeilnehmerEORI,'') as EmpfängerEORI
	                 
                    FROM [VERAG].[dbo].[tblDakosy_EZA]
                  inner join tblDakosy_EZA_Warenposition on ezaWP_EzaId=eza_Id
                  inner join tblDakosy_EZA_Adressen on ezaAd_EZAId=eza_Id and [ezaAd_AdressTyp]='CN' 
                   where  
                    eza_anmeldedatum between '" & txtVon._value & "' and '" & txtBis._value & "'
                  and 
                  (
                  left(ezaWP_WarennummerEZT,4) IN (2523,2716,2808,2814,3102,3105,7201,7203,7318,7326,7601,7616)
                  OR   left(ezaWP_WarennummerEZT,4)  between 7301 and 7311
                  OR   left(ezaWP_WarennummerEZT,4)  between 7205 and 7229
                  OR   left(ezaWP_WarennummerEZT,4)  between 7603 and 7614
                  OR   ezaWP_WarennummerEZT like '25070080%'
                  OR   ezaWP_WarennummerEZT like '283421%'
                  OR   ezaWP_WarennummerEZT like '260112%'
                  OR   ezaWP_WarennummerEZT like '720211%'
                  OR   ezaWP_WarennummerEZT like '720219%'
                  OR   ezaWP_WarennummerEZT like '720241%'
                  OR   ezaWP_WarennummerEZT like '720249%'
                  OR   ezaWP_WarennummerEZT like '720260%'
                  OR   ezaWP_WarennummerEZT like '280410%'
   
                  )
                    and ezaWP_WarennummerEZT not like '310560%'
                        and (eza_status between 50  and 60 or [eza_ANR] LIKE 'ATC%')
                    
            "

            If cbxVERAG.Checked Then
                SQLSTR &= " 
                            and eza_VertretungsVerhaeltnisCode=2
                          "
            Else
                SQLSTR &= " 
                            and (select   isnull([ezaAd_NameFirma1],[ezaAd_TeilnehmerEORI]) FROM tblDakosy_EZA_Adressen WHERE ezaAd_EZAId=eza_Id and [ezaAd_AdressTyp]='CN')='" & EORI & "' 
                          "
            End If

            SQLSTR &= "   group by ezaAd_NameFirma1,ezaAd_TeilnehmerEORI,eza_VersendungsLandCode,left(ezaWP_WarennummerEZT,8),ezaWP_AnmeldeVErfahren,ezaWP_VerfahrensCodeVorangegangenesVerfahren"


            Dim dt = SQL.loadDgvBySql(SQLSTR, "FMZOLL", 600)




            '----- XML ----------------------------------------------------------------------------------------------------------------------------------------


            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                '---------------------------------------------------------------------------------------------------------------------------------------------
                doCBAM_XML(dt, EORI)
                '---------------------------------------------------------------------------------------------------------------------------------------------
                Button2.Enabled = True
                btnCBAM_DS_DAKOSY_XML.Enabled = True

            End If


            '---------------------------------------------------------------------------------------------------------------------------------------------

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try

        Me.Cursor = Cursors.Default

    End Sub

    Private Sub doCBAM_XML(dt As DataTable, EORI As String)
        Dim CBAM_XML As New VERAG_PROG_ALLGEMEIN.CBAM_QReport_17.QReportType

        CBAM_XML.Year = txtJahr.Text
        CBAM_XML.ReportingPeriod = cboQuartal._value
        EORI = txtEori.Text ' Sont VERAG problem..

        If cbxVERAG.Checked Then ' VERAG--> Indirect Representative
            CBAM_XML.Representative.IdentificationNumber = EORI
            Dim AD As VERAG_PROG_ALLGEMEIN.cAdressen = Nothing
            Select Case EORI
                Case "DE4824105"
                    AD = New VERAG_PROG_ALLGEMEIN.cAdressen(712255) 'VERAG GmbH (DE)
                Case "ATEOS1000000199"
                    AD = New VERAG_PROG_ALLGEMEIN.cAdressen(711104) 'VERAG AG (AT)
                Case "ATEOS1000059121"
                    AD = New VERAG_PROG_ALLGEMEIN.cAdressen(600006) 'VERAG Customs Service GmbH (AT)

            End Select
            If AD IsNot Nothing Then
                CBAM_XML.Representative.Name = AD.Name_1
                If If(AD.Name_2, "") <> "" Then CBAM_XML.Representative.Name &= " " & AD.Name_2
                If AD.LandKz <> "" Then CBAM_XML.Representative.RepresentativeAddress.Country = VERAG_PROG_ALLGEMEIN.cProgramFunctions.getISO2Land(AD.LandKz)
                CBAM_XML.Representative.RepresentativeAddress.Street = If(AD.Straße, "")
                CBAM_XML.Representative.RepresentativeAddress.Postcode = If(AD.PLZ, "")
            End If
        End If

        'Dim KD_IMP = VERAG_PROG_ALLGEMEIN.cKunde.LOAD_ByEORI(EORI)
        'If KD_IMP.hasEntry Then
        '    Dim A_IMP As New VERAG_PROG_ALLGEMEIN.cAdressen(KD_IMP.KundenNrZentrale)
        '    If A_IMP IsNot Nothing AndAlso A_IMP.hasEntry Then
        '        CBAM_XML.Importer.Name = A_IMP.Name_1
        '        If If(A_IMP.Name_2, "") <> "" Then CBAM_XML.Importer.Name &= " " & A_IMP.Name_2
        '        If A_IMP.LandKz <> "" Then CBAM_XML.Importer.ImporterAddress.Country = VERAG_PROG_ALLGEMEIN.cProgramFunctions.getISO2Land(A_IMP.LandKz)
        '        CBAM_XML.Importer.ImporterAddress.Street = If(A_IMP.Straße, "")
        '        CBAM_XML.Importer.ImporterAddress.Postcode = If(A_IMP.PLZ, "")
        '    End If
        'End If


        For Each g In dt.Rows
            Dim GOODS As New VERAG_PROG_ALLGEMEIN.CBAM_QReport_17.ImportedGoodType
            GOODS.CommodityCode.HsCode = g("Tarifnummer").ToString.Substring(0, 6)
            GOODS.CommodityCode.CnCode = g("Tarifnummer").ToString.Substring(6, 2)
            GOODS.OriginCountry.Country = g("VersendungsLand")
            GOODS.MeasureImported.NetMass = g("Eigenmasse")
            GOODS.MeasureImported.MeasurementUnit = "KG"

            ' ezaAd_NameFirma1, ezaAd_TeilnehmerEORI,
            If g("EmpfängerEORI") <> "" Then


                Dim KD_IMP = VERAG_PROG_ALLGEMEIN.cKunde.LOAD_ByEORI(g("EmpfängerEORI"))
                If KD_IMP.hasEntry Then
                    Dim A_IMP As New VERAG_PROG_ALLGEMEIN.cAdressen(KD_IMP.KundenNrZentrale)
                    If A_IMP IsNot Nothing AndAlso A_IMP.hasEntry Then
                        GOODS.Importer.Name = A_IMP.Name_1
                        If If(A_IMP.Name_2, "") <> "" Then GOODS.Importer.Name &= " " & A_IMP.Name_2
                        If A_IMP.LandKz <> "" Then GOODS.Importer.ImporterAddress.Country = VERAG_PROG_ALLGEMEIN.cProgramFunctions.getISO2Land(A_IMP.LandKz)
                        GOODS.Importer.ImporterAddress.Street = If(A_IMP.Straße, "")
                        GOODS.Importer.ImporterAddress.Postcode = If(A_IMP.PLZ, "")
                    End If
                End If
            Else
                GOODS.Importer.Name = g("Empfänger")

            End If


            Dim IQ As New VERAG_PROG_ALLGEMEIN.CBAM_QReport_17.ImportedQuantityType
            If g("Verfahren") <> "" Then IQ.Procedure.RequestedProc = g("Verfahren").ToString.Substring(0, 2)
            If g("VorangegangenesVerfahren") <> "" Then IQ.Procedure.PreviousProc = g("VorangegangenesVerfahren").ToString.Substring(0, 2)
            GOODS.ImportedQuantity.Add(IQ)
            CBAM_XML.ImportedGood.Add(GOODS)
        Next

        Dim optFileName = EORI & "_XML_" & txtVon._value & "-" & txtBis._value
        Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\VERAG\SDL\tmp\" ' My.Computer.FileSystem.GetTempFileName
        If Not My.Computer.FileSystem.DirectoryExists(sPath) Then
            My.Computer.FileSystem.CreateDirectory(sPath)
        End If

        Dim filename = sPath & optFileName & ".xml"
        While System.IO.File.Exists(filename)
            filename = sPath & optFileName & Now.ToString("ddMMyyyyHHmmss") & ".xml"
        End While


        CBAM_XML.SaveToFile(filename)
        btnCBAM_DS_DAKOSY_XML.Tag = filename
        btnCBAM_DS_DAKOSY_XML.Enabled = True

        'Me.Cursor = Cursors.WaitCursor
    End Sub


    Private Sub btnCBAM_DS_DAKOSY_Deteil_Click(sender As Object, e As EventArgs) Handles btnCBAM_DS_DAKOSY_Deteil.Click, btnCBAM_DS_DAKOSY_Summe.Click, btnCBAM_DS_DHF_VERAG_Deteil.Click,
        btnCBAM_DS_DHF_VERAG_Summe.Click, btnCBAM_DS_DHF_UNISPED_Deteil.Click, btnCBAM_DS_DHF_UNISPED_Summe.Click, btnCBAM_DS_TELOTEC_Detail.Click, btnCBAM_DS_TELOTEC_Summe.Click,
        btnCBAM_DS_DAKOSY_XML.Click

        If sender.tag <> "" Then
            Try : Process.Start(sender.tag) : Catch : End Try
        End If
    End Sub

    Private Sub btnCBAM_DS_DAKOSY_Copy_Click(sender As Object, e As EventArgs) Handles btnCBAM_DS_DAKOSY_Copy.Click
        Dim f As New List(Of String)
        If btnCBAM_DS_DAKOSY_Deteil.Tag <> "" Then f.Add(btnCBAM_DS_DAKOSY_Deteil.Tag)
        If btnCBAM_DS_DAKOSY_Summe.Tag <> "" Then f.Add(btnCBAM_DS_DAKOSY_Summe.Tag)

        If f.Count > 0 Then
            'Zwischenablage
            Clipboard.Clear()
            Dim d As New DataObject(DataFormats.FileDrop, f.ToArray)
            Clipboard.SetDataObject(d, True)
        End If
    End Sub

    Private Sub btnCBAM_DS_DHF_VERAG_Copy_Click(sender As Object, e As EventArgs) Handles btnCBAM_DS_DHF_VERAG_Copy.Click
        Dim f As New List(Of String)
        If btnCBAM_DS_DHF_VERAG_Deteil.Tag <> "" Then f.Add(btnCBAM_DS_DHF_VERAG_Deteil.Tag)
        If btnCBAM_DS_DHF_VERAG_Summe.Tag <> "" Then f.Add(btnCBAM_DS_DHF_VERAG_Summe.Tag)

        If f.Count > 0 Then
            'Zwischenablage
            Clipboard.Clear()
            Dim d As New DataObject(DataFormats.FileDrop, f.ToArray)
            Clipboard.SetDataObject(d, True)
        End If
    End Sub

    Private Sub btnCBAM_DS_DHF_UNISPED_Copy_Click(sender As Object, e As EventArgs) Handles btnCBAM_DS_DHF_UNISPED_Copy.Click
        Dim f As New List(Of String)
        If btnCBAM_DS_DHF_UNISPED_Deteil.Tag <> "" Then f.Add(btnCBAM_DS_DHF_UNISPED_Deteil.Tag)
        If btnCBAM_DS_DHF_UNISPED_Summe.Tag <> "" Then f.Add(btnCBAM_DS_DHF_UNISPED_Summe.Tag)

        If f.Count > 0 Then
            'Zwischenablage
            Clipboard.Clear()
            Dim d As New DataObject(DataFormats.FileDrop, f.ToArray)
            Clipboard.SetDataObject(d, True)
        End If
    End Sub

    Private Sub btnCBAM_DS_TELOTEC_Copy_Click(sender As Object, e As EventArgs) Handles btnCBAM_DS_TELOTEC_Copy.Click
        Dim f As New List(Of String)
        If btnCBAM_DS_TELOTEC_Detail.Tag <> "" Then f.Add(btnCBAM_DS_TELOTEC_Detail.Tag)
        If btnCBAM_DS_TELOTEC_Summe.Tag <> "" Then f.Add(btnCBAM_DS_TELOTEC_Summe.Tag)

        If f.Count > 0 Then
            'Zwischenablage
            Clipboard.Clear()
            Dim d As New DataObject(DataFormats.FileDrop, f.ToArray)
            Clipboard.SetDataObject(d, True)
        End If
    End Sub

    Private Sub btnCBAM_DS_DHF_VERAG_Click(sender As Object, e As EventArgs) Handles btnCBAM_DS_DHF_VERAG.Click, btnCBAM_DS_DHF_UNISPED.Click
        Dim EZOLL_SQL = "EZOLL"
        If sender Is btnCBAM_DS_DHF_VERAG Then
            btnCBAM_DS_DHF_VERAG_Deteil.Enabled = False
            btnCBAM_DS_DHF_VERAG_Copy.Enabled = False
            btnCBAM_DS_DHF_VERAG_Deteil.Tag = ""
        End If
        If sender Is btnCBAM_DS_DHF_UNISPED Then
            btnCBAM_DS_DHF_UNISPED_Deteil.Enabled = False
            btnCBAM_DS_DHF_UNISPED_Copy.Enabled = False
            btnCBAM_DS_DHF_UNISPED_Deteil.Tag = ""
            EZOLL_SQL = "EZOLL_UNISPED"
        End If

        Try
            Dim EORI = txtEori.Text
            If cbxVERAG.Checked Then EORI = "VERAG"
            If EORI = "" Then MsgBox("EORI eingeben!") : Exit Sub
            If txtVon._value = "" Then MsgBox("Datum von eingeben!") : Exit Sub
            If txtBis._value = "" Then MsgBox("Datum bis eingeben!") : Exit Sub
            Me.Cursor = Cursors.WaitCursor

            Dim SQLSTR = "
               select  CRN Zollbelegnummer,cast(DecDT as date) Anmeldedatum,LRN,zsAnmGdsitem.ComCd Tarifnummer,Net Eigenmasse,ItVal Rechnungspreis
				                 , Orig Ursprungsland

				                 , isnull((SELECT TIN FROM zsAnmConorTra WHERE (zsAnmConorTra.LizenzNr =  zsAnmHea.LizenzNr ) And (zsAnmConorTra.OperatorID =zsAnmHea.OperatorID ) And (zsAnmConorTra.VorgangID = zsAnmHea.VorgangID ) And (zsAnmConorTra.AnmID = zsAnmHea.AnmID )),'') EORI_Absender 
				                 , isnull((SELECT Na FROM zsAnmConorTra WHERE (zsAnmConorTra.LizenzNr =  zsAnmHea.LizenzNr ) And (zsAnmConorTra.OperatorID =zsAnmHea.OperatorID ) And (zsAnmConorTra.VorgangID = zsAnmHea.VorgangID ) And (zsAnmConorTra.AnmID = zsAnmHea.AnmID )),'') Absender
				                 , isnull((SELECT TIN FROM zsAnmConeeTra WHERE (zsAnmConeeTra.LizenzNr =  zsAnmHea.LizenzNr ) And (zsAnmConeeTra.OperatorID =zsAnmHea.OperatorID ) And (zsAnmConeeTra.VorgangID = zsAnmHea.VorgangID ) And (zsAnmConeeTra.AnmID = zsAnmHea.AnmID )),'')EORI_Empfänger
				                 , isnull((SELECT Na FROM zsAnmConeeTra WHERE (zsAnmConeeTra.LizenzNr =  zsAnmHea.LizenzNr ) And (zsAnmConeeTra.OperatorID =zsAnmHea.OperatorID ) And (zsAnmConeeTra.VorgangID = zsAnmHea.VorgangID ) And (zsAnmConeeTra.AnmID = zsAnmHea.AnmID )),'')Empfänger
				                   from  				 zsAnmRefs 
				                 inner join zsAnmDHFRefs on zsAnmRefs.LizenzNr=zsAnmDHFRefs.LizenzNr and zsAnmRefs.AnmID=zsAnmDHFRefs.AnmID and zsAnmRefs.VorgangID=zsAnmDHFRefs.VorgangID and zsAnmRefs.OperatorID=zsAnmDHFRefs.OperatorID
			                     inner join zsAnmGdsitem  on zsAnmRefs.LizenzNr=zsAnmGdsitem.LizenzNr and zsAnmRefs.AnmID=zsAnmGdsitem.AnmID and zsAnmRefs.VorgangID=zsAnmGdsitem.VorgangID and zsAnmRefs.OperatorID=zsAnmGdsitem.OperatorID
				                 inner join zsAnmHea on zsAnmHea.LizenzNr=zsAnmGdsitem.LizenzNr and zsAnmHea.AnmID=zsAnmGdsitem.AnmID and zsAnmHea.VorgangID=zsAnmGdsitem.VorgangID and zsAnmHea.OperatorID=zsAnmGdsitem.OperatorID
				                 inner join zsAnmDeclarant on zsAnmHea.LizenzNr=zsAnmDeclarant.LizenzNr and zsAnmHea.AnmID=zsAnmDeclarant.AnmID and zsAnmHea.VorgangID=zsAnmDeclarant.VorgangID and zsAnmHea.OperatorID=zsAnmDeclarant.OperatorID
				                 inner join zsAnmTransp on zsAnmHea.LizenzNr=zsAnmTransp.LizenzNr and zsAnmHea.AnmID=zsAnmTransp.AnmID and zsAnmHea.VorgangID=zsAnmTransp.VorgangID and zsAnmHea.OperatorID=zsAnmTransp.OperatorID
					 	                where DecDT between '" & txtVon._value & "' and '" & txtBis._value & "'
					                  and 
                  (
                  left(ComCd,4) IN (2523,2716,2808,2814,3102,3105,7201,7203,7318,7326,7601,7616)
                  OR   left(ComCd,4)  between 7301 and 7311
                  OR   left(ComCd,4)  between 7205 and 7229
                  OR   left(ComCd,4)  between 7603 and 7614
                  OR   ComCd like '25070080%'
                  OR   ComCd like '283421%'
                  OR   ComCd like '260112%'
                  OR   ComCd like '720211%'
                  OR   ComCd like '720219%'
                  OR   ComCd like '720241%'
                  OR   ComCd like '720249%'
                  OR   ComCd like '720260%'
                  OR   ComCd like '280410%'  
                  ) 
                  and ComCd not like '310560%' 
                 and crn is not null
                and zsAnmDHFRefs.ErledigungsTypID like 'F%'
             "


            If cbxVERAG.Checked Then
                SQLSTR &= " 
                                and DecRep IN(3)
                             "
            Else
                SQLSTR &= " 
                             and (SELECT TIN FROM zsAnmConeeTra WHERE (zsAnmConeeTra.LizenzNr =  zsAnmHea.LizenzNr ) And (zsAnmConeeTra.OperatorID =zsAnmHea.OperatorID ) And (zsAnmConeeTra.VorgangID = zsAnmHea.VorgangID ) And (zsAnmConeeTra.AnmID = zsAnmHea.AnmID )) LIKE '" & EORI & "' 
                 "
            End If

            SQLSTR &= "   order by  Absender"

            Dim dt = SQL.loadDgvBySql(SQLSTR, EZOLL_SQL, 600)
            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                Dim path = VERAG_PROG_ALLGEMEIN.cProgramFunctions.genExcelFromDT_NEW(dt,,,,, False, EORI & "_Detailbericht_" & txtVon._value & "-" & txtBis._value)
                If sender Is btnCBAM_DS_DHF_VERAG Then
                    btnCBAM_DS_DHF_VERAG_Deteil.Tag = path
                    btnCBAM_DS_DHF_VERAG_Deteil.Enabled = True
                    btnCBAM_DS_DHF_VERAG_Copy.Enabled = True
                End If
                If sender Is btnCBAM_DS_DHF_UNISPED Then
                    btnCBAM_DS_DHF_UNISPED_Deteil.Tag = path
                    btnCBAM_DS_DHF_UNISPED_Deteil.Enabled = True
                    btnCBAM_DS_DHF_UNISPED_Copy.Enabled = True
                End If
                Me.Cursor = Cursors.Default

                End If



        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Me.Cursor = Cursors.Default


        '-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


        If sender Is btnCBAM_DS_DHF_VERAG Then
            btnCBAM_DS_DHF_VERAG_Summe.Enabled = False
            btnCBAM_DS_DHF_VERAG_Summe.Tag = ""
        End If

        If sender Is btnCBAM_DS_DHF_UNISPED Then
            btnCBAM_DS_DHF_UNISPED_Summe.Enabled = False
            btnCBAM_DS_DHF_UNISPED_Summe.Tag = ""
        End If

        Try
            Dim EORI = txtEori.Text
            If cbxVERAG.Checked Then EORI = "VERAG"
            If EORI = "" Then MsgBox("EORI eingeben!") : Exit Sub
            If txtVon._value = "" Then MsgBox("Datum von eingeben!") : Exit Sub
            If txtBis._value = "" Then MsgBox("Datum bis eingeben!") : Exit Sub
            Me.Cursor = Cursors.WaitCursor

            Dim SQLSTR = "
                 select   left(zsAnmGdsitem.ComCd,8) Tarifnummer, sum(Net)Eigenmasse, sum(ItVal)Rechnungspreis,Orig
  		                       from  				 zsAnmRefs 
				                    inner join zsAnmDHFRefs on zsAnmRefs.LizenzNr=zsAnmDHFRefs.LizenzNr and zsAnmRefs.AnmID=zsAnmDHFRefs.AnmID and zsAnmRefs.VorgangID=zsAnmDHFRefs.VorgangID and zsAnmRefs.OperatorID=zsAnmDHFRefs.OperatorID
			                          inner join zsAnmGdsitem  on zsAnmRefs.LizenzNr=zsAnmGdsitem.LizenzNr and zsAnmRefs.AnmID=zsAnmGdsitem.AnmID and zsAnmRefs.VorgangID=zsAnmGdsitem.VorgangID and zsAnmRefs.OperatorID=zsAnmGdsitem.OperatorID
				                     inner join zsAnmHea on zsAnmHea.LizenzNr=zsAnmGdsitem.LizenzNr and zsAnmHea.AnmID=zsAnmGdsitem.AnmID and zsAnmHea.VorgangID=zsAnmGdsitem.VorgangID and zsAnmHea.OperatorID=zsAnmGdsitem.OperatorID
				                     inner join zsAnmDeclarant on zsAnmHea.LizenzNr=zsAnmDeclarant.LizenzNr and zsAnmHea.AnmID=zsAnmDeclarant.AnmID and zsAnmHea.VorgangID=zsAnmDeclarant.VorgangID and zsAnmHea.OperatorID=zsAnmDeclarant.OperatorID
				                     inner join zsAnmTransp on zsAnmHea.LizenzNr=zsAnmTransp.LizenzNr and zsAnmHea.AnmID=zsAnmTransp.AnmID and zsAnmHea.VorgangID=zsAnmTransp.VorgangID and zsAnmHea.OperatorID=zsAnmTransp.OperatorID
					 	                    where DecDT between '" & txtVon._value & "' and '" & txtBis._value & "'
					                      and 
                      (
                      left(ComCd,4) IN (2523,2716,2808,2814,3102,3105,7201,7203,7318,7326,7601,7616)
                      OR   left(ComCd,4)  between 7301 and 7311
                      OR   left(ComCd,4)  between 7205 and 7229
                      OR   left(ComCd,4)  between 7603 and 7614
                      OR   ComCd like '25070080%'
                      OR   ComCd like '283421%'
                      OR   ComCd like '260112%'
                      OR   ComCd like '720211%'
                      OR   ComCd like '720219%'
                      OR   ComCd like '720241%'
                      OR   ComCd like '720249%'
                      OR   ComCd like '720260%'
                      OR   ComCd like '280410%'  
                      ) 
                      and ComCd not like '310560%' 
                     and crn is not null
                       and zsAnmDHFRefs.ErledigungsTypID like 'F%'  
            "

            If cbxVERAG.Checked Then
                SQLSTR &= " 
                                and DecRep IN(3)
                             "
            Else
                SQLSTR &= " 
                             and (SELECT TIN FROM zsAnmConeeTra WHERE (zsAnmConeeTra.LizenzNr =  zsAnmHea.LizenzNr ) And (zsAnmConeeTra.OperatorID =zsAnmHea.OperatorID ) And (zsAnmConeeTra.VorgangID = zsAnmHea.VorgangID ) And (zsAnmConeeTra.AnmID = zsAnmHea.AnmID )) LIKE '" & EORI & "' 
                 "
            End If
            SQLSTR &= " GROUP BY Orig,left(zsAnmGdsitem.ComCd,8) "


            Dim dt = SQL.loadDgvBySql(SQLSTR, EZOLL_SQL, 600)
            If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                Dim path = VERAG_PROG_ALLGEMEIN.cProgramFunctions.genExcelFromDT_NEW(dt,,,,, False, EORI & "_Summenbericht_" & txtVon._value & "-" & txtBis._value)
                If sender Is btnCBAM_DS_DHF_VERAG Then
                    btnCBAM_DS_DHF_VERAG_Summe.Tag = path
                    btnCBAM_DS_DHF_VERAG_Summe.Enabled = True
                    btnCBAM_DS_DHF_VERAG_Copy.Enabled = True
                End If
                If sender Is btnCBAM_DS_DHF_UNISPED Then
                    btnCBAM_DS_DHF_UNISPED_Summe.Tag = path
                    btnCBAM_DS_DHF_UNISPED_Summe.Enabled = True
                    btnCBAM_DS_DHF_UNISPED_Copy.Enabled = True
                End If
                Me.Cursor = Cursors.Default

            End If



        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try
        Me.Cursor = Cursors.Default

    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles cbxVERAG.CheckedChanged
        txtEori.Enabled = Not cbxVERAG.Checked
    End Sub

    Private Sub btnCBAM_DS_DAKOSY_XML_Click(sender As Object, e As EventArgs) Handles btnCBAM_DS_DAKOSY_XML.Click

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim f As New List(Of String)
        If btnCBAM_DS_DAKOSY_XML.Tag <> "" Then f.Add(btnCBAM_DS_DAKOSY_XML.Tag)

        If f.Count > 0 Then
            'Zwischenablage
            Clipboard.Clear()
            Dim d As New DataObject(DataFormats.FileDrop, f.ToArray)
            Clipboard.SetDataObject(d, True)
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs)
        Dim f As New List(Of String)
        If btnCBAM_DS_DAKOSY_Deteil.Tag <> "" Then f.Add(btnCBAM_DS_DAKOSY_Deteil.Tag)
        If btnCBAM_DS_DAKOSY_Summe.Tag <> "" Then f.Add(btnCBAM_DS_DAKOSY_Summe.Tag)
        If btnCBAM_DS_DAKOSY_XML.Tag <> "" Then f.Add(btnCBAM_DS_DAKOSY_XML.Tag)

        If f.Count > 0 Then
            'Zwischenablage
            Clipboard.Clear()
            Dim d As New DataObject(DataFormats.FileDrop, f.ToArray)
            Clipboard.SetDataObject(d, True)
        End If
    End Sub
End Class
