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

Public Class PCIGroupAgingVC
    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

    Private Sub aGeneratePCIGroupAging_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGeneratePCIGroupAging.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _PCIGroupAging As PCIGroupAging

        Dim _SQL As String = ""
        Select Case GLOBAL_SQLType
            Case eSQLType.MySQL
                _SQL = "delete from PCIGroupAging"
            Case eSQLType.PostgreSQL
                _SQL = "delete from ""PCIGroupAging"""
            Case eSQLType.MSSQL
                _SQL = "delete from PCIGroupAging"
        End Select
        _uow.ExecuteNonQuery(_SQL)
        _uow.CommitChanges()

        Dim _PCIGroup_Collection As New XPCollection(Of PCIGroup)(_uow, CriteriaOperator.Parse("isnull(InvoiceHeader)=False or isnull(RegistryHeader)=False"))
        Dim _PCIGroup As PCIGroup


        RaiseEvent InitWork(1, 1, _PCIGroup_Collection.Count)
        For Each _PCIGroup In _PCIGroup_Collection
            RaiseEvent DoWork()
            _PCIGroupAging = New PCIGroupAging(_uow)
            _PCIGroupAging.PCIGroup = _PCIGroup
            _PCIGroupAging.CustomersFrom = _PCIGroup.CustomersFrom
            _PCIGroupAging.Customers = _PCIGroup.Customers
            _PCIGroupAging.PartnerType = _PCIGroup.PartnerType

            _PCIGroupAging.DateExecution = _PCIGroup.DateExecution_Account
            _PCIGroupAging.DatePayment = _PCIGroup.DatePayment_Account
            _PCIGroupAging.DateCreated = _PCIGroup.DateCreated_Account

            _PCIGroupAging.ConnectInfo = _PCIGroup.ConnectInfo
            _PCIGroupAging.CurrencyName = _PCIGroup.CurrencyName_Account

            _PCIGroupAging.AmountHUF_Account = _PCIGroup.AmountHUF_Account
            _PCIGroupAging.AmountHUF_Account_Hold = 0
            _PCIGroupAging.AmountHUF_Account_Normal = 0

            _PCIGroupAging.BallanceHUF = _PCIGroup.BallanceHUF

            If _PCIGroupAging.DatePayment < Now Then
                _PCIGroupAging.IsExpired = True
            Else
                _PCIGroupAging.IsExpired = False
            End If

            If _PCIGroup.InvoiceHeader IsNot Nothing Then
                Dim _InvoiceHold_Collection As New XPCollection(Of InvoiceHold)(_uow, CriteriaOperator.Parse("InvoiceHeader=?", _PCIGroup.InvoiceHeader))
                Dim _InvoiceHold As InvoiceHold
                Dim _Exists As Boolean = False
                For Each _InvoiceHold In _InvoiceHold_Collection
                    _Exists = True
                    If _InvoiceHold.RowType = eHoldRowType.Hold Then
                        _PCIGroupAging.AmountHUF_Account_Hold += _InvoiceHold.AmountHUF
                    End If
                    If _InvoiceHold.RowType = eHoldRowType.Normal Then
                        _PCIGroupAging.AmountHUF_Account_Normal += _InvoiceHold.AmountHUF
                    End If
                Next
                If _Exists Then
                    For Each _InvoiceHold In _InvoiceHold_Collection
                        Select Case DateDiff(DateInterval.Day, Now, _InvoiceHold.DatePayment)
                            Case 1 To 30
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F02_30HUF += _InvoiceHold.BallanceHUF
                                End If
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F02_30DEV += _InvoiceHold.BallanceDEV
                                End If
                            Case 31 To 90
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F03_90HUF += _InvoiceHold.BallanceHUF
                                End If
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F03_90DEV += _InvoiceHold.BallanceDEV
                                End If
                            Case 91 To 365
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F04_1YEARHUF += _InvoiceHold.BallanceHUF
                                End If
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F04_1YEARDEV += _InvoiceHold.BallanceDEV
                                End If
                            Case 366 To 730
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F05_2YEARHUF += _InvoiceHold.BallanceHUF
                                End If
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F05_2YEARDEV += _InvoiceHold.BallanceDEV
                                End If
                            Case 731 To 1095
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F06_3YEARHUF += _InvoiceHold.BallanceHUF
                                End If
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F06_3YEARDEV += _InvoiceHold.BallanceDEV
                                End If
                            Case 1096 To 2190
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F07_6YEARHUF += _InvoiceHold.BallanceHUF
                                End If
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F07_6YEARDEV += _InvoiceHold.BallanceDEV
                                End If
                            Case Is > 2190
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F08_6PLUSYEARHUF += _InvoiceHold.BallanceHUF
                                End If
                                If _InvoiceHold.BallanceHUF > 0 Then
                                    _PCIGroupAging.F08_6PLUSYEARDEV += _InvoiceHold.BallanceDEV
                                End If
                        End Select
                    Next
                End If
                If _Exists = False Then
                    Select Case DateDiff(DateInterval.Day, Now, _PCIGroupAging.DatePayment)
                        Case 1 To 30
                            _PCIGroupAging.F02_30DEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F02_30HUF = _PCIGroupAging.BallanceHUF
                        Case 31 To 90
                            _PCIGroupAging.F03_90DEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F03_90HUF = _PCIGroupAging.BallanceHUF
                        Case 91 To 365
                            _PCIGroupAging.F04_1YEARDEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F04_1YEARHUF = _PCIGroupAging.BallanceHUF
                        Case 366 To 730
                            _PCIGroupAging.F05_2YEARDEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F05_2YEARHUF = _PCIGroupAging.BallanceHUF
                        Case 731 To 1095
                            _PCIGroupAging.F06_3YEARDEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F06_3YEARDEV = _PCIGroupAging.BallanceHUF
                        Case 1096 To 2190
                            _PCIGroupAging.F07_6YEARDEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F07_6YEARHUF = _PCIGroupAging.BallanceHUF
                        Case Is > 2190
                            _PCIGroupAging.F08_6PLUSYEARDEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F08_6PLUSYEARHUF = _PCIGroupAging.BallanceHUF
                        Case Is < 0
                            _PCIGroupAging.F01_EXPIREDDEV = _PCIGroupAging.BallanceDEV
                            _PCIGroupAging.F01_EXPIREDHUF = _PCIGroupAging.BallanceHUF
                    End Select
                End If
            ElseIf _PCIGroup.RegistryHeader IsNot Nothing Then
                Dim _RegistryBankTransfer_Collection As New XPCollection(Of RegistryBankTransfer)(_uow, CriteriaOperator.Parse("RegistryHeader=?", _PCIGroup.RegistryHeader))
                Dim _RegistryBankTransfer As RegistryBankTransfer
                Dim _Exists As Boolean = False
                For Each _RegistryBankTransfer In _RegistryBankTransfer_Collection
                    _Exists = True
                    If _RegistryBankTransfer.RowType = eHoldRowType.Hold Then
                        _PCIGroupAging.AmountHUF_Account_Hold += _RegistryBankTransfer.AmountHUF
                    End If
                    If _RegistryBankTransfer.RowType = eHoldRowType.Normal Then
                        _PCIGroupAging.AmountHUF_Account_Normal += _RegistryBankTransfer.AmountHUF
                    End If
                Next
                If _Exists Then
                    For Each _RegistryBankTransfer In _RegistryBankTransfer_Collection
                        Select Case DateDiff(DateInterval.Day, Now, _RegistryBankTransfer.DatePayment)
                            Case 1 To 30
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F02_30HUF += _RegistryBankTransfer.BallanceHUF
                                End If
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F02_30DEV += _RegistryBankTransfer.BallanceDEV
                                End If
                            Case 31 To 90
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F03_90HUF += _RegistryBankTransfer.BallanceHUF
                                End If
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F03_90DEV += _RegistryBankTransfer.BallanceDEV
                                End If
                            Case 91 To 365
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F04_1YEARHUF += _RegistryBankTransfer.BallanceHUF
                                End If
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F04_1YEARDEV += _RegistryBankTransfer.BallanceDEV
                                End If
                            Case 366 To 730
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F05_2YEARHUF += _RegistryBankTransfer.BallanceHUF
                                End If
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F05_2YEARDEV += _RegistryBankTransfer.BallanceDEV
                                End If
                            Case 731 To 1095
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F06_3YEARHUF += _RegistryBankTransfer.BallanceHUF
                                End If
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F06_3YEARDEV += _RegistryBankTransfer.BallanceDEV
                                End If
                            Case 1096 To 2190
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F07_6YEARHUF += _RegistryBankTransfer.BallanceHUF
                                End If
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F07_6YEARDEV += _RegistryBankTransfer.BallanceDEV
                                End If
                            Case Is > 2190
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F08_6PLUSYEARHUF += _RegistryBankTransfer.BallanceHUF
                                End If
                                If _RegistryBankTransfer.BallanceHUF > 0 Then
                                    _PCIGroupAging.F08_6PLUSYEARDEV += _RegistryBankTransfer.BallanceDEV
                                End If
                        End Select
                    Next
                End If
                If _Exists = False Then
                    Select Case DateDiff(DateInterval.Day, Now, _PCIGroupAging.DatePayment)
                        Case 1 To 30
                            _PCIGroupAging.F02_30DEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F02_30HUF = _PCIGroupAging.BallanceHUF
                        Case 31 To 90
                            _PCIGroupAging.F03_90DEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F03_90HUF = _PCIGroupAging.BallanceHUF
                        Case 91 To 365
                            _PCIGroupAging.F04_1YEARDEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F04_1YEARHUF = _PCIGroupAging.BallanceHUF
                        Case 366 To 730
                            _PCIGroupAging.F05_2YEARDEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F05_2YEARHUF = _PCIGroupAging.BallanceHUF
                        Case 731 To 1095
                            _PCIGroupAging.F06_3YEARDEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F06_3YEARDEV = _PCIGroupAging.BallanceHUF
                        Case 1096 To 2190
                            _PCIGroupAging.F07_6YEARDEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F07_6YEARHUF = _PCIGroupAging.BallanceHUF
                        Case Is > 2190
                            _PCIGroupAging.F08_6PLUSYEARDEV = _PCIGroup.BallanceDEV
                            _PCIGroupAging.F08_6PLUSYEARHUF = _PCIGroupAging.BallanceHUF
                        Case Is < 0
                            _PCIGroupAging.F01_EXPIREDDEV = _PCIGroupAging.BallanceDEV
                            _PCIGroupAging.F01_EXPIREDHUF = _PCIGroupAging.BallanceHUF
                    End Select
                End If

            Else
                Select Case DateDiff(DateInterval.Day, Now, _PCIGroupAging.DatePayment)
                    Case 1 To 30
                        _PCIGroupAging.F02_30DEV = _PCIGroup.BallanceDEV
                        _PCIGroupAging.F02_30HUF = _PCIGroupAging.BallanceHUF
                    Case 31 To 90
                        _PCIGroupAging.F03_90DEV = _PCIGroup.BallanceDEV
                        _PCIGroupAging.F03_90HUF = _PCIGroupAging.BallanceHUF
                    Case 91 To 365
                        _PCIGroupAging.F04_1YEARDEV = _PCIGroup.BallanceDEV
                        _PCIGroupAging.F04_1YEARHUF = _PCIGroupAging.BallanceHUF
                    Case 366 To 730
                        _PCIGroupAging.F05_2YEARDEV = _PCIGroup.BallanceDEV
                        _PCIGroupAging.F05_2YEARHUF = _PCIGroupAging.BallanceHUF
                    Case 731 To 1095
                        _PCIGroupAging.F06_3YEARDEV = _PCIGroup.BallanceDEV
                        _PCIGroupAging.F06_3YEARDEV = _PCIGroupAging.BallanceHUF
                    Case 1096 To 2190
                        _PCIGroupAging.F07_6YEARDEV = _PCIGroup.BallanceDEV
                        _PCIGroupAging.F07_6YEARHUF = _PCIGroupAging.BallanceHUF
                    Case Is > 2190
                        _PCIGroupAging.F08_6PLUSYEARDEV = _PCIGroup.BallanceDEV
                        _PCIGroupAging.F08_6PLUSYEARHUF = _PCIGroupAging.BallanceHUF
                    Case Is < 0
                        _PCIGroupAging.F01_EXPIREDDEV = _PCIGroupAging.BallanceDEV
                        _PCIGroupAging.F01_EXPIREDHUF = _PCIGroupAging.BallanceHUF
                End Select
            End If


            _uow.CommitChanges()
        Next
        RaiseEvent FinalWork
        _uow.CommitChanges()

        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()

    End Sub

    Private Sub aViewAttachmentsPCIGroupAging_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aViewAttachmentsPCIGroupAging.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCIGroupAging As PCIGroupAging = TryCast(View.SelectedObjects(0), PCIGroupAging)

        If _PCIGroupAging IsNot Nothing Then
            ViewAttachments.ViewAttachments(_ocur, Frame, _PCIGroupAging.CustomersFrom, _
                                             _PCIGroupAging.Customers, _PCIGroupAging.ConnectInfo, _
                                             "")
        End If
    End Sub

    Private Sub aViewRegistryPCIGroupAging_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aViewRegistryPCIGroupAging.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCIGroupAging As PCIGroupAging = TryCast(View.SelectedObjects(0), PCIGroupAging)

        If _PCIGroupAging IsNot Nothing Then
            ViewRegistry.ViewRegistry(_ocur, Application, e, _PCIGroupAging.CustomersFrom, _
                                      _PCIGroupAging.Customers, _PCIGroupAging.ConnectInfo)
        End If
    End Sub
End Class
