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

Public Class CertificateOfContractVC
    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()
        Dim _CertificateofContractHeader As CertificateofContractHeader
        MyBase.OnActivated()
        '-- Általános letiltás --------------------------------------------------------------------------------------------------------------------------
        Frame.GetController(Of CertificateOfContractVC).aFinalCOC.Active.SetItemValue("", False)
        Frame.GetController(Of CertificateOfContractVC).aPrintCertificateOfContract.Active.SetItemValue("", False) ' -- Teljesítési igazolás nyomtatása
        Frame.GetController(Of CertificateOfContractVC).aMakeEditableCOC.Active.SetItemValue("", False)
        Frame.GetController(Of CertificateOfContractVC).aStornoCOC.Active.SetItemValue("", False)
        Frame.GetController(Of CertificateOfContractVC).aViewAttachments_COC.Active.SetItemValue("", False)
        Frame.GetController(Of CertificateOfContractVC).aViewRegistry_COC.Active.SetItemValue("", False)
        Frame.GetController(Of CertificateOfContractVC).aRecalculateCOC.Active.SetItemValue("", False)
        '------------------------------------------------------------------------------------------------------------------------------------------------

        If View.Id = "CertificateofContractHeader_ListView_NotEditable" Then
            Frame.GetController(Of CertificateOfContractVC).aPrintCertificateOfContract.Active.SetItemValue("", True)
            Frame.GetController(Of CertificateOfContractVC).aMakeEditableCOC.Active.SetItemValue("", True)
            Frame.GetController(Of CertificateOfContractVC).aStornoCOC.Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CertificateOfContractVC).aRecalculateCOC.Active.SetItemValue("", True)
        End If
        If View.Id = "CertificateofContractHeader_DetailView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            _CertificateofContractHeader = View.SelectedObjects(0)
            If _CertificateofContractHeader IsNot Nothing Then
                If _CertificateofContractHeader.IsEditable = True Then
                    Frame.GetController(Of CertificateOfContractVC).aFinalCOC.Active.SetItemValue("", True)
                    Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", True)
                    Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", True)
                    Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", True)
                Else
                    Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
                    Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
                    Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
                End If
            End If
        End If
        If View.Id = "CertificateofContractHeader_CertificateofContractRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CertificateOfContractVC).aViewAttachments_COC.Active.SetItemValue("", True)
            Frame.GetController(Of CertificateOfContractVC).aViewRegistry_COC.Active.SetItemValue("", True)

            If TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject IsNot Nothing Then
                _CertificateofContractHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, CertificateofContractHeader)
                If _CertificateofContractHeader IsNot Nothing Then
                    If _CertificateofContractHeader.IsEditable = False Then
                        Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
                        Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
                        Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
                    Else
                        Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
                        Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
                        Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
                    End If
                End If
            End If
        End If
        If View.Id = "Contracts_LookupListView" Then
            'Frame.GetController(Of NewObjectViewController).NewObjectAction.Active.SetItemValue("", False)
        End If
        If View.Id = "CertificateofContractRows_DetailView" Then
            Dim _CertificateofContractRows As CertificateofContractRows = TryCast(View.SelectedObjects(0), CertificateofContractRows)
            If _CertificateofContractRows.CertificateofContractHeader.IsEditable = False Then
                Frame.GetController(Of NewItemRowListViewController).Active.SetItemValue("", False)
                Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
                Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
                Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Else
                Frame.GetController(Of NewItemRowListViewController).Active.SetItemValue("", True)
                Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
                Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
                Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
            End If
            Frame.GetController(Of CertificateOfContractVC).aViewAttachments_COC.Active.SetItemValue("", True)
            Frame.GetController(Of CertificateOfContractVC).aViewRegistry_COC.Active.SetItemValue("", True)
            If _CertificateofContractRows.CertificateofContractHeader IsNot Nothing Then
                If _CertificateofContractRows.CertificateofContractHeader.IsEditable = False Then
                    Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
                    Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
                    Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
                Else
                    Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", True)
                    Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", True)
                    Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", True)
                End If
            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", True)
    End Sub
    Private Sub aPrintCertificateOfContract_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aPrintCertificateOfContract.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CertificateofContractHeader As CertificateofContractHeader

        Dim _ReportData As ReportData
        If e.SelectedObjects.Count > 0 Then
            _CertificateofContractHeader = e.SelectedObjects.Item(0)
            _ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _CertificateofContractHeader.Contracts.ContractTemplate.COC_ReportData.Oid), True)
            If _ReportData Is Nothing Or _CertificateofContractHeader Is Nothing Then
            Else
                Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("CertificateofContractHeader.DocumentNumber =?", _CertificateofContractHeader.DocumentNumber), True)
            End If
        End If
    End Sub

    Private Sub aFinalCOC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalCOC.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CertificateofContractHeader As CertificateofContractHeader = TryCast(View.SelectedObjects(0), CertificateofContractHeader)

        If _CertificateofContractHeader IsNot Nothing Then
            _CertificateofContractHeader.IsEditable = False
            _CertificateofContractHeader.IsStorno = False
            _ocur.CommitChanges()
            View.Close()
        End If

    End Sub

    Private Sub aMakeEditableCOC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aMakeEditableCOC.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CertificateofContractHeader As CertificateofContractHeader = TryCast(View.SelectedObjects(0), CertificateofContractHeader)

        If _CertificateofContractHeader IsNot Nothing Then
            If _CertificateofContractHeader.IsEditable = False And _CertificateofContractHeader.IsStorno = False Then
                _CertificateofContractHeader.IsEditable = True
                _ocur.CommitChanges()
                View.Refresh()
            End If
        End If
    End Sub

    Private Sub aStornoCOC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aStornoCOC.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _CertificateofContractHeader As CertificateofContractHeader = TryCast(View.SelectedObjects(0), CertificateofContractHeader)
        Dim _CertificateofContractRows As CertificateofContractRows


        If _CertificateofContractHeader IsNot Nothing Then
            If _CertificateofContractHeader.IsEditable = False And _CertificateofContractHeader.IsStorno = False Then
                For Each _CertificateofContractRows In _CertificateofContractHeader.CertificateofContractRows
                    _CertificateofContractRows.ContractRows = Nothing
                Next
                _CertificateofContractHeader.IsStorno = True
                _CertificateofContractHeader.IsEditable = False
            End If
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub

    Private Sub aViewRegistry_COC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewRegistry_COC.Execute
        Dim _CertificateofContractRows As CertificateofContractRows = TryCast(View.SelectedObjects(0), CertificateofContractRows)

        If _CertificateofContractRows IsNot Nothing Then
            If _CertificateofContractRows.ContractRows IsNot Nothing Then
                If _CertificateofContractRows.ContractRows.RegistryRowsFinancialControlling IsNot Nothing Then
                    Dim _CustomersFrom As CustomersFrom = _CertificateofContractRows.ContractRows.RegistryRowsFinancialControlling.RegistryHeader.CustomersFrom
                    Dim _Customers As Customers = _CertificateofContractRows.ContractRows.RegistryRowsFinancialControlling.RegistryHeader.Customers
                    Dim _DocumentNumber As String = _CertificateofContractRows.ContractRows.RegistryRowsFinancialControlling.RegistryHeader.DocumentNumber
                    ViewRegistry.ViewRegistry(View.ObjectSpace, Application, e, _CustomersFrom, _Customers, _DocumentNumber)
                End If
            End If
        End If

    End Sub

    Private Sub aViewAttachments_COC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewAttachments_COC.Execute
        Dim _CertificateofContractRows As CertificateofContractRows = TryCast(View.SelectedObjects(0), CertificateofContractRows)
        If _CertificateofContractRows IsNot Nothing Then
            If _CertificateofContractRows.ContractRows IsNot Nothing Then
                If _CertificateofContractRows.ContractRows.RegistryRowsFinancialControlling IsNot Nothing Then
                    Dim _CustomersFrom As CustomersFrom = _CertificateofContractRows.ContractRows.RegistryRowsFinancialControlling.RegistryHeader.CustomersFrom
                    Dim _Customers As Customers = _CertificateofContractRows.ContractRows.RegistryRowsFinancialControlling.RegistryHeader.Customers
                    Dim _DocumentNumber As String = _CertificateofContractRows.ContractRows.RegistryRowsFinancialControlling.RegistryHeader.DocumentNumber
                    ViewAttachments.ViewAttachments(View.ObjectSpace, Frame, _CustomersFrom, _Customers, _DocumentNumber, "")
                End If
            End If
        End If
    End Sub

    Private Sub aRecalculateCOC_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRecalculateCOC.Execute
        Dim _CertificateofContractHeader As CertificateofContractHeader
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _CertificateofContractHeader In View.SelectedObjects
            RaiseEvent DoWork()
            _CertificateofContractHeader.Recalculate(_uow, _CertificateofContractHeader, False)
        Next
        _uow.CommitChanges()
        RaiseEvent FinalWork
        View.Refresh()
    End Sub
End Class
