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.ExpressApp.Editors
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo

Public Class InvoiceHoldPopupVC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

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

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

    End Sub

    Private Sub aToTable_InvoiceHold_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTable_InvoiceHold.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHoldPopup As InvoiceHoldPopup = TryCast(View.SelectedObjects(0), InvoiceHoldPopup)
        Dim _InvoiceHold_Collection As XPCollection(Of InvoiceHold)
        Dim _InvoiceHold As InvoiceHold
        Dim _AmountRemainingHUF As Double = _InvoiceHoldPopup.InvoiceHeader.TotalBruttoHUF
        Dim _AmountRemainingDEV As Double = _InvoiceHoldPopup.InvoiceHeader.TotalBruttoDEV


        _InvoiceHold = _ocur.FindObject(Of InvoiceHold)(CriteriaOperator.Parse("RowType='Normal' and InvoiceHeader.Oid=?", _InvoiceHoldPopup.InvoiceHeader.Oid), PersistentCriteriaEvaluationBehavior.InTransaction)

        If _InvoiceHold IsNot Nothing Then
            _ocur.Delete(_InvoiceHold)
        End If

        _ocur.CommitChanges()

        _InvoiceHold = _ocur.CreateObject(Of InvoiceHold)()
        _InvoiceHold.RowType = eHoldRowType.Hold
        _InvoiceHold.InvoiceHeader = _InvoiceHoldPopup.InvoiceHeader
        _InvoiceHold.ShortName = _InvoiceHoldPopup.row_ShortName
        _InvoiceHold.AmountHUF = _InvoiceHoldPopup.row_AmountHUF
        _InvoiceHold.AmountDEV = _InvoiceHoldPopup.row_AmountDEV
        _InvoiceHold.DatePayment = _InvoiceHoldPopup.row_DatePayment

        _ocur.CommitChanges()

        _InvoiceHold_Collection = New XPCollection(Of InvoiceHold)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, CriteriaOperator.Parse("InvoiceHeader.Oid=?", _InvoiceHoldPopup.InvoiceHeader.Oid))
        For Each _InvoiceHold In _InvoiceHold_Collection
            _AmountRemainingHUF -= _InvoiceHold.AmountHUF
            _AmountRemainingDEV -= _InvoiceHold.AmountDEV
        Next

        _InvoiceHold = _ocur.CreateObject(Of InvoiceHold)()
        _InvoiceHold.RowType = eHoldRowType.Normal
        _InvoiceHold.InvoiceHeader = _InvoiceHoldPopup.InvoiceHeader
        _InvoiceHold.ShortName = ""
        _InvoiceHold.AmountHUF = _AmountRemainingHUF
        _InvoiceHold.AmountDEV = _AmountRemainingDEV
        _InvoiceHold.DatePayment = _InvoiceHoldPopup.InvoiceHeader.DatePayment

        _ocur.CommitChanges()


        Dim _DetailView As DetailView = TryCast(View, DetailView)
        If _DetailView IsNot Nothing Then
            Dim _ListPropertyEditor As ListPropertyEditor = _DetailView.FindItem("InvoiceHold")

            If _ListPropertyEditor IsNot Nothing Then
                TryCast(_ListPropertyEditor.ListView, ListView).CollectionSource.Reload()
            End If

        End If



    End Sub

    Private Sub aToTableD_InvoiceHold_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aToTableD_InvoiceHold.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHoldPopup As InvoiceHoldPopup = TryCast(View.SelectedObjects(0), InvoiceHoldPopup)
        Dim _InvoiceHold_Collection As XPCollection(Of InvoiceHold) = Nothing
        Dim _InvoiceHold As InvoiceHold = Nothing
        Dim _AmountRemainingHUF As Double = _InvoiceHoldPopup.InvoiceHeader.TotalBruttoHUF
        Dim _AmountRemainingDEV As Double = _InvoiceHoldPopup.InvoiceHeader.TotalBruttoDEV
        Dim _DetailView As DetailView = TryCast(View, DetailView)
        Dim _ListPropertyEditor As ListPropertyEditor = _DetailView.FindItem("InvoiceHold")
        Dim _IsNormal = False

        If _ListPropertyEditor IsNot Nothing Then


            Dim _ListView As ListView = TryCast(_ListPropertyEditor.ListView, ListView)

            For Each _InvoiceHold In _ListView.SelectedObjects
                If _InvoiceHold.RowType = eHoldRowType.Hold Then
                    _ocur.Delete(_InvoiceHold)
                End If
            Next
            _ocur.CommitChanges()

            _InvoiceHold = _ocur.FindObject(Of InvoiceHold)(CriteriaOperator.Parse("RowType='Normal' and InvoiceHeader.Oid=?", _InvoiceHoldPopup.InvoiceHeader.Oid), PersistentCriteriaEvaluationBehavior.InTransaction)
            If _InvoiceHold IsNot Nothing Then
                _ocur.Delete(_InvoiceHold)
            End If
            _ocur.CommitChanges()

            _InvoiceHold_Collection = New XPCollection(Of InvoiceHold)(PersistentCriteriaEvaluationBehavior.InTransaction, _ocur.Session, CriteriaOperator.Parse("InvoiceHeader.Oid=?", _InvoiceHoldPopup.InvoiceHeader.Oid))
            For Each _InvoiceHold In _InvoiceHold_Collection
                _AmountRemainingHUF -= _InvoiceHold.AmountHUF
                _AmountRemainingDEV -= _InvoiceHold.AmountDEV
                _IsNormal = True
            Next

            If _IsNormal Then
                _InvoiceHold = _ocur.CreateObject(Of InvoiceHold)()
                _InvoiceHold.RowType = eHoldRowType.Normal
                _InvoiceHold.InvoiceHeader = _InvoiceHoldPopup.InvoiceHeader
                _InvoiceHold.ShortName = ""
                _InvoiceHold.AmountHUF = _AmountRemainingHUF
                _InvoiceHold.AmountDEV = _AmountRemainingDEV
                _InvoiceHold.DatePayment = _InvoiceHoldPopup.InvoiceHeader.DatePayment

                _ocur.CommitChanges()
            End If


            _ListView.CollectionSource.Reload()
        End If

    End Sub

    Private Sub aBackToRowEdit_InvoiceHold_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aBackToRowEdit_InvoiceHold.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHoldPopup As InvoiceHoldPopup = TryCast(View.SelectedObjects(0), InvoiceHoldPopup)
        Dim _InvoiceHold_Collection As XPCollection(Of InvoiceHold) = Nothing
        Dim _InvoiceHold As InvoiceHold = Nothing

        Dim _DetailView As DetailView = TryCast(View, DetailView)
        Dim _ListPropertyEditor As ListPropertyEditor = _DetailView.FindItem("InvoiceHold")
        Dim _IsNormal = False

        If _ListPropertyEditor IsNot Nothing Then
            Dim _ListView As ListView = TryCast(_ListPropertyEditor.ListView, ListView)
            If _ListView IsNot Nothing Then
                _InvoiceHold = TryCast(_ListView.SelectedObjects(0), InvoiceHold)
                If _InvoiceHold IsNot Nothing Then
                    _InvoiceHoldPopup.row_AmountHUF = _InvoiceHold.AmountHUF
                    _InvoiceHoldPopup.row_AmountDEV = _InvoiceHold.AmountDEV
                    _InvoiceHoldPopup.row_DatePayment = _InvoiceHold.DatePayment
                    _InvoiceHoldPopup.row_ShortName = _InvoiceHold.ShortName
                End If


            End If
        End If
    End Sub
End Class
