Imports System.IO
Imports System.Globalization

Public Class usrCntlAuswertungenAufschub_AbgabenKontrolleTRIER



    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Private Property rst As Object

    Private Sub usrCntlBrgBuchungenFremd_Load(sender As Object, e As EventArgs) Handles Me.Load


    End Sub

    Function get_DT() As DataTable
        Return SQL.loadDgvBySql("", "FMZOLL")
    End Function

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Me.Cursor = Cursors.WaitCursor
        Try

            Dim displayFilter = False

            Dim srchAufschubnehmer = ""
            Dim AKTO = ""
            Dim AKTONr = ""
            Dim Faelligkeit = ""

            Dim erg = ""
            Dim TMPFIRMA = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA

            Dim fd As New OpenFileDialog

            If fd.ShowDialog = DialogResult.OK Then

                Dim dt As New DataTable
                dt.Columns.Add("AKTO")
                dt.Columns.Add("Faelligkeit")
                dt.Columns.Add("ATC")
                dt.Columns.Add("BUDat")
                dt.Columns.Add("Abfertigungsort")
                dt.Columns.Add("Betrag")
                dt.Columns.Add("Fakturiert")
                dt.Columns.Add("Differenz")

                ' Dim fileReader() = System.IO.File.ReadAllLines(fd.FileName, System.Text.Encoding.UTF32)
                Dim cnt = 1
                '  For Each line In fileReader
                '  Dim Flines = System.IO.File.ReadLines(fd.FileName)
                For Each line As String In System.IO.File.ReadLines(fd.FileName, System.Text.Encoding.Default)
                    'MsgBox(line.Length)
                    If srchAufschubnehmer = "" AndAlso line.Contains("Aufschubnehmer: ") Then
                        srchAufschubnehmer = line.Replace("Aufschubnehmer: ", "").Trim
                        AKTO = srchAufschubnehmer.Substring(0, 13).Trim
                        If AKTO.Contains("-") Then Dim sp = AKTO.Split("-") : AKTONr = sp(1) 'String.Format("{000000}", sp(1))

                        If srchAufschubnehmer.ToUpper.Contains("VERAG") Then
                            TMPFIRMA = "VERAG"
                        ElseIf srchAufschubnehmer.ToUpper.Contains("UNISPED") Then
                            TMPFIRMA = "UNISPED"
                        ElseIf srchAufschubnehmer.ToUpper.Contains("IMEX") Then
                            TMPFIRMA = "IMEX"

                        End If
                    End If
                    If Faelligkeit = "" AndAlso line.Contains("Fälligkeit: ") Then
                        Faelligkeit = line.Replace("Fälligkeit: ", "").Trim
                    End If


                    If line.Length = "79" And line.Contains("ATC") Then
                        Dim zollamt As String = line.Substring(0, 28).Trim
                        Dim atc As String = line.Substring(28, 25).Trim
                        Dim datum As Date = CDate(line.Substring(53, 10).Trim)
                        Dim betrag As Double = CDbl(line.Substring(63, 16).Trim)

                        '  Dim fakturiert = CDbl(SQL.DLookup("Betrag", "Zkteing", "[Status]='F' AND [Registriernummer]='" & atc & "' AND [AKTO]=" & AKTONr & "", "FMZOLL", "0"))
                        Dim fakturiert = CDbl(SQL.DLookup("sum(Preis)", "Rechnungsausgang inner join [RechnungsausgangPositionen] On Rechnungsausgang.RK_ID=[RechnungsausgangPositionen].RK_ID", " [Status]<>0 and DruckDatumZeit is not null  and (left([RechnungsausgangPositionen].Belegdaten,21)='" & atc & "' or [RechnungsausgangPositionen].[Weitere_Reg_Nr] ='" & atc & "' )AND [AufschubkontoNr]='" & AKTO & "'", "FMZOLL", "0"))

                        Dim differenz = fakturiert - betrag

                        If Not cbx.Checked OrElse differenz <> 0 Then
                            erg &= zollamt & " -  " & atc & ":     " & betrag & " | " & differenz & vbNewLine
                            dt.Rows.Add({AKTO, Faelligkeit, atc, datum, zollamt, betrag, fakturiert, differenz})
                        End If

                    End If
                    cnt += 1
                    lnlCnt.Text = cnt '& "/" & Flines.Count
                    Me.Refresh()
                Next

                If dt.Rows.Count = 0 Then
                    MsgBox("Keine Differenzen gefunden!")
                Else

                    If cbxExcel.Checked Then

                        If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
                            VERAG_PROG_ALLGEMEIN.cProgramFunctions.genExcelFromDT_NEW(dt, {"F1:F" & (dt.Rows.Count + 1), "G1:G" & (dt.Rows.Count + 1), "H1:H" & (dt.Rows.Count + 1)},,,, True)
                        Else



                            Dim rpt As New rptAufschubabgleich
                            rpt.DataSource = dt

                            Dim print As New frmPrintLayout

                            print.Text = "Abgabenliste"

                            If rpt.DataSource Is Nothing OrElse rpt.DataSource.rows.count = 0 Then
                                rpt.Dispose() : print.Dispose()
                                MsgBox("Keine Daten vorhanden!")
                                Me.Cursor = Cursors.Default
                                Exit Sub
                            End If

                            Select Case TMPFIRMA
                                Case "IMEX" : rpt.picVERAG.Image = My.Resources.IMEX_LOGO_simple : rpt.Label8.Text = "IMEX Customs Service GmbH"
                                Case "ATILLA" : rpt.picVERAG.Image = My.Resources.Atilla : rpt.Label8.Text = "ATILLA Spedition"
                                Case "UNISPED" : rpt.picVERAG.Image = My.Resources.Atilla : rpt.Label8.Text = "UNISPED Spedition"
                                Case "AMBAR" : rpt.picVERAG.Image = My.Resources.Atilla : rpt.Label8.Text = "AMBAR"
                                Case "FRONTOFFICE" : rpt.picVERAG.Image = My.Resources.Atilla : rpt.Label8.Text = "FRONTOFFICE"
                                Case Else
                            End Select


                            Dim sum = 0
                            rpt.lblKontoBez.Text = srchAufschubnehmer
                            rpt.lblFaelligkeit.Text = Faelligkeit
                            ' rpt.lblAnzahl.Text = cnt
                            rpt.lblDetails.Text = Now.ToLongDateString & ", " & VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME

                            Dim sumBetrag As Double = 0
                            Dim sumFakturiert As Double = 0
                            Dim sumDifferenz As Double = 0

                            Dim Anz = 0
                            AddHandler rpt.Detail.Format, Sub()

                                                              rpt.lblAKTO.Text = checkNull(rpt.Fields.Item("AKTO").Value)
                                                              'rpt.lblFaelligkeit.Text = checkNull(rpt.Fields.Item("Faelligkeit").Value)
                                                              rpt.lblATC.Text = checkNull(rpt.Fields.Item("ATC").Value)

                                                              rpt.lblBUDatValue.Text = checkNull(rpt.Fields.Item("BUDat").Value)
                                                              rpt.lblAbfertigungsort.Text = checkNull(rpt.Fields.Item("Abfertigungsort").Value)
                                                              rpt.lblBetrag.Text = checkNull(rpt.Fields.Item("Betrag").Value)
                                                              rpt.lblFakturiert.Text = checkNull(rpt.Fields.Item("Fakturiert").Value)
                                                              rpt.lblDifferenz.Text = checkNull(rpt.Fields.Item("Differenz").Value)

                                                              Dim tmp_betrag As Double = 0
                                                              Dim tmp_fakturiert As Double = 0
                                                              Dim tmp_differenz As Double = 0

                                                              If IsNumeric(checkNull(rpt.Fields.Item("betrag").Value)) Then tmp_betrag = CDbl(rpt.Fields.Item("Betrag").Value())
                                                              rpt.lblBetrag.Text = CDbl(tmp_betrag).ToString("N2", Globalization.CultureInfo.CurrentCulture)

                                                              If IsNumeric(checkNull(rpt.Fields.Item("Fakturiert").Value)) Then tmp_fakturiert = CDbl(rpt.Fields.Item("Fakturiert").Value())
                                                              rpt.lblFakturiert.Text = CDbl(tmp_fakturiert).ToString("N2", Globalization.CultureInfo.CurrentCulture)

                                                              If IsNumeric(checkNull(rpt.Fields.Item("Differenz").Value)) Then tmp_differenz = CDbl(rpt.Fields.Item("Differenz").Value())
                                                              rpt.lblDifferenz.Text = CDbl(tmp_differenz).ToString("N2", Globalization.CultureInfo.CurrentCulture)


                                                              sumBetrag += tmp_betrag
                                                              sumFakturiert += tmp_fakturiert
                                                              sumDifferenz += tmp_differenz

                                                              Anz += 1

                                                              If Anz Mod 2 = 0 Then
                                                                  rpt.Detail.BackColor = Color.FromArgb(240, 240, 240)
                                                              Else
                                                                  rpt.Detail.BackColor = Color.White
                                                              End If

                                                          End Sub

                            AddHandler rpt.ReportFooter1.Format, Sub()

                                                                     rpt.lblBetragSum.Text = sumBetrag.ToString("N2", CultureInfo.CurrentCulture)
                                                                     rpt.lblFakturiertSum.Text = sumFakturiert.ToString("N2", CultureInfo.CurrentCulture)
                                                                     rpt.lblDifferenzSum.Text = sumDifferenz.ToString("N2", CultureInfo.CurrentCulture)
                                                                     rpt.lblAnzahl.Text = Anz.ToString("N0") & "(" & Anz.ToString("N0") & " im Datensatz)"

                                                                 End Sub

                            print.Viewer.LoadDocument(rpt)
                            print.Viewer.ViewType = GrapeCity.Viewer.Common.Model.ViewType.Continuous
                            print.Show()
                        End If
                    End If
                End If

            End If


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



    End Sub

    Function checkNull(o As Object, Optional ret As String = "") As Object
        If o Is Nothing Then Return ret
        If o Is DBNull.Value Then Return ret
        Return o

    End Function



End Class
