Imports IntraSell_DLL
Imports IntraSell_Net_Base

Public Class ArtikelPreisregeln
    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")
            FillComboBoxInDG(ArtKatNrColumn, "SELECT artkatnr, name from `grArtikel-Kategorien` order by name asc", "Name", "ArtKatNr")
            FillComboBoxInDG(PreislisteNameColumn, SQL_PREISLISTEN, "PreislisteName")

            'Fill grid data 
            Try
                Grartikel_vkpreisperselectionTableAdapter.FillNeu(DsPreise._grartikel_vkpreisperselection)
            Catch ex As Exception
                HandleAppError(ex, Silent:=True)
            End Try

            FillComboBox(ArtKatNrComboBox, "SELECT artkatnr, name from `grArtikel-Kategorien` order by name asc", "Name", "ArtKatNr")
            FillComboBox(PreislisteComboBox, SQL_PREISLISTEN, "PreislisteName")

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

            ds = DsPreise
            ParentBindingNavigator.BindingSource = GrartikelvkpreisperselectionBindingSource

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

    End Sub

    'Filtern
    Private Sub FilterButton_Click(sender As Object, e As System.EventArgs) Handles FilterButton.Click
        Try
            Dim filter As String = "1=1 "

            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 AdressenControl1.IDNR > 0 Then
                filter += " and IDNR =  " & AdressenControl1.IDNR
            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

            If PreislisteComboBox.Text.Length > 0 Then
                filter = "PreislisteName Like '" + PreislisteComboBox.Text + "%'"
            End If

            GrartikelvkpreisperselectionBindingSource.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()
            GrartikelvkpreisperselectionBindingSource.EndEdit()
            Grartikel_vkpreisperselectionTableAdapter.Update(DsPreise)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub GrartikelvkpreisperselectionBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles GrartikelvkpreisperselectionBindingSource.AddingNew
        Try
            ' Dim row = GrartikelvkpreisperselectionBindingSource.Current
            ' Dim added As dsPreise._grartikel_vkpreisperselectionRow = row.Row
            ' added.Id = NextId("`grartikel-vkpreisperselection`", "Id")
            ' added.ArtNr = -1
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
End Class