Imports jdk.nashorn.internal.objects.annotations

Public Class frmDakosyStammreferenzVerwalten

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
    Private Sub frmDakosyStammreferenz_Load(sender As Object, e As EventArgs) Handles Me.Load
        cboVerfahren.Items.Clear()
        cboVerfahren.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("NCTS - Versandverfahren", "NCTS"))
        cboVerfahren.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("ZA - Zollanmeldung", "EZA"))
        cboVerfahren.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AES - Ausfuhr", "AES"))
        cboVerfahren.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VB - Versandbeendigung", "AA"))
        'cboVerfahren.changeItem(verfahren)

        cboLand.Items.Clear()
        cboLand.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("DE", "DE"))
        cboLand.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AT", "AT"))
        '  cboLand.changeItem(verfahren)

        cboMandant.Items.Clear()
        cboMandant.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERG", "VERG"))
        cboMandant.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("ATIL", "ATIL"))
        cboMandant.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IMEX", "IMEX"))
        cboMandant.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("UNAT", "UNAT"))

        cboLand.changeItem("DE")
        cboVerfahren.changeItem("EZA")

        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_firma
            Case "VERAG" : cboMandant.changeItem("VERG")
            Case "ATILLA" : cboMandant.changeItem("ATIL") : cboVerfahren.changeItem("NCTS")
            Case "IMEX" : cboMandant.changeItem("IMEX")
            Case "UNISPED" : cboMandant.changeItem("UNAT")
        End Select

    End Sub

    Private Sub cboVerfahren_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboVerfahren.SelectedIndexChanged, cboLand.SelectedIndexChanged
        initDGV()
    End Sub

    Sub initDGV()
        With dgvStammref
            .Columns.Clear()
            If cboLand._value <> "" And cboVerfahren._value <> "" Then
                Dim where = If(txtSearch.Text <> "", " AND (Ordnungsbegriff LIKE '" & txtSearch.Text & "%' OR dyStr_KdNr LIKE '" & txtSearch.Text & "%') ", "")
                .DataSource = SQL.loadDgvBySql("SELECT [dySr_Id], [dySr_Stammreferenz],[dySr_Bezeichnung],isnull(dySr_dyId,-1) as dySr_dyId,dySr_dy_Mandant,dySr_dy_Niederlassung,dyStr_KdNr,Ordnungsbegriff,CASE WHEN dySr_visible=1 THEN 'JA' ELSE 'NEIN' END as Sichtbar, dySr_WarenortID FROM [tblDakosy_Stammreferenzen]  LEFT JOIN Adressen ON AdressenNr=dyStr_KdNr WHERE dySr_Land='" & cboLand._value & "' AND dySr_Verfahren='" & cboVerfahren._value & "'  and dySr_dy_Mandant='" & cboMandant._value & "'  " & If(cboNL._value = "", "", " AND dySr_dy_Niederlassung='" & cboNL._value & "'") & " " & where & " ORDER BY dyStr_KdNr, dySr_Bezeichnung ", "FMZOLL")
                'MsgBox("SELECT [dySr_Id], [dySr_Stammreferenz],[dySr_Bezeichnung],isnull(dySr_dyId,-1) as dySr_dyId,dySr_dy_Mandant,dySr_dy_Niederlassung,dyStr_KdNr,Ordnungsbegriff FROM [tblDakosy_Stammreferenzen]  LEFT JOIN Adressen ON AdressenNr=dyStr_KdNr WHERE dySr_Land='" & cboLand._value & "' AND dySr_Verfahren='" & cboVerfahren._value & "'  and dySr_dy_Mandant='" & cboMandant._value & "'  " & If(cboNL._value = "", "", " AND dySr_dy_Niederlassung='" & cboNL._value & "'") & " " & where & " ORDER BY dyStr_KdNr, dySr_Bezeichnung ")
                .Columns("dySr_dyId").Visible = False
                .Columns("dySr_Id").Visible = False
                .Columns("dySr_dy_Mandant").Visible = False
                .Columns("dySr_dy_Niederlassung").Visible = False

                If cboVerfahren._value = "AA" Then
                    .Columns("dySr_WarenortID").HeaderText = "WarenortID"
                    .Columns("dySr_WarenortID").Visible = True
                Else
                    .Columns("dySr_WarenortID").Visible = False
                End If

                .Columns("dySr_Stammreferenz").HeaderText = "Stammreferenz"
                .Columns("dySr_Stammreferenz").Width = 150
                .Columns("dySr_Bezeichnung").HeaderText = "Bezeichnung"
                .Columns("dySr_Bezeichnung").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

                .Columns("dyStr_KdNr").HeaderText = "KdNr"
                .Columns("dyStr_KdNr").Width = 60

                .Columns("Ordnungsbegriff").HeaderText = "Firma"
                .Columns("Ordnungsbegriff").Width = 250
                .ClearSelection()
            End If
        End With
    End Sub

    'Private Sub MyDatagridview1_CellDoubleClick(sender As Object, e As Object) Handles dgvStammref.DoubleClick, dgvStammref.CellDoubleClick, dgvStammref.CellContentDoubleClick
    '    editSelected()
    'End Sub

    Sub editSelected()
        If dgvStammref.SelectedRows.Count > 0 Then
            Dim ZA As DAKOSY_Worker.cDakosy_Zollanmeldungen
            If dgvStammref.SelectedRows(0).Cells("dySr_dyId").Value Is DBNull.Value Then
                ZA = New DAKOSY_Worker.cDakosy_Zollanmeldungen
                ZA.SAVE()
            Else
                ZA = New DAKOSY_Worker.cDakosy_Zollanmeldungen(dgvStammref.SelectedRows(0).Cells("dySr_dyId").Value)
            End If

            If ZA.dy_Id <= 0 Then ZA.SAVE()
            ' MsgBox(dgvStammref.SelectedRows(0).Cells("dySr_dyId").Value)
            Dim f As New frmZollanmeldung(cboVerfahren._value, ZA)
            f.isSTAMMREF = True
            f.STAMMREF = dgvStammref.SelectedRows(0).Cells("dySr_Stammreferenz").Value
            f.ShowDialog(Me)

            SQL.doSQL("UPDATE [tblDakosy_Stammreferenzen] SET dySr_dyId='" & ZA.dy_Id & "' WHERE dySr_Id='" & dgvStammref.SelectedRows(0).Cells("dySr_Id").Value & "'", "FMZOLL")

            initDGV()
        End If
    End Sub

    Sub duplicateSelected()

        If cboVerfahren._value = "" Then Exit Sub
        If cboLand._value = "" Then Exit Sub
        If cboMandant._value = "" Then Exit Sub
        ' If cboNL._value = "" Then Exit Sub

        Dim fd As New frmStammreferenzPopup(cboMandant._value, cboNL._value, cboVerfahren._value)

        AddHandler fd.Shown, Sub()
                                 fd.cboStammrefDakosy.changeItem(dgvStammref.SelectedRows(0).Cells("dySr_Stammreferenz").Value)
                                 fd.txtBezeichnung.Text = dgvStammref.SelectedRows(0).Cells("dySr_Bezeichnung").Value & " - Kopie"
                                 If dgvStammref.SelectedRows(0).Cells("dyStr_KdNr").Value IsNot DBNull.Value Then
                                     fd.KdSearchBox1.KdNr = dgvStammref.SelectedRows(0).Cells("dyStr_KdNr").Value
                                 End If
                             End Sub



        If fd.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

            Dim SR As New DAKOSY_Worker.cDakosy_Stammreferenzen() 'dgvStammref.SelectedRows(0).Cells("dySr_Id").Value)
            '  SR.dySr_Id = -1
            SR.dySr_Bezeichnung = fd.txtBezeichnung.Text
            SR.dySr_Stammreferenz = fd.cboStammrefDakosy._value
            SR.dySr_Land = cboLand._value
            SR.dySr_Verfahren = cboVerfahren._value
            ' SR. dySr_dyId As Object = Nothing
            SR.dySr_dy_Mandant = cboMandant._value
            SR.dySr_dy_Niederlassung = fd.cboNL._value
            SR.dySr_dy_Standard = fd.cbxStandard.Checked
            SR.dyStr_KdNr = fd.KdSearchBox1.KdNrNullInt
            If cboVerfahren._value = "AA" Then
                SR.dySr_WarenortID = fd.cbxWarenort._value
            Else
                SR.dySr_WarenortID = -1
            End If


            If SR.SAVE Then
                    Dim ZA As New DAKOSY_Worker.cDakosy_Zollanmeldungen() 'dgvStammref.SelectedRows(0).Cells("dySr_dyId").Value)

                    ZA.dy_Id = 0
                    ZA.dy_ART = SR.dySr_Verfahren
                    ZA.dy_Status = -1
                    ZA.dy_Status_KEWILL_Equivalent = -1
                    If ZA.SAVE() Then
                        DUPLICATE_Verfahren(dgvStammref.SelectedRows(0).Cells("dySr_dyId").Value, ZA)

                        SR.dySr_dyId = ZA.dy_Id
                        SR.SAVE()

                        Dim f As New frmZollanmeldung(cboVerfahren._value, ZA)
                        f.isSTAMMREF = True
                        f.STAMMREF = SR.dySr_Stammreferenz
                        f.ShowDialog(Me)
                        '    f.CopyFromZA_Id = dgvStammref.SelectedRows(0).Cells("dySr_Id").Value



                        '  SQL.doSQL("UPDATE [tblDakosy_Stammreferenzen] SET dySr_dyId='" & ZA.dy_Id & "' WHERE dySr_Id='" & dgvStammref.SelectedRows(0).Cells("dySr_Id").Value & "'", "FMZOLL")
                    End If
                    initDGV()
                End If

                ' Dim f As New frmZollanmeldung(cboVerfahren._value, New DAKOSY_Worker.cDakosy_Zollanmeldungen)

            End If



        'If dgvStammref.SelectedRows.Count > 0 Then
        '    Dim ZA As New DAKOSY_Worker.cDakosy_Zollanmeldungen(dgvStammref.SelectedRows(0).Cells("dySr_dyId").Value)
        '    If ZA.dy_Id <= 0 Then ZA.SAVE()
        '  Dim f As New frmZollanmeldung(cboVerfahren._value, ZA)
        '    f.isSTAMMREF = True
        '    f.STAMMREF = dgvStammref.SelectedRows(0).Cells("dySr_Stammreferenz").Value
        '    f.ShowDialog(Me)
        '    SQL.doSQL("UPDATE [tblDakosy_Stammreferenzen] SET dySr_dyId='" & ZA.dy_Id & "' WHERE dySr_Id='" & dgvStammref.SelectedRows(0).Cells("dySr_Id").Value & "'", "FMZOLL")

        '    initDGV()
        'End If
    End Sub

    Sub DUPLICATE_Verfahren(AnmID As Integer, ByRef ZA As DAKOSY_Worker.cDakosy_Zollanmeldungen)
        'duplicate'
        Select Case cboVerfahren._value
            Case "EZA"
                Dim EZA = DAKOSY_Worker.cDakosyEZA.LOADByAnmIDFull(AnmID, True)
                EZA.eza_Id = -1
                EZA.eza_dyaAnmID = ZA.dy_Id
                EZA.SAVE()
            Case "NCTS"
                Dim NCTS = DAKOSY_Worker.cDakosyNCTSOut004.LOADByAnmIDFull(AnmID, True)
                NCTS.ncts_Id = -1
                NCTS.ncts_dyaAnmID = ZA.dy_Id
                NCTS.SAVE()
            'Case "NCTS"
            '    Dim NCTS = DAKOSY_Worker.cDakosyNCTSOut.LOADByAnmIDFull(AnmID, True)
            '    NCTS.ncts_Id = -1
            '    NCTS.ncts_dyaAnmID = ZA.dy_Id
            '    NCTS.SAVE()
            Case "AES"
                Dim AES = DAKOSY_Worker.cDakosyAES.LOADByAnmIDFull(AnmID, True)
                AES.aes_Id = -1
                AES.aes_dyaAnmID = ZA.dy_Id
                AES.SAVE()
        End Select

    End Sub
    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click

        If cboVerfahren._value = "" Then cboVerfahren.Focus() : Exit Sub
        If cboLand._value = "" Then cboLand.Focus() : Exit Sub
        If cboMandant._value = "" Then cboMandant.Focus() : Exit Sub

        changeEntry(cboMandant._value, cboLand._value, cboVerfahren._value)

    End Sub

    Private Sub cboMandant_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboMandant.SelectedIndexChanged
        cboNL.Items.Clear()
        Select Case cboMandant._value
            Case "VERG"
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SUB", "SUB"))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SBG", "SBG"))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("WAI", "WAI"))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("NEU", "NEU"))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("NKD", "NKD"))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("DTB", "DTB"))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AMBWO", "AMBWO"))
                cboNL.changeItem("")

            Case "IMEX"
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IME", "IME"))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("GLO", "GLO"))
                cboNL.changeItem("IME")



            Case "ATIL"
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SUW", "SUW"))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AMBWO", "AMBWO"))
                cboNL.changeItem("SUW")
            Case "UNAT"
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("ATSPED", "ATSPED"))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("DESPED", "DESPED"))
                cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("ZOLL", "ZOLL"))
                cboNL.changeItem("ATSP")

        End Select
        initDGV()
    End Sub

    Private Sub cboNL_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboNL.SelectedIndexChanged
        initDGV()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        editSelected()
    End Sub

    Private Sub btnDuplicate_Click(sender As Object, e As EventArgs) Handles btnDuplicate.Click
        duplicateSelected()
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If vbYes = MsgBox("Möchten Sie die Stammreferenz wirklich löschen?", vbYesNoCancel) Then
            Dim SR As New DAKOSY_Worker.cDakosy_Stammreferenzen(CInt(dgvStammref.SelectedRows(0).Cells("dySr_Id").Value))
            SR.DELETE()
            initDGV()
        End If
    End Sub

    Private Sub MyTextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        initDGV()
    End Sub

    Private Sub dgvStammref_SelectionChanged(sender As Object, e As EventArgs) Handles dgvStammref.SelectionChanged
            Button2.Enabled = False
            If dgvStammref.SelectedRows.Count > 0 Then
                Button2.Enabled = True
        End If
        btnDuplicate.Enabled = Button2.Enabled
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnDuplicate.Click

    End Sub

    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
        If dgvStammref.SelectedRows.Count > 0 Then
            SQL.doSQL("Update [tblDakosy_Stammreferenzen]  SET dySr_visible=CASE WHEN dySr_visible = 1 THEN 0 ELSE 1 END WHERE [dySr_Id]='" & dgvStammref.SelectedRows(0).Cells("dySr_Id").Value & "' ", "FMZOLL")
            initDGV()
        End If
    End Sub


    Private Sub changeEntry(mandant As String, NL As String, verfahren As String, Optional id As Integer = -1)

        Dim fd As frmStammreferenzPopup

        If id > 0 Then
            fd = New frmStammreferenzPopup(id)
        Else
            fd = New frmStammreferenzPopup(mandant, NL, verfahren)
        End If

        If fd.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

            Dim SR As DAKOSY_Worker.cDakosy_Stammreferenzen

            If fd.id > 0 Then
                SR = New DAKOSY_Worker.cDakosy_Stammreferenzen(id)
            Else
                SR = New DAKOSY_Worker.cDakosy_Stammreferenzen()
            End If


            SR.dySr_Bezeichnung = fd.txtBezeichnung.Text
            SR.dySr_Stammreferenz = fd.cboStammrefDakosy._value
            SR.dySr_Land = cboLand._value
            SR.dySr_Verfahren = fd.verfahren
            ' SR. dySr_dyId As Object = Nothing
            SR.dySr_dy_Mandant = fd.mandant
            SR.dySr_dy_Niederlassung = fd.cboNL._value
            SR.dySr_dy_Standard = fd.cbxStandard.Checked
            SR.dyStr_KdNr = fd.KdSearchBox1.KdNrNullInt
            SR.dySr_WarenortID = fd.cbxWarenort._value

            If SR.SAVE AndAlso SR.dySr_Verfahren <> "AA" Then
                Dim ZA As New DAKOSY_Worker.cDakosy_Zollanmeldungen()
                ZA.dy_ART = SR.dySr_Verfahren
                ZA.dy_Status = -1
                ZA.dy_Status_KEWILL_Equivalent = -1
                If ZA.SAVE() Then

                    SR.dySr_dyId = ZA.dy_Id
                    SR.SAVE()

                    Dim f As New frmZollanmeldung(verfahren, ZA)
                    f.isSTAMMREF = True
                    f.STAMMREF = SR.dySr_Stammreferenz
                    f.ShowDialog(Me)

                End If
                initDGV()
            End If

        End If


    End Sub


    Private Sub dgvStammref_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvStammref.CellDoubleClick

        Try
            If e.RowIndex >= 0 Then
                Dim ID = dgvStammref.Rows(e.RowIndex).Cells("dySr_Id").Value
                changeEntry("", "", "", ID)
            End If


        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
            Me.Cursor = Cursors.Default
        End Try

        Me.Cursor = Cursors.Default

    End Sub
End Class