Public Class usrCntlZollbeschau
    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 Rolle 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)
        If param.Length > 4 Then Rolle = param(4)
        ' 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
            txtT1.Text = If(SENDUNG.tblSnd_ATB_T1, "")
            txtATB.Text = If(SENDUNG.tblSnd_ATB_Nr, "")
            txtATC.Text = If(SENDUNG.tblSnd_ATCMRN_Fremd, "")
            txtWarenbez.Text = If(SENDUNG.tblSnd_Warenbezeichnung, "")
            txtAnzVerp.Text = If(SENDUNG.tblSnd_Colli, "")
            txtGewicht.Text = If(SENDUNG.tblSnd_Gewicht, "")
            txtGewicht.Text &= " kg"
            txtAbsender.Text = If(SENDUNG.tblSnd_Absender, "")
            txtWarenwert.Text = If(SENDUNG.tblSnd_Warenwert, "")
            txtFrächter.Text = If(SENDUNG.tblSnd_Frachtfuehrer, "")
            txtAbsender.Text = If(SENDUNG.tblSnd_Absender, "")
            txtZollagent.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA

        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
            txtUid.Text = If(ADRESSE.UstIdKz, "") & If(ADRESSE.UstIdNr, "")
            txtEori.Text = If(KUNDE.EORITIN, "")
            txtTel.Text = If(ADRESSE.Telefon, "")
            txtFax.Text = If(ADRESSE.Telefax, "")
            txtEmail.Text = If(ADRESSE.E_Mail, "")
            '  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
        Label16.Text = Rolle.ToString
        txtDurationZoll1.Enabled = False
        txtDurationZoll2.Enabled = False
        initControlsHandler()
        txtAnsprechpartnerVERAG.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME
        txtBeschauDat.Text = Date.Today.ToString("dd.MM.yyyy")
        txtVZDatum.Text = Date.Today.ToString("dd.MM.yyyy")
        loadDGV()
        calcPreis()

        If IsNumeric(VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_durchwahl) Then
            txtDW.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_durchwahl
        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 DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        txtZoll1Date._value = CDate(sender.value).ToShortDateString

    End Sub

    Private Sub DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged
        txtZoll2Date._value = CDate(sender.value).ToShortDateString
    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 DateTimePicker3_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker3.ValueChanged
        txtVZDatum._value = CDate(sender.value).ToShortDateString
    End Sub

    Private Sub txtZoll1Date_ValueChanged() Handles txtZoll1Date.ValueChanged
        If txtZoll1Date._value <> "" Then
            txtDurationZoll1.Enabled = True
            txtTimeZoll1.Enabled = True
            If txtTimeZoll1.Text = "" Then txtTimeZoll1.Text = Date.Now.ToString("HH:mm")
            If txtDurationZoll1.Text = "" Then txtDurationZoll1.Text = "00:00"
        Else
            txtDurationZoll1.Enabled = False
            txtTimeZoll1.Enabled = False
            txtTimeZoll1.Text = ""
            txtDurationZoll1.Text = ""
        End If
    End Sub

    Private Sub txtZoll2Date_ValueChanged() Handles txtZoll2Date.ValueChanged
        If txtZoll2Date._value <> "" Then
            txtDurationZoll2.Enabled = True
            txtTimeZoll2.Enabled = True
            If txtTimeZoll2.Text = "" Then txtTimeZoll2.Text = Date.Now.ToString("HH:mm")
            If txtDurationZoll2.Text = "" Then txtDurationZoll2.Text = "00:00"
        Else

            txtDurationZoll2.Enabled = False
            txtTimeZoll2.Enabled = False
            txtTimeZoll2.Text = ""
            txtDurationZoll2.Text = ""
        End If
    End Sub

    Private Sub loadDGV()

        dgvPositionen.Rows.Add(1, "Vorbereitung Waren zum Zollbeschau durch Ambar", 1, 60)
        dgvPositionen.Rows.Add(2, "Mehraufwandskosten")
        dgvPositionen.Rows.Add(3, "Lagereingang (Mindessatz 30€); (pro Palette 10€)")
        dgvPositionen.Rows.Add(4, "Lagerung pro KW 10€/Palette; KW: ")
        dgvPositionen.Rows.Add(5, "Lagerausgang (pro Palette 10€) ")
        dgvPositionen.Rows.Add(6, "Verpackungsmaterial je nach Bedarf (Kartonagen, Klebeband, Wickelfolie, Paletten)")
        dgvPositionen.Rows.Add(7, "Händische Abladung (mind-satz. 80€, Stundenpau. bei 2 Person. 50€ pro Stunde/Person, gilt bei Lose-/Kartonwaren ohne Palettierung ")
        dgvPositionen.Rows.Add(8, "Samstagzuschlag/Spätzuschlag ab 17 Uhr (Mindestsatz 30€)")
        dgvPositionen.Rows.Add(9, "Bürgschaftskosten Verwahrlager 1% vom Warenwert")

    End Sub

    Private Function calcPreis() As Double

        Dim gesamtpreis As Double = 0

        For Each r As DataGridViewRow In dgvPositionen.Rows
            If (IsNumeric(r.Cells(2).Value) AndAlso IsNumeric(r.Cells(3).Value)) Then
                gesamtpreis += r.Cells(2).Value * r.Cells(3).Value
            End If

        Next

        lblgesamtpreis.Text = "Gesamtpreis: " & gesamtpreis & " €"
        Return gesamtpreis


    End Function

    Private Sub dgvPositionen_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgvPositionen.CellValueChanged

        If e.ColumnIndex = 3 Or e.ColumnIndex = 2 Then
            calcPreis()
        End If

    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
End Class
