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

Public Class ImmovablesPriceListVC
    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 = "ImmovablesPriceListHeader_ImmovablesPriceListRowsA_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "ImmovablesPriceListHeader_ImmovablesPriceListRowsC_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "ImmovablesPriceListHeader_ImmovablesPriceListRowsG_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "ImmovablesPriceListHeader_ImmovablesPriceListRowsO_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "ImmovablesPriceListHeader_ImmovablesPriceListRowsA_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "ImmovablesPriceListHeader_ImmovablesPriceListRowsC_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "ImmovablesPriceListHeader_ImmovablesPriceListRowsG_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "ImmovablesPriceListHeader_ImmovablesPriceListRowsO_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aGenerateIPriceList_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGenerateIPriceList.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _ImmovablesPriceListHeader = _uow.GetObjectByKey(Of ImmovablesPriceListHeader)(TryCast(View.SelectedObjects(0), ImmovablesPriceListHeader).Oid)

        Dim _ApartmentsCollection_DEL As New XPCollection(_uow, GetType(ImmovablesPriceListRowsA), CriteriaOperator.Parse("ImmovablesPriceListHeader=?", _ImmovablesPriceListHeader))
        Dim _OfficesCollection_DEL As New XPCollection(_uow, GetType(ImmovablesPriceListRowsO), CriteriaOperator.Parse("ImmovablesPriceListHeader=?", _ImmovablesPriceListHeader))
        Dim _GaragesCollection_DEL As New XPCollection(_uow, GetType(ImmovablesPriceListRowsG), CriteriaOperator.Parse("ImmovablesPriceListHeader=?", _ImmovablesPriceListHeader))
        Dim _ContainerCollection_DEL As New XPCollection(_uow, GetType(ImmovablesPriceListRowsC), CriteriaOperator.Parse("ImmovablesPriceListHeader=?", _ImmovablesPriceListHeader))

        _uow.Delete(_ApartmentsCollection_DEL)
        _uow.Delete(_OfficesCollection_DEL)
        _uow.Delete(_GaragesCollection_DEL)
        _uow.Delete(_ContainerCollection_DEL)
        _uow.CommitChanges()

        Dim _ApartmentsCollection As New XPCollection(_uow, GetType(Apartments), CriteriaOperator.Parse("(CanForSale=True Or CanForRent = True) and ImmovableGroups=?", _ImmovablesPriceListHeader.ImmovablesGroups))
        Dim _OfficesCollection As New XPCollection(_uow, GetType(Offices), CriteriaOperator.Parse("(CanForSale=True Or CanForRent = True) and ImmovableGroups=?", _ImmovablesPriceListHeader.ImmovablesGroups))
        Dim _GaragesCollection As New XPCollection(_uow, GetType(Garages), CriteriaOperator.Parse("(CanForSale=True Or CanForRent = True) and ImmovableGroups=?", _ImmovablesPriceListHeader.ImmovablesGroups))
        Dim _ContainerCollection As New XPCollection(_uow, GetType(Container), CriteriaOperator.Parse("(CanForSale=True Or CanForRent = True) and ImmovableGroups=?", _ImmovablesPriceListHeader.ImmovablesGroups))

        Dim _ImmovablesPriceListRowsA As ImmovablesPriceListRowsA
        Dim _ImmovablesPriceListRowsO As ImmovablesPriceListRowsO
        Dim _ImmovablesPriceListRowsG As ImmovablesPriceListRowsG
        Dim _ImmovablesPriceListRowsC As ImmovablesPriceListRowsC

        For Each _Apartments As Apartments In _ApartmentsCollection
            _ImmovablesPriceListRowsA = New ImmovablesPriceListRowsA(_uow)
            _ImmovablesPriceListRowsA.ImmovablesPriceListHeader = _ImmovablesPriceListHeader
            _ImmovablesPriceListRowsA.Apartments = _Apartments
            _ImmovablesPriceListRowsA.Save()
        Next

        For Each _Offices As Offices In _OfficesCollection
            _ImmovablesPriceListRowsO = New ImmovablesPriceListRowsO(_uow)
            _ImmovablesPriceListRowsO.ImmovablesPriceListHeader = _ImmovablesPriceListHeader
            _ImmovablesPriceListRowsO.Offices = _Offices
            _ImmovablesPriceListRowsO.Save()
        Next

        For Each _Garages As Garages In _GaragesCollection
            _ImmovablesPriceListRowsG = New ImmovablesPriceListRowsG(_uow)
            _ImmovablesPriceListRowsG.ImmovablesPriceListHeader = _ImmovablesPriceListHeader
            _ImmovablesPriceListRowsG.Garages = _Garages
            _ImmovablesPriceListRowsG.Save()
        Next

        For Each _Container As Container In _ContainerCollection
            _ImmovablesPriceListRowsC = New ImmovablesPriceListRowsC(_uow)
            _ImmovablesPriceListRowsC.ImmovablesPriceListHeader = _ImmovablesPriceListHeader
            _ImmovablesPriceListRowsC.Container = _Container
            _ImmovablesPriceListRowsC.Save()
        Next
        _uow.CommitChanges()
        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
  
End Class
