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.SystemModule
Imports FarPoint.Win.Spread
Imports System.IO
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports DevExpress.ExpressApp.Editors
Imports System.Drawing
Imports System.Globalization

Public Class ImmovablesCostRecordExcelVC
    Inherits DevExpress.ExpressApp.ViewController

    Private _FpSpread As FpSpread
    Dim _WaitFormClass As New WaitFormClass
    Public Sub New()
        MyBase.New()


        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        If View.Id = "ImmovablesCostRecordExcel_ListView" Then
            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _ImmovablesCostRecordExcel As ImmovablesCostRecordExcel = _ocur.FindObject(Of ImmovablesCostRecordExcel)(CriteriaOperator.Parse("IsEditable=?", True))

            'If _ImmovablesCostRecordExcel IsNot Nothing Then
            '    Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            'End If
        End If

        If View.Id = "ImmovablesCostRecordExcel_DetailView" Then
            AddHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf SaveAction_Executing
            AddHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf SaveAction_Executing

            Frame.GetController(Of ImmovablesCostRecordExcelVC)().aTempLikor.Active.SetItemValue("", False)

            Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
            Dim _ImmovablesCostRecordExcel As ImmovablesCostRecordExcel = _ocur.FindObject(Of ImmovablesCostRecordExcel)(CriteriaOperator.Parse("IsEditable=?", True))

            If _ImmovablesCostRecordExcel IsNot Nothing Then
                Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            End If
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "ImmovablesCostRecordExcel_DetailView" Then
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAction.Executing, AddressOf SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndCloseAction.Executing, AddressOf SaveAction_Executing
            RemoveHandler Frame.GetController(Of ModificationsController)().SaveAndNewAction.Executing, AddressOf SaveAction_Executing

            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        End If

        If View.Id = "ImmovablesCostRecordExcel_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "ImmovablesCostRecordExcel_DetailView" Then

            Dim _ExcelControlViewItem As ViewItem
            _ExcelControlViewItem = TryCast(View, DetailView).GetItems(Of ExcelPropertyEditor)(0)
            If _ExcelControlViewItem IsNot Nothing Then
                AddHandler _ExcelControlViewItem.ControlCreated, AddressOf ExcelControlViewItem_ControlCreated
            End If

        End If
    End Sub
    Private Sub ExcelControlViewItem_ControlCreated(ByVal sender As Object, ByVal e As EventArgs)
        Dim itemControl As Object = (CType(sender, ViewItem)).Control

        _FpSpread = TryCast(itemControl, ExcelUserControl).FpSpreadControl

        If _FpSpread IsNot Nothing Then
            _FpSpread.EditModeReplace = True
            Dim _FileName As String = Path.GetTempPath & "OutSpread.xml"
            Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
            Dim _ImmovablesCostRecordExcel As ImmovablesCostRecordExcel = TryCast(View.SelectedObjects(0), ImmovablesCostRecordExcel)
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            If _ocur.Session.IsNewObject(_ImmovablesCostRecordExcel) = False Then
                If _ImmovablesCostRecordExcel IsNot Nothing Then
                    If _ImmovablesCostRecordExcel.ExcelFile IsNot Nothing Then
                        _ImmovablesCostRecordExcel.ExcelFile.SaveToStream(_StreamFile)
                        _StreamFile.Close()
                        _FpSpread.Open(_FileName)
                    End If
                End If
            End If
        End If


    End Sub
    Sub SaveAction_Executing(sender As Object, e As System.ComponentModel.CancelEventArgs)
        Dim _FileName As String = Path.GetTempPath & "InSpread.xml"
        Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
        Dim _ImmovablesCostRecordExcel As ImmovablesCostRecordExcel = TryCast(View.SelectedObjects(0), ImmovablesCostRecordExcel)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        If _ImmovablesCostRecordExcel IsNot Nothing Then
            If _FpSpread IsNot Nothing Then
                _FpSpread.Save(_StreamFile, False)
                _StreamFile.Close()
                Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)
                _ImmovablesCostRecordExcel.ExcelFile = New FileData(_ocur.Session)
                _ImmovablesCostRecordExcel.ExcelFile.LoadFromStream("Excel worksheet", _StreamFile2)
                _ImmovablesCostRecordExcel.Save()
                _StreamFile2.Close()
            End If
        End If
    End Sub
    Private Sub aInsertCostRecords_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aInsertCostRecords.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _Sort As New SortProperty("Immovables.UniqueObjects.Name", DB.SortingDirection.Ascending)
        Dim _SortRecordGroup As New SortProperty("ShortName", DB.SortingDirection.Ascending)
        Dim _SortRecordMainGroup As New SortProperty("ImmovablesCostRecordMainGroup.GroupName", DB.SortingDirection.Ascending)
        Dim _ImmovablesCostRecordExcel As ImmovablesCostRecordExcel = TryCast(View.SelectedObjects(0), ImmovablesCostRecordExcel)
        _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelImmovables.Sorting.Add(_Sort)
        _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelMainGroup.Sorting.Add(_SortRecordMainGroup)
        Dim _ImmovablesCostRecordGroup As XPCollection = _ocur.CreateCollection(GetType(ImmovablesCostRecordGroup), CriteriaOperator.Parse("1=2"))
        Dim _ImmovablesCostRecordMainGroup_Collection As XPCollection = _ocur.CreateCollection(GetType(ImmovablesCostRecordMainGroup), CriteriaOperator.Parse("1=2"))

        For Each _ImmovablesCostRecordExcelMainGroup As ImmovablesCostRecordExcelMainGroup In _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelMainGroup
            If _ImmovablesCostRecordExcelMainGroup.IsUseInExcel = True Then
                If _ImmovablesCostRecordExcelMainGroup.ImmovablesCostRecordMainGroup.ImmovablesCostRecordGroup.Count > 0 Then
                    _ImmovablesCostRecordMainGroup_Collection.Add(_ImmovablesCostRecordExcelMainGroup.ImmovablesCostRecordMainGroup)

                    For Each _ImmovablesCostRecordGroup_Act As ImmovablesCostRecordGroup In _ImmovablesCostRecordExcelMainGroup.ImmovablesCostRecordMainGroup.ImmovablesCostRecordGroup
                        _ImmovablesCostRecordGroup.Add(_ImmovablesCostRecordGroup_Act)
                    Next
                End If
            End If
        Next
        _ImmovablesCostRecordGroup.Sorting.Add(_SortRecordGroup)
        Dim _ik As Int16 = 0
        Dim _ColumnHeaderName As New ArrayList
        Dim _BackColor As Drawing.Color = Drawing.ColorTranslator.FromHtml("&Hfff0f0f0")
        Dim _Average As Double = 0

        _FpSpread.Save(Path.GetTempPath + "SaveSp.xml", False)
        Dim _FpSpread_Orig As FpSpread = New FpSpread()
        _FpSpread_Orig.Open(Path.GetTempPath + "SaveSp.xml")


        _FpSpread.Reset()

        _FpSpread.EditModeReplace = True

        _FpSpread.ActiveSheet.ColumnHeader.RowCount = 5
        _FpSpread.ActiveSheet.RowHeader.ColumnCount = 3
        _FpSpread.ActiveSheet.ColumnCount = 5 * _ImmovablesCostRecordGroup.Count
        _FpSpread.ActiveSheet.RowCount = _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelImmovables.Count

        'Oszlop fejléc
        _FpSpread.ActiveSheet.AddColumnHeaderSpanCell(0, 0, 1, (5 * _ImmovablesCostRecordGroup.Count))
        _FpSpread.ActiveSheet.ColumnHeader.Cells(0, 0).Text = "Leolvasások"

        '---------------------------Oszlofejléc felépítése----------------------------
        Dim _MainGroupIndex As Int16 = 0
        Dim _RecordGroupIndex As Int16 = 0
        For Each _ImmovablesCostRecordMainGroup As ImmovablesCostRecordMainGroup In _ImmovablesCostRecordMainGroup_Collection
            Dim _ImmovablesCostRecordGroup_Collection As XPCollection = _ocur.CreateCollection(GetType(ImmovablesCostRecordGroup), _
                                CriteriaOperator.Parse("ImmovablesCostRecordMainGroup.Oid=?", _ImmovablesCostRecordMainGroup.Oid))

            _FpSpread.ActiveSheet.AddColumnHeaderSpanCell(1, _MainGroupIndex, 1, _ImmovablesCostRecordGroup_Collection.Count * 5)
            _FpSpread.ActiveSheet.ColumnHeader.Cells(1, _MainGroupIndex).Text = _ImmovablesCostRecordMainGroup.GroupName

            _ImmovablesCostRecordGroup_Collection.Sorting.Add(_SortRecordGroup)
            For Each _ImmovablesCostRecordGroup_ACT As ImmovablesCostRecordGroup In _ImmovablesCostRecordGroup_Collection
                _FpSpread.ActiveSheet.AddColumnHeaderSpanCell(2, _MainGroupIndex + _RecordGroupIndex, 1, 5)
                _FpSpread.ActiveSheet.ColumnHeader.Cells(2, _MainGroupIndex + _RecordGroupIndex).Text = _ImmovablesCostRecordGroup_ACT.Oid.ToString
                _FpSpread.ActiveSheet.ColumnHeader.Rows(2).Visible = False

                _FpSpread.ActiveSheet.AddColumnHeaderSpanCell(3, _MainGroupIndex + _RecordGroupIndex, 1, 5)
                _FpSpread.ActiveSheet.ColumnHeader.Cells(3, _MainGroupIndex + _RecordGroupIndex).Text = _ImmovablesCostRecordGroup_ACT.ShortName

                _FpSpread.ActiveSheet.ColumnHeader.Cells(4, _MainGroupIndex + _RecordGroupIndex).Text = "Elõzõ"                     'Fix érték 1
                _FpSpread.ActiveSheet.ColumnHeader.Cells(4, _MainGroupIndex + _RecordGroupIndex + 1).Text = "Aktuális"                              'Fix érték 2
                _FpSpread.ActiveSheet.ColumnHeader.Cells(4, _MainGroupIndex + _RecordGroupIndex + 2).Text = "Különbség"                              'Fix érték 3
                _FpSpread.ActiveSheet.ColumnHeader.Cells(4, _MainGroupIndex + _RecordGroupIndex + 3).Text = "Egységár"                               'Fix érték 4
                _FpSpread.ActiveSheet.ColumnHeader.Cells(4, _MainGroupIndex + _RecordGroupIndex + 4).Text = "Érték"                                  'Fix érték 5

                If _ImmovablesCostRecordGroup_ACT.CostRecordBaseMode = eCostRecordBaseMode.eFix Then
                    _FpSpread.ActiveSheet.Columns(_MainGroupIndex + _RecordGroupIndex).Visible = False
                    _FpSpread.ActiveSheet.Columns(_MainGroupIndex + _RecordGroupIndex + 1).Visible = False
                    _FpSpread.ActiveSheet.Columns(_MainGroupIndex + _RecordGroupIndex + 2).Visible = False
                End If

                _RecordGroupIndex += 5
            Next
            _RecordGroupIndex = 0
            _MainGroupIndex += _ImmovablesCostRecordGroup_Collection.Count * 5
        Next


        '---------------------------Sor fejléc------------------------------------
        _FpSpread.ActiveSheet.AddRowHeaderSpanCell(0, 0, _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelImmovables.Count, 1)
        _FpSpread.ActiveSheet.RowHeader.Cells(0, 0).Column.Width = 60
        _FpSpread.ActiveSheet.RowHeader.Cells(0, 0).Text = "Ingatlanok"

        '----------------------------Sorfejléc felépítése--------------------------------
        For Each _ImmovablesCostRecordExcelImmovables As ImmovablesCostRecordExcelImmovables In _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelImmovables
            Dim _Immovables As Immovables = TryCast(_ImmovablesCostRecordExcelImmovables.Immovables, Immovables)
            _FpSpread.ActiveSheet.RowHeader.Cells(_ik, 2).Column.Width = 100
            _FpSpread.ActiveSheet.RowHeader.Cells(_ik, 2).Row.Height = 30
            _FpSpread.ActiveSheet.RowHeader.Cells(_ik, 1).Text = _Immovables.Oid.ToString
            _FpSpread.ActiveSheet.RowHeader.Columns(1).Visible = False                                              'Oid elrejtése
            _FpSpread.ActiveSheet.RowHeader.Cells(_ik, 2).Text = _Immovables.UniqueObjects.Name
            _ik += 1
        Next

        For _ik = 0 To _ImmovablesCostRecordExcel.ImmovablesCostRecordExcelImmovables.Count - 1
            Dim _Immovables As Immovables = _ocur.FindObject(Of Immovables)(CriteriaOperator.Parse("UniqueObjects.Name=?", _FpSpread.ActiveSheet.RowHeader.Cells(_ik, 2).Text))
            If _Immovables IsNot Nothing Then
                For _jk = 0 To _FpSpread.ActiveSheet.ColumnCount - 1 Step 5
                    Dim _ImmovablesCostRecordGroupOID As String = _FpSpread.ActiveSheet.ColumnHeader.Cells(2, _jk).Text
                    If _ImmovablesCostRecordGroupOID <> "" Then
                        Dim _ActImmovablesCostRecordGroup As ImmovablesCostRecordGroup = _ocur.FindObject(Of ImmovablesCostRecordGroup) _
                                                                                         (CriteriaOperator.Parse("Oid=?", _ImmovablesCostRecordGroupOID))
                        Dim _NumCellsType As New CellType.NumberCellType
                        Dim nfi As NumberFormatInfo = New CultureInfo("hu-HU", False).NumberFormat
                        _NumCellsType.DecimalSeparator = nfi.NumberDecimalSeparator
                        _NumCellsType.Separator = nfi.NumberGroupSeparator
                        _NumCellsType.DecimalPlaces = _ActImmovablesCostRecordGroup.DecimalPlace
                        _NumCellsType.NegativeRed = True

                        Select Case _ActImmovablesCostRecordGroup.CostRecordBaseMode
                            Case eCostRecordBaseMode.eFix
                                Select Case _ActImmovablesCostRecordGroup.eCostRecordMode
                                    Case eCostRecordMode.eNormal


                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).CellType = _NumCellsType
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 2).CellType = _NumCellsType
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 3).CellType = _NumCellsType
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 3).Value = _FpSpread_Orig.ActiveSheet.Cells(_ik, _jk + 3).Value
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 3).BackColor = _BackColor
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).CellType = _NumCellsType
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).Formula = (_FpSpread.ActiveSheet.Cells(_ik, _jk + 3).ToString)
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).Locked = True
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).TabStop = False


                                    Case eCostRecordMode.eQuarter
                                        Dim _ImmovablesQuater As Double = 0
                                        Select Case _Immovables.ImmovablesTypes
                                            Case eImmovablesTypes.eApartment
                                                _ImmovablesQuater = TryCast(_Immovables, Apartments).AreaNettoForCostRecord
                                            Case eImmovablesTypes.eContainer
                                                _ImmovablesQuater = TryCast(_Immovables, Container).AreaNettoForCostRecord
                                            Case eImmovablesTypes.eGarage
                                                _ImmovablesQuater = TryCast(_Immovables, Garages).AreaNettoForCostRecord
                                            Case eImmovablesTypes.eOffice
                                                _ImmovablesQuater = TryCast(_Immovables, Offices).AreaNettoForCostRecord
                                            Case eImmovablesTypes.eStudentHostel
                                                _ImmovablesQuater = 1 'TryCast(_ImmovablesCR.Immovables, StudentHostel).????
                                        End Select

                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).Value = _ImmovablesQuater
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).CellType = _NumCellsType
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 2).CellType = _NumCellsType
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 3).CellType = _NumCellsType
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 3).Value = _FpSpread_Orig.ActiveSheet.Cells(_ik, _jk + 3).Value
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 3).BackColor = _BackColor
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).CellType = _NumCellsType
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).Formula = (_FpSpread.ActiveSheet.Cells(_ik, _jk + 1).ToString) & _
                                                 "*" & (_FpSpread.ActiveSheet.Cells(_ik, _jk + 3).ToString)
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).Locked = True
                                        _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).TabStop = False

                                End Select
                            Case eCostRecordBaseMode.eCosuption
                                Select Case _ActImmovablesCostRecordGroup.eCostRecordMode
                                    Case eCostRecordMode.eNormal
                                        Dim _ActImmovablesCostRecord As ImmovablesCostRecord = _ocur.FindObject(Of ImmovablesCostRecord) _
                                                                (CriteriaOperator.Parse("ImmovablesCostRecordGroup=? AND Immovables=?", _ActImmovablesCostRecordGroup, _Immovables))

                                        If _ActImmovablesCostRecord IsNot Nothing Then
                                            Dim _PrevEvents As ImmovablesCostRecordEvents
                                            If _ActImmovablesCostRecord.ImmovablesCostRecordEvents.Count > 0 Then
                                                Dim _SortEvents As New SortProperty("ReadDate", DB.SortingDirection.Descending)
                                                _ActImmovablesCostRecord.ImmovablesCostRecordEvents.Sorting.Add(_SortEvents)
                                                _PrevEvents = _ActImmovablesCostRecord.ImmovablesCostRecordEvents(0)

                                                If _PrevEvents.IsReader = True Then
                                                    _FpSpread.ActiveSheet.Cells(_ik, _jk).Value = 0
                                                    _FpSpread.ActiveSheet.Cells(_ik, _jk).Tag = _PrevEvents.ImmovablesCostRecord.Oid.ToString
                                                    _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).Value = _PrevEvents.ReadedValue
                                                    _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).Tag = _PrevEvents.Oid.ToString
                                                    _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).CellType = _NumCellsType
                                                Else
                                                    _FpSpread.ActiveSheet.Cells(_ik, _jk).Value = _PrevEvents.ReadedValue
                                                    _FpSpread.ActiveSheet.Cells(_ik, _jk).Tag = _PrevEvents.ImmovablesCostRecord.Oid.ToString

                                                    _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).Value = 0
                                                    _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).CellType = _NumCellsType
                                                    _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).BackColor = _BackColor
                                                End If

                                            Else
                                                _FpSpread.ActiveSheet.Cells(_ik, _jk).Value = 0
                                                _FpSpread.ActiveSheet.Cells(_ik, _jk).Tag = _ActImmovablesCostRecord.Oid.ToString

                                            End If
                                            _FpSpread.ActiveSheet.Cells(_ik, _jk).Locked = True
                                            _FpSpread.ActiveSheet.Cells(_ik, _jk).TabStop = False
                                            _FpSpread.ActiveSheet.Cells(_ik, _jk).CellType = _NumCellsType

                                            _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).CellType = _NumCellsType
                                            _FpSpread.ActiveSheet.Cells(_ik, _jk + 2).CellType = _NumCellsType
                                            _FpSpread.ActiveSheet.Cells(_ik, _jk + 2).Formula = (_FpSpread.ActiveSheet.Cells(_ik, _jk + 1).ToString) & _
                                                                                 "-" & (_FpSpread.ActiveSheet.Cells(_ik, _jk).ToString)

                                            _FpSpread.ActiveSheet.Cells(_ik, _jk + 2).Locked = True
                                            _FpSpread.ActiveSheet.Cells(_ik, _jk + 2).TabStop = False

                                            _FpSpread.ActiveSheet.Cells(_ik, _jk + 3).Value = _FpSpread_Orig.ActiveSheet.Cells(_ik, _jk + 3).Value
                                            _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).CellType = _NumCellsType
                                            _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).Formula = (_FpSpread.ActiveSheet.Cells(_ik, _jk + 3).ToString) & _
                                                                                "*" & (_FpSpread.ActiveSheet.Cells(_ik, _jk + 2).ToString)
                                            _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).Locked = True
                                            _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).TabStop = False

                                        End If
                                    Case eCostRecordMode.eQuarter
                                        'Beep()
                                        MsgBox("---===>>> W T F <<<===---")
                                End Select
                        End Select
                    End If
                Next
            End If
        Next

        If File.Exists(Path.GetTempPath + "SaveSp.xml") Then
            File.Delete(Path.GetTempPath + "SaveSp.xml")
        End If
        _FpSpread.Refresh()
    End Sub
    Private Sub aFinalizeCostRecords_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aFinalizeCostRecords.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _ImmovablesCostRecordExcel As ImmovablesCostRecordExcel = TryCast(View.SelectedObjects(0), ImmovablesCostRecordExcel)
        Dim _ColumnCount As Int16 = _FpSpread.ActiveSheet.ColumnCount
        Dim _RowCount As Int16 = _FpSpread.ActiveSheet.RowCount
        Dim _DelImmovablesCostRecordEvents_Collection As New ArrayList
        _WaitFormClass.Initwork(1, 1, _RowCount)
        For _ik = 0 To _RowCount - 1
            Dim _Immovables As Immovables = _ocur.FindObject(Of Immovables)(CriteriaOperator.Parse("UniqueObjects.Name=?", _FpSpread.ActiveSheet.RowHeader.Cells(_ik, 2).Text))
            For _jk = 0 To _ColumnCount - 1 Step 5
                Dim _ImmovablesCostRecordGroupOID As String = _FpSpread.ActiveSheet.ColumnHeader.Cells(2, _jk).Text
                If _ImmovablesCostRecordGroupOID <> "" Then
                    Dim _ActImmovablesCostRecordGroup As ImmovablesCostRecordGroup = _ocur.FindObject(Of ImmovablesCostRecordGroup) _
                                                                                     (CriteriaOperator.Parse("Oid=?", _ImmovablesCostRecordGroupOID))
                    Dim _ActImmovablesCostRecord As ImmovablesCostRecord = _ocur.FindObject(Of ImmovablesCostRecord) _
                                                                (CriteriaOperator.Parse("ImmovablesCostRecordGroup=? AND Immovables=?", _ActImmovablesCostRecordGroup, _Immovables))
                    If _ActImmovablesCostRecord IsNot Nothing Then
                        Dim _NewImmovablesCostRecordEvents As ImmovablesCostRecordEvents = _ocur.CreateObject(Of ImmovablesCostRecordEvents)()
                        _NewImmovablesCostRecordEvents.ReadedValue = _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).Value
                        If _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).Tag IsNot Nothing Then
                            Dim _DelImmovablesCostRecordEvents As ImmovablesCostRecordEvents = _ocur.FindObject(Of ImmovablesCostRecordEvents)(CriteriaOperator.Parse("Oid=?", _FpSpread.ActiveSheet.Cells(_ik, _jk + 1).Tag.ToString))

                            _NewImmovablesCostRecordEvents.ReadDate = _ImmovablesCostRecordExcel.DateExecution '_DelImmovablesCostRecordEvents.ReadDate

                            _DelImmovablesCostRecordEvents_Collection.Add(_DelImmovablesCostRecordEvents)
                        Else
                            _NewImmovablesCostRecordEvents.ReadDate = _ImmovablesCostRecordExcel.DateExecution 'GetSQLTime(_ocur.Session)
                        End If

                        _NewImmovablesCostRecordEvents.ImmovablesCostRecordExcel = _ImmovablesCostRecordExcel
                        _NewImmovablesCostRecordEvents.ImmovablesCostRecord = _ActImmovablesCostRecord
                        _NewImmovablesCostRecordEvents.ReadedValuePrev = _FpSpread.ActiveSheet.Cells(_ik, _jk).Value
                        _NewImmovablesCostRecordEvents.ReadedValueDiff = _FpSpread.ActiveSheet.Cells(_ik, _jk + 2).Value
                        _NewImmovablesCostRecordEvents.ReadedValuePrice = _FpSpread.ActiveSheet.Cells(_ik, _jk + 3).Value
                        _NewImmovablesCostRecordEvents.ReadedValueSumPrice = _FpSpread.ActiveSheet.Cells(_ik, _jk + 4).Value
                        _NewImmovablesCostRecordEvents.ReadIndex += 1
                    End If
                End If
            Next
            _WaitFormClass.DoWork()
            System.Windows.Forms.Application.DoEvents()
        Next
        _ocur.Delete(_DelImmovablesCostRecordEvents_Collection)
        _ocur.CommitChanges()

        _Waitformclass.FinalWork()
        _FpSpread.Refresh()
        _FpSpread.Enabled = False
    End Sub
    Private Sub aImportCostRecords_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aImportCostRecords.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _cs As String
        Dim _ik As Int16 = 0
        Dim _Count As Long = 0
        Dim _Rtime As New ArrayList
        Dim _SID As New ArrayList
        Dim _WH As New ArrayList
        Dim _QM1 As New ArrayList
        Dim _QM2 As New ArrayList
        Dim _ARAM As New ArrayList
        Dim _ColumnHeaderName As New ArrayList
        Dim _BackColor As Drawing.Color = Drawing.ColorTranslator.FromHtml("&Hfff0f0f0")
        'Dim _NumCellsType As New CellType.NumberCellType

        'Dim nfi As NumberFormatInfo = New CultureInfo("hu-HU", False).NumberFormat
        '_NumCellsType.DecimalSeparator = nfi.NumberDecimalSeparator
        '_NumCellsType.Separator = nfi.NumberGroupSeparator
        '_NumCellsType.DecimalPlaces = 2
        '_NumCellsType.NegativeRed = True

        Dim _ActImmovables As XPCollection = _ocur.CreateCollection(GetType(Immovables), CriteriaOperator.Parse("1=2"))
        Dim _ImmovablesCostRecordGroup As XPCollection = _ocur.CreateCollection(GetType(ImmovablesCostRecordGroup), CriteriaOperator.Parse("1=1"))
        Dim _OpenFileDialog As New OpenFileDialog
        If _OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim _xlsFile As String = _OpenFileDialog.FileName
            _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
            _cs += "Data Source=" & _xlsFile.ToString & ";"
            _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
            Dim conn As New OleDbConnection(_cs)
            Dim cmd As New OleDbCommand
            Dim dr As OleDbDataReader
            Dim dr1 As OleDbDataReader
            Dim mSQL As String
            Dim _UpdatedCount As Long = 0


            Try

                mSQL = "select * from [Munka1$]"
                cmd.CommandTimeout = 60
                cmd.Connection = conn
                cmd.CommandType = CommandType.Text
                cmd.CommandText = mSQL
                conn.Open()

                If conn.State = ConnectionState.Open Then
                    dr1 = cmd.ExecuteReader(CommandBehavior.SingleResult)

                    While dr1.Read
                        If dr1(0).ToString <> "" And dr1(0).ToString <> "RTIME" Then
                            _Count += 1
                        End If
                    End While
                    dr1.Close()

                    dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                    _WaitFormClass.Initwork(1, 1, _Count)
                    While dr.Read
                        If dr(0).ToString <> "" And dr(0).ToString <> "RTIME" Then

                            _Rtime.Add(dr(0).ToString) '"RTIME"
                            _SID.Add(dr(1).ToString) '"SID"
                            _WH.Add(dr(4).ToString) '"WH"
                            _QM1.Add(dr(2).ToString) '"QH[1]"
                            _QM2.Add(dr(3).ToString) '"QH[2]"
                            _ARAM.Add(dr(5).ToString) '"ARAM"
                            _WaitFormClass.DoWork()
                            System.Windows.Forms.Application.DoEvents()
                        End If
                    End While

                    dr.Close()
                End If
                conn.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
                conn.Close()
            Finally
                _Waitformclass.FinalWork()
            End Try
        End If


        Try

            If _FpSpread.ActiveSheet.RowCount = 1 And _FpSpread.ActiveSheet.ColumnCount = 1 Then Throw New Exception("*Üres az Excel file, nem lehet adatokat importálni!")
            Dim _ImmovablesCostRecordExcel As ImmovablesCostRecordExcel = TryCast(View.SelectedObjects(0), ImmovablesCostRecordExcel)
            Dim _ImmovablesCostRecordExcelImmovables As ImmovablesCostRecordExcelImmovables
            For _ik = 0 To _SID.Count - 1
                Dim _ImmovablesCostRecord_Collection As XPCollection = _ocur.CreateCollection(GetType(ImmovablesCostRecord), _
                                CriteriaOperator.Parse("SID=?", _SID(_ik)))
                _WaitFormClass.Initwork(1, 1, _ImmovablesCostRecord_Collection.Count)
                For Each _ImmovablesCostRecord As ImmovablesCostRecord In _ImmovablesCostRecord_Collection

                    _ImmovablesCostRecordExcelImmovables = _ocur.FindObject(Of ImmovablesCostRecordExcelImmovables)(CriteriaOperator.Parse("Immovables=? AND ImmovablesCostRecordExcel=?", _
                                                                                                                                               _ImmovablesCostRecord.Immovables, _ImmovablesCostRecordExcel))
                    If _ImmovablesCostRecordExcelImmovables IsNot Nothing Then
                        For _jk = 0 To _FpSpread.ActiveSheet.RowCount - 1
                            If _FpSpread.ActiveSheet.RowHeader.Cells(_jk, 1).Text = _ImmovablesCostRecordExcelImmovables.Immovables.Oid.ToString Then

                                For _lk = 0 To _FpSpread.ActiveSheet.ColumnCount - 1 Step 5
                                    If _FpSpread.ActiveSheet.ColumnHeader.Cells(2, _lk).Text = _ImmovablesCostRecord.ImmovablesCostRecordGroup.Oid.ToString Then
                                        Dim _NumCellsType As New CellType.NumberCellType
                                        Dim nfi As NumberFormatInfo = New CultureInfo("hu-HU", False).NumberFormat
                                        _NumCellsType.DecimalSeparator = nfi.NumberDecimalSeparator
                                        _NumCellsType.Separator = nfi.NumberGroupSeparator
                                        _NumCellsType.DecimalPlaces = _ImmovablesCostRecord.ImmovablesCostRecordGroup.DecimalPlace
                                        _NumCellsType.NegativeRed = True

                                        _FpSpread.ActiveSheet.Cells(_jk, _lk + 1).CellType = _NumCellsType
                                        _FpSpread.ActiveSheet.Cells(_jk, _lk + 2).CellType = _NumCellsType
                                        Select Case _ImmovablesCostRecord.SIDChild
                                            Case eSIDChild.SID_QM1
                                                _FpSpread.ActiveSheet.Cells(_jk, _lk + 1).Value = Convert.ToDouble(_QM1(_ik))
                                            Case eSIDChild.SID_QM2
                                                _FpSpread.ActiveSheet.Cells(_jk, _lk + 1).Value = Convert.ToDouble(_QM2(_ik))
                                            Case eSIDChild.SID_WH
                                                _FpSpread.ActiveSheet.Cells(_jk, _lk + 1).Value = Convert.ToDouble(_WH(_ik))
                                            Case eSIDChild.SID_ARAM
                                                _FpSpread.ActiveSheet.Cells(_jk, _lk + 1).Value = Convert.ToDouble(_ARAM(_ik))
                                            Case eSIDChild.SID_NONE
                                        End Select
                                    End If
                                Next
                            End If
                        Next
                    End If
                Next
                _WaitFormClass.DoWork()
                System.Windows.Forms.Application.DoEvents()
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly)
        Finally
            _Waitformclass.FinalWork()
        End Try

    End Sub
  
    Private Sub aTempLikor_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aTempLikor.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As UnitOfWork = New UnitOfWork(_ocur.Session.DataLayer)
        Dim _cs As String
        Dim _ImmovablesName As New ArrayList
        Dim _SID As New ArrayList
        Dim _OpenFileDialog As New OpenFileDialog
        If _OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then

            Dim _xlsFile As String = _OpenFileDialog.FileName

            _cs = "Provider=Microsoft.ACE.OLEDB.12.0;"
            _cs += "Data Source=" & _xlsFile.ToString & ";"
            _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
            Dim conn As New OleDbConnection(_cs)
            Dim cmd As New OleDbCommand
            Dim dr As OleDbDataReader
            Dim mSQL As String
            Dim _Count As Long = 0
            Dim _UpdatedCount As Long = 0
            Try

                mSQL = "select * from [Sheet$]"
                cmd.CommandTimeout = 60
                cmd.Connection = conn
                cmd.CommandType = CommandType.Text
                cmd.CommandText = mSQL
                conn.Open()

                If conn.State = ConnectionState.Open Then

                    dr = cmd.ExecuteReader(CommandBehavior.SingleResult)

                    While dr.Read
                        If dr(0).ToString <> "" Or dr(0).ToString <> "RTIME" Then
                            '_form.ProgressBarControl1.Properties.Maximum = _Count

                            _ImmovablesName.Add(dr(0).ToString)
                            _SID.Add(dr(1).ToString)

                            '_WaitFormClass.DoWork
                            'System.Windows.Forms.Application.DoEvents()
                        End If
                    End While
                    dr.Close()
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

        Dim _ImmovablesCostRecordGroup As XPCollection = _ocur.CreateCollection(GetType(ImmovablesCostRecordGroup), CriteriaOperator.Parse("1=1"))
        Dim _ik As Int16 = 0
        For Each _ActImmo In _ImmovablesName
            Dim _Immo As Immovables = _ocur.FindObject(Of Immovables)(CriteriaOperator.Parse("UniqueObjects.Name=?", _ActImmo))
            If _Immo IsNot Nothing Then
                For Each _ActImmovablesCostRecordGroup In _ImmovablesCostRecordGroup
                    Dim _ImmovablesCostRecord As ImmovablesCostRecord = _ocur.CreateObject(Of ImmovablesCostRecord)()
                    _ImmovablesCostRecord.Immovables = _ocur.GetObjectByKey(Of Immovables)(_Immo.Oid)
                    _ImmovablesCostRecord.SID = _SID(_ik)
                    _ImmovablesCostRecord.ImmovablesCostRecordGroup = _ActImmovablesCostRecordGroup
                    _ImmovablesCostRecord.ShortName = TryCast(_ActImmovablesCostRecordGroup, ImmovablesCostRecordGroup).ShortName

                    _ImmovablesCostRecord.Save()
                Next
            End If
            _ik += 1
        Next
        _ocur.CommitChanges()
    End Sub
    Private Sub aSetUnitPrice_CustomizePopupWindowParams(sender As System.Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs) Handles aSetUnitPrice.CustomizePopupWindowParams
        Dim _ocur As Xpo.XPObjectSpace = View.ObjectSpace
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _SetUnitPrice_PopUp As SetUnitPrice_PopUp = _onew.CreateObject(Of SetUnitPrice_PopUp)()
        _SetUnitPrice_PopUp.UnitPrice = 1

        e.View = Application.CreateDetailView(_onew, "SetUnitPrice_PopUp_DetailView", False, _SetUnitPrice_PopUp)
    End Sub
    Private Sub aSetUnitPrice_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs) Handles aSetUnitPrice.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _SetUnitPrice_PopUp As SetUnitPrice_PopUp = TryCast(e.PopupWindow.View.SelectedObjects(0), SetUnitPrice_PopUp)
        Dim _Unitprice As Double = _SetUnitPrice_PopUp.UnitPrice
        Dim _ImmovablesCostRecordGroup As ImmovablesCostRecordGroup = _ocur.GetObjectByKey(Of ImmovablesCostRecordGroup)(_SetUnitPrice_PopUp.ImmovablesCostRecordGroup.Oid)
        Dim _ColumnIndex As Int16 = 0
        For _ik As Int16 = 0 To _FpSpread.ActiveSheet.ColumnHeader.Columns.Count / 5 - 1
            If _FpSpread.ActiveSheet.ColumnHeader.Cells(2, _ik * 5).Text = _ImmovablesCostRecordGroup.Oid.ToString Then
                _ColumnIndex = _ik * 5 + 3
                Exit For
            End If
        Next
        If _ColumnIndex <> 0 Then
            For _ik As Int16 = 0 To _FpSpread.ActiveSheet.RowCount - 1
                _FpSpread.ActiveSheet.Cells(_ik, _ColumnIndex).Value = _Unitprice
            Next

        Else
            Throw New Exception("*Nem lehet beazonosítani a mérőórát!")
        End If
    End Sub

End Class
