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 DevExpress.ExpressApp.Editors
Imports exontrol.EXG2ANTTLib
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.Persistent.BaseImpl
Imports System.Net.Mail
Imports System.Reflection
Imports DevExpress.ExpressApp.Utils
Imports DevExpress.ExpressApp.Model
Imports System.Linq

Public Class ProductsGant_VC
    Inherits Nuvolar.Module.ProductsGant_VC
   
    Dim _GanttControl As exontrol.EXG2ANTTLib.exg2antt
    Dim _ProductGandRowsOidindex As Long = 0
    Dim _GantItemColumn_Array As New ArrayList
    Dim _WaitFormClass As New WaitFormClass
    Dim _GantChange As Boolean = False
    Dim _FocusedItem As String

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Structure SISGantColumn
        Public IsGroupColumn As Boolean
        Public Index As Long
        Public ColumnID As String
        Public Caption As String
        Public Width As Long
        Public GantColumnIndex As Long
    End Structure
    
#Region "Overrides"
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        AddHandler DoWork, AddressOf _WaitFormClass.DoWork
        AddHandler InitWork, AddressOf _WaitFormClass.InitWork
        AddHandler FinalWork, AddressOf _WaitFormClass.Finalwork

        If View.Id = "ProductsGant_DetailView" Then

            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)

            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _GanttContolViewItem As ViewItem = _DetailView.FindItem("G2antt")
            If _GanttContolViewItem IsNot Nothing Then
                AddHandler _GanttContolViewItem.ControlCreated, AddressOf GanttContolViewItem_ControlCreated
                AddHandler Frame.GetController(Of RefreshController).RefreshAction.Executing, AddressOf RefreshAction_Executing
            End If
        End If
        If View.Id = "ProductsGant_ProductsGantRows_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()

        RemoveHandler DoWork, AddressOf _WaitFormClass.DoWork
        RemoveHandler InitWork, AddressOf _WaitFormClass.InitWork
        RemoveHandler FinalWork, AddressOf _WaitFormClass.Finalwork

        If View.Id = "ProductsGant_DetailView" Then
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)
        End If

        If View.Id = "ProductsGant_ProductsGantRows_ListView" Then
            Frame.GetController(Of DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub
#End Region
    Private Sub GanttContolViewItem_ControlCreated(sender As Object, e As EventArgs)
        Dim _ItemControl As Object = TryCast(sender, ViewItem).Control
        _GanttControl = TryCast(_ItemControl, G2anttUserControl).Exg2antt

        AddHandler _GanttControl.EditCloseEvent, AddressOf _GanttControl_EditCloseEvent
        AddHandler _GanttControl.SelectionChanged, AddressOf _GanttControl_SelectionChanged

        Frame.GetController(Of ProductsGant_VC).aProductsGant_Refresh.DoExecute()
    End Sub
    Private Sub aProductsGant_Refresh_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProductsGant_Refresh.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ProductsGant As ProductsGant = TryCast(View.SelectedObjects(0), ProductsGant)
        If _ProductsGant Is Nothing Then
            _ProductsGant = TryCast(TryCast(TryCast(View, ListView).CollectionSource, PropertyCollectionSource).MasterObject, ProductsGant)
        End If

        Dim _ProductsGantRowsBar_Collection As New XPCollection(Of ProductsGantRowsBar)(_onew.Session, CriteriaOperator.Parse("BarType=3"))

        If _ProductsGantRowsBar_Collection.Count > 0 Then
            For Each _ProductsGantRowsBar As ProductsGantRowsBar In _ProductsGantRowsBar_Collection
                With _ProductsGantRowsBar
                    .FromDate = GetSQLTime(_onew.Session).Date
                    If .ProductsGantRows.Products.CustomersOrderPriority.Count > 0 Then
                        .ProductsGantRows.Products.CustomersOrderPriority.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))

                        .ToDate = .FromDate.AddDays(.ProductsGantRows.Products.CustomersOrderPriority(0).DeadlineWeek * 7)
                    Else
                        .ToDate = .FromDate
                    End If
                End With

            Next
        End If
        _ProductsGantRowsBar_Collection = New XPCollection(Of ProductsGantRowsBar)(_onew.Session, CriteriaOperator.Parse("BarType=0"))

        If _ProductsGantRowsBar_Collection.Count > 0 Then
            For Each _ProductsGantRowsBar As ProductsGantRowsBar In _ProductsGantRowsBar_Collection
                With _ProductsGantRowsBar
                    If _ProductsGantRowsBar.ProductsGantRows.ManualRotation > 0 Then
                        .ToDate = .FromDate.AddDays(_ProductsGantRowsBar.ProductsGantRows.ManualRotation)
                    ElseIf _ProductsGantRowsBar.ProductsGantRows.CalculatedRotation > 0 Then
                        .ToDate = .FromDate.AddDays(_ProductsGantRowsBar.ProductsGantRows.CalculatedRotation)
                    Else
                        .ToDate = .FromDate.AddDays(1)

                    End If
                End With
            Next
        End If
        _onew.CommitChanges()

        Dim _DetailView As DetailView = TryCast(View, DetailView)
        Dim _ProductsGantRowsViewItem As ListPropertyEditor = _DetailView.FindItem("ProductsGantRows")
        Dim _IModelListView As IModelListView = Application.Model.Views.GetNode("ProductsGant_ProductsGantRows_ListView")
        If _GanttControl IsNot Nothing And _IModelListView IsNot Nothing Then

            _GantChange = False


            AddHandler _GanttControl.Change, AddressOf GanttControl_Change
            AddHandler _GanttControl.LayoutChanged, AddressOf GanttControl_LayoutChanged


            With _GanttControl
                .BeginUpdate()
                .Columns.Clear()
                .DrawGridLines = exontrol.EXG2ANTTLib.GridLinesEnum.exRowLines
                .LinesAtRoot = exontrol.EXG2ANTTLib.LinesAtRootEnum.exGroupLinesAtRoot
                .HasButtons = exontrol.EXG2ANTTLib.ExpandButtonEnum.exArrow
                .HasLines = HierarchyLineEnum.exSolidLine

                With .Chart
                    .MarkNowColor = Drawing.Color.Red
                    .MarkNowWidth = 3
                    .DrawGridLines = exontrol.EXG2ANTTLib.GridLinesEnum.exAllLines
                    .FirstVisibleDate = GetSQLTime(_onew.Session).ToString("yyyy.MM.dd")
                    .PaneWidthRight = 500
                    .FirstWeekDay = 1
                    .UnitWidth = 36
                    .LevelCount = 2
                    .get_Level(0).ToolTip = _GanttControl.Chart.get_Level(0).Label
                    .get_Level(0).Unit = 256
                    .get_Level(1).ToolTip = _GanttControl.Chart.get_Level(1).Label
                    .OverviewVisible = exontrol.EXG2ANTTLib.OverviewVisibleEnum.exOverviewShowAll
                    .AllowOverviewZoom = exontrol.EXG2ANTTLib.OverviewZoomEnum.exZoomOnRClick
                    .AllowLinkBars = True
                    .set_PaneWidth(True, 800)
                    ' .HistogramVisible = True
                    '.HistogramHeight = 80
                    '.OverviewZoomCaption = "Year|1/2Year|1/4Year|<img>3</img>Month|Third|<img>2</img>Week|<img>1</img>Day|Hour|Min|Sec"
                    '.set_Label(exontrol.EXG2ANTTLib.UnitEnum.exYear, "")
                    '.set_Label(exontrol.EXG2ANTTLib.UnitEnum.exHalfYear, "")
                    '.set_Label(exontrol.EXG2ANTTLib.UnitEnum.exQuarterYear, "")
                    '.set_Label(exontrol.EXG2ANTTLib.UnitEnum.exThirdMonth, "")
                    '.set_Label(exontrol.EXG2ANTTLib.UnitEnum.exHour, "")
                    '.set_Label(exontrol.EXG2ANTTLib.UnitEnum.exMinute, "")
                    '.set_Label(exontrol.EXG2ANTTLib.UnitEnum.exSecond, "")
                    '.OverviewZoomUnit = 64
                End With

                '.SingleSel = False

                With .Chart.Bars
                    With .Copy("Task", "Accounted")
                        .Color = Drawing.Color.Black
                        .Shape = ShapeBarEnum.exShapeSolid
                        .StartShape = ShapeCornerEnum.exShapeIconUp3
                        .EndShape = ShapeCornerEnum.exShapeIconDown3
                        .StartColor = Drawing.Color.LightYellow
                        .EndColor = Drawing.Color.Yellow
                        .Pattern = PatternEnum.exPatternBox + PatternEnum.exPatternDot + PatternEnum.exPatternShadow
                        .Height = 18
                        .HistogramPattern = PatternEnum.exPatternBox + PatternEnum.exPatternDot + PatternEnum.exPatternShadow
                        .HistogramColor = Drawing.Color.Yellow
                        .OverlaidType = OverlaidBarsTypeEnum.exOverlaidBarsStack
                    End With
                    With .Copy("Task", "Ordered")
                        .Color = Drawing.Color.Black
                        .Shape = ShapeBarEnum.exShapeSolid
                        .StartColor = Drawing.Color.LightPink
                        .EndColor = Drawing.Color.Pink
                        .Pattern = PatternEnum.exPatternBox + PatternEnum.exPatternDot + PatternEnum.exPatternShadow
                        .Height = 18
                        .HistogramPattern = PatternEnum.exPatternBox + PatternEnum.exPatternDot + PatternEnum.exPatternShadow
                        .HistogramColor = Drawing.Color.Pink
                        .OverlaidType = OverlaidBarsTypeEnum.exOverlaidBarsStack
                    End With
                    With .Copy("Summary", "Reported")
                        .Color = Drawing.Color.DarkGray
                        .Shape = ShapeBarEnum.exShapeSolid
                        .StartShape = ShapeCornerEnum.exShapeIconUp3
                        .EndShape = ShapeCornerEnum.exShapeIconDown3
                        .HistogramRulerLinesColor = Drawing.Color.Black
                        .StartColor = Drawing.Color.DarkGreen
                        .EndColor = Drawing.Color.LightGreen
                        .Pattern = PatternEnum.exPatternBox + PatternEnum.exPatternDot + PatternEnum.exPatternShadow + PatternEnum.exPatternEmpty
                        .Height = 18
                        .HistogramPattern = PatternEnum.exPatternBox + PatternEnum.exPatternDot + PatternEnum.exPatternShadow + PatternEnum.exPatternEmpty
                        .HistogramColor = Drawing.Color.Green
                        .OverlaidType = OverlaidBarsTypeEnum.exOverlaidBarsStack
                    End With
                    With .Copy("Milestone", "OrderArrive")
                        .StartShape = ShapeCornerEnum.exShapeIconVBar
                        .StartColor = Drawing.Color.IndianRed
                        .EndColor = Drawing.Color.DarkRed
                        .Color = Drawing.Color.Black
                        .Pattern = PatternEnum.exRectangularCurve + PatternEnum.exPatternDot + PatternEnum.exPatternShadow
                        .Shape = ShapeBarEnum.exShapeSolidCenter
                        .Height = 18
                        .HistogramPattern = PatternEnum.exRectangularCurve + PatternEnum.exPatternDot + PatternEnum.exPatternShadow
                        .HistogramColor = Drawing.Color.Red
                        .OverlaidType = OverlaidBarsTypeEnum.exOverlaidBarsStack
                    End With
                End With

                For Each _Column In _IModelListView.Columns
                    If _Column.Index <> -1 Then
                        Dim _SISGantColumn As New SISGantColumn
                        With _SISGantColumn
                            .Caption = _Column.Caption
                            .ColumnID = _Column.Id
                            .Index = _Column.Index
                            .Width = _Column.Width
                        End With
                        If _Column.GroupIndex = 0 Then
                            _SISGantColumn.IsGroupColumn = True
                        Else
                            _SISGantColumn.IsGroupColumn = False
                        End If

                        _GantItemColumn_Array.Add(_SISGantColumn)
                    End If
                Next

                Dim _GroupColumn As SISGantColumn = Nothing

                For Each _SISGantColumn As SISGantColumn In _GantItemColumn_Array
                    If _SISGantColumn.IsGroupColumn Then
                        _GroupColumn = _SISGantColumn
                        Exit For
                    End If
                Next

                Dim _GantColumnIndex As Integer = 0

                .TreeColumnIndex = 1

                .Columns.Add(_GantColumnIndex)
                .Columns(_GantColumnIndex).Caption = "ProductGandRowsOid"
                .Columns(_GantColumnIndex).Key = "ProductGandRowsOid"
                .Columns(_GantColumnIndex).Width = 100
                .Columns(_GantColumnIndex).Visible = False
                _ProductGandRowsOidindex = _GantColumnIndex
                _GantColumnIndex += 1

                .Columns.Add(_GantColumnIndex)
                .Columns(_GantColumnIndex).Key = _GroupColumn.ColumnID
                .Columns(_GantColumnIndex).Caption = _GroupColumn.Caption
                .Columns(_GantColumnIndex).Width = _GroupColumn.Width
                _GroupColumn.GantColumnIndex = _GantColumnIndex
                _GantColumnIndex += 1

                For _ik = 0 To _GantItemColumn_Array.Count
                    For Each _SISGantColumn As SISGantColumn In _GantItemColumn_Array
                        If Not _SISGantColumn.IsGroupColumn And _SISGantColumn.Index = _ik Then
                            .Columns.Add(_GantColumnIndex)
                            .Columns(_GantColumnIndex).Key = _SISGantColumn.ColumnID
                            .Columns(_GantColumnIndex).Caption = _SISGantColumn.Caption
                            .Columns(_GantColumnIndex).Width = _SISGantColumn.Width
                            If _SISGantColumn.ColumnID <> "Customers" And _SISGantColumn.ColumnID <> "Products" Then
                                .Columns(_GantColumnIndex).Editor.EditType = EditTypeEnum.EditType
                            End If
                            If _GroupColumn.ColumnID = "Customers" Or _GroupColumn.ColumnID = "Products" Then
                                .Columns.Item(_GroupColumn.Caption).SortOrder = exontrol.EXG2ANTTLib.SortOrderEnum.SortAscending
                            End If
                            _SISGantColumn.GantColumnIndex = _GantColumnIndex

                            _GantColumnIndex += 1
                            Exit For
                        End If
                    Next
                Next

                'Csoportsítás
                Dim _GroupObjects_Collection As New ArrayList
                If Not String.IsNullOrEmpty(_GroupColumn.ColumnID) Then
                    Select Case _GroupColumn.ColumnID
                        Case "Customers"
                            For Each _ProductsGantRows As ProductsGantRows In _ProductsGant.ProductsGantRows
                                If _GroupObjects_Collection.Count = 0 Then
                                    _GroupObjects_Collection.Add(_ProductsGantRows.Customers)
                                Else
                                    Dim _Need As Boolean = True
                                    For Each _Customers As Customers In _GroupObjects_Collection
                                        If _ProductsGantRows.Customers.Oid = _Customers.Oid Then
                                            _Need = False
                                            Exit For
                                        End If
                                    Next

                                    If _Need Then _GroupObjects_Collection.Add(_ProductsGantRows.Customers)
                                End If
                            Next

                            Dim _CustomerColumn As exontrol.EXG2ANTTLib.Column = Nothing
                            For Each _G2Column As exontrol.EXG2ANTTLib.Column In _GanttControl.Columns
                                If _G2Column.Key = _GroupColumn.ColumnID Then
                                    _CustomerColumn = _G2Column
                                    Exit For
                                End If
                            Next

                            If _CustomerColumn IsNot Nothing Then
                                For Each _Customers As Customers In _GroupObjects_Collection
                                    Dim _GroupItem As Long = _GanttControl.Items.AddItem()

                                    _GanttControl.Items.set_CellValue(_GroupItem, _CustomerColumn.Index, "<b><fgcolor=0000FF>" + _Customers.FullName + "</fgcolor></b>")
                                    _GanttControl.Items.set_CellValueFormat(_GroupItem, _CustomerColumn.Index, ValueFormatEnum.exHTML)
                                    Dim _GroupCollectionElement_Collection As New XPCollection(Of ProductsGantRows)(_onew.Session, CriteriaOperator.Parse("Customers=? AND ProductsGant=?", _onew.GetObject(_Customers), _onew.GetObject(_ProductsGant)))

                                    For Each _ProductsGantRows As ProductsGantRows In _GroupCollectionElement_Collection
                                        Dim _Item As Long = _GanttControl.Items.get_FindItem(_ProductsGantRows.Oid.ToString, 0)
                                        If _Item = 0 Then
                                            _Item = .Items.InsertItem(_GroupItem, "")
                                            .Items.set_ExpandItem(_Item, True)
                                            SetItemsAndItemData(_Item, _ProductsGantRows)
                                        End If
                                    Next
                                    _GanttControl.Items.set_ExpandItem(_GroupItem, True)
                                Next
                            End If
                        Case "Products"
                            For Each _ProductsGantRows As ProductsGantRows In _ProductsGant.ProductsGantRows
                                If _GroupObjects_Collection.Count = 0 Then
                                    _GroupObjects_Collection.Add(_ProductsGantRows.Products)
                                Else
                                    Dim _Need As Boolean = True
                                    For Each _Products As Products In _GroupObjects_Collection
                                        If _ProductsGantRows.Products.Oid = _Products.Oid Then
                                            _Need = False
                                            Exit For
                                        End If
                                    Next

                                    If _Need Then _GroupObjects_Collection.Add(_ProductsGantRows.Products)
                                End If
                            Next

                            Dim _ProductsColumn As exontrol.EXG2ANTTLib.Column = Nothing
                            For Each _G2Column As exontrol.EXG2ANTTLib.Column In _GanttControl.Columns
                                If _G2Column.Key = _GroupColumn.ColumnID Then
                                    _ProductsColumn = _G2Column
                                    Exit For
                                End If
                            Next

                            If _ProductsColumn IsNot Nothing Then
                                For Each _Products As Products In _GroupObjects_Collection
                                    Dim _GroupItem As Long = _GanttControl.Items.AddItem()
                                    _GanttControl.Items.set_CellValue(_GroupItem, _ProductsColumn.Index, _Products.ShortName)

                                    Dim _GroupCollectionElement_Collection As New XPCollection(Of ProductsGantRows)(_onew.Session, CriteriaOperator.Parse("Products=? AND ProductsGant=?", _onew.GetObject(_Products), _onew.GetObject(_ProductsGant)))

                                    For Each _ProductsGantRows As ProductsGantRows In _GroupCollectionElement_Collection
                                        Dim _Item As Long = _GanttControl.Items.get_FindItem(_ProductsGantRows.Oid.ToString, 0)
                                        If _Item = 0 Then
                                            _Item = _GanttControl.Items.InsertItem(_GroupItem, "")

                                            SetItemsAndItemData(_Item, _ProductsGantRows)
                                        End If
                                    Next
                                Next
                            End If

                    End Select
                End If
                If Not String.IsNullOrEmpty(_FocusedItem) Then
                    Dim _SelectedItem As Long = _GanttControl.Items.get_FindItem(_FocusedItem, 0)
                    If _SelectedItem > 0 Then
                        .Items.set_SelectItem(_SelectedItem, True)
                    End If
                End If
                .EndUpdate()

                _GantItemColumn_Array.Clear()
                _GantChange = True
            End With
        End If
    End Sub
    Private Sub SetItemBars(_item As Long, _ProductsGantRows As ProductsGantRows)
        For Each _ProductsGantRowsBar As ProductsGantRowsBar In _ProductsGantRows.ProductsGantRowsBar




        Next
    End Sub
    Private Sub aProductsGant_SetGantItemColumn_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aProductsGant_SetGantItemColumn.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

        Dim _ProductsGant_SetGantItemColumn_PopUp_CollectionSource As New CollectionSource(_onew, GetType(ProductsGant_SetGantItemColumn_PopUp))
        Dim _IModelListView As IModelListView = Application.Model.Views.GetNode("ProductsGant_ProductsGantRows_ListView")

        For Each _Column In _IModelListView.Columns
            Dim _ProductsGant_SetGantItemColumn_PopUp As New ProductsGant_SetGantItemColumn_PopUp(_onew.Session)
            With _ProductsGant_SetGantItemColumn_PopUp
                .ColumnCaption = _Column.Caption
                If _Column.Index <> -1 Then
                    .IsVisable = True
                Else
                    .IsVisable = False
                End If
                .Index = _Column.Index
                If _Column.GroupIndex <> -1 Then
                    .IsGroupColumn = True
                Else
                    .IsGroupColumn = False
                End If
                .Width = _Column.Width
                .GroupIndex = _Column.GroupIndex
            End With

            _ProductsGant_SetGantItemColumn_PopUp_CollectionSource.Add(_ProductsGant_SetGantItemColumn_PopUp)
        Next

        e.View = Application.CreateListView("ProductsGant_SetGantItemColumn_PopUp_ListView", _ProductsGant_SetGantItemColumn_PopUp_CollectionSource, False)
    End Sub
    Private Sub aProductsGant_SetGantItemColumn_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aProductsGant_SetGantItemColumn.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _IModelListView As IModelListView = Application.Model.Views.GetNode("ProductsGant_ProductsGantRows_ListView")

        For Each _ProductsGant_SetGantItemColumn_PopUp As ProductsGant_SetGantItemColumn_PopUp In TryCast(e.PopupWindow.View, ListView).CollectionSource.Collection
            For Each _Column In _IModelListView.Columns
                If _Column.Caption = _ProductsGant_SetGantItemColumn_PopUp.ColumnCaption Then
                    _Column.Index = _ProductsGant_SetGantItemColumn_PopUp.Index
                    If _ProductsGant_SetGantItemColumn_PopUp.IsGroupColumn Then
                        _Column.GroupIndex = _ProductsGant_SetGantItemColumn_PopUp.GroupIndex
                    Else
                        _Column.GroupIndex = -1
                    End If
                    _Column.Width = _ProductsGant_SetGantItemColumn_PopUp.Width
                    Exit For
                End If
            Next
        Next
        Application.SaveModelChanges()

        _GanttControl.BeginUpdate()
        For Each _Column As exontrol.EXG2ANTTLib.Column In _GanttControl.Columns
            For Each _ColumnModel In _IModelListView.Columns
                If _ColumnModel.Caption = _Column.Caption Then
                    _Column.Width = _ColumnModel.Width
                End If
            Next
        Next
        _GanttControl.EndUpdate()

    End Sub
    Private Sub _GanttControl_EditCloseEvent(sender As Object)
        Dim _Item As Long = _GanttControl.Items.get_SelectedItem(0)

        Dim _Oid As String = _GanttControl.Items.get_CellValue(_GanttControl.Items.get_SelectedItem(0), 0)
        If String.IsNullOrEmpty(_Oid) Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

            Dim _ProductsGantRows As ProductsGantRows = _ocur.FindObject(Of ProductsGantRows)(CriteriaOperator.Parse("Oid=?", _Oid))

            If _ProductsGantRows IsNot Nothing Then
                For Each _G2Column As exontrol.EXG2ANTTLib.Column In _GanttControl.Columns
                    Select Case _G2Column.Key
                        Case "ManualRotation"
                            _ProductsGantRows.ManualRotation = _GanttControl.Items.get_CellValue(_GanttControl.Items.get_SelectedItem(0), _G2Column.Index)
                        Case "CalculatedRotation"
                            _ProductsGantRows.CalculatedRotation = _GanttControl.Items.get_CellValue(_GanttControl.Items.get_SelectedItem(0), _G2Column.Index)
                    End Select
                Next

            End If
            _ocur.CommitChanges()
        End If
    End Sub
    Private Sub aProductsGant_CreateCustomersReport_CustomizePopupWindowParams1(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aProductsGant_CreateCustomersReport.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ReportDate As Date = GetSQLTime(_onew.Session).Date
        Dim _ProductsGantRows_Oid As String = _GanttControl.Items.get_CellValue(_GanttControl.Items.get_SelectedItem(0), 0)
        If String.IsNullOrEmpty(_ProductsGantRows_Oid) Then
            MsgBox("Nincs kiválasztva egyetlen sor sem, nem lehet azonosítani az Ügyfelet!", MsgBoxStyle.OkOnly, "Hiba!")
            Exit Sub
        End If
        Dim _ProductsGantRows_Selected As ProductsGantRows = _onew.FindObject(Of ProductsGantRows)(CriteriaOperator.Parse("Oid=?", _ProductsGantRows_Oid))

        Dim _ProductsGantCustomersReport_CollectionSource As New CollectionSource(_onew, GetType(ProductsGantCustomersReport))
        _ProductsGantCustomersReport_CollectionSource.BeginUpdateCriteria()
        _ProductsGantCustomersReport_CollectionSource.Criteria.Clear()
        _ProductsGantCustomersReport_CollectionSource.Criteria("First") = CriteriaOperator.Parse("GetDate(DateExecution)=? AND Customers.Oid=?", _ReportDate.Date, _ProductsGantRows_Selected.Customers.Oid)
        _ProductsGantCustomersReport_CollectionSource.EndUpdateCriteria()

        If _GanttControl.Items.get_SelectedItem(0) > 0 Then
            Dim _ProductsGantRows_Collection As New XPCollection(Of ProductsGantRows)(_onew.Session, CriteriaOperator.Parse("Customers=?", _ProductsGantRows_Selected.Customers))

            If _ProductsGantRows_Collection.Count > 0 Then
                For Each _ProductsGantRows As ProductsGantRows In _ProductsGantRows_Collection
                    Dim _ProductsGantCustomersReport As New ProductsGantCustomersReport(_onew.Session)
                    With _ProductsGantCustomersReport
                        .Customers = _ProductsGantRows_Selected.Customers
                        .Products = _ProductsGantRows.Products
                        .QTT = 0
                        .DateExecution = _ReportDate
                    End With

                    _ProductsGantCustomersReport_CollectionSource.Add(_ProductsGantCustomersReport)
                Next
            End If

        End If
        e.View = Application.CreateListView("ProductsGantCustomersReport_ListView_PopUp", _ProductsGantCustomersReport_CollectionSource, False)
    End Sub
    Private Sub aProductsGant_CreateCustomersReport_Execute_1(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aProductsGant_CreateCustomersReport.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _ProductsGant As ProductsGant = TryCast(View.SelectedObjects(0), ProductsGant)
        Dim _Customers As Customers = Nothing
        Dim _Products As Products = Nothing
        Dim _QTT As Double = 0
        Dim _DateExecution As Date = Date.MinValue


        For Each _ProductsGantCustomersReport As ProductsGantCustomersReport In TryCast(e.PopupWindow.View, ListView).CollectionSource.Collection

            If _ProductsGantCustomersReport.QTT > 0 Then

                Dim _ProductsGantCustomersReport_New As ProductsGantCustomersReport = _ocur.FindObject(Of ProductsGantCustomersReport)(CriteriaOperator.Parse("Customers=? AND Products=? AND DateExecution=?", _
                                                                                                                                            _ocur.GetObject(_ProductsGantCustomersReport.Customers), _
                                                                                                                                            _ocur.GetObject(_ProductsGantCustomersReport.Products), _
                                                                                                                                             _ProductsGantCustomersReport.DateExecution))
                If _ProductsGantCustomersReport_New Is Nothing Then
                    _ProductsGantCustomersReport_New = New ProductsGantCustomersReport(_ocur.Session)
                    _ProductsGantCustomersReport_New.Customers = _ocur.GetObject(_ProductsGantCustomersReport.Customers)
                    _ProductsGantCustomersReport_New.Products = _ocur.GetObject(_ProductsGantCustomersReport.Products)
                    _ProductsGantCustomersReport_New.QTT = _ocur.GetObject(_ProductsGantCustomersReport.QTT)
                    _ProductsGantCustomersReport_New.DateExecution = _ocur.GetObject(_ProductsGantCustomersReport.DateExecution)
                End If
               

                Dim _ProductsGantRows As ProductsGantRows = _ocur.FindObject(Of ProductsGantRows)(CriteriaOperator.Parse("ProductsGant=? AND Customers.Oid=? AND Products.Oid=?", _
                                                                            _ProductsGant, _ProductsGantCustomersReport_New.Customers.Oid, _ProductsGantCustomersReport_New.Products.Oid))
                If _ProductsGantRows IsNot Nothing Then
                    Dim _ProductsGantRowsBar As ProductsGantRowsBar = _ocur.FindObject(Of ProductsGantRowsBar) _
                                                        (CriteriaOperator.Parse("ProductsGantRows.Oid=? AND GetDate(FromDate)=? AND BarType=2", _
                                                                     _ProductsGantRows.Oid, _ProductsGantCustomersReport.DateExecution.Date))
                    If _ProductsGantRowsBar Is Nothing Then
                        _ProductsGantRowsBar = New ProductsGantRowsBar(_ocur.Session)
                        With _ProductsGantRowsBar
                            .ProductsGantRows = _ProductsGantRows
                            .FromDate = _ProductsGantCustomersReport.DateExecution
                            If _ProductsGantRowsBar.ProductsGantRows.Products.CustomersOrderPriority.Count > 0 Then
                                .ProductsGantRows.Products.CustomersOrderPriority.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))
                                .ToDate = .FromDate.AddDays(.ProductsGantRows.Products.CustomersOrderPriority(0).DeadlineWeek * 7)
                            Else
                                If _ProductsGantRowsBar.ProductsGantRows.CalculatedRotation > 0 Then
                                    .ToDate = .FromDate.AddDays(.ProductsGantRows.CalculatedRotation)
                                ElseIf _ProductsGantRowsBar.ProductsGantRows.ManualRotation > 0 Then
                                    .ToDate = .FromDate.AddDays(.ProductsGantRows.ManualRotation)
                                Else
                                    .ToDate = .FromDate.AddDays(1) 'temp
                                End If
                            End If
                            .BarType = eBarType.eReported
                            .Caption = _ProductsGantCustomersReport_New.QTT.ToString
                        End With
                    Else
                        With _ProductsGantRowsBar
                            .FromDate = _ProductsGantCustomersReport.DateExecution
                            If _ProductsGantRowsBar.ProductsGantRows.Products.CustomersOrderPriority.Count > 0 Then
                                .ProductsGantRows.Products.CustomersOrderPriority.Sorting.Add(New SortProperty("RowIndex", DB.SortingDirection.Ascending))
                                .ToDate = .FromDate.AddDays(.ProductsGantRows.Products.CustomersOrderPriority(0).DeadlineWeek * 7)
                            Else
                                If _ProductsGantRowsBar.ProductsGantRows.CalculatedRotation > 0 Then
                                    .ToDate = .FromDate.AddDays(.ProductsGantRows.CalculatedRotation)
                                ElseIf _ProductsGantRowsBar.ProductsGantRows.ManualRotation > 0 Then
                                    .ToDate = .FromDate.AddDays(.ProductsGantRows.ManualRotation)
                                Else
                                    .ToDate = .FromDate.AddDays(1) 'temp
                                End If
                            End If
                            .Caption = _ProductsGantCustomersReport_New.QTT.ToString
                        End With
                    End If
                End If
            End If
        Next
        _ocur.CommitChanges()

        Dim _ProductsGantCustomersReport_DEL_Collection As New XPCollection(Of ProductsGantCustomersReport)(_ocur.Session, CriteriaOperator.Parse("QTT='0'"))
        If _ProductsGantCustomersReport_DEL_Collection.Count > 0 Then
            _ocur.Delete(_ProductsGantCustomersReport_DEL_Collection)
            _ocur.CommitChanges()
        End If


    End Sub
    Private Sub aProductsGant_OpenProducts_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aProductsGant_OpenProducts.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

        Dim _ProductsGant As ProductsGant = TryCast(View.SelectedObjects(0), ProductsGant)

        If _GanttControl.Items.get_SelectedItem(0) > 0 Then

            Dim _ProductsGantRows_Oid As String = _GanttControl.Items.get_CellValue(_GanttControl.Items.get_SelectedItem(0), 0)
            Dim _ProductsGantRows As ProductsGantRows = _onew.FindObject(Of ProductsGantRows)(CriteriaOperator.Parse("Oid=?", _ProductsGantRows_Oid))
            If _ProductsGantRows IsNot Nothing Then
                e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Products_DetailView", True, _ProductsGantRows.Products)
            End If
        End If

    End Sub
    Private Sub GanttControl_Change(sender As Object, Item As Integer, ColIndex As Integer, ByRef NewValue As Object)
        If _GantChange Then
            Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
            Dim _IModelListView As IModelListView = Application.Model.Views.GetNode("ProductsGant_ProductsGantRows_ListView")
            If _GanttControl IsNot Nothing And _IModelListView IsNot Nothing Then

                Dim _ProductsGant As ProductsGant = TryCast(View.SelectedObjects(0), ProductsGant)
                Dim _ProductsGantRows_Oid As String = _GanttControl.Items.get_CellValue(Item, 0)
                Dim _ProductsGantRows As ProductsGantRows = _onew.FindObject(Of ProductsGantRows)(CriteriaOperator.Parse("Oid=?", _ProductsGantRows_Oid))
                Dim _ColumnID As String = ""
                If _ProductsGantRows IsNot Nothing Then

                    For Each _Column In _IModelListView.Columns
                        If _Column.Caption = _GanttControl.Columns(ColIndex).Caption Then
                            _ColumnID = _Column.Id
                            Exit For
                        End If
                    Next

                    If Not String.IsNullOrEmpty(_ColumnID) Then
                        Select Case _ColumnID
                            Case "CalculatedRotation"
                                _ProductsGantRows.CalculatedRotation = CType(NewValue, Double)
                            Case "ManualRotation"
                                _ProductsGantRows.ManualRotation = CType(NewValue, Double)
                        End Select

                        _onew.CommitChanges()
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub RefreshAction_Executing(sender As Object, e As CancelEventArgs)
        If _GanttControl IsNot Nothing Then
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            With _GanttControl
                .BeginUpdate()
                .Chart.FirstVisibleDate = GetSQLTime(_ocur.Session).ToString("yyyy.MM.dd")
                .EndUpdate()
            End With
        End If
    End Sub
    Private Sub GanttControl_LayoutChanged(sender As Object)
        If _GantChange Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _ProductsGantRowsViewItem As ListPropertyEditor = _DetailView.FindItem("ProductsGantRows")
            Dim _IModelListView As IModelListView = Application.Model.Views.GetNode("ProductsGant_ProductsGantRows_ListView")
            For Each _Column In _IModelListView.Columns
                For Each _G2Column As exontrol.EXG2ANTTLib.Column In _GanttControl.Columns
                    If _Column.Id = _G2Column.Key Then
                        _Column.Width = _G2Column.Width
                    End If
                Next
            Next
            Application.SaveModelChanges()
        End If
    End Sub
    Private Sub SetItemsAndItemData(_Item As Long, _ProductsGantRows As ProductsGantRows)
        For Each _G2Column As exontrol.EXG2ANTTLib.Column In _GanttControl.Columns
            Select Case _G2Column.Key
                Case "ProductGandRowsOid"
                    _GanttControl.Items.set_CellValue(_Item, _G2Column.Index, _ProductsGantRows.Oid.ToString)
                Case "Customers"
                    _GanttControl.Items.set_CellValue(_Item, _G2Column.Index, _ProductsGantRows.Customers.FullName)
                Case "Products"
                    _GanttControl.Items.set_CellValue(_Item, _G2Column.Index, _ProductsGantRows.Products.ShortName)
                Case "CalculatedRotation"
                    _GanttControl.Items.set_CellValue(_Item, _G2Column.Index, _ProductsGantRows.CalculatedRotation)
                Case "ManualRotation"
                    _GanttControl.Items.set_CellValue(_Item, _G2Column.Index, _ProductsGantRows.ManualRotation)
            End Select

        Next

        For Each _ProductsGantRowsBar As ProductsGantRowsBar In _ProductsGantRows.ProductsGantRowsBar
            Dim _Key As String = Guid.NewGuid.ToString
            Select Case _ProductsGantRowsBar.BarType
                Case eBarType.eAccounted
                    _GanttControl.Items.AddBar(CInt(_Item), "Accounted", _ProductsGantRowsBar.FromDate, _ProductsGantRowsBar.ToDate, _Key, _ProductsGantRowsBar.Caption)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanResize, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanMove, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanStartLink, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanBeLinked, False)
                Case eBarType.eOrdered
                    _GanttControl.Items.AddBar(CInt(_Item), "Ordered", _ProductsGantRowsBar.FromDate, _ProductsGantRowsBar.ToDate, _Key, _ProductsGantRowsBar.Caption)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanResize, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanMove, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanStartLink, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanBeLinked, False)
                Case eBarType.eReported
                    _GanttControl.Items.AddBar(CInt(_Item), "Reported", _ProductsGantRowsBar.FromDate, _ProductsGantRowsBar.ToDate, _Key, _ProductsGantRowsBar.Caption)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanResize, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanMove, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanStartLink, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanBeLinked, False)
                Case eBarType.eOrderArrive
                    _GanttControl.Items.AddBar(CInt(_Item), "OrderArrive", _ProductsGantRowsBar.ToDate, _ProductsGantRowsBar.ToDate, _Key, _ProductsGantRowsBar.Caption)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanResize, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanMove, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanStartLink, False)
                    _GanttControl.Items.set_ItemBar(CInt(_Item), _Key, exontrol.EXG2ANTTLib.ItemBarPropertyEnum.exBarCanBeLinked, False)
            End Select
        Next
    End Sub

    Private Sub _GanttControl_SelectionChanged(sender As Object)
        _FocusedItem = _GanttControl.Items.get_CellValue(_GanttControl.Items.FocusItem, 0).ToString
    End Sub

End Class
