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.ExpressApp.SystemModule
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.Editors
Imports System.IO
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.Persistent.BaseImpl

Public Class WorkSheet_VC
    Inherits DevExpress.ExpressApp.ViewController
    Private _selection As ArrayList
    Private _noselect As Boolean = False
    Private _eWorkRowType_ACT As eWorkRowType
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
        _selection = New ArrayList
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of WorkSheet_VC).aCloseWorkSheet_Subcontractor.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aOpenWorkSheet_Subcontractor.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aToTableWSR.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aToTableWSR_D.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aCreateWorksheetCostRows.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aRefreshWorksheetCostRows.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aCreateInvoiceFromWorksheetCost.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aReOpenWorksheetHeader.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aOpenSubcontractorInvoice.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aOpenWorkSheetFile.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aAddHRPerson_WorkSheet_Header.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aPrintWorkSheet.Active.SetItemValue("", False)

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

        If View.Id = "WorkSheet_Header_ListView_Subcontractor_Manage" Then
            'Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)

            Frame.GetController(Of WorkSheet_VC).aReOpenWorksheetHeader.Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aReOpenWorksheetHeader.TargetObjectsCriteria = "WorkSheetState = 'eClosed'"

            Frame.GetController(Of WorkSheet_VC).aOpenSubcontractorInvoice.Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aOpenSubcontractorInvoice.TargetObjectsCriteria = "SubcontractorInvoice is Not Null"

            Frame.GetController(Of WorkSheet_VC).aOpenWorkSheetFile.Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aOpenWorkSheetFile.TargetObjectsCriteria = "WorkSheetFile is Not Null"
        End If

        If View.Id = "WorkSheet_Header_ListView_Subcontractor" Or _
            View.Id = "WorkSheet_Header_ListView_Subcontractor_Closed" Or _
            View.Id = "WorkSheet_Header_ListView_Subcontractor_Ready" 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)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)

            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))
            If _Contacts IsNot Nothing Then
                Dim _Contacts_Collection As New XPCollection(Of Contacts)(_ocur.Session, CriteriaOperator.Parse("Customers=? and IsNull(User)=False", _Contacts.Customers))
                Dim _CS As CollectionSource = TryCast(View, ListView).CollectionSource

                Dim _Criteria1 As String = ""
                Dim _Criteria2 As String = ""

                If _Contacts_Collection.Count > 0 Then
                    For ik = 0 To _Contacts_Collection.Count - 1
                        If ik < _Contacts_Collection.Count - 1 Then
                            _Criteria1 += "UserCreated.Oid = " + "'" + _Contacts_Collection(ik).User.Oid.ToString + "'" + " Or "
                        Else
                            _Criteria1 += "UserCreated.Oid = " + "'" + _Contacts_Collection(ik).User.Oid.ToString + "'"
                        End If
                    Next
                Else
                    _Criteria1 = "1=2"
                End If
                'If Not _Criteria1.Contains("1=2") Then
                '    Dim _WorkSheet_Header_HRPerson_Collection As New XPCollection(Of WorkSheet_Header_HRPerson)(_ocur.Session, _
                '                                                             CriteriaOperator.Parse("HRPerson.User=?", _ocur.GetObject(SecuritySystem.CurrentUser)))
                '    For Each _WorkSheet_Header As WorkSheet_Header In _CS.Collection
                '        Dim _WorkSheet_Header_Find As WorkSheet_Header=


                '    Next
                'End If
                _CS.BeginUpdateCriteria()
                _CS.Criteria("FilteR") = CriteriaOperator.Parse(_Criteria1)
                _CS.EndUpdateCriteria()
            End If
        End If
        If View.Id = "WorkSheet_Header_ListView_Support" Then
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))
            Dim _CS As CollectionSource = TryCast(View, ListView).CollectionSource
            Dim _Criteria1 As String = "1=2"
            Dim _Criteria2 As String = ""

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    _Criteria1 = "Customers.Oid='" & _Contacts.Customers.Oid.ToString & "'"
                End If
            End If
            _CS.BeginUpdateCriteria()
            _CS.Criteria("FilteR") = CriteriaOperator.Parse(_Criteria1)
            _CS.EndUpdateCriteria()
        End If
        If View.Id = "WorkSheet_Header_ListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aPrintWorkSheet.Active.SetItemValue("", True)
        End If
        If View.Id = "WorkSheet_Header_WorkSheet_Rows_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            AddHandler View.SelectionChanged, AddressOf WorkSheet_Header_WorkSheet_Rows_ListView_SelectionChanged
        End If
        If View.Id = "WorkSheet_Header_StorageOutRows_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If
        If View.Id = "WorkSheet_Header_Worksheet_CostRows_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            AddHandler View.SelectionChanged, AddressOf WorkSheet_Header_Worksheet_CostRows_ListView_SelectionChanged
        End If
        If View.Id = "WorkSheet_Header_WorkSheet_ReopenReason_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)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "WorkSheet_Header_Worksheet_Documets_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If
        If View.Id = "CreateInvoiceFromWorksheetCost_PopUp_Worksheet_CostRows_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If
        If View.Id = "WorkSheet_Header_WorkSheet_Header_HRPerson_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aAddHRPerson_WorkSheet_Header.Active.SetItemValue("", True)
        End If
        If View.Id = "WorkSheet_Header_AddHRPerson_PopUp_ListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If

        If View.Id = "OpenWorkSheet_SubcontractorPopUp_WorkSheet_Header_Collection_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If

        If View.Id = "CRM_Opportunities_WorkSheet_Header_ListView" Then
            Frame.GetController(Of WorkSheet_VC).aPrintWorkSheet.Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If
        '----------------DeatilViewZ----------------------------------

        If View.Id = "OpenWorkSheet_SubcontractorPopUp_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "CloseWorkSheet_SubcontractorpopUp_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "WorkSheet_Header_DetailView_Subcontractor" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "WorkSheet_Header_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aToTableWSR.Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aToTableWSR_D.Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aCreateWorksheetCostRows.Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aRefreshWorksheetCostRows.Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aCreateInvoiceFromWorksheetCost.Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aPrintWorkSheet.Active.SetItemValue("", True)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        If View.Id = "WorkSheet_Header_ListView_Subcontractor_Manage" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)

            Frame.GetController(Of WorkSheet_VC).aReOpenWorksheetHeader.Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aOpenSubcontractorInvoice.Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aOpenWorkSheetFile.Active.SetItemValue("", False)
        End If
        If View.Id = "WorkSheet_Header_ListView_Subcontractor" Or _
            View.Id = "WorkSheet_Header_ListView_Subcontractor_Closed" 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)
            Frame.GetController(Of WorkSheet_VC).aCloseWorkSheet_Subcontractor.Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aOpenWorkSheet_Subcontractor.Active.SetItemValue("", False)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", False)
        End If
        If View.Id = "WorkSheet_Header_ListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If

        If View.Id = "WorkSheet_Header_WorkSheet_Rows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            RemoveHandler View.SelectionChanged, AddressOf WorkSheet_Header_WorkSheet_Rows_ListView_SelectionChanged
        End If
        If View.Id = "WorkSheet_Header_StorageOutRows_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
        End If
        If View.Id = "WorkSheet_Header_Worksheet_CostRows_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            RemoveHandler View.SelectionChanged, AddressOf WorkSheet_Header_Worksheet_CostRows_ListView_SelectionChanged
        End If
        If View.Id = "WorkSheet_Header_WorkSheet_ReopenReason_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)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "WorkSheet_Header_Worksheet_Documets_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
        End If
        If View.Id = "CreateInvoiceFromWorksheetCost_PopUp_Worksheet_CostRows_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
        End If
        If View.Id = "WorkSheet_Header_WorkSheet_Header_HRPerson_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aAddHRPerson_WorkSheet_Header.Active.SetItemValue("", False)
        End If
        If View.Id = "WorkSheet_Header_AddHRPerson_PopUp_ListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "OpenWorkSheet_SubcontractorPopUp_WorkSheet_Header_Collection_ListView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC).aGetAuditTrail.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
        End If
        '---------------------DetailViewZ----------------
        If View.Id = "OpenWorkSheet_SubcontractorPopUp_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "CloseWorkSheet_SubcontractorpopUp_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If
        If View.Id = "WorkSheet_Header_DetailView_Subcontractor" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
        End If
        If View.Id = "WorkSheet_Header_DetailView" Then
            Frame.GetController(Of NewObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
            Frame.GetController(Of DeleteObjectsViewController).Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aToTableWSR.Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aToTableWSR_D.Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aCreateWorksheetCostRows.Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aRefreshWorksheetCostRows.Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aCreateInvoiceFromWorksheetCost.Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aPrintWorkSheet.Active.SetItemValue("", False)
        End If

    End Sub
    Protected Overridable Sub aOpenWorkSheet_Subcontractor_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOpenWorkSheet_Subcontractor.CustomizePopupWindowParams
    End Sub
    Protected Overridable Sub aOpenWorkSheet_Subcontractor_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOpenWorkSheet_Subcontractor.Execute
    End Sub
    Protected Overridable Sub aCloseWorkSheet_Subcontractor_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCloseWorkSheet_Subcontractor.CustomizePopupWindowParams
    End Sub
    Protected Overridable Sub aCloseWorkSheet_Subcontractor_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCloseWorkSheet_Subcontractor.Execute
    End Sub
    '----------------WorksheetRows actions---------------------
    Private Sub aToTableWSR_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableWSR.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _WorkSheet_Header.row_Services Is Nothing Then
                If _WorkSheet_Header.row_Units Is Nothing Then Throw New Exception("*A mennyiségi egységet kötelezően meg  kell adni!")
                If _WorkSheet_Header.row_Quantity = 0 Then Throw New Exception("*A mennyiséget kötelezően meg  kell adni!")
                If _WorkSheet_Header.row_Description = "" Then Throw New Exception("*A leírást kötelezően meg  kell adni!")
            End If

            Dim _WorkSheet_Rows As WorkSheet_Rows = _ocur.CreateObject(Of WorkSheet_Rows)()

            _WorkSheet_Rows.WorkSheet_Header = _WorkSheet_Header

            If _WorkSheet_Header.row_Services IsNot Nothing Then
                _WorkSheet_Rows.RowGroup1 = _WorkSheet_Header.row_Services.RowGroup1
                _WorkSheet_Rows.RowGroup2 = _WorkSheet_Header.row_Services.RowGroup2
                _WorkSheet_Rows.Units = _WorkSheet_Header.row_Services.Units
                _WorkSheet_Rows.WorkRowType = _WorkSheet_Header.row_WorkRowType
                _WorkSheet_Rows.WorkType = _WorkSheet_Header.row_WorkType
                _WorkSheet_Rows.ShortName = _WorkSheet_Header.row_Services.ShortName
                _WorkSheet_Rows.Description = _WorkSheet_Header.row_Services.Description
                _WorkSheet_Rows.Quantity = _WorkSheet_Header.row_Quantity
                _WorkSheet_Rows.Services = _WorkSheet_Header.row_Services
                _WorkSheet_Rows.RowIndex = _WorkSheet_Header.WorkSheet_Rows.Count
            Else
                _WorkSheet_Rows.RowGroup1 = _WorkSheet_Header.row_RowGroup1
                _WorkSheet_Rows.RowGroup2 = _WorkSheet_Header.row_RowGroup2
                _WorkSheet_Rows.Units = _WorkSheet_Header.row_Units
                _WorkSheet_Rows.WorkRowType = _WorkSheet_Header.row_WorkRowType
                _WorkSheet_Rows.WorkType = _WorkSheet_Header.row_WorkType
                _WorkSheet_Rows.ShortName = _WorkSheet_Header.row_ShortName
                _WorkSheet_Rows.Description = _WorkSheet_Header.row_Description
                _WorkSheet_Rows.Quantity = _WorkSheet_Header.row_Quantity
                _WorkSheet_Rows.RowIndex = _WorkSheet_Header.WorkSheet_Rows.Count
            End If

            _ocur.CommitChanges()

            _WorkSheet_Header.row_RowGroup1 = ""
            _WorkSheet_Header.row_RowGroup2 = ""
            _WorkSheet_Header.row_Units = Nothing
            _eWorkRowType_ACT = _WorkSheet_Header.row_WorkRowType
            _WorkSheet_Header.row_WorkType = eWorkType.eGuarantee
            _WorkSheet_Header.row_ShortName = ""
            _WorkSheet_Header.row_Description = ""
            _WorkSheet_Header.row_Quantity = 0
            _WorkSheet_Header.row_Services = Nothing

            If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If
        Catch ext As Exception
            MsgBox(ext.Message)
        Finally
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            _WorkSheet_Header.row_WorkRowType = _eWorkRowType_ACT
            Dim ItemToFocus As ViewItem = TryCast(View, DetailView).FindItem("row_RowGroup1")
            If ItemToFocus IsNot Nothing Then
                Dim ItemControl As Object
                ItemControl = ItemToFocus.Control
                ItemControl.Focus()
            End If
        End Try
    End Sub
    Private Sub aToTableWSR_D_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableWSR_D.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            Dim _WorkSheet_Rows_Collection As ArrayList = New ArrayList
            Dim _WorkSheet_Rows_ListPropertyEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("WorkSheet_Rows")
            If _WorkSheet_Rows_ListPropertyEditor Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _WorkSheet_Rows_ListPropertyEditor.ListView.SelectedObjects.Count = 0 Then Throw New Exception("*Nincs kijelölve egyetlen sor sem!")

            For Each _WorkSheet_Rows In _WorkSheet_Rows_ListPropertyEditor.ListView.SelectedObjects
                Dim _WorkSheet_Rows_Act As WorkSheet_Rows = _WorkSheet_Rows
                For Each _WorkSheet_Rows_ActHeader As WorkSheet_Rows In _WorkSheet_Header.WorkSheet_Rows
                    If _WorkSheet_Rows_ActHeader.RowIndex > _WorkSheet_Rows_Act.RowIndex Then
                        _WorkSheet_Rows_ActHeader.RowIndex -= 1
                    End If
                Next
                _WorkSheet_Rows_Collection.Add(_WorkSheet_Rows)
            Next

            _ocur.Delete(_WorkSheet_Rows_Collection)
            _ocur.CommitChanges()

            If Frame.GetController(Of RefreshController).RefreshAction.Enabled Then
                Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
            End If
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
    '------------WorksheetCostRows actions----------------------
    Private Sub aCreateWorksheetCostRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCreateWorksheetCostRows.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a művelet megszakadt!")
            If _WorkSheet_Header.CurrencyName Is Nothing Then Throw New Exception("*Nincs beállítva pénznem!")
            Dim _Worksheet_CostRows_Del_Collection As ArrayList

            If _WorkSheet_Header.Worksheet_CostRows.Count > 0 Then
                _Worksheet_CostRows_Del_Collection = New ArrayList
                For Each _Worksheet_CostRows As Worksheet_CostRows In _WorkSheet_Header.Worksheet_CostRows
                    _Worksheet_CostRows_Del_Collection.Add(_Worksheet_CostRows)
                Next
                _ocur.Delete(_Worksheet_CostRows_Del_Collection)
            End If

            If _WorkSheet_Header.StorageOutRows.Count > 0 Then
                For Each _StorageOutRows As StorageOutRows In _WorkSheet_Header.StorageOutRows
                    Dim _Worksheet_CostRows As Worksheet_CostRows = _ocur.CreateObject(Of Worksheet_CostRows)()
                    _Worksheet_CostRows.RowIndex = _WorkSheet_Header.Worksheet_CostRows.Count
                    _Worksheet_CostRows.WorkSheet_Header = _WorkSheet_Header
                    _Worksheet_CostRows.StorageOutRows = _StorageOutRows

                    _Worksheet_CostRows.ShortName = _StorageOutRows.Products.ShortName
                    _Worksheet_CostRows.Description = _StorageOutRows.Products.Description
                    _Worksheet_CostRows.QTT = _StorageOutRows.QTT
                    _Worksheet_CostRows.Units = _StorageOutRows.Products.Units

                    _Worksheet_CostRows.FinalPriceHUF_Out = _StorageOutRows.Products.ListPriceOutHUF
                    _Worksheet_CostRows.FinalPriceDEV_Out = _StorageOutRows.Products.ListPriceOutDEV
                    _Worksheet_CostRows.SumPriceHUF_Out = _StorageOutRows.Products.ListPriceOutHUF * _Worksheet_CostRows.QTT
                    _Worksheet_CostRows.SumPriceDEV_Out = _StorageOutRows.Products.ListPriceOutDEV * _Worksheet_CostRows.QTT


                    Dim _DeliveryNoteInRows As DeliveryNoteInRows
                    Dim _StorageOutRowsInRows As StorageOutRowsInRows
                    _Worksheet_CostRows.SumPriceHUF_In = 0
                    _Worksheet_CostRows.SumPriceDEV_In = 0

                    For Each _StorageOutRowsInRows In _StorageOutRows.StorageOutRowsInRows
                        If _StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows IsNot Nothing Then
                            _DeliveryNoteInRows = _StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows
                            _Worksheet_CostRows.SumPriceHUF_In += (_DeliveryNoteInRows.ListPriceHUF - _DeliveryNoteInRows.DiscountPriceHUF) * _StorageOutRowsInRows.QTT
                            _Worksheet_CostRows.SumPriceDEV_In += (_DeliveryNoteInRows.ListPriceDEV - _DeliveryNoteInRows.DiscountPriceDEV) * _StorageOutRowsInRows.QTT
                        Else
                            _Worksheet_CostRows.SumPriceHUF_In += _StorageOutRows.Products.ListPriceInHUF * _StorageOutRowsInRows.QTT
                            _Worksheet_CostRows.SumPriceDEV_In += _StorageOutRows.Products.ListPriceInDEV * _StorageOutRowsInRows.QTT
                        End If
                    Next
                    _Worksheet_CostRows.FinalPriceHUF_In = _Worksheet_CostRows.SumPriceHUF_In / _Worksheet_CostRows.QTT
                    _Worksheet_CostRows.FinalPriceDEV_In = _Worksheet_CostRows.SumPriceDEV_In / _Worksheet_CostRows.QTT

                    _Worksheet_CostRows.DifferentHUF = (_Worksheet_CostRows.FinalPriceHUF_Out - _Worksheet_CostRows.FinalPriceHUF_In) * _Worksheet_CostRows.QTT
                    _Worksheet_CostRows.DifferentDEV = (_Worksheet_CostRows.FinalPriceDEV_Out - _Worksheet_CostRows.FinalPriceDEV_In) * _Worksheet_CostRows.QTT
                    If _WorkSheet_Header.CurrencyName.ShortName <> "HUF" Then
                        _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceDEV_In / _Worksheet_CostRows.FinalPriceDEV_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                    Else
                        _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceHUF_In / _Worksheet_CostRows.FinalPriceHUF_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                    End If
                Next
            End If

            If _WorkSheet_Header.WorkSheet_Rows.Count > 0 Then
                For Each _WorkSheet_Rows As WorkSheet_Rows In _WorkSheet_Header.WorkSheet_Rows
                    Dim _Worksheet_CostRows As Worksheet_CostRows = _ocur.CreateObject(Of Worksheet_CostRows)()
                    _Worksheet_CostRows.RowIndex = _WorkSheet_Header.Worksheet_CostRows.Count + 1
                    _Worksheet_CostRows.WorkSheet_Header = _WorkSheet_Header
                    _Worksheet_CostRows.WorkSheet_Rows = _WorkSheet_Rows

                    _Worksheet_CostRows.ShortName = _WorkSheet_Rows.ShortName
                    _Worksheet_CostRows.Description = _WorkSheet_Rows.Description
                    _Worksheet_CostRows.QTT = _WorkSheet_Rows.Quantity
                    _Worksheet_CostRows.Units = _WorkSheet_Rows.Units

                    If _WorkSheet_Rows.Services IsNot Nothing Then
                        _Worksheet_CostRows.FinalPriceHUF_Out = _WorkSheet_Rows.Services.ListPriceHUF
                        _Worksheet_CostRows.FinalPriceDEV_Out = _WorkSheet_Rows.Services.ListPriceDEV
                        _Worksheet_CostRows.SumPriceHUF_Out = _WorkSheet_Rows.Services.ListPriceHUF * _Worksheet_CostRows.QTT
                        _Worksheet_CostRows.SumPriceDEV_Out = _WorkSheet_Rows.Services.ListPriceDEV * _Worksheet_CostRows.QTT

                        _Worksheet_CostRows.FinalPriceHUF_In = _WorkSheet_Rows.Services.CostPriceHUF
                        _Worksheet_CostRows.FinalPriceDEV_In = _WorkSheet_Rows.Services.CostPriceDEV
                        _Worksheet_CostRows.SumPriceHUF_In = _WorkSheet_Rows.Services.CostPriceHUF * _Worksheet_CostRows.QTT
                        _Worksheet_CostRows.SumPriceDEV_In = _WorkSheet_Rows.Services.CostPriceDEV * _Worksheet_CostRows.QTT

                        _Worksheet_CostRows.DifferentHUF = (_Worksheet_CostRows.FinalPriceHUF_Out - _Worksheet_CostRows.FinalPriceHUF_In) * _Worksheet_CostRows.QTT
                        _Worksheet_CostRows.DifferentDEV = (_Worksheet_CostRows.FinalPriceDEV_Out - _Worksheet_CostRows.FinalPriceDEV_In) * _Worksheet_CostRows.QTT
                        If _WorkSheet_Header.CurrencyName.ShortName <> "HUF" Then
                            _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceDEV_In / _Worksheet_CostRows.FinalPriceDEV_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                        Else
                            _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceHUF_In / _Worksheet_CostRows.FinalPriceHUF_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                        End If
                    ElseIf _WorkSheet_Rows.RecipeFromOther IsNot Nothing Then
                        _Worksheet_CostRows.FinalPriceHUF_Out = _WorkSheet_Rows.RecipeFromOther.ListPriceHUF
                        _Worksheet_CostRows.FinalPriceDEV_Out = _WorkSheet_Rows.RecipeFromOther.ListPriceDEV
                        _Worksheet_CostRows.SumPriceHUF_Out = _WorkSheet_Rows.RecipeFromOther.ListPriceHUF * _Worksheet_CostRows.QTT
                        _Worksheet_CostRows.SumPriceDEV_Out = _WorkSheet_Rows.RecipeFromOther.ListPriceDEV * _Worksheet_CostRows.QTT

                        _Worksheet_CostRows.FinalPriceHUF_In = 0 '_WorkSheet_Rows.RecipeFromOther.CostPriceHUF
                        _Worksheet_CostRows.FinalPriceDEV_In = 0 '_WorkSheet_Rows.RecipeFromOther.CostPriceDEV
                        _Worksheet_CostRows.SumPriceHUF_In = 0 '_WorkSheet_Rows.RecipeFromOther.CostPriceHUF * _Worksheet_CostRows.QTT
                        _Worksheet_CostRows.SumPriceDEV_In = 0 '_WorkSheet_Rows.RecipeFromOther.CostPriceDEV * _Worksheet_CostRows.QTT

                        _Worksheet_CostRows.DifferentHUF = (_Worksheet_CostRows.FinalPriceHUF_Out - _Worksheet_CostRows.FinalPriceHUF_In) * _Worksheet_CostRows.QTT
                        _Worksheet_CostRows.DifferentDEV = (_Worksheet_CostRows.FinalPriceDEV_Out - _Worksheet_CostRows.FinalPriceDEV_In) * _Worksheet_CostRows.QTT
                        If _WorkSheet_Header.CurrencyName.ShortName <> "HUF" Then
                            _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceDEV_In / _Worksheet_CostRows.FinalPriceDEV_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                        Else
                            _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceHUF_In / _Worksheet_CostRows.FinalPriceHUF_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                        End If
                    Else
                        _Worksheet_CostRows.FinalPriceHUF_Out = 0
                        _Worksheet_CostRows.FinalPriceDEV_Out = 0
                        _Worksheet_CostRows.SumPriceHUF_Out = 0
                        _Worksheet_CostRows.SumPriceDEV_Out = 0

                        _Worksheet_CostRows.FinalPriceHUF_In = 0
                        _Worksheet_CostRows.FinalPriceDEV_In = 0
                        _Worksheet_CostRows.SumPriceHUF_In = 0
                        _Worksheet_CostRows.SumPriceDEV_In = 0

                        _Worksheet_CostRows.DifferentPercent = 0
                        _Worksheet_CostRows.DifferentHUF = 0
                        _Worksheet_CostRows.DifferentDEV = 0
                    End If

                Next
            End If

            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
    Private Sub aRefreshWorksheetCostRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRefreshWorksheetCostRows.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _WorkSheet_Header.Worksheet_CostRows.Count = 0 Then Throw New Exception("*Nincsenek elszámolási sorok, nem lehet frissíteni!")

            If _WorkSheet_Header.WorkSheet_Rows.Count > 0 Then
                For Each _WorkSheet_Rows As WorkSheet_Rows In _WorkSheet_Header.WorkSheet_Rows
                    Dim _Worksheet_CostRows As Worksheet_CostRows = _ocur.FindObject(Of Worksheet_CostRows)(CriteriaOperator.Parse("WorkSheet_Rows=? AND WorkSheet_Header=?", _WorkSheet_Rows, _WorkSheet_Header))

                    If _Worksheet_CostRows Is Nothing Then
                        _Worksheet_CostRows = _ocur.CreateObject(Of Worksheet_CostRows)()
                        _Worksheet_CostRows.WorkSheet_Header = _WorkSheet_Header
                        _Worksheet_CostRows.WorkSheet_Rows = _WorkSheet_Rows

                        _Worksheet_CostRows.ShortName = _WorkSheet_Rows.ShortName
                        _Worksheet_CostRows.Description = _WorkSheet_Rows.Description
                        _Worksheet_CostRows.QTT = _WorkSheet_Rows.Quantity
                        _Worksheet_CostRows.Units = _WorkSheet_Rows.Units

                        If _WorkSheet_Rows.Services IsNot Nothing Then
                            _Worksheet_CostRows.FinalPriceHUF_Out = _WorkSheet_Rows.Services.ListPriceHUF
                            _Worksheet_CostRows.FinalPriceDEV_Out = _WorkSheet_Rows.Services.ListPriceDEV
                            _Worksheet_CostRows.SumPriceHUF_Out = _WorkSheet_Rows.Services.ListPriceHUF * _Worksheet_CostRows.QTT
                            _Worksheet_CostRows.SumPriceDEV_Out = _WorkSheet_Rows.Services.ListPriceDEV * _Worksheet_CostRows.QTT

                            _Worksheet_CostRows.FinalPriceHUF_In = _WorkSheet_Rows.Services.CostPriceHUF
                            _Worksheet_CostRows.FinalPriceDEV_In = _WorkSheet_Rows.Services.CostPriceDEV
                            _Worksheet_CostRows.SumPriceHUF_In = _WorkSheet_Rows.Services.CostPriceHUF * _Worksheet_CostRows.QTT
                            _Worksheet_CostRows.SumPriceDEV_In = _WorkSheet_Rows.Services.CostPriceDEV * _Worksheet_CostRows.QTT

                            _Worksheet_CostRows.DifferentHUF = (_Worksheet_CostRows.FinalPriceHUF_Out - _Worksheet_CostRows.FinalPriceHUF_In) * _Worksheet_CostRows.QTT
                            _Worksheet_CostRows.DifferentDEV = (_Worksheet_CostRows.FinalPriceDEV_Out - _Worksheet_CostRows.FinalPriceDEV_In) * _Worksheet_CostRows.QTT
                            If _WorkSheet_Header.CurrencyName.ShortName <> "HUF" Then
                                _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceDEV_In / _Worksheet_CostRows.FinalPriceDEV_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                            Else
                                _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceHUF_In / _Worksheet_CostRows.FinalPriceHUF_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                            End If
                        ElseIf _WorkSheet_Rows.RecipeFromOther IsNot Nothing Then
                            _Worksheet_CostRows.FinalPriceHUF_Out = _WorkSheet_Rows.RecipeFromOther.ListPriceHUF
                            _Worksheet_CostRows.FinalPriceDEV_Out = _WorkSheet_Rows.RecipeFromOther.ListPriceDEV
                            _Worksheet_CostRows.SumPriceHUF_Out = _WorkSheet_Rows.RecipeFromOther.ListPriceHUF * _Worksheet_CostRows.QTT
                            _Worksheet_CostRows.SumPriceDEV_Out = _WorkSheet_Rows.RecipeFromOther.ListPriceDEV * _Worksheet_CostRows.QTT

                            _Worksheet_CostRows.FinalPriceHUF_In = 0 '_WorkSheet_Rows.RecipeFromOther.CostPriceHUF
                            _Worksheet_CostRows.FinalPriceDEV_In = 0 '_WorkSheet_Rows.RecipeFromOther.CostPriceDEV
                            _Worksheet_CostRows.SumPriceHUF_In = 0 '_WorkSheet_Rows.RecipeFromOther.CostPriceHUF * _Worksheet_CostRows.QTT
                            _Worksheet_CostRows.SumPriceDEV_In = 0 '_WorkSheet_Rows.RecipeFromOther.CostPriceDEV * _Worksheet_CostRows.QTT

                            _Worksheet_CostRows.DifferentHUF = (_Worksheet_CostRows.FinalPriceHUF_Out - _Worksheet_CostRows.FinalPriceHUF_In) * _Worksheet_CostRows.QTT
                            _Worksheet_CostRows.DifferentDEV = (_Worksheet_CostRows.FinalPriceDEV_Out - _Worksheet_CostRows.FinalPriceDEV_In) * _Worksheet_CostRows.QTT
                            If _WorkSheet_Header.CurrencyName.ShortName <> "HUF" Then
                                _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceDEV_In / _Worksheet_CostRows.FinalPriceDEV_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                            Else
                                _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceHUF_In / _Worksheet_CostRows.FinalPriceHUF_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                            End If
                        Else
                            _Worksheet_CostRows.FinalPriceHUF_Out = 0
                            _Worksheet_CostRows.FinalPriceDEV_Out = 0
                            _Worksheet_CostRows.SumPriceHUF_Out = 0
                            _Worksheet_CostRows.SumPriceDEV_Out = 0

                            _Worksheet_CostRows.FinalPriceHUF_In = 0
                            _Worksheet_CostRows.FinalPriceDEV_In = 0
                            _Worksheet_CostRows.SumPriceHUF_In = 0
                            _Worksheet_CostRows.SumPriceDEV_In = 0

                            _Worksheet_CostRows.DifferentPercent = 0
                            _Worksheet_CostRows.DifferentHUF = 0
                            _Worksheet_CostRows.DifferentDEV = 0
                        End If
                    Else
                        _Worksheet_CostRows.ShortName = _WorkSheet_Rows.ShortName
                        _Worksheet_CostRows.Description = _WorkSheet_Rows.Description
                        _Worksheet_CostRows.QTT = _WorkSheet_Rows.Quantity
                        _Worksheet_CostRows.Units = _WorkSheet_Rows.Units
                    End If
                Next
            End If
            If _WorkSheet_Header.StorageOutRows.Count > 0 Then
                For Each _StorageOutRows As StorageOutRows In _WorkSheet_Header.StorageOutRows
                    Dim _Worksheet_CostRows As Worksheet_CostRows = _ocur.FindObject(Of Worksheet_CostRows)(CriteriaOperator.Parse("StorageOutRows=? AND WorkSheet_Header=?", _StorageOutRows, _WorkSheet_Header))
                    If _Worksheet_CostRows Is Nothing Then
                        _Worksheet_CostRows = _ocur.CreateObject(Of Worksheet_CostRows)()
                        _Worksheet_CostRows.WorkSheet_Header = _WorkSheet_Header
                        _Worksheet_CostRows.StorageOutRows = _StorageOutRows

                        _Worksheet_CostRows.ShortName = _StorageOutRows.Products.ShortName
                        _Worksheet_CostRows.Description = _StorageOutRows.Products.Description
                        _Worksheet_CostRows.QTT = _StorageOutRows.QTT
                        _Worksheet_CostRows.Units = _StorageOutRows.Products.Units

                        _Worksheet_CostRows.FinalPriceHUF_Out = _StorageOutRows.Products.ListPriceOutHUF
                        _Worksheet_CostRows.FinalPriceDEV_Out = _StorageOutRows.Products.ListPriceOutDEV
                        _Worksheet_CostRows.SumPriceHUF_Out = _StorageOutRows.Products.ListPriceOutHUF * _Worksheet_CostRows.QTT
                        _Worksheet_CostRows.SumPriceDEV_Out = _StorageOutRows.Products.ListPriceOutDEV * _Worksheet_CostRows.QTT


                        Dim _DeliveryNoteInRows As DeliveryNoteInRows
                        Dim _StorageOutRowsInRows As StorageOutRowsInRows
                        _Worksheet_CostRows.SumPriceHUF_In = 0
                        _Worksheet_CostRows.SumPriceDEV_In = 0

                        For Each _StorageOutRowsInRows In _StorageOutRows.StorageOutRowsInRows
                            If _StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows IsNot Nothing Then
                                _DeliveryNoteInRows = _StorageOutRowsInRows.StorageInRows.DeliveryNoteInRows
                                _Worksheet_CostRows.SumPriceHUF_In += (_DeliveryNoteInRows.ListPriceHUF - _DeliveryNoteInRows.DiscountPriceHUF) * _StorageOutRowsInRows.QTT
                                _Worksheet_CostRows.SumPriceDEV_In += (_DeliveryNoteInRows.ListPriceDEV - _DeliveryNoteInRows.DiscountPriceDEV) * _StorageOutRowsInRows.QTT
                            Else
                                _Worksheet_CostRows.SumPriceHUF_In += _StorageOutRows.Products.ListPriceInHUF * _StorageOutRowsInRows.QTT
                                _Worksheet_CostRows.SumPriceDEV_In += _StorageOutRows.Products.ListPriceInDEV * _StorageOutRowsInRows.QTT
                            End If
                        Next
                        _Worksheet_CostRows.FinalPriceHUF_In = _Worksheet_CostRows.SumPriceHUF_In / _Worksheet_CostRows.QTT
                        _Worksheet_CostRows.FinalPriceDEV_In = _Worksheet_CostRows.SumPriceDEV_In / _Worksheet_CostRows.QTT

                        _Worksheet_CostRows.DifferentHUF = (_Worksheet_CostRows.FinalPriceHUF_Out - _Worksheet_CostRows.FinalPriceHUF_In) * _Worksheet_CostRows.QTT
                        _Worksheet_CostRows.DifferentDEV = (_Worksheet_CostRows.FinalPriceDEV_Out - _Worksheet_CostRows.FinalPriceDEV_In) * _Worksheet_CostRows.QTT
                        If _WorkSheet_Header.CurrencyName.ShortName <> "HUF" Then
                            _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceDEV_In / _Worksheet_CostRows.FinalPriceDEV_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                        Else
                            _Worksheet_CostRows.DifferentPercent = Math.Round((1 - (_Worksheet_CostRows.FinalPriceHUF_In / _Worksheet_CostRows.FinalPriceHUF_Out)) * 100, 2, MidpointRounding.AwayFromZero)
                        End If
                    Else
                        _Worksheet_CostRows.ShortName = _StorageOutRows.Products.ShortName
                        _Worksheet_CostRows.Description = _StorageOutRows.Products.Description
                        _Worksheet_CostRows.QTT = _StorageOutRows.QTT
                        _Worksheet_CostRows.Units = _StorageOutRows.Products.Units
                    End If
                Next
            End If

            '------============ Worksheet_CostRows collection ============---------

            If _WorkSheet_Header.Worksheet_CostRows.Count > 0 Then
                Dim _Worksheet_CostRows_DEL_Collection As New ArrayList
                For Each _Worksheet_CostRows As Worksheet_CostRows In _WorkSheet_Header.Worksheet_CostRows
                    Dim _WorkSheet_Rows As WorkSheet_Rows = Nothing
                    Dim _StorageOutRows As StorageOutRows = Nothing

                    If _Worksheet_CostRows.StorageOutRows IsNot Nothing Then
                        _StorageOutRows = _ocur.FindObject(Of StorageOutRows)(CriteriaOperator.Parse("StorageOutHeader.WorkSheet_Header=?", _WorkSheet_Header))
                        If _StorageOutRows Is Nothing Then
                            _Worksheet_CostRows_DEL_Collection.Add(_StorageOutRows)
                        Else
                            _Worksheet_CostRows.ShortName = _StorageOutRows.Products.ShortName
                            _Worksheet_CostRows.Description = _StorageOutRows.Products.Description
                            _Worksheet_CostRows.QTT = _StorageOutRows.QTT
                            _Worksheet_CostRows.Units = _StorageOutRows.Products.Units
                        End If
                    End If

                    If _Worksheet_CostRows.WorkSheet_Rows IsNot Nothing Then
                        _WorkSheet_Rows = _ocur.FindObject(Of WorkSheet_Rows)(CriteriaOperator.Parse("Oid=?", _Worksheet_CostRows.WorkSheet_Rows.Oid))

                        If _WorkSheet_Rows Is Nothing Then
                            _Worksheet_CostRows_DEL_Collection.Add(_Worksheet_CostRows)
                        Else
                            If _Worksheet_CostRows.WorkSheet_Rows IsNot Nothing Then

                                _Worksheet_CostRows.ShortName = _WorkSheet_Rows.ShortName
                                _Worksheet_CostRows.Description = _WorkSheet_Rows.Description
                                _Worksheet_CostRows.QTT = _WorkSheet_Rows.Quantity
                                _Worksheet_CostRows.Units = _WorkSheet_Rows.Units
                            End If
                        End If
                    End If

                    _WorkSheet_Rows = Nothing
                    _StorageOutRows = Nothing
                Next
                _ocur.Delete(_Worksheet_CostRows_DEL_Collection)
                _ocur.CommitChanges()
            End If
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
    Private Sub aCreateInvoiceFromWorksheetCost_Cancel(sender As Object, e As System.EventArgs) Handles aCreateInvoiceFromWorksheetCost.Cancel
        _noselect = False
    End Sub
    Private Sub aCreateInvoiceFromWorksheetCost_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateInvoiceFromWorksheetCost.CustomizePopupWindowParams
        _noselect = True
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CreateInvoiceFromWorksheetCost_PopUp As CreateInvoiceFromWorksheetCost_PopUp = _onew.CreateObject(Of CreateInvoiceFromWorksheetCost_PopUp)()
        Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)

        _CreateInvoiceFromWorksheetCost_PopUp.Customers = _onew.GetObjectByKey(Of Customers)(_WorkSheet_Header.Customers.Oid)
        _CreateInvoiceFromWorksheetCost_PopUp.WorkSheet_Header = _onew.GetObject(_WorkSheet_Header)

        e.View = Application.CreateDetailView(_onew, "CreateInvoiceFromWorksheetCost_PopUp_DetailView", False, _CreateInvoiceFromWorksheetCost_PopUp)
    End Sub
    Private Sub aCreateInvoiceFromWorksheetCost_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateInvoiceFromWorksheetCost.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Try
            Dim _CreateInvoiceFromWorksheetCost_PopUp As CreateInvoiceFromWorksheetCost_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), CreateInvoiceFromWorksheetCost_PopUp)
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _WorkSheet_Header.Worksheet_CostRows.Count = 0 Then Throw New Exception("*Nincsenek elszámolási sorok, nem lehet számlázni!")

            If _WorkSheet_Header.Worksheet_CostRows.Count > 0 Then
                Dim _CreateInvoice As Boolean = False
                For Each _Worksheet_CostRows As Worksheet_CostRows In _WorkSheet_Header.Worksheet_CostRows
                    If _Worksheet_CostRows.InvoiceRows Is Nothing Then
                        _CreateInvoice = True
                        Exit For
                    End If
                Next

                If _CreateInvoice = False Then Throw New Exception("*Nincs olyan költségsor, amelyről nem készült volna számla!")

                Dim _WorkSheet_Rows_ListPropertyEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("Worksheet_CostRows")
                If _WorkSheet_Rows_ListPropertyEditor Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
                If _WorkSheet_Rows_ListPropertyEditor.ListView.SelectedObjects.Count = 0 Then Throw New Exception("*Nincs kijelölve egyetlen sor sem!")

                Frame.GetController(Of WorkSheet_VC).aRefreshWorksheetCostRows.DoExecute()

                Dim _InvoiceHeader As InvoiceHeader = _ocur.CreateObject(Of InvoiceHeader)()
                _InvoiceHeader.CustomersFrom = _WorkSheet_Header.CustomersFrom
                _InvoiceHeader.Customers = _ocur.GetObjectByKey(Of Customers)(_CreateInvoiceFromWorksheetCost_PopUp.Customers.Oid)
                _InvoiceHeader.InvoiceType = _ocur.GetObjectByKey(Of InvoiceType)(_CreateInvoiceFromWorksheetCost_PopUp.InvoiceType.Oid)
                _InvoiceHeader.DateExecution = _CreateInvoiceFromWorksheetCost_PopUp.DateExecution
                _InvoiceHeader.DatePayment = _CreateInvoiceFromWorksheetCost_PopUp.DatePayment
                _InvoiceHeader.PaymentOption = _ocur.GetObjectByKey(Of PaymentOption)(_CreateInvoiceFromWorksheetCost_PopUp.PaymentOption.Oid)
                _InvoiceHeader.BankInformation = _ocur.GetObjectByKey(Of BankInformation)(_CreateInvoiceFromWorksheetCost_PopUp.BankInformation.Oid)
                _InvoiceHeader.Description = _CreateInvoiceFromWorksheetCost_PopUp.Description
                _InvoiceHeader.DescriptionHeader = _CreateInvoiceFromWorksheetCost_PopUp.DescriptionHeader
                _InvoiceHeader.InvoicePeriod = _CreateInvoiceFromWorksheetCost_PopUp.InvoicePeriod
                _InvoiceHeader.CurrencyRate = _CreateInvoiceFromWorksheetCost_PopUp.CurrencyRate
                _InvoiceHeader.IsProforma = True
                _InvoiceHeader.IsEditable = False

                If _InvoiceHeader.InvoiceType.NumberGeneratorProforma IsNot Nothing Then
                    Dim _NG As NumberGenerator = _InvoiceHeader.InvoiceType.NumberGeneratorProforma
                    _InvoiceHeader.DocumentNumberProforma = _NG.GetNewNumber(_NG)
                End If
                SetInvoiceSaveProperties(_InvoiceHeader)

                For Each _Worksheet_CostRows As Worksheet_CostRows In _WorkSheet_Rows_ListPropertyEditor.ListView.SelectedObjects
                    Dim _Worksheet_CostRows_ACT = _ocur.FindObject(Of Worksheet_CostRows)(CriteriaOperator.Parse("Oid=?", _Worksheet_CostRows.Oid))
                    If _Worksheet_CostRows_ACT.InvoiceRows Is Nothing Then
                        Dim _InvoiceRows As InvoiceRows = _ocur.CreateObject(Of InvoiceRows)()
                        _InvoiceRows.InvoiceHeader = _InvoiceHeader
                        _InvoiceRows.JobNumberObjects = _WorkSheet_Header.JobNumberObjects
                        _InvoiceRows.UniqueObjects = _WorkSheet_Header.UniqueObjects
                        _InvoiceRows.CostPlace = _WorkSheet_Header.CostPlace
                        _InvoiceRows.CostHolder = _WorkSheet_Header.CostHolder
                        _InvoiceRows.Controlling = _Worksheet_CostRows_ACT.Controlling
                        _InvoiceRows.InvoiceRowtype = eInvoiceRowType.FromWorkSheet
                        _InvoiceRows.Worksheet_CostRows = _Worksheet_CostRows_ACT
                        _InvoiceRows.RowIndex = _Worksheet_CostRows_ACT.RowIndex
                        If _Worksheet_CostRows_ACT.StorageOutRows IsNot Nothing Then
                            _InvoiceRows.CustomsTariffs = _Worksheet_CostRows_ACT.StorageOutRows.Products.CustomsTariff
                        Else
                            _InvoiceRows.CustomsTariffs = Nothing ' Kitárolási sornál kell
                        End If

                        _InvoiceRows.ShortName = _Worksheet_CostRows_ACT.ShortName
                        _InvoiceRows.Description = _Worksheet_CostRows_ACT.Description
                        _InvoiceRows.ListPriceHUF = _Worksheet_CostRows_ACT.FinalPriceHUF_Out
                        _InvoiceRows.ListPriceDEV = _Worksheet_CostRows_ACT.FinalPriceDEV_Out
                        _InvoiceRows.DiscountPriceHUF = 0 'Kedvezmény összege HUF
                        _InvoiceRows.DiscountPriceDEV = 0 'Kedvezmény összege DEV
                        _InvoiceRows.QTT = _Worksheet_CostRows_ACT.QTT
                        _InvoiceRows.Units = _Worksheet_CostRows_ACT.Units
                        If _Worksheet_CostRows_ACT.StorageOutRows IsNot Nothing Then
                            _InvoiceRows.VAT = _Worksheet_CostRows_ACT.StorageOutRows.Products.VAT
                        ElseIf _Worksheet_CostRows_ACT.WorkSheet_Rows.Services IsNot Nothing Then
                            _InvoiceRows.VAT = _Worksheet_CostRows_ACT.WorkSheet_Rows.Services.VAT
                        Else
                            _InvoiceRows.VAT = _ocur.FindObject(Of VAT)(CriteriaOperator.Parse("DefaultForBusinnes=True"))
                        End If

                        _Worksheet_CostRows_ACT.InvoiceRows = _InvoiceRows
                    End If
                Next

                _ocur.CommitChanges()

                Dim _ReportData As ReportData
                If _WorkSheet_Header.CurrencyName.ShortName = "HUF" Then
                    _ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _InvoiceHeader.InvoiceType.ReportData.Oid), True)
                End If

                If _WorkSheet_Header.CurrencyName.ShortName <> "HUF" Then
                    _ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _InvoiceHeader.InvoiceType.ReportData_DEV.Oid), True)
                End If
                If _ReportData IsNot Nothing Then
                    Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("InvoiceHeader.Oid=?", _InvoiceHeader.Oid), True)
                End If
            End If

        Catch ext As NullReferenceException
            MsgBox("Núri Ferenc!" + vbNewLine + ext.Message)
            _ocur.Rollback()

        Catch ext As Exception
            MsgBox(ext.Message)

        End Try
        _noselect = False


    End Sub
    Private Sub aReOpenWorksheetHeader_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aReOpenWorksheetHeader.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")

            _WorkSheet_Header.WorkSheetState = eWorkSheetState.eOpened

            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
    Private Sub aOpenWorkSheetFile_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenWorkSheetFile.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _WorkSheet_Header.WorkSheetFile Is Nothing Then Throw New Exception("*Nincs csatolt munkalap!")

            Dim _FilePath As String = Path.Combine(Path.GetTempPath(), _WorkSheet_Header.WorkSheetFile.FileName)
            Dim _Stream As Stream = New FileStream(_FilePath, FileMode.Create)
            _WorkSheet_Header.WorkSheetFile.SaveToStream(_Stream)
            _Stream.Close()
            Process.Start(_FilePath)
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
    Private Sub aOpenSubcontractorInvoice_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOpenSubcontractorInvoice.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _WorkSheet_Header.SubcontractorInvoice Is Nothing Then Throw New Exception("*Nincs csatolt alvállalkozói számla!")

            Dim _FilePath As String = Path.Combine(Path.GetTempPath(), _WorkSheet_Header.SubcontractorInvoice.FileName)
            Dim _Stream As Stream = New FileStream(_FilePath, FileMode.Create)
            _WorkSheet_Header.SubcontractorInvoice.SaveToStream(_Stream)
            _Stream.Close()
            Process.Start(_FilePath)
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub

    Private Sub aPrintWorkSheet_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aPrintWorkSheet.Execute
        Try
            Dim _ReportData As ReportData
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

            If _WorkSheet_Header.WorkSheetType Is Nothing Then Throw New Exception("*Nincs beállítva munkalap típus!")
            _ReportData = _WorkSheet_Header.WorkSheetType.ReportData

            If _ReportData IsNot Nothing Or _WorkSheet_Header Is Nothing Then
                Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("Oid=?", _WorkSheet_Header.Oid), True)
            End If
        Catch ext As NullReferenceException
            MsgBox(ext.Message, MsgBoxStyle.Information)
        Catch ext As Exception
            MsgBox(ext.Message, MsgBoxStyle.Information)
        End Try
    End Sub

    Private Sub aWorkSheet_Rows_ChangeRows_Cancel(sender As Object, e As System.EventArgs) Handles aWorkSheet_Rows_ChangeRows.Cancel
        _noselect = False
    End Sub
    Private Sub aWorkSheet_Rows_ChangeRows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aWorkSheet_Rows_ChangeRows.CustomizePopupWindowParams
        _noselect = True
        Try
            If _selection.Count = 0 Then Throw New Exception("*Nincs kiválasztott sor!")
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Worksheet_Rows_ChangeGroup As Worksheet_Rows_ChangeGroup = _onew.CreateObject(Of Worksheet_Rows_ChangeGroup)()

            e.View = Application.CreateDetailView(_onew, "Worksheet_Rows_ChangeGroup_DetailView", False, _Worksheet_Rows_ChangeGroup)
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
    Private Sub aWorkSheet_Rows_ChangeRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aWorkSheet_Rows_ChangeRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _Worksheet_Rows_ChangeGroup As Worksheet_Rows_ChangeGroup = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), Worksheet_Rows_ChangeGroup))

        If _Worksheet_Rows_ChangeGroup.Change_RowGroup1 Then
            For Each _Worksheet_Rows As WorkSheet_Rows In _selection
                _Worksheet_Rows.RowGroup1 = _Worksheet_Rows_ChangeGroup.RowGroup1
            Next
        End If
        If _Worksheet_Rows_ChangeGroup.Change_RowGroup2 Then
            For Each _Worksheet_Rows As WorkSheet_Rows In _selection
                _Worksheet_Rows.RowGroup2 = _Worksheet_Rows_ChangeGroup.RowGroup2
            Next
        End If
        _ocur.CommitChanges()
        _noselect = False
    End Sub
    Private Sub WorkSheet_Header_Worksheet_CostRows_ListView_SelectionChanged(sender As Object, e As EventArgs)
        Dim _WorkSheet_CostRows As Worksheet_CostRows
        If View Is Nothing Then Exit Sub

        If _noselect = True Then Exit Sub
        If View.SelectedObjects.Count > 0 Then
            _selection.Clear()

            For Each _WorkSheet_CostRows In View.SelectedObjects
                _selection.Add(_WorkSheet_CostRows)
            Next
        End If
    End Sub
    Private Sub WorkSheet_Header_WorkSheet_Rows_ListView_SelectionChanged(sender As Object, e As EventArgs)
        If View Is Nothing Then Exit Sub
        If _noselect = True Then Exit Sub
        If View.SelectedObjects.Count > 0 Then
            _selection.Clear()
            For Each _WorkSheet_Rows In View.SelectedObjects
                _selection.Add(_WorkSheet_Rows)
            Next
        End If
    End Sub
    Private Sub aWorkSheet_CostRows_ChangeRows_Cancel(sender As Object, e As System.EventArgs) Handles aWorkSheet_CostRows_ChangeRows.Cancel
        _noselect = False
    End Sub
    Private Sub aWorkSheet_CostRows_ChangeRows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aWorkSheet_CostRows_ChangeRows.CustomizePopupWindowParams
        _noselect = True
        Try
            If _selection.Count = 0 Then Throw New Exception("*Nincs kiválasztott sor!")
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _Worksheet_CostRows_ChangeGroup As Worksheet_CostRows_ChangeGroup = _onew.CreateObject(Of Worksheet_CostRows_ChangeGroup)()

            e.View = Application.CreateDetailView(_onew, "Worksheet_CostRows_ChangeGroup_DetailView", False, _Worksheet_CostRows_ChangeGroup)
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub
    Private Sub aWorkSheet_CostRows_ChangeRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aWorkSheet_CostRows_ChangeRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _Worksheet_CostRows_ChangeGroup As Worksheet_CostRows_ChangeGroup = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), Worksheet_CostRows_ChangeGroup))

        If _Worksheet_CostRows_ChangeGroup.Change_RowGroup1 Then
            For Each _WorkSheet_CostRows As Worksheet_CostRows In _selection
                _WorkSheet_CostRows.RowGroup1 = _Worksheet_CostRows_ChangeGroup.RowGroup1
            Next
        End If
        If _Worksheet_CostRows_ChangeGroup.Change_RowGroup2 Then
            For Each _WorkSheet_CostRows As Worksheet_CostRows In _selection
                _WorkSheet_CostRows.RowGroup2 = _Worksheet_CostRows_ChangeGroup.RowGroup2
            Next
        End If
        If _Worksheet_CostRows_ChangeGroup.Change_Controlling Then
            For Each _WorkSheet_CostRows As Worksheet_CostRows In _selection
                _WorkSheet_CostRows.Controlling = _ocur.GetObjectByKey(Of Controlling)(_Worksheet_CostRows_ChangeGroup.Controlling.Oid)
            Next
        End If
        _ocur.CommitChanges()
        _noselect = False
    End Sub
    Private Sub aCloseWorkSheet_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCloseWorkSheet.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            _WorkSheet_Header.WorkSheetState = eWorkSheetState.eClosed
            _WorkSheet_Header.DateClosed = GetSQLTime(_ocur.Session)
            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message, MsgBoxStyle.Information)
        End Try
    End Sub
    Private Sub aSetReadyToCloseWorkSheet_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSetReadyToCloseWorkSheet.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            _WorkSheet_Header.WorkSheetState = eWorkSheetState.eReadyforClose
            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message, MsgBoxStyle.Information)
        End Try
    End Sub
    Private Sub aReopenWorkSheet_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aReopenWorkSheet.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ReopenWorkSheet_PopUp As ReopenWorkSheet_PopUp = _onew.CreateObject(Of ReopenWorkSheet_PopUp)()
        e.View = Application.CreateDetailView(_onew, "ReopenWorkSheet_PopUp_DetailView", False, _ReopenWorkSheet_PopUp)
    End Sub
    Private Sub aReopenWorkSheet_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aReopenWorkSheet.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _ReopenWorkSheet_PopUp As ReopenWorkSheet_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ReopenWorkSheet_PopUp)
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)

            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _ReopenWorkSheet_PopUp.WorkSheet_ReopenReason_Type Is Nothing Then Throw New Exception("*Az újranyitás típúsát kötelező megadni!")
            _WorkSheet_Header.WorkSheetState = eWorkSheetState.eReopened

            Dim _WorkSheet_ReopenReason As WorkSheet_ReopenReason = _ocur.CreateObject(Of WorkSheet_ReopenReason)()
            _WorkSheet_ReopenReason.WorkSheet_Header = _WorkSheet_Header
            _WorkSheet_ReopenReason.WorkSheet_ReopenReason_Type = _ocur.GetObjectByKey(Of WorkSheet_ReopenReason_Type)(_ReopenWorkSheet_PopUp.WorkSheet_ReopenReason_Type.Oid)
            _WorkSheet_ReopenReason.Description = _ReopenWorkSheet_PopUp.Description

            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message, MsgBoxStyle.Information)
        End Try
    End Sub
    Private Sub aAddHRPerson_WorkSheet_Header_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aAddHRPerson_WorkSheet_Header.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

            Dim _CS_WorkSheet_Header_AddHRPerson As New CollectionSource(_onew, GetType(WorkSheet_Header_AddHRPerson_PopUp))
            Dim _HRPerson_Collection As New XPCollection(Of HRPerson)(_onew.Session)

            For Each _HRPerson As HRPerson In _HRPerson_Collection
                Dim _WorkSheet_Header_AddHRPerson_PopUp As WorkSheet_Header_AddHRPerson_PopUp = _onew.CreateObject(Of WorkSheet_Header_AddHRPerson_PopUp)()
                _WorkSheet_Header_AddHRPerson_PopUp.HRPerson = _HRPerson
                _CS_WorkSheet_Header_AddHRPerson.Add(_WorkSheet_Header_AddHRPerson_PopUp)
            Next
            e.View = Application.CreateListView("WorkSheet_Header_AddHRPerson_PopUp_ListView", _CS_WorkSheet_Header_AddHRPerson, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try

    End Sub
    Private Sub aAddHRPerson_WorkSheet_Header_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aAddHRPerson_WorkSheet_Header.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace

            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, WorkSheet_Header)

            For Each _WorkSheet_Header_AddHRPerson_PopUp As WorkSheet_Header_AddHRPerson_PopUp In e.PopupWindow.View.SelectedObjects
                Dim _WorkSheet_Header_HRPerson As WorkSheet_Header_HRPerson = _ocur.FindObject(Of WorkSheet_Header_HRPerson) _
                         (CriteriaOperator.Parse("WorkSheet_Header=? AND HRPerson=?", _WorkSheet_Header, _ocur.GetObject(_WorkSheet_Header_AddHRPerson_PopUp.HRPerson)))
                If _WorkSheet_Header_HRPerson Is Nothing Then
                    _WorkSheet_Header_HRPerson = New WorkSheet_Header_HRPerson(_ocur.Session)
                    _WorkSheet_Header_HRPerson.WorkSheet_Header = _WorkSheet_Header
                    _WorkSheet_Header_HRPerson.HRPerson = _ocur.GetObject(_WorkSheet_Header_AddHRPerson_PopUp.HRPerson)
                End If
            Next

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
    Private Sub SetInvoiceSaveProperties(_InvoiceHeader As InvoiceHeader)
        On Error GoTo CheckError
        _InvoiceHeader.Saved_Customers_Name = _InvoiceHeader.Customers.Name.ToString

        _InvoiceHeader.Saved_Customers_Address_Country = _InvoiceHeader.Customers.Address1.Country.ToString
        _InvoiceHeader.Saved_Customers_Address_ZipPostal = _InvoiceHeader.Customers.Address1.ZipPostal.ToString
        _InvoiceHeader.Saved_Customers_Address_Street = _InvoiceHeader.Customers.Address1.Street.ToString
        _InvoiceHeader.Saved_Customers_Address_City = _InvoiceHeader.Customers.Address1.City.ToString
        _InvoiceHeader.Saved_Customers_Address_StateProvince = _InvoiceHeader.Customers.Address1.StateProvince.ToString

        _InvoiceHeader.Saved_Customers_VATNumber = _InvoiceHeader.Customers.VATNumber.ToString
        _InvoiceHeader.Saved_Customers_VATNumberEU = _InvoiceHeader.Customers.VATNumberEU.ToString

        _InvoiceHeader.Saved_CustomersFrom_Name = _InvoiceHeader.CustomersFrom.Name.ToString

        _InvoiceHeader.Saved_CustomersFrom_Address_Country = _InvoiceHeader.CustomersFrom.Address1.Country.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_ZipPostal = _InvoiceHeader.CustomersFrom.Address1.ZipPostal.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_Street = _InvoiceHeader.CustomersFrom.Address1.Street.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_City = _InvoiceHeader.CustomersFrom.Address1.City.ToString
        _InvoiceHeader.Saved_CustomersFrom_Address_StateProvince = _InvoiceHeader.CustomersFrom.Address1.StateProvince.ToString

        _InvoiceHeader.Saved_CustomersFrom_VATNumber = _InvoiceHeader.CustomersFrom.VATNumber.ToString
        _InvoiceHeader.Saved_CustomersFrom_VATNumberEU = _InvoiceHeader.CustomersFrom.VATNumberEU.ToString


        _InvoiceHeader.Saved_CustomersFrom_Bank_ShortName = _InvoiceHeader.BankInformation.FullBankAccount.ToString
        _InvoiceHeader.Saved_CustomersFrom_Bank_IBAN = _InvoiceHeader.BankInformation.IBAN.ToString
        _InvoiceHeader.Saved_CustomersFrom_Bank_SWIFT = _InvoiceHeader.BankInformation.SWIFT.ToString
        _InvoiceHeader.Saved_CustomersFrom_Bank_AccountNumber = _InvoiceHeader.BankInformation.AccountNumber.ToString

CheckError:
        If Err.Number = 91 Then Resume Next
        If Err.Number = 13 Then Resume Next
    End Sub
    Private Sub aWorkSheet_InsertRecipe_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aWorkSheet_InsertRecipe.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, WorkSheet_Header)
            If _WorkSheet_Header Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _WorkSheet_Header.Recipe Is Nothing Then Throw New Exception("*Nincs kiválasztva egyetlen recept se a munkalap fejlécén!" + vbNewLine + _
                                                                            "Kérem ellenőrizze!")
            If _WorkSheet_Header.Recipe.RecipeFromOther.Count = 0 Then _
                    Throw New Exception("*A kiválasztott receptnek nincs egyetlen beilleszthető sora sem!" + vbNewLine + _
                                                                            "Kérem ellenőrizze!")
            If _WorkSheet_Header.Recipe.RecipeFromOther.Count > 0 Then
                For Each _RecipeFromOther As RecipeFromOther In _WorkSheet_Header.Recipe.RecipeFromOther
                    Dim _WorkSheet_Rows As WorkSheet_Rows = _ocur.FindObject(Of WorkSheet_Rows)(CriteriaOperator.Parse("RecipeFromOther=? AND WorkSheet_Header=?", _RecipeFromOther, _WorkSheet_Header))

                    If _WorkSheet_Rows Is Nothing Then
                        _WorkSheet_Rows = _ocur.CreateObject(Of WorkSheet_Rows)()
                        _WorkSheet_Rows.WorkSheet_Header = _WorkSheet_Header
                        _WorkSheet_Rows.RowGroup1 = _RecipeFromOther.RowGroup1
                        _WorkSheet_Rows.RowGroup2 = _RecipeFromOther.RowGroup2
                        _WorkSheet_Rows.WorkType = eWorkType.ePayable
                        _WorkSheet_Rows.WorkRowType = eWorkRowType.eManual
                        _WorkSheet_Rows.Description = _RecipeFromOther.Description
                        _WorkSheet_Rows.ShortName = _RecipeFromOther.ShortName
                        _WorkSheet_Rows.Units = _RecipeFromOther.Units
                        _WorkSheet_Rows.Quantity = _RecipeFromOther.QTT
                        _WorkSheet_Rows.RecipeFromOther = _RecipeFromOther
                        _WorkSheet_Rows.RowIndex = _WorkSheet_Header.WorkSheet_Rows.Count
                    End If
                Next
            End If
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        End Try
    End Sub
End Class
