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

Public Class RegistryTelephoneVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

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

    End Sub

    Private Sub aControllRegistryTelephone_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aControllRegistryTelephone.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(RegistryTelephone))
        Dim _RegistryTelephone As RegistryTelephone
        Dim _Phones_Collection As New XPCollection(Of Phones)(_onew.Session, CriteriaOperator.Parse(""))
        Dim _RegistryHeader As RegistryHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), RegistryHeader))
        Dim _Phones As Phones
        Dim _RegistryTelephoneParameters As RegistryTelephoneParameters = _onew.FindObject(Of RegistryTelephoneParameters)(CriteriaOperator.Parse("CustomersFrom.Oid=?", _RegistryHeader.CustomersFrom.Oid))

        For Each _Phones In _Phones_Collection
            _RegistryTelephone = _onew.FindObject(Of RegistryTelephone)(CriteriaOperator.Parse("RegistryHeader=? and UniqueObjects=?", _RegistryHeader, _Phones.UniqueObjects))
            If _RegistryTelephone Is Nothing Then
                _RegistryTelephone = _onew.CreateObject(Of RegistryTelephone)()
            End If
            _RegistryTelephone.UniqueObjects = _Phones.UniqueObjects
            _RegistryTelephone.RegistryTelephoneParameters = _RegistryTelephoneParameters
            _RegistryTelephone.RegistryHeader = _RegistryHeader
            _CollectionSource.Add(_RegistryTelephone)
        Next
        _onew.CommitChanges()   'ezt át kell nézni!!!!!!

        _CollectionSource.BeginUpdateCriteria()
        _CollectionSource.Criteria("ByTelephone") = CriteriaOperator.Parse("RegistryHeader.Oid=?", _RegistryHeader.Oid) 'PersistentCriteriaEvaluationBehavior.InTransaction
        _CollectionSource.EndUpdateCriteria()

        e.View = Application.CreateListView("RegistryTelephone_ListView", _CollectionSource, False)
    End Sub

    Private Sub aControllRegistryTelephone_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aControllRegistryTelephone.Execute
        '//Telefonszámla rögzítése
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _RegistryHeader As RegistryHeader = TryCast(View.SelectedObjects(0), RegistryHeader)
        Dim _RegistryTelephone As RegistryTelephone

        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 _ListView As ListView = TryCast(e.PopupWindow.View, ListView)

        '// Ellenőrzések
        'e.PopupWindow.View.ObjectSpace.CommitChanges()

        If _TechnicalContractTemplate Is Nothing Then
            '// Nincs technikai szerződési sablon
            MsgBox(String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions\RegistryTelephone", "NoTechnicalContractTemplate")), _
                      MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, _
                      String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
            Exit Sub
        End If

        '// Ellenőrzés, hogy a total bruttó le lett-e bontva
        If _ListView IsNot Nothing Then
            Dim _BruttoAmountRemaining As Double = 0
            For Each _RegistryTelephone In _ListView.CollectionSource.List
                _BruttoAmountRemaining += _RegistryTelephone.Amount_Brutto_INTERNET + _RegistryTelephone.Amount_Brutto_OTHER_DEFAULTVAT + _RegistryTelephone.Amount_Brutto_OTHER_NOVAT + _RegistryTelephone.Amount_Brutto_PARKING + _RegistryTelephone.Amount_Brutto_TELEPHON
            Next

            If _BruttoAmountRemaining <> _RegistryHeader.F_AmountBruttoHUF Then
                '// Hiba, hogy neincs minden sor lebontva
                MsgBox(String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions\RegistryTelephone", "NotEqualTotalwithDivided")), _
                       MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, _
                       String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
                Exit Sub
            End If
        End If


        '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        '//Technikai szerződés létrehozása
        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

        If _ListView IsNot Nothing Then
            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)

            _ocur.Delete(_RegistryHeader.RegistryRowsFinancial)
            _ocur.Delete(_RegistryHeader.RegistryRowsFinancialControlling)
            For Each _RegistryTelephone In _ListView.CollectionSource.List
                ''//Először az ÁFÁ-s tételek rögzítése
                If _RegistryTelephone.Amount_Brutto_INTERNET > 0 Then
                    Create_RRF_Row(_ocur, _RegistryHeader, _RegistryTelephone.Amount_Brutto_INTERNET, _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT_INTERNET))
                    Create_RRFC_Row(_ocur, _Contracts, _RegistryHeader, _RegistryTelephone.Amount_Netto_INTERNET, _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_INTERNET),
                                    _ocur.GetObject(_RegistryTelephone.UniqueObjects), _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT_INTERNET))
                End If
                If _RegistryTelephone.Amount_Brutto_OTHER_DEFAULTVAT > 0 Then
                    Create_RRF_Row(_ocur, _RegistryHeader, _RegistryTelephone.Amount_Brutto_OTHER_DEFAULTVAT, _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT))
                    Create_RRFC_Row(_ocur, _Contracts, _RegistryHeader, _RegistryTelephone.Amount_Netto_OTHER_DEFAULTVAT, _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_OTHER_DEFAULTVAT), _
                                   _ocur.GetObject(_RegistryTelephone.UniqueObjects), _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT))
                End If
                If _RegistryTelephone.Amount_Brutto_OTHER_NOVAT > 0 Then
                    Create_RRF_Row(_ocur, _RegistryHeader, _RegistryTelephone.Amount_Brutto_OTHER_NOVAT, _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_NOVAT))
                    Create_RRFC_Row(_ocur, _Contracts, _RegistryHeader, _RegistryTelephone.Amount_Brutto_OTHER_NOVAT, _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_OTHER_NOVAT), _
                                   _ocur.GetObject(_RegistryTelephone.UniqueObjects), _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_NOVAT))
                End If
                If _RegistryTelephone.Amount_Brutto_PARKING > 0 Then
                    Create_RRF_Row(_ocur, _RegistryHeader, _RegistryTelephone.Amount_Brutto_PARKING, _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_NOVAT))
                    Create_RRFC_Row(_ocur, _Contracts, _RegistryHeader, _RegistryTelephone.Amount_Brutto_PARKING, _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_PARKING), _
                                   _ocur.GetObject(_RegistryTelephone.UniqueObjects), _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_NOVAT))
                End If
                If _RegistryTelephone.Amount_Brutto_TELEPHON > 0 Then
                    Create_RRF_Row(_ocur, _RegistryHeader, Math.Round(_RegistryTelephone.Amount_Brutto_TELEPHON * 0.7, 0, MidpointRounding.AwayFromZero), _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT))
                    Create_RRF_Row(_ocur, _RegistryHeader, _RegistryTelephone.Amount_Brutto_TELEPHON - Math.Round(_RegistryTelephone.Amount_Brutto_TELEPHON * 0.7, 0, MidpointRounding.AwayFromZero), _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_NOVAT))
                    Create_RRFC_Row(_ocur, _Contracts, _RegistryHeader, _RegistryTelephone.AmountNetto_TELEPHON_80, _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_TELEPHON_80), _
                                   _ocur.GetObject(_RegistryTelephone.UniqueObjects), _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT))
                    Create_RRFC_Row(_ocur, _Contracts, _RegistryHeader, _RegistryTelephone.AmountNetto_TELEPHON_20, _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.Controlling_TELEPHON_20), _
                                   _ocur.GetObject(_RegistryTelephone.UniqueObjects), _ocur.GetObject(_RegistryTelephone.RegistryTelephoneParameters.VAT_DEFAULTVAT))
                End If
            Next
        End If

        _ocur.CommitChanges()

        '// Kerekítés rendberakása -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
        If _RegistryHeader.BruttoAmountRemaining = 0 And Math.Abs(_RegistryHeader.NettoAmountRemaining) < 5 Then
            'Beep()
            Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling
            _RegistryHeader.RegistryRowsFinancialControlling.Sorting.Add(New SortProperty("Amount", DB.SortingDirection.Descending))
            For Each _RegistryRowsFinancialControlling In _RegistryHeader.RegistryRowsFinancialControlling
                If _RegistryRowsFinancialControlling.Controlling.Oid.ToString = _RegistryTelephone.RegistryTelephoneParameters.Controlling_TELEPHON_80.Oid.ToString Then
                    'Beep()
                    If _RegistryHeader.NettoAmountRemaining > 0 Then
                        _RegistryRowsFinancialControlling.Amount += _RegistryHeader.NettoAmountRemaining
                    Else
                        _RegistryRowsFinancialControlling.Amount -= _RegistryHeader.NettoAmountRemaining
                    End If
                    _ocur.CommitChanges()
                    Exit For
                End If
            Next
        End If

        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
    End Sub
    Private Sub Create_RRF_Row(_ocur As Xpo.XPObjectSpace, _RegistryHeader As RegistryHeader, _BruttoAmount As Double, _VAT As VAT)
        Dim _RegistryRowsFinancial As RegistryRowsFinancial = _ocur.FindObject(Of RegistryRowsFinancial)(CriteriaOperator.Parse("RegistryHeader=? and VAT=?", _RegistryHeader, _VAT))

        If _RegistryRowsFinancial Is Nothing Then
            _RegistryRowsFinancial = _ocur.CreateObject(Of RegistryRowsFinancial)()
            _RegistryRowsFinancial.RegistryHeader = _RegistryHeader
            _RegistryRowsFinancial.BruttoAmount = _BruttoAmount
            _RegistryRowsFinancial.VAT = _VAT
            _RegistryRowsFinancial.DateVAT = _RegistryHeader.F_DateVAT
        Else
            _RegistryRowsFinancial.BruttoAmount += _BruttoAmount
        End If
    End Sub
    Private Sub Create_RRFC_Row(_ocur As Xpo.XPObjectSpace, _Contracts As Contracts, _RegistryHeader As RegistryHeader, _
                                _Amount As Double, _Controlling As Controlling, _UniqueObjects As UniqueObjects, _VAT As VAT)
        Dim _ContractRows As ContractRows
        Dim _RegistryRowsFinancialControlling As RegistryRowsFinancialControlling

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

        'IDE KELL VALAMI ÁFA !

        _ContractRows.VAT = _VAT
        _ContractRows.Controlling = _Controlling
        _ContractRows.JobNumberObjects = _RegistryHeader.JobNumberObjects

        _RegistryRowsFinancialControlling = _ocur.CreateObject(Of RegistryRowsFinancialControlling)()
        _RegistryRowsFinancialControlling.RegistryHeader = _RegistryHeader
        _RegistryRowsFinancialControlling.ContractRows = _ContractRows
        _RegistryRowsFinancialControlling.Amount = _Amount
        _RegistryRowsFinancialControlling.Controlling = _Controlling
        _RegistryRowsFinancialControlling.UniqueObjects = _UniqueObjects
    End Sub
End Class
