Public Class frmPostEinschreiben



    Dim MONAT As Date = CDate("01." & Now.Month & "." & Now.Year) '.AddMonths(-1)
    Dim srcTAG As Date = CDate(Now.ToShortDateString)

    Dim JAHR As Date = CDate("01.01." & Now.Year)


    ' Dim bindPOST As New cEasyBinding("FMZOLL")
    Dim bindPOST As New cEasyBinding("FMZOLL")
    Dim myTable As New DataTable
    Private Sub frmVorauskasse_Load(sender As Object, e As EventArgs) Handles Me.Load

        Me.Top = 0
        Me.Height = My.Computer.Screen.WorkingArea.Height

        txtAbfertDat.Text = Now.ToShortDateString 'CDate("01." & Now.ToString("MM.yyyy")).AddMonths(-1).ToShortDateString
        txtAbfertDatBis.Text = Now.ToShortDateString

        btnMonat.Text = MONAT.ToString("MMMM")
        btnJahr.Text = JAHR.Year

        Dim QSwhere = ""
        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            Case "ATILLA" : QSwhere = ""
            Case "IMEX" : QSwhere = " AND mit_firma IN ('IMEX','BEIDE') "
            Case "AMBAR" : QSwhere = " AND mit_firma IN ('AMBAR','BEIDE') "
            Case Else
                QSwhere = " AND mit_firma <> 'IMEX' "
        End Select


        '  cboSachbearbeiterQS.fillWithSQL("SELECT  ID,/*cast(ID as nvarchar(3)) +' ' + Name as*/ Name FROM Personal WHERE [Status]<>'L' " & QSwhere & " ORDER BY Name ", False, "FMZOLL", True)

        Dim where = ""
        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            Case "IMEX"
                where = " AND FilialenNr IN (5501) "
            Case "ATILLA"
                where = " AND FilialenNr IN (4801) "
            Case "AMBAR"
                where = " AND FilialenNr IN (5701) "
            Case Else
                where = " AND FilialenNr NOT IN (5501) "
        End Select

        cboFiliale.fillWithSQL("SELECT FilialenNr, Grenzstelle FROM Filialen WHERE Fil_Status='A' " & where & " ORDER BY FilialenNr ", True, "FMZOLL", True)
        Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
            Case "IMEX"
                'cboFiliale._allowedValuesFreiText = {"5501"}
                cboFiliale.changeItem("5501")
            Case "ATILLA"
                cboFiliale._allowedValuesFreiText = {"4801"}
                cboFiliale.changeItem("4801")
                cboFiliale.Enabled = False
            Case "AMBAR"
                cboFiliale._allowedValuesFreiText = {"5701"}
                cboFiliale.changeItem("5701")
                cboFiliale.Enabled = False
        End Select

        cboSachbearbeiterQS.fillWithSQL("SELECT [mit_id],[mit_vname] + ' '+ [mit_nname] as NAME " &
                                  " FROM [ADMIN].[dbo].[tblMitarbeiter] WHERE mit_niederlassung IN ('SUB','Suben')  AND mit_abteilung='QS' AND [mit_gekuendigt]=0   " & QSwhere & "  order by name", False, "ADMIN", True)

        btnMonat.PerformClick()
        btnSuche.PerformClick()
        bindPOST.updateImmediately = True
        AddHandler bindPOST.bindingdataTable.ColumnChanged, Sub(send As Object, ev As DataColumnChangeEventArgs)
                                                                btn.PerformClick() 'geht anders nicht ??

                                                            End Sub

    End Sub


    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        txtAbfertDat._value = CDate(sender.value).ToShortDateString
    End Sub

    Private Sub DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged
        txtAbfertDatBis._value = CDate(sender.value).ToShortDateString
    End Sub

    Sub initBtns(MonatJahr)
        btnMonat.Text = MONAT.ToString("MMMM")
        btnJahr.Text = JAHR.Year
        Select Case MonatJahr
            Case "Monat"
                DateTimePicker1.Value = MONAT
                DateTimePicker2.Value = MONAT.AddMonths(1).AddDays(-1)
            Case "Jahr"
                '  If cbxGeschaftsjahr.Checked Then
                'DateTimePicker1.Value = JAHR.AddMonths(1)
                ' DateTimePicker2.Value = JAHR.AddYears(1).AddMonths(1).AddDays(-1)
                ' Else
                DateTimePicker1.Value = JAHR
                DateTimePicker2.Value = JAHR.AddYears(1).AddDays(-1)
                'End If
            Case "Tag"
                '  If cbxGeschaftsjahr.Checked Then
                'DateTimePicker1.Value = JAHR.AddMonths(1)
                ' DateTimePicker2.Value = JAHR.AddYears(1).AddMonths(1).AddDays(-1)
                ' Else
                DateTimePicker1.Value = srcTAG
                DateTimePicker2.Value = srcTAG
                If srcTAG = Now.ToShortDateString Then
                    Button5.Text = "Heute"
                ElseIf srcTAG = Now.AddDays(-1).ToShortDateString Then
                    Button5.Text = "Gestern"
                ElseIf srcTAG = Now.AddDays(-2).ToShortDateString Then
                    Button5.Text = "Vorgestern"
                Else
                    Button5.Text = srcTAG.ToString("dd.MM")
                End If

                'End If
            Case ""
                DateTimePicker1.Value = ("01." & Now.Month & "." & Now.Year)
                DateTimePicker2.Value = Now
        End Select
        btnSuche.PerformClick()
    End Sub

    Dim biningInit = False
    Private Sub btnSuche_Click(sender As Object, e As EventArgs) Handles btnSuche.Click
        Me.Cursor = Cursors.WaitCursor

        Dim TopMax = If(IsNumeric(txtMaxSrch.Text), " TOP " & txtMaxSrch.Text, "")
        Dim sqlstr As String = ""


        '  bindPOST = New cEasyBinding("FMZOLL")
        sqlstr &= " SELECT " & TopMax & " [post_id],[post_Datum],[post_FilialenNr],[post_AbfertigungsNr],[post_art],[post_Empfaenger_KdNr],[post_Empfaenger_Name1],post_Empfaenger_Adresse1 + ' ' + post_Empfaenger_Adresse2 + ' ' + post_Empfaenger_Adresse3 as Adresse,[post_ContainerLKWNr],[post_FirmaInAuftragKdnr],[post_FirmaInAuftrag],[post_Sachbearbeiter],[post_ReferenzPaketdienst],[post_KdAuftrNrAvisierer],[post_RgEmpfaenger_Name] FROM [tblPostversand] WHERE 1=1 "

        With MyDatagridview1
            '  .DataSource = myTable
            '.SET_SQL(sqlstr & getVorauskasseWhere() & " ORDER BY post_Datum desc ", "FMZOLL", getVorauskasseList)
            '  .LOAD()
            'bindPOST.bindingdataSet.Tables(0) = New DataTable
            bindPOST.initBinding(sqlstr & getVorauskasseWhere() & " ORDER BY post_Datum desc", "tblPostversand", getVorauskasseList)
            .DataSource = bindPOST.bindingdataTable
            lblErgebnis.Text = "Ergebnisse: " & .Rows.Count

            'If Not biningInit Then
            '    biningInit = True

            'End If

            ' setDGVOptions()
        End With
        showDGV()

        Me.Cursor = Cursors.Default

    End Sub


    Function getVorauskasseWhere() As String
        Dim sqlstr = ""

        If txtAbfertDat.Text <> "" Then
            If txtAbfertDatBis.Text <> "" Then
                sqlstr &= " AND ([post_Datum] BETWEEN @Datum AND @DatumBis) "
            Else
                sqlstr &= " AND ([post_Datum] BETWEEN @Datum AND @Datum ) "
            End If
        End If

        If txtAbfertigungsnummer.Text <> "" Then
            '     If txtAbfertigungsnummerBis.Text <> "" Then
            'sqlstr &= " AND ([vk_AbfertigungsNr] BETWEEN @AbfertigungsNr AND @AbfertigungsNr_bis ) "
            'Else
            'sqlstr &= " AND vk_AbfertigungsNr LIKE @AbfertigungsNr "
            'End If
            sqlstr &= " AND post_AbfertigungsNr LIKE @AbfertigungsNr "
        End If
        ' If txtUnterNr.Text <> "" Then sqlstr &= " AND [UnterNr] LIKE @UnterNr "
        If cboFiliale._value <> "" Then sqlstr &= " AND [post_FilialenNr] LIKE @FilialenNr "

        If cboSachbearbeiterQS._value <> "" Then sqlstr &= " AND [post_SachbearbeiterId] = @PersonalnummerQS "
        If MyTextBox1.Text <> "" Then sqlstr &= " AND [post_ContainerLKWNr] LIKE @LKW "
        If txtFirma.Text <> "" Then sqlstr &= " AND [post_Empfaenger_Name1] LIKE @Firma "
        If txtInAuftrag.Text <> "" Then sqlstr &= " AND [post_FirmaInAuftrag] LIKE @InAuftrag "

        If txtReferenz.Text <> "" Then sqlstr &= " AND [post_ReferenzPaketdienst] LIKE @Referenz "

        Return sqlstr
    End Function
    Function getVorauskasseList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Datum", txtAbfertDat.Text & " 00:00:00"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DatumBis", txtAbfertDatBis.Text & " 23:59:59"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AbfertigungsNr", txtAbfertigungsnummer.Text.Replace("*", "%")))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FilialenNr", cboFiliale._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PersonalnummerQS", cboSachbearbeiterQS._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LKW", MyTextBox1.Text.Replace("*", "%")))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Firma", txtFirma.Text.Replace("*", "%")))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("InAuftrag", txtInAuftrag.Text.Replace("*", "%")))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Referenz", txtReferenz.Text.Replace("*", "%")))

        Return list

    End Function

    Sub showDGV()

        With MyDatagridview1

            If .RowCount = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            .RowHeadersVisible = False
            '     .ReadOnly = True
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False


            .Columns("post_id").Visible = False
            .Columns("post_art").Visible = False
            .Columns("post_KdAuftrNrAvisierer").Visible = False
            .Columns("post_RgEmpfaenger_Name").Visible = False

            .Columns("post_FilialenNr").Width = 45
            .Columns("post_FilialenNr").HeaderText = "Filiale"
            .Columns("post_FilialenNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            .Columns("post_AbfertigungsNr").Width = 80
            .Columns("post_AbfertigungsNr").HeaderText = "Abf-Nr"
            .Columns("post_AbfertigungsNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            .Columns("post_Datum").Width = 100
            .Columns("post_Datum").HeaderText = "Datum"
            .Columns("post_Datum").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

            .Columns("post_ContainerLKWNr").Width = 120
            .Columns("post_ContainerLKWNr").HeaderText = "LKW Kennz."



            .Columns("post_Empfaenger_KdNr").HeaderText = "KdNr"
            .Columns("post_Empfaenger_KdNr").Width = 50
            .Columns("post_Empfaenger_Name1").HeaderText = "Empfänger"
            .Columns("post_Empfaenger_Name1").Width = 150
            .Columns("Adresse").HeaderText = "Adresse"
            .Columns("Adresse").Width = 150

            .Columns("post_FirmaInAuftragKdnr").HeaderText = "KdNr"
            .Columns("post_FirmaInAuftragKdnr").Width = 50
            .Columns("post_FirmaInAuftrag").HeaderText = "in Auftrag"
            .Columns("post_FirmaInAuftrag").Width = 150

            .Columns("post_Sachbearbeiter").MinimumWidth = 100
            .Columns("post_Sachbearbeiter").HeaderText = "Sachb."

            .Columns("post_ReferenzPaketdienst").HeaderText = "Referenz Paketdienst"
            .Columns("post_ReferenzPaketdienst").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            For Each c As DataGridViewColumn In .Columns
                If c.Name = "post_ReferenzPaketdienst" Then
                    c.ReadOnly = False
                Else
                    c.ReadOnly = True
                End If
            Next

        End With
    End Sub


    Private Sub FlatButton7_Click(sender As Object, e As EventArgs) Handles FlatButton7.Click
        btnSuche.PerformClick()
        '        MyDatagridview1.RELOAD()
        '  setDGVOptions()
    End Sub

    'Sub setDGVOptions()
    '    Try
    '        With MyDatagridview1
    '            For Each r As DataGridViewRow In .Rows
    '                If r.Cells("vk_storno").Value IsNot DBNull.Value AndAlso r.Cells("vk_storno").Value Then
    '                    r.DefaultCellStyle.ForeColor = Color.Red
    '                ElseIf r.Cells("vk_GeldEingetroffen").Value IsNot DBNull.Value AndAlso r.Cells("vk_GeldEingetroffen").Value Then
    '                    r.DefaultCellStyle.ForeColor = Color.DarkGreen
    '                End If
    '            Next
    '        End With
    '    Catch ex As Exception
    '        MsgBox(ex.Message)
    '    End Try
    'End Sub
    Private Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click
        Me.Cursor = Cursors.WaitCursor
        cProgramFunctions.genExcelFromDGV_NEW(MyDatagridview1, False)
        Me.Cursor = Cursors.Default
    End Sub




    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        srcTAG = srcTAG.AddDays(-1)
        initBtns("Tag")
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        srcTAG = srcTAG.AddDays(1)
        initBtns("Tag")
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        initBtns("Tag")
    End Sub

    Private Sub btnMonat_Click(sender As Object, e As EventArgs) Handles btnMonat.Click
        initBtns("Monat")
    End Sub



    Private Sub btnJahr_Click(sender As Object, e As EventArgs) Handles btnJahr.Click
        initBtns("Jahr")
    End Sub

    Private Sub btnBisMonat_Click(sender As Object, e As EventArgs) Handles btnBisMonat.Click
        MONAT = MONAT.AddMonths(1)
        initBtns("Monat")
    End Sub

    Private Sub btnVonMonat_Click(sender As Object, e As EventArgs) Handles btnVonMonat.Click
        MONAT = MONAT.AddMonths(-1)
        initBtns("Monat")
    End Sub

    Private Sub btnBisJahr_Click(sender As Object, e As EventArgs) Handles btnBisJahr.Click
        JAHR = JAHR.AddYears(1)
        initBtns("Jahr")
    End Sub

    Private Sub btnVonJahr_Click(sender As Object, e As EventArgs) Handles btnVonJahr.Click
        JAHR = JAHR.AddYears(-1)
        initBtns("Jahr")
    End Sub '

    Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click

        If MyDatagridview1.SelectedRows.Count > 0 Then
            Dim VK As New VERAG_PROG_ALLGEMEIN.cPostversand(MyDatagridview1.SelectedRows(0).Cells("post_id").Value)
            If vbYes <> MsgBox("Möchten Sie den Eintrag wirklich löschen?", vbYesNoCancel) Then Exit Sub
            VK.DELETE()
            btnSuche.PerformClick()

            'MyDatagridview1.RELOAD()
            '  setDGVOptions()
        End If

    End Sub


    Private Sub gridAviso_DoubleClick(sender As Object, e As System.EventArgs) Handles MyDatagridview1.DoubleClick
        If MyDatagridview1.SelectedRows.Count = 0 And MyDatagridview1.SelectedRows.Count = 0 Then Exit Sub

        Dim kdnr = MyDatagridview1.SelectedRows(0).Cells("post_Empfaenger_KdNr").Value
        SDL.FormularManagerNEU.OPEN_FORM(SDL.FormularManagerArten.Verzollungsunterlagen, kdnr, {kdnr, Nothing, Nothing, Nothing, "", MyDatagridview1.SelectedRows(0).Cells("post_id").Value})
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SDL.FormularManagerNEU.OPEN_FORM(SDL.FormularManagerArten.Verzollungsunterlagen)
        btnSuche.PerformClick()
    End Sub

    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        bindPOST.updateBinding() ' geht nur mit dem Umweg über Button
    End Sub
End Class

