Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class KundenPreisregeln
    Inherits AbstractForm
    Implements InterfaceExportableGrid

    Private Sub Form_Verlassen() Handles MyBase.Verlassen
        Me.Close()
    End Sub
    Public Overloads Sub Export(sender As Object) Implements InterfaceExportableGrid.Export
        FunctionsDataGrid.ExportDGToCSV(PreiseDataGridView)
    End Sub


    'Handle Errors old data in the table for example -1 in ArtKAtNr
    Private Sub DataGridView1_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles PreiseDataGridView.DataError

        WriteLog("Error happened " & e.Context.ToString())

        If (e.Context = DataGridViewDataErrorContexts.Commit) _
            Then
            WriteLog("Commit error")
        End If
        If (e.Context = DataGridViewDataErrorContexts.CurrentCellChange) Then
            WriteLog("Cell change")
        End If
        If (e.Context = DataGridViewDataErrorContexts.Parsing) Then
            WriteLog("parsing error")
        End If
        If (e.Context = _
            DataGridViewDataErrorContexts.LeaveControl) Then
            WriteLog("leave control error")
        End If

        If (TypeOf (e.Exception) Is ConstraintException) Then
            Dim view As DataGridView = CType(sender, DataGridView)
            view.Rows(e.RowIndex).ErrorText = "an error"
            view.Rows(e.RowIndex).Cells(e.ColumnIndex) _
                .ErrorText = "an error"

            e.ThrowException = False
        End If
    End Sub

    Private Sub ArtikelPreise_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load

        Try
            'Fill grid combos first 
            FillComboBoxInDG(IDNRColumn, AdressenControl.SQL_PREISLISTEN, "Adr", "IDNR")
            FillComboBoxInDG(ArtNrColumn, ArtikelControl.SQL, "Art", "ArtNR")

            'Fill grid data 
            Try
                Grartikel_kundenpreisTableAdapter.Fill(DsPreise._grartikel_kundenpreis, PreislisteName:=PreislisteComboBox.SelectedValue, IDNR:=AdressenControl1.IDNR)
            Catch ex As Exception
                ' HandleAppError(ex)
            End Try
            'Me.GrArtikellisteTableAdapter.Fill(Me.DsArtikel.grArtikelliste)
            FillComboBox(ArtKatNrComboBox, "SELECT artkatnr, name from `grArtikel-Kategorien` order by name asc", "Name", "ArtKatNr")
            FillComboBox(PreislisteComboBox, SQL_PREISLISTEN, "PreislisteName")


            ArtKatNrComboBox.Text = ""
            PreislisteComboBox.Text = ""

            'Abstract 
            ds = DsPreise
            ParentBindingNavigator.BindingSource = GrartikelkundenpreisBindingSource

            Dim fU = New FormUtility
            fU.MakeSplitterCustomizable(SplitContainer, "KUNDENPREISREGELN_SPLITTER_SIZE")
        Catch ex As Exception
            HandleAppError(ex)
        End Try

    End Sub

    Function IstKundeOderPreislisteAusgewaehlt() As Boolean
        Return AdressenControl1.IDNR > 0 Or PreislisteComboBox.Text.Length > 0
    End Function

    'Filtern
    Private Sub FilterButton_Click(sender As Object, e As System.EventArgs) Handles FilterButton.Click
        Try
            LabelInstruktion.Text = ""

            If IsDBNull(PreislisteComboBox.SelectedValue) Then
                LabelInstruktion.Text = "Wähle eine Preisliste"
                'Exit Sub
                Grartikel_kundenpreisTableAdapter.Fill(DsPreise._grartikel_kundenpreis, IDNR:=AdressenControl1.IDNR, PreislisteName:=Nothing)
            Else
                Grartikel_kundenpreisTableAdapter.Fill(DsPreise._grartikel_kundenpreis, IDNR:=AdressenControl1.IDNR, PreislisteName:=PreislisteComboBox.SelectedValue)
            End If



            Dim filter As String = "1=1 "

            If AdressenControl1.IDNR > 0 Then
                filter += " and (IDNR is null or IDNR =  " & AdressenControl1.IDNR & ")"
            End If

            If PreislisteComboBox.Text.Length > 0 Then
                filter += " and (PreislisteName is null or PreislisteName Like '" + PreislisteComboBox.Text + "')"
            End If

            If ArtikelControl.ArtNr > 0 And IsNumeric(ArtikelControl.ArtNr) Then
                filter += " and ArtNr = " + CStr(ArtikelControl.ArtNr)
            End If

            If BezeichnungTextBox.Text.Length > 0 Then
                filter += " and ( EAN Like '%" + BezeichnungTextBox.Text + "%' or Bezeichnung Like '%" + BezeichnungTextBox.Text + "%') "
            End If

            If ArtKatNrComboBox.SelectedValue > 0 Then
                filter += " and ArtKatNr =  " & CStr(ArtKatNrComboBox.SelectedValue)
            End If

            If PreisVonTextBox.Text.Length > 0 And IsNumeric(PreisVonTextBox.Text) Then
                filter += " and VKPreis  >= " + PreisVonTextBox.Text + ""
            End If

            If PreisBisTextBox.Text.Length > 0 And IsNumeric(PreisBisTextBox.Text) Then
                filter += " and VKPreis <= " + PreisBisTextBox.Text + ""
            End If

            WriteLog("Preise Filter: " & filter)

            GrartikelkundenpreisBindingSource.Filter = filter

        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub BindingNavigatorSaveItem_Click(sender As Object, e As System.EventArgs) Handles MyBase.Save
        Try
            Validate()
            GrartikelkundenpreisBindingSource.EndEdit()
            Grartikel_kundenpreisTableAdapter.Update(DsPreise)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    'update  
    Private Sub PreiseDataGridView_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles PreiseDataGridView.CellEndEdit
        Try

            Dim curRow As dsPreise._grartikel_kundenpreisRow = GrartikelkundenpreisBindingSource.Current.row
            curRow.Datum = Now

            If Not IstKundeOderPreislisteAusgewaehlt() Then
                PreislisteComboBox.BackColor = Color.Yellow
                AdressenControl1.BackColor = Color.Yellow
                Exit Sub
            End If

            Dim editedRow = PreiseDataGridView.Rows(e.RowIndex)
            'editedRow.Cells(2).Value = Me.AdressenControl1.IDNR '"IDNR"
            curRow.IDNR = AdressenControl1.IDNR '"IDNR"

            If PreislisteComboBox.SelectedValue.ToString.Length > 0 Then
                curRow.PreislisteName = PreislisteComboBox.SelectedValue
            End If

            If curRow.IsStkAbNull Then
                curRow.StkAb = 1
            End If

            If IsNull(editedRow.Cells(0).Value) Then
                Try
                    If IsDBNull(curRow.Id) Then
                        curRow.Id = NextId("`grartikel-vkpreisperselection`", "Id")
                    End If
                Catch ex As Exception

                End Try

            End If

        Catch ex As Exception
            Label1.Text = ex.Message
        End Try
    End Sub

    Private Sub AdressenControl1_IDNRChanged(idnr As Integer) Handles AdressenControl1.IDNRChanged
        If IsNumeric(idnr) Then
            PreiseDataGridView.Enabled = idnr > 0
            FilterButton_Click(Nothing, Nothing)
        End If
    End Sub


    Private Sub PreislisteComboBox_Validated(sender As Object, e As EventArgs) Handles PreislisteComboBox.Validating
        AdressenControl1.IDNR = -1
        PreiseDataGridView.Enabled = PreislisteComboBox.SelectedValue.ToString.Length > 0
        FilterButton_Click(Nothing, Nothing)
    End Sub

End Class