Imports Microsoft.VisualBasic
Imports System
Imports System.Linq
Imports System.Text
Imports DevExpress.ExpressApp
Imports DevExpress.Data.Filtering
Imports System.Collections.Generic
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.Layout
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Templates
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Model.NodeGenerators
Imports DevExpress.Xpo
Imports System.Net.Mail

' For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppViewControllertopic.
Public Class OffersOutVC
    Inherits ViewController
    Protected _selection As New ArrayList
    Protected _noselect As Boolean = False


    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of OffersOutVC).aOffersOutFinalWidthVersion.Active.SetItemValue("", False) 'Véglegesítés verziószámmal
        Frame.GetController(Of OffersOutVC).aOffersOut_ChangeRows.Active.SetItemValue("", False) '--Sorok modosítása
        Frame.GetController(Of OffersOutVC).aOffersOutFinal.Active.SetItemValue("", False) ' -- Véglegesítés (OfferOutHeader_DetailView)
        Frame.GetController(Of OffersOutVC).aTotable_OfferOut.Active.SetItemValue("", False) ' -- Sor hozzáadása (OfferOutHeader_OfferOutRows_ListView)
        Frame.GetController(Of OffersOutVC).aTotableD_OfferOut.Active.SetItemValue("", False) ' -- Sor törlése (OfferOutHeader_OfferOutRows_ListView)
        Frame.GetController(Of OffersOutVC).aOrderOfferedRows.Active.SetItemValue("", False) ' -- Ajánlat megrendelése (NINCS KIDOLGOZVA)
        Frame.GetController(Of OffersOutVC).aUp_OfferOut.Active.SetItemValue("", False) ' kiválasztott sor fel
        Frame.GetController(Of OffersOutVC).aDown_OfferOut.Active.SetItemValue("", False) ' kiválasztott sor le
        Frame.GetController(Of OffersOutVC).aCloneOfferOut.Active.SetItemValue("", False) ' Kiválasztott Kimenő ajánlat klónozása
        Frame.GetController(Of OffersOutVC).aCreateOrderIn_From_OfferOut.Active.SetItemValue("", False) ' A kiválasztott kimenő árajánlatokból bejövő megrendeléseket készít
        Frame.GetController(Of OffersOutVC).aCloneOfferOut_CRM.Active.SetItemValue("", False) 'A kiválasztott kimenő ajánlatot klónozza le egy másik CRM_Op.ra
        Frame.GetController(Of OffersOutVC).aImportRecipe.Active.SetItemValue("", False) 'Recept import
        Frame.GetController(Of OffersOutVC).aInsert_Discount_RowOther.Active.SetItemValue("", False) 'Egyedi kedvezmény sor beszúrása
        Frame.GetController(Of OffersOutVC).aInsertServicesToOfferOut.Active.SetItemValue("", False) 'Szolgáltatás hozzáadása sorként
        Frame.GetController(Of OffersOutVC).aBackToRowEdit_OfferOut.Active.SetItemValue("", False)
        Frame.GetController(Of OffersOutVC).aOfferOut_ChangeState.Active.SetItemValue("", False)
        '------------------------- LISTVIEWS --------------------------------------------------------------------------------------------------------------------------------------
        If View.Id = "OfferOutHeader_OfferOutCostRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
        End If

        If View.Id = "OfferOutHeader_ListView_Editable" Then
            Frame.GetController(Of OffersOutVC).aCloneOfferOut.Active.SetItemValue("", True)
            Frame.GetController(Of OffersOutVC).aCloneOfferOut_CRM.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OfferOutHeader_ListView_NotEditable" Or _
            View.Id = "CRM_Opportunities_OfferOutHeader_ListView" Then
            Frame.GetController(Of OffersOutVC).aCloneOfferOut.Active.SetItemValue("", True)
            Frame.GetController(Of OffersOutVC).aCreateOrderIn_From_OfferOut.Active.SetItemValue("", True)
            Frame.GetController(Of OffersOutVC).aCloneOfferOut_CRM.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutVC).aOfferOut_ChangeState.Active.SetItemValue("", True)

            AddHandler View.SelectionChanged, AddressOf OfferOutHeader_OfferOutRows_ListView_SelectionChanged
        End If
        If View.Id = "OfferOutHeader_OfferOutRows_ListView" Then
            Frame.GetController(Of OffersOutVC).aOffersOut_ChangeRows.Active.SetItemValue("", True)
            AddHandler View.SelectionChanged, AddressOf OfferOutHeader_OfferOutRows_ListView_SelectionChanged
        End If
        If View.Id = "CRM_Opportunities_OfferOutHeader_ListView" Then
            Frame.GetController(Of OffersOutVC).aCloneOfferOut.Active.SetItemValue("", True)
            Frame.GetController(Of OffersOutVC).aCloneOfferOut_CRM.Active.SetItemValue("", True)
        End If
        If View.Id = "OfferOutHeader_OfferOutRows_ListView" Then ' -- A beágyazott ListView feletti toolbar
            Frame.GetController(Of OffersOutVC).aUp_OfferOut.Active.SetItemValue("", True)
            Frame.GetController(Of OffersOutVC).aDown_OfferOut.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutVC).aImportRecipe.Active.SetItemValue("", True) 'Recept import
            Frame.GetController(Of OffersOutVC).aInsertServicesToOfferOut.Active.SetItemValue("", True)
        End If
        If View.Id Like "OrderInHeaderFromOfferOutRowsHeader_OrderInHeaderFromOfferOutRowsRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If

        If View.Id = "ProductFinderVariable_ProductSelectOriginal_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If
        If View.Id = "ProductFinderVariable_ProductSelectVariables_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OfferOutHeader_ListView_Support" Then
            Frame.GetController(Of OffersOutVC).aBackToEditOfferOut.Active.SetItemValue("", False)
            'Frame.GetController(Of OffersOutVC).a()
        End If
        If View.Id = "OfferOutHeader_OfferInHeader_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
        End If
        '------------------------- DETAILVIEWS ------------------------------------------------------------------------------------------------------------------------------------
        If View.Id = "OfferOutHeader_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutVC).aOffersOutFinalWidthVersion.Active.SetItemValue("", True)

            Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
            If _OfferOutHeader IsNot Nothing Then
                If _OfferOutHeader.IsEditable = False Then ' -- Az ajánlat véglegesített vagy szerkeszthetõ
                    Frame.GetController(Of OffersOutVC).aOffersOutFinal.Active.SetItemValue("", False)
                    Frame.GetController(Of OffersOutVC).aOffersOutFinalWidthVersion.Active.SetItemValue("", False)
                Else
                    Frame.GetController(Of OffersOutVC).aOffersOutFinal.Active.SetItemValue("", True)
                    Frame.GetController(Of OffersOutVC).aTotable_OfferOut.Active.SetItemValue("", True)
                    Frame.GetController(Of OffersOutVC).aTotableD_OfferOut.Active.SetItemValue("", True)
                    Frame.GetController(Of OffersOutVC).aInsert_Discount_RowOther.Active.SetItemValue("", True)
                    Frame.GetController(Of OffersOutVC).aBackToRowEdit_OfferOut.Active.SetItemValue("", True)
                End If
            End If
        End If
        If View.Id = "OrderInHeaderFromOfferOutRowsHeader_DetailView" Or View.Id = "OffersOutCloneForCRM_DetailView" Then
            AddHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "ProductFinderVariable_DetailView" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If

        If View.Id = "OfferOutHeader_ListView_Support" Then 'WEB Supportnál ajánlatok listázása
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User = ?", _ocur.GetObject(SecuritySystem.CurrentUser)))

            If _Contacts IsNot Nothing Then
                If _Contacts.Customers IsNot Nothing Then
                    TryCast(View, ListView).CollectionSource.Criteria("ByCustomers") = CriteriaOperator.Parse("Customers = ?", _Contacts.Customers)
                Else
                    TryCast(View, ListView).CollectionSource.Criteria("ByCustomers") = CriteriaOperator.Parse("1=2")
                End If
            Else
                TryCast(View, ListView).CollectionSource.Criteria("ByCustomers") = CriteriaOperator.Parse("1=2")
            End If
        End If
        If View.Id = "OfferInHeader_ListView_OfferOutHeaderInHeader" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        '------------------------- LISTVIEWS --------------------------------------------------------------------------------------------------------------------------------------
        If View.Id = "OfferOutHeader_OfferOutCostRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OfferOutHeader_OfferOutRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        End If

        If View.Id = "OfferOutHeader_ListView_Editable" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OfferOutHeader_ListView_NotEditable" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of OffersOutVC).aOfferOut_ChangeState.Active.SetItemValue("", False)

            RemoveHandler View.SelectionChanged, AddressOf OfferOutHeader_OfferOutRows_ListView_SelectionChanged
        End If
        If View.Id = "OfferOutHeader_OfferOutRows_ListView" Then
            Frame.GetController(Of OffersOutVC).aOffersOut_ChangeRows.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutVC).aImportRecipe.Active.SetItemValue("", False) 'Recept import
        End If
        If View.Id Like "OrderInHeaderFromOfferOutRowsHeader_OrderInHeaderFromOfferOutRowsRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
        If View.Id = "ProductFinderVariable_ProductSelectOriginal_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
        If View.Id = "ProductFinderVariable_ProductSelectVariables_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OfferOutHeader_ListView_Support" Then
            Frame.GetController(Of OffersOutVC).aBackToEditOfferOut.Active.SetItemValue("", True)
        End If
        If View.Id = "OfferOutHeader_OfferInHeader_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        End If
        '------------------------- DETAILVIEWS ------------------------------------------------------------------------------------------------------------------------------------
        If View.Id = "OfferOutHeader_DeatilView" Then
            Frame.GetController(Of OffersOutVC).aOffersOutFinal.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutVC).aOffersOutFinalWidthVersion.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutVC).aTotable_OfferOut.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutVC).aTotableD_OfferOut.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutVC).aBackToRowEdit_OfferOut.Active.SetItemValue("", False)
            Frame.GetController(Of OffersOutVC).aOffersOutFinalWidthVersion.Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OrderInHeaderFromOfferOutRowsHeader_DetailView" Or View.Id = "OffersOutCloneForCRM_DetailView" Then
            RemoveHandler Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DialogController)().AcceptAction.Executing, AddressOf AcceptAction_Executing
        End If
        If View.Id = "OfferInHeader_ListView_OfferOutHeaderInHeader" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        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 OfferOutHeader_OfferOutRows_ListView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If View Is Nothing Then Exit Sub
        If View.SelectedObjects.Count > 0 Then
            _selection.Clear()
            For Each _OfferOutRows In View.SelectedObjects
                _selection.Add(_OfferOutRows)
            Next
        End If
    End Sub
    Private Sub aOffersOutFinal_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOffersOutFinal.Execute
        ' Kimenő Árajánlat véglegesítése ------------------------------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _OfferOutHeader As OfferOutHeader = TryCast(e.SelectedObjects(0), OfferOutHeader)

        If _OfferOutHeader.OfferOutRows.Count > 0 Then
            _OfferOutHeader.RecalculateOfferOut(_OfferOutHeader, _ocur)
            If _OfferOutHeader.DocumentNumber = "" Then
                _OfferOutHeader.DocumentNumber = _OfferOutHeader.OfferOutParameters.NumberGenerator.GetNewNumber(_OfferOutHeader.OfferOutParameters.NumberGenerator)
            End If
            _OfferOutHeader.IsEditable = False
            _OfferOutHeader.Save()

            '// Speciális Task beadása, hogy az árajánlat kiadásáról Task keletkezzen AlertPeriod-dal

            If _OfferOutHeader.CRM_Opportunities IsNot Nothing Then

                Dim _CRM_Leads_Activity_Task As CRM_Leads_Activity_Task
                Dim _CRM_Leads_Activity As CRM_Leads_Activity

                '// Minden korábban kiadott árajánlathoz tartozó figyelmeztetés visszaállítása

                Dim _CRM_Leads_Activity_Collection As New XPCollection(Of CRM_Leads_Activity)(_ocur.Session, _
                            CriteriaOperator.Parse("Subject='Érdeklődés a kiadott ajánlatról' and CRM_Leads=? and CRM_Opportunities=?", _
                            _OfferOutHeader.CRM_Opportunities.CRM_Leads, _OfferOutHeader.CRM_Opportunities))

                For Each _CRM_Leads_Activity In _CRM_Leads_Activity_Collection
                    For Each _CRM_Leads_Activity_Task In _CRM_Leads_Activity.CRM_Leads_Activity_Task
                        _CRM_Leads_Activity_Task.Status = General.TaskStatus.Deferred
                        _CRM_Leads_Activity_Task.Results = "Új ajánlat kiadva"
                    Next
                Next

                _CRM_Leads_Activity = _ocur.CreateObject(Of CRM_Leads_Activity)()
                _CRM_Leads_Activity.CRM_Leads = _OfferOutHeader.CRM_Opportunities.CRM_Leads
                _CRM_Leads_Activity.CRM_Opportunities = _OfferOutHeader.CRM_Opportunities
                _CRM_Leads_Activity.EventType = eEventType.eAlert
                _CRM_Leads_Activity.HRPerson = _OfferOutHeader.CRM_Opportunities.HRPerson
                _CRM_Leads_Activity.StartOn = DateAdd(DateInterval.Day, 3, GetSQLTime(_ocur.Session))
                _CRM_Leads_Activity.EndOn = DateAdd(DateInterval.Hour, 1, _CRM_Leads_Activity.StartOn)
                _CRM_Leads_Activity.Subject = "Érdeklődés a kiadott ajánlatról"


                _CRM_Leads_Activity_Task = _ocur.CreateObject(Of CRM_Leads_Activity_Task)()
                _CRM_Leads_Activity_Task.CRM_Leads_Activity = _CRM_Leads_Activity
                _CRM_Leads_Activity_Task.HRPerson = _OfferOutHeader.CRM_Opportunities.HRPerson
                _CRM_Leads_Activity_Task.DueDate = DateAdd(DateInterval.Day, 3, GetSQLTime(_ocur.Session))
                _CRM_Leads_Activity_Task.Status = General.TaskStatus.NotStarted
                _CRM_Leads_Activity_Task.Subject = "Érdeklődés a kiadott ajánlatról"
                _CRM_Leads_Activity_Task.Description = "-"
                _CRM_Leads_Activity_Task.AlertPeriod = _OfferOutHeader.OfferOutParameters.AlertPeriod



            End If


            'Email küldés

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

                Dim _MailMessage As New MailMessage
                Dim _SmtpClient As SmtpClient = New SmtpClient(_WebParameters.SMTPHost)
                _MailMessage.From = New MailAddress(_WebParameters.Email_SupportAdmin)
                If Not String.IsNullOrEmpty(_OfferOutHeader.Contact.Email) Then
                    _MailMessage.To.Add(_OfferOutHeader.Contact.Email)
                ElseIf Not String.IsNullOrEmpty(_OfferOutHeader.Customers.Email) Then
                    _MailMessage.To.Add(_OfferOutHeader.Customers.Email)
                Else
                    Throw New Exception("*Nincs email cím beállítva!")
                End If

                _MailMessage.Subject = "Új ajánlat érkezett"
                _MailMessage.IsBodyHtml = True
                _MailMessage.Body = String.Format("{0} számmal új ajánlat készült az Ön(ök) részére.", _OfferOutHeader.DocumentNumber)

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

                _SmtpClient.Send(_MailMessage)

                If _OfferOutHeader.UserCreated IsNot Nothing Then
                    If Not String.IsNullOrEmpty(_OfferOutHeader.UserCreated.Email) Then
                        _MailMessage.To.Clear()
                        _MailMessage.To.Add(_OfferOutHeader.UserCreated.Email)
                        _MailMessage.Body = String.Format("Ön {0} számmal új ajánlatot véglegesített a(z) {1} ügyfél részére {2} kapcsolattartónak.", _
                                                          _OfferOutHeader.DocumentNumber, _OfferOutHeader.Customers.FullName, _OfferOutHeader.Contact.FullName)

                        _SmtpClient.Send(_MailMessage)
                    End If
                End If
            Catch ex As Exception
                Dim sf As StackFrame = New StackFrame()
                Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
                Dim _ErrorLog As New ErrorLog(_uow)
                _ErrorLog.Description = ex.Message
                _ErrorLog.SubName = TryCast(sf.GetMethod(), System.Reflection.MethodBase).Name
                _uow.CommitTransaction()
            End Try

            _ocur.CommitChanges()
            View.Close()

        Else
            '-------------Teszt miatt------------------------------
            Throw New Exception("*Nincs egy sora sem a kimenő ajánlatnak, így nem lehet véglegesíteni!")
        End If
    End Sub
    Private Sub aTotable_OfferOut_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTotable_OfferOut.Execute
        ' hozzáad egy új sort a fejléchez ------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = CType(e.SelectedObjects(0), OfferOutHeader)
        Dim _OfferOutRows As OfferOutRows = New OfferOutRows(_ocur.Session)
        _OfferOutRows.OfferOutHeader = _OfferOutHeader

        Dim ItemToFocus As ViewItem = TryCast(View, DetailView).FindItem("row_Products")
        If ItemToFocus IsNot Nothing Then
            Dim ItemControl As Object
            ItemControl = ItemToFocus.Control
            ItemControl.Focus()
        End If

        _OfferOutRows.AddRows(_OfferOutHeader)
        _ocur.CommitChanges()

        _OfferOutHeader.row_Installation = Nothing
        _OfferOutHeader.row_Products = Nothing
        _OfferOutHeader.row_ShortName = ""
        _OfferOutHeader.row_QTT = 0
        _OfferOutHeader.row_ListPriceHUF = 0
        _OfferOutHeader.row_ListPriceDEV = 0
        _OfferOutHeader.row_DiscountPercent = 0


        View.Refresh()
    End Sub
    Private Sub aTotableD_OfferOut_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTotableD_OfferOut.Execute
        ' a kiválasztott sorokat törli -----------------------------------------------
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(e.SelectedObjects(0), OfferOutHeader)
        Dim _OfferOutRows_Collection As ArrayList = New ArrayList
        Dim _OfferOutRows_ListEditor As ListPropertyEditor

        _OfferOutRows_ListEditor = TryCast(View, DetailView).FindItem("OfferOutRows")
        For Each _OfferOutRows As OfferOutRows In _OfferOutRows_ListEditor.ListView.SelectedObjects
            Dim _OffOutRows As OfferOutRows = _OfferOutRows
            For Each _OfferOutRows2 As OfferOutRows In _OfferOutHeader.OfferOutRows
                If _OfferOutRows2.RowIndex > _OffOutRows.RowIndex Then
                    _OfferOutRows2.RowIndex -= 1
                End If
            Next
        Next

        For Each _OfferOutRows As OfferOutRows In _OfferOutRows_ListEditor.ListView.SelectedObjects
            _OfferOutRows_Collection.Add(_OfferOutRows)
        Next

        _ocur.Delete(_OfferOutRows_Collection)
        _ocur.CommitChanges()
        View.Refresh()
    End Sub
    Private Sub aOrderOfferedRows_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aOrderOfferedRows.Execute


    End Sub
    Private Sub aUp_OfferOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aUp_OfferOut.Execute
        ' A kiválaszott sort egy sorral feljebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutRows).OfferOutHeader
        Dim _OfferOutRowsSelected As OfferOutRows
        Dim _OfferOutRowsMinus As OfferOutRows
        Dim _OfferOutRows As OfferOutRows

        Try
            _OfferOutRowsSelected = TryCast(View.SelectedObjects(0), OfferOutRows) ' Kiveszi a kijelölt sorok közül az elsőt
        Catch
            ' TESZT MIATT!!!! ----------------------------------------------------------------
            Throw New Exception("*Nincs sor kiválasztva, vagy nem a sor kijelölése az aktív!")
        End Try

        If _OfferOutRowsSelected.RowIndex > 0 Then
            For Each _OfferOutRows In _OfferOutHeader.OfferOutRows
                If _OfferOutRows.RowIndex = _OfferOutRowsSelected.RowIndex - 1 Then
                    _OfferOutRowsMinus = _OfferOutRows
                    Exit For
                End If
            Next

            If _OfferOutRowsMinus IsNot Nothing Then
                _OfferOutRowsSelected.RowIndex -= 1
                _OfferOutRowsMinus.RowIndex += 1
            End If
            _OfferOutRowsMinus.Save()
            _OfferOutRowsSelected.Save()
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aDown_OfferOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aDown_OfferOut.Execute
        ' A kiválaszott sort egy sorral lejebb helyezi
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.CurrentObject, OfferOutRows).OfferOutHeader
        Dim _OfferOutRowsSelected As OfferOutRows
        Dim _OfferOutRowsPlus As OfferOutRows
        Dim _OfferOutRows As OfferOutRows

        Try
            _OfferOutRowsSelected = TryCast(View.SelectedObjects(0), OfferOutRows) ' Kiveszi a kijelölt sorok közül az elsőt
        Catch
            ' TESZT MIATT!!!! ----------------------------------------------------------------
            Throw New Exception("*Nincs sor kiválasztva, vagy nem a sor kijelölése az aktív!")
        End Try

        If _OfferOutRowsSelected.RowIndex < _OfferOutHeader.OfferOutRows.Count - 1 Then
            For Each _OfferOutRows In _OfferOutHeader.OfferOutRows
                If _OfferOutRows.RowIndex = _OfferOutRowsSelected.RowIndex + 1 Then
                    _OfferOutRowsPlus = _OfferOutRows
                    Exit For
                End If
            Next

            If _OfferOutRowsPlus IsNot Nothing Then
                _OfferOutRowsSelected.RowIndex += 1
                _OfferOutRowsPlus.RowIndex -= 1
            End If
            _OfferOutRowsPlus.Save()
            _OfferOutRowsSelected.Save()
            _ocur.CommitChanges()
            View.Refresh()
        End If
    End Sub
    Private Sub aCloneOfferOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aCloneOfferOut.Execute
        ' Az akció egy kiválasztott kimenő ajánlatot klónoz,a hozzá tartozó sorokkal, de lehet benne módosítani -------------------------------------------
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferOutHeader As OfferOutHeader = _onew.GetObject(TryCast(e.SelectedObjects(0), OfferOutHeader))
        Dim _OfferOutHeader_New As OfferOutHeader = _onew.CreateObject(Of OfferOutHeader)()
        Dim _OfferOutRows As OfferOutRows
        Dim _criteria As String
        If _OfferOutHeader.DocumentNumber.Contains(" -> ") Then
            _criteria = _OfferOutHeader.DocumentNumber.Remove(InStr(_OfferOutHeader.DocumentNumber, "->") - 2, _OfferOutHeader.DocumentNumber.Length - InStr(_OfferOutHeader.DocumentNumber, "->") + 2)
        Else
            _criteria = _OfferOutHeader.DocumentNumber
        End If
        Dim _OfferOutHeader_Collection As New XPCollection(Of OfferOutHeader)(_onew.Session, CriteriaOperator.Parse("DocumentNumber like '" & _criteria & "%'"))

        ' -------------------- Fejléc adatok átadása ----------------------------------------
        _OfferOutHeader_New.Contact = _OfferOutHeader.Contact
        _OfferOutHeader_New.CRM_Opportunities = _OfferOutHeader.CRM_Opportunities
        _OfferOutHeader_New.CurrencyName = _OfferOutHeader.CurrencyName
        _OfferOutHeader_New.Customers = _OfferOutHeader.Customers
        _OfferOutHeader_New.CustomersFrom = _OfferOutHeader.CustomersFrom
        _OfferOutHeader_New.DateCreated = _OfferOutHeader.DateCreated
        _OfferOutHeader_New.DateExecution = _OfferOutHeader.DateExecution
        _OfferOutHeader_New.DatePayment = _OfferOutHeader.DatePayment
        _OfferOutHeader_New.Description = _OfferOutHeader.Description
        _OfferOutHeader_New.DateDue = _OfferOutHeader.DateDue
        _OfferOutHeader_New.ObjectCreated = _OfferOutHeader.ObjectCreated
        _OfferOutHeader_New.OfferLOSTReason = _OfferOutHeader.OfferLOSTReason
        _OfferOutHeader_New.OfferLOSTReasonDescription = _OfferOutHeader.OfferLOSTReasonDescription
        _OfferOutHeader_New.OfferOutParameters = _OfferOutHeader.OfferOutParameters
        _OfferOutHeader_New.OfferOutState = _OfferOutHeader.OfferOutState
        _OfferOutHeader_New.PaymentOption = _OfferOutHeader.PaymentOption
        _OfferOutHeader_New.QualityOption = _OfferOutHeader.QualityOption
        _OfferOutHeader_New.ShipmentOption = _OfferOutHeader.ShipmentOption

        If _OfferOutHeader_Collection.Count > 0 Then
            If _OfferOutHeader_Collection.Count = 1 Then
                _OfferOutHeader_New.DocumentNumber = _OfferOutHeader.DocumentNumber + " -> " + _OfferOutHeader_Collection.Count.ToString
            Else
                _OfferOutHeader_New.DocumentNumber = _criteria + " -> " + _OfferOutHeader_Collection.Count.ToString
            End If
        End If

        _OfferOutHeader_New.IsEditable = True
        _OfferOutHeader_New.IsStorno = False
        _OfferOutHeader_New.DocumentBody = _OfferOutHeader.DocumentBody
        _OfferOutHeader_New.ExcelFile = _OfferOutHeader.ExcelFile
        _OfferOutHeader_New.ExcelProperty = _OfferOutHeader.ExcelProperty
        _OfferOutHeader_New.OtherCostPercent = _OfferOutHeader.OtherCostPercent

        _OfferOutHeader_New.Save()
        ' -------------------- Sor(ok) átadása-----------------------------------------------
        For Each _OfferOutRows In _OfferOutHeader.OfferOutRows
            Dim _OfferOutRows_New As OfferOutRows = _onew.CreateObject(Of OfferOutRows)()
            _OfferOutRows_New.OfferOutHeader = _OfferOutHeader_New
            _OfferOutRows_New.CustomsTariffs = _OfferOutRows.CustomsTariffs
            _OfferOutRows_New.Description = _OfferOutRows.Description
            _OfferOutRows_New.DescriptionPrivate = _OfferOutRows.DescriptionPrivate
            _OfferOutRows_New.DiscountPercent = _OfferOutRows.DiscountPercent
            _OfferOutRows_New.Installation = _OfferOutRows.Installation
            _OfferOutRows_New.ListPriceDEV = _OfferOutRows.ListPriceDEV
            _OfferOutRows_New.ListPriceHUF = _OfferOutRows.ListPriceHUF
            _OfferOutRows_New.FinalPriceDEV = _OfferOutRows.FinalPriceDEV
            _OfferOutRows_New.FinalPriceHUF = _OfferOutRows.FinalPriceHUF
            _OfferOutRows_New.Product = _OfferOutRows.Product
            _OfferOutRows_New.QTT = _OfferOutRows.QTT
            _OfferOutRows_New.QualityOption = _OfferOutRows.QualityOption
            _OfferOutRows_New.RowIndex = _OfferOutRows.RowIndex
            _OfferOutRows_New.RowType = _OfferOutRows.RowType
            _OfferOutRows_New.ShortName = _OfferOutRows.ShortName
            _OfferOutRows_New.VAT = _OfferOutRows.VAT
            _OfferOutRows_New.RowGroup1 = _OfferOutRows.RowGroup1
            _OfferOutRows_New.RowGroup2 = _OfferOutRows.RowGroup2
            _OfferOutRows_New.CanChangeProperty = _OfferOutRows.CanChangeProperty
            _OfferOutRows_New.DiscountPriceDEV = _OfferOutRows.DiscountPriceDEV
            _OfferOutRows_New.DiscountPriceHUF = _OfferOutRows.DiscountPriceHUF
            _OfferOutRows_New.OfferInRows = _OfferOutRows.OfferInRows
            _OfferOutRows_New.Recipe = _OfferOutRows.Recipe
            _OfferOutRows_New.SumPriceDEV = _OfferOutRows.SumPriceDEV
            _OfferOutRows_New.SumPriceHUF = _OfferOutRows.SumPriceHUF
            _OfferOutRows_New.Units = _OfferOutRows.Units

            _OfferOutRows_New.Save()
        Next
        ' ----------- Az új adatokkal megjeleníteni az új objektumot ----------------------------------
        _onew.CommitChanges()
        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, _OfferOutHeader_New)
    End Sub
    Private Sub aCreateOrderIn_From_OfferOut_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateOrderIn_From_OfferOut.CustomizePopupWindowParams
        ' Az akció egy detailview hoz létre a kiválasztott kimenő megrendelésből, és megjeleníti az összes sorát
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OrderInHeaderFromOfferOutRowsHeader As OrderInHeaderFromOfferOutRowsHeader = _onew.CreateObject(Of OrderInHeaderFromOfferOutRowsHeader)()
        Dim _OrderInHeaderFromOfferOutRowsRows As OrderInHeaderFromOfferOutRowsRows
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
        Dim _OfferOutRows As OfferOutRows

        _OrderInHeaderFromOfferOutRowsHeader.CustomersFrom = _onew.GetObject(_OfferOutHeader.CustomersFrom)
        _OrderInHeaderFromOfferOutRowsHeader.CurrencyName = _onew.GetObject(_OfferOutHeader.CurrencyName)

        For Each _OfferOutRows In _OfferOutHeader.OfferOutRows
            _OrderInHeaderFromOfferOutRowsRows = _onew.CreateObject(Of OrderInHeaderFromOfferOutRowsRows)()
            _OrderInHeaderFromOfferOutRowsRows.OrderInHeaderFromOfferOutRowsHeader = _OrderInHeaderFromOfferOutRowsHeader
            _OrderInHeaderFromOfferOutRowsRows.OfferOutRows = _onew.GetObject(_OfferOutRows)
            _OrderInHeaderFromOfferOutRowsRows.QTT_Process = _OfferOutRows.QTT
        Next

        e.View = Application.CreateDetailView(_onew, "OrderInHeaderFromOfferOutRowsHeader_DetailView", False, _OrderInHeaderFromOfferOutRowsHeader)
    End Sub
    Private Sub aCreateOrderIn_From_OfferOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateOrderIn_From_OfferOut.Execute
        ' Az akció a popup-on kiválasztott sorokból és a hozzá kézzel megadott mennyiségi szándékokból egy bejövő megrendelést hoz létre
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = _ocur.GetObject(TryCast(e.SelectedObjects(0), OfferOutHeader))

        Dim _OfferOutRows As OfferOutRows
        Dim _OrderInHeaderFromOfferOutRowsHeader As OrderInHeaderFromOfferOutRowsHeader = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OrderInHeaderFromOfferOutRowsHeader))
        Dim _OrderInHeaderFromOfferOutRowsRows_ListEditor As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("OrderInHeaderFromOfferOutRowsRows")
        Dim _OrderInRows_New As OrderInRows
        Dim _OrderInRowsOther_New As OrderInRowsOther
        Dim _QTT_Process As Double = 0

        ' Bejövő megrendelés fejlécének a létrehozása és adatokkal feltöltése
        Dim _OrderInHeader_New As OrderInHeader = _ocur.CreateObject(Of OrderInHeader)()
        With _OrderInHeader_New
            .Contacts = _OfferOutHeader.Contact
            .CRM_Opportunities = _OfferOutHeader.CRM_Opportunities
            .Customers = _OfferOutHeader.Customers
            .CustomersFrom = _OfferOutHeader.CustomersFrom
            .DateCreated = GetSQLTime(_ocur.Session)
            .DateExecution = _OfferOutHeader.DateExecution
            .ObjectCreated = GetSQLTime(_ocur.Session)
            .OrderInParameters = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OrderInHeaderFromOfferOutRowsHeader).OrderInParameters)
            .ShipmentOption = _OfferOutHeader.ShipmentOption
            .PaymentOption = _OfferOutHeader.PaymentOption
            .OfferOutHeader = _OfferOutHeader

        End With

        '------------------------------ A Sorok létrehozása típusok szerint ----------------------------------------------------------------------------------
        For i = 0 To _OrderInHeaderFromOfferOutRowsRows_ListEditor.ListView.SelectedObjects.Count - 1
            _OfferOutRows = _ocur.GetObject(TryCast(_OrderInHeaderFromOfferOutRowsRows_ListEditor.ListView.SelectedObjects(i), OrderInHeaderFromOfferOutRowsRows).OfferOutRows)
            _QTT_Process = TryCast(_OrderInHeaderFromOfferOutRowsRows_ListEditor.ListView.SelectedObjects(i), OrderInHeaderFromOfferOutRowsRows).QTT_Process

            If _OfferOutRows.RowType = eRowType.IsProduct Then
                _OrderInRows_New = _ocur.CreateObject(Of OrderInRows)()
                With _OrderInRows_New
                    .OrderInHeader = _OrderInHeader_New
                    .Products = _OfferOutRows.Product
                    .ListPriceDEV = _OfferOutRows.ListPriceDEV
                    .ListPriceHUF = _OfferOutRows.ListPriceHUF
                    .RowGroup1 = _OfferOutRows.RowGroup1
                    .RowGroup2 = _OfferOutRows.RowGroup2
                    .Description = _OfferOutRows.Description
                    .DescriptionPrivate = _OfferOutRows.DescriptionPrivate
                    .OfferOutRows = _OfferOutRows
                    .VAT = _OfferOutRows.VAT

                    If _QTT_Process <= _OfferOutRows.QTT And _QTT_Process > 0 Then
                        .QTT = _QTT_Process
                    Else
                        ' TESZT MIATT!!!!!! --------------------------------------------------------------------------------------------------------
                        Throw New Exception("*Az egyik megadott mennyiségi szándék értéke nagyobb mint az eredeti mennyiség, vagy kevesebb mint 0!")
                    End If

                    .DiscountPriceDEV = _OfferOutRows.DiscountPriceDEV
                    .DiscountPriceHUF = _OfferOutRows.DiscountPriceHUF
                    .RowIndex = _OrderInHeader_New.OrderInRows.Count

                End With

            End If

            If _OfferOutRows.RowType = eRowType.IsOther Then
                _OrderInRowsOther_New = _ocur.CreateObject(Of OrderInRowsOther)()
                With _OrderInRowsOther_New
                    .OrderInHeader = _OrderInHeader_New
                    .OfferOutRows = _OfferOutRows
                    .Description = _OfferOutRows.Description

                    If _QTT_Process <= _OfferOutRows.QTT And _QTT_Process > 0 Then
                        .QTT = _QTT_Process
                    Else
                        ' TESZT MIATT!!!!!! --------------------------------------------------------------------------------------------------------
                        Throw New Exception("*Az egyik megadott mennyiségi szándék értéke nagyobb mint az eredeti mennyiség, vagy kevesebb mint 0!")
                    End If

                    .ShortName = _OfferOutRows.ShortName
                    .Units = _OfferOutRows.Units
                    .VAT = _OfferOutRows.VAT
                    .Description = _OfferOutRows.Description
                    .DiscountPriceDEV = _OfferOutRows.DiscountPriceDEV
                    .DiscountPriceHUF = _OfferOutRows.DiscountPriceHUF
                    .ListPriceDEV = _OfferOutRows.ListPriceDEV
                    .ListPriceHUF = _OfferOutRows.ListPriceHUF
                    .QualityOption = _OfferOutRows.QualityOption
                    .RowGroup1 = _OfferOutRows.RowGroup1
                    .RowGroup2 = _OfferOutRows.RowGroup2
                    .RowIndex = _OfferOutRows.RowIndex
                    .OfferOutRows = _OfferOutRows
                End With
            End If
        Next

        _ocur.CommitChanges()
        Dim _SortCollection As New SortingCollection
        _SortCollection.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))

        Dim _OrderInRows_Collection As New XPCollection(Of OrderInRows)(_ocur.Session, CriteriaOperator.Parse("OrderInHeader.Oid=?", _OrderInHeader_New.Oid))
        _OrderInRows_Collection.Sorting = _SortCollection

        Dim _OrderInRowsOther_Collection As New XPCollection(Of OrderInRowsOther)(_ocur.Session, CriteriaOperator.Parse("OrderInHeader.Oid=?", _OrderInHeader_New.Oid))
        _OrderInRowsOther_Collection.Sorting = _SortCollection

        Dim _Index As Long = 1
        For Each _OrderInRows In _OrderInRows_Collection
            _OrderInRows.RowIndex = _Index
            _Index += 1
        Next

        Dim _Index2 As Long = 1
        For Each _OrderInRowsOther In _OrderInRowsOther_Collection
            _OrderInRowsOther.RowIndex = _Index2
            _Index2 += 1
            _OrderInRowsOther.Save()
        Next

        _ocur.CommitChanges()
    End Sub
    Private Sub aCloneOfferOut_CRM_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCloneOfferOut_CRM.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferOutHeader As OfferOutHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), OfferOutHeader))
        Dim _OffersOutCloneForCRM As OffersOutCloneForCRM = _onew.CreateObject(Of OffersOutCloneForCRM)()

        _OffersOutCloneForCRM.OfferOutHeader = _OfferOutHeader

        e.View = Application.CreateDetailView(_onew, "OffersOutCloneForCRM_DetailView", False, _OffersOutCloneForCRM)
    End Sub
    Private Sub aCloneOfferOut_CRM_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCloneOfferOut_CRM.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferOutHeader As OfferOutHeader = _onew.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OffersOutCloneForCRM).OfferOutHeader)
        Dim _OfferOutHeader_New As OfferOutHeader = _onew.CreateObject(Of OfferOutHeader)()
        Dim _OfferOutRows As OfferOutRows

        ' -------------------- Fejléc adatok átadása ----------------------------------------
        _OfferOutHeader_New.CRM_Opportunities = _onew.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OffersOutCloneForCRM).CRM_Opportunities)
        _OfferOutHeader_New.CurrencyName = _OfferOutHeader.CurrencyName
        _OfferOutHeader_New.DatePayment = _OfferOutHeader.DatePayment
        _OfferOutHeader_New.DateDue = _OfferOutHeader.DateDue
        _OfferOutHeader_New.Description = _OfferOutHeader.Description
        _OfferOutHeader_New.OfferOutState = _OfferOutHeader.OfferOutState
        _OfferOutHeader_New.PaymentOption = _OfferOutHeader.PaymentOption
        _OfferOutHeader_New.QualityOption = _OfferOutHeader.QualityOption
        _OfferOutHeader_New.ShipmentOption = _OfferOutHeader.ShipmentOption
        _OfferOutHeader_New.IsEditable = True
        _OfferOutHeader_New.IsStorno = False
        _OfferOutHeader_New.DocumentBody = _OfferOutHeader.DocumentBody
        _OfferOutHeader_New.ExcelFile = _OfferOutHeader.ExcelFile
        _OfferOutHeader_New.ExcelProperty = _OfferOutHeader.ExcelProperty
        _OfferOutHeader_New.OtherCostPercent = _OfferOutHeader.OtherCostPercent

        ' -------------------- Sor(ok) átadása-----------------------------------------------
        For Each _OfferOutRows In _OfferOutHeader.OfferOutRows
            Dim _OfferOutRows_New As OfferOutRows = _onew.CreateObject(Of OfferOutRows)()
            _OfferOutRows_New.OfferOutHeader = _OfferOutHeader_New
            _OfferOutRows_New.CustomsTariffs = _OfferOutRows.CustomsTariffs
            _OfferOutRows_New.Description = _OfferOutRows.Description
            _OfferOutRows_New.DescriptionPrivate = _OfferOutRows.DescriptionPrivate
            _OfferOutRows_New.DiscountPercent = _OfferOutRows.DiscountPercent
            _OfferOutRows_New.Installation = _OfferOutRows.Installation
            _OfferOutRows_New.FinalPriceDEV = _OfferOutRows.FinalPriceDEV
            _OfferOutRows_New.FinalPriceHUF = _OfferOutRows.FinalPriceHUF
            _OfferOutRows_New.QTT = _OfferOutRows.QTT
            _OfferOutRows_New.ListPriceDEV = _OfferOutRows.ListPriceDEV
            _OfferOutRows_New.ListPriceHUF = _OfferOutRows.ListPriceHUF
            _OfferOutRows_New.Product = _OfferOutRows.Product

            _OfferOutRows_New.QualityOption = _OfferOutRows.QualityOption
            _OfferOutRows_New.RowIndex = _OfferOutRows.RowIndex
            _OfferOutRows_New.RowType = _OfferOutRows.RowType
            _OfferOutRows_New.ShortName = _OfferOutRows.ShortName
            _OfferOutRows_New.VAT = _OfferOutRows.VAT
            _OfferOutRows_New.RowGroup1 = _OfferOutRows.RowGroup1
            _OfferOutRows_New.RowGroup2 = _OfferOutRows.RowGroup2
            _OfferOutRows_New.CanChangeProperty = _OfferOutRows.CanChangeProperty
            _OfferOutRows_New.DiscountPriceDEV = _OfferOutRows.DiscountPriceDEV
            _OfferOutRows_New.DiscountPriceHUF = _OfferOutRows.DiscountPriceHUF
            _OfferOutRows_New.OfferInRows = _OfferOutRows.OfferInRows
            _OfferOutRows_New.Recipe = _OfferOutRows.Recipe
            _OfferOutRows_New.SumPriceDEV = _OfferOutRows.SumPriceDEV
            _OfferOutRows_New.SumPriceHUF = _OfferOutRows.SumPriceHUF
            _OfferOutRows_New.Units = _OfferOutRows.Units
        Next
        ' ----------- Az új adatokkal megjeleníteni az új objektumot ----------------------------------
        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "OfferOutHeader_DetailView", True, _OfferOutHeader_New)
        e.ShowViewParameters.TargetWindow = TargetWindow.NewWindow

    End Sub
    Private Sub aOffersOut_ChangeRows_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOffersOut_ChangeRows.CustomizePopupWindowParams
        _noselect = True
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferOutRows_ChangeGroup As OfferOutRows_ChangeGroup = _onew.CreateObject(Of OfferOutRows_ChangeGroup)()
        If View.SelectedObjects.Count > 0 Then
            Dim _OfferOutRows As OfferOutRows = TryCast(View.SelectedObjects(0), OfferOutRows)
            _OfferOutRows_ChangeGroup.VAT = _onew.GetObject(_OfferOutRows.VAT)
            _OfferOutRows_ChangeGroup.Units = _onew.GetObject(_OfferOutRows.Units)
            _OfferOutRows_ChangeGroup.RowGroup1 = _onew.GetObject(_OfferOutRows.RowGroup1)
            _OfferOutRows_ChangeGroup.RowGroup2 = _onew.GetObject(_OfferOutRows.RowGroup2)
            _OfferOutRows_ChangeGroup.QualityOption = _onew.GetObject(_OfferOutRows.QualityOption)
            _OfferOutRows_ChangeGroup.FinalPricePercent = 0
            _OfferOutRows_ChangeGroup.ShortName = _onew.GetObject(_OfferOutRows.ShortName)
            _OfferOutRows_ChangeGroup.Description = _onew.GetObject(_OfferOutRows.Description)

            e.View = Application.CreateDetailView(_onew, "OfferOutRows_ChangeGroup_DetailView", False, _OfferOutRows_ChangeGroup)
        Else
            Throw New UserFriendlyException("Nincs kijelölt elem!")
        End If
    End Sub
    Private Sub aOffersOut_ChangeRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOffersOut_ChangeRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutRows_ChangeGroup As OfferOutRows_ChangeGroup = TryCast(e.PopupWindow.View.SelectedObjects(0), OfferOutRows_ChangeGroup)

        Dim _OfferOutRows As OfferOutRows '= TryCast(View.SelectedObjects(0), OfferOutRows)
        'If _OfferOutRows IsNot Nothing Then

        If _OfferOutRows_ChangeGroup.Change_Description Then
            For Each _OfferOutRows In _selection
                _OfferOutRows.Description = _OfferOutRows_ChangeGroup.Description
                _OfferOutRows.Save()
            Next
        End If
        If _OfferOutRows_ChangeGroup.Change_FinalPricePercent Then
            For Each _OfferOutRows In _selection
                '_OfferOutRows.FinalPriceHUF = _OfferOutRows.FinalPriceHUF - (_OfferOutRows.FinalPriceHUF * (_OfferOutRows_ChangeGroup.FinalPricePercent / 100))
                _OfferOutRows.DiscountPercent = _OfferOutRows_ChangeGroup.FinalPricePercent
                _OfferOutRows.RecalculateRow()
                _OfferOutRows.CanChangeProperty = False

                If _OfferOutRows.OfferOutHeader.CurrencyName.ShortName = "HUF" Then
                    _OfferOutRows.FinalPriceHUF = _OfferOutRows.ListPriceHUF - _OfferOutRows.DiscountPriceHUF
                Else
                    _OfferOutRows.FinalPriceDEV = _OfferOutRows.ListPriceDEV - _OfferOutRows.FinalPriceDEV
                End If

                _OfferOutRows.CanChangeProperty = True

                _OfferOutRows.Save()
            Next
        End If
        If _OfferOutRows_ChangeGroup.Change_QualityOption Then
            For Each _OfferOutRows In _selection
                _OfferOutRows.QualityOption = _ocur.GetObjectByKey(Of QualityOption)(_OfferOutRows.QualityOption.Oid)
                _OfferOutRows.Save()
            Next
        End If
        If _OfferOutRows_ChangeGroup.Change_RowGroup1 Then
            For Each _OfferOutRows In _selection
                _OfferOutRows.RowGroup1 = _OfferOutRows_ChangeGroup.RowGroup1
                _OfferOutRows.Save()
            Next
        End If
        If _OfferOutRows_ChangeGroup.Change_RowGroup2 Then
            For Each _OfferOutRows In _selection
                _OfferOutRows.RowGroup2 = _OfferOutRows_ChangeGroup.RowGroup2
                _OfferOutRows.Save()
            Next
        End If
        If _OfferOutRows_ChangeGroup.Change_ShortName Then
            For Each _OfferOutRows In _selection
                _OfferOutRows.ShortName = _OfferOutRows_ChangeGroup.ShortName
                _OfferOutRows.Save()
            Next
        End If
        If _OfferOutRows_ChangeGroup.Change_Units Then
            For Each _OfferOutRows In _selection
                _OfferOutRows.Units = _ocur.GetObjectByKey(Of Units)(_OfferOutRows_ChangeGroup.Units.Oid)
                _OfferOutRows.Save()
            Next
        End If
        If _OfferOutRows_ChangeGroup.Change_VAT Then
            For Each _OfferOutRows In _selection
                _OfferOutRows.VAT = _ocur.GetObjectByKey(Of VAT)(_OfferOutRows_ChangeGroup.VAT.Oid)
                _OfferOutRows.Save()
            Next
        End If
        If _OfferOutRows_ChangeGroup.Change_OfferInRows Then
            For Each _OfferOutRows In _selection
                _OfferOutRows.OfferInRows = _ocur.GetObjectByKey(Of OfferInRows)(_OfferOutRows_ChangeGroup.OfferInRows.Oid)
                _OfferOutRows.Save()
            Next
        End If
        'End If
        _ocur.CommitChanges()
        _noselect = False
    End Sub
    Private Sub aOffersOutFinalWidthVersion_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOffersOutFinalWidthVersion.CustomizePopupWindowParams
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(OfferOutHeader))
        _CollectionSource.BeginUpdateCriteria()
        _CollectionSource.Criteria("1") = CriteriaOperator.Parse("CRM_Opportunities.Oid=?", _OfferOutHeader.CRM_Opportunities.Oid)
        _CollectionSource.Criteria("2") = CriteriaOperator.Parse("IsEditable=?", _OfferOutHeader.IsEditable = False)
        _CollectionSource.EndUpdateCriteria()

        e.View = Application.CreateListView("OfferOutHeader_ListView_Clone_CRM", _CollectionSource, True)
    End Sub
    Private Sub aOffersOutFinalWidthVersion_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOffersOutFinalWidthVersion.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader_Popup As OfferOutHeader = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OfferOutHeader))
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
        Dim _DocumentNumber As String
        Dim _DocumentNumber_POPUP As String

        If InStr(_OfferOutHeader_Popup.DocumentNumber, "->") = 0 Then
            _DocumentNumber_POPUP = _OfferOutHeader_Popup.DocumentNumber
        Else
            _DocumentNumber_POPUP = _OfferOutHeader_Popup.DocumentNumber.Remove(InStr(_OfferOutHeader_Popup.DocumentNumber, "->") - 1, _
                                   _OfferOutHeader_Popup.DocumentNumber.Length - InStr(_OfferOutHeader_Popup.DocumentNumber, "->") + 1)
        End If

        Dim _OfferOutHeader_Collection As New XPCollection(Of OfferOutHeader)(_ocur.Session, CriteriaOperator.Parse("DocumentNumber like '" & _DocumentNumber_POPUP & "%'"))

        If _OfferOutHeader_Collection.Count = 1 Then
            _DocumentNumber = _DocumentNumber_POPUP & "->1"
        Else
            _DocumentNumber = _DocumentNumber_POPUP + "->" + (_OfferOutHeader_Collection.Count).ToString()
        End If

        If _OfferOutHeader.OfferOutRows.Count > 0 Then
            _OfferOutHeader.DocumentNumber = _DocumentNumber

            _OfferOutHeader.IsEditable = False
            _ocur.CommitChanges()
            _OfferOutHeader.RecalculateOfferOut(_OfferOutHeader, _ocur)
            _ocur.CommitChanges()
            View.Refresh()
        Else
            '-------------Teszt miatt------------------------------
            _ocur.Rollback()
            Throw New Exception("*Nincs egy sora sem a kimenő ajánlatnak, így nem lehet véglegesíteni!")
        End If
    End Sub
    Private Sub aGenerateOfferOutCost_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGenerateOfferOutCost.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _OfferOutRows As OfferOutRows
        Dim _OfferOutCostRows As OfferOutCostRows
        Dim _ListView As ListView = TryCast(View, ListView)
        Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
        If _ocur.Session.IsNewObject(_PropertyCollectionSource.MasterObject) Then ' Amennyiben ha a fejléc új objektum, rámentünk, hogy létre tudjuk hozmni hozzá a költség sorokat
            _ocur.CommitChanges()
        End If
        Dim _OfferOutHeader As OfferOutHeader = _uow.GetObjectByKey(Of OfferOutHeader)(TryCast(_PropertyCollectionSource.MasterObject, OfferOutHeader).Oid)
        Dim _CustomersOrderPriority As CustomersOrderPriority
        Dim _PricingIn As Pricing = Nothing
        Dim _OfferOutCostRows_Collection As New XPCollection(Of OfferOutCostRows)(_uow, CriteriaOperator.Parse("OfferOutHeader=?", _OfferOutHeader))

        _uow.Delete(_OfferOutCostRows_Collection)

        For Each _OfferOutRows In _OfferOutHeader.OfferOutRows
            _OfferOutCostRows = New OfferOutCostRows(_uow)
            _OfferOutCostRows.OfferOutHeader = _OfferOutHeader
            _OfferOutCostRows.RowGroup1 = _OfferOutRows.RowGroup1
            _OfferOutCostRows.RowGroup2 = _OfferOutRows.RowGroup2
            _PricingIn = Nothing
            If _OfferOutRows.RowType = eRowType.IsProduct Then
                If _OfferOutRows.Product IsNot Nothing Then
                    For Each _CustomersOrderPriority In _OfferOutRows.Product.CustomersOrderPriority
                        If _CustomersOrderPriority.RowIndex = 1 Then
                            If _CustomersOrderPriority.CustomersOrder.PricingIn IsNot Nothing Then
                                _PricingIn = _CustomersOrderPriority.CustomersOrder.PricingIn
                                Exit For
                            End If
                        End If
                    Next
                    If _PricingIn IsNot Nothing Then
                        _OfferOutCostRows.FinalPriceHUF_In = _PricingIn.GetCurrentPriceNettoHUF(_OfferOutRows.Product, _OfferOutRows.QTT, _OfferOutCostRows.OfferOutHeader.CRM_Opportunities)
                        _OfferOutCostRows.FinalPriceDEV_In = _PricingIn.GetCurrentPriceNettoDEV(_OfferOutRows.Product, _OfferOutRows.QTT, _OfferOutCostRows.OfferOutHeader.CRM_Opportunities)
                    Else
                        'If _OfferOutRows.Product.ListPriceInHUF > 1 Then
                        '    _OfferOutCostRows.FinalPriceHUF_In = _OfferOutRows.Product.ListPriceInHUF '0
                        'Else
                        _OfferOutCostRows.FinalPriceHUF_In = _OfferOutRows.Product.ListPriceInHUF
                        'End If
                        'If _OfferOutRows.Product.ListPriceInDEV > 0 Then
                        '    _OfferOutCostRows.FinalPriceDEV_In = _OfferOutRows.Product.ListPriceInDEV '0
                        'Else
                        _OfferOutCostRows.FinalPriceDEV_In = _OfferOutRows.Product.ListPriceInDEV
                        'End If
                    End If
                End If
            ElseIf _OfferOutRows.RowType = eRowType.IsOther Then

                If _OfferOutRows.FinalPriceHUF > 0 Or _OfferOutRows.FinalPriceDEV > 0 Then
                    If _OfferOutRows.OfferInRows IsNot Nothing Then
                        _OfferOutCostRows.FinalPriceHUF_In = _OfferOutRows.OfferInRows.FinalPriceHUF
                        _OfferOutCostRows.FinalPriceDEV_In = _OfferOutRows.OfferInRows.FinalPriceDEV
                    Else
                        If _OfferOutHeader.OtherCostPercent <> 0 Then
                            _OfferOutCostRows.FinalPriceDEV_In = _OfferOutRows.FinalPriceDEV * (_OfferOutHeader.OtherCostPercent / 100)
                            _OfferOutCostRows.FinalPriceHUF_In = _OfferOutRows.FinalPriceHUF * (_OfferOutHeader.OtherCostPercent / 100)
                        Else
                            _OfferOutCostRows.FinalPriceDEV_In = 0
                            _OfferOutCostRows.FinalPriceHUF_In = 0
                        End If
                    End If
                Else
                    'IDE KELL MEGÍRNI HOGY MI LEGYEN A KEDVEZMÉNY SOROKKAL......
                End If
            ElseIf _OfferOutRows.RowType = eRowType.IsInstallation Then
                _OfferOutCostRows.FinalPriceDEV_In = 0
                _OfferOutCostRows.FinalPriceHUF_In = 0
            End If

            _OfferOutCostRows.SumPriceDEV_In = _OfferOutCostRows.FinalPriceDEV_In * _OfferOutRows.QTT
            _OfferOutCostRows.SumPriceHUF_In = _OfferOutCostRows.FinalPriceHUF_In * _OfferOutRows.QTT

            _OfferOutCostRows.FinalPriceDEV_Out = _OfferOutRows.FinalPriceDEV
            _OfferOutCostRows.SumPriceDEV_Out = _OfferOutRows.FinalPriceDEV * _OfferOutRows.QTT

            _OfferOutCostRows.FinalPriceHUF_Out = _OfferOutRows.FinalPriceHUF
            _OfferOutCostRows.SumPriceHUF_Out = _OfferOutRows.FinalPriceHUF * _OfferOutRows.QTT

            If _OfferOutCostRows.OfferOutHeader.CurrencyName.ShortName = "HUF" Then
                _OfferOutCostRows.DifferentHUF = (_OfferOutCostRows.FinalPriceHUF_Out - _OfferOutCostRows.FinalPriceHUF_In) * _OfferOutRows.QTT
            Else
                _OfferOutCostRows.DifferentDEV = (_OfferOutCostRows.FinalPriceDEV_Out - _OfferOutCostRows.FinalPriceDEV_In) * _OfferOutRows.QTT
            End If

            If _OfferOutCostRows.FinalPriceHUF_Out <> 0 Then
                _OfferOutCostRows.DifferentPercent = Math.Round((1 - (_OfferOutCostRows.FinalPriceHUF_In / _OfferOutCostRows.FinalPriceHUF_Out)) * 100, 2, MidpointRounding.AwayFromZero)
            Else
                _OfferOutCostRows.DifferentPercent = 0
            End If

            _OfferOutCostRows.Description = _OfferOutRows.Description
            _OfferOutCostRows.ShortName = _OfferOutRows.ShortName
            _OfferOutCostRows.QTT = _OfferOutRows.QTT
            _OfferOutCostRows.Units = _OfferOutRows.Units

            _OfferOutCostRows.OfferOutHeader = _OfferOutHeader 'Fejléc
            _OfferOutCostRows.OfferOutRows = _OfferOutRows 'Sor

            _OfferOutCostRows.Save()
        Next
        _uow.CommitChanges()


        Dim nestedFrame As NestedFrame = TryCast(Frame, NestedFrame)
        If nestedFrame IsNot Nothing Then
            Dim parentView As View = nestedFrame.View
            parentView.ObjectSpace.Refresh()
        End If

    End Sub
    Private Sub aImportRecipe_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aImportRecipe.CustomizePopupWindowParams
        Dim _OfferOutHeader As OfferOutHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferOutHeader)

        If _OfferOutHeader.IsEditable Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ImportRecipePopUp As ImportRecipePopUp = _onew.CreateObject(Of ImportRecipePopUp)()
            e.View = Application.CreateDetailView(_onew, "ImportRecipePopUp_DetailView", False, _ImportRecipePopUp)
        Else
            '----------TESZT MIATT-----------------------------------------
            Throw New Exception("*A kimenő ajánlat végleges vagy stornó!")
        End If
    End Sub
    Private Sub aImportRecipe_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aImportRecipe.Execute
        Try
            Dim _ImportRecipePopUp As ImportRecipePopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ImportRecipePopUp)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _ListView As ListView = TryCast(View, ListView)
            Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
            Dim _OfferOutHeader As OfferOutHeader = TryCast(_PropertyCollectionSource.MasterObject, OfferOutHeader)
            Dim _Recipe As Recipe = _ocur.GetObjectByKey(Of Recipe)(_ImportRecipePopUp.Recipe.Oid)

            If _OfferOutHeader IsNot Nothing And _Recipe IsNot Nothing Then
                For Each _RecipeFrom As RecipeFrom In _Recipe.RecipeFrom
                    Dim _OfferOutRows As OfferOutRows = _ocur.CreateObject(Of OfferOutRows)()
                    _OfferOutRows.OfferOutHeader = _OfferOutHeader
                    _OfferOutRows.CanChangeProperty = False
                    _OfferOutRows.RowType = eRowType.IsProduct
                    _OfferOutRows.RowGroup1 = _RecipeFrom.RowGroup1
                    _OfferOutRows.RowGroup2 = _RecipeFrom.RowGroup2
                    _OfferOutRows.Product = _RecipeFrom.Products
                    _OfferOutRows.QTT = _RecipeFrom.QTT
                    _OfferOutRows.Units = _RecipeFrom.Products.Units
                    _OfferOutRows.FinalPriceDEV = _RecipeFrom.QTT * _RecipeFrom.ListPriceDEV
                    _OfferOutRows.FinalPriceHUF = _RecipeFrom.QTT * _RecipeFrom.ListPriceHUF
                    _OfferOutRows.VAT = _RecipeFrom.Products.VAT

                    If _OfferOutHeader.OfferOutRows.Count = 0 Then
                        _OfferOutRows.RowIndex = 0
                    Else
                        _OfferOutRows.RowIndex = _OfferOutHeader.OfferOutRows.Count - 1
                    End If
                Next

                For Each _RecipeFromOther As RecipeFromOther In _Recipe.RecipeFromOther
                    Dim _OfferOutRows As OfferOutRows = _ocur.CreateObject(Of OfferOutRows)()
                    _OfferOutRows.OfferOutHeader = _OfferOutHeader
                    _OfferOutRows.CanChangeProperty = False
                    _OfferOutRows.RowType = eRowType.IsOther
                    _OfferOutRows.RowGroup1 = _RecipeFromOther.RowGroup1
                    _OfferOutRows.RowGroup2 = _RecipeFromOther.RowGroup2
                    _OfferOutRows.ShortName = _RecipeFromOther.ShortName
                    _OfferOutRows.Description = _RecipeFromOther.Description
                    _OfferOutRows.QTT = _RecipeFromOther.QTT
                    _OfferOutRows.Units = _RecipeFromOther.Units
                    _OfferOutRows.VAT = _ocur.FindObject(Of VAT)(CriteriaOperator.Parse("DefaultForBusinnes='True'"))
                    _OfferOutRows.ListPriceHUF = _RecipeFromOther.ListPriceHUF
                    _OfferOutRows.ListPriceDEV = _RecipeFromOther.ListPriceDEV
                    _OfferOutRows.FinalPriceHUF = _RecipeFromOther.QTT * _RecipeFromOther.ListPriceHUF
                    _OfferOutRows.FinalPriceDEV = _RecipeFromOther.QTT * _RecipeFromOther.ListPriceDEV
                    If _OfferOutHeader.OfferOutRows.Count = 0 Then
                        _OfferOutRows.RowIndex = 0
                    Else
                        _OfferOutRows.RowIndex = _OfferOutHeader.OfferOutRows.Count - 1
                    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
    Private Sub aInsert_Discount_RowOther_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aInsert_Discount_RowOther.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _InsertDiscountRowPopup As InsertDiscountRowPopup = _onew.CreateObject(Of InsertDiscountRowPopup)()
        Dim _OfferOutHeader As OfferOutHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), OfferOutHeader))

        _InsertDiscountRowPopup.OfferOutHeader = _OfferOutHeader

        If _InsertDiscountRowPopup.OfferOutHeader.CurrencyName.ShortName = "HUF" Then
            For Each _OfferOutRows As OfferOutRows In _InsertDiscountRowPopup.OfferOutHeader.OfferOutRows
                _InsertDiscountRowPopup.SUMFinalPrice += _OfferOutRows.SumPriceHUF
            Next
        Else
            For Each _OfferOutRows As OfferOutRows In _InsertDiscountRowPopup.OfferOutHeader.OfferOutRows
                _InsertDiscountRowPopup.SUMFinalPrice += _OfferOutRows.SumPriceDEV
            Next
        End If

        e.View = Application.CreateDetailView(_onew, "InsertDiscountRowPopup_DetailView", False, _InsertDiscountRowPopup)
    End Sub
    Private Sub aInsert_Discount_RowOther_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aInsert_Discount_RowOther.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InsertDiscountRowPopup As InsertDiscountRowPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), InsertDiscountRowPopup)
        Dim _OfferOutRows_New As OfferOutRows = _ocur.CreateObject(Of OfferOutRows)()

        If _InsertDiscountRowPopup.DiscountPriceHUF < 0 Or _InsertDiscountRowPopup.DiscountPriceDEV < 0 Then
            '------ TESZT MIATT!!! ----------------------------------------------
            Throw New Exception("*A fix kedvezmény értéke nem lehet negatív szám!")
        End If

        If _InsertDiscountRowPopup.DiscountPercent >= 0 And _InsertDiscountRowPopup.DiscountPercent <= 99 And _InsertDiscountRowPopup.ShortName <> "" Then
            If _InsertDiscountRowPopup.OfferOutHeader.CurrencyName.ShortName = "HUF" Then
                If _InsertDiscountRowPopup.DiscountPercent > 0 Then
                    _InsertDiscountRowPopup.DiscountPriceHUF = Math.Round((_InsertDiscountRowPopup.SUMFinalPrice * (_InsertDiscountRowPopup.DiscountPercent / 100)), 0, MidpointRounding.AwayFromZero)
                End If
            Else
                If _InsertDiscountRowPopup.DiscountPercent > 0 Then
                    _InsertDiscountRowPopup.DiscountPriceDEV = Math.Round((_InsertDiscountRowPopup.SUMFinalPrice * (_InsertDiscountRowPopup.DiscountPercent / 100)), 2, MidpointRounding.AwayFromZero)
                End If
            End If

            '------------ Új sor létrehozása ---------------------------------------------------------------
            Dim _DefaultVAT As VAT
            Dim _VAT_Collection As New XPCollection(Of VAT)(_ocur.Session)

            For Each _VAT As VAT In _VAT_Collection
                If _VAT.DefaultForBusinnes = True Then
                    _DefaultVAT = _VAT
                    Exit For
                End If
            Next

            If _DefaultVAT IsNot Nothing Then
                _OfferOutRows_New.VAT = _DefaultVAT
            End If
            _OfferOutRows_New.CanChangeProperty = False
            _OfferOutRows_New.OfferOutHeader = _ocur.GetObjectByKey(Of OfferOutHeader)(_InsertDiscountRowPopup.OfferOutHeader.Oid)
            _OfferOutRows_New.RowType = eRowType.IsOther
            _OfferOutRows_New.QTT = 1
            _OfferOutRows_New.Description = _InsertDiscountRowPopup.Description
            _OfferOutRows_New.RowGroup1 = _InsertDiscountRowPopup.Row_Group1
            _OfferOutRows_New.RowGroup2 = _InsertDiscountRowPopup.Row_Group2
            _OfferOutRows_New.Units = _ocur.FindObject(Of Units)(CriteriaOperator.Parse("ShortName='DB'"))
            _OfferOutRows_New.RowIndex = _InsertDiscountRowPopup.OfferOutHeader.OfferOutRows.Count
            _OfferOutRows_New.SumPriceHUF -= _InsertDiscountRowPopup.DiscountPriceHUF
            _OfferOutRows_New.SumPriceDEV -= _InsertDiscountRowPopup.DiscountPriceDEV
            _OfferOutRows_New.ListPriceHUF = _OfferOutRows_New.SumPriceHUF
            _OfferOutRows_New.ListPriceDEV = _OfferOutRows_New.SumPriceDEV
            _OfferOutRows_New.FinalPriceHUF = _OfferOutRows_New.SumPriceHUF
            _OfferOutRows_New.FinalPriceDEV = _OfferOutRows_New.SumPriceDEV
            If _InsertDiscountRowPopup.DiscountPercent > 0 Then
                _OfferOutRows_New.ShortName = _InsertDiscountRowPopup.ShortName & " " & _InsertDiscountRowPopup.DiscountPercent.ToString & "%"
            Else
                _OfferOutRows_New.ShortName = _InsertDiscountRowPopup.ShortName
            End If
            _OfferOutRows_New.Save()
            _ocur.CommitChanges()
        Else
            '--------------------TESZT MIATT--------------------------------------------------------------------------------'
            Throw New UserFriendlyException("A kedvezmény % értéke nem lehet kisebb mint 0 (nulla), vagy nagyobb mint 99 és a megnevezés mező kitöltés kötelező!")
        End If
    End Sub
    Private Sub aInsertServicesToOfferOut_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aInsertServicesToOfferOut.CustomizePopupWindowParams
        ' Az akció a szolgáltatások listview-ját hozza fel.
        Dim _OfferOutHeader As OfferOutHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferOutHeader)

        If _OfferOutHeader.IsEditable Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _CollectionSource As CollectionSource = New CollectionSource(_onew, GetType(Services))

            e.View = Application.CreateListView("Services_ListView_Browse", _CollectionSource, False)
        Else
            '----------TESZT MIATT-----------------------------------------
            Throw New Exception("*A kimenő ajánlat végleges vagy stornó!")
        End If
    End Sub
    Private Sub aInsertServicesToOfferOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aInsertServicesToOfferOut.Execute
        'A popup-on kiválasztott szolgáltatásokból létrehoz kimenő árajánlati sorokat
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferOutHeader)
        Dim _Services As Services
        Dim _OfferOutRows_New As OfferOutRows

        For Each _Services In e.PopupWindow.View.SelectedObjects
            _OfferOutRows_New = _ocur.CreateObject(Of OfferOutRows)()

            _OfferOutRows_New.RowType = eRowType.IsOther
            _OfferOutRows_New.OfferOutHeader = _OfferOutHeader
            _OfferOutRows_New.ShortName = _Services.ShortName
            _OfferOutRows_New.CustomsTariffs = _ocur.GetObject(_Services.CustomsTariffs)
            _OfferOutRows_New.Description = _Services.Description
            _OfferOutRows_New.ListPriceDEV = _Services.ListPriceDEV
            _OfferOutRows_New.ListPriceHUF = _Services.ListPriceHUF
            _OfferOutRows_New.Units = _ocur.GetObject(_Services.Units)
            _OfferOutRows_New.FinalPriceDEV = _Services.ListPriceDEV
            _OfferOutRows_New.FinalPriceHUF = _Services.ListPriceHUF
            _OfferOutRows_New.VAT = _ocur.GetObject(_Services.VAT)
            _OfferOutRows_New.Recipe = _ocur.GetObject(_Services.Recipe)
            _OfferOutRows_New.QTT = 1
            _OfferOutRows_New.RowGroup1 = _Services.RowGroup1
            _OfferOutRows_New.RowGroup2 = _Services.RowGroup2

            'Indexelés
            If _OfferOutHeader.OfferOutRows.Count = 0 Then
                _OfferOutRows_New.RowIndex = 0
            Else
                _OfferOutRows_New.RowIndex = _OfferOutHeader.OfferOutRows.Count - 1
            End If

            _OfferOutRows_New.Save()
        Next
        _ocur.CommitChanges()
    End Sub
    Private Sub aOfferOut_ChangeState_Cancel(sender As Object, e As System.EventArgs)
        _noselect = False
    End Sub
    Private Sub aOfferOut_ChangeState_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOfferOut_ChangeState.CustomizePopupWindowParams
        _noselect = True
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferOut_ChangeState_PopUp As OfferOut_ChangeState_PopUp = _onew.CreateObject(Of OfferOut_ChangeState_PopUp)()

        e.View = Application.CreateDetailView(_onew, "OfferOut_ChangeState_PopUp_DetailView", False, _OfferOut_ChangeState_PopUp)
    End Sub
    Private Sub aOfferOut_ChangeState_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOfferOut_ChangeState.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOut_ChangeState_PopUp As OfferOut_ChangeState_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), OfferOut_ChangeState_PopUp)
        For Each _OfferOutHeader As OfferOutHeader In _selection
            _OfferOutHeader.OfferOutState = _ocur.GetObject(_OfferOut_ChangeState_PopUp.OfferOutState)
            If _OfferOut_ChangeState_PopUp.OfferOutState = eOfferOutState.eClosedLOST Then
                _OfferOutHeader.OfferLOSTReason = _ocur.GetObject(_OfferOut_ChangeState_PopUp.OfferLOSTReason)
                _OfferOutHeader.OfferLOSTReasonDescription = _OfferOut_ChangeState_PopUp.OfferLOSTReasonDescription
            End If
        Next
        _ocur.CommitChanges()
        _noselect = False
    End Sub
    Private Sub aBackToEditOfferOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToEditOfferOut.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
            If _OfferOutHeader Is Nothing Then Throw New Exception("*Váratlan hiba történt, a folyamat megszakadt!")
            If _OfferOutHeader.IsEditable = True Then Throw New Exception("*Az ajánlat már szerkeszthető státuszban van!")

            _OfferOutHeader.IsEditable = True

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub aProductVariableFinder_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aProductVariableFinder.CustomizePopupWindowParams
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _OfferOutRows As OfferOutRows = TryCast(View.SelectedObjects(0), OfferOutRows)
            If _OfferOutRows.RowType <> eRowType.IsProduct Then Throw New Exception("*A kiválsztott sor nem tartalmaz terméket!")
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ProductFinderVariable As ProductFinderVariable = _onew.CreateObject(Of ProductFinderVariable)()

            _ProductFinderVariable.Products = _onew.GetObject(_OfferOutRows.Product)
            _ProductFinderVariable.MasterKey = Guid.NewGuid

            e.View = Application.CreateDetailView(_onew, "ProductFinderVariable_DetailView", False, _ProductFinderVariable)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub aProductVariableFinder_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aProductVariableFinder.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _ProductFinderVariable As ProductFinderVariable = TryCast(e.PopupWindow.View.SelectedObjects(0), ProductFinderVariable)
            Dim _ProductFinderVariable_ListEditor As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("ProductSelectVariables")
            Dim _NewProducts As Products = TryCast(_ProductFinderVariable_ListEditor.ListView.SelectedObjects(0), ProductSelect).Products

            If _NewProducts Is Nothing Then Throw New Exception("*Nincs kiválasztva az alternatíva!")

            Dim _OfferOutRows As OfferOutRows = TryCast(View.SelectedObjects(0), OfferOutRows)

            _OfferOutRows.Product = _ocur.GetObject(_NewProducts)
            _OfferOutRows.ListPriceHUF = _NewProducts.ListPriceOutHUF
            _OfferOutRows.ListPriceDEV = _NewProducts.ListPriceOutDEV
            _OfferOutRows.FinalPriceHUF = _NewProducts.ListPriceOutHUF
            _OfferOutRows.FinalPriceDEV = _NewProducts.ListPriceOutDEV

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub aBackToRowEdit_OfferOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToRowEdit_OfferOut.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(e.SelectedObjects(0), OfferOutHeader)
        Dim _OfferOutRows As OfferOutRows
        Dim _OfferOutRows_ListEditor As ListPropertyEditor


        _OfferOutRows_ListEditor = TryCast(View, DetailView).FindItem("OfferOutRows")
        If _OfferOutRows_ListEditor IsNot Nothing Then
            _OfferOutRows = _OfferOutRows_ListEditor.ListView.SelectedObjects(0)
            If _OfferOutRows IsNot Nothing Then
                _OfferOutHeader.row_RowGroup1 = _OfferOutRows.RowGroup1
                _OfferOutHeader.row_RowGroup2 = _OfferOutRows.RowGroup2
                _OfferOutHeader.row_DiscountPercent = 0
                _OfferOutHeader.row_Installation = Nothing
                _OfferOutHeader.row_ListPriceDEV = _OfferOutRows.ListPriceDEV
                _OfferOutHeader.row_ListPriceHUF = _OfferOutRows.ListPriceHUF
                _OfferOutHeader.row_QTT = _OfferOutRows.QTT
                _OfferOutHeader.row_RowType = _OfferOutRows.RowType
                _OfferOutHeader.row_ShortName = _OfferOutRows.ShortName
                _OfferOutHeader.row_Units = _OfferOutRows.Units
                _OfferOutHeader.row_VAT = _OfferOutRows.VAT
                _OfferOutHeader.row_Description = _OfferOutRows.Description
                _OfferOutHeader.row_Installation = _OfferOutRows.Installation
                _OfferOutHeader.row_Products = _OfferOutRows.Product

            End If
        End If

    End Sub

    Private Sub aImportOrderInRows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aImportOrderInRows.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _ImportOrderInRowsPopUp As ImportOrderInRowsPopUp = _onew.CreateObject(Of ImportOrderInRowsPopUp)()
            Dim _OfferOutHeader As OfferOutHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferOutHeader)
            _ImportOrderInRowsPopUp.CRM_Opportunities = _onew.GetObject(_OfferOutHeader.CRM_Opportunities)

            e.View = Application.CreateDetailView(_onew, "ImportOrderInRowsPopUp_DetailView", False, _ImportOrderInRowsPopUp)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aImportOrderInRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aImportOrderInRows.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _OfferOutHeader As OfferOutHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferOutHeader)
            Dim _ImportOrderInRowsPopUp As ImportOrderInRowsPopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ImportOrderInRowsPopUp)
            Dim _OrderInHeader_ListEditor As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("OrderInHeader")
            Dim _OrderInHeader As OrderInHeader = TryCast(_OrderInHeader_ListEditor.ListView.SelectedObjects(0), OrderInHeader)
            If _OrderInHeader Is Nothing Then Throw New Exception("*Nincs kiválasztott bejövő megrendelés!")
            If _OrderInHeader.OrderInRows.Count = 0 And _OrderInHeader.OrderInRowsOther.Count = 0 Then Throw New Exception("*A kiválasztott megrendelésnek nincsenek sorai!")

            For Each _OrderInRows As OrderInRows In _OrderInHeader.OrderInRows
                Dim _OfferOutRows As OfferOutRows = _ocur.CreateObject(Of OfferOutRows)()

                _OfferOutRows.OfferOutHeader = _OfferOutHeader
                _OfferOutRows.RowType = eRowType.IsProduct
                _OfferOutRows.RowGroup1 = _OrderInRows.RowGroup1
                _OfferOutRows.RowGroup2 = _OrderInRows.RowGroup2
                _OfferOutRows.Product = _ocur.GetObject(_OrderInRows.Products)
                _OfferOutRows.ShortName = _ocur.GetObject(_OrderInRows.Products).ShortName
                _OfferOutRows.Description = _OrderInRows.Description
                _OfferOutRows.DescriptionPrivate = _OrderInRows.DescriptionPrivate
                _OfferOutRows.ListPriceHUF = _OfferOutRows.ListPriceHUF
                _OfferOutRows.ListPriceDEV = _OfferOutRows.ListPriceDEV
                _OfferOutRows.DiscountPriceHUF = _OrderInRows.DiscountPriceHUF
                _OfferOutRows.DiscountPriceDEV = _OrderInRows.DiscountPriceDEV
                _OfferOutRows.QTT = _OrderInRows.QTT
                _OfferOutRows.Units = _ocur.GetObject(_OrderInRows.Products.Units)
                _OfferOutRows.QualityOption = _OrderInRows.QualityOption
                _OfferOutRows.VAT = _ocur.GetObject(_OrderInRows.Products.VAT)
                _OfferOutRows.FinalPriceHUF = _OrderInRows.FinalPriceHUF
                _OfferOutRows.FinalPriceDEV = _OrderInRows.FinalPriceDEV

            Next

            For Each _OrderInRowsOther As OrderInRowsOther In _OrderInHeader.OrderInRowsOther
                Dim _OfferOutRows As OfferOutRows = _ocur.CreateObject(Of OfferOutRows)()

                _OfferOutRows.OfferOutHeader = _OfferOutHeader
                _OfferOutRows.RowType = eRowType.IsOther
                _OfferOutRows.RowGroup1 = _OrderInRowsOther.RowGroup1
                _OfferOutRows.RowGroup2 = _OrderInRowsOther.RowGroup2
                _OfferOutRows.ShortName = _OrderInRowsOther.ShortName
                _OfferOutRows.Description = _OrderInRowsOther.Description
                _OfferOutRows.ListPriceHUF = _OrderInRowsOther.ListPriceHUF
                _OfferOutRows.ListPriceDEV = _OrderInRowsOther.ListPriceDEV
                _OfferOutRows.DiscountPriceHUF = _OrderInRowsOther.DiscountPriceHUF
                _OfferOutRows.DiscountPriceDEV = _OrderInRowsOther.DiscountPriceDEV
                _OfferOutRows.QTT = _OrderInRowsOther.QTT
                _OfferOutRows.Units = _ocur.GetObject(_OrderInRowsOther.Units)
                _OfferOutRows.QualityOption = _ocur.GetObject(_OrderInRowsOther.QualityOption)
                _OfferOutRows.VAT = _ocur.GetObject(_OrderInRowsOther.VAT)

            Next

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub

    Private Sub aOfferOut_RecalculateDEV_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aOfferOut_RecalculateDEV.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
        Dim _RecalcDEV As RecalcDEV = _onew.CreateObject(Of RecalcDEV)()

        '// MNB árfolyam lekérése
        Dim _CurrencyRate_Collection = New XPCollection(Of CurrencyRate) _
        (_onew.Session, CriteriaOperator.Parse("CurrencyName.ShortName=? and IsMNB=True and GetDate(DateValid)<=?", _
         _OfferOutHeader.CurrencyName.ShortName, Now.Date))
        _CurrencyRate_Collection.Sorting.Add(New SortProperty("DateValid", DB.SortingDirection.Descending))
        If _CurrencyRate_Collection.Count > 0 Then
            For Each _CurrencyRate In _CurrencyRate_Collection
                If _CurrencyRate.DateValid.Date = Now.Date Then
                    _RecalcDEV.CurrencyRate = _CurrencyRate.RateAverage
                    Exit For
                End If
                Exit For
            Next
        End If

        e.View = Application.CreateDetailView(_onew, "RecalcDEV_DetailView", True, _RecalcDEV)
    End Sub

    Private Sub aOfferOut_RecalculateDEV_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aOfferOut_RecalculateDEV.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
        Dim _RecalcDEV As RecalcDEV = TryCast(e.PopupWindow.View.SelectedObjects(0), RecalcDEV)
        Dim _OfferOutRows As OfferOutRows

        For Each _OfferOutRows In _OfferOutHeader.OfferOutRows
            If _RecalcDEV.PriceMode = ePriceMode.eHUFToDEV Then
                _OfferOutRows.ListPriceDEV = Math.Round(_OfferOutRows.ListPriceHUF / _RecalcDEV.CurrencyRate, 2, MidpointRounding.AwayFromZero)
                _OfferOutRows.DiscountPriceDEV = Math.Round(_OfferOutRows.ListPriceDEV * (_OfferOutRows.DiscountPercent / 100), 2, MidpointRounding.AwayFromZero)
                _OfferOutRows.FinalPriceDEV = _OfferOutRows.ListPriceDEV - _OfferOutRows.DiscountPriceDEV
                _OfferOutRows.RecalculateRow()
                _OfferOutRows.RecalculateRow2()
            End If
            If _RecalcDEV.PriceMode = ePriceMode.eDEVToHUF Then
                _OfferOutRows.ListPriceHUF = Math.Round(_OfferOutRows.ListPriceDEV * _RecalcDEV.CurrencyRate, 2, MidpointRounding.AwayFromZero)
                _OfferOutRows.DiscountPriceHUF = Math.Round(_OfferOutRows.ListPriceHUF * (_OfferOutRows.DiscountPercent / 100), 2, MidpointRounding.AwayFromZero)
                _OfferOutRows.FinalPriceHUF = _OfferOutRows.ListPriceHUF - _OfferOutRows.DiscountPriceHUF
                _OfferOutRows.RecalculateRow()
                _OfferOutRows.RecalculateRow2()
            End If
        Next
        _ocur.CommitChanges()

    End Sub

    Private Sub aOfferOutHeader_AddOfferInHeader_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aOfferOutHeader_AddOfferInHeader.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferOutHeader As OfferOutHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferOutHeader)

        Dim _OfferOutHeader_CollectionSource As New CollectionSource(_onew, GetType(OfferInHeader))

        _OfferOutHeader_CollectionSource.BeginUpdateCriteria()
        _OfferOutHeader_CollectionSource.Criteria.Add("CRM", CriteriaOperator.Parse("CRM_Opportunities=?", _onew.GetObject(_OfferOutHeader.CRM_Opportunities)))
        _OfferOutHeader_CollectionSource.EndUpdateCriteria()

        e.View = Application.CreateListView("OfferInHeader_ListView_OfferOutHeaderInHeader", _OfferOutHeader_CollectionSource, False)
    End Sub
    Private Sub aOfferOutHeader_AddOfferInHeader_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aOfferOutHeader_AddOfferInHeader.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferOutHeader)

        For Each _OfferInHeader As OfferInHeader In e.PopupWindow.View.SelectedObjects
            Dim _OfferOutHeaderInHeader As New OfferOutHeaderInHeader(_ocur.Session)
            With _OfferOutHeaderInHeader
                .OfferInHeader = _ocur.GetObject(_OfferInHeader)
                .OfferOutHeader = _OfferOutHeader
            End With
        Next

        _ocur.CommitChanges()
    End Sub

    Private Sub aOfferOutHeader_ImportOfferInRows_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aOfferOutHeader_ImportOfferInRows.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OfferInRows_CollectionSource As New CollectionSource(_onew, GetType(OfferInRows))

        e.View = Application.CreateListView("OfferInRows_ListView", _OfferInRows_CollectionSource, False)
    End Sub
    Private Sub aOfferOutHeader_ImportOfferInRows_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aOfferOutHeader_ImportOfferInRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferOutHeader)
        If _OfferOutHeader IsNot Nothing Then
            If e.PopupWindow.View.SelectedObjects.Count > 0 Then
                For Each _OfferInRows As OfferInRows In e.PopupWindow.View.SelectedObjects
                    Dim _OfferOutRows As New OfferOutRows(_ocur.Session)
                    With _OfferOutRows
                        .OfferOutHeader = _OfferOutHeader
                        .RowType = _OfferInRows.RowType
                        .RowGroup1 = _OfferInRows.RowGroup1
                        .RowGroup2 = _OfferInRows.RowGroup2
                        .Product = _ocur.GetObject(_OfferInRows.Product)
                        .Installation = _ocur.GetObject(_OfferInRows.Installation)
                        .ShortName = _OfferInRows.ShortName
                        .Description = _OfferInRows.Description
                        .DescriptionPrivate = _OfferInRows.DescriptionPrivate
                        .QTT = _OfferInRows.QTT
                        .QualityOption = _ocur.GetObject(_OfferInRows.QualityOption)
                        .Units = _ocur.GetObject(_OfferInRows.Units)
                        .VAT = _ocur.GetObject(_OfferInRows.VAT)
                        .SumPriceHUF = _OfferInRows.SumPriceHUF
                        .SumPriceDEV = _OfferInRows.SumPriceDEV
                        .FinalPriceHUF = _OfferInRows.FinalPriceHUF
                        .FinalPriceDEV = _OfferInRows.FinalPriceDEV
                    End With
                Next

            End If
            _ocur.CommitChanges()
        End If

    End Sub


    
End Class
