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.BaseImpl
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.Utils
Imports System.Reflection
Imports DevExpress.Data.Filtering

Public Class HRPerson_VC
    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()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If SecuritySystem.CurrentUserName <> "SysAdmin" Then Frame.GetController(Of HRPerson_VC).aCreateUniqeObjects.Active.SetItemValue("", False)

        If View.Id = "User_ListView_Deleted" Then
            If TryCast(View, ListView) IsNot Nothing Then
                If TryCast(View, ListView).CollectionSource IsNot Nothing Then
                    If TryCast(View, ListView).CollectionSource.Collection IsNot Nothing Then
                        If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection) IsNot Nothing Then
                            If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection IsNot Nothing Then
                                If TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, XPCollection) IsNot Nothing Then
                                    TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, XPCollection).SelectDeleted = True
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
        If View.Id = "HRPerson_ListView_Deleted" Then
            If TryCast(View, ListView) IsNot Nothing Then
                If TryCast(View, ListView).CollectionSource IsNot Nothing Then
                    If TryCast(View, ListView).CollectionSource.Collection IsNot Nothing Then
                        If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection) IsNot Nothing Then
                            If TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection IsNot Nothing Then
                                If TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, XPCollection) IsNot Nothing Then
                                    TryCast(TryCast(TryCast(View, ListView).CollectionSource.Collection, ProxyCollection).OriginalCollection, XPCollection).SelectDeleted = True
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub aCreateUniqeObjects_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateUniqeObjects.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        For Each _HRPerson As HRPerson In View.SelectedObjects
            If _HRPerson.UniqueObjects Is Nothing Then
                Dim _UniqueObjects As UniqueObjects = New UniqueObjects(_uow)
                _UniqueObjects.Name = _HRPerson.FullName
                _UniqueObjects.HRPerson = _uow.GetObjectByKey(Of HRPerson)(_HRPerson.Oid)
                _UniqueObjects.HRPerson.UniqueObjects = _UniqueObjects
            End If
        Next
        _uow.CommitChanges()
    End Sub
    Private Sub aCloneHRPersonTasks_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCloneHRPersonTasks.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractRowsCloneView As ContractRowsCloneView = _onew.CreateObject(Of ContractRowsCloneView)()

        _ContractRowsCloneView.DayOfExecution = eDayOfMonth.e1
        _ContractRowsCloneView.ActionFrequency = eActionFrequency.eMonthly
        _ContractRowsCloneView.DayOfExecutionInWeek = eDayOfWeek.eMonday
        _ContractRowsCloneView.ActionCount = 1

        e.View = Application.CreateDetailView(_onew, "ContractRowsCloneView_DetailView_HRPerson", False, _ContractRowsCloneView)
    End Sub
    Private Sub aCloneHRPersonTasks_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCloneHRPersonTasks.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Try
            Dim _HRPersonTasks As HRPersonTasks = TryCast(View.SelectedObjects(0), HRPersonTasks)
            If _HRPersonTasks Is Nothing Then Throw New Exception("*Nincs kiválasztott feladat!")
            Dim _ContractRowsCloneView As ContractRowsCloneView = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractRowsCloneView)
            If _ContractRowsCloneView Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")


            Dim _DateStart As Date
            Dim _DateCur As Date
            Dim _DiffDay As Long

            RaiseEvent InitWork(1, 1, _ContractRowsCloneView.ActionCount)
            For i = 1 To _ContractRowsCloneView.ActionCount
                Dim _HRPersonTasks_New As HRPersonTasks = _ocur.CreateObject(Of HRPersonTasks)()
                _HRPersonTasks_New.AlertPeriod = _HRPersonTasks.AlertPeriod
                _HRPersonTasks_New.AlertPeriodAlias = _HRPersonTasks.AlertPeriodAlias
                _HRPersonTasks_New.AssignedTo = _HRPersonTasks.AssignedTo
                _HRPersonTasks_New.Description = _HRPersonTasks.Description
                _HRPersonTasks_New.HRPerson = _HRPersonTasks.HRPerson
                _HRPersonTasks_New.HRPersonAlias = _HRPersonTasks.HRPersonAlias
                _HRPersonTasks_New.Subject = _HRPersonTasks.Subject

                _DateStart = _HRPersonTasks.DueDate

                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eDaily Then
                    _DateCur = DateAdd(DateInterval.Day, i, _DateStart)
                End If
                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eMonthly Then
                    If _ContractRowsCloneView.DayOfExecution = eDayOfMonth.eLastDay Then
                        _DateCur = DateAdd(DateInterval.Month, i + 1, _DateStart)
                        _DateCur = DateAdd(DateInterval.Day, -1, CDate(Year(_DateCur) & "/" & Month(_DateCur) & "/01"))
                    Else
                        _DateCur = DateAdd(DateInterval.Month, i, _DateStart)
                        _DateCur = CDate(Year(_DateCur) & "/" & Month(_DateCur) & "/" & _ContractRowsCloneView.DayOfExecution)
                    End If
                End If
                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eWeekly Then
                    _DiffDay = _ContractRowsCloneView.DayOfExecutionInWeek - DatePart(DateInterval.Weekday, _DateStart)
                    _DiffDay += 1
                    _DateCur = DateAdd(DateInterval.WeekOfYear, i, _DateStart)
                    _DateCur = DateAdd(DateInterval.Day, _DiffDay, _DateCur)

                End If
                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eTwoWeekly Then
                    _DiffDay = _ContractRowsCloneView.DayOfExecutionInWeek - DatePart(DateInterval.Weekday, _DateStart)
                    _DiffDay += 1
                    _DateCur = DateAdd(DateInterval.WeekOfYear, i * 2, _DateStart)
                    _DateCur = DateAdd(DateInterval.Day, _DiffDay, _DateCur)
                End If
                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eQuarter Then
                    _DateCur = DateAdd(DateInterval.Quarter, i, _DateStart)
                    _DateCur = CDate(Year(_DateCur) & "/" & Month(_DateCur) & "/" & _ContractRowsCloneView.DayOfExecution)
                End If
                If _ContractRowsCloneView.ActionFrequency = eActionFrequency.eYear Then
                    _DateCur = DateAdd(DateInterval.Year, i, _DateStart)
                    _DateCur = CDate(Year(_DateCur) & "/" & Month(_DateCur) & "/" & _ContractRowsCloneView.DayOfExecution)
                End If
                _HRPersonTasks_New.DueDate = _DateCur
                RaiseEvent DoWork()
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        Finally
            RaiseEvent FinalWork
        End Try
    End Sub

    Private Sub aMergeUsers_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aMergeUsers.Execute
        Try
            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Object As Object
            Dim _User As User
            Dim _UserActive As User

            For Each _User In View.SelectedObjects

                _UserActive = _onew.FindObject(Of User)(CriteriaOperator.Parse("UserName=?", _User.UserName))
                If _UserActive IsNot Nothing Then
                    RaiseEvent InitWork(1, 1, _onew.Session.CollectReferencingObjects(_onew.GetObjectByKey(Of User)(_User.Oid)).Count)
                    For Each _Object In _onew.Session.CollectReferencingObjects(_onew.GetObjectByKey(Of User)(_User.Oid))
                        RaiseEvent DoWork()
                        Try
                            For Each _PropertyInfo As PropertyInfo In _Object.GetType.GetProperties
                                If _PropertyInfo.PropertyType.Name = "User" Then
                                    _PropertyInfo.SetValue(_Object, _onew.GetObjectByKey(Of User)(_UserActive.Oid), Nothing)
                                End If
                            Next
                        Catch ex As Exception
                            Throw New Exception(ex.Message)
                        End Try
                    Next
                    RaiseEvent FinalWork
                End If
            Next
            _onew.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
        End Try
    End Sub

    Private Sub aMergeHRPerson_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aMergeHRPerson.Execute
        Try
            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Object As Object
            Dim _HRPerson As HRPerson
            Dim _HRPersonActive As HRPerson

            For Each _HRPerson In View.SelectedObjects

                _HRPersonActive = _onew.FindObject(Of HRPerson)(CriteriaOperator.Parse("User.UserName=?", _HRPerson.User.UserName))
                If _HRPersonActive IsNot Nothing Then
                    RaiseEvent InitWork(1, 1, _onew.Session.CollectReferencingObjects(_onew.GetObjectByKey(Of HRPerson)(_HRPerson.Oid)).Count)
                    For Each _Object In _onew.Session.CollectReferencingObjects(_onew.GetObjectByKey(Of HRPerson)(_HRPerson.Oid))
                        RaiseEvent DoWork()
                        Try
                            For Each _PropertyInfo As PropertyInfo In _Object.GetType.GetProperties
                                If _PropertyInfo.PropertyType.Name = "HRPerson" Then
                                    _PropertyInfo.SetValue(_Object, _onew.GetObjectByKey(Of HRPerson)(_HRPersonActive.Oid), Nothing)
                                End If
                            Next
                        Catch ex As Exception
                            Throw New Exception(ex.Message)
                        End Try
                    Next
                    RaiseEvent FinalWork
                End If
            Next
            _onew.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
        End Try
    End Sub
End Class
