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.Persistent.Validation
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.Persistent.BaseImpl

Public Class FixedAssetsVC
    Inherits DevExpress.ExpressApp.ViewController
    Protected _selection As ArrayList
    Private _noselect As Boolean = False
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

        _selection = New ArrayList()
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        If View.Id Like "FixedAssets_ListView*" Then
            AddHandler View.SelectionChanged, AddressOf view_SelectionChanged
        End If
        If View.Id = "FixedAssetsUsingPopup_DetailView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf FixedAssetsUsingPopup_DetailViewAcceptAction_Executing
        End If
    End Sub

    Private Sub aToDocuments_FixAssets_Cancel(sender As Object, e As System.EventArgs) Handles aToDocuments_FixAssets.Cancel
        _noselect = False
    End Sub
    Private Sub aToDocuments_FixAssets_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aToDocuments_FixAssets.CustomizePopupWindowParams
        _noselect = True
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _FixedAssetsUsingPopup As FixedAssetsUsingPopup = _onew.CreateObject(Of FixedAssetsUsingPopup)()
        _FixedAssetsUsingPopup.FixedAssetsUsingMoveType = eFixedAssetsUsingMoveType.eOut
        e.View = Application.CreateDetailView(_onew, "FixedAssetsUsingPopup_DetailView", True, _FixedAssetsUsingPopup)
    End Sub
    Private Sub aToDocuments_FixAssets_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aToDocuments_FixAssets.Execute
        '// Tárgyieszköz kiadása
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _FixedAssetsUsingPopup As FixedAssetsUsingPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), FixedAssetsUsingPopup)
        Dim _FixedAssets As FixedAssets
        Dim I As Long = 0
        Dim _FixedAssetsUsingHeader As FixedAssetsUsingHeader = Nothing
        Dim _FixedAssetsUsingRows As FixedAssetsUsingRows = Nothing

        For Each _FixedAssets In _selection
            If I = 0 Then
                '--Fejléc ...
                _FixedAssetsUsingHeader = _ocur.CreateObject(Of FixedAssetsUsingHeader)()
                _FixedAssetsUsingHeader.CustomersFrom = _ocur.GetObject(_FixedAssets.CustomersFrom)
                _FixedAssetsUsingHeader.FixedAssetsUsingMoveType = eFixedAssetsUsingMoveType.eOut
                _FixedAssetsUsingHeader.FixedAssetsUsingType = _FixedAssetsUsingPopup.FixedAssetsUsingType
                _FixedAssetsUsingHeader.FixedAssetsUsingParameters = _ocur.GetObject(_FixedAssetsUsingPopup.FixedAssetsUsingParameters)
                _FixedAssetsUsingHeader.DateExecution = _FixedAssetsUsingPopup.DateExecution
                Select Case _FixedAssetsUsingPopup.FixedAssetsUsingType
                    Case eFixedAssetsUsingType.eCostHolder
                        _FixedAssetsUsingHeader.CostHolder = _ocur.GetObject(_FixedAssetsUsingPopup.CostHolder)
                    Case eFixedAssetsUsingType.eCustomers
                        _FixedAssetsUsingHeader.Customers = _ocur.GetObject(_FixedAssetsUsingPopup.Customers)
                    Case eFixedAssetsUsingType.eHRPerson
                        _FixedAssetsUsingHeader.HRperson = _ocur.GetObject(_FixedAssetsUsingPopup.HRPerson)
                End Select
                I += 1
            End If

            '-- Sorok
            _FixedAssetsUsingRows = _ocur.CreateObject(Of FixedAssetsUsingRows)()
            _FixedAssetsUsingRows.FixedAssetsUsingHeader = _FixedAssetsUsingHeader
            _FixedAssetsUsingRows.FixedAssets = _FixedAssets

            _FixedAssets.CostHolder_Using = Nothing
            _FixedAssets.HRPerson_Using = Nothing
            _FixedAssets.Customers_Using = Nothing

            Select Case _FixedAssetsUsingPopup.FixedAssetsUsingType
                Case eFixedAssetsUsingType.eCostHolder
                    _FixedAssets.CostHolder_Using = _ocur.GetObject(_FixedAssetsUsingPopup.CostHolder)
                Case eFixedAssetsUsingType.eCustomers
                    _FixedAssets.Customers_Using = _ocur.GetObject(_FixedAssetsUsingPopup.Customers)
                Case eFixedAssetsUsingType.eHRPerson
                    _FixedAssets.HRPerson_Using = _ocur.GetObject(_FixedAssetsUsingPopup.HRPerson)
            End Select
        Next
        _ocur.CommitChanges()
        View.Refresh()

        _noselect = False

        Dim _ReportData As ReportData = _FixedAssetsUsingHeader.FixedAssetsUsingParameters.ReportData
        If _ReportData Is Nothing Then Throw New Exception("*Nincs beállítva riport!")
        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, DevExpress.Data.Filtering.CriteriaOperator.Parse("FixedAssetsUsingHeader.DocumentNumber =?", _FixedAssetsUsingHeader.DocumentNumber), True)
    End Sub

    Private Sub view_SelectionChanged(sender As Object, e As EventArgs)
        Dim i As Long = 0
        Dim _FixedAssets As FixedAssets

        If View Is Nothing Then Exit Sub

        If View.Id Like "FixedAssets_ListView*" Then
            If _noselect = False Then
                _selection.Clear()
                For Each _FixedAssets In View.SelectedObjects
                    _selection.Add(_FixedAssets)
                Next
            End If
        End If
    End Sub

    Private Sub aToDocumentsD_FixAssets_Cancel(sender As Object, e As System.EventArgs) Handles aToDocumentsD_FixAssets.Cancel
        _noselect = False
    End Sub
    Private Sub aToDocumentsD_FixAssets_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aToDocumentsD_FixAssets.CustomizePopupWindowParams
        _noselect = True
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _FixedAssetsUsingPopup As FixedAssetsUsingPopup = _onew.CreateObject(Of FixedAssetsUsingPopup)()
        _FixedAssetsUsingPopup.FixedAssetsUsingMoveType = eFixedAssetsUsingMoveType.eIn
        e.View = Application.CreateDetailView(_onew, "FixedAssetsUsingPopup_DetailView", True, _FixedAssetsUsingPopup)
    End Sub
    Private Sub aToDocumentsD_FixAssets_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aToDocumentsD_FixAssets.Execute
        '// Tárgyieszköz visszavétele
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _FixedAssetsUsingPopup As FixedAssetsUsingPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), FixedAssetsUsingPopup)
        Dim _FixedAssets As FixedAssets = Nothing
        Dim _FixedAssets_Base As FixedAssets = Nothing
        Dim I As Long = 0
        Dim _FixedAssetsUsingHeader As FixedAssetsUsingHeader = Nothing
        Dim _FixedAssetsUsingRows As FixedAssetsUsingRows = Nothing

        For Each _FixedAssets In _selection
            If I = 0 Then
                '--Fejléc ...
                _FixedAssets_Base = _FixedAssets
                _FixedAssetsUsingHeader = _ocur.CreateObject(Of FixedAssetsUsingHeader)()
                _FixedAssetsUsingHeader.CustomersFrom = _ocur.GetObject(_FixedAssets.CustomersFrom)
                _FixedAssetsUsingHeader.FixedAssetsUsingMoveType = eFixedAssetsUsingMoveType.eIn

                _FixedAssetsUsingHeader.FixedAssetsUsingParameters = _ocur.GetObject(_FixedAssetsUsingPopup.FixedAssetsUsingParameters)
                _FixedAssetsUsingHeader.DateExecution = _FixedAssetsUsingPopup.DateExecution
                If _FixedAssets.Customers_Using IsNot Nothing Then
                    _FixedAssetsUsingHeader.FixedAssetsUsingType = eFixedAssetsUsingType.eCustomers
                    _FixedAssetsUsingHeader.Customers = _ocur.GetObject(_FixedAssets.Customers_Using)
                End If
                If _FixedAssets.HRPerson_Using IsNot Nothing Then
                    _FixedAssetsUsingHeader.FixedAssetsUsingType = eFixedAssetsUsingType.eHRPerson
                    _FixedAssetsUsingHeader.HRperson = _ocur.GetObject(_FixedAssets.HRPerson_Using)
                End If
                If _FixedAssets.CostHolder_Using IsNot Nothing Then
                    _FixedAssetsUsingHeader.FixedAssetsUsingType = eFixedAssetsUsingType.eCostHolder
                    _FixedAssetsUsingHeader.CostHolder = _ocur.GetObject(_FixedAssets.CostHolder_Using)
                End If


            End If
            If I = 0 Then
                '-- Sorok
                _FixedAssetsUsingRows = _ocur.CreateObject(Of FixedAssetsUsingRows)()
                _FixedAssetsUsingRows.FixedAssetsUsingHeader = _FixedAssetsUsingHeader
                _FixedAssetsUsingRows.FixedAssets = _FixedAssets

                _FixedAssets.CostHolder_Using = Nothing
                _FixedAssets.HRPerson_Using = Nothing
                _FixedAssets.Customers_Using = Nothing
            Else
                If _FixedAssets.CostHolder_Using Is _FixedAssets_Base.CostHolder_Using And _FixedAssets.CostHolder_Using IsNot Nothing Then
                    '-- Sorok
                    _FixedAssetsUsingRows = _ocur.CreateObject(Of FixedAssetsUsingRows)()
                    _FixedAssetsUsingRows.FixedAssetsUsingHeader = _FixedAssetsUsingHeader
                    _FixedAssetsUsingRows.FixedAssets = _FixedAssets

                    _FixedAssets.CostHolder_Using = Nothing
                    _FixedAssets.HRPerson_Using = Nothing
                    _FixedAssets.Customers_Using = Nothing
                ElseIf _FixedAssets.Customers_Using Is _FixedAssets_Base.Customers_Using And _FixedAssets.Customers_Using IsNot Nothing Then
                    '-- Sorok
                    _FixedAssetsUsingRows = _ocur.CreateObject(Of FixedAssetsUsingRows)()
                    _FixedAssetsUsingRows.FixedAssetsUsingHeader = _FixedAssetsUsingHeader
                    _FixedAssetsUsingRows.FixedAssets = _FixedAssets

                    _FixedAssets.CostHolder_Using = Nothing
                    _FixedAssets.HRPerson_Using = Nothing
                    _FixedAssets.Customers_Using = Nothing
                ElseIf _FixedAssets.HRPerson_Using Is _FixedAssets_Base.HRPerson_Using And _FixedAssets.HRPerson_Using IsNot Nothing Then
                    '-- Sorok
                    _FixedAssetsUsingRows = _ocur.CreateObject(Of FixedAssetsUsingRows)()
                    _FixedAssetsUsingRows.FixedAssetsUsingHeader = _FixedAssetsUsingHeader
                    _FixedAssetsUsingRows.FixedAssets = _FixedAssets

                    _FixedAssets.CostHolder_Using = Nothing
                    _FixedAssets.HRPerson_Using = Nothing
                    _FixedAssets.Customers_Using = Nothing
                End If

            End If


            I = 1
        Next
        _ocur.CommitChanges()
        View.Refresh()

        Dim _ReportData As ReportData = _FixedAssetsUsingHeader.FixedAssetsUsingParameters.ReportData
        If _ReportData Is Nothing Then Throw New Exception("*Nincs beállítva riport!")
        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, DevExpress.Data.Filtering.CriteriaOperator.Parse("FixedAssetsUsingHeader.DocumentNumber =?", _FixedAssetsUsingHeader.DocumentNumber), True)
    End Sub

    Private Sub FixedAssetsUsingPopup_DetailViewAcceptAction_Executing(sender As Object, e As CancelEventArgs)
        Validator.RuleSet.ValidateAll(View.ObjectSpace, View.SelectedObjects, "DialogOk_Contexts")
    End Sub

    Private Sub aPrintFixedAssets_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aPrintFixedAssets.Execute
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _FixedAssetsUsingHeader As FixedAssetsUsingHeader = TryCast(View.SelectedObjects(0), FixedAssetsUsingHeader)
            If _FixedAssetsUsingHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            Dim _ReportData As ReportData = _FixedAssetsUsingHeader.FixedAssetsUsingParameters.ReportData
            If _ReportData Is Nothing Then Throw New Exception("*Nincs beállítva riport!")
            Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, DevExpress.Data.Filtering.CriteriaOperator.Parse("FixedAssetsUsingHeader.DocumentNumber =?", _FixedAssetsUsingHeader.DocumentNumber), True)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
End Class
