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.ExpressApp.Utils
Imports DevExpress.Xpo
Imports System.Linq
Imports DevExpress.Persistent.BaseImpl

Public Class OrderInHeader_PermissionVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "OrderInHeader_ListView_Permission" Then

            Frame.GetController(Of RefreshController).RefreshAction.Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)

            Dim _CollectionSource As CollectionSource = TryCast(View, ListView).CollectionSource
            _CollectionSource.BeginUpdateCriteria()
            _CollectionSource.Criteria("Default") = CriteriaOperator.Parse("1=2")
            _CollectionSource.EndUpdateCriteria()
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "OrderInHeader_ListView_Permission" Then
            Frame.GetController(Of RefreshController).RefreshAction.Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aOrderInHeader_Permission_Refresh_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aOrderInHeader_Permission_Refresh.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _CollectionSource As CollectionSource = TryCast(View, ListView).CollectionSource

            Dim _OrderInHeader_Collection As New XPCollection(Of OrderInHeader)(_ocur.Session, CriteriaOperator.Parse("IsEditable=True AND OrderInParameters.NeedPermission=True"))

            Dim _Filter As String = "Oid in("

            For Each _OrderInHeader As OrderInHeader In _OrderInHeader_Collection
                If _OrderInHeader.SubjectToApproval(_OrderInHeader, _ocur) Then

                    _Filter += "'" + _OrderInHeader.Oid.ToString + "',"
                End If
            Next

            _Filter = _Filter.Remove(_Filter.Length - 1, 1)
            _Filter += ")"

            _CollectionSource.BeginUpdateCriteria()
            _CollectionSource.Criteria.Remove("Default")
            _CollectionSource.Criteria.Remove("Filter")
            If _Filter.Length > 8 Then
                _CollectionSource.Criteria.Add("Filter", CriteriaOperator.Parse(_Filter))
            Else
                _CollectionSource.Criteria.Add("Filter", CriteriaOperator.Parse("1=2"))
            End If
            _CollectionSource.EndUpdateCriteria()

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub aOrderInHeader_SetToApproval_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aOrderInHeader_SetToApproval.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OrderInHeader As OrderInHeader = TryCast(View.SelectedObjects(0), OrderInHeader)
            Dim _User As User = TryCast(SecuritySystem.CurrentUser, User)
            If _OrderInHeader.OrderInParameters.NeedPermissionHRPerson IsNot Nothing Then
                If _OrderInHeader.OrderInParameters.NeedPermissionHRPerson.User.Oid = _User.Oid Then
                    _OrderInHeader.PermissionAllowed = Not _OrderInHeader.PermissionAllowed

                    _ocur.CommitChanges()
                Else
                    Throw New Exception(String.Format("Ön nem jogosult az ellenőrzésre a(z) {0} megrendelésnél!", _OrderInHeader.DocumentNumber))
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\NuvolarExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
End Class
