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

Public Class ProjectPlanVC
    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 aProjectPlanViewContract_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProjectPlanViewContract.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ProjectPlanRows As ProjectPlanRows = _onew.GetObject(TryCast(View.SelectedObjects(0), ProjectPlanRows))


        Dim _ContractRows_Controlling As New CollectionSource(_onew, GetType(ContractRows))
        Dim _ContractRows As ContractRows

        '_ContractRows.GLHeader.DateExecution.Date

        _ContractRows_Controlling.BeginUpdateCriteria()
        '_ContractRows_Controlling.Criteria("Plan1") = CriteriaOperator.Parse("(DebitChartOfAccounts.AccountNumber=? or CreditChartOfAccounts.AccountNumber=?)", _ProjectPlanRows.AccountNumber, _ProjectPlanRows.AccountNumber)
        If _ProjectPlanRows.Controlling Is Nothing Then
            _ContractRows_Controlling.Criteria("Controlling") = CriteriaOperator.Parse("isnull(Controlling)=True")
        Else
            _ContractRows_Controlling.Criteria("Controlling") = CriteriaOperator.Parse("Controlling=?", _ProjectPlanRows.Controlling)
        End If
        If _ProjectPlanRows.CostHolder Is Nothing Then
            _ContractRows_Controlling.Criteria("CostHolder") = CriteriaOperator.Parse("isnull(CostHolder)=True")
        Else
            _ContractRows_Controlling.Criteria("CostHolder") = CriteriaOperator.Parse("CostHolder=?", _ProjectPlanRows.CostHolder)
        End If
        If _ProjectPlanRows.CostPlace Is Nothing Then
            _ContractRows_Controlling.Criteria("CostPlace") = CriteriaOperator.Parse("isnull(CostPlace)=True")
        Else
            _ContractRows_Controlling.Criteria("CostPlace") = CriteriaOperator.Parse("CostPlace=?", _ProjectPlanRows.CostPlace)
        End If
        If _ProjectPlanRows.JobNumberObjects Is Nothing Then
            _ContractRows_Controlling.Criteria("JobNumberObjects") = CriteriaOperator.Parse("isnull(JobNumberObjects)=True")
        Else
            _ContractRows_Controlling.Criteria("JobNumberObjects") = CriteriaOperator.Parse("JobNumberObjects=?", _ProjectPlanRows.JobNumberObjects)
        End If
        If _ProjectPlanRows.UniqueObjects Is Nothing Then
            _ContractRows_Controlling.Criteria("UniqueObjects") = CriteriaOperator.Parse("isnull(UniqueObjects)=True")
        Else
            _ContractRows_Controlling.Criteria("UniqueObjects") = CriteriaOperator.Parse("UniqueObjects=?", _ProjectPlanRows.UniqueObjects)
        End If

        _ContractRows_Controlling.Criteria("DateExecution") = CriteriaOperator.Parse("GetYear(DateExecution)=? and GetMonth(DateExecution)=?  and Contracts.CustomersFrom=?", _
                                                                                     _ProjectPlanRows.DateYear, _ProjectPlanRows.DateMonth, _ProjectPlanRows.ProjectPlan.CustomersFrom)
        _ContractRows_Controlling.EndUpdateCriteria()


        e.ShowViewParameters.CreatedView = Application.CreateListView("ContractRows_ListView_ProjectPlan", _ContractRows_Controlling, True)
    End Sub

    Private Sub aProjectPlanGenerateFact_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProjectPlanGenerateFact.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        Dim _ProjectPlan As ProjectPlan = _uow.GetObjectByKey(Of ProjectPlan)(TryCast(View.SelectedObjects(0), ProjectPlan).Oid)
        Dim _ProjectPlanRows As ProjectPlanRows
        Dim _SQL As String

        Select Case GLOBAL_SQLType
            Case eSQLType.PostgreSQL
                _SQL = "Update ""ProjectPlanRows"" set "
                _SQL += " ""DebitAmountFactHUF""=0,""CreditAmountFactHUF""=0, "
                _SQL += " ""DebitAmountContractHUF""=0,""CreditAmountContractHUF""=0 "
                _SQL += " Where ""ProjectPlan""='" & _ProjectPlan.Oid.ToString & "'"
            Case Else
                _SQL = "Update ProjectPlanRows set "
                _SQL += " DebitAmountFactHUF=0,CreditAmountFactHUF=0, "
                _SQL += " DebitAmountContractHUF=0,CreditAmountContractHUF=0 "
                _SQL += " Where ProjectPlan='" & _ProjectPlan.Oid.ToString & "'"
        End Select
        _onew.Session.ExecuteNonQuery(_SQL)

        _onew.CommitChanges()

        Dim _ChartOfAccounts As ChartOfAccounts
        Dim _AccountNumber As String = ""
        Dim _Customers As Customers
        Dim _Controlling As Controlling
        Dim _VAT As VAT
        Dim _GLRows_xpquery = New XPQuery(Of GLRows)(_uow)
        Dim query As System.Linq.IQueryable(Of Object)

        Dim _criteria As String = ""
        '// Tartozik láb alapján
        query = From _glrows In _GLRows_xpquery
                       Where _glrows.GLHeader.IsEditable = False And _
                             _glrows.GLHeader.DateExecution.Date >= _ProjectPlan.DateFrom.Date And _
                             _glrows.GLHeader.DateExecution.Date < _ProjectPlan.DateTo.Date And _
                             _glrows.GLHeader IsNot Nothing And _
                             _glrows.GLHeader.CustomersFrom.Oid = _ProjectPlan.CustomersFrom.Oid _
           Group _glrows By _glrows.DebitChartOfAccounts.AccountNumber, _
                            _glrows.GLHeader.DateExecution.Date.Year, _
                            _glrows.GLHeader.DateExecution.Date.Month, _
                            _glrows.Controlling, _
                            _glrows.CostHolder, _
                            _glrows.CostPlace, _
                            _glrows.UniqueObjects, _
                            _glrows.JobNumberObjects Into g = Group _
           Select New With {Key .AccountNumber = AccountNumber, _
                            Key .DateYear = Year, _
                            Key .DateMonth = Month, _
                            Key .Controlling = Controlling, _
                            Key .CostHolder = CostHolder, _
                            Key .CostPlace = CostPlace, _
                            Key .UniqueObjects = UniqueObjects, _
                            Key .JobNumberObjects = JobNumberObjects, _
                            Key .AmountNettoHUF = g.Sum(Function(inv) (inv.AmountHUF))}

        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()

            '//-------------------------------------------------------------------------------------------------------------
            _criteria = " AccountNumber='" & item.AccountNumber & "' and"
            _criteria += " DateYear=" & item.DateYear & " and "
            _criteria += " DateMonth=" & item.DateMonth & " and "
            If item.Controlling Is Nothing Then
                _criteria += "isnull(Controlling)=True and "
            Else
                _criteria += " Controlling.Oid='" & item.Controlling.Oid.ToString & "' and "
            End If
            If item.CostHolder Is Nothing Then
                _criteria += " isnull(CostHolder)=True and "
            Else
                _criteria += " CostHolder.Oid='" & item.CostHolder.Oid.ToString & "' and "
            End If
            If item.CostPlace Is Nothing Then
                _criteria += " isnull(CostPlace)=True and "
            Else
                _criteria += " CostPlace.Oid='" & item.CostPlace.Oid.ToString & "' and "
            End If
            If item.UniqueObjects Is Nothing Then
                _criteria += " isnull(UniqueObjects)=True and "
            Else
                _criteria += " UniqueObjects.Oid='" & item.UniqueObjects.Oid.ToString & "' and "
            End If
            If item.JobNumberObjects Is Nothing Then
                _criteria += " isnull(JobNumberObjects)=True "
            Else
                _criteria += " JobNumberObjects.Oid='" & item.JobNumberObjects.Oid.ToString & "' "
            End If

            _ProjectPlanRows = _uow.FindObject(Of ProjectPlanRows)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse(_criteria))
            If _ProjectPlanRows Is Nothing Then
                _ProjectPlanRows = New ProjectPlanRows(_uow)
            End If
            _ProjectPlanRows.ProjectPlan = _ProjectPlan
            _ProjectPlanRows.DebitAmountFactHUF += item.AmountNettoHUF

            _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", item.AccountNumber))
            If _ChartOfAccounts IsNot Nothing Then
                _ProjectPlanRows.ChartOfAccountsName = _ChartOfAccounts.Name
            End If
            _ProjectPlanRows.AccountNumber = item.AccountNumber
            _ProjectPlanRows.Controlling = item.Controlling
            _ProjectPlanRows.CostHolder = item.CostHolder
            _ProjectPlanRows.DateYear = item.DateYear
            _ProjectPlanRows.DateMonth = item.DateMonth
            _ProjectPlanRows.JobNumberObjects = item.JobNumberObjects
            _ProjectPlanRows.UniqueObjects = item.UniqueObjects
            _uow.CommitTransaction()
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork

        '// Követel láb alapján
        query = From _glrows In _GLRows_xpquery
                       Where _glrows.GLHeader.IsEditable = False And _
                             _glrows.GLHeader.DateExecution.Date >= _ProjectPlan.DateFrom.Date And _
                             _glrows.GLHeader.DateExecution.Date < _ProjectPlan.DateTo.Date And _
                             _glrows.GLHeader IsNot Nothing And _
                             _glrows.GLHeader.CustomersFrom.Oid = _ProjectPlan.CustomersFrom.Oid _
           Group _glrows By _glrows.CreditChartOfAccounts.AccountNumber, _
                            _glrows.GLHeader.DateExecution.Date.Year, _
                            _glrows.GLHeader.DateExecution.Date.Month, _
                            _glrows.Controlling, _
                            _glrows.CostHolder, _
                            _glrows.CostPlace, _
                            _glrows.UniqueObjects, _
                            _glrows.JobNumberObjects Into g = Group _
           Select New With {Key .AccountNumber = AccountNumber, _
                            Key .DateYear = Year, _
                            Key .DateMonth = Month, _
                            Key .Controlling = Controlling, _
                            Key .CostHolder = CostHolder, _
                            Key .CostPlace = CostPlace, _
                            Key .UniqueObjects = UniqueObjects, _
                            Key .JobNumberObjects = JobNumberObjects, _
                            Key .AmountNettoHUF = g.Sum(Function(inv) (inv.AmountHUF))}

        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()

            '//-------------------------------------------------------------------------------------------------------------
            _criteria = " AccountNumber='" & item.AccountNumber & "' and"
            _criteria += " DateYear=" & item.DateYear & " and "
            _criteria += " DateMonth=" & item.DateMonth & " and "
            If item.Controlling Is Nothing Then
                _criteria += "isnull(Controlling)=True and "
            Else
                _criteria += " Controlling.Oid='" & item.Controlling.Oid.ToString & "' and "
            End If
            If item.CostHolder Is Nothing Then
                _criteria += " isnull(CostHolder)=True and "
            Else
                _criteria += " CostHolder.Oid='" & item.CostHolder.Oid.ToString & "' and "
            End If
            If item.CostPlace Is Nothing Then
                _criteria += " isnull(CostPlace)=True and "
            Else
                _criteria += " CostPlace.Oid='" & item.CostPlace.Oid.ToString & "' and "
            End If
            If item.UniqueObjects Is Nothing Then
                _criteria += " isnull(UniqueObjects)=True and "
            Else
                _criteria += " UniqueObjects.Oid='" & item.UniqueObjects.Oid.ToString & "' and "
            End If
            If item.JobNumberObjects Is Nothing Then
                _criteria += " isnull(JobNumberObjects)=True "
            Else
                _criteria += " JobNumberObjects.Oid='" & item.JobNumberObjects.Oid.ToString & "' "
            End If

            _ProjectPlanRows = _uow.FindObject(Of ProjectPlanRows)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse(_criteria))
            If _ProjectPlanRows Is Nothing Then
                _ProjectPlanRows = New ProjectPlanRows(_uow)
            End If
            _ProjectPlanRows.ProjectPlan = _ProjectPlan
            _ProjectPlanRows.CreditAmountFactHUF += item.AmountNettoHUF

            _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", item.AccountNumber))
            If _ChartOfAccounts IsNot Nothing Then
                _ProjectPlanRows.ChartOfAccountsName = _ChartOfAccounts.Name
            End If
            _ProjectPlanRows.AccountNumber = item.AccountNumber
            _ProjectPlanRows.Controlling = item.Controlling
            _ProjectPlanRows.CostHolder = item.CostHolder
            _ProjectPlanRows.DateYear = item.DateYear
            _ProjectPlanRows.DateMonth = item.DateMonth
            _ProjectPlanRows.JobNumberObjects = item.JobNumberObjects
            _ProjectPlanRows.UniqueObjects = item.UniqueObjects
            _uow.CommitTransaction()
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork

        '// Most a vevői szerződési sorok  jönnek
        Dim _ContractRows_xpquery = New XPQuery(Of ContractRows)(_uow)

        query = From _ContractRows In _ContractRows_xpquery
                       Where _ContractRows.Contracts.State = State.Signed And _
                             _ContractRows.Contracts.ContractTemplate.PartnerType = ePartnerType.eReceivables And _
                             _ContractRows.DateExecution.Date >= _ProjectPlan.DateFrom.Date And _
                             _ContractRows.DateExecution.Date < _ProjectPlan.DateTo.Date And _
                             _ContractRows.Contracts IsNot Nothing And _
                             _ContractRows.Contracts.CustomersFrom.Oid = _ProjectPlan.CustomersFrom.Oid _
           Group _ContractRows By _ContractRows.DateExecution.Date.Year, _
                            _ContractRows.DateExecution.Date.Month, _
                            _ContractRows.Controlling, _
                            _ContractRows.CostHolder, _
                            _ContractRows.CostPlace, _
                            _ContractRows.UniqueObjects, _
                            _ContractRows.Contracts.Customers, _
                            _ContractRows.VAT, _
                            _ContractRows.JobNumberObjects Into g = Group _
           Select New With {Key .DateYear = Year, _
                            Key .DateMonth = Month, _
                            Key .Controlling = Controlling, _
                            Key .CostHolder = CostHolder, _
                            Key .CostPlace = CostPlace, _
                            Key .UniqueObjects = UniqueObjects, _
                            Key .JobNumberObjects = JobNumberObjects, _
                            Key .VAT = VAT,
                            Key .Customers = Customers,
                            Key .AmountNettoHUF = g.Sum(Function(inv) (inv.QTT * inv.NettoPriceHUF))}

        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()
            _Customers = item.Customers
            _ChartOfAccounts = _Customers.CustomersGLParameters.GetChartOfAccounts(item.DateYear, eVATMode.eOut)
            If _ChartOfAccounts IsNot Nothing Then
                _AccountNumber = _ChartOfAccounts.AccountNumber
            End If

            '//Bruttó sor tartozikba -------------------
            '//-------------------------------------------------------------------------------------------------------------
            _criteria = " AccountNumber='" & _AccountNumber & "' and"
            _criteria += " DateYear=" & item.DateYear & " and "
            _criteria += " DateMonth=" & item.DateMonth & " and "
            If item.Controlling Is Nothing Then
                _criteria += "isnull(Controlling)=True and "
            Else
                _criteria += " Controlling.Oid='" & item.Controlling.Oid.ToString & "' and "
            End If
            If item.CostHolder Is Nothing Then
                _criteria += " isnull(CostHolder)=True and "
            Else
                _criteria += " CostHolder.Oid='" & item.CostHolder.Oid.ToString & "' and "
            End If
            If item.CostPlace Is Nothing Then
                _criteria += " isnull(CostPlace)=True and "
            Else
                _criteria += " CostPlace.Oid='" & item.CostPlace.Oid.ToString & "' and "
            End If
            If item.UniqueObjects Is Nothing Then
                _criteria += " isnull(UniqueObjects)=True and "
            Else
                _criteria += " UniqueObjects.Oid='" & item.UniqueObjects.Oid.ToString & "' and "
            End If
            If item.JobNumberObjects Is Nothing Then
                _criteria += " isnull(JobNumberObjects)=True "
            Else
                _criteria += " JobNumberObjects.Oid='" & item.JobNumberObjects.Oid.ToString & "' "
            End If

            _ProjectPlanRows = _uow.FindObject(Of ProjectPlanRows)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse(_criteria))
            If _ProjectPlanRows Is Nothing Then
                _ProjectPlanRows = New ProjectPlanRows(_uow)
            End If
            _ProjectPlanRows.ProjectPlan = _ProjectPlan
            _VAT = item.VAT
            If _VAT IsNot Nothing Then
                _ProjectPlanRows.DebitAmountContractHUF += Math.Round(item.AmountNettoHUF * (1 + _VAT.KeyValue), 0, MidpointRounding.AwayFromZero)
            Else
                _ProjectPlanRows.DebitAmountContractHUF += item.AmountNettoHUF
            End If


            _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", _AccountNumber))
            If _ChartOfAccounts IsNot Nothing Then
                _ProjectPlanRows.ChartOfAccountsName = _ChartOfAccounts.Name
            End If
            _ProjectPlanRows.AccountNumber = _AccountNumber
            _ProjectPlanRows.Controlling = item.Controlling
            _ProjectPlanRows.CostHolder = item.CostHolder
            _ProjectPlanRows.DateYear = item.DateYear
            _ProjectPlanRows.DateMonth = item.DateMonth
            _ProjectPlanRows.JobNumberObjects = item.JobNumberObjects
            _ProjectPlanRows.UniqueObjects = item.UniqueObjects
            _uow.CommitTransaction()

            '//Nettó sor követelbe --------------------------
            _Controlling = item.Controlling
            If _Controlling IsNot Nothing Then
                _ChartOfAccounts = _Controlling.GetChartOfAccounts(item.DateYear, False)
                If _ChartOfAccounts IsNot Nothing Then
                    _AccountNumber = _ChartOfAccounts.AccountNumber
                    '//-------------------------------------------------------------------------------------------------------------
                    _criteria = " AccountNumber='" & _AccountNumber & "' and"
                    _criteria += " DateYear=" & item.DateYear & " and "
                    _criteria += " DateMonth=" & item.DateMonth & " and "
                    If item.Controlling Is Nothing Then
                        _criteria += "isnull(Controlling)=True and "
                    Else
                        _criteria += " Controlling.Oid='" & item.Controlling.Oid.ToString & "' and "
                    End If
                    If item.CostHolder Is Nothing Then
                        _criteria += " isnull(CostHolder)=True and "
                    Else
                        _criteria += " CostHolder.Oid='" & item.CostHolder.Oid.ToString & "' and "
                    End If
                    If item.CostPlace Is Nothing Then
                        _criteria += " isnull(CostPlace)=True and "
                    Else
                        _criteria += " CostPlace.Oid='" & item.CostPlace.Oid.ToString & "' and "
                    End If
                    If item.UniqueObjects Is Nothing Then
                        _criteria += " isnull(UniqueObjects)=True and "
                    Else
                        _criteria += " UniqueObjects.Oid='" & item.UniqueObjects.Oid.ToString & "' and "
                    End If
                    If item.JobNumberObjects Is Nothing Then
                        _criteria += " isnull(JobNumberObjects)=True "
                    Else
                        _criteria += " JobNumberObjects.Oid='" & item.JobNumberObjects.Oid.ToString & "' "
                    End If

                    _ProjectPlanRows = _uow.FindObject(Of ProjectPlanRows)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse(_criteria))
                    If _ProjectPlanRows Is Nothing Then
                        _ProjectPlanRows = New ProjectPlanRows(_uow)
                    End If
                    _ProjectPlanRows.ProjectPlan = _ProjectPlan
                    _ProjectPlanRows.CreditAmountContractHUF += item.AmountNettoHUF
                    _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", _AccountNumber))
                    If _ChartOfAccounts IsNot Nothing Then
                        _ProjectPlanRows.ChartOfAccountsName = _ChartOfAccounts.Name
                    End If
                    _ProjectPlanRows.AccountNumber = _AccountNumber
                    _ProjectPlanRows.Controlling = item.Controlling
                    _ProjectPlanRows.CostHolder = item.CostHolder
                    _ProjectPlanRows.DateYear = item.DateYear
                    _ProjectPlanRows.DateMonth = item.DateMonth
                    _ProjectPlanRows.JobNumberObjects = item.JobNumberObjects
                    _ProjectPlanRows.UniqueObjects = item.UniqueObjects
                    _uow.CommitTransaction()
                End If
            End If
            '//ÁFA követelbe -------------------------------
            If _VAT IsNot Nothing Then
                _ChartOfAccounts = _VAT.GetChartOfAccounts(item.DateYear, eVATMode.eOut)
                If _ChartOfAccounts IsNot Nothing Then
                    _AccountNumber = _ChartOfAccounts.AccountNumber
                    '//-------------------------------------------------------------------------------------------------------------
                    _criteria = " AccountNumber='" & _AccountNumber & "' and"
                    _criteria += " DateYear=" & item.DateYear & " and "
                    _criteria += " DateMonth=" & item.DateMonth & " and "
                    If item.Controlling Is Nothing Then
                        _criteria += "isnull(Controlling)=True and "
                    Else
                        _criteria += " Controlling.Oid='" & item.Controlling.Oid.ToString & "' and "
                    End If
                    If item.CostHolder Is Nothing Then
                        _criteria += " isnull(CostHolder)=True and "
                    Else
                        _criteria += " CostHolder.Oid='" & item.CostHolder.Oid.ToString & "' and "
                    End If
                    If item.CostPlace Is Nothing Then
                        _criteria += " isnull(CostPlace)=True and "
                    Else
                        _criteria += " CostPlace.Oid='" & item.CostPlace.Oid.ToString & "' and "
                    End If
                    If item.UniqueObjects Is Nothing Then
                        _criteria += " isnull(UniqueObjects)=True and "
                    Else
                        _criteria += " UniqueObjects.Oid='" & item.UniqueObjects.Oid.ToString & "' and "
                    End If
                    If item.JobNumberObjects Is Nothing Then
                        _criteria += " isnull(JobNumberObjects)=True "
                    Else
                        _criteria += " JobNumberObjects.Oid='" & item.JobNumberObjects.Oid.ToString & "' "
                    End If

                    _ProjectPlanRows = _uow.FindObject(Of ProjectPlanRows)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse(_criteria))
                    If _ProjectPlanRows Is Nothing Then
                        _ProjectPlanRows = New ProjectPlanRows(_uow)
                    End If
                    _ProjectPlanRows.ProjectPlan = _ProjectPlan
                    _ProjectPlanRows.CreditAmountContractHUF += Math.Round(item.AmountNettoHUF * _VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                    _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", _AccountNumber))
                    If _ChartOfAccounts IsNot Nothing Then
                        _ProjectPlanRows.ChartOfAccountsName = _ChartOfAccounts.Name
                    End If
                    _ProjectPlanRows.AccountNumber = _AccountNumber
                    _ProjectPlanRows.Controlling = item.Controlling
                    _ProjectPlanRows.CostHolder = item.CostHolder
                    _ProjectPlanRows.DateYear = item.DateYear
                    _ProjectPlanRows.DateMonth = item.DateMonth
                    _ProjectPlanRows.JobNumberObjects = item.JobNumberObjects
                    _ProjectPlanRows.UniqueObjects = item.UniqueObjects
                    _uow.CommitTransaction()
                End If
            End If
        Next
        _uow.CommitTransaction()
        RaiseEvent FinalWork

        '// Most a Szállítói szerződési sorok  jönnek

        query = From _ContractRows In _ContractRows_xpquery
                       Where _ContractRows.Contracts.State = State.Signed And _
                             _ContractRows.Contracts.ContractTemplate.PartnerType = ePartnerType.ePayabels And _
                             _ContractRows.DateExecution.Date >= _ProjectPlan.DateFrom.Date And _
                             _ContractRows.DateExecution.Date < _ProjectPlan.DateTo.Date And _
                             _ContractRows.Contracts IsNot Nothing And _
                             _ContractRows.Contracts.CustomersFrom.Oid = _ProjectPlan.CustomersFrom.Oid _
           Group _ContractRows By _ContractRows.DateExecution.Date.Year, _
                            _ContractRows.DateExecution.Date.Month, _
                            _ContractRows.Controlling, _
                            _ContractRows.CostHolder, _
                            _ContractRows.CostPlace, _
                            _ContractRows.UniqueObjects, _
                            _ContractRows.Contracts.Customers, _
                            _ContractRows.VAT, _
                            _ContractRows.JobNumberObjects Into g = Group _
           Select New With {Key .DateYear = Year, _
                            Key .DateMonth = Month, _
                            Key .Controlling = Controlling, _
                            Key .CostHolder = CostHolder, _
                            Key .CostPlace = CostPlace, _
                            Key .UniqueObjects = UniqueObjects, _
                            Key .JobNumberObjects = JobNumberObjects, _
                            Key .VAT = VAT,
                            Key .Customers = Customers,
                            Key .AmountNettoHUF = g.Sum(Function(inv) (inv.QTT * inv.NettoPriceHUF))}

        RaiseEvent InitWork(1, 1, query.Count)
        For Each item In query
            RaiseEvent DoWork()
            _Customers = item.Customers
            _ChartOfAccounts = _Customers.CustomersGLParameters.GetChartOfAccounts(item.DateYear, eVATMode.eIn)
            If _ChartOfAccounts IsNot Nothing Then
                _AccountNumber = _ChartOfAccounts.AccountNumber
            End If

            '//Bruttó sor követel -------------------
            '//-------------------------------------------------------------------------------------------------------------
            _criteria = " AccountNumber='" & _AccountNumber & "' and"
            _criteria += " DateYear=" & item.DateYear & " and "
            _criteria += " DateMonth=" & item.DateMonth & " and "
            If item.Controlling Is Nothing Then
                _criteria += "isnull(Controlling)=True and "
            Else
                _criteria += " Controlling.Oid='" & item.Controlling.Oid.ToString & "' and "
            End If
            If item.CostHolder Is Nothing Then
                _criteria += " isnull(CostHolder)=True and "
            Else
                _criteria += " CostHolder.Oid='" & item.CostHolder.Oid.ToString & "' and "
            End If
            If item.CostPlace Is Nothing Then
                _criteria += " isnull(CostPlace)=True and "
            Else
                _criteria += " CostPlace.Oid='" & item.CostPlace.Oid.ToString & "' and "
            End If
            If item.UniqueObjects Is Nothing Then
                _criteria += " isnull(UniqueObjects)=True and "
            Else
                _criteria += " UniqueObjects.Oid='" & item.UniqueObjects.Oid.ToString & "' and "
            End If
            If item.JobNumberObjects Is Nothing Then
                _criteria += " isnull(JobNumberObjects)=True "
            Else
                _criteria += " JobNumberObjects.Oid='" & item.JobNumberObjects.Oid.ToString & "' "
            End If

            _ProjectPlanRows = _uow.FindObject(Of ProjectPlanRows)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse(_criteria))
            If _ProjectPlanRows Is Nothing Then
                _ProjectPlanRows = New ProjectPlanRows(_uow)
            End If
            _ProjectPlanRows.ProjectPlan = _ProjectPlan
            _VAT = item.VAT
            If _VAT IsNot Nothing Then
                _ProjectPlanRows.CreditAmountContractHUF += Math.Round(item.AmountNettoHUF * (1 + _VAT.KeyValue), 0, MidpointRounding.AwayFromZero)
            Else
                _ProjectPlanRows.CreditAmountContractHUF += item.AmountNettoHUF
            End If


            _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", _AccountNumber))
            If _ChartOfAccounts IsNot Nothing Then
                _ProjectPlanRows.ChartOfAccountsName = _ChartOfAccounts.Name
            End If
            _ProjectPlanRows.AccountNumber = _AccountNumber
            _ProjectPlanRows.Controlling = item.Controlling
            _ProjectPlanRows.CostHolder = item.CostHolder
            _ProjectPlanRows.DateYear = item.DateYear
            _ProjectPlanRows.DateMonth = item.DateMonth
            _ProjectPlanRows.JobNumberObjects = item.JobNumberObjects
            _ProjectPlanRows.UniqueObjects = item.UniqueObjects
            _uow.CommitTransaction()

            '//Nettó sor tartozikba --------------------------
            _Controlling = item.Controlling
            If _Controlling IsNot Nothing Then
                _ChartOfAccounts = _Controlling.GetChartOfAccounts(item.DateYear, False)
                If _ChartOfAccounts IsNot Nothing Then
                    _AccountNumber = _ChartOfAccounts.AccountNumber
                    '//-------------------------------------------------------------------------------------------------------------
                    _criteria = " AccountNumber='" & _AccountNumber & "' and"
                    _criteria += " DateYear=" & item.DateYear & " and "
                    _criteria += " DateMonth=" & item.DateMonth & " and "
                    If item.Controlling Is Nothing Then
                        _criteria += "isnull(Controlling)=True and "
                    Else
                        _criteria += " Controlling.Oid='" & item.Controlling.Oid.ToString & "' and "
                    End If
                    If item.CostHolder Is Nothing Then
                        _criteria += " isnull(CostHolder)=True and "
                    Else
                        _criteria += " CostHolder.Oid='" & item.CostHolder.Oid.ToString & "' and "
                    End If
                    If item.CostPlace Is Nothing Then
                        _criteria += " isnull(CostPlace)=True and "
                    Else
                        _criteria += " CostPlace.Oid='" & item.CostPlace.Oid.ToString & "' and "
                    End If
                    If item.UniqueObjects Is Nothing Then
                        _criteria += " isnull(UniqueObjects)=True and "
                    Else
                        _criteria += " UniqueObjects.Oid='" & item.UniqueObjects.Oid.ToString & "' and "
                    End If
                    If item.JobNumberObjects Is Nothing Then
                        _criteria += " isnull(JobNumberObjects)=True "
                    Else
                        _criteria += " JobNumberObjects.Oid='" & item.JobNumberObjects.Oid.ToString & "' "
                    End If

                    _ProjectPlanRows = _uow.FindObject(Of ProjectPlanRows)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse(_criteria))
                    If _ProjectPlanRows Is Nothing Then
                        _ProjectPlanRows = New ProjectPlanRows(_uow)
                    End If
                    _ProjectPlanRows.ProjectPlan = _ProjectPlan
                    _ProjectPlanRows.DebitAmountContractHUF += item.AmountNettoHUF
                    _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", _AccountNumber))
                    If _ChartOfAccounts IsNot Nothing Then
                        _ProjectPlanRows.ChartOfAccountsName = _ChartOfAccounts.Name
                    End If
                    _ProjectPlanRows.AccountNumber = _AccountNumber
                    _ProjectPlanRows.Controlling = item.Controlling
                    _ProjectPlanRows.CostHolder = item.CostHolder
                    _ProjectPlanRows.DateYear = item.DateYear
                    _ProjectPlanRows.DateMonth = item.DateMonth
                    _ProjectPlanRows.JobNumberObjects = item.JobNumberObjects
                    _ProjectPlanRows.UniqueObjects = item.UniqueObjects
                    _uow.CommitTransaction()
                End If
            End If
            '//ÁFA követelbe -------------------------------
            If _VAT IsNot Nothing Then
                _ChartOfAccounts = _VAT.GetChartOfAccounts(item.DateYear, eVATMode.eIn)
                If _ChartOfAccounts IsNot Nothing Then
                    _AccountNumber = _ChartOfAccounts.AccountNumber
                    '//-------------------------------------------------------------------------------------------------------------
                    _criteria = " AccountNumber='" & _AccountNumber & "' and"
                    _criteria += " DateYear=" & item.DateYear & " and "
                    _criteria += " DateMonth=" & item.DateMonth & " and "
                    If item.Controlling Is Nothing Then
                        _criteria += "isnull(Controlling)=True and "
                    Else
                        _criteria += " Controlling.Oid='" & item.Controlling.Oid.ToString & "' and "
                    End If
                    If item.CostHolder Is Nothing Then
                        _criteria += " isnull(CostHolder)=True and "
                    Else
                        _criteria += " CostHolder.Oid='" & item.CostHolder.Oid.ToString & "' and "
                    End If
                    If item.CostPlace Is Nothing Then
                        _criteria += " isnull(CostPlace)=True and "
                    Else
                        _criteria += " CostPlace.Oid='" & item.CostPlace.Oid.ToString & "' and "
                    End If
                    If item.UniqueObjects Is Nothing Then
                        _criteria += " isnull(UniqueObjects)=True and "
                    Else
                        _criteria += " UniqueObjects.Oid='" & item.UniqueObjects.Oid.ToString & "' and "
                    End If
                    If item.JobNumberObjects Is Nothing Then
                        _criteria += " isnull(JobNumberObjects)=True "
                    Else
                        _criteria += " JobNumberObjects.Oid='" & item.JobNumberObjects.Oid.ToString & "' "
                    End If

                    _ProjectPlanRows = _uow.FindObject(Of ProjectPlanRows)(PersistentCriteriaEvaluationBehavior.InTransaction, CriteriaOperator.Parse(_criteria))
                    If _ProjectPlanRows Is Nothing Then
                        _ProjectPlanRows = New ProjectPlanRows(_uow)
                    End If
                    _ProjectPlanRows.ProjectPlan = _ProjectPlan
                    _ProjectPlanRows.DebitAmountContractHUF += Math.Round(item.AmountNettoHUF * _VAT.KeyValue, 0, MidpointRounding.AwayFromZero)
                    _ChartOfAccounts = _uow.FindObject(Of ChartOfAccounts)(CriteriaOperator.Parse("AccountNumber=?", _AccountNumber))
                    If _ChartOfAccounts IsNot Nothing Then
                        _ProjectPlanRows.ChartOfAccountsName = _ChartOfAccounts.Name
                    End If
                    _ProjectPlanRows.AccountNumber = _AccountNumber
                    _ProjectPlanRows.Controlling = item.Controlling
                    _ProjectPlanRows.CostHolder = item.CostHolder
                    _ProjectPlanRows.DateYear = item.DateYear
                    _ProjectPlanRows.DateMonth = item.DateMonth
                    _ProjectPlanRows.JobNumberObjects = item.JobNumberObjects
                    _ProjectPlanRows.UniqueObjects = item.UniqueObjects
                    _uow.CommitTransaction()
                End If
            End If
        Next
        _uow.CommitTransaction()

        _SQL = " Delete from ""ProjectPlanRows"" "
        _SQL += " Where ""DebitAmountPlanHUF""=0 and "
        _SQL += "       ""CreditAmountPlanHUF""=0 and "
        _SQL += "       ""DebitAmountFactHUF""=0 and "
        _SQL += "       ""CreditAmountFactHUF""=0 and "
        _SQL += "       ""DebitAmountContractHUF""=0 and "
        _SQL += "       ""CreditAmountContractHUF""=0 "
        _onew.Session.ExecuteNonQuery(_SQL)

        RaiseEvent FinalWork

    End Sub

    Private Sub aProjectPlanGeneratePlan_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProjectPlanEditPlan.Execute

    End Sub

    Private Sub aProjectPlanShowPlan_Execute(sender As Object, e As SimpleActionExecuteEventArgs)

    End Sub

    Private Sub aProjectPlanViewFact_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProjectPlanViewFact.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ProjectPlanRows As ProjectPlanRows = _onew.GetObject(TryCast(View.SelectedObjects(0), ProjectPlanRows))


        Dim _GLRows_Controlling As New CollectionSource(_onew, GetType(GLRows))
        Dim _GLRows As GLRows

        '_GLRows.GLHeader.DateExecution.Date

        _GLRows_Controlling.BeginUpdateCriteria()
        _GLRows_Controlling.Criteria("Plan1") = CriteriaOperator.Parse("(DebitChartOfAccounts.AccountNumber=? or CreditChartOfAccounts.AccountNumber=?)", _
                                                                       _ProjectPlanRows.AccountNumber, _ProjectPlanRows.AccountNumber)
        If _ProjectPlanRows.Controlling Is Nothing Then
            _GLRows_Controlling.Criteria("Controlling") = CriteriaOperator.Parse("isnull(Controlling)=True")
        Else
            _GLRows_Controlling.Criteria("Controlling") = CriteriaOperator.Parse("Controlling=?", _ProjectPlanRows.Controlling)
        End If
        If _ProjectPlanRows.CostHolder Is Nothing Then
            _GLRows_Controlling.Criteria("CostHolder") = CriteriaOperator.Parse("isnull(CostHolder)=True")
        Else
            _GLRows_Controlling.Criteria("CostHolder") = CriteriaOperator.Parse("CostHolder=?", _ProjectPlanRows.CostHolder)
        End If
        If _ProjectPlanRows.CostPlace Is Nothing Then
            _GLRows_Controlling.Criteria("CostPlace") = CriteriaOperator.Parse("isnull(CostPlace)=True")
        Else
            _GLRows_Controlling.Criteria("CostPlace") = CriteriaOperator.Parse("CostPlace=?", _ProjectPlanRows.CostPlace)
        End If
        If _ProjectPlanRows.JobNumberObjects Is Nothing Then
            _GLRows_Controlling.Criteria("JobNumberObjects") = CriteriaOperator.Parse("isnull(JobNumberObjects)=True")
        Else
            _GLRows_Controlling.Criteria("JobNumberObjects") = CriteriaOperator.Parse("JobNumberObjects=?", _ProjectPlanRows.JobNumberObjects)
        End If
        If _ProjectPlanRows.UniqueObjects Is Nothing Then
            _GLRows_Controlling.Criteria("UniqueObjects") = CriteriaOperator.Parse("isnull(UniqueObjects)=True")
        Else
            _GLRows_Controlling.Criteria("UniqueObjects") = CriteriaOperator.Parse("UniqueObjects=?", _ProjectPlanRows.UniqueObjects)
        End If
        _GLRows_Controlling.Criteria("DateExecution") = CriteriaOperator.Parse("GetYear(GLHeader.DateExecution)=? and GetMonth(GLHeader.DateExecution)=? and GLHeader.IsEditable=False and GLHeader.CustomersFrom=?", _
                                                                               _ProjectPlanRows.DateYear, _ProjectPlanRows.DateMonth, _ProjectPlanRows.ProjectPlan.CustomersFrom)
        _GLRows_Controlling.EndUpdateCriteria()


        e.ShowViewParameters.CreatedView = Application.CreateListView("GLRows_ListView_ProjectPlan", _GLRows_Controlling, True)


    End Sub
End Class
