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.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.Data.Filtering

Public Class RateDiffVC
    Inherits DevExpress.ExpressApp.ViewController
    Protected selection As ArrayList
    Protected noselect As Boolean = False
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork
    Public Sub New()
        MyBase.New()
        selection = New ArrayList()
        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

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

        Frame.GetController(Of RateDiffVC).aCreateRateDiffView.Active.SetItemValue("", False)
        Frame.GetController(Of RateDiffVC).aViewPDCI_RateDiff.Active.SetItemValue("", False)
        Frame.GetController(Of RateDiffVC).aOpenObject_RateDiff.Active.SetItemValue("", False)
        Frame.GetController(Of RateDiffVC).aViewAttachments_RateDiff.Active.SetItemValue("", False)
        Frame.GetController(Of RateDiffVC).aViewRegistry_RateDiff.Active.SetItemValue("", False)

        If View.Id = "RateDiffView_ListView" Then
            AddHandler View.SelectionChanged, AddressOf view_SelectionChanged
            Frame.GetController(Of RateDiffVC).aCreateRateDiffView.Active.SetItemValue("", True)
            Frame.GetController(Of RateDiffVC).aViewPDCI_RateDiff.Active.SetItemValue("", True)
            Frame.GetController(Of RateDiffVC).aOpenObject_RateDiff.Active.SetItemValue("", True)
            Frame.GetController(Of RateDiffVC).aViewAttachments_RateDiff.Active.SetItemValue("", True)
            Frame.GetController(Of RateDiffVC).aViewRegistry_RateDiff.Active.SetItemValue("", True)


            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If

        
    End Sub

    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        If View.Id = "RateDiffView_ListView" Then
            RemoveHandler View.SelectionChanged, AddressOf view_SelectionChanged
            Frame.GetController(Of RateDiffVC).aCreateRateDiffView.Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
        End If

    End Sub
    Private Sub view_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _RateDiffView As RateDiffView

        If View Is Nothing Then Exit Sub

        If View.Id Like "RateDiffView_ListView*" And noselect = False Then
            selection.Clear()
            For Each _RateDiffView In View.SelectedObjects
                selection.Add(_RateDiffView)
            Next
        End If

    End Sub
    Private Sub aCreateRateDiffView_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateRateDiffView.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _RateDiffPopUp As RateDiffPopUp = _onew.CreateObject(Of RateDiffPopUp)()

        _RateDiffPopUp.DateExecution = Now

        e.View = Application.CreateDetailView(_onew, "RateDiffPopUp_DetailView", False, _RateDiffPopUp)
    End Sub

    Private Sub aCreateRateDiffView_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateRateDiffView.Execute

        '// Megcsinálja az árfolyam elszámoló vegyes bizonylatot

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RateDiffPopUp As RateDiffPopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), RateDiffPopUp)
        Dim _RateDiffView As RateDiffView
        Dim _GLHeader_Ratediff As GLMixed = Nothing
        Dim _GLRows_RateDiff As GLRows
        Dim _DebitChartOfAccounts As ChartOfAccounts = Nothing
        Dim _CreditChartOfAccounts As ChartOfAccounts = Nothing
        Dim _Controlling As Controlling = Nothing
        Dim _GLMixedNG As GLMixedNG
        Dim _GLRows As GLRows

        Dim _GLHeader_RateDiffParameter = _ocur.FindObject(Of GLHeader_RateDiffParameter)(CriteriaOperator.Parse("1=1"))
        If _GLHeader_RateDiffParameter Is Nothing Then Exit Sub

        Dim _ChartOfAccounts_RateDiff_Win As ChartOfAccounts = _GLHeader_RateDiffParameter.GetChartOfAccounts(_RateDiffPopUp.DateExecution.Year, eWinLoss.eRateDiffWin)
        Dim _ChartOfAccounts_RateDiff_Loss As ChartOfAccounts = _GLHeader_RateDiffParameter.GetChartOfAccounts(_RateDiffPopUp.DateExecution.Year, eWinLoss.eRateDiffLoss)
        Dim _Controlling_RateDiff_Win As Controlling = _GLHeader_RateDiffParameter.Controlling_RateDiff_Win
        Dim _Controlling_RateDiff_Loss As Controlling = _GLHeader_RateDiffParameter.Controlling_RateDiff_Loss

        Dim _CustomersFrom As CustomersFrom = Nothing
        Dim i As Long = 0
        Dim _InAmountHUF As Double = 0
        Dim _OutAmountHUF As Double = 0

        If selection.Count <= 0 Then Exit Sub

        For Each _RateDiffView In selection
            If i = 0 Then
                _CustomersFrom = _RateDiffView.CustomersFrom
                i = 1
            End If
            If _RateDiffView.CustomersFrom.Oid <> _CustomersFrom.Oid Then
                Throw New Exception("*Csak azonos saját cégű sorok jelölhető ki egyszerre !")
                Exit For
            End If
        Next

        _GLHeader_Ratediff = _ocur.CreateObject(Of GLMixed)()
        _GLHeader_Ratediff.CustomersFrom = _CustomersFrom
        _GLMixedNG = _ocur.FindObject(Of GLMixedNG)(CriteriaOperator.Parse("CustomersFrom=?", _GLHeader_Ratediff.CustomersFrom), True)
        If _GLMixedNG IsNot Nothing Then
            If _GLHeader_Ratediff.DocumentNumber = "" Then
                _GLHeader_Ratediff.DocumentNumber = _GLMixedNG.NumberGenerator.GetNewNumber(_GLMixedNG.NumberGenerator_RateDiff)
            End If
        End If
        _GLHeader_Ratediff.DateExecution = _RateDiffPopUp.DateExecution
        _GLHeader_Ratediff.DateCreated = _RateDiffPopUp.DateExecution
        _GLHeader_Ratediff.NoteHeader = _RateDiffPopUp.Description
        _GLHeader_Ratediff.CurrencyRate = 1

        _GLHeader_Ratediff.GLDocumentType = eGLDocumentType.eGLRateDiff
        _GLHeader_Ratediff.IsEditable = False
        _GLHeader_Ratediff.IsStorno = False
        _GLHeader_Ratediff.CurrencyName = _ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))

        For Each _RateDiffView In selection
            Select Case _RateDiffView.PartnerType
                Case ePartnerType.ePayabels
                    If _RateDiffView.RateDiffState = 1 Then
                        _DebitChartOfAccounts = _RateDiffView.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Ratediff.DateExecution.Year, eVATMode.eIn)
                        _CreditChartOfAccounts = _ChartOfAccounts_RateDiff_Win
                        _Controlling = _Controlling_RateDiff_Win
                        _InAmountHUF = Math.Abs(_RateDiffView.OutAmountHUF_Diff)
                    End If
                    If _RateDiffView.RateDiffState = -1 Then
                        _DebitChartOfAccounts = _ChartOfAccounts_RateDiff_Loss
                        _CreditChartOfAccounts = _RateDiffView.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Ratediff.DateExecution.Year, eVATMode.eIn)
                        _Controlling = _Controlling_RateDiff_Loss
                        _OutAmountHUF = Math.Abs(_RateDiffView.OutAmountHUF_Diff)
                    End If

                Case ePartnerType.eReceivables
                    If _RateDiffView.RateDiffState = -1 Then
                        _DebitChartOfAccounts = _ChartOfAccounts_RateDiff_Loss
                        _CreditChartOfAccounts = _RateDiffView.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Ratediff.DateExecution.Year, eVATMode.eOut)
                        _Controlling = _Controlling_RateDiff_Loss
                        _OutAmountHUF = Math.Abs(_RateDiffView.InAmountHUF_Diff)
                    End If
                    If _RateDiffView.RateDiffState = 1 Then
                        _DebitChartOfAccounts = _RateDiffView.Customers.CustomersGLParameters.GetChartOfAccounts(_GLHeader_Ratediff.DateExecution.Year, eVATMode.eOut)
                        _CreditChartOfAccounts = _ChartOfAccounts_RateDiff_Win
                        _Controlling = _Controlling_RateDiff_Win
                        _InAmountHUF = Math.Abs(_RateDiffView.InAmountHUF_Diff)
                    End If
            End Select
            _GLRows = _ocur.FindObject(Of GLRows)(CriteriaOperator.Parse("Oid=?", _RateDiffView.Oid))


            _GLRows_RateDiff = _ocur.CreateObject(Of GLRows)()
            _GLRows.GLRowsRateDiff = _GLRows_RateDiff
            _GLRows_RateDiff.GLRowsRateDiff = _GLRows
            _GLRows_RateDiff.GLHeader = _GLHeader_Ratediff
            _GLRows_RateDiff.InAmountHUF = _InAmountHUF
            _GLRows_RateDiff.OutAmountHUF = _OutAmountHUF
            _GLRows_RateDiff.AddGLRows(_GLHeader_Ratediff, _DebitChartOfAccounts, _CreditChartOfAccounts, 0, Math.Abs(_RateDiffView.InAmountHUF_Diff - _RateDiffView.OutAmountHUF_Diff), Nothing, _
                                        _RateDiffView.Customers, Nothing, Nothing, "", Nothing, _Controlling, Nothing, Nothing, Nothing, _RateDiffView.ConnectInfo, _
                                         eGLRowsTypeVAT.eNettoRow, Guid.NewGuid, eTransactionType.eNotSet, eCashType.ePartner, _RateDiffView.PartnerType)
        Next

        _ocur.CommitChanges()

        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
    End Sub

    Private Sub SimpleAction1_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewPDCI_RateDiff.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RateDiffView As RateDiffView = TryCast(View.SelectedObjects(0), RateDiffView)

        ViewPDCI.ViewPDCIDetail(_onew, Application, e, _RateDiffView.CustomersFrom, _
                          _RateDiffView.Customers, _RateDiffView.PartnerType, _
                          _RateDiffView.ConnectInfo)
    End Sub

    Private Sub aOpenObject_RateDiff_Execute_1(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenObject_RateDiff.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _RateDiffView As RateDiffView = TryCast(View.SelectedObjects(0), RateDiffView)
        Dim _GLCassa As GLCassa
        Dim _GLBank As GLBank
        Dim _GLCompensation As GLCompensation
        Dim _DetailView As DetailView
        Dim _GLRows As GLRows

        _GLRows = _ocur.FindObject(Of GLRows)(CriteriaOperator.Parse("Oid=?", _RateDiffView.Oid), True)
        _GLCassa = _ocur.FindObject(Of GLCassa)(CriteriaOperator.Parse("Oid=?", _GLRows.GLHeader.Oid), True)
        If _GLCassa IsNot Nothing Then
            _DetailView = Application.CreateDetailView(_onew, _onew.GetObject(_GLCassa), True)
            e.ShowViewParameters.CreatedView = _DetailView
        Else
            _GLBank = _ocur.FindObject(Of GLBank)(CriteriaOperator.Parse("Oid=?", _GLRows.GLHeader.Oid), True)
            If _GLBank IsNot Nothing Then
                _DetailView = Application.CreateDetailView(_onew, _onew.GetObject(_GLBank), True)
                e.ShowViewParameters.CreatedView = _DetailView
            Else
                _GLCompensation = _ocur.FindObject(Of GLCompensation)(CriteriaOperator.Parse("Oid=?", _GLRows.GLHeader.Oid), True)
                If _GLBank IsNot Nothing Then
                    _DetailView = Application.CreateDetailView(_onew, _onew.GetObject(_GLCompensation), True)
                    e.ShowViewParameters.CreatedView = _DetailView
                End If
            End If
        End If
    End Sub

    Private Sub aViewRegistry_RateDiff_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewRegistry_RateDiff.Execute
        Dim _RateDiffView As RateDiffView = TryCast(View.SelectedObjects(0), RateDiffView)
        Dim _CustomersFrom As CustomersFrom = _RateDiffView.CustomersFrom
        Dim _Customers As Customers = _RateDiffView.Customers
        Dim _DocumentNumber As String = _RateDiffView.ConnectInfo

        ViewRegistry.ViewRegistry(View.ObjectSpace, Application, e, _CustomersFrom, _Customers, _DocumentNumber)
    End Sub

    Private Sub aViewAttachments_RateDiff_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewAttachments_RateDiff.Execute
        '-- Karton sorához tartozó eredeti dokumentum megnyitása
        Dim _RateDiffView As RateDiffView = TryCast(View.SelectedObjects(0), RateDiffView)
        Dim _CustomersFrom As CustomersFrom = _RateDiffView.CustomersFrom
        Dim _Customers As Customers = _RateDiffView.Customers
        Dim _DocumentNumber As String = _RateDiffView.ConnectInfo

        ViewAttachments.ViewAttachments(View.ObjectSpace, Frame, _CustomersFrom, _Customers, _DocumentNumber, "")
    End Sub
End Class
