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.ExpressApp.ConditionalAppearance
Public Enum ePlace
    Left = 0
    Center = 1
    Right = 2
End Enum
Public Enum eYearFormat
    YearNoDigit = 0
    YearOneDigit = 1
    YearTwoDigit = 2
    YearFourDigit = 4
End Enum
<DefaultClassOptions()> _
<NavigationItem("GeneralStock")> _
<DefaultProperty("DisplayName")> _
<Appearance("Hide NewNumberPerMonth when NewNumberPerYear is False", AppearanceItemType.ViewItem, "NewNumberPerYear =False", TargetItems:="NewNumberPerMonth", visibility:=Editors.ViewItemVisibility.Hide)> _
<Appearance("Disable MonthAfterYear when NewNumberPerMonth is True", AppearanceItemType.ViewItem, "NewNumberPerMonth =True", TargetItems:="MonthAfterYear", Enabled:=False)> _
Public Class NumberGenerator
    Inherits BaseObject
    Private _NumberGeneratorGroups As NumberGeneratorGroups
    Private _PrefixString As String
    Private _PrefixTag As String
    Private _SuffixString As String
    Private _SuffixTag As String
    Private _SerialNumber As Long
    Private _SerialNumberPlace As ePlace
    Private _SerialNumberDigit As Long = 4
    Private _YearFormat As eYearFormat
    Private _NewNumberPerYear As Boolean
    Private _NewNumberPerMonth As Boolean
    Private _ShortName As String
    Private _MonthAfterYear As Boolean
    Private _CurrentDay As Boolean
    Private _CurrentDayFormat As String

    Public Function GetNewNumber(ByVal NGO As NumberGenerator, Optional ByVal _YearString As String = "", Optional _MonthString As String = "")
        Dim _NGSemafor As NumberGeneratorSemafor

        If _YearString = "" Or _YearString Is Nothing Then
            _YearString = Year(GetSQLTime(Session)).ToString
        Else

        End If

        If (_MonthString = "" And MonthAfterYear) Or (_MonthString Is Nothing And MonthAfterYear) Then
            _MonthString = Month(GetSQLTime(Session)).ToString
        End If

        If NewNumberPerYear = True Then
            If _MonthString <> "" And NGO.MonthAfterYear And NGO.NewNumberPerMonth Then
                _NGSemafor = Session.FindObject(Of NumberGeneratorSemafor)(CriteriaOperator.Parse("NumberGenerator=? and YearString=? and MonthString=? and NewNumberPerMonth=?", NGO.Oid, _YearString, _MonthString, True))
            Else
                _NGSemafor = Session.FindObject(Of NumberGeneratorSemafor)(CriteriaOperator.Parse("NumberGenerator=? and YearString=? and NewNumberPerMonth=?", NGO.Oid, _YearString, False))
            End If
        Else
            Dim CurrentYear As Integer
            Dim BaseYear As Integer = Convert.ToInt32(_YearString) - 4
            For CurrentYear = Convert.ToInt32(_YearString) To BaseYear Step -1
                'If _MonthString <> "" And NGO.MonthAfterYear And NGO.NewNumberPerMonth Then
                '    _NGSemafor = Session.FindObject(Of NumberGeneratorSemafor)(CriteriaOperator.Parse("NumberGenerator=? and YearString=? and MonthString=? and NewNumberPerMonth=?", NGO.Oid, CurrentYear.ToString(), _MonthString, True))
                '    If _NGSemafor IsNot Nothing Then
                '        Exit For
                '    End If
                'Else
                _NGSemafor = Session.FindObject(Of NumberGeneratorSemafor)(CriteriaOperator.Parse("NumberGenerator=? and YearString=? and NewNumberPerMonth=?", NGO.Oid, CurrentYear.ToString(), False))
                If _NGSemafor IsNot Nothing Then
                    Exit For
                End If
                'End If
            Next
            If _NGSemafor Is Nothing Then
                Dim _NGSemaforLastToNew As NumberGeneratorSemafor
                'If _MonthString <> "" And NGO.MonthAfterYear And NGO.NewNumberPerMonth Then
                '    _NGSemaforLastToNew = Session.FindObject(Of NumberGeneratorSemafor)(CriteriaOperator.Parse("NumberGenerator=? and YearString=? and MonthString=?", NGO.Oid, (Convert.ToInt32(_YearString) - 5).ToString(), _MonthString))
                'Else
                _NGSemaforLastToNew = Session.FindObject(Of NumberGeneratorSemafor)(CriteriaOperator.Parse("NumberGenerator=? and YearString=? and NewNumberPerMonth=?", NGO.Oid, (Convert.ToInt32(_YearString) - 5).ToString(), False))
                'End If
                If _NGSemaforLastToNew IsNot Nothing Then
                    _NGSemafor = New NumberGeneratorSemafor(Session)
                    _NGSemafor.NumberGenerator = NGO
                    _NGSemafor.YearString = _YearString
                    _NGSemafor.MonthString = _MonthString
                    _NGSemafor.SerialNumber = _NGSemaforLastToNew.SerialNumber
                    _NGSemafor.Save()
                End If
            End If
        End If
        If _NGSemafor Is Nothing Then
            _NGSemafor = New NumberGeneratorSemafor(Session)
            _NGSemafor.NumberGenerator = NGO
            _NGSemafor.YearString = _YearString
            _NGSemafor.MonthString = _MonthString
            _NGSemafor.SerialNumber = Me.SerialNumber + 1
            If _NewNumberPerMonth = True Then
                _NGSemafor.NewNumberPerMonth = True
            End If
            _NGSemafor.Save()
        Else
            _NGSemafor.SerialNumber += 1
            If _NewNumberPerMonth = False Then
                _NGSemafor.MonthString = _MonthString
            End If
            _NGSemafor.Save()
        End If

        '--- Paraméterek inicializálása
        _PrefixString = NGO.PrefixString
        _PrefixTag = NGO.PrefixTag
        _SuffixString = NGO.SuffixString
        _SuffixTag = NGO.SuffixTag
        _SerialNumberPlace = NGO.SerialNumberPlace
        _SerialNumberDigit = NGO.SerialNumberDigit
        _YearFormat = NGO.YearFormat
        _NewNumberPerYear = NGO.NewNumberPerYear
        _MonthAfterYear = NGO.MonthAfterYear
        Return CreateSampleString(_NGSemafor.YearString, _NGSemafor.MonthString, _NGSemafor.SerialNumber)
    End Function
    Function CreateSampleString(ByVal YearString As String, ByVal MonthString As String, ByVal NumberLong As Long) As String
        Dim _NumberString As String = ""
        Dim _SampleString As String = ""
        Dim _CurrentDateString As String = ""

        If NewNumberPerYear = True Then
            Select Case YearFormat
                Case eYearFormat.YearNoDigit
                    YearString = ""
                Case eYearFormat.YearFourDigit
                Case eYearFormat.YearOneDigit
                    YearString = Mid(YearString, 4, 1)
                Case eYearFormat.YearTwoDigit
                    YearString = Mid(YearString, 3, 2)
            End Select
        Else
            YearString = ""
        End If

        _NumberString = AlignStr(CStr(NumberLong), eDirection.Left, "0", _SerialNumberDigit)

        If CurrentDay Then
            _CurrentDateString = GetSQLTime(Session).ToString(CurrentDayFormat)
        Else
            _CurrentDateString = ""
        End If

        If MonthAfterYear Then
            If MonthString.Length = 1 Then MonthString = "0" + MonthString
            If _SuffixTag = "" Then
                YearString += SuffixString & MonthString
            Else
                YearString += SuffixTag & MonthString
            End If

        End If

        If _SerialNumberPlace = ePlace.Center Then
            _SampleString = PrefixString & PrefixTag & _NumberString & SuffixTag & YearString & _CurrentDateString & SuffixTag & SuffixString
        ElseIf _SerialNumberPlace = ePlace.Left Then
            If CurrentDay Then
                _SampleString = PrefixString & PrefixTag & YearString & _CurrentDateString & SuffixTag & _NumberString
            Else
                _SampleString = PrefixString & PrefixTag & YearString & _CurrentDateString & SuffixTag & _NumberString & SuffixTag & SuffixString
            End If
        ElseIf _SerialNumberPlace = ePlace.Right Then
            _SampleString = PrefixString & _PrefixTag & _NumberString & SuffixTag & SuffixString & SuffixTag & YearString & _CurrentDateString
        End If



        Return _SampleString
    End Function
    ReadOnly Property SampleString()
        Get
            Return CreateSampleString(Year(Now), Month(Now), 0)
        End Get
    End Property
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
        _SerialNumberPlace = ePlace.Right
        OnChanged("SerialNumberPlace")
        _NewNumberPerYear = True
        OnChanged("NewNumberPerYear")
        _YearFormat = eYearFormat.YearTwoDigit
        OnChanged("YearFormat")
        CurrentDayFormat = "ddMMyyyy"
        _NewNumberPerMonth = False

    End Sub
    <RuleRequiredField("", DefaultContexts.Save)> _
    Property NumberGeneratorGroups As NumberGeneratorGroups
        Get
            Return _NumberGeneratorGroups
        End Get
        Set(ByVal value As NumberGeneratorGroups)
            SetPropertyValue("NumberGeneratorGroups", _NumberGeneratorGroups, value)
        End Set
    End Property
    <RuleRequiredField("", DefaultContexts.Save)> _
    <ImmediatePostData(True)> _
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property PrefixString As String
        Get
            Return _PrefixString
        End Get
        Set(ByVal value As String)
            SetPropertyValue("PrefixString", _PrefixString, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property PrefixTag As String
        Get
            Return _PrefixTag
        End Get
        Set(ByVal value As String)
            SetPropertyValue("PrefixTag", _PrefixTag, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property SuffixString As String
        Get
            Return _SuffixString
        End Get
        Set(ByVal value As String)
            SetPropertyValue("SuffixString", _SuffixString, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property SuffixTag As String
        Get
            Return _SuffixTag
        End Get
        Set(ByVal value As String)
            SetPropertyValue("SuffixTag", _SuffixTag, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property SerialNumber As Long
        Get
            Return _SerialNumber
        End Get
        Set(ByVal value As Long)
            SetPropertyValue("SerialNumber", _SerialNumber, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property SerialNumberPlace As ePlace
        Get
            Return _SerialNumberPlace
        End Get
        Set(ByVal value As ePlace)
            SetPropertyValue("SerialNumberPlace", _SerialNumberPlace, value)
        End Set
    End Property
    <RuleRequiredField("", DefaultContexts.Save)> _
    <ImmediatePostData(True)> _
    Property YearFormat As eYearFormat
        Get
            Return _YearFormat
        End Get
        Set(ByVal value As eYearFormat)
            SetPropertyValue("YearFormat", _YearFormat, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property NewNumberPerYear As Boolean
        Get
            Return _NewNumberPerYear
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("NewNumberPerYear", _NewNumberPerYear, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property NewNumberPerMonth As Boolean
        Get
            Return _NewNumberPerMonth
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("NewNumberPerMonth", _NewNumberPerMonth, value)
            If Session.IsObjectsLoading = False And Session.IsObjectsSaving = False Then
                If value = True Then
                    Me.MonthAfterYear = True
                End If
            End If
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property SerialNumberDigit As Long
        Get
            Return _SerialNumberDigit
        End Get
        Set(ByVal value As Long)
            SetPropertyValue("SerialNumberDigit", _SerialNumberDigit, value)
        End Set
    End Property
    <ImmediatePostData(True)> _
    Property MonthAfterYear As Boolean
        Get
            Return _MonthAfterYear
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("MonthAfterYear", _MonthAfterYear, value)
        End Set
    End Property
    Property CurrentDay As Boolean
        Get
            Return _CurrentDay
        End Get
        Set(value As Boolean)
            SetPropertyValue("CurrentDay", _CurrentDay, value)
        End Set
    End Property
    Property CurrentDayFormat As String
        Get
            Return _CurrentDayFormat
        End Get
        Set(value As String)
            SetPropertyValue("CurrentDayFormat", _CurrentDayFormat, value)
        End Set
    End Property
    ReadOnly Property DisplayName() As String
        Get
            Return SampleString & ", " & ShortName
        End Get
    End Property

End Class
<DefaultClassOptions()> _
<NavigationItem("GeneralStock")> _
Public Class NumberGeneratorGroups
    Inherits BaseObject
    Private _ShortName As String
    Private _Description As String
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    <RuleRequiredField("", DefaultContexts.Save)> _
    <RuleUniqueValue("", DefaultContexts.Save)> _
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(ByVal value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    <RuleRequiredField("", DefaultContexts.Save)> _
    Property Description As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            SetPropertyValue("Description", _Description, value)
        End Set
    End Property
End Class
<DefaultClassOptions()> _
<NavigationItem("GeneralStock")> _
Public Class NumberGeneratorSemafor
    Inherits BaseObject
    Private _NumberGenerator As NumberGenerator
    Private _YearString As String
    Private _MonthString As String
    Private _SerialNumber As Long
    Private _NewNumberPerMonth As Boolean
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Property NumberGenerator() As NumberGenerator
        Get
            Return _NumberGenerator
        End Get
        Set(ByVal value As NumberGenerator)
            SetPropertyValue("NumberGenerator", _NumberGenerator, value)
        End Set
    End Property
    Property YearString As String
        Get
            Return _YearString
        End Get
        Set(ByVal value As String)
            SetPropertyValue("YearString", _YearString, value)
        End Set
    End Property
    Property MonthString As String
        Get
            Return _MonthString
        End Get
        Set(value As String)
            SetPropertyValue("MonthString", _MonthString, value)
        End Set
    End Property
    Property SerialNumber As Long
        Get
            Return _SerialNumber
        End Get
        Set(ByVal value As Long)
            SetPropertyValue("SerialNumber", _SerialNumber, value)
        End Set
    End Property
    Property NewNumberPerMonth As Boolean
        Get
            Return _NewNumberPerMonth
        End Get
        Set(ByVal value As Boolean)
            SetPropertyValue("NewNumberPerMonth", _NewNumberPerMonth, value)
        End Set
    End Property
End Class


