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

<DefaultClassOptions()> _
<NavigationItem(False)> _
<CreatableItem(False)> _
Public Class StorageLocation
    Inherits BaseObject

    Private _ShortName As String
    Private _Barcode As String 'Vonalkód
    Private _Storage As Storage
    Private _BarcodeEAN13Base As String

    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub
    Public Overrides Sub AfterConstruction()
        MyBase.AfterConstruction()
    End Sub

    Property Barcode As String
        Get
            Return _Barcode
        End Get
        Set(value As String)
            SetPropertyValue("Barcode", _Barcode, value)
        End Set
    End Property
    Property ShortName As String
        Get
            Return _ShortName
        End Get
        Set(value As String)
            SetPropertyValue("ShortName", _ShortName, value)
        End Set
    End Property
    Property Storage As Storage
        Get
            Return _Storage
        End Get
        Set(value As Storage)
            SetPropertyValue("Storage", _Storage, value)
        End Set
    End Property
    <NonPersistent(), ImmediatePostData()> _
    Property BarcodeEAN13Base As String
        Get
            Return _BarcodeEAN13Base
        End Get
        Set(value As String)
            SetPropertyValue("BarcodeEAN13Base", _BarcodeEAN13Base, value)
            If Not Session.IsObjectsLoading And Not Session.IsObjectsSaving Then
                If EAN13Generator(value) <> "" Then
                    Me.Barcode = EAN13Generator(value)
                Else

                End If

            End If
        End Set
    End Property
    <VisibleInListView(False), VisibleInLookupListView(False)>
    ReadOnly Property StorageComputedLocations As XPCollection(Of StorageComputedLocations)
        Get
            Return New XPCollection(Of StorageComputedLocations)(Session, CriteriaOperator.Parse("StorageLocation=?", Me))
        End Get
    End Property
    Function EAN13Generator(ByVal _Base As String) As String
        If _Base = Nothing Then
            Return ""
            Exit Function
        End If
        If _Base.Length = 12 Then
            Dim i As Long
            Dim _CheckSum As Long = 0
            For i = 1 To Len(_Base)
                If i Mod 2 = 0 Then
                    _CheckSum += Val(Mid(_Base, i, 1)) * 3
                Else
                    _CheckSum += Val(Mid(_Base, i, 1))
                End If
            Next
            _CheckSum = 10 - (_CheckSum Mod 10)
            If _CheckSum = 10 Then _CheckSum = 0

            Return _Base & LTrim(RTrim(CStr(_CheckSum)))
        Else
            Return ""
        End If
    End Function
End Class
