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
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.CloneObject
Imports System.Linq
Imports System.Linq.Expressions
Imports DevExpress.Persistent.BaseImpl

Public Class RRFCQueryVC
    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()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _User As User

        If _ocur IsNot Nothing Then
            _User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
        End If

        If View.Id = "RRFCQueryHeader_RRFCQueryRows_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "RRFCQueryHeader_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteria = "UserCreated.Oid='" & _User.Oid.ToString & "'"
        End If
        If View.Id = "RRFCQueryHeader_DetailView" Then
            Frame.GetController(Of DeleteObjectsViewController).DeleteAction.TargetObjectsCriteria = "UserCreated.Oid='" & _User.Oid.ToString & "'"
            Frame.GetController(Of RRFCQueryVC).aQueryRRFC.TargetObjectsCriteria = "UserCreated.Oid='" & _User.Oid.ToString & "'"
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "RRFCQueryHeader_ListView" Then
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aQueryRRFC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aQueryRRFC.Execute
        'Frame.GetController(Of ModificationsController).SaveAction.DoExecute()

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _RRFCQueryHeader As RRFCQueryHeader = _uow.GetObjectByKey(Of RRFCQueryHeader)(TryCast(View.SelectedObjects(0), RRFCQueryHeader).Oid)
        Dim _RRFCQueryRows As RRFCQueryRows


        Dim _RRFCQueryRows_Collection As New XPCollection(_uow, GetType(RRFCQueryRows), _
        CriteriaOperator.Parse("RRFCQueryHeader=?", _RRFCQueryHeader))
        _uow.Delete(_RRFCQueryRows_Collection)

        Dim _RowIndex As Long = 0

        Dim _RRFC_xpquery = New XPQuery(Of RegistryRowsFinancialControlling)(_uow)

        Dim query = From _RRFC In _RRFC_xpquery
                    Where _RRFC.RegistryHeader.CustomersFrom Is _RRFCQueryHeader.CustomersFrom And _
                    _RRFC.ReInvoice_ContractRows.InvoiceRows IsNot Nothing And _
                    _RRFC.ReInvoice_ContractRows.InvoiceRows.InvoiceHeader.IsEditable = False
                Select New With {Key .CustomersFrom = _RRFC.RegistryHeader.CustomersFrom, _
                                 Key .RegistryHeader = _RRFC.RegistryHeader, _
                                 Key .RegistryRowsFinancialControlling = _RRFC.Oid, _
                                 Key .InvoiceRows = _RRFC.ReInvoice_ContractRows.InvoiceRows}

        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()
            _RRFCQueryRows = New RRFCQueryRows(_uow)
            _RRFCQueryRows.RRFCQueryHeader = _RRFCQueryHeader
            _RRFCQueryRows.RegistryHeader = item.RegistryHeader
            _RRFCQueryRows.InvoiceRows = item.InvoiceRows
            _RRFCQueryRows.RegistryRowsFinancialControlling = _uow.GetObjectByKey(Of RegistryRowsFinancialControlling)(item.RegistryRowsFinancialControlling)
            _RowIndex += 1
            If _RowIndex Mod 100 = 0 Then _uow.CommitChanges()
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork
        If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub aViewAttachments_RRFC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewAttachments_RRFC.Execute
        Dim _RRFCQueryRows As RRFCQueryRows = TryCast(View.SelectedObjects(0), RRFCQueryRows)
        Dim _CustomersFrom As CustomersFrom = _RRFCQueryRows.RegistryHeader.CustomersFrom
        Dim _Customers As Customers = _RRFCQueryRows.RegistryHeader.Customers
        Dim _DocumentNumber As String = _RRFCQueryRows.RegistryHeader.DocumentNumber

        ViewAttachments.ViewAttachments(View.ObjectSpace, Frame, _CustomersFrom, _Customers, _DocumentNumber, "")
    End Sub
    Private Sub aViewRegistry_RRFC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewRegistry_RRFC.Execute
        Dim _RRFCQueryRows As RRFCQueryRows = TryCast(View.SelectedObjects(0), RRFCQueryRows)
        Dim _CustomersFrom As CustomersFrom = _RRFCQueryRows.RegistryHeader.CustomersFrom
        Dim _Customers As Customers = _RRFCQueryRows.RegistryHeader.Customers
        Dim _DocumentNumber As String = _RRFCQueryRows.RegistryHeader.DocumentNumber

        ViewRegistry.ViewRegistry(View.ObjectSpace, Application, e, _CustomersFrom, _Customers, _DocumentNumber)

    End Sub
End Class
