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

Public Class CRMEstateStatisticsVC
    Inherits DevExpress.ExpressApp.ViewController
    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()

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

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

        If View.Id = "ContractRows_ListView_Apartments" Or _
           View.Id = "ContractRows_ListView_Offices" Or _
           View.Id = "ContractRows_ListView_Garages" Or _
           View.Id = "ContractRows_ListView_Containers" Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _ListView As ListView = TryCast(View, ListView)
            Dim _Controlling_Collections As New XPCollection(Of Controlling)(_ocur.Session, CriteriaOperator.Parse("IsEstateStatistics=True"))
            _ListView.CollectionSource.BeginUpdateCriteria()
            _ListView.CollectionSource.Criteria("") = New InOperator("Controlling", _Controlling_Collections)
            _ListView.CollectionSource.EndUpdateCriteria()

        End If
    End Sub

    Private Sub aCRMEstateStatisticsCreate_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aCRMEstateStatisticsCreate.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CRMEstateStatistics As CRMEstateStatistics
        Dim _Immovables As Immovables

        '// Lakások
        Dim _Apartments_Collection As New XPCollection(Of Apartments)(_onew.Session)
        Dim _Apartments As Apartments


        RaiseEvent InitWork(1, 1, _Apartments_Collection.Count)
        For Each _Apartments In _Apartments_Collection
            RaiseEvent DoWork()
            _Immovables = TryCast(_Apartments, Immovables)

            _CRMEstateStatistics = _onew.FindObject(Of CRMEstateStatistics)(CriteriaOperator.Parse("Immovables=?", _Immovables))
            If _CRMEstateStatistics Is Nothing Then
                _CRMEstateStatistics = _onew.CreateObject(Of CRMEstateStatistics)()
            End If
            _CRMEstateStatistics.QTT_Sold = 1
            _CRMEstateStatistics.NettoPriceHUF = GetNettoPriceHUF(_onew, _Immovables.UniqueObjects)
            If _CRMEstateStatistics.NettoPriceHUF = 0 Then
                _CRMEstateStatistics.NettoPriceHUF = _Apartments.PriceNettoHUF
            Else
                If _CRMEstateStatistics.Immovables.ImmovableSoldState <> eImmovableSoldState.eSold Then
                    _CRMEstateStatistics.Immovables.ImmovableSoldState = eImmovableSoldState.eSold
                End If
            End If
            _CRMEstateStatistics.Immovables = _Immovables
            _CRMEstateStatistics.AreaNettoForSale_Sold = _Apartments.AreaNettoForSale


            _onew.CommitChanges()

        Next
        _onew.CommitChanges()
        RaiseEvent FinalWork

        '// Garázsok
        Dim _Garages_Collection As New XPCollection(Of Garages)(_onew.Session)
        Dim _Garages As Garages


        RaiseEvent InitWork(1, 1, _Garages_Collection.Count)
        For Each _Garages In _Garages_Collection
            RaiseEvent DoWork()
            _Immovables = TryCast(_Garages, Immovables)

            _CRMEstateStatistics = _onew.FindObject(Of CRMEstateStatistics)(CriteriaOperator.Parse("Immovables=?", _Immovables))
            If _CRMEstateStatistics Is Nothing Then
                _CRMEstateStatistics = _onew.CreateObject(Of CRMEstateStatistics)()
            End If
            _CRMEstateStatistics.QTT_Sold = 1
            _CRMEstateStatistics.NettoPriceHUF = GetNettoPriceHUF(_onew, _Immovables.UniqueObjects)
            If _CRMEstateStatistics.NettoPriceHUF = 0 Then
                _CRMEstateStatistics.NettoPriceHUF = _Garages.PriceNettoHUF
            Else
                If _CRMEstateStatistics.Immovables.ImmovableSoldState <> eImmovableSoldState.eSold Then
                    _CRMEstateStatistics.Immovables.ImmovableSoldState = eImmovableSoldState.eSold
                End If
            End If

            _CRMEstateStatistics.Immovables = _Immovables
            _CRMEstateStatistics.AreaNettoForSale_Sold = _Garages.AreaNetto

            _onew.CommitChanges()

        Next
        _onew.CommitChanges()
        RaiseEvent FinalWork

        '// Üzletek, irodák
        Dim _Offices_Collection As New XPCollection(Of Offices)(_onew.Session)
        Dim _Offices As Offices


        RaiseEvent InitWork(1, 1, _Offices_Collection.Count)
        For Each _Offices In _Offices_Collection
            RaiseEvent DoWork()
            _Immovables = TryCast(_Offices, Immovables)

            _CRMEstateStatistics = _onew.FindObject(Of CRMEstateStatistics)(CriteriaOperator.Parse("Immovables=?", _Immovables))
            If _CRMEstateStatistics Is Nothing Then
                _CRMEstateStatistics = _onew.CreateObject(Of CRMEstateStatistics)()
            End If
            _CRMEstateStatistics.QTT_Sold = 1
            _CRMEstateStatistics.NettoPriceHUF = GetNettoPriceHUF(_onew, _Immovables.UniqueObjects)
            If _CRMEstateStatistics.NettoPriceHUF = 0 Then
                _CRMEstateStatistics.NettoPriceHUF = _Offices.PriceNettoHUF
            Else
                If _CRMEstateStatistics.Immovables.ImmovableSoldState <> eImmovableSoldState.eSold Then
                    _CRMEstateStatistics.Immovables.ImmovableSoldState = eImmovableSoldState.eSold
                End If
            End If
            _CRMEstateStatistics.Immovables = _Immovables
            _CRMEstateStatistics.AreaNettoForSale_Sold = _Offices.AreaNettoForSale

            _onew.CommitChanges()

        Next
        _onew.CommitChanges()
        RaiseEvent FinalWork

        '// Tárolók
        Dim _Container_Collection As New XPCollection(Of Container)(_onew.Session)
        Dim _Container As Container


        RaiseEvent InitWork(1, 1, _Container_Collection.Count)
        For Each _Container In _Container_Collection
            RaiseEvent DoWork()
            _Immovables = TryCast(_Container, Immovables)

            _CRMEstateStatistics = _onew.FindObject(Of CRMEstateStatistics)(CriteriaOperator.Parse("Immovables=?", _Immovables))
            If _CRMEstateStatistics Is Nothing Then
                _CRMEstateStatistics = _onew.CreateObject(Of CRMEstateStatistics)()
            Else
                If _CRMEstateStatistics.Immovables.ImmovableSoldState <> eImmovableSoldState.eSold Then
                    _CRMEstateStatistics.Immovables.ImmovableSoldState = eImmovableSoldState.eSold
                End If
            End If
            _CRMEstateStatistics.QTT_Sold = 1
            _CRMEstateStatistics.NettoPriceHUF = GetNettoPriceHUF(_onew, _Immovables.UniqueObjects)
            If _CRMEstateStatistics.NettoPriceHUF = 0 Then
                _CRMEstateStatistics.NettoPriceHUF = _Container.PriceNettoHUF
            End If
            _CRMEstateStatistics.Immovables = _Immovables
            _CRMEstateStatistics.AreaNettoForSale_Sold = _Container.AreaNetto

            _onew.CommitChanges()

        Next
        _onew.CommitChanges()
        RaiseEvent FinalWork

    End Sub

    Private Function GetNettoPriceHUF(_onew As Xpo.XPObjectSpace, _UniqueObjects As UniqueObjects) As Double
        Dim _ContractRows_Collection As New XPCollection(Of ContractRows)(_onew.Session, CriteriaOperator.Parse("UniqueObjects=?", _UniqueObjects))
        Dim _ContractRows As ContractRows
        Dim _NettoPriceHUF As Double = 0
        Dim _Controlling_Collection As New XPCollection(Of Controlling)(_onew.Session, CriteriaOperator.Parse("IsEstateStatistics=True"))



        _ContractRows_Collection.Filter = New InOperator("Controlling", _Controlling_Collection)

        For Each _ContractRows In _ContractRows_Collection
            _NettoPriceHUF += Math.Round(_ContractRows.QTT * _ContractRows.NettoPriceHUF, 0, MidpointRounding.AwayFromZero)
        Next

        Return _NettoPriceHUF

    End Function
End Class
