Imports Microsoft.VisualBasic
Imports System
Imports System.Linq
Imports System.Text
Imports DevExpress.ExpressApp
Imports DevExpress.Data.Filtering
Imports System.Collections.Generic
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.Layout
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.ExpressApp.Templates
Imports DevExpress.Persistent.Validation
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Model.NodeGenerators
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB


' For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppViewControllertopic.
Partial Public Class PCICheckerVC
    Inherits 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()
        InitializeComponent()
        RegisterActions(components)
        ' Target required Views (via the TargetXXX properties) and create their Actions.
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        ' Perform various tasks depending on the target View.
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        ' Access and customize the target View control.
    End Sub
    Protected Overrides Sub OnDeactivated()
        ' Unsubscribe from previously subscribed events and release other references and resources.
        MyBase.OnDeactivated()
    End Sub

    Private Sub aGeneratePCIChecker_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGeneratePCIChecker.Execute

        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _PCIGLChecker As PCIGLChecker = _uow.GetObjectByKey(Of PCIGLChecker)(TryCast(View.SelectedObjects(0), PCIGLChecker).Oid)
        Dim _PCIGLCheckerRows As PCIGLCheckerRows
        Dim _SQL As String = ""
        Dim _IN As String = ""
        Dim _SelectStatementResultRow As SelectStatementResultRow
        Dim _SelectedData As DevExpress.Xpo.DB.SelectedData = Nothing
        Dim _AccountNumber As String = ""
        '// először a nem nulla egyenlegű Főkönyvből

        _uow.Delete(_PCIGLChecker.PCIGLCheckerRows)
        _uow.CommitChanges()

        Dim _CustomersGLParametersYear_Collection As New XPCollection(Of CustomersGLParametersYear)(_uow, CriteriaOperator.Parse("IsNull(GCRecord)=True and IsNull(CustomersGLParameters.GCRecord)=True"))
        Dim _CustomersGLParametersYear As CustomersGLParametersYear

        For Each _CustomersGLParametersYear In _CustomersGLParametersYear_Collection
            If _IN = "" Then
                Select Case _PCIGLChecker.PartnerType
                    Case ePartnerType.eReceivables
                        _IN = "'" & _CustomersGLParametersYear.ChartOfAccountsOut.AccountNumber & "'"
                    Case ePartnerType.ePayabels
                        _IN = "'" & _CustomersGLParametersYear.ChartOfAccountsIn.AccountNumber & "'"
                End Select
            Else
                Select Case _PCIGLChecker.PartnerType
                    Case ePartnerType.eReceivables
                        _IN += ",'" & _CustomersGLParametersYear.ChartOfAccountsOut.AccountNumber & "'"
                    Case ePartnerType.ePayabels
                        _IN += ",'" & _CustomersGLParametersYear.ChartOfAccountsIn.AccountNumber & "'"
                End Select
            End If
        Next
        _IN = " IN (" & _IN & ")"

        Select Case GLOBAL_SQLType
            Case eSQLType.MySQL, eSQLType.MSSQL
                _SQL = " SELECT MAX(t4.Oid) AS Customers,t2.ConnectInfo,"

                If _PCIGLChecker.PartnerType = ePartnerType.eReceivables Then
                    _SQL += " SUM((CASE t1.Side WHEN 'T' THEN t1.AmountHUF ELSE 0 END)-(CASE t1.Side WHEN 'K' THEN t1.AmountHUF ELSE 0 END)) AS BallanceHUF"
                Else
                    _SQL += " SUM((CASE t1.Side WHEN 'K' THEN t1.AmountHUF ELSE 0 END)-(CASE t1.Side WHEN 'T' THEN t1.AmountHUF ELSE 0 END)) AS BallanceHUF"
                End If


                _SQL += " FROM GLDetails t1 JOIN GLRows t2 ON"
                _SQL += " t1.GLRows=t2.Oid LEFT JOIN Customers t3 ON"
                _SQL += " t2.Customer=t3.Oid LEFT JOIN Organization t4 ON"
                _SQL += " t3.Oid=t4.Oid LEFT JOIN GLHeader t5 ON"
                _SQL += " t2.GLHeader=t5.Oid LEFT JOIN ChartOfAccounts t6 ON"
                _SQL += " t1.ChartOfAccounts = t6.Oid"

                _SQL += " WHERE t2.PartnerType=" & _PCIGLChecker.PartnerType
                _SQL += " AND t5.IsEditable=0 AND t6.AccountNumber " & _IN
                _SQL += " and t5.GCRecord is null"
                _SQL += " and t5.CustomersFrom='" & _PCIGLChecker.CustomersFrom.Oid.ToString & "'"
                _SQL += " AND t5.DateExecution<'" & _PCIGLChecker.DateClose.AddDays(1).Year.ToString & "-" & _PCIGLChecker.DateClose.AddDays(1).Month.ToString & "-" & _PCIGLChecker.DateClose.AddDays(1).Day.ToString & "'"

                _SQL += " GROUP BY t4.FullName,t2.ConnectInfo"
                _SQL += " HAVING ROUND(SUM((CASE t1.Side WHEN 'T' THEN t1.AmountHUF ELSE 0 END)-(CASE t1.Side WHEN 'K' THEN t1.AmountHUF ELSE 0 END)),0)<>0 "
            Case eSQLType.PostgreSQL

        End Select

        _SelectedData = _uow.ExecuteQuery(_SQL)
        If _SelectedData.ResultSet.Length > 0 Then
            RaiseEvent InitWork(1, 1, _SelectedData.ResultSet(0).Rows.Count)
            For Each _SelectStatementResultRow In _SelectedData.ResultSet(0).Rows
                RaiseEvent DoWork()
                _PCIGLCheckerRows = _uow.FindObject(Of PCIGLCheckerRows)(CriteriaOperator.Parse("Customers.Oid=? and PartnerType=? and ConnectInfo=? and PCIGLChecker.Oid=?", _SelectStatementResultRow.Values(0), _PCIGLChecker.PartnerType, _SelectStatementResultRow.Values(1), _PCIGLChecker.Oid))
                If _PCIGLCheckerRows IsNot Nothing Then
                    _PCIGLCheckerRows.BallanceHUF_GL = _SelectStatementResultRow.Values(2)
                Else
                    _PCIGLCheckerRows = New PCIGLCheckerRows(_uow)
                    _PCIGLCheckerRows.PCIGLChecker = _PCIGLChecker
                    _PCIGLCheckerRows.PartnerType = _PCIGLChecker.PartnerType
                    _PCIGLCheckerRows.Customers = _uow.FindObject(Of Customers)(CriteriaOperator.Parse("Oid=?", _SelectStatementResultRow.Values(0).ToString))
                    _PCIGLCheckerRows.ConnectInfo = _SelectStatementResultRow.Values(1)
                    _PCIGLCheckerRows.BallanceHUF_GL = _SelectStatementResultRow.Values(2)
                End If
                _uow.CommitChanges()
            Next
            RaiseEvent FinalWork
        End If

        '// másodszor a nem nulla egyenlegű a Folyószámlából
        Select Case GLOBAL_SQLType
            Case eSQLType.MySQL
                _SQL = " SELECT t2.Oid,t1.Connectinfo,SUM(t1.BallanceHUF)"

                _SQL += " FROM PCIDetail t1 JOIN Organization t2 ON"
                _SQL += " t1.Customers = t2.Oid"

                _SQL += " WHERE t1.PartnerType=" & _PCIGLChecker.PartnerType
                _SQL += " AND t1.DateExecution<'" & _PCIGLChecker.DateClose.AddDays(1).Year.ToString & "-" & _PCIGLChecker.DateClose.AddDays(1).Month.ToString & "-" & _PCIGLChecker.DateClose.AddDays(1).Day.ToString & "'"

                _SQL += " GROUP BY t2.Oid,t1.ConnectInfo"
                _SQL += " HAVING ROUND(SUM(t1.BallanceHUF),0) <> 0"
            Case eSQLType.PostgreSQL

        End Select

        _SelectedData = _uow.ExecuteQuery(_SQL)
        If _SelectedData.ResultSet.Length > 0 Then
            RaiseEvent InitWork(1, 1, _SelectedData.ResultSet(0).Rows.Count)
            For Each _SelectStatementResultRow In _SelectedData.ResultSet(0).Rows
                RaiseEvent DoWork()
                _PCIGLCheckerRows = _uow.FindObject(Of PCIGLCheckerRows)(CriteriaOperator.Parse("Customers.Oid=? and PartnerType=? and ConnectInfo=? and PCIGLChecker.Oid=?", _SelectStatementResultRow.Values(0), _PCIGLChecker.PartnerType, _SelectStatementResultRow.Values(1), _PCIGLChecker.Oid))
                If _PCIGLCheckerRows IsNot Nothing Then
                    _PCIGLCheckerRows.BallanceHUF_PCI = _SelectStatementResultRow.Values(2)
                Else
                    _PCIGLCheckerRows = New PCIGLCheckerRows(_uow)
                    _PCIGLCheckerRows.PCIGLChecker = _PCIGLChecker
                    _PCIGLCheckerRows.PartnerType = _PCIGLChecker.PartnerType
                    _PCIGLCheckerRows.Customers = _uow.FindObject(Of Customers)(CriteriaOperator.Parse("Oid=?", _SelectStatementResultRow.Values(0).ToString))
                    _PCIGLCheckerRows.ConnectInfo = _SelectStatementResultRow.Values(1)
                    _PCIGLCheckerRows.BallanceHUF_PCI = _SelectStatementResultRow.Values(2)
                End If
                _uow.CommitChanges()
            Next
            RaiseEvent FinalWork
        End If

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

    End Sub

    Private Sub aPCICheck_PCIChecker_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aPCICheck_PCIChecker.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _PCIGLCheckerRows As PCIGLCheckerRows = TryCast(View.SelectedObjects(0), PCIGLCheckerRows)


        For Each _PCIGLCheckerRows In View.SelectedObjects
            If _PCIGLCheckerRows IsNot Nothing Then
                GLFunctions.ReconfigurePCI(_uow, _PCIGLCheckerRows.PCIGLChecker.CustomersFrom, _PCIGLCheckerRows.Customers, _PCIGLCheckerRows.PartnerType, _PCIGLCheckerRows.ConnectInfo)
            End If
        Next
        _uow.CommitChanges()
    End Sub

    Private Sub aViewPCIDGroup_GLDetails_PCIChecker_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aViewPCIDGroup_GLDetails_PCIChecker.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCIGLCheckerRows As PCIGLCheckerRows = TryCast(View.SelectedObjects(0), PCIGLCheckerRows)

        ViewPDCI.ViewGLDetails(_onew, Application, e, _PCIGLCheckerRows.PCIGLChecker.CustomersFrom, _
                          _PCIGLCheckerRows.Customers, _PCIGLCheckerRows.PartnerType, _
                          _PCIGLCheckerRows.ConnectInfo)
    End Sub

    Private Sub aViewPCIDGroup_PCIChecker_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aViewPCIDGroup_PCIChecker.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCIGLCheckerRows As PCIGLCheckerRows = TryCast(View.SelectedObjects(0), PCIGLCheckerRows)

        ViewPDCI.ViewPDCIDetail(_onew, Application, e, _PCIGLCheckerRows.PCIGLChecker.CustomersFrom, _
                          _PCIGLCheckerRows.Customers, _PCIGLCheckerRows.PartnerType, _
                          _PCIGLCheckerRows.ConnectInfo)
    End Sub
End Class
