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 DevExpress.Xpo
Imports DevExpress.ExpressApp.SystemModule

Public Class PCIDetailVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

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

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of PCIDetailVC).aOpenObjectPCIDetail.Active.SetItemValue("", False)
        Frame.GetController(Of PCIDetailVC).aViewAttachmentsPCIDetail.Active.SetItemValue("", False)
        Frame.GetController(Of PCIDetailVC).aViewRegistryPCIDetail.Active.SetItemValue("", False)
        Frame.GetController(Of PCIDetailVC).aPCICheckDetail.Active.SetItemValue("", False)
        Frame.GetController(Of PCIDetailVC).aChangeCurrencyRatePCIDetail.Active.SetItemValue("", False)

        If View.Id = "PCIDetail_ListView" Then
            Frame.GetController(Of PCIDetailVC).aOpenObjectPCIDetail.Active.SetItemValue("", True)
            Frame.GetController(Of PCIDetailVC).aViewAttachmentsPCIDetail.Active.SetItemValue("", True)
            Frame.GetController(Of PCIDetailVC).aViewRegistryPCIDetail.Active.SetItemValue("", True)
            Frame.GetController(Of PCIDetailVC).aPCICheckDetail.Active.SetItemValue("", True)
            Frame.GetController(Of PCIDetailVC).aChangeCurrencyRatePCIDetail.Active.SetItemValue("", True)
            Frame.GetController(Of RefreshController).RefreshAction.Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aViewAttachmentsPCIDetail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewAttachmentsPCIDetail.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCIDetail As PCIDetail = TryCast(View.SelectedObjects(0), PCIDetail)

        If _PCIDetail IsNot Nothing Then
            ViewAttachments.ViewAttachments(_ocur, Frame, _PCIDetail.CustomersFrom, _
                                             _PCIDetail.Customers, _PCIDetail.ConnectInfo, _
                                             "")
        End If
    End Sub

    Private Sub aViewRegistryPCIDetail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewRegistryPCIDetail.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCIDetail As PCIDetail = TryCast(View.SelectedObjects(0), PCIDetail)

        If _PCIDetail IsNot Nothing Then
            ViewRegistry.ViewRegistry(_ocur, Application, e, _PCIDetail.CustomersFrom, _
                                      _PCIDetail.Customers, _PCIDetail.ConnectInfo)
        End If
    End Sub

    Private Sub aOpenObjectPCIDetail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenObjectPCIDetail.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _PCIDetail As PCIDetail = TryCast(View.SelectedObjects(0), PCIDetail)
        Dim _GLRows As GLRows


        If _PCIDetail IsNot Nothing Then
            _GLRows = _PCIDetail.GLRows
            If _GLRows IsNot Nothing Then
                If TryCast(_GLRows.GLHeader, GLAccounts) IsNot Nothing Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "GLAccounts_DetailView", True, _onew.GetObject(TryCast(_GLRows.GLHeader, GLAccounts)))
                End If
                If TryCast(_GLRows.GLHeader, GLCassa) IsNot Nothing Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "GLCassa_DetailView", True, _onew.GetObject(TryCast(_GLRows.GLHeader, GLCassa)))
                End If
                If TryCast(_GLRows.GLHeader, GLBank) IsNot Nothing Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "GLBank_DetailView", True, _onew.GetObject(TryCast(_GLRows.GLHeader, GLBank)))
                End If
                If TryCast(_GLRows.GLHeader, GLCompensation) IsNot Nothing Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "GLCompensation_DetailView", True, _onew.GetObject(TryCast(_GLRows.GLHeader, GLCompensation)))
                End If
                If TryCast(_GLRows.GLHeader, GLMixed) IsNot Nothing Then
                    e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "GLMixed_DetailView", True, _onew.GetObject(TryCast(_GLRows.GLHeader, GLMixed)))
                End If
            End If
        End If
    End Sub

    Private Sub aPCICheckDetail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aPCICheckDetail.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _PCIDetail As PCIDetail = TryCast(View.SelectedObjects(0), PCIDetail)

        If _PCIDetail IsNot Nothing Then
            GLFunctions.ReconfigurePCI(_uow, _PCIDetail.CustomersFrom, _PCIDetail.Customers, _PCIDetail.PartnerType, _PCIDetail.ConnectInfo)
            If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then _
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub

    Private Sub aChangeCurrencyRatePCIDetail_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aChangeCurrencyRatePCIDetail.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCIDetail As PCIDetail = TryCast(View.SelectedObjects(0), PCIDetail)
        Dim _CurrencyRatePopup As CurrencyRatePopup = _onew.CreateObject(Of CurrencyRatePopup)()
        If _PCIDetail IsNot Nothing Then
            If _PCIDetail.GLRows IsNot Nothing Then
                _CurrencyRatePopup.CurrencyRate = _PCIDetail.GLRows.CurrencyRate2
            End If
        End If
        e.View = Application.CreateDetailView(_onew, "CurrencyRatePopup_DetailView", False, _CurrencyRatePopup)
    End Sub

    Private Sub aChangeCurrencyRatePCIDetail_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aChangeCurrencyRatePCIDetail.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _CurrencyRatePopup As CurrencyRatePopup = e.PopupWindow.View.SelectedObjects(0)
        Dim _PCIDetail As PCIDetail = TryCast(View.SelectedObjects(0), PCIDetail)
        Dim _GLRows As GLRows
        Dim _CurrencyRate As Double = _CurrencyRatePopup.CurrencyRate
        If _PCIDetail IsNot Nothing Then
            If _PCIDetail.GLRows IsNot Nothing Then
                _GLRows = _PCIDetail.GLRows
                _GLRows.InAmountDEV2 = Math.Round(_GLRows.InAmountDEV * _CurrencyRate, 2, MidpointRounding.AwayFromZero)
                _GLRows.OutAmountDEV2 = Math.Round(_GLRows.OutAmountDEV * _CurrencyRate, 2, MidpointRounding.AwayFromZero)
                _GLRows.CurrencyRate2 = _CurrencyRate
                _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
