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

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Editors

Public Class OfferAskIn_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "OfferAskInCreateOfferAskOutFromCustomersPopUp_CustomersQualification_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.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "OfferAskInCreateOfferAskOutFromCustomersPopUp_CustomersQualification_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.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aOfferAskInRowsAddOperations_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOfferAskInRowsAddOperations.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

            e.View = Application.CreateListView(_onew, GetType(Operations), False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aOfferAskInRowsAddOperations_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOfferAskInRowsAddOperations.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _OfferAskIn As OfferAskIn = Nothing
            If TryCast(View, ListView) IsNot Nothing Then
                _OfferAskIn = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferAskIn)
                If _OfferAskIn Is Nothing Then
                    _OfferAskIn = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, OfferAskInRows).OfferAskIn
                End If
            ElseIf TryCast(View, DetailView) IsNot Nothing Then
                _OfferAskIn = TryCast(View.SelectedObjects(0), OfferAskInRows).OfferAskIn
            End If

            If _OfferAskIn Is Nothing Then Throw New Exception("*Nincs bejövő ajánlat fejléc!")

            For Each _Operations As Operations In e.PopupWindow.View.SelectedObjects
                Dim _OfferAskInRows As OfferAskInRows
                _OfferAskInRows = _ocur.FindObject(Of OfferAskInRows)(CriteriaOperator.Parse("OfferAskIn=? AND Operations=?", _ocur.GetObjectByKey(Of OfferAskIn)(_OfferAskIn.Oid), _
                                                                                             _ocur.GetObjectByKey(Of Operations)(_Operations.Oid)))
                If _OfferAskInRows Is Nothing Then
                    _OfferAskInRows = _ocur.CreateObject(Of OfferAskInRows)()
                    _OfferAskInRows.OfferAskIn = _ocur.GetObjectByKey(Of OfferAskIn)(_OfferAskIn.Oid)
                    _OfferAskInRows.Operations = _ocur.GetObjectByKey(Of Operations)(_Operations.Oid)
                End If
            Next
            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub

    Private Sub aOfferAskInCreateOfferAskOutFromCustomers_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aOfferAskInCreateOfferAskOutFromCustomers.CustomizePopupWindowParams
        Try
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _OfferAskInCreateOfferAskOutFromCustomersPopUp As OfferAskInCreateOfferAskOutFromCustomersPopUp = _onew.CreateObject(Of OfferAskInCreateOfferAskOutFromCustomersPopUp)()
            Dim _OfferAskInRows As OfferAskInRows = TryCast(View.SelectedObjects(0), OfferAskInRows)

            If _OfferAskInRows Is Nothing Then Throw New Exception("*Nincs kiválasztott művelet!")

            _OfferAskInCreateOfferAskOutFromCustomersPopUp.Operations = _onew.GetObject(_OfferAskInRows.Operations)

            e.View = Application.CreateDetailView(_onew, "OfferAskInCreateOfferAskOutFromCustomersPopUp_DetailView", False, _OfferAskInCreateOfferAskOutFromCustomersPopUp)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        End Try
    End Sub
    Private Sub aOfferAskInCreateOfferAskOutFromCustomers_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aOfferAskInCreateOfferAskOutFromCustomers.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _OfferAskInRows As OfferAskInRows = TryCast(View.SelectedObjects(0), OfferAskInRows)
            Dim _OfferAskInCreateOfferAskOutFromCustomersPopUp As OfferAskInCreateOfferAskOutFromCustomersPopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), OfferAskInCreateOfferAskOutFromCustomersPopUp)

            Dim _OfferAskInRows_ListPropertyEditor As ListPropertyEditor = TryCast(e.PopupWindow.View, DetailView).FindItem("CustomersQualification")

            For Each _CustomersQualification As CustomersQualification In _OfferAskInRows_ListPropertyEditor.ListView.SelectedObjects
                Dim _OfferAskOut As OfferAskOut
                _OfferAskOut = _ocur.FindObject(Of OfferAskOut)(CriteriaOperator.Parse("CustomersFrom=? AND Customers=? AND OfferAskOutState='eInProgress'", _
                                                                                       _ocur.GetObject(_OfferAskInRows.OfferAskIn.CustomersFrom), _ocur.GetObject(_CustomersQualification.Customers)))
                If _OfferAskOut Is Nothing Then
                    _OfferAskOut = _ocur.CreateObject(Of OfferAskOut)()
                    _OfferAskOut.CustomersFrom = _ocur.GetObject(_OfferAskInRows.OfferAskIn.CustomersFrom)
                    _OfferAskOut.Customers = _ocur.GetObject(_CustomersQualification.Customers)
                    _OfferAskOut.OfferAskOutState = eOfferAskOutState.eInProgress
                    _OfferAskOut.JobNumberObjects = _ocur.GetObject(_OfferAskInRows.OfferAskIn.JobNumberObjects)
                End If

                _OfferAskOut.DateValid = _OfferAskInCreateOfferAskOutFromCustomersPopUp.DateValid

                Dim _OfferAskOutRows As OfferAskOutRows = _ocur.CreateObject(Of OfferAskOutRows)()
                _OfferAskOutRows.OfferAskOut = _ocur.GetObject(_OfferAskOut)
                _OfferAskOutRows.Operations = _ocur.GetObjectByKey(Of Operations)(_OfferAskInCreateOfferAskOutFromCustomersPopUp.Operations.Oid)
            Next

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