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.CloneObject
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.Persistent.BaseImpl


Public Class TakeOverProtocolVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "TakeoverProtocolHeader_ListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If

        If View.Id = "TakeoverProtocolHeader_TakeOverProtocolRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
        End If

        If View.Id = "TakeoverProtocolRows_ListView_Finalized" Then
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            AddHandler Frame.GetController(Of ListViewProcessCurrentObjectController).ProcessCurrentObjectAction.Executing, AddressOf TakeoverProtocolRows_ListView_Finalized_ProcessCurrentObjectAction_Executing
        End If
        '--------------------DetailView-----------------------
        If View.Id = "TakeoverProtocolHeader_DetailView" Then
            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 = "TakeoverProtocolHeader_ListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "TakeoverProtocolHeader_TakeOverProtocolRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If

        If View.Id = "TakeoverProtocolRows_ListView_Finalized" Then
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            RemoveHandler Frame.GetController(Of ListViewProcessCurrentObjectController).ProcessCurrentObjectAction.Executing, AddressOf TakeoverProtocolRows_ListView_Finalized_ProcessCurrentObjectAction_Executing
        End If
        '--------------------DetailView-----------------------
        If View.Id = "TakeoverProtocolHeader_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aToTable_TPR_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_TPR.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _TakeoverProtocolHeader As TakeoverProtocolHeader = TryCast(View.SelectedObjects(0), TakeoverProtocolHeader)
            If _TakeoverProtocolHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            Dim _TakeoverProtocolRows As TakeoverProtocolRows = _ocur.CreateObject(Of TakeoverProtocolRows)()

            _TakeoverProtocolRows.TakeoverProtocolHeader = _TakeoverProtocolHeader
            _TakeoverProtocolRows.Installation = _TakeoverProtocolHeader.row_Installation
            _TakeoverProtocolRows.UniqueObjects = _TakeoverProtocolHeader.row_UniqueObjects

            _TakeoverProtocolHeader.row_Installation.TakeoverProtocolRows = _TakeoverProtocolRows

            _ocur.CommitChanges()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            Dim _TakeoverProtocolHeader As TakeoverProtocolHeader = TryCast(View.SelectedObjects(0), TakeoverProtocolHeader)
            _TakeoverProtocolHeader.row_Installation = Nothing
            _TakeoverProtocolHeader.row_UniqueObjects = Nothing

            Dim ItemToFocus As ViewItem = TryCast(View, DetailView).FindItem("row_Installation")
            If ItemToFocus IsNot Nothing Then
                Dim ItemControl As Object
                ItemControl = ItemToFocus.Control
                ItemControl.Focus()
            End If
        End Try
    End Sub
    Private Sub aToTableD_TPR_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_TPR.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _TakeoverProtocolHeader As TakeoverProtocolHeader = TryCast(View.SelectedObjects(0), TakeoverProtocolHeader)
            If _TakeoverProtocolHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            Dim _TakeoverProtocolRows_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("TakeOverProtocolRows")
            If _TakeoverProtocolRows_ListEditor.ListView.SelectedObjects.Count = 0 Then Throw New Exception("*Nincs kiválasztva egy sor sem!")

            Dim _TakeoverProtocolRows_DelList As New ArrayList
            For Each _TakeoverProtocolRows As TakeoverProtocolRows In _TakeoverProtocolRows_ListEditor.ListView.SelectedObjects
                _TakeoverProtocolRows.Installation.TakeoverProtocolRows = Nothing
                _TakeoverProtocolRows_DelList.Add(_TakeoverProtocolRows)
            Next
            _ocur.Delete(_TakeoverProtocolRows_DelList)
            _ocur.CommitChanges()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
            If nestedFrame IsNot Nothing Then
                Dim parentView As View = nestedFrame.View
                parentView.ObjectSpace.Refresh()
            End If
        End Try
    End Sub
    Private Sub aFinalize_TPR_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalize_TPR.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _TakeoverProtocolHeader As TakeoverProtocolHeader = TryCast(View.SelectedObjects(0), TakeoverProtocolHeader)
            If _TakeoverProtocolHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _TakeoverProtocolHeader.TakeOverProtocolRows.Count = 0 Then Throw New Exception("*Legalább egy sornak lennie kell, üres átadást nem lehet véglegesíteni!")
            _TakeoverProtocolHeader.IsEditable = False

            _ocur.CommitChanges()
            If _TakeoverProtocolHeader.TakeoverProtocolType.ReportData Is Nothing Then Throw New Exception("*Nincs beállítva nyomtatvány!" + vbNewLine + "Nem lehet bizonylatot nyomtatni. Kérem, ellenőrizze!")
            Dim _ReportData As ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _TakeoverProtocolHeader.TakeoverProtocolType.ReportData.Oid), True)
            If _ReportData IsNot Nothing Then
                Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("TakeoverProtocolHeader.DocumentNumber=?", _TakeoverProtocolHeader.DocumentNumber), True)
            End If
            View.Close()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aStorno_TPR_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aStorno_TPR.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _TakeoverProtocolHeader As TakeoverProtocolHeader = TryCast(View.SelectedObjects(0), TakeoverProtocolHeader)
            If _TakeoverProtocolHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _TakeoverProtocolHeader.IsEditable = True Then Throw New Exception("*Csak végleges átadást lehet sztornírozni!")
            _TakeoverProtocolHeader.IsStorno = True

            For Each _TakeoverProtocolRows As TakeoverProtocolRows In _TakeoverProtocolHeader.TakeOverProtocolRows
                _TakeoverProtocolRows.Installation.TakeoverProtocolRows = Nothing
            Next

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aPrint_TPR_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aPrint_TPR.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _TakeoverProtocolHeader As TakeoverProtocolHeader = TryCast(View.SelectedObjects(0), TakeoverProtocolHeader)
            If _TakeoverProtocolHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _TakeoverProtocolHeader.TakeoverProtocolType.ReportData Is Nothing Then Throw New Exception("*Nincs beállítva nyomtatvány!" + vbNewLine + "Nem lehet bizonylatot nyomtatni. Kérem, ellenőrizze!")

            Dim _ReportData As ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _TakeoverProtocolHeader.TakeoverProtocolType.ReportData.Oid), True)
            If _ReportData IsNot Nothing Then Throw New Exception("*Nincs beállítva nyomtatvány!")
            Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("TakeoverProtocolHeader.DocumentNumber=?", _TakeoverProtocolHeader.DocumentNumber), True)
            View.Close()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aBakToEdit_TPR_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBakToEdit_TPR.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _TakeoverProtocolHeader As TakeoverProtocolHeader = TryCast(View.SelectedObjects(0), TakeoverProtocolHeader)
            If _TakeoverProtocolHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            _TakeoverProtocolHeader.IsEditable = True

            _ocur.CommitChanges()

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "TakeoverProtocolHeader_DetailView", False, _onew.GetObject(_TakeoverProtocolHeader))
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub

    Private Sub TakeoverProtocolRows_ListView_Finalized_ProcessCurrentObjectAction_Executing(sender As Object, e As CancelEventArgs)
        e.Cancel = True
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _TakeoverProtocolHeader As TakeoverProtocolHeader = TryCast(View.SelectedObjects(0), TakeoverProtocolRows).TakeoverProtocolHeader
        Dim _ShowViewParameters As New ShowViewParameters
        Dim _ShowViewSource As New ShowViewSource(Frame, Nothing)
        _ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "TakeoverProtocolHeader_DetailView", False, _onew.GetObject(_TakeoverProtocolHeader))
        _ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild
        _ShowViewParameters.TargetWindow = TargetWindow.NewWindow
        Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)
    End Sub

End Class
