Imports System.Linq
Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions


' For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppViewControllertopic.
Partial Public Class ProductsBarcodesVC
    Inherits ViewController

    Public Sub New()
        InitializeComponent()
        RegisterActions(components)
        ' Target required Views (via the TargetXXX properties) and create their Actions.
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        ' Perform various tasks depending on the target View.
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        ' Access and customize the target View control.
    End Sub
    Protected Overrides Sub OnDeactivated()
        ' Unsubscribe from previously subscribed events and release other references and resources.
        MyBase.OnDeactivated()
    End Sub

    Private Sub aGenerateBarcodePrivate_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGenerateBarcodePrivate.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _IsExists As Boolean

        For Each _Products As Products In View.SelectedObjects
            _IsExists = False
            For Each _ProductsBarcodes As ProductsBarcodes In _Products.ProductsBarcodes
                If _ProductsBarcodes.BarcodeType = "PRIVATE" Then
                    If LTrim(RTrim(_ProductsBarcodes.Barcode)) = "" Then
                        _ProductsBarcodes.Barcode = GeneralFunction.EAN13Generator("2" + AlignStr(LTrim(RTrim(Mid(_Products.ProductNumber, 1, 11))), eDirection.Right, "0", 11))
                        _ocur.CommitChanges()
                    End If
                    _IsExists = True
                    Exit For
                End If
            Next
            If _IsExists = False Then
                Dim _ProductsBarcodes As ProductsBarcodes = _ocur.CreateObject(Of ProductsBarcodes)()
                _ProductsBarcodes.BarcodeType = "PRIVATE"
                _ProductsBarcodes.Barcode = GeneralFunction.EAN13Generator("2" + AlignStr(LTrim(RTrim(Mid(_Products.ProductNumber, 1, 11))), eDirection.Right, "0", 11))
                _ProductsBarcodes.Products = _Products
                _ProductsBarcodes.Multiplier = 1
                _ProductsBarcodes.Units = _Products.Units
                _ocur.CommitChanges()

            End If

        Next
    End Sub
End Class
