Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports SISBusiness.Module
Imports DevExpress.ExpressApp.Win.Editors
Imports DevExpress.Xpo
Imports DevExpress.XtraGrid.Columns

Public Class GLRowsVC
    Inherits SISBusiness.Module.GLRowsVC
    Dim _WaitFormClass As New WaitFormClass
    Private _GridListEditor As GridListEditor
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        AddHandler DoWork, AddressOf _WaitFormClass.DoWork
        AddHandler InitWork, AddressOf _WaitFormClass.Initwork
        AddHandler FinalWork, AddressOf _WaitFormClass.FinalWork
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        RemoveHandler DoWork, AddressOf _WaitFormClass.DoWork
        RemoveHandler InitWork, AddressOf _WaitFormClass.Initwork
        RemoveHandler FinalWork, AddressOf _WaitFormClass.FinalWork

        If _GridListEditor IsNot Nothing Then
            If _GridListEditor.GridView IsNot Nothing Then
                RemoveHandler _GridListEditor.GridView.CellValueChanged, AddressOf GLRows_ListView_Bank_CellValueChanged
            End If
        End If
    End Sub


    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()

        If View.Id = "GLRows_ListView_Bank" Then
            _GridListEditor = TryCast(TryCast(View, ListView).Editor, GridListEditor)

            If _GridListEditor IsNot Nothing Then
                AddHandler _GridListEditor.GridView.CellValueChanged, AddressOf GLRows_ListView_Bank_CellValueChanged
            End If

            'If _GridListEditor IsNot Nothing Then
            '    _GridListEditor.AllowEdit = True
            '    For i As Integer = 0 To _GridListEditor.Columns.Count - 1
            '        If _GridListEditor.GridView.Columns(i).FieldName <> "CurrencyRate2" Then
            '            _GridListEditor.GridView.Columns(i).OptionsColumn.ReadOnly = True
            '            _GridListEditor.GridView.Columns(i).OptionsColumn.AllowEdit = False
            '            _GridListEditor.GridView.Columns(i).OptionsColumn.TabStop = False
            '        End If
            '    Next i
            'End If
        End If
    End Sub
    Private Sub GLRows_ListView_Bank_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        If TryCast(e.Column, GridColumn).FieldName = "CurrencyRate2" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
            Dim _GLRows As GLRows = View.SelectedObjects(0)
            If _GLRows IsNot Nothing Then
                _GLRows.InAmountDEV2 = Math.Round(_GLRows.InAmountDEV * e.Value, 2, MidpointRounding.AwayFromZero)
                _GLRows.OutAmountDEV2 = Math.Round(_GLRows.OutAmountDEV * e.Value, 2, MidpointRounding.AwayFromZero)
                _GLRows.CurrencyRate2 = e.Value
                _ocur.CommitChanges()

                If _GLRows.GLHeader IsNot Nothing Then
                    GLFunctions.ReconfigurePCI(_uow, _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
                End If
            End If
        End If
    End Sub

End Class
