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.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Data.Filtering

Public Class RegistryFinancialVC
    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()
        If View.Id = "RegistryHeader_DetailView" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)

            If _RegistryHeader IsNot Nothing Then
                If _RegistryHeader.RegistryType IsNot Nothing Then
                    If _RegistryHeader.RegistryType.RegistryMainType = eRegistryMainType.eAccount Then
                        If _RegistryHeader.IsEditable = False Then
                        Else
                            Select Case _RegistryHeader.CurrentWorkflowSystemSteps.StepFinancialType
                                Case eStepFinancialType.eCheckVAT, eStepFinancialType.eCheckVATPlusControlling
                                    'Frame.GetController(Of RegistryFinancialVC).aToTable_RRF.Active.SetItemValue("", False)
                                    'Frame.GetController(Of RegistryFinancialVC).aToTableD_RRF.Active.SetItemValue("", False)
                            End Select
                        End If
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub aToTable_RRF_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_RRF.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)
        Dim _RegistryRowsFinancial As RegistryRowsFinancial

        If _RegistryHeader IsNot Nothing Then
            _RegistryRowsFinancial = _ocur.CreateObject(Of RegistryRowsFinancial)()
            _RegistryRowsFinancial.RegistryHeader = _RegistryHeader
            _RegistryRowsFinancial.BruttoAmount = _RegistryHeader.row_VAT_BruttoAmount
            _RegistryRowsFinancial.VAT = _RegistryHeader.row_VAT
            _RegistryRowsFinancial.DateVAT = _RegistryHeader.F_DateVAT

            _ocur.CommitChanges()
            View.Refresh()

        End If

    End Sub

    Private Sub aToTableD_RRF_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_RRF.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)
        Dim _ListView_RRF As ListPropertyEditor
        If _RegistryHeader IsNot Nothing Then
            _ListView_RRF = TryCast(View, DetailView).FindItem("RegistryRowsFinancial")
            If _ListView_RRF IsNot Nothing Then

                _ocur.Delete(_ListView_RRF.ListView.SelectedObjects)
                _ocur.CommitChanges()

                View.Refresh()
            End If
        End If
    End Sub

    Private Sub aToTable_RRFC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_RRFC.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling

        If _RegistryHeader IsNot Nothing Then

            '//Banki csomag ellenőrzése
            If _RegistryHeader.F_PayMode = ePayMode.InTransfer Then
                If _RegistryHeader.RegistryBankTransfer.Count = 0 And _RegistryHeader.BruttoAmountRemainingBank = 0 Then
                    Dim _RegistryBankTransfer As RegistryBankTransfer = _ocur.CreateObject(Of RegistryBankTransfer)()
                    _RegistryBankTransfer.RegistryHeader = _RegistryHeader
                    _RegistryBankTransfer.DatePayment = _RegistryHeader.F_DatePayment
                    _RegistryBankTransfer.AmountDEV = _RegistryHeader.F_AmountBruttoDEV
                    _RegistryBankTransfer.AmountHUF = _RegistryHeader.F_AmountBruttoHUF
                End If
            End If


            _RegistryRowsFinancialControlling = _ocur.CreateObject(Of RegistryRowsFinancialControlling)()
            _RegistryRowsFinancialControlling.RegistryHeader = _RegistryHeader
            _RegistryRowsFinancialControlling.ContractRows = _ocur.GetObject(_RegistryHeader.row_RRFC_ContractRows)
            _RegistryRowsFinancialControlling.Amount = _RegistryHeader.row_RRFC_Amount
            _RegistryRowsFinancialControlling.Controlling = _RegistryHeader.row_RRFC_Controlling
            _RegistryRowsFinancialControlling.CostHolder = _RegistryHeader.row_RRFC_CostHolder
            _RegistryRowsFinancialControlling.CostPlace = _RegistryHeader.row_RRFC_CostPlace
            _RegistryRowsFinancialControlling.Note = _RegistryHeader.row_RRFC_Note
            _RegistryRowsFinancialControlling.JobNumberObjects = _RegistryHeader.JobNumberObjects
            _RegistryRowsFinancialControlling.UniqueObjects = _RegistryHeader.UniqueObjects

            _ocur.CommitChanges()

            View.Refresh()
            _RegistryHeader.row_RRFC_Amount = _RegistryHeader.NettoAmountRemaining

        End If

    End Sub

    Private Sub aToTableD_RRFC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_RRFC.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)
        Dim _ListView_RRF As ListPropertyEditor
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling
        Dim _ContractRows_ArrayList As New ArrayList

        If _RegistryHeader IsNot Nothing Then
            _ListView_RRF = TryCast(View, DetailView).FindItem("RegistryRowsFinancialControlling")
            If _ListView_RRF IsNot Nothing Then
                For Each _RegistryRowsFinancialControlling In _ListView_RRF.ListView.SelectedObjects
                    If _RegistryRowsFinancialControlling.ContractRows IsNot Nothing Then
                        If _RegistryRowsFinancialControlling.ContractRows.Contracts IsNot Nothing Then
                            If _RegistryRowsFinancialControlling.ContractRows.Contracts.ContractTemplate.IsTechnical Then
                                _ContractRows_ArrayList.Add(_RegistryRowsFinancialControlling.ContractRows)
                            End If
                        End If
                    End If
                Next
                _ocur.Delete(_ListView_RRF.ListView.SelectedObjects)
                _ocur.CommitChanges()

                View.Refresh()
            End If
        End If
    End Sub

    Private Sub aToTable_RRFC_With_TContract_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_RRFC_With_TContract.Execute
        '// Technikai szerződés sor + Táblázatba beadás !!!

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False) ' A validáció ideiglenes letiltása az akció végrehajtásának idejére.

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)
        Dim _TechnicalContractTemplate As ContractTemplate = _ocur.FindObject(Of ContractTemplate)(CriteriaOperator.Parse("CustomersFrom=? And IsTechnical=True", _RegistryHeader.CustomersFrom))

        Dim _Contracts As Contracts = _ocur.FindObject(Of Contracts)(CriteriaOperator.Parse("CustomersFrom=? AND Customers=? AND ContractTemplate=?", _RegistryHeader.CustomersFrom, _RegistryHeader.Customers, _TechnicalContractTemplate))
        Dim _ContractRows As ContractRows



        Dim _RegistryRowsFinancial As RegistryRowsFinancial
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling
        Dim _TechnicalLimitAmount As Double = _RegistryHeader.row_RRFC_Controlling.TechnicalContractLimit
        If _TechnicalLimitAmount = 0 Then _TechnicalLimitAmount = 200000

        If _RegistryHeader IsNot Nothing Then
            '//Banki csomag ellenőrzése
            If _RegistryHeader.F_PayMode = ePayMode.InTransfer Then
                If _RegistryHeader.RegistryBankTransfer.Count = 0 And _RegistryHeader.BruttoAmountRemainingBank = 0 Then
                    Dim _RegistryBankTransfer As RegistryBankTransfer = _ocur.CreateObject(Of RegistryBankTransfer)()
                    _RegistryBankTransfer.RegistryHeader = _RegistryHeader
                    _RegistryBankTransfer.DatePayment = _RegistryHeader.F_DatePayment
                    _RegistryBankTransfer.AmountDEV = _RegistryHeader.F_AmountBruttoDEV
                    _RegistryBankTransfer.AmountHUF = _RegistryHeader.F_AmountBruttoHUF
                    
                End If
            End If
        End If

        If _RegistryHeader.row_RRFC_Amount <= _TechnicalLimitAmount Then
            If _Contracts Is Nothing Then
                _Contracts = _ocur.CreateObject(Of Contracts)()
                _Contracts.CustomersFrom = _RegistryHeader.CustomersFrom
                _Contracts.Customers = _RegistryHeader.Customers
                _Contracts.ContractTemplate = _TechnicalContractTemplate
                _Contracts.DateCreated = _RegistryHeader.F_DateCreated
                _Contracts.DateExecution = _RegistryHeader.F_DateExecution
                _Contracts.DocumentNumber = _Contracts.ContractTemplate.NumberGenerator.GetNewNumber(_Contracts.ContractTemplate.NumberGenerator)
                _Contracts.State = State.Signed
            End If

            _ContractRows = _ocur.CreateObject(Of ContractRows)()
            _ContractRows.Contracts = _Contracts
            _ContractRows.CurrencyName = _RegistryHeader.F_CurrencyName
            _ContractRows.ContractRowsType = eContractRowsType.eFix
            _ContractRows.DateExecution = _RegistryHeader.F_DateExecution
            _ContractRows.Controlling = _RegistryHeader.row_RRFC_Controlling
            If _RegistryHeader.F_CurrencyName.ShortName = "HUF" Then
                _ContractRows.NettoPriceHUF = _RegistryHeader.row_RRFC_Amount
            Else
                If _RegistryHeader.F_CurrencyRate <> 0 Then
                    _ContractRows.NettoPriceDEV = _RegistryHeader.row_RRFC_Amount / _RegistryHeader.F_CurrencyRate
                Else
                    _ContractRows.NettoPriceDEV = 0
                End If
                _ContractRows.NettoPriceHUF = _RegistryHeader.row_RRFC_Amount
            End If
            _ContractRows.ShortName = _RegistryHeader.row_RRFC_Note
            _ContractRows.UniqueObjects = _RegistryHeader.UniqueObjects

            'IDE KELL VALAMI ÁFA !

            For Each _RegistryRowsFinancial In _RegistryHeader.RegistryRowsFinancial
                _ContractRows.VAT = _RegistryRowsFinancial.VAT
                Exit For
            Next

            _ContractRows.CostHolder = _RegistryHeader.row_RRFC_CostHolder
            _ContractRows.CostPlace = _RegistryHeader.row_RRFC_CostPlace
            _ContractRows.Controlling = _RegistryHeader.row_RRFC_Controlling
            _ContractRows.JobNumberObjects = _RegistryHeader.JobNumberObjects

            _RegistryRowsFinancialControlling = _ocur.CreateObject(Of RegistryRowsFinancialControlling)()
            _RegistryRowsFinancialControlling.RegistryHeader = _RegistryHeader
            _RegistryRowsFinancialControlling.ContractRows = _ContractRows
            _RegistryRowsFinancialControlling.Amount = _RegistryHeader.row_RRFC_Amount
            _RegistryRowsFinancialControlling.Controlling = _RegistryHeader.row_RRFC_Controlling
            _RegistryRowsFinancialControlling.CostHolder = _RegistryHeader.row_RRFC_CostHolder
            _RegistryRowsFinancialControlling.CostPlace = _RegistryHeader.row_RRFC_CostPlace
            _RegistryRowsFinancialControlling.Note = _RegistryHeader.row_RRFC_Note
            _RegistryRowsFinancialControlling.JobNumberObjects = _RegistryHeader.JobNumberObjects
            _RegistryRowsFinancialControlling.UniqueObjects = _RegistryHeader.UniqueObjects

            _ocur.CommitChanges()

            View.Refresh()

            _RegistryHeader.row_RRFC_Amount = _RegistryHeader.NettoAmountRemaining
        Else
            '-------!!!!!!!TESZT MIATT!!!!!------------------------------------------
            Dim _Message As String = String.Format("Technikai limit van beállítva ! {0}", _TechnicalLimitAmount)
            Throw New Exception(_Message)
        End If

    End Sub

    Private Sub aToTable_RRFC_With_RContract_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aToTable_RRFC_With_RContract.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)

        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(Contracts))

        _CS.BeginUpdateCriteria()
        _CS.Criteria.Add("Filter1", CriteriaOperator.Parse("CustomersFrom.Oid=? and Customers.Oid=? and ContractTemplate.PartnerType=1 and ContractTemplate.IsTechnical=False and (State = 1 OR State = 2)", _RegistryHeader.CustomersFrom.Oid, _RegistryHeader.Customers.Oid))
        _CS.EndUpdateCriteria()

        e.View = Application.CreateListView("Contracts_Finder_Payables", _CS, False)
    End Sub

    Private Sub aToTable_RRFC_With_RContract_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aToTable_RRFC_With_RContract.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling
        Dim _Contracts As Contracts = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), Contracts))
        Dim _ContractRows As ContractRows
        If _Contracts Is Nothing Then Exit Sub

        If _RegistryHeader IsNot Nothing And _Contracts IsNot Nothing Then

            _ContractRows = _ocur.CreateObject(Of ContractRows)()
            _ContractRows.Contracts = _Contracts
            _ContractRows.ContractRowsType = eContractRowsType.eFix
            _ContractRows.DateExecution = _RegistryHeader.F_DateExecution
            _ContractRows.Controlling = _RegistryHeader.row_RRFC_Controlling
            _ContractRows.CurrencyName = _RegistryHeader.F_CurrencyName
            _ContractRows.NettoPriceHUF = _RegistryHeader.row_RRFC_Amount
            If Not _ContractRows.CurrencyName.ShortName = "HUF" Then
                If _RegistryHeader.F_CurrencyRate <> 0 Then
                    _ContractRows.NettoPriceDEV = Math.Round(_RegistryHeader.row_RRFC_Amount / _RegistryHeader.F_CurrencyRate, 2, MidpointRounding.AwayFromZero)
                End If
            End If
            _ContractRows.ShortName = _RegistryHeader.row_RRFC_Note
            _ContractRows.UniqueObjects = _RegistryHeader.UniqueObjects
            If _RegistryHeader.RegistryRowsFinancial.Count > 0 Then
                _ContractRows.VAT = TryCast(_RegistryHeader.RegistryRowsFinancial(0), RegistryRowsFinancial).VAT
            End If
            _ContractRows.CostHolder = _RegistryHeader.row_RRFC_CostHolder
            _ContractRows.CostPlace = _RegistryHeader.row_RRFC_CostPlace
            _ContractRows.Controlling = _RegistryHeader.row_RRFC_Controlling
            _ContractRows.JobNumberObjects = _RegistryHeader.JobNumberObjects
            _ContractRows.QTT = 1
            _ContractRows.Units = _ocur.FindObject(Of Units)(CriteriaOperator.Parse("DefaultForBusinnes=True"))

            _RegistryRowsFinancialControlling = _ocur.CreateObject(Of RegistryRowsFinancialControlling)()
            _RegistryRowsFinancialControlling.RegistryHeader = _RegistryHeader
            _RegistryRowsFinancialControlling.ContractRows = _ContractRows
            _RegistryRowsFinancialControlling.Amount = _RegistryHeader.row_RRFC_Amount
            _RegistryRowsFinancialControlling.Controlling = _RegistryHeader.row_RRFC_Controlling
            _RegistryRowsFinancialControlling.CostHolder = _RegistryHeader.row_RRFC_CostHolder
            _RegistryRowsFinancialControlling.CostPlace = _RegistryHeader.row_RRFC_CostPlace
            _RegistryRowsFinancialControlling.Note = _RegistryHeader.row_RRFC_Note
            _RegistryRowsFinancialControlling.JobNumberObjects = _RegistryHeader.JobNumberObjects
            _RegistryRowsFinancialControlling.UniqueObjects = _RegistryHeader.UniqueObjects

            _ocur.CommitChanges()

        End If
        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub

    Private Sub aRegistryTaskGotoNextStepWithFC_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs)

    End Sub

    Private Sub aRegistryTaskGotoNextStepWithFC_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs)

    End Sub
End Class
