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 ImmovablesVC
    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()

    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub
    Private Sub aIRentGetContracts_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aIRentGetContracts.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Immovables As Immovables

        Dim _ContractRows As ContractRows
        Dim _ContractRows_Collection As XPCollection(Of ContractRows)
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        RaiseEvent InitWork(1, 1, TryCast(View, ListView).CollectionSource.List.Count)
        For Each _Immovables In TryCast(View, ListView).CollectionSource.List
            RaiseEvent DoWork()
            _Immovables.Last_ContractsRows = Nothing
            _ContractRows_Collection = New XPCollection(Of ContractRows)(_ocur.Session, CriteriaOperator.Parse("Contracts.State='Signed' and UniqueObjects=? " & _
                                                                                     " and Contracts.ContractTemplate.PartnerType='eReceivables'",
                                                                                     _Immovables.UniqueObjects))
            _ContractRows_Collection.Sorting.Add(New SortProperty("GetDate(DateExecution)", DB.SortingDirection.Descending))
            For Each _ContractRows In _ContractRows_Collection
                _Immovables.Last_ContractsRows = _ContractRows
                Exit For
            Next
        Next
        RaiseEvent FinalWork
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub
End Class
