Imports VERAG_PROG_ALLGEMEIN

Public Class usrCntlQualitaetskontrolle
    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)
    Dim FV_ID = -1
    Public KdNR As Integer = -1
    Public KdFirma As String = ""
    Public SENDUNG As VERAG_PROG_ALLGEMEIN.cSendungen = Nothing
    Public AVISO As VERAG_PROG_ALLGEMEIN.cAviso = Nothing
    Public EZA_Nr As String
    Dim packstueckeZOLL_TMP As String = ""


    Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    Sub New(param() As Object)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        If param.Length > 0 AndAlso param(0) IsNot Nothing Then KdNR = param(0)
        If param.Length > 1 Then KdFirma = param(1)
        If param.Length > 2 Then SENDUNG = param(2)
        If param.Length > 3 Then AVISO = param(3)
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub
    Public Sub ChangeKDNr(kdnr As Integer) _
       Implements FormualrInterface.ChangeKDNr
        initKdNR(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 btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        Dim kdsrch As New SDL.frmKundenSuche
        If kdsrch.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
            If IsNumeric(kdsrch.kundenNrSearch.Text) Then
                initKdNR(kdsrch.kundenNrSearch.Text)
                RaiseEvent KDNR_CHANGED(KdNR)
            End If
        End If
        '  Panel1.Enabled = True
    End Sub



    Public Sub initSND()
        If SENDUNG IsNot Nothing Then
            txtFilialNr.Text = SENDUNG.FilialenNr.ToString
            txtAbfNr.Text = SENDUNG.AbfertigungsNr.ToString
            txtGewicht.Text &= " kg"

        End If
        If AVISO IsNot Nothing Then
            txtLKWKennz.Text = If(AVISO.LKW_Nr, "")
        End If
    End Sub

    Public Sub initKdNR(KdNR)
        If KdNR > 0 Then
            Me.KdNR = KdNR

            Dim KUNDE As New VERAG_PROG_ALLGEMEIN.cKunde(KdNR)
            Dim ADRESSE As New VERAG_PROG_ALLGEMEIN.cAdressen(KdNR)

            txtFirma.Text = If(ADRESSE.Name_1, "") & If(If(ADRESSE.Name_2, "") <> "", " " & ADRESSE.Name_2, "")
            txtAdresse1.Text = If(ADRESSE.Straße, "")
            txtAdresse2.Text = (If(ADRESSE.LandKz, "") & " " & If(ADRESSE.PLZ, "") & " " & If(ADRESSE.Ort, "")).ToString.Trim
            '  txtAnsprechpartnerVERAG.Text = If(ADRESSE.Ansprechpartner, "")
        Else
            txtFirma.Text = KdFirma
        End If

    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 usrCntlZollbeschau_Load(sender As Object, e As EventArgs) Handles Me.Load
        initControlsHandler()
        'txtAnsprechpartnerVERAG.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
        txtBeschauDat.Text = Date.Today.ToString("dd.MM.yyyy")

        cbxEinf.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Einfuhr", "E"))
        cbxEinf.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Ausfuhr", "A"))


        cbxTyp.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Obst und Gemüse", "O"))
        cbxTyp.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Bananen", "B"))
        cbxTyp.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Eier und Gefl.", "E"))

        cbxUrsprungslang.fillWithSQL("SELECT [Länderverzeichnis für die Außenhandelsstatistik].LandKz, [Länderverzeichnis für die Außenhandelsstatistik].LandBez FROM [Länderverzeichnis für die Außenhandelsstatistik] ORDER BY [Länderverzeichnis für die Außenhandelsstatistik].LandKz; ", True, "VERAG", True)
        cbxBestimmungsland.Items.AddRange(cbxUrsprungslang.Items.Cast(Of VERAG_PROG_ALLGEMEIN.MyListItem).ToArray())


        If SENDUNG IsNot Nothing Then
            If SENDUNG.tblSnd_DakosyRef <> "" Then
                loadEZollData(SENDUNG.tblSnd_DakosyRef)
            ElseIf SENDUNG.FilialenNr > 0 AndAlso SENDUNG.AbfertigungsNr > 0 Then
                Dim ref = SENDUNG.FilialenNr.ToString & "/" & SENDUNG.AbfertigungsNr.ToString
                loadEZollData(ref)
            End If

        End If

        initKdNR(KdNR)
        initSND()
    End Sub

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


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If KdNR > 0 Then
            Dim f As New frmKundenUebersichtZOLL(KdNR)
            f.ShowDialog(Me)
        End If
    End Sub



    Private Sub dtpBeschauDat_ValueChanged(sender As Object, e As EventArgs) Handles dtpBeschauDat.ValueChanged
        txtBeschauDat._value = CDate(sender.value).ToShortDateString
    End Sub


    Private Sub loadDGV(dt As DataTable)

        Dim couter As Integer = 1
        For Each row As DataRow In dt.Rows

            dgvPositionen.Rows.Add(row("ItNr"), row("Nr"), row("Kind"), row("GdsDes"), row("Net"))
            couter = +1

        Next

    End Sub

    Private Sub dgvPositionen_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) Handles dgvPositionen.DefaultValuesNeeded

        e.Row.Cells("pos").Value = dgvPositionen.Rows.Count
    End Sub

    Private Sub loadEZollData(EZA_Nr As String)

        Dim KDSQL As New SQL
        Dim notFound As Boolean = True

        'EZA_Nr = "4/23208/15"

        Try

            Dim dEZOLL As DataTable = KDSQL.loadDgvBySql("SELECT LizenzNr, OperatorID, VorgangID, AnmID, GeaendertAm, GeaendertVon, CRN, LRN" &
           " FROM zsAnmRefs" &
           " WHERE (VorgangID = 'EF') AND (LRN = '" & EZA_Nr & "')" &
           " ORDER BY GeaendertAm DESC", "EZOLL")
            If dEZOLL Is Nothing Then MsgBox("Keine Daten im EZOLL vorhanden, Daten können nicht geladen werden!") : Exit Sub
            If dEZOLL.Rows.Count = 0 Then MsgBox("Keine Daten im EZOLL vorhanden, Daten können nicht geladen werden!") : Exit Sub

            Dim DR_zsAnmRefs As DataRow = dEZOLL.Rows(0)
            notFound = False


            Dim DTzsAnmTransp As DataTable = KDSQL.loadDgvBySql("SELECT DepIdnt, DepNat, CrossIdntNat" &
              " FROM zsAnmTransp" &
              " WHERE (LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & ")", "EZOLL")

            If DTzsAnmTransp.Rows().Count > 0 Then
                If Not IsNull(DTzsAnmTransp.Rows(0).Item("DepIdnt")) Then txtLKWKennz.Text = Strings.Left(DTzsAnmTransp.Rows(0).Item("DepIdnt"), 25)
                If Not IsNull(DTzsAnmTransp.Rows(0).Item("DepNat")) Then cbxBestimmungsland.changeItem(DTzsAnmTransp.Rows(0).Item("DepNat"))
                If Not IsNull(DTzsAnmTransp.Rows(0).Item("CrossIdntNat")) Then cbxUrsprungslang.changeItem(DTzsAnmTransp.Rows(0).Item("CrossIdntNat"))
            End If

            ' Tabelle zsAnmHea lesen.
            Dim DTzsAnmHea As DataTable = KDSQL.loadDgvBySql("SELECT TotItem, TotPack, TotGross" &
                " FROM zsAnmHea" &
                " WHERE (LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (AnmID = " & DR_zsAnmRefs.Item("AnmID") & ")", "EZOLL")

            If DTzsAnmHea.Rows().Count > 0 Then
                If Not IsNull(DTzsAnmHea.Rows(0).Item("TotPack")) Then txtAnzVerp.Text = DTzsAnmHea.Rows(0).Item("TotPack") 'Gesamt packstücke
                If Not IsNull(DTzsAnmHea.Rows(0).Item("TotGross")) Then txtGewicht.Text = DTzsAnmHea.Rows(0).Item("TotGross") ' Gesamtbruttogewicht
                If Not IsNull(DTzsAnmHea.Rows(0).Item("TotItem")) Then txtPos.Text = DTzsAnmHea.Rows(0).Item("TotItem") ' Anzahl Pos

            End If

            ' Tabelle zsAnmGdsitem lesen.
            Dim DTzsAnmGdsitem As DataTable = KDSQL.loadDgvBySql("SELECT item.ItNr ,item.GdsDes, item.Net, pack.Kind, pack.Nr, item.Orig" &
" FROM zsAnmGdsitem as item" &
" INNER JOIN 
as pack ON item.LizenzNr = pack.LizenzNr And item.OperatorID = pack.OperatorID and item.VorgangID = pack.VorgangID and pack.AnmID = item.AnmID" &
" WHERE (item.LizenzNr = '" & DR_zsAnmRefs.Item("LizenzNr") & "') And (item.OperatorID = " & DR_zsAnmRefs.Item("OperatorID") & ") And (item.VorgangID = '" & DR_zsAnmRefs.Item("VorgangID") & "') And (item.AnmID = " & DR_zsAnmRefs.Item("AnmID") & " and item.GdsItemID = pack.GdsItemID)" &
                    " ORDER BY item.GdsItemID", "EZOLL")

            If DTzsAnmGdsitem.Rows().Count > 0 Then

                loadDGV(DTzsAnmGdsitem)
            End If
        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR("Fehler EZOLL Personal: " & vbNewLine & ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)

        End Try


    End Sub

    Function IsNull(o) As Boolean
        Try
            If o Is Nothing Then Return True
            If o Is DBNull.Value Then Return True
            If o.ToString.Trim = "" Then Return True
        Catch : End Try
        Return False
    End Function

    Private Sub dgvPositionen_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvPositionen.CellValueChanged
        Dim gewicht As Double = 0
        Dim colli As Double = 0

        For Each r As DataGridViewRow In dgvPositionen.Rows


            If r IsNot Nothing And IsNumeric(r.Cells(4).Value) Then
                gewicht += CDbl(r.Cells(4).Value)
            End If

            If r IsNot Nothing And IsNumeric(r.Cells(1).Value) Then
                colli += CDbl(r.Cells(1).Value)
            End If


        Next

        txtGewicht.Text = gewicht & " kg"
        txtAnzVerp.Text = colli
    End Sub
End Class
