Public Class usrcntlVerzollungsunterlagen_BAR
    Implements FormualrInterface

    Public Event CHANGED(name, value)
    Public Event KDNR_CHANGED(kdnr)
    Public Event DELETE(c As Control)
    Public Event MOVE_UP(c As Control)
    Public Event MOVE_DOWN(c As Control)

    Public KdNR As Integer = -1
    Public Sub ChangeKDNr(kdnr As Integer) _
       Implements FormualrInterface.ChangeKDNr
        'initKdNR(kdnr)
        txtFirma.KdNr = kdnr
    End Sub

    'Dim KdNR As Integer = -1
    Sub initControlsHandler()
        For Each c In Me.Controls
            Select Case c.GetType.ToString
                Case GetType(VERAG_PROG_ALLGEMEIN.MyTextBox).ToString
                    AddHandler DirectCast(c, VERAG_PROG_ALLGEMEIN.MyTextBox).Leave, Sub()
                                                                                        RaiseEvent CHANGED(c.name, c.text)
                                                                                    End Sub
                Case GetType(VERAG_PROG_ALLGEMEIN.MyComboBox).ToString
                    AddHandler DirectCast(c, VERAG_PROG_ALLGEMEIN.MyComboBox).SelectedIndexChanged, Sub()
                                                                                                        RaiseEvent CHANGED(c.name, c._value)
                                                                                                    End Sub
                Case GetType(TextBox).ToString
                    AddHandler DirectCast(c, TextBox).Leave, Sub()
                                                                 RaiseEvent CHANGED(c.name, c.text)
                                                             End Sub
            End Select
        Next
    End Sub





    Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click
        RaiseEvent DELETE(Me)
    End Sub

    Private Sub btnup_Click(sender As Object, e As EventArgs) Handles btnUp.Click
        RaiseEvent MOVE_UP(Me)
    End Sub

    Private Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click
        RaiseEvent MOVE_DOWN(Me)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub usrcntlVollmacht_AT_Load(sender As Object, e As EventArgs) Handles Me.Load
        cboFiliale.fillWithSQL("SELECT FilialenNr, Grenzstelle FROM Filialen WHERE Fil_Status='A' ORDER BY FilialenNr ", True, "FMZOLL", True)

        initSprache()

        txtFirma.initKdBox(Me.FindForm)
        AddHandler txtFirma.PropertyChanged, Sub()
                                                 Me.KdNR = txtFirma.KdNr_value
                                                 If txtFirma.KdNr_value > 0 Then
                                                     If txtFirma.KdData_ADRESSEN IsNot Nothing Then
                                                         Dim ADRESSE = txtFirma.KdData_ADRESSEN
                                                         txtFirma.Text = If(ADRESSE.Name_1, ADRESSE.Ordnungsbegriff)
                                                         txtAdresse1.Text = (If(ADRESSE.LandKz, "") & " " & If(ADRESSE.PLZ, "") & " " & If(ADRESSE.Ort, "")).ToString.Trim
                                                     End If
                                                 End If

                                                 RaiseEvent KDNR_CHANGED(KdNR)
                                             End Sub
        initControlsHandler()

        If KdNR > 0 Then txtFirma.KdNr = KdNR



        cboTEXT.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        cboTEXT.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AT Fiskal", "AT_FISKAL"))
        cboTEXT.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("DE Fiskal", "DE_FISKAL"))
        cboTEXT.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("EV Verzollung", "EV"))


        '  cboFiliale.fillWithSQL(" SELECT FilialenNr, Grenzstelle FROM Filialen WHERE Fil_Status='A' ORDER BY FilialenNr ", , "FMZOLL", True)


        cboRechnungAn.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        cboRechnungAn.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("1 - Absender", "1"))
        cboRechnungAn.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("2 - Empfänger", "2"))
        cboRechnungAn.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("3 - Auftraggeber", "3"))
        cboRechnungAn.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("4 - Avisierer", "4"))


        If Not cboTEXT._value = "DE_FISKAL" Then
            If txtFirma.KdNr_value > 0 AndAlso txtFirma.KdData_ADRESSEN IsNot Nothing Then
                If txtFirma.KdData_ADRESSEN.LandKz = "DE" Or txtFirma.KdData_ADRESSEN.LandKz = "AT" Then
                    cboSprache.changeItem("DE")
                Else
                    cboSprache.changeItem("EN")
                End If
            End If
        End If


        loaded = True
    End Sub

    Sub initSprache()
        If cboTEXT._value = "DE_FISKAL" Then
            cboSprache.Items.Clear()
            cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Englisch", "EN"))
            cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Französisch", "FR"))
            cboSprache.changeItem("EN")
        Else
            cboSprache.Items.Clear()
            cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Deutsch", "DE"))
            cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Englisch", "EN"))
            'cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Französisch", "FR")) ??
            cboSprache.changeItem("DE")

        End If

    End Sub

    Private Sub pic_Click(sender As Object, e As EventArgs) Handles pic.Click
        FormularManagerNEU.genPDF(Me, True)
    End Sub



    Private Sub PictureBox1_Click(sender As Object, e As EventArgs)
        FormularManagerNEU.genPDF(Me, , , , True)
    End Sub

    Private Sub cboRechnungAn_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboRechnungAn.SelectedIndexChanged
        If SPEDBUCH IsNot Nothing Then
            Select Case cboRechnungAn._value
                Case "1" : loadAdressen(SPEDBUCH.AbsenderKundenNr, SPEDBUCH.LKW_Kennzeichen)
                Case "2" : loadAdressen(SPEDBUCH.EmpfängerKundenNr, SPEDBUCH.LKW_Kennzeichen)
                Case "3" : loadAdressen(SPEDBUCH.VermittlerKundenNr, SPEDBUCH.LKW_Kennzeichen)
                Case "4" : loadAdressen(SPEDBUCH.AvisiererKundenNr, SPEDBUCH.LKW_Kennzeichen)
                Case Else : loadAdressen(-1, "")
            End Select
        End If
    End Sub

    Sub loadAdressen(KdNr, Optional LKW_Kz = Nothing)
        Me.KdNR = KdNr
        If KdNr IsNot Nothing AndAlso IsNumeric(KdNr) AndAlso KdNr > 0 Then
            Dim AD As New VERAG_PROG_ALLGEMEIN.cAdressen(KdNr)
            txtFirma.Text = If(AD.Name_1, "")
            '   txtFirma2.Text = If(AD.Name_2, "")
            txtAdresse1.Text = If(AD.LandKz, "") & " " & If(AD.PLZ, "") & " " & If(AD.Ort, "")

        Else
            txtFirma.Text = ""
            '   txtFirma2.Text = ""
            txtAdresse1.Text = ""
        End If
        If LKW_Kz IsNot Nothing Then txtLKWContainer.Text = LKW_Kz.ToString

    End Sub

    Private Sub txtAbfertigungsNr_TextChanged(sender As Object, e As EventArgs) Handles txtAbfertigungsnummer.Leave, txtAbfertigungsnummer.Leave
        If txtAbfertigungsnummer.Text.Length >= 8 And cboFiliale._value <> "" And cboSpedBuchUnterNr._value <> "" Then
            If cboRechnungAn._value = "" Then cboRechnungAn.changeItem("2")

        End If

    End Sub


    Private Sub txtAbfertigungsnummer_Leave(sender As Object, e As EventArgs) Handles txtAbfertigungsnummer.Leave
        If IsNumeric(txtAbfertigungsnummer.Text) AndAlso txtAbfertigungsnummer.Text.Length <= 6 Then
            txtAbfertigungsnummer.Text = Now.ToString("yy") & CInt(txtAbfertigungsnummer.Text).ToString("000000")
        End If
    End Sub

    Private Sub txtAbfertigungsnummer_TextChanged(sender As Object, e As EventArgs) Handles txtAbfertigungsnummer.TextChanged, cboFiliale.TextChanged
        If cboFiliale.Text.StartsWith("/") Then
            If cboFiliale.Text.EndsWith("/") And cboFiliale.Text.Contains("-") Then
                Dim t As String = cboFiliale.Text
                t = t.Replace("/", "")
                Dim v() = t.Split("-")
                If v.Length > 1 Then
                    txtAbfertigungsnummer.Text = v(1)

                    loadedFilAbf = False 'damit changeItem kein erneuters laden verursachts
                    cboFiliale.changeItem(v(0))
                    loadedFilAbf = True

                    getFilialeAbfertNUrnterNr(v(0), v(1))

                End If
            End If
        Else
            getFilialeAbfertNUrnterNr(cboFiliale._value, txtAbfertigungsnummer.Text)
        End If
    End Sub
    Dim loaded = False
    Dim loadedFilAbf = True
    Sub getFilialeAbfertNUrnterNr(FilialenNr, AbfertigungsNr)
        If Not loadedFilAbf Then Exit Sub
        If FilialenNr <> String.Empty And AbfertigungsNr.Length >= 8 Then
            cboSpedBuchUnterNr.fillWithSQL("SELECT  [UnterNr], convert(varchar(10),[Abfertigungsdatum], 104) + ' | ' + cast(Abfertigungsarten.Abfertigungsart as varchar(2)) + ' ' + Abfertigungsarten.Abfertigungsbezeichnung + '  - ' + isnull(cast(EmpfängerKundenNr as varchar(10)),'') + ' ' + isnull(Empfänger,'') " &
                                           " FROM [Speditionsbuch] inner join Abfertigungsarten on Abfertigungsarten.Abfertigungsart=Speditionsbuch.Abfertigungsart  where FilialenNr='" & FilialenNr & "' and AbfertigungsNr='" & AbfertigungsNr & "'", True, "FMZOLL", False)
            If loaded Then
                If cboSpedBuchUnterNr.Items.Count = 1 Then
                    cboSpedBuchUnterNr.SelectedIndex = 0
                    Button1.PerformClick()
                End If
            End If
        Else
            cboSpedBuchUnterNr.Items.Clear()
        End If
    End Sub


    Private Sub cboFiliale_Leave(sender As Object, e As EventArgs) Handles cboFiliale.Leave
        'If cboFiliale.Text.StartsWith("/") Then
        '    loadedFilAbf = False 'damit changeItem kein erneuters laden verursachts
        '    cboFiliale.changeItem(RECHNUNG.FilialenNr)
        '    loadedFilAbf = True
        'End If

        'If cboSpedBuchUnterNr.Items.Count = 1 Then
        '    cboSpedBuchUnterNr.SelectedIndex = 0
        'ElseIf cboSpedBuchUnterNr.Items.Count > 1 Then
        '    cboSpedBuchUnterNr.DroppedDown = True
        '    cboSpedBuchUnterNr.Focus()
        'End If
    End Sub


    Dim SPEDBUCH As VERAG_PROG_ALLGEMEIN.cSpeditionsbuch = Nothing
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Dim FORMULARE As frmFormulare = Me.FindForm
        If cbx.Checked Then FORMULARE.clearExceptMe(Me)

        SPEDBUCH = Nothing
        SPEDBUCH = New VERAG_PROG_ALLGEMEIN.cSpeditionsbuch(cboFiliale._value, txtAbfertigungsnummer.Text, cboSpedBuchUnterNr._value) 'txtUnterNr.Text)
        If Not SPEDBUCH.hasEntry Then Exit Sub
        If cboRechnungAn._value = "" Then cboRechnungAn.changeItem("2")


        If SPEDBUCH.DokumentId_Steuerbeleg IsNot Nothing AndAlso SPEDBUCH.DokumentId_Steuerbeleg.ToString <> "" Then
            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(SPEDBUCH.DokumentId_Steuerbeleg)
            FORMULARE.addForm("Sonst_PDF", {DS.OPEN_SINGLE(False)})
        End If
        If SPEDBUCH.DokumentId_Mitteilung IsNot Nothing AndAlso SPEDBUCH.DokumentId_Mitteilung.ToString <> "" Then
            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(SPEDBUCH.DokumentId_Mitteilung)
            FORMULARE.addForm("Sonst_PDF", {DS.OPEN_SINGLE(False)})
        End If
        txtFirma.Focus() ' Leave

        Select Case SPEDBUCH.Abfertigungsart
            Case "26"
                cboTEXT.changeItem("AT_FISKAL")
            Case "28" 'DE
                cboTEXT.changeItem("DE_FISKAL")
            Case "38" 'EV
                cboTEXT.changeItem("EV")
        End Select
    End Sub

    Private Sub cboTEXT_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboTEXT.SelectedIndexChanged
        initSprache()
    End Sub
End Class
