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 System.IO
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Utils

Public Class InvoiceFactoring_VC
    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()

        If View.Id = "InvoiceHeader_ListView_Factoring_Sended" Then
            '// Bankba elküldött faktorálandó számlák
            Frame.GetController(Of InvoiceVC).aRecalculateHUF.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC).aViewInvoiceHold.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC).aCreateInvoiceCreditNote.Active.SetItemValue("", False)

        End If
        If View.Id = "InvoiceHeader_ListView_Factoring_Contracted" Then
            '// Elfogadott engedményezett szlák
            Frame.GetController(Of InvoiceVC).aRecalculateHUF.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC).aViewInvoiceHold.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC).aCreateInvoiceCreditNote.Active.SetItemValue("", False)

        End If
        If View.Id = "InvoiceHeader_ListView_Factoring_Not_Send" Then
            '// Előkészítés alatti faktorálandó számlák
            Frame.GetController(Of InvoiceVC).aRecalculateHUF.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC).aViewInvoiceHold.Active.SetItemValue("", False)
            Frame.GetController(Of InvoiceVC).aCreateInvoiceCreditNote.Active.SetItemValue("", False)
        End If
        If View.Id = "InvoiceHeader_ListView" Then
            '// Végleges számlák
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub

    Private Sub aFactoringSet_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aFactoringSet.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        For Each _InvoiceHeader In View.SelectedObjects
            If _InvoiceHeader.IsFactoring = False Then
                _InvoiceHeader.IsFactoring = True
            ElseIf _InvoiceHeader.IsFactoring = True And _InvoiceHeader.Factoring_Send = False Then
                _InvoiceHeader.IsFactoring = False
            End If
        Next
        _ocur.CommitChanges()
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub

    Private Sub aFactoringSendToBank_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aFactoringSendToBank.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _InvoiceFactoringPopup As InvoiceFactoringPopup = _onew.CreateObject(Of InvoiceFactoringPopup)()

        _InvoiceFactoringPopup.Factoring_Send_Date = Now

        Dim _InvoiceFactoringParameter As InvoiceFactoringParameter

        Dim _InvoiceFactoringParameter_Collection As New XPCollection(Of InvoiceFactoringParameter)(_onew.Session)
        If _InvoiceFactoringParameter_Collection.Count = 1 Then
            For Each _InvoiceFactoringParameter In _InvoiceFactoringParameter_Collection
                _InvoiceFactoringPopup.InvoiceFactoringParameter = _InvoiceFactoringParameter
                Exit For
            Next
        End If

        e.View = Application.CreateDetailView(_onew, "InvoiceFactoringPopup_DetailView", True, _InvoiceFactoringPopup)


    End Sub

    Private Sub aFactoringSendToBank_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aFactoringSendToBank.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader
        Dim _InvoiceFactoringPopup As InvoiceFactoringPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), InvoiceFactoringPopup)
        Dim _Factoring_Send_Number As String = ""
        Dim _InvoiceFactoringBank As InvoiceFactoringBank
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)

        For Each _InvoiceHeader In View.SelectedObjects
            If _InvoiceHeader.IsFactoring = True Then
                If _Factoring_Send_Number = "" Then
                    _Factoring_Send_Number = _InvoiceFactoringPopup.InvoiceFactoringParameter.NumberGenerator.GetNewNumber(_InvoiceFactoringPopup.InvoiceFactoringParameter.NumberGenerator)
                End If
                _InvoiceHeader.Factoring_Send = True
                _InvoiceHeader.Factoring_Send_Date = _InvoiceFactoringPopup.Factoring_Send_Date
                _InvoiceHeader.Factoring_Send_Number = _Factoring_Send_Number
                _InvoiceHeader.InvoiceFactoringParameter = _ocur.GetObject(_InvoiceFactoringPopup.InvoiceFactoringParameter)
            End If
        Next

        _InvoiceFactoringBank = _ocur.CreateObject(Of InvoiceFactoringBank)()
        _InvoiceFactoringBank.Factoring_Send_Date = _InvoiceFactoringPopup.Factoring_Send_Date
        _InvoiceFactoringBank.Factoring_Send_Number = _Factoring_Send_Number
        _InvoiceFactoringBank.CustomersFrom = _InvoiceHeader.CustomersFrom
        _InvoiceFactoringBank.Customers = _InvoiceHeader.Customers

        For Each _InvoiceHeader In View.SelectedObjects
            _InvoiceHeader.InvoiceFactoringBank = _InvoiceFactoringBank
        Next

        _ocur.CommitChanges()
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)

        '// Most kell a kérelmet kinyomtatni !!!!

        Dim _ReportData As ReportData = _ocur.GetObject(_InvoiceFactoringPopup.InvoiceFactoringParameter.ReportData_Question)
        If _ReportData IsNot Nothing Then
            Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("Factoring_Send_Number=?", _Factoring_Send_Number), True)
        End If


    End Sub

    Private Sub aFactoringBackFromBank_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aFactoringBackFromBank.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceFactoringBank As InvoiceFactoringBank
        Dim _InvoiceHeader As InvoiceHeader
        Dim _ReadyForDelete As Boolean = False
        Dim _InvoiceFactoringBank_Collection As New XPCollection(Of InvoiceFactoringBank)(_ocur.Session, CriteriaOperator.Parse("1=2"))
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)

        For Each _InvoiceFactoringBank In View.SelectedObjects
            _ReadyForDelete = False
            For Each _InvoiceHeader In _InvoiceFactoringBank.InvoiceHeader
                If _InvoiceHeader.Factoring_Send = True And _InvoiceHeader.InvoiceFactoringRows Is Nothing Then
                    _ReadyForDelete = True
                    _InvoiceHeader.Factoring_Send = False
                    _InvoiceHeader.Factoring_Send_Date = Nothing
                    _InvoiceHeader.Factoring_Send_Number = ""
                    _InvoiceHeader.InvoiceFactoringBank = Nothing
                End If
            Next
            If _ReadyForDelete Then
                _InvoiceFactoringBank_Collection.Add(_InvoiceFactoringBank)
            End If
        Next
        _ocur.Delete(_InvoiceFactoringBank_Collection)
        _ocur.CommitChanges()

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

    Private Sub aFactoringCreate_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aFactoringCreate.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _InvoiceFactoringHeaderPopup As InvoiceFactoringHeaderPopup = _onew.CreateObject(Of InvoiceFactoringHeaderPopup)()

        e.View = Application.CreateDetailView(_onew, "InvoiceFactoringHeaderPopup_DetailView", True, _InvoiceFactoringHeaderPopup)

    End Sub

    Private Sub aFactoringCreate_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aFactoringCreate.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceHeader As InvoiceHeader
        Dim _InvoiceFactoringHeaderPopup As InvoiceFactoringHeaderPopup = TryCast(e.PopupWindow.View.SelectedObjects(0), InvoiceFactoringHeaderPopup)

        Dim _InvoiceFactoringHeader As InvoiceFactoringHeader = _ocur.CreateObject(Of InvoiceFactoringHeader)()
        _InvoiceFactoringHeader.DocumentNumber = _InvoiceFactoringHeaderPopup.DocumentNumber
        _InvoiceFactoringHeader.Description = _InvoiceFactoringHeaderPopup.Description
        _InvoiceFactoringHeader.DateExecution = _InvoiceFactoringHeaderPopup.DateExecution
        _InvoiceFactoringHeader.InterestPercent = _InvoiceFactoringHeaderPopup.InterestPercent

        '// FILEDATA áthozása másik objectspace-ről !!!!
        If _InvoiceFactoringHeaderPopup.ContractFile IsNot Nothing Then
            Dim _MemoryStream As New MemoryStream
            _InvoiceFactoringHeaderPopup.ContractFile.SaveToStream(_MemoryStream)
            _MemoryStream.Seek(0, System.IO.SeekOrigin.Begin)
            Dim _FileData As FileData = _ocur.CreateObject(Of FileData)()
            _FileData.LoadFromStream(_InvoiceFactoringHeaderPopup.ContractFile.FileName, _MemoryStream)
            _InvoiceFactoringHeader.ContractFile = _FileData
        End If
        '// EXCEL TÁBLÁZAT MENTÉSE

        If _InvoiceFactoringHeaderPopup.SpreadFile IsNot Nothing Then
            Dim _MemoryStreamXML As New MemoryStream
            _InvoiceFactoringHeaderPopup.ContractFile.SaveToStream(_MemoryStreamXML)
            _MemoryStreamXML.Seek(0, System.IO.SeekOrigin.Begin)
            Dim _SpreadFile As FileData = _ocur.CreateObject(Of FileData)()
            _SpreadFile.LoadFromStream(_InvoiceFactoringHeaderPopup.SpreadFile.FileName, _MemoryStreamXML)
            _InvoiceFactoringHeader.SpreadFile = _SpreadFile
        End If

        Dim _InvoiceFactoringRows As InvoiceFactoringRows
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)
        For Each _InvoiceHeader In View.SelectedObjects
            _InvoiceFactoringRows = _ocur.CreateObject(Of InvoiceFactoringRows)()
            _InvoiceFactoringRows.InvoiceFactoringHeader = _InvoiceFactoringHeader
            _InvoiceFactoringRows.InvoiceHeader = _InvoiceHeader
            _InvoiceHeader.InvoiceFactoringRows = _InvoiceFactoringRows
        Next
        _ocur.CommitChanges()
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub

    Private Sub aFactoringDestroy_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aFactoringDestroy.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceFactoringHeader As InvoiceFactoringHeader
        Dim _InvoiceFactoringRows As InvoiceFactoringRows

        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", False)

        For Each _InvoiceFactoringHeader In View.SelectedObjects
            For Each _InvoiceFactoringRows In _InvoiceFactoringHeader.InvoiceFactoringRows
                _InvoiceFactoringRows.InvoiceHeader.InvoiceFactoringRows = Nothing
                _InvoiceFactoringRows.InvoiceHeader = Nothing
            Next
            _ocur.Delete(_InvoiceFactoringHeader.InvoiceFactoringRows)
            _ocur.Delete(_InvoiceFactoringHeader)
        Next
        _ocur.CommitChanges()
        Frame.GetController(Of DevExpress.ExpressApp.Validation.PersistenceValidationController)().Active.SetItemValue("", True)
    End Sub


    Private Sub aFactoringSendToBankPrintPreview_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aFactoringSendToBankPrintPreview.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _InvoiceFactoringBank As InvoiceFactoringBank = View.SelectedObjects(0)

        If _InvoiceFactoringBank IsNot Nothing Then

            Dim _InvoiceHeader_Collection As New XPCollection(Of InvoiceHeader)(_ocur.Session, CriteriaOperator.Parse("InvoiceFactoringBank =?", _InvoiceFactoringBank))

            If _InvoiceHeader_Collection.Count > 0 Then
                Dim _ReportData As ReportData = _ocur.GetObject(_InvoiceHeader_Collection(0).InvoiceFactoringParameter.ReportData_Question)
                If _ReportData IsNot Nothing Then
                    Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("Factoring_Send_Number=?", _InvoiceFactoringBank.Factoring_Send_Number), True)
                End If
            Else
                MsgBox("Az összeállított kérelemhez nem tartozik faktorizált számla(k)!", MsgBoxStyle.OkOnly, String.Format(CaptionHelper.GetLocalizedText("Exceptions\SISBusinessExceptions", "MsgBoxCheckItAgain")))
            End If
        End If
    End Sub
End Class
