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.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.SystemModule
Imports System.Linq
Imports System.Linq.Expressions

Public Class CostHolderVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

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

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "SetHRPersonToCostHolder_SetHRPersonToCostHolderCostHolder_ListView" Or View.Id = "SetHRPersonToCostHolder_SetHRPersonToCostHolderHRPerson_ListView" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If
    End Sub

    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "SetHRPersonToCostHolder_SetHRPersonToCostHolderCostHolder_ListView" Or View.Id = "SetHRPersonToCostHolder_SetHRPersonToCostHolderHRPerson_ListView" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aSetCostHolderToHRPerson_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSetCostHolderToHRPerson.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _SetHRPersonToCostHolder As SetHRPersonToCostHolder = _onew.CreateObject(Of SetHRPersonToCostHolder)()

        Dim _CostHolder As CostHolder = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, CostHolder)
        Dim _HRPerson As HRPerson = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, HRPerson)

        If _HRPerson IsNot Nothing Then
            _SetHRPersonToCostHolder.Mode = 1
            _SetHRPersonToCostHolder.HRPerson = _onew.GetObject(_HRPerson)
            If View.CurrentObject Is Nothing Then
                _SetHRPersonToCostHolder.DateFrom = FirstDayOfYear(GetSQLTime(_onew.Session))
                _SetHRPersonToCostHolder.DateTo = LastDayOfYear(GetSQLTime(_onew.Session))

            Else
                _SetHRPersonToCostHolder.DateFrom = TryCast(View.CurrentObject, HRPersonCostHolder).DateFrom
                _SetHRPersonToCostHolder.DateTo = TryCast(View.CurrentObject, HRPersonCostHolder).DateTo
            End If
            e.View = Application.CreateDetailView(_onew, "SetHRPersonToCostHolder_DetailView", False, _SetHRPersonToCostHolder)
        ElseIf _CostHolder IsNot Nothing Then
            _SetHRPersonToCostHolder.Mode = 0
            _SetHRPersonToCostHolder.CostHolder = _onew.GetObject(_CostHolder)
            If View.CurrentObject Is Nothing Then
                _SetHRPersonToCostHolder.DateFrom = FirstDayOfYear(GetSQLTime(_onew.Session))
                _SetHRPersonToCostHolder.DateTo = LastDayOfYear(GetSQLTime(_onew.Session))

            Else
                _SetHRPersonToCostHolder.DateFrom = TryCast(View.CurrentObject, HRPersonCostHolder).DateFrom
                _SetHRPersonToCostHolder.DateTo = TryCast(View.CurrentObject, HRPersonCostHolder).DateTo
            End If
            e.View = Application.CreateDetailView(_onew, "SetHRPersonToCostHolder_DetailView", False, _SetHRPersonToCostHolder)
        End If

    End Sub

    Private Sub aSetCostHolderToHRPerson_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSetCostHolderToHRPerson.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _SetHRPersonToCostHolder As SetHRPersonToCostHolder = TryCast(e.PopupWindow.View.SelectedObjects(0), SetHRPersonToCostHolder)
        Dim _HRPersonCostHolder As HRPersonCostHolder
        Dim _HRPersonCostHolder_Collection As XPCollection(Of HRPersonCostHolder)
        Dim _SumWorkPercent As Double = 0
        Dim IsValid As Boolean = False
        Dim LastItemDate As Date

        Dim _HRPersonCostHolder_Query As New XPQuery(Of HRPersonCostHolder)(_ocur.Session)
        Dim query = From s In _HRPersonCostHolder_Query
                    Where s.HRPerson.Oid = _SetHRPersonToCostHolder.HRPerson.Oid And
                        s.WorkPercent > 0
                    Group s By s.DateFrom, s.DateTo Into g = Group
                    Order By DateFrom, DateTo
                    Select New With {.DateFrom = DateFrom, .DateTo = DateTo}

        For Each item In query
            If item.DateFrom = _SetHRPersonToCostHolder.DateFrom And item.DateTo = _SetHRPersonToCostHolder.DateTo Then
                IsValid = True
                Exit For
            End If
            LastItemDate = item.DateTo
        Next
        If query.Count = 0 Then
            IsValid = True
        End If

        If IsValid = False Then
            If DateDiff(DateInterval.Day, LastItemDate.Date, _SetHRPersonToCostHolder.DateFrom.Date) <> 1 Then
                Throw New UserFriendlyException("A beállított dátumoknak meg kell egyezniük egy már létező időszak dátumaival, vagy folytonosan kell követniük az utolsó időszakot!")
            End If
        End If

        If _SetHRPersonToCostHolder.Mode = 1 Then
            Dim _ListPropertyEditor As DevExpress.ExpressApp.Editors.ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("SetHRPersonToCostHolderCostHolder")

            For Each _SetHRPersonToCostHolderCostHolder In _ListPropertyEditor.ListView.CollectionSource.Collection
                If _SetHRPersonToCostHolderCostHolder.WorkPercent < 0 Or _SetHRPersonToCostHolderCostHolder.WorkPercent > 100 Then
                    Throw New UserFriendlyException("Az beállított százalékos érték nem lehet nagyobb 100-nál és nem lehet negatív!")
                End If

                _SumWorkPercent = _SumWorkPercent + _SetHRPersonToCostHolderCostHolder.WorkPercent
            Next

            If _SumWorkPercent <> 100 Then
                Throw New UserFriendlyException("A beállított százalékos értékek összege csak 100 lehet!")
            Else
                For Each _SetHRPersonToCostHolderCostHolder In _ListPropertyEditor.ListView.CollectionSource.Collection

                    _HRPersonCostHolder = _ocur.FindObject(Of HRPersonCostHolder)(CriteriaOperator.Parse("CostHolder.Oid=? And HRPerson.Oid=? And DateFrom=? And DateTo=?", _SetHRPersonToCostHolderCostHolder.CostHolder.Oid, _SetHRPersonToCostHolder.HRPerson.Oid, _SetHRPersonToCostHolder.DateFrom, _SetHRPersonToCostHolder.DateTo))

                    If _HRPersonCostHolder Is Nothing Then
                        If _SetHRPersonToCostHolderCostHolder.WorkPercent > 0 Then
                            _HRPersonCostHolder = _ocur.CreateObject(Of HRPersonCostHolder)()

                            _HRPersonCostHolder.CostHolder = _ocur.GetObjectByKey(Of CostHolder)(_SetHRPersonToCostHolderCostHolder.CostHolder.Oid)
                            _HRPersonCostHolder.HRPerson = _ocur.GetObjectByKey(Of HRPerson)(_SetHRPersonToCostHolder.HRPerson.Oid)
                            _HRPersonCostHolder.DateFrom = _SetHRPersonToCostHolder.DateFrom
                            _HRPersonCostHolder.DateTo = _SetHRPersonToCostHolder.DateTo
                            _HRPersonCostHolder.WorkPercent = _SetHRPersonToCostHolderCostHolder.WorkPercent
                        End If
                    Else
                        If _SetHRPersonToCostHolderCostHolder.WorkPercent >= 0 Then
                            _HRPersonCostHolder.WorkPercent = _SetHRPersonToCostHolderCostHolder.WorkPercent
                        End If
                    End If
                Next
                _ocur.CommitChanges()

            End If
        End If

        If _SetHRPersonToCostHolder.Mode = 0 Then
            Dim _ListPropertyEditor As DevExpress.ExpressApp.Editors.ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("SetHRPersonToCostHolderHRPerson")

            For Each _SetHRPersonToCostHolderHRPerson In _ListPropertyEditor.ListView.CollectionSource.Collection
                _SumWorkPercent = 0

                If _SetHRPersonToCostHolderHRPerson.WorkPercent < 0 Or _SetHRPersonToCostHolderHRPerson.WorkPercent > 100 Then
                    Throw New UserFriendlyException("Az beállított százalékos érték nem lehet nagyobb 100-nál és nem lehet negatív!")
                End If

                _HRPersonCostHolder_Collection = New XPCollection(Of HRPersonCostHolder)(_ocur.Session, CriteriaOperator.Parse("HRPerson.Oid=? And DateFrom=? And DateTo=?", _SetHRPersonToCostHolderHRPerson.HRPerson.Oid, _SetHRPersonToCostHolder.DateFrom, _SetHRPersonToCostHolder.DateTo))

                For Each _HRPersonCostHolder_Member In _HRPersonCostHolder_Collection
                    If _HRPersonCostHolder_Member.CostHolder.Oid = _SetHRPersonToCostHolder.CostHolder.Oid Then
                        _SumWorkPercent = _SumWorkPercent + _SetHRPersonToCostHolderHRPerson.WorkPercent
                    Else
                        _SumWorkPercent = _SumWorkPercent + _HRPersonCostHolder_Member.WorkPercent
                    End If
                Next

                If _SumWorkPercent <> 100 And _SumWorkPercent <> 0 Then
                    Dim _WrongUserName As String = _SetHRPersonToCostHolderHRPerson.HRPerson.displayname
                    Throw New UserFriendlyException("A következő munkavállaló foglaltsága nagyobb 100 százaléknál: " & _WrongUserName)
                End If
            Next

            For Each _SetHRPersonToCostHolderHRPerson In _ListPropertyEditor.ListView.CollectionSource.Collection

                _HRPersonCostHolder = _ocur.FindObject(Of HRPersonCostHolder)(CriteriaOperator.Parse("CostHolder.Oid=? And HRPerson.Oid=? And DateFrom=? And DateTo=?", _SetHRPersonToCostHolder.CostHolder.Oid, _SetHRPersonToCostHolderHRPerson.HRPerson.Oid, _SetHRPersonToCostHolder.DateFrom, _SetHRPersonToCostHolder.DateTo))

                If _HRPersonCostHolder Is Nothing Then
                    If _SetHRPersonToCostHolderHRPerson.WorkPercent > 0 Then
                        _HRPersonCostHolder = _ocur.CreateObject(Of HRPersonCostHolder)()

                        _HRPersonCostHolder.CostHolder = _ocur.GetObjectByKey(Of CostHolder)(_SetHRPersonToCostHolder.CostHolder.Oid)
                        _HRPersonCostHolder.HRPerson = _ocur.GetObjectByKey(Of HRPerson)(_SetHRPersonToCostHolderHRPerson.HRPerson.Oid)
                        _HRPersonCostHolder.DateFrom = _SetHRPersonToCostHolder.DateFrom
                        _HRPersonCostHolder.DateTo = _SetHRPersonToCostHolder.DateTo
                        _HRPersonCostHolder.WorkPercent = _SetHRPersonToCostHolderHRPerson.WorkPercent
                    End If
                Else
                    If _SetHRPersonToCostHolderHRPerson.WorkPercent > 0 Then
                        _HRPersonCostHolder.WorkPercent = _SetHRPersonToCostHolderHRPerson.WorkPercent
                    End If
                End If
            Next
            _ocur.CommitChanges()


        End If

    End Sub
End Class
