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.Xpo

Public Class OrderInAcknowledgementVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

        InitializeComponent()
        RegisterActions(components)

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

        If View.Id = "OrderInHeader_OrderInAcknowledgementHeader_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OrderInAcknowledgementHeader_OrderInAcknowledgementRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().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.DeleteObjectsViewController)().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 = "OrderInHeader_OrderInAcknowledgementHeader_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
        If View.Id = "OrderInAcknowledgementHeader_OrderInAcknowledgementRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().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.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aCreateAcknowledgementFromOrderIn_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateAcknowledgementFromOrderIn.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _OrderInHeader As OrderInHeader = _onew.GetObject(TryCast(View.SelectedObjects(0), OrderInHeader))
        Dim _OrderInAcknowledgementPopup As OrderInAcknowledgementPopup = _onew.CreateObject(Of OrderInAcknowledgementPopup)()

        e.View = Application.CreateDetailView(_onew, "OrderInAcknowledgementPopup_DetailView", False, _OrderInAcknowledgementPopup)
    End Sub
    Private Sub aCreateAcknowledgementFromOrderIn_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateAcknowledgementFromOrderIn.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OrderInAcknowledgementHeader_New As OrderInAcknowledgementHeader = _ocur.CreateObject(Of OrderInAcknowledgementHeader)()
        Dim _OrderInHeader As OrderInHeader = _ocur.GetObject(TryCast(View.SelectedObjects(0), OrderInHeader))
        Dim _OrderInAcknowledgementRows_New As OrderInAcknowledgementRows
        Dim _OrderInRows As OrderInRows
        Dim _OrderInRowsOther As OrderInRowsOther
        Dim i As Integer = 0

        '----- FEJLÉC ADATOK ÁTADÁSA --------------------------------------------------------------------------
        _OrderInAcknowledgementHeader_New.OrderInHeader = _OrderInHeader
        _OrderInAcknowledgementHeader_New.DateCreated = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OrderInAcknowledgementPopup).DateCreated)
        _OrderInAcknowledgementHeader_New.DocumentNumber = _OrderInHeader.OrderInParameters.NumberGeneratorAcknowledgement.GetNewNumber(_OrderInHeader.OrderInParameters.NumberGeneratorAcknowledgement)
        _OrderInAcknowledgementHeader_New.NoteHeader = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OrderInAcknowledgementPopup).NoteHeader)
        _OrderInAcknowledgementHeader_New.PaymentOption = _OrderInHeader.PaymentOption
        _OrderInAcknowledgementHeader_New.ShipmentOption = _OrderInHeader.ShipmentOption

        _OrderInAcknowledgementHeader_New.Save()
        _OrderInHeader.OrderInRows.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))

        '----- SOROK ÁTADÁSA ----------------------------------------------------------------------------------
        For i = 0 To _OrderInHeader.OrderInRows.Count - 1
            _OrderInRows = _ocur.GetObject(_OrderInHeader.OrderInRows(i))
            _OrderInAcknowledgementRows_New = _ocur.CreateObject(Of OrderInAcknowledgementRows)()

            _OrderInAcknowledgementRows_New.OrderInAcknowledgementHeader = _OrderInAcknowledgementHeader_New
            _OrderInAcknowledgementRows_New.FinalPriceDEV = _OrderInRows.FinalPriceDEV
            _OrderInAcknowledgementRows_New.FinalPriceHUF = _OrderInRows.FinalPriceHUF
            _OrderInAcknowledgementRows_New.Description = _OrderInRows.Description
            _OrderInAcknowledgementRows_New.QTT = _OrderInRows.QTT
            _OrderInAcknowledgementRows_New.QualityOption = _OrderInRows.QualityOption
            _OrderInAcknowledgementRows_New.RowGroup1 = _OrderInRows.RowGroup1
            _OrderInAcknowledgementRows_New.RowGroup2 = _OrderInRows.RowGroup2
            _OrderInAcknowledgementRows_New.Units = _OrderInRows.Products.Units
            _OrderInAcknowledgementRows_New.VAT = _OrderInRows.VAT
            _OrderInAcknowledgementRows_New.SumPriceHUF = _OrderInRows.FinalPriceHUF * _OrderInRows.QTT
            _OrderInAcknowledgementRows_New.SumPriceDEV = _OrderInRows.FinalPriceDEV * _OrderInRows.QTT
            _OrderInAcknowledgementRows_New.ShortName = _OrderInRows.Products.ShortName
            _OrderInAcknowledgementRows_New.RowIndex = i
            _OrderInAcknowledgementRows_New.OrderInRows = _OrderInRows

            _OrderInAcknowledgementRows_New.Save()
        Next

        For y = 0 To _OrderInHeader.OrderInRowsOther.Count - 1
            _OrderInRowsOther = _ocur.GetObject(_OrderInHeader.OrderInRowsOther(y))
            _OrderInAcknowledgementRows_New = _ocur.CreateObject(Of OrderInAcknowledgementRows)()

            _OrderInAcknowledgementRows_New.OrderInAcknowledgementHeader = _OrderInAcknowledgementHeader_New
            _OrderInAcknowledgementRows_New.FinalPriceDEV = _OrderInRowsOther.ListPriceDEV - _OrderInRowsOther.DiscountPriceDEV
            _OrderInAcknowledgementRows_New.FinalPriceHUF = _OrderInRowsOther.ListPriceHUF - _OrderInRowsOther.DiscountPriceHUF
            _OrderInAcknowledgementRows_New.Description = _OrderInRowsOther.Description
            _OrderInAcknowledgementRows_New.QTT = _OrderInRowsOther.QTT
            _OrderInAcknowledgementRows_New.QualityOption = _OrderInRowsOther.QualityOption
            _OrderInAcknowledgementRows_New.RowGroup1 = _OrderInRowsOther.RowGroup1
            _OrderInAcknowledgementRows_New.RowGroup2 = _OrderInRowsOther.RowGroup2
            _OrderInAcknowledgementRows_New.Units = _OrderInRowsOther.Units
            _OrderInAcknowledgementRows_New.VAT = _OrderInRowsOther.VAT
            _OrderInAcknowledgementRows_New.SumPriceHUF = (_OrderInRowsOther.ListPriceHUF - _OrderInRowsOther.DiscountPriceHUF) * _OrderInRowsOther.QTT
            _OrderInAcknowledgementRows_New.SumPriceDEV = (_OrderInRowsOther.ListPriceDEV - _OrderInRowsOther.DiscountPriceDEV) * _OrderInRowsOther.QTT
            _OrderInAcknowledgementRows_New.OrderInRowsOther = _OrderInRowsOther
            If i = 0 Then
                _OrderInAcknowledgementRows_New.RowIndex = y
            Else
                _OrderInAcknowledgementRows_New.RowIndex = y + i + 1
            End If
            _OrderInAcknowledgementRows_New.ShortName = _OrderInRowsOther.ShortName

            _OrderInAcknowledgementRows_New.Save()
        Next

        _OrderInAcknowledgementHeader_New.RecalculateOrderInAcknowledgement(_OrderInAcknowledgementHeader_New, _ocur)

        _ocur.CommitChanges()
    End Sub
    Private Sub aCreateContractsFromAcknowledgement_CustomizePopupWindowParams(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aCreateContractsFromAcknowledgement.CustomizePopupWindowParams
        Dim _OrderInAcknowledgementHeader As OrderInAcknowledgementHeader = TryCast(View.SelectedObjects(0), OrderInAcknowledgementHeader)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ContractsFromAcknowledgement_PopUp As ContractsFromAcknowledgement_PopUp = _onew.CreateObject(Of ContractsFromAcknowledgement_PopUp)()

        _ContractsFromAcknowledgement_PopUp.OrderInAcknowledgementHeader = _OrderInAcknowledgementHeader
        _ContractsFromAcknowledgement_PopUp.CustomersFrom = _onew.GetObject(_OrderInAcknowledgementHeader.OrderInHeader.CustomersFrom)
        _ContractsFromAcknowledgement_PopUp.Save()

        e.View = Application.CreateDetailView(_onew, _ContractsFromAcknowledgement_PopUp, True)

    End Sub
    Private Sub aCreateContractsFromAcknowledgement_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aCreateContractsFromAcknowledgement.Execute

        'A szerződés sor jelölő adatai itt nem fontosak így a függvény élettartamára kikapcsoljuk a validálást
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ContractsFromAcknowledgement_PopUp As ContractsFromAcknowledgement_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), ContractsFromAcknowledgement_PopUp)
        Dim _Contracts As Contracts
        Dim _ContractRows As ContractRows
        Dim _ContractTemplate As ContractTemplate = _ocur.FindObject(Of ContractTemplate)(CriteriaOperator.Parse("Oid=?", _ContractsFromAcknowledgement_PopUp.ContractTemplate.Oid))



        _Contracts = _ocur.CreateObject(Of Contracts)()
        _Contracts.ContractTemplate = _ContractTemplate
        _Contracts.State = State.Ready
        _Contracts.DocumentNumber = _Contracts.ContractTemplate.NumberGenerator.GetNewNumber(_Contracts.ContractTemplate.NumberGenerator)
        _Contracts.CustomersFrom = _ContractsFromAcknowledgement_PopUp.OrderInAcknowledgementHeader.OrderInHeader.CustomersFrom
        _Contracts.Customers = _ContractsFromAcknowledgement_PopUp.OrderInAcknowledgementHeader.OrderInHeader.Customers
        _Contracts.DateCreated = GetSQLTime(_ocur.Session)
        _Contracts.CRM_Opportunities = _ContractsFromAcknowledgement_PopUp.OrderInAcknowledgementHeader.OrderInHeader.CRM_Opportunities
        _Contracts.ShortName = _ContractsFromAcknowledgement_PopUp.OrderInAcknowledgementHeader.DocumentNumber & " számú rendelés visszaigazolás szerződése"


        For Each _OrderInAcknowledgementRows As OrderInAcknowledgementRows In _ContractsFromAcknowledgement_PopUp.OrderInAcknowledgementHeader.OrderInAcknowledgementRows

            _ContractRows = _ocur.CreateObject(Of ContractRows)()
            _ContractRows.Contracts = _Contracts
            _ContractRows.ShortName = _OrderInAcknowledgementRows.ShortName
            _ContractRows.DateExecution = _OrderInAcknowledgementRows.DateExecution
            _ContractRows.Units = _OrderInAcknowledgementRows.Units
            _ContractRows.VAT = _OrderInAcknowledgementRows.VAT
            _ContractRows.ContractRowsType = eContractRowsType.eFix
            _ContractRows.CurrencyName = _ContractsFromAcknowledgement_PopUp.OrderInAcknowledgementHeader.OrderInHeader.OrderInParameters.CurrencyName
            _ContractRows.NettoPriceHUF = _OrderInAcknowledgementRows.FinalPriceHUF
            _ContractRows.NettoPriceDEV = _OrderInAcknowledgementRows.FinalPriceDEV
            _ContractRows.RowGroup1 = _OrderInAcknowledgementRows.RowGroup1
            _ContractRows.RowGroup2 = _OrderInAcknowledgementRows.RowGroup2
        Next
        _ocur.CommitChanges()
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)

    End Sub
    Private Sub aAcknowledgementRows_Up_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aAcknowledgementRows_Up.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OrderInAcknowledgementRowsSelected As OrderInAcknowledgementRows = TryCast(View.SelectedObjects(0), OrderInAcknowledgementRows)
            If _OrderInAcknowledgementRowsSelected Is Nothing Then Throw New Exception("*Nincs kiválasztva egyetlen sor sem!")
            If _OrderInAcknowledgementRowsSelected.RowIndex = 0 Then
                _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader.OrderInAcknowledgementRows.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))
                Dim _RowIndex As Long = 1
                For Each _OrderInAcknowledgementRows As OrderInAcknowledgementRows In _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader.OrderInAcknowledgementRows
                    _OrderInAcknowledgementRows.RowIndex = _RowIndex
                    _RowIndex += 1
                Next
            Else
                Dim _HaveZeroIndex As Boolean = False
                For Each _OrderInAcknowledgementRows As OrderInAcknowledgementRows In _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader.OrderInAcknowledgementRows
                    If _OrderInAcknowledgementRows.RowIndex = 0 Then
                        _HaveZeroIndex = True
                        Exit For
                    End If
                Next
                If _HaveZeroIndex Then
                    Dim _RowIndex As Long = 1
                    For Each _OrderInAcknowledgementRows As OrderInAcknowledgementRows In _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader.OrderInAcknowledgementRows
                        _OrderInAcknowledgementRows.RowIndex = _RowIndex
                        _RowIndex += 1
                    Next
                Else
                    If _OrderInAcknowledgementRowsSelected.RowIndex = 1 Then Throw New Exception("*A kiválasztott sor a legelső a listában!")
                    Dim _OrderInAcknowledgementRowsPrev As OrderInAcknowledgementRows = _ocur.FindObject(Of OrderInAcknowledgementRows) _
                                                            (CriteriaOperator.Parse("OrderInAcknowledgementHeader=? AND RowIndex=?", _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader, _OrderInAcknowledgementRowsSelected.RowIndex - 1))
                    If _OrderInAcknowledgementRowsPrev IsNot Nothing Then
                        _OrderInAcknowledgementRowsSelected.RowIndex -= 1
                        _OrderInAcknowledgementRowsPrev.RowIndex += 1
                    End If
                End If
            End If

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
    Private Sub aAcknowledgementRows_Down_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aAcknowledgementRows_Down.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _OrderInAcknowledgementRowsSelected As OrderInAcknowledgementRows = TryCast(View.SelectedObjects(0), OrderInAcknowledgementRows)
            If _OrderInAcknowledgementRowsSelected Is Nothing Then Throw New Exception("*Nincs kiválasztva egyetlen sor sem!")
            If _OrderInAcknowledgementRowsSelected.RowIndex = 0 Then
                _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader.OrderInAcknowledgementRows.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))
                Dim _RowIndex As Long = 1
                For Each _OrderInAcknowledgementRows As OrderInAcknowledgementRows In _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader.OrderInAcknowledgementRows
                    _OrderInAcknowledgementRows.RowIndex = _RowIndex
                    _RowIndex += 1
                Next
            Else
                Dim _HaveZeroIndex As Boolean = False
                For Each _OrderInAcknowledgementRows As OrderInAcknowledgementRows In _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader.OrderInAcknowledgementRows
                    If _OrderInAcknowledgementRows.RowIndex = 0 Then
                        _HaveZeroIndex = True
                        Exit For
                    End If
                Next
                If _HaveZeroIndex Then
                    Dim _RowIndex As Long = 1
                    For Each _OrderInAcknowledgementRows As OrderInAcknowledgementRows In _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader.OrderInAcknowledgementRows
                        _OrderInAcknowledgementRows.RowIndex = _RowIndex
                        _RowIndex += 1
                    Next
                Else
                    If _OrderInAcknowledgementRowsSelected.RowIndex = _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader.OrderInAcknowledgementRows.Count Then Throw New Exception("*A kiválasztott sor a legutolsó a listában!")
                    Dim _OrderInAcknowledgementRowsNext As OrderInAcknowledgementRows = _ocur.FindObject(Of OrderInAcknowledgementRows) _
                                                            (CriteriaOperator.Parse("OrderInAcknowledgementHeader=? AND RowIndex=?", _OrderInAcknowledgementRowsSelected.OrderInAcknowledgementHeader, _OrderInAcknowledgementRowsSelected.RowIndex + 1))
                    If _OrderInAcknowledgementRowsNext IsNot Nothing Then
                        _OrderInAcknowledgementRowsSelected.RowIndex += 1
                        _OrderInAcknowledgementRowsNext.RowIndex -= 1
                    End If
                End If
            End If

            _ocur.CommitChanges()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
End Class
