Imports javax.smartcardio

Public Class frmOriginalArchiv_ATR



    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)

    Public SendungKoppeln As Boolean = False
    Public SendungsId As Integer = -1

    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


        cboDatum.Items.Clear()
        cboDatum.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Ausstellungs-Datum", "ADat"))
        cboDatum.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Erhalten-Datum", "EDat"))
        cboDatum.changeItem("ADat")


        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
        If SendungKoppeln Then btnSendungKoppeln.Visible = True

        '  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


        btnMonat.PerformClick()
        btnSuche.PerformClick()


    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 & " atr_id,atr_documentNr, atr_austellungsdatum, atr_FilialenNr, atr_AbfertigungsNr, atr_LKW,atr_EmpfaengerKdNr, atr_Empfaenger, atr_AbsenderKdNr,atr_Absender, atr_Sachbearbeiter,atr_DHL_ReferenzNr  FROM [tblOriginalATR] WHERE 1=1 "

        With MyDatagridview1
            .SET_SQL(sqlstr & getWhere() & " ORDER BY atr_austellungsdatum desc", "FMZOLL", getList)
            .LOAD()

            lblErgebnis.Text = "Ergebnisse: " & .Rows.Count

            ' setDGVOptions()
        End With
        showDGV()

        Me.Cursor = Cursors.Default

    End Sub


    Function getWhere() As String
        Dim sqlstr = ""


        If cboDatum._value = "ADat" Then

            If txtAbfertDat.Text <> "" Then
                If txtAbfertDatBis.Text <> "" Then
                    sqlstr &= " AND ([atr_austellungsdatum] BETWEEN @Datum AND @DatumBis) "
                Else
                    sqlstr &= " AND ([atr_austellungsdatum] BETWEEN @Datum AND @Datum ) "
                End If
            End If
        ElseIf cboDatum._value = "EDat" Then

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


        If txtSuche._value <> "" Then
            sqlstr &= " AND ( "
            sqlstr &= " atr_documentNr LIKE '" & txtSuche._value & "%' OR "
            sqlstr &= " atr_absender LIKE '" & txtSuche._value & "%' OR "
            sqlstr &= " atr_empfaenger LIKE '" & txtSuche._value & "%' OR "
            sqlstr &= " atr_DHL_ReferenzNr LIKE '" & txtSuche._value & "%' OR "
            sqlstr &= " atr_LKW LIKE '" & txtSuche._value & "%' OR "
            sqlstr &= " atr_location LIKE '" & txtSuche._value & "%' OR "
            sqlstr &= " atr_info LIKE '" & txtSuche._value & "%' OR "
            sqlstr &= " atr_DHL_ReferenzNr LIKE '" & txtSuche._value & "%' "
            sqlstr &= " ) "
        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 atr_AbfertigungsNr LIKE @AbfertigungsNr "
        End If
        ' If txtUnterNr.Text <> "" Then sqlstr &= " AND [UnterNr] LIKE @UnterNr "
        If cboFiliale._value <> "" Then sqlstr &= " AND [atr_FilialenNr] LIKE @FilialenNr "

        If txtSB._value <> "" Then sqlstr &= " AND [atr_mitId] = @PersonalnummerQS "
        If txtLKWKennzeichen.Text <> "" Then sqlstr &= " AND [atr_LKW] LIKE @LKW "
        If txtFEmpfänger.Text <> "" Then sqlstr &= " AND [atr_empfaenger] LIKE @Empfaenger "
        If txtAbsender.Text <> "" Then sqlstr &= " AND [atr_absender] LIKE @Absender "
        If txtDocumetNummer.Text <> "" Then sqlstr &= " AND [atr_documentNr] LIKE @DocumentNummer "

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

        Return sqlstr
    End Function
    Function getList() 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("DocumentNummer", txtDocumetNummer.Text.Replace("*", "%")))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FilialenNr", cboFiliale._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("PersonalnummerQS", txtSB._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("LKW", txtLKWKennzeichen.Text.Replace("*", "%")))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Absender", txtAbsender.Text.Replace("*", "%")))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Empfaenger", txtFEmpfänger.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("atr_id").Visible = False
            '  .Columns("post_art").Visible = False
            ' .Columns("post_KdAuftrNrAvisierer").Visible = False
            ' .Columns("atr_EmpfaengerKdNr").Visible = False
            ' .Columns("atr_AbsenderKdNr").Visible = False

            .Columns("atr_FilialenNr").Width = 45
            .Columns("atr_FilialenNr").HeaderText = "Filiale"
            .Columns("atr_FilialenNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            .Columns("atr_AbfertigungsNr").Width = 80
            .Columns("atr_AbfertigungsNr").HeaderText = "Abf-Nr"
            .Columns("atr_AbfertigungsNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            .Columns("atr_documentNr").Width = 120
            .Columns("atr_documentNr").HeaderText = "ATR-Nr"
            .Columns("atr_documentNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            .Columns("atr_austellungsdatum").Width = 100
            .Columns("atr_austellungsdatum").HeaderText = "Datum"
            .Columns("atr_austellungsdatum").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

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



            .Columns("atr_EmpfaengerKdNr").HeaderText = "KdNr"
            .Columns("atr_EmpfaengerKdNr").Width = 50
            .Columns("atr_Empfaenger").HeaderText = "Empfänger"
            .Columns("atr_Empfaenger").Width = 150

            .Columns("atr_AbsenderKdNr").HeaderText = "KdNr"
            .Columns("atr_AbsenderKdNr").Width = 50
            .Columns("atr_Absender").HeaderText = "in Auftrag"
            .Columns("atr_Absender").Width = 150

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

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


        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 ATR As New VERAG_PROG_ALLGEMEIN.cOrigianlATR(MyDatagridview1.SelectedRows(0).Cells("atr_id").Value)
            If vbYes <> MsgBox("Möchten Sie den Eintrag wirklich löschen?", vbYesNoCancel) Then Exit Sub
            ATR.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 atr_id = MyDatagridview1.SelectedRows(0).Cells("atr_id").Value
        Dim f As New frmATR(atr_id)
        f.ShowDialog(Me)
        btnSuche.PerformClick()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim f As New frmATR
        f.ShowDialog(Me)
        btnSuche.PerformClick()
    End Sub

    Private Sub txtSuche_TextChanged(sender As Object, e As EventArgs) Handles txtSuche.TextChanged

    End Sub

    Private Sub txtSuche_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSuche.KeyDown
        If e.KeyCode = Keys.Return Then
            btnSuche.PerformClick()
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnSendungKoppeln.Click
        If MyDatagridview1.SelectedRows.Count > 0 Then
            Dim ATR = New VERAG_PROG_ALLGEMEIN.cOrigianlATR(MyDatagridview1.SelectedRows(0).Cells("atr_id").Value)
            If ATR IsNot Nothing Then

                If vbYes = MsgBox("ATR in Anhang speichern?", vbYesNoCancel) Then
                    If ATR.atr_dsId < 0 Then MsgBox("Kein PDF-Anhang!") : Exit Sub
                    Dim ANH = New VERAG_PROG_ALLGEMEIN.cAvisoAnhaenge(ATR.atr_anhID)
                    ANH.anh_SendungsId = SendungsId
                    Dim SND As New VERAG_PROG_ALLGEMEIN.cSendungen(SendungsId)
                    ANH.anh_AvisoId = SND.tblSnd_AvisoID
                    ANH.anh_docId = ATR.atr_dsId
                    ANH.anh_Name = "ATR_ARCHIV"
                    ANH.anh_Typ = VERAG_PROG_ALLGEMEIN.cProgramFunctions.getFilePathTypeValid(VERAG_PROG_ALLGEMEIN.cDATENSERVER.GET_PDFPath_BY_DocID(ATR.atr_dsId))
                    ANH.anh_Art = "ATR"
                    If ANH.SAVE() Then 'Eintrag wird überschreiben 
                        ATR.atr_anhID = ANH.anh_id
                        ATR.atr_SendungsId = SendungsId
                        ATR.atr_AvisoId = SND.tblSnd_AvisoID
                        ATR.atr_FilialenNr = SND.FilialenNr
                        ATR.atr_AbfertigungsNr = SND.AbfertigungsNr
                        If ATR.SAVE() Then
                            frmATR.insertVermerkATR(ATR)
                            Me.Close()
                        End If
                    End If
                End If
            End If
        End If

    End Sub

    Private Sub PictureBox6_Click(sender As Object, e As EventArgs) Handles PictureBox6.Click
        Dim webAddress As String = "https://wiki.verag.ag/de/software/aviso/howtos/ATRArchiv"
        Process.Start(webAddress)
    End Sub
End Class

