Imports System.Web.UI.WebControls.Expressions
Imports VERAG_PROG_ALLGEMEIN.VERAG_OUT_NCTS

Public Class usrcntlVAT_ATEZ
    Dim sql As New VERAG_PROG_ALLGEMEIN.SQL
    Dim user, pw As String
    Dim land As String
    Public dataTable As New DataTable()
    Public va As VAT
    Dim uidDefault As String
    Public createCostumer As Boolean = False
    Public setAmountOnly As Boolean = False

    Sub New()

        InitializeComponent()

    End Sub


    Sub New(_UID As String, _name As String, _land As String, _PLZ As String, _Strasse As String, _Ort As String)

        InitializeComponent()
        uidDefault = _UID
        _name = _name.Replace(Chr(34), "") 'Anführungszeichen entfernen
        _name = _name.Replace(Chr(176), "") '° entfernen
        txtFirma.Text = _name
        txtPLZ.Text = _PLZ
        txtOrt.Text = _Ort
        txtStrasse.Text = _Strasse
        land = _land

    End Sub

    Private Sub init()

        'cboLand.changeItem(land)
        'initDataTable()
        btnUebernehmen.Enabled = False
        Cursor = Cursors.Default

        If uidDefault <> "" Then
            uidDefault = uidDefault.Replace(" ", "")
            uidDefault = uidDefault.Trim()

            If uidDefault.ToUpper.StartsWith("ATU") Then
                uidDefault = uidDefault.Replace("ATU", "")
                txtUID.Text = "ATU " & uidDefault
            Else

                txtUID.Text = uidDefault
            End If

        End If

        bcxOnlySetPrice.Checked = setAmountOnly


    End Sub


    Sub initDGV(dt As DataTable)

        dgvVAT.DataSource = dt


        If dgvVAT.Columns.Count > 0 Then

            Dim pageCpunterCheck = 0



            lblPagecount.Text &= dgvVAT.Rows(0).Cells("total_pages").Value

            With dgvVAT
                .RowHeadersWidth = 10
                .Columns("count").HeaderText = "#"
                .Columns("count").Width = 25
                .Columns("total_VAT_currency").HeaderText = "Währung"
                .Columns("total_VAT_currency").Width = 50
                .Columns("invoice_id").HeaderText = "Rechnungs-Nr"
                .Columns("seller_VAT_id").HeaderText = "UID-Nr"
                .Columns("seller_VAT_id").Width = 100
                .Columns("seller_address_city").Width = 100
                .Columns("seller_address_city").HeaderText = "Stadt"
                .Columns("seller_address_country").Visible = False
                .Columns("seller_address_country").HeaderText = "Land"
                .Columns("total_vat_amount").HeaderText = "MWST"
                .Columns("seller_address_postcode").Width = 75
                .Columns("seller_address_postcode").HeaderText = "PLZ"
                .Columns("seller_address_street").Width = 125
                .Columns("seller_address_street").HeaderText = "Strasse"
                .Columns("seller_name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns("seller_name").HeaderText = "Leistender"
                .Columns("status").Width = 75
                .Columns("status").HeaderText = "Status"
                .Columns("invoice_date").Width = 100
                .Columns("invoice_date").HeaderText = "Rechnungsdatum"
                .Columns("doc_index").HeaderText = "Dok.#"
                .Columns("doc_index").Width = 50
                .Columns("page_count").HeaderText = "Seiten"
                .Columns("page_count").Width = 50
                .Columns("start_page").HeaderText = "von"
                .Columns("start_page").Width = 50
                .Columns("end_page").HeaderText = "bis"
                .Columns("end_page").Width = 50
                .Sort(.Columns("start_page"), System.ComponentModel.ListSortDirection.Ascending)


                '----------------------------------------
                .Columns("count").Visible = False
                .Columns("document_count").Visible = False
                .Columns("total_pages").Visible = False
                .Columns("seller_address_full").Visible = False
                .Columns("doc_index").Visible = False
                '----------------------------------------



            End With

            For Each row As DataGridViewRow In dgvVAT.Rows


                If IsNumeric(row.Cells("page_count").Value) AndAlso row.Cells("page_count").Value <> 0 Then
                    pageCpunterCheck += row.Cells("page_count").Value
                End If

            Next

            If dgvVAT.Rows(0).Cells("total_pages").Value <> pageCpunterCheck Then
                lblLand.Text = "ACHTUNG, es wurden nur " & pageCpunterCheck & " von möglichen " & dgvVAT.Rows(0).Cells("total_pages").Value & " Seiten per KI ausgelesen, das kann uU zu fehlern beim Aufsplitten der Seiten führen!"
                lblLand.ForeColor = Color.Red
                lblLand.Visible = True
            Else
                lblLand.Visible = False

            End If

        End If


    End Sub

    Sub setColorIndgv()

        For Each row As DataGridViewRow In dgvVAT.Rows

            Dim correctPages As Boolean = False

            If IsNumeric(row.Cells("start_page").Value) AndAlso IsNumeric(row.Cells("end_page").Value) AndAlso IsNumeric(row.Cells("page_count").Value) Then
                If (Math.Abs(row.Cells("end_page").Value - row.Cells("start_page").Value) + 1) = row.Cells("page_count").Value Then
                    correctPages = True
                Else
                    row.DefaultCellStyle.BackColor = Color.IndianRed
                End If
            End If


            If Not IsDBNull(row.Cells("status").Value) Then
                Dim akiv As String = (row.Cells("status").Value.ToString).ToLower
                If akiv = "success" Then

                    If Not IsDBNull(row.Cells("total_vat_amount").Value) Then

                        If IsNumeric(row.Cells("total_vat_amount").Value) AndAlso row.Cells("total_vat_amount").Value <> 0 Then
                            If correctPages Then row.DefaultCellStyle.BackColor = Color.LightGreen
                        Else
                            If correctPages Then row.DefaultCellStyle.BackColor = Color.IndianRed
                        End If

                    Else

                        row.DefaultCellStyle.BackColor = Color.LightYellow
                    End If

                ElseIf akiv = "nonactive" Then
                    row.DefaultCellStyle.BackColor = Color.IndianRed
                Else
                    row.DefaultCellStyle.BackColor = Color.LightYellow
                End If
            End If





        Next


    End Sub

    Private Sub btnUebernehmen_Click(sender As Object, e As EventArgs) Handles btnUebernehmen.Click

        Dim o As Form = Me.FindForm
        '   If Me.Parent.GetType() Is GetType(Form) Then o = Me.FindForm

        For Each row As DataGridViewRow In dgvVAT.SelectedRows
            va = New VAT(IIf(IsDBNull(row.Cells("count").Value), "", row.Cells("count").Value),
                IIf(IsDBNull(row.Cells("total_VAT_currency").Value), "", row.Cells("total_VAT_currency").Value),
                               IIf(IsDBNull(row.Cells("invoice_id").Value), "", row.Cells("invoice_id").Value),
                               IIf(IsDBNull(row.Cells("seller_VAT_id").Value), "", row.Cells("seller_VAT_id").Value),
                               IIf(IsDBNull(row.Cells("seller_address_city").Value), "", row.Cells("seller_address_city").Value),
                               IIf(IsDBNull(row.Cells("seller_address_country").Value), "", row.Cells("seller_address_country").Value),
                               IIf(IsDBNull(row.Cells("seller_address_postcode").Value), "", row.Cells("seller_address_postcode").Value),
                               IIf(IsDBNull(row.Cells("seller_address_street").Value), "", row.Cells("seller_address_street").Value),
                               IIf(IsDBNull(row.Cells("seller_address_full").Value), "", row.Cells("seller_address_full").Value),
                               IIf(IsDBNull(row.Cells("seller_name").Value), "", row.Cells("seller_name").Value),
                                IIf(IsDBNull(row.Cells("total_VAT_amount").Value), "", row.Cells("total_VAT_amount").Value),
                                IIf(IsDBNull(row.Cells("invoice_date").Value) Or Not IsDate(row.Cells("invoice_date").Value), Nothing, CDate(row.Cells("invoice_date").Value)),
                               IIf(IsDBNull(row.Cells("status").Value), "", row.Cells("status").Value),
                                IIf(IsDBNull(row.Cells("document_count").Value), "", row.Cells("document_count").Value),
                                IIf(IsDBNull(row.Cells("total_pages").Value), "", row.Cells("total_pages").Value),
                                IIf(IsDBNull(row.Cells("doc_index").Value), "", row.Cells("doc_index").Value),
                                IIf(IsDBNull(row.Cells("page_count").Value), "", row.Cells("page_count").Value))
        Next

        If va.status.ToLower <> "success" Then
            Dim a As MsgBoxResult = MsgBox("Die Rechnung " & va.invoice_id & " besitzt den Status: " & va.status & vbNewLine & "Trotzdem fortfahren?", vbYesNo)
            If a = vbYes Then
                o.DialogResult = DialogResult.OK
                Exit Sub
            Else
                o.DialogResult = DialogResult.None
            End If
        Else
            lblWarning.Text = ""
            o.DialogResult = DialogResult.OK
            Exit Sub
        End If



    End Sub


    Public Class VAT

        Public Property count As Integer
        Public Property currency As String
        Public Property invoice_id As String
        Public Property seller_VAT_id As String
        Public Property seller_address_city As String
        Public Property seller_address_country As String
        Public Property seller_address_postcode As String
        Public Property seller_address_street As String
        Public Property seller_address_full As String
        Public Property seller_name As String
        Public Property invoice_date As Date
        Public Property total_VAT_amount As String
        Public Property status As String
        Public Property document_count As Integer
        Public Property total_pages As Integer
        Public Property page_count As Integer
        Public Property doc_index As Integer



        Public Sub New(_count As Integer, _currency As String, _invoice_id As String, _seller_VAT_id As String, _seller_address_city As String, _seller_address_country As String, _seller_address_postcode As String, _seller_address_street As String, _seller_address_full As String, _seller_name As String, _total_VAT_amount As String, _invoice_date As DateTime, _status As String, _document_count As Integer, _total_pages As Integer, _doc_index As Integer, _page_count As Integer)
            count = _count
            currency = _currency
            invoice_id = _invoice_id
            seller_VAT_id = _seller_VAT_id
            seller_address_city = _seller_address_city
            seller_address_country = _seller_address_country
            seller_address_postcode = _seller_address_postcode
            seller_address_street = _seller_address_street
            seller_address_full = _seller_address_full
            seller_name = _seller_name
            total_VAT_amount = _total_VAT_amount
            invoice_date = _invoice_date
            status = _status
            document_count = _document_count
            total_pages = _total_pages
            page_count = _page_count
            doc_index = _doc_index
        End Sub


    End Class

    Private Sub dgvCreditsafe_SelectionChanged(sender As Object, e As EventArgs) Handles dgvVAT.SelectionChanged
        If dgvVAT.SelectedRows.Count = 0 Then
            btnUebernehmen.Enabled = False
        Else
            btnUebernehmen.Enabled = True
        End If

        Me.setColorIndgv()

    End Sub


    Private Sub usrcntlCreditsafe_Load(sender As Object, e As EventArgs) Handles Me.Load
        init()
        setColorIndgv()
    End Sub

    Private Sub dgvVAT_Paint(sender As Object, e As PaintEventArgs) Handles dgvVAT.Paint
        setColorIndgv()
    End Sub

    Private Sub dgvCreditsafe_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvVAT.CellDoubleClick
        If dgvVAT.SelectedRows.Count = 1 Then
            btnUebernehmen.PerformClick()
        End If
    End Sub
End Class
