Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.Validation
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Xpo
Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Imports System.Reflection
Imports DevExpress.Xpo.DB
Imports System.Net

Public Class StoragePDACollectionHeaderVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork()
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()



        If View.Id = "StoragePDACollectionHeader_ListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "StoragePDACollectionHeader_StoragePDACollectionRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            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 AuditTrailVC)().aGetAuditTrail.Active.SetItemValue("", False)
        End If

        '--------------------------DetailViewZ----------------------------------------
        If View.Id = "StoragePDACollectionHeader_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "StorageInFromPDACollectionPopup_DetailView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "StoragePDACollectionHeader_ListView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "StoragePDACollectionHeader_StoragePDACollectionRows_ListView" Then
            Frame.GetController(Of LinkUnlinkController)().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)
            Frame.GetController(Of ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of AuditTrailVC)().aGetAuditTrail.Active.SetItemValue("", True)
        End If

        '--------------------------DetailViewZ----------------------------------------
        If View.Id = "StoragePDACollectionHeader_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "StorageInFromPDACollectionPopup_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
    End Sub
    Private Sub AcceptAction_Executing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Validator.RuleSet.ValidateAll(View.ObjectSpace, View.SelectedObjects, DefaultContexts.Save)
    End Sub

    Private Sub aInsertStoragePDARows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _StoragePDACollectionRows As StoragePDACollectionRows = _onew.CreateObject(Of StoragePDACollectionRows)()

        e.View = Application.CreateDetailView(_onew, "StoragePDACollectionRows_DetailView", False, _StoragePDACollectionRows)
    End Sub
    Private Sub aInsertStoragePDARows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs)

        Try
            Dim _ocur As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
            Dim _StoragePDACollectionRows_PopUp As StoragePDACollectionRows = TryCast(e.PopupWindow.View.SelectedObjects(0), StoragePDACollectionRows)
            If _StoragePDACollectionRows_PopUp Is Nothing Then Throw New Exception("*")
            Dim _StoragePDACollectionRows As StoragePDACollectionRows = _ocur.CreateObject(Of StoragePDACollectionRows)()
            _StoragePDACollectionRows.StoragePDACollectionHeader = _ocur.GetObject(_StoragePDACollectionHeader)
            _StoragePDACollectionRows.Products = _ocur.GetObject(_StoragePDACollectionRows_PopUp.Products)
            _StoragePDACollectionRows.QTT = _StoragePDACollectionRows_PopUp.QTT
            _StoragePDACollectionRows.Description = _StoragePDACollectionRows_PopUp.Description

            _ocur.CommitChanges()

            Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
            If nestedFrame IsNot Nothing Then
                Dim parentView As View = nestedFrame.View
                parentView.ObjectSpace.Refresh()
            End If
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try

    End Sub
    Private Sub aDeleteStoragePDARows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs)
        Try
            Dim _StoragePDACollectionRows_ListEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("StoragePDACollectionRows")
            If _StoragePDACollectionRows_ListEditor Is Nothing Then Throw New Exception("*Váratlan hiba történt, művelet megszakadt!")
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _StoragePDACollectionRows_DEL_Collection As New ArrayList
            For Each _StoragePDACollectionRows As StoragePDACollectionRows In _StoragePDACollectionRows_ListEditor.ListView.SelectedObjects
                _StoragePDACollectionRows_DEL_Collection.Add(_StoragePDACollectionRows)
            Next
            _ocur.Delete(_StoragePDACollectionRows_DEL_Collection)
            _ocur.CommitChanges()
        Catch ext As Exception
            MsgBox(ext.Message)
        End Try
    End Sub

    Private Sub aLocatePDACollectionIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aLocatePDACollectionIn.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
            If _StoragePDACollectionHeader Is Nothing Then Throw New Exception("*Nincs kijelölve egyetlen PDA kollekció sem!")
            If _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eClosed Then Throw New Exception("*A kollekció már fel lett dolgozva!")
            If _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eOpen Then Throw New Exception("*A kollekció még nem lett lokalizálva!")
            Dim _StorageComputedLocations As StorageComputedLocations = Nothing

            For Each _StoragePDACollectionRows As StoragePDACollectionRows In _StoragePDACollectionHeader.StoragePDACollectionRows
                If _StoragePDACollectionRows.StorageLocation IsNot Nothing Then
                    Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed) _
                                       (CriteriaOperator.Parse("Storage=? AND Products=?", _StoragePDACollectionRows.StorageLocation.Storage, _StoragePDACollectionRows.Products))

                    If _StorageComputed Is Nothing Then Throw New Exception("*Végzetes hiba történt, a folymat megszakadt!")

                    If _StoragePDACollectionRows.Products.ProductType = eProductType.ePacketNumber Or _
                        _StoragePDACollectionRows.Products.ProductType = eProductType.eSerialNumber Then

                        _StorageComputedLocations = _ocur.CreateObject(Of StorageComputedLocations)()

                        _StorageComputedLocations.StorageComputed = _StorageComputed
                        _StorageComputedLocations.QTT = _StoragePDACollectionRows.QTT
                        _StorageComputedLocations.StorageLocation = _StoragePDACollectionRows.StorageLocation

                        _StorageComputedLocations.SerialNumber = _StoragePDACollectionRows.SerialNumber
                        _StorageComputedLocations.PacketNumber = _StoragePDACollectionRows.PacketNumber

                    Else
                        _StorageComputedLocations = _ocur.FindObject(Of StorageComputedLocations)(CriteriaOperator.Parse("StorageComputed=? AND StorageLocation=?", _
                                                                                                                   _StorageComputed, _StoragePDACollectionRows.StorageLocation))
                        If _StorageComputedLocations Is Nothing Then
                            _StorageComputedLocations = _ocur.CreateObject(Of StorageComputedLocations)()

                            _StorageComputedLocations.StorageComputed = _StorageComputed
                            _StorageComputedLocations.QTT = _StoragePDACollectionRows.QTT
                            _StorageComputedLocations.StorageLocation = _StoragePDACollectionRows.StorageLocation
                        Else
                            _StorageComputedLocations.QTT += _StoragePDACollectionRows.QTT
                        End If

                    End If

                End If
            Next
            _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eClosed
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aLocatePDACollectionOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aLocatePDACollectionOut.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
            If _StoragePDACollectionHeader Is Nothing Then Throw New Exception("*Nincs kijelölve egyetlen PDA kollekció sem!")

            'For Each _StoragePDACollectionRows As StoragePDACollectionRows In _StoragePDACollectionHeader.StoragePDACollectionRows
            '    Dim _StorageComputed As StorageComputed = _ocur.FindObject(Of StorageComputed) _
            '                               (CriteriaOperator.Parse("Storage=? AND Products=?", _StoragePDACollectionRows.StorageLocation.Storage, _StoragePDACollectionRows.Products))
            '    Dim _StorageComputedLocations As StorageComputedLocations = _ocur.FindObject(Of StorageComputedLocations)(CriteriaOperator.Parse("StorageLocation=? AND StorageComputed=?", _StoragePDACollectionRows.StorageLocation, _StorageComputed))

            '    ' _StorageComputedLocations.QTT -= _StoragePDACollectionRows.QTT
            'Next
            _StoragePDACollectionHeader.StoragePDACollectionHeaderStatus = eStoragePDACollectionHeaderStatus.eClosed
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub

    Private Sub aGetBarcodeStoragePDA_Execute(sender As Object, e As ParametrizedActionExecuteEventArgs) Handles aGetBarcodeStoragePDA.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

        Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
        Dim _Barcode As String = TryCast(e.ParameterCurrentValue, String)

        TryCast(sender, ParametrizedAction).Value = Nothing

        _Barcode = _Barcode.Replace("ö", "0")

        If LTrim(RTrim(_Barcode)) <> "" Then
            Dim _ProductsBarcodes As ProductsBarcodes = _onew.FindObject(Of ProductsBarcodes)(CriteriaOperator.Parse("StartsWith([Barcode], ?)", _Barcode))
            If _ProductsBarcodes IsNot Nothing Then
                Dim _StoragePDACollectionRows As StoragePDACollectionRows

                _StoragePDACollectionRows = _onew.CreateObject(Of StoragePDACollectionRows)()
                _StoragePDACollectionRows.StoragePDACollectionHeader = _onew.GetObject(_StoragePDACollectionHeader)
                _StoragePDACollectionRows.Barcode = _Barcode
                _StoragePDACollectionRows.Products = _ProductsBarcodes.Products
                If _StoragePDACollectionHeader.row_QTT <= 0 Then
                    _StoragePDACollectionRows.QTT = 1
                Else
                    _StoragePDACollectionRows.QTT = _StoragePDACollectionHeader.row_QTT
                End If
                _StoragePDACollectionRows.QTT_Processed = 0

                _onew.CommitChanges()
            End If
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If

        'Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        'Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
        'Dim _Barcode As String = TryCast(e.ParameterCurrentValue, String)

        'TryCast(sender, ParametrizedAction).Value = Nothing

        'If LTrim(RTrim(_Barcode)) <> "" Then
        '    Dim _ProductsBarcodes As ProductsBarcodes = _ocur.FindObject(Of ProductsBarcodes)(CriteriaOperator.Parse("StartsWith([Barcode], ?)", _Barcode))
        '    If _ProductsBarcodes IsNot Nothing Then
        '        Dim _StoragePDACollectionRows As StoragePDACollectionRows

        '        _StoragePDACollectionRows = _ocur.CreateObject(Of StoragePDACollectionRows)()
        '        _StoragePDACollectionRows.StoragePDACollectionHeader = _StoragePDACollectionHeader
        '        _StoragePDACollectionRows.Barcode = _Barcode
        '        _StoragePDACollectionRows.Products = _ProductsBarcodes.Products
        '        If _StoragePDACollectionHeader.row_QTT <= 0 Then
        '            _StoragePDACollectionRows.QTT = 1
        '        Else
        '            _StoragePDACollectionRows.QTT = _StoragePDACollectionHeader.row_QTT
        '        End If
        '        _StoragePDACollectionRows.QTT_Processed = 0

        '        '_onew.CommitChanges()
        '    End If
        '    'Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        'End If


    End Sub

    Private Sub aImportCSVToStoragePDA_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aImportCSVToStoragePDA.Execute
    End Sub

    Private Sub aPrintReport_StoragePDA_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aPrintReport_StoragePDA.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ReportData_CollectionSource = New CollectionSource(_onew, GetType(ReportData))


        _ReportData_CollectionSource.BeginUpdateCriteria()
        _ReportData_CollectionSource.Criteria("Criteria") = CriteriaOperator.Parse("DataTypeName='Nuvolar.Module.PricingProductsBarcodePrint'")
        _ReportData_CollectionSource.EndUpdateCriteria()

        e.View = Application.CreateListView("ReportData_ListView_Select", _ReportData_CollectionSource, True)
    End Sub
    Private Sub aPrintReport_StoragePDA_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aPrintReport_StoragePDA.Execute
        Dim _ReportData As ReportData = TryCast(e.PopupWindow.View.SelectedObjects(0), ReportData)
        If _ReportData Is Nothing Then Throw New Exception("*Nincs beállítva nyomtatvány!")
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
            '// Le kell generálni a sorokat
            Dim _textStreamReader As StreamReader
            Dim _assembly As [Assembly]
            Dim _SQL As String
            Try
                _assembly = [Assembly].GetExecutingAssembly()
                _textStreamReader = New StreamReader(_assembly.GetManifestResourceStream("Nuvolar.Module.StoragePDAGenerateDataforPrint.sql"))
                _SQL = _textStreamReader.ReadToEnd

                _SQL = String.Format(_SQL, _StoragePDACollectionHeader.Oid.ToString, SecuritySystem.CurrentUserId.ToString)
                _ocur.Session.ExecuteNonQuery(_SQL)

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical)
            End Try

        Catch ext As Exception
            MsgBox(ext.Message, MsgBoxStyle.OkOnly)
        Finally
            RaiseEvent FinalWork()
            Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("MasterKey=?", SecuritySystem.CurrentUserId))
        End Try
    End Sub

    Private Sub aImportPDA_StoragePDA_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aImportPDA_StoragePDA.Execute
        ''Megnyitja a PDA-t és beemeli a sorokat
        'Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        'Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = _onew.GetObjectByKey(Of StoragePDACollectionHeader)(TryCast(View.SelectedObjects(0), StoragePDACollectionHeader).Oid)
        'Dim _Products As Products

        'Try
        '    GLOBAL_HAS_Audit = False
        '    Dim _DeliveryNoteInType As DeliveryNoteInType = _onew.FindObject(Of DeliveryNoteInType)(CriteriaOperator.Parse("IsNull(PDAPath)=False and PDAPath !=''"))

        '    If File.Exists(_DeliveryNoteInType.PDAPath) Then
        '        Dim _StreamFile = New FileStream(_DeliveryNoteInType.PDAPath, FileMode.Open, FileAccess.Read)
        '        Dim _StreamReader As New StreamReader(_StreamFile)
        '        Dim _StrBuffer As String = ""
        '        Dim _LineCount As Long = File.ReadAllLines(_DeliveryNoteInType.PDAPath).Length
        '        _StreamFile.Seek(0, SeekOrigin.Begin)
        '        _StrBuffer = _StreamReader.ReadToEnd()
        '        _StreamReader.Close()
        '        _StreamFile.Close()
        '        File.Delete(_DeliveryNoteInType.PDAPath)
        '        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)
        '        Using parser As New TextFieldParser(New StringReader(_StrBuffer))
        '            parser.TextFieldType = FieldType.Delimited
        '            parser.SetDelimiters(",")
        '            RaiseEvent InitWork(1, 1, _LineCount)
        '            While Not parser.EndOfData
        '                Try
        '                    RaiseEvent DoWork()
        '                    Dim columns As String() = parser.ReadFields()
        '                    _Products = _onew.FindObject(Of Products)(CriteriaOperator.Parse("ProductNumber=?", columns(2).ToString))
        '                    If _Products IsNot Nothing Then
        '                        Dim _StoragePDACollectionRows As StoragePDACollectionRows

        '                        _StoragePDACollectionRows = _onew.CreateObject(Of StoragePDACollectionRows)()
        '                        _StoragePDACollectionRows.StoragePDACollectionHeader = _onew.GetObject(_StoragePDACollectionHeader)
        '                        _StoragePDACollectionRows.Products = _onew.GetObject(_Products)
        '                        _StoragePDACollectionRows.QTT = Val(columns(4))
        '                        _StoragePDACollectionRows.QTT_Processed = 0

        '                        _onew.CommitChanges()
        '                    End If
        '                Catch ex As MalformedLineException
        '                    'MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        '                End Try
        '            End While
        '        End Using
        '        _onew.CommitChanges()
        '        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
        '    End If
        'Catch ex As Exception
        '    MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        'Finally
        '    RaiseEvent FinalWork()
        '    GLOBAL_HAS_Audit = True
        '    Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        'End Try
    End Sub

    Private Sub aToTable_StoragePDA_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aToTable_StoragePDA.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)

        Dim _Start As String

        'Dim watch As Stopwatch = Stopwatch.StartNew()

        If _StoragePDACollectionHeader.row_Products IsNot Nothing Then
            Dim _StoragePDACollectionRows As StoragePDACollectionRows

            _StoragePDACollectionRows = _onew.CreateObject(Of StoragePDACollectionRows)()
            _StoragePDACollectionRows.StoragePDACollectionHeader = _onew.GetObject(_StoragePDACollectionHeader)
            _StoragePDACollectionRows.Barcode = ""
            _StoragePDACollectionRows.Products = _onew.GetObjectByKey(Of Products)(_StoragePDACollectionHeader.row_Products.Oid)
            If _StoragePDACollectionHeader.row_QTT < 0 Then
                _StoragePDACollectionRows.QTT = 1
            Else
                _StoragePDACollectionRows.QTT = _StoragePDACollectionHeader.row_QTT
            End If
            _StoragePDACollectionRows.QTT_Processed = 0

            _onew.CommitChanges()
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
        'watch.Stop()
        'MsgBox(watch.Elapsed.TotalMilliseconds)
    End Sub

    Private Sub aToTableD_StoragePDA_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aToTableD_StoragePDA.Execute
        ' a kiválasztott sorokat törli -----------------------------------------------
        'Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), StoragePDACollectionHeader))
        Dim _StoragePDACollectionRows_Collection As ArrayList = New ArrayList
        Dim _StoragePDACollectionRows_ListEditor As ListPropertyEditor

        Try
            GLOBAL_HAS_Audit = False
            _StoragePDACollectionRows_ListEditor = TryCast(View, DetailView).FindItem("StoragePDACollectionRows")

            For Each _StoragePDACollectionRows As StoragePDACollectionRows In _StoragePDACollectionRows_ListEditor.ListView.SelectedObjects
                _StoragePDACollectionRows_Collection.Add(_onew.GetObject(_StoragePDACollectionRows))
            Next

            _onew.Delete(_StoragePDACollectionRows_Collection)
            _onew.CommitChanges()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        Finally
            GLOBAL_HAS_Audit = True
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End Try
    End Sub

    Private Sub aToTable_StoragePDA_ocur_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aToTable_StoragePDA_ocur.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)

        If _StoragePDACollectionHeader.row_Products IsNot Nothing Then
            Dim _StoragePDACollectionRows As StoragePDACollectionRows
            _StoragePDACollectionRows = _ocur.CreateObject(Of StoragePDACollectionRows)()
            _StoragePDACollectionRows.StoragePDACollectionHeader = _ocur.GetObject(_StoragePDACollectionHeader)
            _StoragePDACollectionRows.Barcode = ""
            _StoragePDACollectionRows.Products = _ocur.GetObjectByKey(Of Products)(_StoragePDACollectionHeader.row_Products.Oid)
            _StoragePDACollectionRows.RowIndex = _StoragePDACollectionHeader.StoragePDACollectionRows.Count + 1
            If _StoragePDACollectionHeader.row_QTT < 0 Then
                _StoragePDACollectionRows.QTT = 1
            Else
                _StoragePDACollectionRows.QTT = _StoragePDACollectionHeader.row_QTT
            End If
            _StoragePDACollectionRows.QTT_Processed = 0
        End If
    End Sub

    Private Sub aToTableD_StoragePDA_ocur_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aToTableD_StoragePDA_ocur.Execute
        ' a kiválasztott sorokat törli -----------------------------------------------

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
        Dim _StoragePDACollectionRows_Collection As ArrayList = New ArrayList
        Dim _StoragePDACollectionRows_ListEditor As ListPropertyEditor
        Try
            _StoragePDACollectionRows_ListEditor = TryCast(View, DetailView).FindItem("StoragePDACollectionRows")
            For Each _StoragePDACollectionRows As StoragePDACollectionRows In _StoragePDACollectionRows_ListEditor.ListView.SelectedObjects
                _StoragePDACollectionRows_Collection.Add(_ocur.GetObject(_StoragePDACollectionRows))
            Next
            _ocur.Delete(_StoragePDACollectionRows_Collection)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        Finally
        End Try
    End Sub

    Private Sub aGetBarcodeStoragePDA_ocur_Execute(sender As Object, e As ParametrizedActionExecuteEventArgs) Handles aGetBarcodeStoragePDA_ocur.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
        Dim _Barcode As String = TryCast(e.ParameterCurrentValue, String)

        TryCast(sender, ParametrizedAction).Value = Nothing

        If LTrim(RTrim(_Barcode)) <> "" Then
            Dim _ProductsBarcodes As ProductsBarcodes = _ocur.FindObject(Of ProductsBarcodes)(CriteriaOperator.Parse("StartsWith([Barcode], ?)", _Barcode), True)
            If _ProductsBarcodes IsNot Nothing Then
                Dim _StoragePDACollectionRows As StoragePDACollectionRows


                _StoragePDACollectionRows = _ocur.CreateObject(Of StoragePDACollectionRows)()
                _StoragePDACollectionRows.StoragePDACollectionHeader = _StoragePDACollectionHeader
                _StoragePDACollectionRows.Barcode = _Barcode
                _StoragePDACollectionRows.Products = _ProductsBarcodes.Products
                _StoragePDACollectionRows.RowIndex = _StoragePDACollectionHeader.StoragePDACollectionRows.Count + 1
                If _StoragePDACollectionHeader.row_QTT <= 0 Then
                    _StoragePDACollectionRows.QTT = 1
                Else
                    _StoragePDACollectionRows.QTT = _StoragePDACollectionHeader.row_QTT
                End If
                _StoragePDACollectionRows.QTT_Processed = 0

            End If
        End If
    End Sub

    Private Sub aImportPDA_StoragePDA_ocur_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aImportPDA_StoragePDA_ocur.Execute
        ''Megnyitja a PDA-t és beemeli a sorokat
        'Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        'Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
        'Dim _Products As Products

        'Try
        '    Dim _DeliveryNoteInType As DeliveryNoteInType = _ocur.FindObject(Of DeliveryNoteInType)(CriteriaOperator.Parse("IsNull(PDAPath)=False and PDAPath !=''"), True)

        '    If File.Exists(_DeliveryNoteInType.PDAPath) Then
        '        Dim _StreamFile = New FileStream(_DeliveryNoteInType.PDAPath, FileMode.Open, FileAccess.Read)
        '        Dim _StreamReader As New StreamReader(_StreamFile)
        '        Dim _StrBuffer As String = ""
        '        Dim _LineCount As Long = File.ReadAllLines(_DeliveryNoteInType.PDAPath).Length
        '        _StreamFile.Seek(0, SeekOrigin.Begin)
        '        _StrBuffer = _StreamReader.ReadToEnd()
        '        _StreamReader.Close()
        '        _StreamFile.Close()
        '        File.Delete(_DeliveryNoteInType.PDAPath)
        '        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)
        '        Using parser As New TextFieldParser(New StringReader(_StrBuffer))
        '            parser.TextFieldType = FieldType.Delimited
        '            parser.SetDelimiters(",")
        '            RaiseEvent InitWork(1, 1, _LineCount)
        '            While Not parser.EndOfData
        '                Try
        '                    RaiseEvent DoWork()
        '                    Dim columns As String() = parser.ReadFields()
        '                    _Products = _ocur.FindObject(Of Products)(CriteriaOperator.Parse("ProductNumber=?", columns(2).ToString))
        '                    If _Products IsNot Nothing Then
        '                        Dim _StoragePDACollectionRows As StoragePDACollectionRows

        '                        _StoragePDACollectionRows = _ocur.CreateObject(Of StoragePDACollectionRows)()
        '                        _StoragePDACollectionRows.StoragePDACollectionHeader = _ocur.GetObject(_StoragePDACollectionHeader)
        '                        _StoragePDACollectionRows.Products = _ocur.GetObject(_Products)
        '                        _StoragePDACollectionRows.QTT = Val(columns(4))
        '                        _StoragePDACollectionRows.QTT_Processed = 0
        '                        _StoragePDACollectionRows.RowIndex = _StoragePDACollectionHeader.StoragePDACollectionRows.Count + 1
        '                    End If
        '                Catch ex As MalformedLineException
        '                    'MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        '                End Try
        '            End While
        '        End Using
        '    End If
        'Catch ex As Exception
        '    MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        'Finally
        '    RaiseEvent FinalWork()
        'End Try
    End Sub

    Private Sub aGeneratePDARowsByPricingActionHeader_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aGeneratePDARowsByPricingActionHeader.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _StoragePDACollectionProductSelectPopup As StoragePDACollectionProductSelectPopup = _onew.CreateObject(Of StoragePDACollectionProductSelectPopup)()

        Dim _PricingActionHeader_Collection As New XPCollection(Of PricingActionHeader)(_onew.Session, CriteriaOperator.Parse("PricingActionType=2"))
        Dim _PricingActionHeader As PricingActionHeader
        _StoragePDACollectionProductSelectPopup.DateExecution = Now

        For Each _PricingActionHeader In _PricingActionHeader_Collection
            _StoragePDACollectionProductSelectPopup.PricingActionHeader.Add(_PricingActionHeader)
        Next

        e.View = Application.CreateDetailView(_onew, "StoragePDACollectionProductSelectPopup_DetailView", True, _StoragePDACollectionProductSelectPopup)

    End Sub

    Private Sub aGeneratePDARowsByPricingActionHeader_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aGeneratePDARowsByPricingActionHeader.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
        Dim _StoragePDACollectionProductSelectPopup As StoragePDACollectionProductSelectPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), StoragePDACollectionProductSelectPopup)

        Dim _ListPropertyEditor As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("PricingActionHeader")

        If _ListPropertyEditor IsNot Nothing Then
            For Each _PricinhActionHeader As PricingActionHeader In _ListPropertyEditor.ListView.SelectedObjects
                Dim _textStreamReader As StreamReader
                Dim _assembly As [Assembly]
                Dim _SQL As String
                Try
                    _assembly = [Assembly].GetExecutingAssembly()
                    _textStreamReader = New StreamReader(_assembly.GetManifestResourceStream("Nuvolar.Module.StoragePDAGenerateDatafromPricingActionHeader.sql"))
                    _SQL = _textStreamReader.ReadToEnd

                    _SQL = String.Format(_SQL, _PricinhActionHeader.Oid.ToString, _StoragePDACollectionHeader.Oid.ToString, Format(_StoragePDACollectionProductSelectPopup.DateExecution.Date, "yyyy-MM-dd"), _StoragePDACollectionProductSelectPopup.IsOvverides.ToString)
                    _ocur.Session.ExecuteNonQuery(_SQL)

                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Critical)
                End Try

            Next
        End If

        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()


    End Sub

    Private Sub aGeneratePDARowsByDiscount_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aGeneratePDARowsByDiscount.CustomizePopupWindowParams
        'Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        'Dim _PricingProductsBarcodeCustomers_CollectionSource = New CollectionSource(_onew, GetType(PricingProductsBarcodeCustomers))


        '_PricingProductsBarcodeCustomers_CollectionSource.BeginUpdateCriteria()
        '_PricingProductsBarcodeCustomers_CollectionSource.Criteria("Criteria") = CriteriaOperator.Parse("StartsWith([Barcode], '99') And [ExpirationDate] >= LocalDateTimeNow() And [ActualDate] <= LocalDateTimeNow() And (BarcodeType='' or IsNull(BarcodeType)=True)")
        '_PricingProductsBarcodeCustomers_CollectionSource.EndUpdateCriteria()

        'e.View = Application.CreateListView("PricingProductsBarcodeCustomers_ListView_Select", _PricingProductsBarcodeCustomers_CollectionSource, True)
    End Sub

    Private Sub aGeneratePDARowsByDiscount_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aGeneratePDARowsByDiscount.Execute
        'Dim _StoragePDACollectionHeader As StoragePDACollectionHeader = TryCast(View.SelectedObjects(0), StoragePDACollectionHeader)
        'Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        'RaiseEvent InitWork(1, 1, TryCast(e.PopupWindow.View, ListView).CollectionSource.List.Count)
        'For Each _PricingProductsBarcodeCustomers As PricingProductsBarcodeCustomers In TryCast(e.PopupWindow.View, ListView).CollectionSource.List
        '    RaiseEvent DoWork()
        '    If _PricingProductsBarcodeCustomers.NonPersistent_QTT > 0 Then
        '        Dim _StoragePDACollectionRows As StoragePDACollectionRows = _onew.CreateObject(Of StoragePDACollectionRows)()
        '        _StoragePDACollectionRows.StoragePDACollectionHeader = _onew.GetObject(_StoragePDACollectionHeader)
        '        _StoragePDACollectionRows.Barcode = _PricingProductsBarcodeCustomers.Barcode
        '        _StoragePDACollectionRows.Products = _onew.GetObject(_PricingProductsBarcodeCustomers.Products)
        '        _StoragePDACollectionRows.QTT = _PricingProductsBarcodeCustomers.NonPersistent_QTT
        '        _StoragePDACollectionRows.RowIndex = 0
        '        _onew.CommitChanges()
        '    End If
        'Next
        'RaiseEvent FinalWork()
        'Frame.GetController(Of RefreshController).RefreshAction.DoExecute()

    End Sub
End Class
