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.SystemModule
Imports System.Linq
Imports System.Linq.Expressions
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo.DB

Public Class PCICheckVC
    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
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "PCICheck_ListView_RegistryHeader" Then
            AddHandler Frame.GetController(Of RefreshController).RefreshAction.Executed, AddressOf PCICheck_ListView_RegistryHeader_RefreshAction_Executed
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
        If View.Id = "PCICheck_ListView_InvoiceHeader" Then
            AddHandler Frame.GetController(Of RefreshController).RefreshAction.Executed, AddressOf PCICheck_ListView_InvoiceHeader_RefreshAction_Executed
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
        If View.Id = "PCICheck_ListView_GLAccounts" Then
            AddHandler Frame.GetController(Of RefreshController).RefreshAction.Executed, AddressOf PCICheck_ListView_GLAccounts_RefreshAction_Executed
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
        If View.Id = "PCICheck_ListView_GLRows" Then
            AddHandler Frame.GetController(Of RefreshController).RefreshAction.Executed, AddressOf PCICheck_ListView_GLRows_RefreshAction_Executed
            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub

    Private Sub PCICheck_ListView_RegistryHeader_RefreshAction_Executed(sender As Object, e As ActionBaseEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCICheck As PCICheck
        Dim _SelectStatementResultRow As SelectStatementResultRow
        Dim _SelectedData As DevExpress.Xpo.DB.SelectedData = Nothing
        Dim _SQL As String = ""
        Dim _RegistryHeader As RegistryHeader
        Select Case GLOBAL_SQLType
            Case eSQLType.PostgreSQL
                _SQL = " SELECT t1.Oid"

                _SQL += " FROM RegistryHeader t1 LEFT JOIN PCIGroup t2 ON"
                _SQL += " t1.CustomersFrom=t2.CustomersFrom AND "
                _SQL += " t1.Customers=t2.Customers AND "
                _SQL += " t1.F_ConnectInfo=t2.ConnectInfo AND "
                _SQL += " t2.PartnerType=1 LEFT JOIN RegistryType t3 ON"
                _SQL += " t1.RegistryType=t3.Oid LEFT JOIN Organization t4 ON"
                _SQL += " t1.CustomersFrom=t4.oid LEFT JOIN Organization t5 ON"
                _SQL += " t1.Customers = t5.Oid"

                _SQL += " WHERE t2.Oid IS NULL AND t1.GCRecord IS NULL AND t1.IsEditable=0 AND"
                _SQL += " t1.RegistryAcceptState=0 AND t3.RegistryMainType=0"
            
            Case eSQLType.MSSQL, eSQLType.MySQL
                _SQL = " SELECT t1.Oid"

                _SQL += " FROM RegistryHeader t1 LEFT JOIN PCIGroup t2 ON"
                _SQL += " t1.CustomersFrom=t2.CustomersFrom AND "
                _SQL += " t1.Customers=t2.Customers AND "
                _SQL += " t1.F_ConnectInfo=t2.ConnectInfo AND "
                _SQL += " t2.PartnerType=1 LEFT JOIN RegistryType t3 ON"
                _SQL += " t1.RegistryType=t3.Oid LEFT JOIN Organization t4 ON"
                _SQL += " t1.CustomersFrom=t4.oid LEFT JOIN Organization t5 ON"
                _SQL += " t1.Customers = t5.Oid"

                _SQL += " WHERE t2.Oid IS NULL AND t1.GCRecord IS NULL AND t1.IsEditable=0 AND"
                _SQL += " t1.RegistryAcceptState=0 AND t3.RegistryMainType=0 and t1.F_GLAccounts is null and t1.IsStorno=0"
        End Select
        _SelectedData = _ocur.Session.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()
                _RegistryHeader = _ocur.FindObject(Of RegistryHeader)(CriteriaOperator.Parse("Oid=?", _SelectStatementResultRow.Values(0)))
                If _RegistryHeader IsNot Nothing Then
                    _PCICheck = _ocur.CreateObject(Of PCICheck)()
                    _PCICheck.RegistryHeader = _RegistryHeader
                    TryCast(View, ListView).CollectionSource.Add(_PCICheck)
                End If
            Next
            RaiseEvent FinalWork
        End If

        
    End Sub
    Private Sub PCICheck_ListView_InvoiceHeader_RefreshAction_Executed(sender As Object, e As ActionBaseEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCICheck As PCICheck
        Dim _SelectStatementResultRow As SelectStatementResultRow
        Dim _SelectedData As DevExpress.Xpo.DB.SelectedData = Nothing
        Dim _SQL As String = ""
        Dim _InvoiceHeader As InvoiceHeader
        Select Case GLOBAL_SQLType
            Case eSQLType.PostgreSQL
                _SQL = " SELECT t1.Oid"

                _SQL += " FROM InvoiceHeader t1 LEFT JOIN PCIGroup t2 ON"
                _SQL += " t1.CustomersFrom=t2.CustomersFrom AND "
                _SQL += " t1.Customers=t2.Customers AND "
                _SQL += " t1.ConnectInfo=t2.ConnectInfo AND "
                _SQL += " t2.PartnerType=0 LEFT JOIN Organization t4 ON"
                _SQL += " t1.CustomersFrom=t4.oid LEFT JOIN Organization t5 ON"
                _SQL += " t1.Customers = t5.Oid"

                _SQL += " WHERE t2.Oid IS NULL AND t1.GCRecord IS NULL AND t1.IsEditable=0 AND"
                _SQL += " t1.DocumentNumber<>''"

            Case eSQLType.MSSQL, eSQLType.MySQL
                _SQL = " SELECT t1.Oid"

                _SQL += " FROM InvoiceHeader t1 LEFT JOIN PCIGroup t2 ON"
                _SQL += " t1.CustomersFrom=t2.CustomersFrom AND "
                _SQL += " t1.Customers=t2.Customers AND "
                _SQL += " t1.ConnectInfo=t2.ConnectInfo AND "
                _SQL += " t2.PartnerType=0 LEFT JOIN Organization t4 ON"
                _SQL += " t1.CustomersFrom=t4.oid LEFT JOIN Organization t5 ON"
                _SQL += " t1.Customers = t5.Oid"

                _SQL += " WHERE t2.Oid IS NULL AND t1.GCRecord IS NULL AND t1.IsEditable=0 AND"
                _SQL += " t1.DocumentNumber<>'' and t1.GLAccounts is null"
        End Select
        _SelectedData = _ocur.Session.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()
                _InvoiceHeader = _ocur.FindObject(Of InvoiceHeader)(CriteriaOperator.Parse("Oid=?", _SelectStatementResultRow.Values(0)))
                If _InvoiceHeader IsNot Nothing Then
                    _PCICheck = _ocur.CreateObject(Of PCICheck)()
                    _PCICheck.InvoiceHeader = _InvoiceHeader
                    TryCast(View, ListView).CollectionSource.Add(_PCICheck)
                End If
            Next
            RaiseEvent FinalWork
        End If

    End Sub
    Private Sub PCICheck_ListView_GLAccounts_RefreshAction_Executed(sender As Object, e As ActionBaseEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCICheck As PCICheck
        Dim _SelectStatementResultRow As SelectStatementResultRow
        Dim _SelectedData As DevExpress.Xpo.DB.SelectedData = Nothing
        Dim _SQL As String = ""
        Dim _GLAccounts As GLAccounts
        Select Case GLOBAL_SQLType
            Case eSQLType.PostgreSQL
                _SQL = " SELECT t1.Oid"

                _SQL += " FROM GLHeader t0 join GLAccounts t1 on "
                _SQL += " t0.Oid=t1.Oid LEFT JOIN PCIGroup t2 ON"
                _SQL += " t0.CustomersFrom=t2.CustomersFrom AND "
                _SQL += " t1.Customers=t2.Customers AND "
                _SQL += " t1.ConnectInfo=t2.ConnectInfo AND "
                _SQL += " t2.PartnerType=t1.PartnerType LEFT JOIN Organization t4 ON"
                _SQL += " t0.CustomersFrom=t4.oid LEFT JOIN Organization t5 ON"
                _SQL += " t1.Customers = t5.Oid"

                _SQL += " WHERE t2.Oid IS NULL AND t0.GCRecord IS NULL AND t0.IsEditable=0 AND"
                _SQL += " t0.DocumentNumber<>''"

            Case eSQLType.MSSQL, eSQLType.MySQL
                _SQL = " SELECT t1.Oid"

                _SQL += " FROM GLHeader t0 join GLAccounts t1 on "
                _SQL += " t0.Oid=t1.Oid LEFT JOIN PCIGroup t2 ON"
                _SQL += " t0.CustomersFrom=t2.CustomersFrom AND "
                _SQL += " t1.Customers=t2.Customers AND "
                _SQL += " t1.ConnectInfo=t2.ConnectInfo AND "
                _SQL += " t2.PartnerType=t1.PartnerType LEFT JOIN Organization t4 ON"
                _SQL += " t0.CustomersFrom=t4.oid LEFT JOIN Organization t5 ON"
                _SQL += " t1.Customers = t5.Oid"

                _SQL += " WHERE t2.Oid IS NULL AND t0.GCRecord IS NULL AND t0.IsEditable=0 AND"
                _SQL += " t0.DocumentNumber<>''"
        End Select
        _SelectedData = _ocur.Session.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()
                _GLAccounts = _ocur.FindObject(Of GLAccounts)(CriteriaOperator.Parse("Oid=?", _SelectStatementResultRow.Values(0)))
                If _GLAccounts IsNot Nothing Then
                    _PCICheck = _ocur.CreateObject(Of PCICheck)()
                    _PCICheck.GLAccounts = _GLAccounts
                    TryCast(View, ListView).CollectionSource.Add(_PCICheck)
                End If
            Next
            RaiseEvent FinalWork
        End If
    End Sub
    Private Sub PCICheck_ListView_GLRows_RefreshAction_Executed(sender As Object, e As ActionBaseEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCICheck As PCICheck
        Dim _SelectStatementResultRow As SelectStatementResultRow
        Dim _SelectedData As DevExpress.Xpo.DB.SelectedData = Nothing
        Dim _SQL As String = ""
        Dim _GLRows As GLRows
        Select Case GLOBAL_SQLType
            Case eSQLType.PostgreSQL
                _SQL = " SELECT t1.Oid"

                _SQL += " FROM GLHeader t0 join GLRows t1 on "
                _SQL += " t0.Oid=t1.GLHeader LEFT JOIN PCIDetail t2 ON"
                _SQL += " t0.CustomersFrom=t2.CustomersFrom AND "
                _SQL += " t1.Customer=t2.Customers AND "
                _SQL += " t1.ConnectInfo=t2.ConnectInfo AND "
                _SQL += " t2.PartnerType=t1.PartnerType "

                _SQL += " WHERE t2.Oid IS NULL AND t0.GCRecord IS NULL AND t0.IsEditable=0 AND"
                _SQL += " t0.DocumentNumber<>'' and t1.PartnerType in (0,1) and t1.Customer is not null and t1.GCRecord is null"
                _SQL += " and t0.GLDocumentType in (0,2,3,4,5)"

            Case eSQLType.MSSQL, eSQLType.MySQL
                _SQL = " SELECT t1.Oid"

                _SQL += " FROM GLHeader t0 join GLRows t1 on "
                _SQL += " t0.Oid=t1.GLHeader LEFT JOIN PCIDetail t2 ON"
                _SQL += " t0.CustomersFrom=t2.CustomersFrom AND "
                _SQL += " t1.Customer=t2.Customers AND "
                _SQL += " t1.ConnectInfo=t2.ConnectInfo AND "
                _SQL += " t2.PartnerType=t1.PartnerType "

                _SQL += " WHERE t2.Oid IS NULL AND t0.GCRecord IS NULL AND t0.IsEditable=0 AND"
                _SQL += " t0.DocumentNumber<>'' and t1.PartnerType in (0,1) and t1.Customer is not null and t1.GCRecord is null"
                _SQL += " and t0.GLDocumentType in (0,2,3,4,5)"
        End Select
        _SelectedData = _ocur.Session.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()
                _GLRows = _ocur.FindObject(Of GLRows)(CriteriaOperator.Parse("Oid=?", _SelectStatementResultRow.Values(0)))
                If _GLRows IsNot Nothing Then
                    _PCICheck = _ocur.CreateObject(Of PCICheck)()
                    _PCICheck.GLRows = _GLRows
                    TryCast(View, ListView).CollectionSource.Add(_PCICheck)
                End If
            Next
            RaiseEvent FinalWork
        End If
    End Sub

    Private Sub aPCICheck_PCICheck_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aPCICheck_PCICheck.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)

        Dim _PCICheck As PCICheck

        RaiseEvent InitWork(1, 1, View.SelectedObjects.Count)
        For Each _PCICheck In View.SelectedObjects
            RaiseEvent DoWork()
            If _PCICheck.GLAccounts IsNot Nothing Then
                GLFunctions.ReconfigurePCI(_uow, _PCICheck.GLAccounts.CustomersFrom, _PCICheck.GLAccounts.Customers, _PCICheck.GLAccounts.PartnerType, _PCICheck.GLAccounts.ConnectInfo)
            ElseIf _PCICheck.InvoiceHeader IsNot Nothing Then
                GLFunctions.ReconfigurePCI(_uow, _PCICheck.InvoiceHeader.CustomersFrom, _PCICheck.InvoiceHeader.Customers, 0, _PCICheck.InvoiceHeader.ConnectInfo)
            ElseIf _PCICheck.RegistryHeader IsNot Nothing Then
                GLFunctions.ReconfigurePCI(_uow, _PCICheck.RegistryHeader.CustomersFrom, _PCICheck.RegistryHeader.Customers, 1, _PCICheck.RegistryHeader.F_ConnectInfo)
            ElseIf _PCICheck.GLRows IsNot Nothing Then
                GLFunctions.ReconfigurePCI(_uow, _PCICheck.GLRows.GLHeader.CustomersFrom, _PCICheck.GLRows.Customer, _PCICheck.GLRows.PartnerType, _PCICheck.GLRows.ConnectInfo)
            End If
        Next
        RaiseEvent FinalWork
    End Sub
    Private Sub aViewPCI_PCICheck_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aViewPCI_PCICheck.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _PCICheck As PCICheck = TryCast(View.SelectedObjects(0), PCICheck)

        If _PCICheck.GLAccounts IsNot Nothing Then
            ViewPDCI.ViewPDCIDetail(_onew, Application, e, _PCICheck.GLAccounts.CustomersFrom, _
                          _PCICheck.GLAccounts.Customers, _PCICheck.GLAccounts.PartnerType, _
                          _PCICheck.GLAccounts.ConnectInfo)
        ElseIf _PCICheck.InvoiceHeader IsNot Nothing Then
            ViewPDCI.ViewPDCIDetail(_onew, Application, e, _PCICheck.InvoiceHeader.CustomersFrom, _
                          _PCICheck.InvoiceHeader.Customers, 0, _
                          _PCICheck.InvoiceHeader.ConnectInfo)
        ElseIf _PCICheck.RegistryHeader IsNot Nothing Then
            ViewPDCI.ViewPDCIDetail(_onew, Application, e, _PCICheck.RegistryHeader.CustomersFrom, _
                          _PCICheck.RegistryHeader.Customers, 1, _
                          _PCICheck.RegistryHeader.F_ConnectInfo)
        ElseIf _PCICheck.GLRows IsNot Nothing Then
            ViewPDCI.ViewPDCIDetail(_onew, Application, e, _PCICheck.GLRows.GLHeader.CustomersFrom, _
                          _PCICheck.GLRows.Customer, _PCICheck.GLRows.PartnerType, _
                          _PCICheck.GLRows.ConnectInfo)
        End If


    End Sub


End Class
