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.ConditionalAppearance
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.CloneObject
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Xpo
Imports System.IO
Imports DevExpress.ExpressApp.Reports
Imports System.Net.Mail
Imports System.Reflection

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

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

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", False)
        Frame.GetController(Of QuestionVC).aCreateAnswer.Active.SetItemValue("", False)
        Frame.GetController(Of QuestionVC).aCloseQuestion.Active.SetItemValue("", False)

        Frame.GetController(Of QuestionVC).aCreateQuestion.Active.SetItemValue("", False)
        Frame.GetController(Of QuestionVC).aCreateWorkSheetfromQuestion.Active.SetItemValue("", False)
        Frame.GetController(Of QuestionVC).aSetUserResponsibleQuestion.Active.SetItemValue("", False)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Question As Question

        If View.Id = "Question_WorkSheet_Header_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id Like "Question_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aCreateWorkSheetfromQuestion.Active.SetItemValue("", True)
            Frame.GetController(Of QuestionVC).aCreateAnswer.Active.SetItemValue("", True)
            Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", True)
            Frame.GetController(Of QuestionVC).aSetUserResponsibleQuestion.Active.SetItemValue("", True)
            _AppearanceController = Frame.GetController(Of AppearanceController)()

            If _AppearanceController IsNot Nothing Then
                AddHandler _AppearanceController.CustomApplyAppearance, AddressOf _AppearanceController_CustomApplyAppearance
                AddHandler _AppearanceController.AppearanceApplied, AddressOf _AppearanceController_AppearanceApplied
            End If
            AddHandler View.SelectionChanged, AddressOf Question_ListView_SelectionChanged
        End If

        If View.Id = "Question_ListView_User" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of Nuvolar.Module.AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
            TryCast(View, ListView).CollectionSource.Criteria("ByUser") = CriteriaOperator.Parse("User = ? OR AliasUser=?", _ocur.GetObject(SecuritySystem.CurrentUser), _ocur.GetObject(SecuritySystem.CurrentUser))
            Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            _AppearanceController = Frame.GetController(Of AppearanceController)()
            If _AppearanceController IsNot Nothing Then
                AddHandler _AppearanceController.CustomApplyAppearance, AddressOf _AppearanceController_CustomApplyAppearance
                AddHandler _AppearanceController.AppearanceApplied, AddressOf _AppearanceController_AppearanceApplied
            End If
        End If

        If View.Id = "Question_ListView_Customers" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", True)
            Frame.GetController(Of RefreshController).Active.SetItemValue("", False)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))
            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("Customers = ?", _Contacts.Customers)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
            End If
            _AppearanceController = Frame.GetController(Of AppearanceController)()

            If _AppearanceController IsNot Nothing Then
                AddHandler _AppearanceController.CustomApplyAppearance, AddressOf _AppearanceController_CustomApplyAppearance
                AddHandler _AppearanceController.AppearanceApplied, AddressOf _AppearanceController_AppearanceApplied
            End If
        End If

        If View.Id = "Question_WorkItem_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If
        If View.Id = "QuestionClosePopup_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "Question_ListView_Subcontractor" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aCreateWorkSheetfromQuestion.Active.SetItemValue("", True)

            Dim _Criteria As String = ""

            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))
            Dim _Contact_Collection As XPCollection(Of Contacts) = New XPCollection(Of Contacts)(_ocur.Session, CriteriaOperator.Parse("Customers=?", _Contacts.Customers))


            If _Contacts IsNot Nothing Then
                If _Contact_Collection.Count > 0 Then
                    For ik = 0 To _Contact_Collection.Count - 1
                        If ik < _Contact_Collection.Count - 1 Then
                            _Criteria += "UserResponsible.Oid = " + "'" + _Contact_Collection(ik).User.Oid.ToString + "'" + " Or "
                        Else
                            _Criteria += "UserResponsible.Oid = " + "'" + _Contact_Collection(ik).User.Oid.ToString + "'"
                        End If
                    Next
                Else
                    _Criteria = "1=2"
                End If

                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("ByUserResponsible") = CriteriaOperator.Parse(_Criteria)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("ByUserResponsible") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("ByUserResponsible") = CriteriaOperator.Parse("1=2")
            End If
        End If
        '-------------- DetailViewZ---------------------------

        If View.Id = "Question_DetailView" Or View.Id = "Question_DetailView_Info" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If

        If View.Id = "Question_DetailView" Then

            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aCreateQuestion.Active.SetItemValue("", True)
            If SecuritySystem.CurrentUserName <> "ivan.szabo" Then Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)

        End If

        If View.Id = "Question_DetailView_User" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aCreateQuestion.Active.SetItemValue("", True)
            If SecuritySystem.CurrentUserName <> "ivan.szabo" Then Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If

        If View.Id = "Question_DetailView_Info" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", False)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aCreateQuestion.Active.SetItemValue("", False)
            If SecuritySystem.CurrentUserName <> "ivan.szabo" Then Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)

            _Question = TryCast(View.SelectedObjects(0), Question)
            If _Question IsNot Nothing Then
                If _Question.QuestionClosed = eQuestionClosed.eClosed Then Frame.GetController(Of QuestionVC).aCloseQuestion.Active.SetItemValue("", False)
                Dim _User As User = _ocur.GetObject(SecuritySystem.CurrentUser)

                If _Question.UserCreated.Oid <> _User.Oid Then
                    If _Question.AliasUser IsNot Nothing Then
                        If _Question.AliasUser.Email <> _User.Email Then
                            Frame.GetController(Of QuestionVC).aCloseQuestion.Active.SetItemValue("", False)
                            Frame.GetController(Of QuestionVC).aCreateReply.Active.SetItemValue("", False)
                        Else
                            Frame.GetController(Of QuestionVC).aCreateReply.Active.SetItemValue("", True)
                            If _Question.QuestionClosed <> eQuestionClosed.eClosed Then Frame.GetController(Of QuestionVC).aCloseQuestion.Active.SetItemValue("", True)
                        End If
                    Else
                        Frame.GetController(Of QuestionVC).aCloseQuestion.Active.SetItemValue("", False)
                        Frame.GetController(Of QuestionVC).aCreateReply.Active.SetItemValue("", False)
                    End If
                Else
                    Frame.GetController(Of QuestionVC).aCreateReply.Active.SetItemValue("", True)
                    If _Question.QuestionClosed <> eQuestionClosed.eClosed Then Frame.GetController(Of QuestionVC).aCloseQuestion.Active.SetItemValue("", True)
                End If
            End If
        End If

        If View.Id = "WorkSheet_Header_DetailView" Then
            Frame.GetController(Of QuestionVC).aCreateWorkSheetfromQuestion.Active.SetItemValue("", False)
        End If

        If View.Id = "Reply_DetailView" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            If SecuritySystem.CurrentUserName <> "ivan.szabo" Then Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
    End Sub
    Protected Sub aCloseQuestion_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCloseQuestion.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Events As Events
        Dim _Question As Question = TryCast(View.SelectedObjects(0), Question)
        Dim _QuestionClosePopup As QuestionClosePopup = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), QuestionClosePopup))

        If _Question IsNot Nothing Then
            _Question.QuestionStatus = eQuestionStatus.eProcessedCustomerClosed
            _Question.QuestionClosed = eQuestionClosed.eClosed
            _Question.Save()

            _Events = _ocur.CreateObject(Of Events)()
            _Events.Question = _Question
            _Events.QuestionStatus = eQuestionStatus.eProcessedCustomerClosed
            _Events.Description = _QuestionClosePopup.Description
            _Events.Save()
            _Events.EventDirection = eEventDirection.eIn

            _ocur.CommitChanges()

            Try
                Dim _WebParameters As WebParameters = _ocur.FindObject(Of WebParameters)(CriteriaOperator.Parse("1=1"))
                If _WebParameters Is Nothing Then Throw New Exception("*Nincsnek e-mail paraméterek beállítva!")
                If _WebParameters.SMTPHost Is Nothing Then Throw New Exception("*Nincs SMTP server beállítva!")
                If _WebParameters.SMTPUserName = "" Then Throw New Exception("*Nincs felhasználói név beállítva!")
                If _WebParameters.SMTPPort = 0 Then Throw New Exception("*Nincs SMTP port beállítva!")



                Dim _MailMessage As New MailMessage
                Dim _SmtpClient As SmtpClient = New SmtpClient(_WebParameters.SMTPHost)
                _MailMessage.From = New MailAddress(_WebParameters.Email_SupportAdmin)
                _MailMessage.To.Add(_Question.UserCreated.Email)
                _MailMessage.Subject = _Question.DocumentNumber + " számú bejelentés"
                _MailMessage.IsBodyHtml = True

                Dim _EmailBody As String = _WebParameters.HTMLMailTemplate_QuestionStatusUpdate.HTMLBody
                _EmailBody = String.Format(_EmailBody, _Question.UserCreated.FullName, _Question.DocumentNumber, DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Enums\Nuvolar.Module.eQuestionStatus", _Question.QuestionStatus.ToString) + _
                                               vbNewLine, TryCast(SecuritySystem.CurrentUser, User).FullName)

                _EmailBody = _EmailBody + vbNewLine + _Question.HTMLEvents
                _MailMessage.Body = _EmailBody


                _SmtpClient.Port = _WebParameters.SMTPPort
                _SmtpClient.Credentials = New System.Net.NetworkCredential(_WebParameters.SMTPUserName, _WebParameters.SMTPPassword)
                _SmtpClient.EnableSsl = _WebParameters.EnableSsl

                _SmtpClient.Send(_MailMessage)

            Catch ex As Exception
                Dim sf As StackFrame = New StackFrame()
                Dim _ErrorLog As ErrorLog = _ocur.CreateObject(Of ErrorLog)()
                _ErrorLog.Description = ex.Message
                _ErrorLog.SubName = TryCast(sf.GetMethod(), MethodBase).Name
                _ocur.CommitChanges()
            End Try

            Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
            If nestedFrame IsNot Nothing Then
                Dim parentView As View = nestedFrame.View
                parentView.ObjectSpace.Refresh()
            End If
        End If
    End Sub
    'Protected Sub aCreateWorkSheetfromQuestion_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateWorkSheetfromQuestionqqq.Execute
    '    Try
    '        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
    '        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

    '        Dim _Question As Question = TryCast(View.SelectedObjects(0), Question)

    '        If _Question Is Nothing Then Throw New Exception("*Nincs kiválasztott elem!")

    '        Dim _WorkSheet_Header As WorkSheet_Header = _onew.CreateObject(Of WorkSheet_Header)()
    '        _WorkSheet_Header.Question = _onew.GetObject(_Question)
    '        _WorkSheet_Header.WorkSheetState = eWorkSheetState.eOpened

    '        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "WorkSheet_Header_DetailView", True, _WorkSheet_Header)
    '    Catch exp As Exception
    '        MsgBox(exp.Message)
    '    End Try
    'End Sub

    Protected Sub aCreateWorkSheetfromQuestion_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateWorkSheetfromQuestion.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _WorkSheetfromQuestion_PopUp As WorkSheetfromQuestion_PopUp = _onew.CreateObject(Of WorkSheetfromQuestion_PopUp)()

        e.View = Application.CreateDetailView(_onew, "WorkSheetfromQuestion_PopUp_DetailView", False, _WorkSheetfromQuestion_PopUp)
    End Sub
    Protected Sub aCreateWorkSheetfromQuestion_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateWorkSheetfromQuestion.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

        Dim _Question As Question = TryCast(View.SelectedObjects(0), Question)
        Dim _WorkSheet_Header As WorkSheet_Header = _onew.CreateObject(Of WorkSheet_Header)()
        Dim _WorkSheetfromQuestion_PopUp As WorkSheetfromQuestion_PopUp = _onew.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), WorkSheetfromQuestion_PopUp))

        _WorkSheet_Header.Question = _onew.GetObject(_Question)
        _WorkSheet_Header.Customers = _onew.GetObjectByKey(Of Customers)(_Question.Customers.Oid)

        _WorkSheet_Header.WorkSheetState = eWorkSheetState.eOpened
        _WorkSheet_Header.CustomersFrom = _onew.GetObject(_WorkSheetfromQuestion_PopUp.CustomersFrom)
        _WorkSheet_Header.WorkSheetType = _onew.GetObject(_WorkSheetfromQuestion_PopUp.WorkSheetType)
        _WorkSheet_Header.DateCreated = GetSQLTime(_onew.Session)

        Dim _ShowViewParameters As New ShowViewParameters
        Dim _ShowViewSource As New ShowViewSource(Frame, Nothing)

        _ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "WorkSheet_Header_DetailView", True, _WorkSheet_Header)
        _ShowViewParameters.NewWindowTarget = NewWindowTarget.MdiChild
        _ShowViewParameters.TargetWindow = TargetWindow.NewWindow
        Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource)
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", False)
        Frame.GetController(Of QuestionVC).aCreateAnswer.Active.SetItemValue("", False)
        Frame.GetController(Of QuestionVC).aCloseQuestion.Active.SetItemValue("", False)
        Frame.GetController(Of QuestionVC).aCreateQuestion.Active.SetItemValue("", False)
        Frame.GetController(Of QuestionVC).aCreateWorkSheetfromQuestion.Active.SetItemValue("", False)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Question As Question

        If View.Id Like "Question_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Frame.GetController(Of QuestionVC).aCreateWorkSheetfromQuestion.Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aCreateAnswer.Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", False)
            _AppearanceController = Frame.GetController(Of AppearanceController)()

            If _AppearanceController IsNot Nothing Then
                AddHandler _AppearanceController.CustomApplyAppearance, AddressOf _AppearanceController_CustomApplyAppearance
                AddHandler _AppearanceController.AppearanceApplied, AddressOf _AppearanceController_AppearanceApplied
            End If
        End If

        If View.Id = "Question_ListView_User" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of Nuvolar.Module.AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
            TryCast(View, ListView).CollectionSource.Criteria("ByUser") = CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser))
            Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            _AppearanceController = Frame.GetController(Of AppearanceController)()
            If _AppearanceController IsNot Nothing Then
                AddHandler _AppearanceController.CustomApplyAppearance, AddressOf _AppearanceController_CustomApplyAppearance
                AddHandler _AppearanceController.AppearanceApplied, AddressOf _AppearanceController_AppearanceApplied
            End If
        End If

        If View.Id = "Question_ListView_Customers" Then
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", False)
            Frame.GetController(Of RefreshController).Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Dim _User As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))
            If _User IsNot Nothing Then
                If _User.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("Customers = ?", _User.Customers)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("ByPartner") = CriteriaOperator.Parse("1=2")
            End If
            _AppearanceController = Frame.GetController(Of AppearanceController)()

            If _AppearanceController IsNot Nothing Then
                AddHandler _AppearanceController.CustomApplyAppearance, AddressOf _AppearanceController_CustomApplyAppearance
                AddHandler _AppearanceController.AppearanceApplied, AddressOf _AppearanceController_AppearanceApplied
            End If
        End If

        If View.Id = "Question_WorkItem_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController).Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
        End If
        If View.Id = "QuestionClosePopup_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "Question_ListView_Subcontractor" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Frame.GetController(Of QuestionVC).aCreateWorkSheetfromQuestion.Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aCreateAnswer.Active.SetItemValue("", False)
            Frame.GetController(Of QuestionVC).aViewQuestionInfo.Active.SetItemValue("", False)
        End If
        '-------------- DetailViewZ---------------------------

        If View.Id = "Question_DetailView" Or View.Id = "Question_DetailView_Info" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
        End If

        If View.Id = "Question_DetailView" Then

            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", True)
            Frame.GetController(Of QuestionVC).aCreateQuestion.Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)

        End If

        If View.Id = "Question_DetailView_User" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", True)
            Frame.GetController(Of QuestionVC).aCreateQuestion.Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If

        If View.Id = "Question_DetailView_Info" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndCloseAction.Active.SetItemValue("", True)
            Frame.GetController(Of ModificationsController).SaveAndNewAction.Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of QuestionVC).aCreateQuestion.Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)

            _Question = TryCast(View.SelectedObjects(0), Question)
            If _Question IsNot Nothing Then
                If _Question.QuestionClosed = eQuestionClosed.eClosed Then Frame.GetController(Of QuestionVC).aCloseQuestion.Active.SetItemValue("", False)
                Dim _User As User = _ocur.GetObject(SecuritySystem.CurrentUser)
                If _User IsNot Nothing Then
                    If _Question.UserCreated.Email <> _User.Email Then
                        Frame.GetController(Of QuestionVC).aCloseQuestion.Active.SetItemValue("", False)
                        Frame.GetController(Of QuestionVC).aCreateReply.Active.SetItemValue("", False)
                    End If
                End If
            End If
        End If

        If View.Id = "WorkSheet_Header_DetailView" Then
            Frame.GetController(Of QuestionVC).aCreateWorkSheetfromQuestion.Active.SetItemValue("", False)
        End If

        If View.Id = "Reply_DetailView" Then
            Frame.GetController(Of ModificationsController).Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
        End If

        Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
    End Sub

    Protected Overridable Sub CustomizeDisabledEditorsAppearance(ByVal e As ApplyAppearanceEventArgs)
    End Sub
    Private Sub _AppearanceController_AppearanceApplied(ByVal sender As Object, ByVal e As ApplyAppearanceEventArgs)
        If e.AppearanceObject.Enabled = False Then
            CustomizeDisabledEditorsAppearance(e)
        End If
    End Sub
    Private Sub _AppearanceController_CustomApplyAppearance(ByVal sender As Object, ByVal e As ApplyAppearanceEventArgs)
        'If (TypeOf View Is ListView) AndAlso (e.ItemType = AppearanceItemType.ViewItem.ToString()) AndAlso (e.ItemName = "Category") AndAlso (e.ContextObjects.Length > 0) Then
        '    If View.SelectedObjects.Contains(e.ContextObjects(0)) Then
        '        CType(e.Item, IAppearanceFormat).FontColor = System.Drawing.Color.Black
        '        e.Handled = True
        '    End If
        'End If
        'Dim _Question As Question = TryCast(e.ContextObjects(0), Question)
        'If (TypeOf View Is ListView) Then
        '    If _Question IsNot Nothing Then
        '        If _Question.QuestionStatus = eQuestionStatus.eActive Then
        '            CType(e.Item, IAppearanceFormat).FontStyle = Drawing.FontStyle.Bold
        '            e.Handled = True
        '        End If
        '    End If
        'End If
    End Sub

    Private Sub Question_ListView_SelectionChanged(sender As Object, e As EventArgs)
        Dim _Question As Question
        If _noselect Then Exit Sub

        If View Is Nothing Then Exit Sub
        If View.SelectedObjects.Count > 0 Then
            _selection.Clear()
            For Each _Question In View.SelectedObjects
                _selection.Add(_Question)
            Next
        End If

    End Sub
    '---Action codeZ---
    Protected Sub aCreateAnswer_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateAnswer.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Question As Question = View.SelectedObjects(0)
        Dim _Answer = _onew.CreateObject(Of Answer)()
        If _Question IsNot Nothing Then
            _Answer.Question = _onew.GetObject(_Question)

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Answer_DetailView", True, _Answer)
        End If

    End Sub
    Protected Sub aCreateReply_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateReply.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Question As Question = View.SelectedObjects(0)
        Dim _Reply As Reply

        If _Question IsNot Nothing Then
            _Reply = _onew.CreateObject(Of Reply)()
            _Reply.Question = _onew.GetObject(_Question)
            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Reply_DetailView", True, _Reply)
        End If

    End Sub
    Protected Sub aCreateQuestion_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateQuestion.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Question As Question = TryCast(View.SelectedObjects(0), Question)
        Dim _Events As Events
        If _Question IsNot Nothing Then
            If _Question.AliasUser IsNot Nothing Then
                Dim _Contacts As Contacts
                If _Question.AliasUser IsNot Nothing Then
                    _Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User.Oid=?", _Question.AliasUser.Oid))
                Else
                    _Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User.Oid=?", _Question.User.Oid))
                End If
                If _Contacts IsNot Nothing Then
                    _Question.Customers = _Contacts.Customers
                    _Question.User = _Question.AliasUser
                End If
            End If
            Replace(Replace(_Question.Description, "file:///P:/", "http://sis-r.hu/user/images/userimages/"), "\", "/")
            _ocur.CommitChanges()

            _Events = _onew.CreateObject(Of Events)()
            _Events.Question = _onew.GetObject(_Question)
            _Events.Description = _Question.Description
            If _Question.Attachment IsNot Nothing Then _Events.Attachment = _onew.GetObject(_Question.Attachment)
            _Events.Save()
            _Events.EventDirection = eEventDirection.eIn
            _onew.CommitChanges()

            Try
                Dim _WebParameters As WebParameters = _ocur.FindObject(Of WebParameters)(CriteriaOperator.Parse("1=1"))
                If _WebParameters Is Nothing Then Throw New Exception("*Nincsnek e-mail paraméterek beállítva!")
                If _WebParameters.SMTPHost Is Nothing Then Throw New Exception("*Nincs SMTP server beállítva!")
                If _WebParameters.SMTPUserName = "" Then Throw New Exception("*Nincs felhasználói név beállítva!")
                If _WebParameters.SMTPPort = 0 Then Throw New Exception("*Nincs SMTP port beállítva!")


                Dim _MailMessage As New MailMessage
                Dim _SmtpClient As SmtpClient = New SmtpClient(_WebParameters.SMTPHost)
                _MailMessage.From = New MailAddress(_WebParameters.Email_SupportAdmin)
                _MailMessage.To.Add(_Question.User.Email)
                _MailMessage.Subject = _Question.DocumentNumber + " számú bejelentés"
                _MailMessage.IsBodyHtml = True

                Dim _EmailBody As String = _WebParameters.HTMLMailTemplate_QuestionUpdate.HTMLBody
                _EmailBody = String.Format(_EmailBody, _Question.UserCreated.FullName, _Question.ObjectCreated.ToString(), _Question.Description, TryCast(SecuritySystem.CurrentUser, User).FullName)

                _MailMessage.Body = _EmailBody

                _SmtpClient.Port = _WebParameters.SMTPPort
                _SmtpClient.Credentials = New System.Net.NetworkCredential(_WebParameters.SMTPUserName, _WebParameters.SMTPPassword)
                _SmtpClient.EnableSsl = _WebParameters.EnableSsl

                _SmtpClient.Send(_MailMessage)

                _MailMessage.To.Clear()
                _MailMessage.To.Add(_WebParameters.Email_SupportAdmin)
                _MailMessage.Subject = "Új kérdés"
                _MailMessage.Body = String.Format("{0} felhasználó új kérdést rögzített {1} számmal.", _Question.User.FullName, _Question.DocumentNumber)

                _SmtpClient.Send(_MailMessage)
            Catch ex As Exception
                Dim sf As StackFrame = New StackFrame()
                Dim _ErrorLog As ErrorLog = _ocur.CreateObject(Of ErrorLog)()
                _ErrorLog.Description = ex.Message
                _ErrorLog.SubName = TryCast(sf.GetMethod(), MethodBase).Name
                _ocur.CommitChanges()
            End Try

            e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Question_DetailView_Info", True, _onew.GetObject(_Question))
            View.Close()
        End If
    End Sub
    Protected Sub aViewQuestionInfo_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewQuestionInfo.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Question As Question = TryCast(View.SelectedObjects(0), Question)
        Dim _Events As Events
        Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))

        If _Question IsNot Nothing Then

            If _Question.QuestionStatus = eQuestionStatus.eActive Then
                If _Contacts IsNot Nothing Then
                    If _Contacts.Customers IsNot Nothing Then
                        If SecuritySystemHelper.IsUserInRoleCurrentUser("Supporter") Then

                            _Question.QuestionStatus = eQuestionStatus.eReviewed

                            _Events = _ocur.CreateObject(Of Events)()
                            _Events.Question = _Question
                            _Events.QuestionStatus = eQuestionStatus.eReviewed
                            _Events.Description = ""
                            _Events.EventDirection = eEventDirection.eOut

                            _ocur.CommitChanges()

                            Try
                                Dim _WebParameters As WebParameters = _ocur.FindObject(Of WebParameters)(CriteriaOperator.Parse("1=1"))
                                If _WebParameters Is Nothing Then Throw New Exception("*Nincsnek e-mail paraméterek beállítva!")
                                If _WebParameters.SMTPHost Is Nothing Then Throw New Exception("*Nincs SMTP server beállítva!")
                                If _WebParameters.SMTPUserName = "" Then Throw New Exception("*Nincs felhasználói név beállítva!")
                                If _WebParameters.SMTPPort = 0 Then Throw New Exception("*Nincs SMTP port beállítva!")


                                Dim _MailMessage As New MailMessage
                                Dim _Attachment As Attachment
                                Dim _SmtpClient As SmtpClient = New SmtpClient(_WebParameters.SMTPHost)
                                _MailMessage.From = New MailAddress(_WebParameters.Email_SupportAdmin)
                                _MailMessage.To.Add(_Question.UserCreated.Email)
                                _MailMessage.Subject = _Question.DocumentNumber + " számú bejelentés"
                                _MailMessage.IsBodyHtml = True

                                Dim _EmailBody As String = _WebParameters.HTMLMailTemplate_QuestionStatusUpdate.HTMLBody
                                _EmailBody = String.Format(_EmailBody, _Question.UserCreated.FullName, _Question.DocumentNumber, DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Enums\Nuvolar.Module.eQuestionStatus", _Question.QuestionStatus.ToString) + _
                                                               vbNewLine, TryCast(SecuritySystem.CurrentUser, User).FullName)
                                _EmailBody = _EmailBody + vbNewLine + CreateHTMLEventsMail(_Question, _ocur.Session)

                                _MailMessage.Body = _EmailBody
                                _MailMessage.Attachments.Clear()

                                _Attachment = New Attachment(GLOBAL_ApplicationStartupPath_Mail & "Images\iRedArrow.png")
                                _Attachment.ContentId = "c001"
                                _MailMessage.Attachments.Add(_Attachment)

                                _Attachment = New Attachment(GLOBAL_ApplicationStartupPath_Mail & "Images\iGreenArrow.png")
                                _Attachment.ContentId = "c002"
                                _MailMessage.Attachments.Add(_Attachment)


                                _SmtpClient.Port = _WebParameters.SMTPPort
                                _SmtpClient.Credentials = New System.Net.NetworkCredential(_WebParameters.SMTPUserName, _WebParameters.SMTPPassword)
                                _SmtpClient.EnableSsl = _WebParameters.EnableSsl

                                _SmtpClient.Send(_MailMessage)
                            Catch ex As Exception
                                Dim sf As StackFrame = New StackFrame()
                                Dim _ErrorLog As ErrorLog = _ocur.CreateObject(Of ErrorLog)()
                                _ErrorLog.Description = ex.Message
                                _ErrorLog.SubName = TryCast(sf.GetMethod(), MethodBase).Name
                                _ocur.CommitChanges()
                            End Try
                        End If
                    End If
                End If
            End If
        End If
        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Question_DetailView_Info", True, _onew.GetObject(_Question))
    End Sub
    Protected Sub aCloseQuestion_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCloseQuestion.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _QuestionClosePopup As QuestionClosePopup = _onew.CreateObject(Of QuestionClosePopup)()

        e.View = Application.CreateDetailView(_onew, "QuestionClosePopup_DetailView", False, _QuestionClosePopup)
    End Sub
    Private Sub aSetUserResponsibleQuestion_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSetUserResponsibleQuestion.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CollectionSource As New CollectionSource(_onew, GetType(User))

        _CollectionSource.BeginUpdateCriteria()
        _CollectionSource.Criteria.Clear()
        _CollectionSource.Criteria("1") = CriteriaOperator.Parse("1=2")
        _CollectionSource.EndUpdateCriteria()

        Dim _UserResponsibles_Collection As XPCollection(Of User) = New XPCollection(Of User)(_onew.Session, CriteriaOperator.Parse("1=1"))
        Dim _UserResponsibles_Back_Collection As New XPCollection(Of User)(_onew.Session, CriteriaOperator.Parse("1=2"))
        Dim _Need As Boolean = False
        For Each _ActUser As User In _UserResponsibles_Collection
            _Need = False
            If SecuritySystemHelper.IsUserInRole("Supporter", _ActUser) Then
                _Need = True
            Else
                Dim _Contacts As Contacts = _onew.FindObject(Of Contacts)(CriteriaOperator.Parse("User.Oid=?", _ActUser.Oid))
                If _Contacts IsNot Nothing Then
                    Dim _Customers As Customers = _Contacts.Customers

                    Dim _Subcontractor As Subcontractor = _onew.FindObject(Of Subcontractor)(CriteriaOperator.Parse("Customers=?", _Customers))
                    If _Subcontractor IsNot Nothing Then
                        _Need = True
                    End If
                End If
            End If
            _UserResponsibles_Back_Collection.Add(_ActUser)
        Next

        For Each _User As User In _UserResponsibles_Back_Collection
            _CollectionSource.Add(_User)
        Next

        _noselect = True
        e.View = Application.CreateListView("User_ListView_Find", _CollectionSource, True)
    End Sub
    Private Sub aSetUserResponsibleQuestion_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSetUserResponsibleQuestion.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _UserResponsible = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), User))
        Dim _Question_Selection As Question
        Dim _Question As Question
        Dim _Events As Events
        For Each _Question_Selection In _selection
            _Question = _ocur.GetObject(_Question_Selection)
            If _Question.UserResponsible Is Nothing Then
                _Events = _ocur.CreateObject(Of Events)()
                _Events.QuestionStatus = eQuestionStatus.eInProgress
                _Events.Question = _ocur.GetObject(_Question)
                _Events.Description = ""
                _Events.EventDirection = eEventDirection.eOut
            End If
            _Question.QuestionStatus = eQuestionStatus.eInProgress
            _Question.UserResponsible = _UserResponsible

        Next
        _ocur.CommitChanges()
        'View.Refresh()

        _noselect = False
    End Sub
    Protected Sub aOpenWorkSheet_Subcontractor_Question_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOpenWorkSheet_Subcontractor_Question.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Question As Question = _onew.GetObject(TryCast(View.SelectedObjects(0), Question))

        Dim _User As User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
        Dim _SubcontractorCustomers As Customers = _Contacts.Customers
        Dim _OpenWorkSheet_SubcontractorPopUp As OpenWorkSheet_SubcontractorPopUp = _onew.CreateObject(Of OpenWorkSheet_SubcontractorPopUp)()
        _OpenWorkSheet_SubcontractorPopUp.Question = _Question
        Dim _Subcontractor As Subcontractor = _onew.FindObject(Of Subcontractor)(CriteriaOperator.Parse("Customers.Oid=?", _SubcontractorCustomers.Oid))

        _OpenWorkSheet_SubcontractorPopUp.Subcontractor = _onew.GetObject(_Subcontractor)

        e.View = Application.CreateDetailView(_onew, "OpenWorkSheet_SubcontractorPopUp_DetailView", False, _OpenWorkSheet_SubcontractorPopUp)
    End Sub
    Protected Sub aOpenWorkSheet_Subcontractor_Question_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOpenWorkSheet_Subcontractor_Question.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OpenWorkSheet_SubcontractorPopUp As OpenWorkSheet_SubcontractorPopUp = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OpenWorkSheet_SubcontractorPopUp))
        Dim _Subcontractor As Subcontractor = _ocur.GetObjectByKey(Of Subcontractor)(_OpenWorkSheet_SubcontractorPopUp.Subcontractor.Oid)
        Dim _User As User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
        Dim _SubcontractorCustomers As Customers = _Contacts.Customers
        Dim _ReportData As ReportData
        Dim _Question As Question = TryCast(View.SelectedObjects(0), Question)

        Dim _WorkSheet_Header As WorkSheet_Header = _ocur.CreateObject(Of WorkSheet_Header)()
        _WorkSheet_Header.Customers = _Question.Customers
        _WorkSheet_Header.Question = _Question
        _WorkSheet_Header.CustomersFrom = _ocur.GetObjectByKey(Of CustomersFrom)(_OpenWorkSheet_SubcontractorPopUp.CustomersFrom.Oid)
        _WorkSheet_Header.WorkSheetType = _ocur.GetObjectByKey(Of WorkSheetType)(_OpenWorkSheet_SubcontractorPopUp.WorkSheetType.Oid)
        _WorkSheet_Header.DateExecution = _OpenWorkSheet_SubcontractorPopUp.DateCreated
        _WorkSheet_Header.DateCreated = GetSQLTime(_ocur.Session)
        _WorkSheet_Header.IsSubcontractor = True
        _WorkSheet_Header.SubcontractorCustomers = _SubcontractorCustomers
        _WorkSheet_Header.Description = _OpenWorkSheet_SubcontractorPopUp.Description
        _WorkSheet_Header.WorkPlace = _ocur.GetObjectByKey(Of WorkPlace)(_OpenWorkSheet_SubcontractorPopUp.WorkPlace.Oid)
        _WorkSheet_Header.WorkSheetState = eWorkSheetState.eOpened
        _WorkSheet_Header.CurrencyName = _ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
        _WorkSheet_Header.WorkHour = _OpenWorkSheet_SubcontractorPopUp.WorkHour
        _WorkSheet_Header.WorkDescription = _OpenWorkSheet_SubcontractorPopUp.WorkDescription
        _WorkSheet_Header.WorkUsedMaterialDescription = _OpenWorkSheet_SubcontractorPopUp.WorkUsedMaterialDescription
        _WorkSheet_Header.WorkDescriptionOther = _OpenWorkSheet_SubcontractorPopUp.WorkDescriptionOther
        _WorkSheet_Header.WorkTravelKm = _OpenWorkSheet_SubcontractorPopUp.WorkTravelKm


        _ocur.CommitChanges()
        _ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _WorkSheet_Header.WorkSheetType.ReportData.Oid), True)
        If _ReportData IsNot Nothing Then
            Dim _XafReport As XafReport = _ReportData.LoadReport(_ocur)

            Dim _Param_Colletion As DevExpress.XtraReports.Parameters.ParameterCollection = _XafReport.Parameters

            For Each _Param As DevExpress.XtraReports.Parameters.Parameter In _Param_Colletion
                If _Param.Name = "Workplace" Then
                    Dim _SubcontractorCustomers_WorkPLace As SubcontractorCustomers = _ocur.FindObject(Of SubcontractorCustomers) _
                                                                            (CriteriaOperator.Parse("Subcontractor=? AND Customers=?", _Subcontractor, _WorkSheet_Header.Customers))
                    If _SubcontractorCustomers_WorkPLace.Workplace Is Nothing Then
                        _Param.Value = "Nincs megadva!"
                    Else
                        _Param.Value = _SubcontractorCustomers_WorkPLace.Workplace.ShortName
                    End If
                End If
            Next

            Dim _FilterString As String = "[Oid]='" & _WorkSheet_Header.Oid.ToString & "'"
            _XafReport.FilterString = _FilterString
            Dim _FileName As String = Path.GetTempPath & _WorkSheet_Header.DocumentNumber & "_Munkalap.pdf"
            Dim _StreamFile As New FileStream(_FileName, FileMode.Create)

            _XafReport.ExportToPdf(_StreamFile)
            _StreamFile.Close()

            Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)

            _WorkSheet_Header.WorkSheetFile_Original = New FileData(_ocur.Session)
            _WorkSheet_Header.WorkSheetFile_Original.LoadFromStream(_WorkSheet_Header.DocumentNumber + "_Munkalap.pdf", _StreamFile2)
            _StreamFile2.Close()
            Process.Start(_FileName)
        End If
        _ocur.CommitChanges()

        'Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("Oid=?", _WorkSheet_Header.Oid), True)

        View.Close()
    End Sub

    Protected Sub aViewQuestionInfo_Subcontractor_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aViewQuestionInfo_Subcontractor.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Question As Question = TryCast(View.SelectedObjects(0), Question)

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Question_DetailView_Info", True, _onew.GetObject(_Question))
    End Sub
End Class
