Imports System
Imports System.ComponentModel
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering

Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Persistent.Validation
Imports DevExpress.Persistent.Base.General
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Linq
Imports System.Linq.Expressions
Imports DevExpress.ExpressApp.ConditionalAppearance
Imports DevExpress.ExpressApp.Editors

'-- Fõkönyvi kivonat 
'-- Fõkönyvi kimutatás részletes 2 féle nézettel (Grid, PivotGrid)
Public Enum eGLReportType
    GeneralLedger = 0
    Controlling = 1
End Enum
Public Enum eGLReportDashboardType
    eNoDashboard = 0
    eDashboard1 = 1
    eDashboard2 = 2
    eDashboard3 = 3
    eDashboard4 = 4
End Enum
Public Enum eGLReportOwner
    ePublic = 0
    ePrivate = 1
End Enum
Public Enum eGLReportState
    eInProgress
    eReady
End Enum
Public Enum eDateExecutionMode
    eHeader = 0
    eRows = 1
End Enum
Public Enum eGLReportCustomViewNumber
    eNotSet = 0
    e01 = 1
    e02 = 2
    e03 = 3
    e04 = 4
    e05 = 5
    e06 = 6
    e07 = 7
    e08 = 8
    e09 = 9
    e10 = 10
    e11 = 11
    e12 = 12
    e13 = 13
    e14 = 14
    e15 = 15
    e16 = 16
    e17 = 17
    e18 = 18
    e19 = 19
    e20 = 20
End Enum
<DefaultClassOptions()> _
<NavigationItem(False), CreatableItem(False)> _
<ImageName("BO_Report")> _
<DeferredDeletion(False)> _
<Appearance("Disable UserCreated,ObjectCreated-GLReport",
                      AppearanceItemType:="ViewItem",
                      Enabled:=False,
                      TargetItems:="ObjectCreated;UserCreated;QueryStatistics",
                      Criteria:="1=1")> _
<Appearance("Hide properties when ReportType=0",
                      AppearanceItemType:="ViewItem",
                      TargetItems:="IsInvoice;IsRegistry;IsContract",
                      Criteria:="ReportType=0",
                      visibility:=Editors.ViewItemVisibility.Hide,
                      context:="DetailView")> _
<Appearance("Hide GLReportRowsCostHolder",
                      AppearanceItemType:="ViewItem",
                      targetitems:="GLReportRowsCostHolder",
                      criteria:="ReportType='GeneralLedger'",
                      visibility:=Editors.ViewItemVisibility.Hide,
                      context:="DetailView")> _
<Appearance("Hide Criteria",
                      AppearanceItemType:="ViewItem",
                      targetitems:="Criteria",
                      criteria:="ReportType != 'GeneralLedger'",
                      visibility:=Editors.ViewItemVisibility.Hide,
                      context:="DetailView")> _
<Appearance("Hide GLReport CustomersFrom",
                      AppearanceItemType:="ViewItem",
                      targetitems:="CustomersFrom",
                      criteria:="IsCustomersFrom=True",
                      visibility:=Editors.ViewItemVisibility.Hide,
                      context:="DetailView")> _
<Appearance("Hide TabFilter",
                      AppearanceItemType:="AppearanceItemType.LayoutItem",
                      targetitems:="@TabFilter",
                      criteria:="IsPublic=True",
                      visibility:=Editors.ViewItemVisibility.Hide,
                      context:="DetailView")> _
<Appearance("Hide parameters when InPublic",
                      AppearanceItemType:="ViewItem",
                      targetitems:="GLReportCustomersFrom;GLReportChartOfAccounts;GLReportUsers;GLReportDateExecutionMode;CustomersFrom;" +
                                   "GLReportDashboardType;ReportType;GroupName1;GroupName2;ShortName;ObjectCreated;UserCreated;IsInvoice;" +
                                   "IsRegistry;IsContract;IsCustomersFrom;GLReportState;GLReportOwner", _
                       criteria:="InPublic=True",
                       visibility:=Editors.ViewItemVisibility.Hide,
                       context:="DetailView")> _
<Appearance("Disable dates when InPublic",
                       AppearanceItemType:="ViewItem",
                       Enabled:=False,
                       TargetItems:="GLReportCustomersFrom;GLReportChartOfAccounts",
                       Criteria:="InPublic=True")> _
Public Class GLReport
    Inherits BaseObject

    Private _GLReportDashboardType As eGLReportDashboardType 'Report milyen mûszerfalra menjen fel
    Private _ReportType As eGLReportType 'Report tipusa
    Private _CustomersFrom As CustomersFrom 'Saját cég

    Private _GroupName1 As String 'I. csoportosítás
    Private _GroupName2 As String ' II. Csoportosítás
    Private _ShortName As String 'Report megnevezése
    Private _FromDate As Date 'Dátumtól
    Private _ToDate As Date 'Dátumig
    Private _ObjectCreated As Date 'Létrehozva
    Private _UserCreated As User 'Létrehozta

    Private _IsInvoice As Boolean = True
    Private _IsRegistry As Boolean = True
    Private _IsContract As Boolean = True

    Private _IsCustomersFrom As Boolean = False 'True akkor cégfüggetlen lekérdezés
    Private _GLReportState As eGLReportState = eGLReportState.eInProgress
    Private _GLReportOwner As eGLReportOwner = eGLReportOwner.ePrivate
    Private _GLReportDateExecutionMode As eDateExecutionMode = eDateExecutionMode.eHeader

    Private _VariantSelectedItemID As String
    Private _Criteria As String 'További kritéria
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        Me.IsContract = True
        Me.IsInvoice = True
        Me.IsRegistry = True
        Me.FromDate = FirstDayOfYear(GetSQLTime(Session))
        Me.ToDate = LastDayOfYear(GetSQLTime(Session))
    End Sub
    Protected Overrides Sub OnSaving()
        MyBase.OnSaving()
        If Session.IsNewObject(Me) Then
            ObjectCreated = GetSQLTime(Session)
            UserCreated = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
        End If
    End Sub
    Protected Overrides Sub OnDeleting()
        MyBase.OnDeleting()
        Select Case GLOBAL_SQLType

            Case eSQLType.PostgreSQL
                Session.ExecuteNonQuery("delete from ""GLReportRowsCostHolder"" where ""GLReport""='" & Me.Oid.ToString & "'")
                Session.ExecuteNonQuery("delete from ""GLReportRows"" where ""GLReport""='" & Me.Oid.ToString & "'")
                Session.ExecuteNonQuery("delete from ""GLReportCustomersFrom"" where ""GLReport""='" & Me.Oid.ToString & "'")
                Session.ExecuteNonQuery("delete from ""GLReportChartOfAccounts"" where ""GLReport""='" & Me.Oid.ToString & "'")
            Case Else
                Session.ExecuteNonQuery("delete from GLReportRowsCostHolder where GLReport='" & Me.Oid.ToString & "'")
                Session.ExecuteNonQuery("delete from GLReportRows where GLReport='" & Me.Oid.ToString & "'")
                Session.ExecuteNonQuery("delete from GLReportCustomersFrom where GLReport='" & Me.Oid.ToString & "'")
                Session.ExecuteNonQuery("delete from GLReportChartOfAccounts where GLReport='" & Me.Oid.ToString & "'")
        End Select
    End Sub
    Property GLReportDashboardType As eGLReportDashboardType
        Get
            Return _GLReportDashboardType
        End Get
        Set(value As eGLReportDashboardType)
            SetPropertyValue("GLReportDashboardType", _GLReportDashboardType, value)
        End Set
    End Property
    <ImmediatePostData()> _
    Property ReportType As eGLReportType
        Get
            Return _ReportType
        End Get
        Set(value As eGLReportType)
            SetPropertyValue("ReportType", _ReportType, value)
        End Set
    End Property

    Property GroupName1 As String
        Get
            Return _GroupName1
        End Get
        Set(value As String)
            SetPropertyValue("GroupName1", _GroupName1, value)
        End Set
    End Property
    Property GroupName2 As String
        Get
            Return _GroupName2
        End Get
        Set(value As String)
            SetPropertyValue("GroupName2", _GroupName2, value)
        End Set
    End Property
    <RuleRequiredField("GLReport-ShortName", DefaultContexts.Save)> _
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    Property FromDate As Date
        Get
            Return _FromDate
        End Get
        Set(value As Date)
            SetPropertyValue("FromDate", _FromDate, value)
        End Set
    End Property
    Property ToDate As Date
        Get
            Return _ToDate
        End Get
        Set(value As Date)
            SetPropertyValue("ToDate", _ToDate, value)
        End Set
    End Property
    Property CustomersFrom As CustomersFrom
        Get
            Return _CustomersFrom
        End Get
        Set(value As CustomersFrom)
            SetPropertyValue("CustomersFrom", _CustomersFrom, value)
        End Set
    End Property
    <NonCloneable()> _
    Property ObjectCreated As Date
        Get
            Return _ObjectCreated
        End Get
        Set(value As Date)
            SetPropertyValue("ObjectCreated", _ObjectCreated, value)
        End Set
    End Property
    <NonCloneable()> _
    Property UserCreated As User
        Get
            Return _UserCreated
        End Get
        Set(value As User)
            SetPropertyValue("UserCreated", _UserCreated, value)
        End Set
    End Property

    Property IsInvoice As Boolean
        Get
            Return _IsInvoice
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsInvoice", _IsInvoice, value)
        End Set
    End Property
    Property IsRegistry As Boolean
        Get
            Return _IsRegistry
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsRegistry", _IsRegistry, value)
        End Set
    End Property
    Property IsContract As Boolean
        Get
            Return _IsContract
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsContract", _IsContract, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property IsCustomersFrom As Boolean
        Get
            Return _IsCustomersFrom
        End Get
        Set(value As Boolean)
            SetPropertyValue("IsCustomersFrom", _IsCustomersFrom, value)
        End Set
    End Property

    <Browsable(False)> _
    Property VariantSelectedItemID As String
        Get
            Return _VariantSelectedItemID
        End Get
        Set(value As String)
            SetPropertyValue("VariantSelectedItemID", _VariantSelectedItemID, value)
        End Set
    End Property
    Property GLReportState As eGLReportState
        Get
            Return _GLReportState
        End Get
        Set(value As eGLReportState)
            SetPropertyValue("GLReportState", _GLReportState, value)
        End Set
    End Property
    Property GLReportOwner As eGLReportOwner
        Get
            Return _GLReportOwner
        End Get
        Set(value As eGLReportOwner)
            SetPropertyValue("GLReportOwner", _GLReportOwner, value)
        End Set
    End Property
    Property GLReportDateExecutionMode As eDateExecutionMode
        Get
            Return _GLReportDateExecutionMode
        End Get
        Set(value As eDateExecutionMode)
            SetPropertyValue("GLReportDateExecutionMode", _GLReportDateExecutionMode, value)
        End Set
    End Property

    <CriteriaOptionsAttribute("ObjectType"), Size(-1)> _
    Property Criteria As String
        Get
            Return _Criteria
        End Get
        Set(value As String)
            SetPropertyValue("Criteria", _Criteria, value)
        End Set
    End Property
    <NonPersistent(), MemberDesignTimeVisibility(False)> _
    Public Property ObjectType() As Type
        Get
            Return GetType(GLRows)
        End Get
        Set(ByVal value As Type)

        End Set
    End Property
    <Browsable(False)> _
    ReadOnly Property InPublic As Boolean
        Get
            If Session.IsNewObject(Me) Then
                Return False
            Else
                Dim _User As User = Session.GetObjectByKey(Of User)(Session.GetKeyValue(SecuritySystem.CurrentUser))
                Dim _GLReportUsers As GLReportUsers
                Dim _InPublic As Boolean = True
                For Each _GLReportUsers In Me.GLReportUsers
                    If _GLReportUsers.User.Oid = _User.Oid Then
                        _InPublic = False
                    End If
                Next
                If _UserCreated.Oid = _User.Oid Then
                    _InPublic = False
                End If
                Return _InPublic
            End If
        End Get
    End Property

    '//XPCollections
    <Association("GLReport-GLReportRows", GetType(GLReportRows)), VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property GLReportRows As XPCollection(Of GLReportRows)
        Get
            Return GetCollection(Of GLReportRows)("GLReportRows")
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property GLReportRowsCostHolder As XPCollection(Of GLReportRowsCostHolder)
        Get
            Dim _GLReportRowsCostHolder_Collection As New XPCollection(Of GLReportRowsCostHolder)(Session, CriteriaOperator.Parse("GLReport=?", Me))
            Return _GLReportRowsCostHolder_Collection
        End Get
    End Property


    '//Szűrőfeltételek
    <Association("GLReport-GLReportCustomersFrom", GetType(GLReportCustomersFrom)), VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property GLReportCustomersFrom As XPCollection(Of GLReportCustomersFrom)
        Get
            Return GetCollection(Of GLReportCustomersFrom)("GLReportCustomersFrom")
        End Get
    End Property
    <Association("GLReport-GLReportChartOfAccounts", GetType(GLReportChartOfAccounts))> _
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property GLReportChartOfAccounts As XPCollection(Of GLReportChartOfAccounts)
        Get
            Return GetCollection(Of GLReportChartOfAccounts)("GLReportChartOfAccounts")
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    ReadOnly Property QueryStatistics As XPCollection(Of QueryStatistics)
        Get
            Return New XPCollection(Of QueryStatistics)(Session, CriteriaOperator.Parse("ObjectName=? and ObjectKey=?", "GLReport", Me.Oid))
        End Get
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)> _
    <Association("GLReport-GLReportUsers", GetType(GLReportUsers))> _
    ReadOnly Property GLReportUsers As XPCollection(Of GLReportUsers)
        Get
            Return GetCollection(Of GLReportUsers)("GLReportUsers")
        End Get
    End Property

End Class
