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.Xpo
Imports System.Linq
Imports System.Linq.Expressions
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Utils

Public Class ContractRowsVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork

    Public Sub New()

        MyBase.New()

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

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of ContractRowsVC).aDescriptionChanger_ContractRows.Active.SetItemValue("", False)
        Frame.GetController(Of ContractRowsVC).aShortNameChanger_ContractRows.Active.SetItemValue("", False)
        Frame.GetController(Of ContractRowsVC).aModifyContractRowsData.Active.SetItemValue("", False)
        Frame.GetController(Of ContractRowsVC).aConnectContractRowToInvoiceRow.Active.SetItemValue("", False)
        If View.Id = "ContractRows_DetailView" Then
            Frame.GetController(Of ContractRowsVC).aDescriptionChanger_ContractRows.Active.SetItemValue("", True)
            Frame.GetController(Of ContractRowsVC).aShortNameChanger_ContractRows.Active.SetItemValue("", True)
        End If

        If View.Id = "ContractRows_ListView_Invoice_Not_MileStone" Or _
           View.Id = "ContractRows_ListView_Invoice_MileStone" Or _
           View.Id = "ContractRows_ListView_Invoice_CustomRate" Or _
           View.Id = "ContractRows_ListView_Invoice_Advance" Then
            Frame.GetController(Of ContractRowsVC).aModifyContractRowsData.Active.SetItemValue("", True)
            Frame.GetController(Of ContractRowsVC).aConnectContractRowToInvoiceRow.Active.SetItemValue("", True)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of ContractRowsVC).aDescriptionChanger_ContractRows.Active.SetItemValue("", False)
        Frame.GetController(Of ContractRowsVC).aShortNameChanger_ContractRows.Active.SetItemValue("", False)
        Frame.GetController(Of ContractRowsVC).aModifyContractRowsData.Active.SetItemValue("", False)
    End Sub

    Private Sub aDescriptionChanger_ContractRows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aDescriptionChanger_ContractRows.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractRows_DescriptionChanger_PopUp As InvoiceHeader_DescriptionChanger_PopUp
        Dim _CS_Description As New CollectionSource(_onew, GetType(InvoiceHeader_DescriptionChanger_PopUp))

        Dim _ContractRows_xpquery = New XPQuery(Of ContractRows)(_ocur.Session)
        Dim query = From _ContractRows In _ContractRows_xpquery _
                    Where _ContractRows.Description IsNot Nothing _
        Group _ContractRows By _ContractRows.Description Into g = Group _
        Select New With {Key .Description = Description}
        For Each item In query
            _ContractRows_DescriptionChanger_PopUp = _onew.CreateObject(Of InvoiceHeader_DescriptionChanger_PopUp)()
            _ContractRows_DescriptionChanger_PopUp.Description = item.Description
            _CS_Description.Add(_ContractRows_DescriptionChanger_PopUp)
        Next
        e.View = Application.CreateListView("InvoiceHeader_DescriptionChanger_PopUp_ListView", _CS_Description, False)
    End Sub
    Private Sub aDescriptionChanger_ContractRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aDescriptionChanger_ContractRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractRows As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)
        For Each _Description In e.PopupWindow.View.SelectedObjects
            If _ContractRows.Description Is Nothing Then
                _ContractRows.Description = TryCast(_Description, InvoiceHeader_DescriptionChanger_PopUp).Description
            Else
                _ContractRows.Description += vbNewLine & TryCast(_Description, InvoiceHeader_DescriptionChanger_PopUp).Description
            End If
        Next
    End Sub

    Private Sub aShortNameChanger_ContractRows_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aShortNameChanger_ContractRows.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractRows_ShortNameChanger_PopUp As InvoiceHeader_DescriptionChanger_PopUp
        Dim _CS_ShortName As New CollectionSource(_onew, GetType(InvoiceHeader_DescriptionChanger_PopUp))

        Dim _ContractRows_xpquery = New XPQuery(Of ContractRows)(_ocur.Session)
        Dim query = From _ContractRows In _ContractRows_xpquery _
                    Where _ContractRows.Description IsNot Nothing _
        Group _ContractRows By _ContractRows.ShortName Into g = Group _
        Select New With {Key .ShortName = ShortName}
        For Each item In query
            _ContractRows_ShortNameChanger_PopUp = _onew.CreateObject(Of InvoiceHeader_DescriptionChanger_PopUp)()
            _ContractRows_ShortNameChanger_PopUp.Description = item.ShortName
            _CS_ShortName.Add(_ContractRows_ShortNameChanger_PopUp)
        Next
        e.View = Application.CreateListView("InvoiceHeader_DescriptionChanger_PopUp_ListView", _CS_ShortName, False)
    End Sub
    Private Sub aShortNameChanger_ContractRows_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aShortNameChanger_ContractRows.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractRows As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)
        For Each _ShortName In e.PopupWindow.View.SelectedObjects
            If _ContractRows.ShortName Is Nothing Then
                _ContractRows.ShortName = TryCast(_ShortName, InvoiceHeader_DescriptionChanger_PopUp).Description
            Else
                _ContractRows.ShortName += " " & TryCast(_ShortName, InvoiceHeader_DescriptionChanger_PopUp).Description
            End If
        Next
    End Sub

    Private Sub aModifyContractRowsData_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aModifyContractRowsData.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace()
        Dim _ContractRowsModifyData As ContractRowsModifyData = _onew.CreateObject(Of ContractRowsModifyData)()

        e.View = Application.CreateDetailView(_onew, "ContractRowsModifyData_DetailView", False, _ContractRowsModifyData)
    End Sub
    Private Sub aModifyContractRowsData_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aModifyContractRowsData.Execute

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractRowsModifyData As ContractRowsModifyData = e.PopupWindow.View.SelectedObjects(0)
        Dim _ArrayList As ArrayList = New ArrayList()
        Dim _uow_lock As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _User As DevExpress.Persistent.BaseImpl.User = _ocur.GetObject(TryCast(SecuritySystem.CurrentUser, DevExpress.Persistent.BaseImpl.User))
        Try
            Dim _ContractRows As ContractRows
            For Each _ContractRows In e.SelectedObjects
                If Not LockingModule.LockObject(_uow_lock, GetType(ContractRows), _ContractRows.Oid.ToString(), _User) Then
                    Dim _LockingObject As LockingObject = _ocur.FindObject(Of LockingObject)(CriteriaOperator.Parse("TableName=?", GetType(ContractRows).Name + "-" + _ContractRows.Oid.ToString()))
                    _ArrayList.Add(_ContractRows)
                End If
            Next

            If _ArrayList.Count = 0 Then
                For Each _ContractRows In e.SelectedObjects
                    If _ContractRowsModifyData.IsDateExecution Then _ContractRows.DateExecution = _ContractRowsModifyData.DateExecution
                    If _ContractRowsModifyData.IsPaymentOption Then _ContractRows.PaymentOption = _ocur.GetObjectByKey(Of PaymentOption)(_ContractRowsModifyData.PaymentOption.Oid)
                    If _ContractRowsModifyData.IsNettoPriceHUF Then _ContractRows.NettoPriceHUF = _ContractRowsModifyData.NettoPriceHUF
                    If _ContractRowsModifyData.IsNettoPriceDEV Then _ContractRows.NettoPriceDEV = _ContractRowsModifyData.NettoPriceDEV
                    If _ContractRowsModifyData.IsDescription Then _ContractRows.Description = _ContractRowsModifyData.Description
                    If _ContractRowsModifyData.IsShortName Then _ContractRows.ShortName = _ContractRowsModifyData.ShortName
                    If _ContractRowsModifyData.IsInvoicePeriod Then _ContractRows.InvoicePeriod = _ContractRowsModifyData.InvoicePeriod
                    If _ContractRowsModifyData.IsVAT = True Then _ContractRows.VAT = _ocur.GetObjectByKey(Of VAT)(_ContractRowsModifyData.VAT.Oid)
                    If _ContractRowsModifyData.IsJobNumberObjects Then _ContractRows.JobNumberObjects = _ocur.GetObjectByKey(Of JobNumberObjects)(_ContractRowsModifyData.JobNumberObjects.Oid)
                    If _ContractRowsModifyData.IsUniqueObjects Then _ContractRows.UniqueObjects = _ocur.GetObjectByKey(Of UniqueObjects)(_ContractRowsModifyData.UniqueObjects.Oid)
                    If _ContractRowsModifyData.IsControlling Then _ContractRows.Controlling = _ocur.GetObjectByKey(Of Controlling)(_ContractRowsModifyData.Controlling.Oid)
                    If _ContractRowsModifyData.IsCostHolder Then _ContractRows.CostHolder = _ocur.GetObjectByKey(Of CostHolder)(_ContractRowsModifyData.CostHolder.Oid)
                Next
                _ocur.CommitChanges()
            End If

            If Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RefreshController).RefreshAction.Enabled Then
                Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RefreshController).RefreshAction.DoExecute()
            End If
            ' View.Refresh()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
        Finally
            If _ArrayList.Count > 0 Then
                Dim ModifyError As String = "Objektum(ok) zárolva van(nak) :" + vbNewLine + vbNewLine

                For Each item As ContractRows In _ArrayList
                    ModifyError += item.Contracts.DocumentNumber.ToString() + " " + item.ShortName.ToString() + vbNewLine
                Next
                ModifyError += vbNewLine + "Ezért a csoportos módosítás nem lett végrehajtva!"

                Dim _Title As String = DevExpress.ExpressApp.Utils.CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")
                MsgBox(String.Format(ModifyError), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, _Title)

            End If
            For Each _ContractRows In e.SelectedObjects
                LockingModule.UnLockObject(_uow_lock, GetType(ContractRows), _ContractRows.Oid.ToString(), _User)
            Next
        End Try
    End Sub

    Private Sub aConnectContractRowToInvoiceRow_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aConnectContractRowToInvoiceRow.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractRows As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)
        Dim _CS As CollectionSource = New CollectionSource(_onew, GetType(InvoiceRows))

        _CS.BeginUpdateCriteria()
        _CS.Criteria.Add("Criteria", CriteriaOperator.Parse("isnull(ContractRows)=True and InvoiceHeader.CustomersFrom.Oid=? and InvoiceHeader.Customers.Oid=? and InvoiceHeader.IsEditable=False and InvoiceHeader.IsStorno=False and InvoiceHeader.IsProforma=False", _
                                                            _ContractRows.Contracts.CustomersFrom.Oid, _ContractRows.Contracts.Customers.Oid))
        _CS.EndUpdateCriteria()

        e.View = Application.CreateListView("InvoiceRows_ListView_ContractRows", _CS, False)

    End Sub
    Private Sub aConnectContractRowToInvoiceRow_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aConnectContractRowToInvoiceRow.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractRows As ContractRows = TryCast(View.SelectedObjects(0), ContractRows)
        Dim _InvoiceRows As InvoiceRows = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), InvoiceRows))

        If _InvoiceRows IsNot Nothing Then
            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)
            _InvoiceRows.ContractRows = _ContractRows
            _ContractRows.InvoiceRows = _InvoiceRows
            _ocur.CommitChanges()

            Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
        End If
    End Sub

    Private Sub aSyncImmovables_Execute(sender As Object, e As SimpleActionExecuteEventArgs)
        'Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        'Dim _ContractRows_Collection As New XPCollection(Of ContractRows)(_onew.Session, CriteriaOperator.Parse("isnull(UniqueObjects)=False"))
        'Dim _ContractRows As ContractRows

        'Dim _Apartments As Apartments
        'Dim _Garages As Garages
        'Dim _Offices As Offices
        'Dim _Container As Container

        'RaiseEvent InitWork(1, 1, _ContractRows_Collection.Count)

        'For Each _ContractRows In _ContractRows_Collection
        '    RaiseEvent DoWork()
        '    _Apartments = _onew.FindObject(Of Apartments)(CriteriaOperator.Parse("UniqueObjects=?", _ContractRows.UniqueObjects))
        '    If _Apartments IsNot Nothing Then
        '        _ContractRows.Apartments = _Apartments
        '    End If

        '    _Garages = _onew.FindObject(Of Garages)(CriteriaOperator.Parse("UniqueObjects=?", _ContractRows.UniqueObjects))
        '    If _Garages IsNot Nothing Then
        '        _ContractRows.Garages = _Garages
        '    End If

        '    _Offices = _onew.FindObject(Of Offices)(CriteriaOperator.Parse("UniqueObjects=?", _ContractRows.UniqueObjects))
        '    If _Offices IsNot Nothing Then
        '        _ContractRows.Offices = _Offices
        '    End If

        '    _Container = _onew.FindObject(Of Container)(CriteriaOperator.Parse("UniqueObjects=?", _ContractRows.UniqueObjects))
        '    If _Container IsNot Nothing Then
        '        _ContractRows.Container = _Container
        '    End If
        '    _onew.CommitChanges()
        'Next
        'RaiseEvent FinalWork

    End Sub
End Class
