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.Data.Filtering

Imports DevExpress.XtraReports.UI
Imports System.Drawing.Printing
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Reports
Imports System.IO
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.XtraPrinting
'Imports DevExpress.XtraReports.UI
Imports DevExpress.XtraPrinting.Preview

Public Class LiquidAssetsReturnVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Frame.GetController(Of LiquidAssetsReturnVC).aPrintLiquidAssetsReturn_Cassa.Active.SetItemValue("", False)
        Frame.GetController(Of LiquidAssetsReturnVC).aGLCassaClose.Active.SetItemValue("", False)
        Frame.GetController(Of LiquidAssetsReturnVC).aGLCassaCloseBack.Active.SetItemValue("", False)
        Frame.GetController(Of LiquidAssetsReturnVC).aGLCassaClosePreviewPDF.Active.SetItemValue("", False)

        If View.Id = "LiquidAssetsReturnHeader_ListView_Cassa" Then
            Frame.GetController(Of LiquidAssetsReturnVC).aPrintLiquidAssetsReturn_Cassa.Active.SetItemValue("", True)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", False)
        End If

        If View.Id = "LiquidAssetsReturnHeader_DetailView_Cassa" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of LiquidAssetsReturnVC).aPrintLiquidAssetsReturn_Cassa.Active.SetItemValue("", True)

            Dim _LiquidAssetsReturnHeader As LiquidAssetsReturnHeader = TryCast(View.SelectedObjects(0), LiquidAssetsReturnHeader)
            If _LiquidAssetsReturnHeader IsNot Nothing Then
                If _LiquidAssetsReturnHeader.GLClosingTime IsNot Nothing Then
                    Frame.GetController(Of ModificationsController).Active.SetItemValue("", False)
                Else
                    Frame.GetController(Of LiquidAssetsReturnVC).aGLCassaClose.Active.SetItemValue("", True)
                End If
            End If
            AddHandler Frame.GetController(Of LiquidAssetsReturnVC).aPrintLiquidAssetsReturn_Cassa.Executing, AddressOf LiquidAssetsReturnHeader_ListView_Cassa_PrintLiquidAssetsReturn_Cassa_Executing
            AddHandler Frame.GetController(Of LiquidAssetsReturnVC).aGLCassaClose.Executing, AddressOf LiquidAssetsReturnHeader_ListView_Cassa_GLCassaClose_Executing
        End If

        If View.Id = "LiquidAssetsReturnHeader_ListView_Cassa_Closed" Then
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of LiquidAssetsReturnVC).aPrintLiquidAssetsReturn_Cassa.Active.SetItemValue("", True)
            Frame.GetController(Of LiquidAssetsReturnVC).aGLCassaCloseBack.Active.SetItemValue("", True)
            Frame.GetController(Of LiquidAssetsReturnVC).aGLCassaClosePreviewPDF.Active.SetItemValue("", True)
        End If

        If View.Id = "LiquidAssetsReturnHeader_LiquidAssetsReturnRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", False)
        End If

        If View.Id = "LiquidAssetsReturnHeader_LiquidAssetsReturnDenomination_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
        Frame.GetController(Of LiquidAssetsReturnVC).aPrintLiquidAssetsReturn_Cassa.Active.SetItemValue("", False)

        If View.Id = "LiquidAssetsReturnHeader_ListView_Cassa" Then
            Frame.GetController(Of LiquidAssetsReturnVC).aPrintLiquidAssetsReturn_Cassa.Active.SetItemValue("", False)
            Frame.GetController(Of CloneObject.CloneObjectViewController).Active.SetItemValue("", True)

        End If

        If View.Id = "LiquidAssetsReturnHeader_DetailView_Cassa" Then
            Frame.GetController(Of LiquidAssetsReturnVC).aPrintLiquidAssetsReturn_Cassa.Active.SetItemValue("", False)
            Frame.GetController(Of LiquidAssetsReturnVC).aGLCassaClose.Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            RemoveHandler Frame.GetController(Of LiquidAssetsReturnVC).aPrintLiquidAssetsReturn_Cassa.Executing, AddressOf LiquidAssetsReturnHeader_ListView_Cassa_PrintLiquidAssetsReturn_Cassa_Executing
            RemoveHandler Frame.GetController(Of LiquidAssetsReturnVC).aGLCassaClose.Executing, AddressOf LiquidAssetsReturnHeader_ListView_Cassa_GLCassaClose_Executing
        End If
        If View.Id = "LiquidAssetsReturnHeader_LiquidAssetsReturnRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", True)
        End If

        If View.Id = "LiquidAssetsReturnHeader_LiquidAssetsReturnDenomination_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.LinkUnlinkController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.NewObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.DeleteObjectsViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.CloneObject.CloneObjectViewController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ExportController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.RecordsNavigationController)().Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aGetReturn_Cassa_LiquidAssetsReturn_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGetReturn_Cassa.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _uow As New UnitOfWork(_ocur.Session.DataLayer)
        Dim _LiquidAssetsReturnHeader As LiquidAssetsReturnHeader
        Dim _LiquidAssetsReturnRows As LiquidAssetsReturnRows
        Dim _GLCassa As GLCassa

        _LiquidAssetsReturnHeader = _uow.GetObjectByKey(Of LiquidAssetsReturnHeader)(TryCast(View.SelectedObjects(0), LiquidAssetsReturnHeader).Oid)
        If _LiquidAssetsReturnHeader IsNot Nothing Then
            _uow.Delete(_LiquidAssetsReturnHeader.LiquidAssetsReturnRows)
            Dim _GLCassa_Collection As New XPCollection(Of GLCassa)(_uow, _
                    CriteriaOperator.Parse("GetDate(DateExecution)>=? and GetDate(DateExecution)<=? and LiquidAssets_Cassa=? and DocumentNumber<>''", _
                                           _LiquidAssetsReturnHeader.DateFrom.Date, _LiquidAssetsReturnHeader.DateTo.Date, _LiquidAssetsReturnHeader.LiquidAssets))

            _GLCassa_Collection.Sorting.Add(New SortProperty("DateExecution", DB.SortingDirection.Ascending))
            RaiseEvent InitWork(1, 1, _GLCassa_Collection.Count)
            For Each _GLCassa In _GLCassa_Collection
                RaiseEvent DoWork()
                _LiquidAssetsReturnRows = New LiquidAssetsReturnRows(_uow)
                _LiquidAssetsReturnRows.LiquidAssetsReturnHeader = _uow.GetObjectByKey(Of LiquidAssetsReturnHeader)(_LiquidAssetsReturnHeader.Oid)
                _LiquidAssetsReturnRows.GLCassa = _GLCassa
            Next
            _uow.CommitChanges()
            _LiquidAssetsReturnHeader.RecalculateBallance()
            _uow.CommitChanges()
            RaiseEvent FinalWork

            Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
        End If
    End Sub
    Private Sub aPrintLiquidAssetsReturn_Cassa_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aPrintLiquidAssetsReturn_Cassa.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _LiquidAssetsReturnHeader As LiquidAssetsReturnHeader
        Dim _ReportData As ReportData

        If e.SelectedObjects.Count > 0 Then
            _LiquidAssetsReturnHeader = e.SelectedObjects.Item(0)
            Dim _CS As New CollectionSource(_ocur, GetType(LiquidAssetsReturnRows))
            _CS.BeginUpdateCriteria()
            _CS.Criteria.Clear()
            _CS.Criteria.Add("Empty", CriteriaOperator.Parse("LiquidAssetsReturnHeader.Oid =?", _LiquidAssetsReturnHeader.Oid))
            _CS.EndUpdateCriteria()

            'If _CS.List.Count = 0 Then
            '    Dim _ReportData_new As ReportData
            '    _ReportData_new = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid = '37'"))
            '    If _LiquidAssetsReturnHeader.LiquidAssets.ReportData_Return Is Nothing Then
            '        _ocur.Rollback()
            '        Throw New Exception("*Nincs megadva a pénztári forgalmi nyomtatvány !")
            '    End If
            '    If _ReportData_new IsNot Nothing Or _LiquidAssetsReturnHeader IsNot Nothing Then
            '        Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData_new, CriteriaOperator.Parse("Oid =?", _LiquidAssetsReturnHeader.Oid), True)
            '    End If
            'Else
            If _LiquidAssetsReturnHeader.LiquidAssets.ReportData_Return Is Nothing Then
                _ocur.Rollback()
                Throw New Exception("*Nincs megadva a pénztári forgalmi nyomtatvány !")
            End If
            _ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _LiquidAssetsReturnHeader.LiquidAssets.ReportData_Return.Oid), True)
            If _ReportData Is Nothing Or _LiquidAssetsReturnHeader Is Nothing Then
            Else
                Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("LiquidAssetsReturnHeader =?", _LiquidAssetsReturnHeader.Oid), True)
            End If
            'End If
        End If
    End Sub
    Private Sub aGLCassaClose_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGLCassaClose.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCassa_Collection As New XPCollection(Of GLCassa)
        Dim _LiquidAssetsReturnHeader As LiquidAssetsReturnHeader = TryCast(View.SelectedObjects(0), LiquidAssetsReturnHeader)
        Dim _GLClosingTime As GLClosingTime
        Dim _GLCassa As GLCassa
        If _LiquidAssetsReturnHeader IsNot Nothing Then
            _GLCassa_Collection = New XPCollection(Of GLCassa)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                _ocur.Session, CriteriaOperator.Parse("isnull(GLClosingTime)=False and LiquidAssets_Cassa.Oid=? and DateExecution >=? and DateExecution<=?", _
                                _LiquidAssetsReturnHeader.LiquidAssets.Oid, _
                                _LiquidAssetsReturnHeader.DateFrom.Date, _
                                DateAdd(DateInterval.Minute, 1439, _LiquidAssetsReturnHeader.DateTo.Date)))

            If _GLCassa_Collection.Count > 0 Then
                Throw New Exception("*Van olyan pénztári tétel amely már ezen időszakon belül le lett zárva !")
            End If

            _GLCassa_Collection = New XPCollection(Of GLCassa)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                _ocur.Session, CriteriaOperator.Parse("IsEditable=True and LiquidAssets_Cassa.Oid=? and DateExecution >=? and DateExecution<=?", _
                                _LiquidAssetsReturnHeader.LiquidAssets.Oid, _
                                _LiquidAssetsReturnHeader.DateFrom.Date, _
                                DateAdd(DateInterval.Minute, 1439, _LiquidAssetsReturnHeader.DateTo.Date)))

            If _GLCassa_Collection.Count > 0 Then
                Throw New Exception("*Van olyan pénztári tétel amely szerkeszthető ezen időszakon belül !")
            End If

            _GLClosingTime = _ocur.CreateObject(Of GLClosingTime)()
            _GLClosingTime.DateFrom = _LiquidAssetsReturnHeader.DateFrom
            _GLClosingTime.DateTo = _LiquidAssetsReturnHeader.DateTo
            _GLClosingTime.GLClosingType = eGLClosingType.eCassa

            _LiquidAssetsReturnHeader.GLClosingTime = _GLClosingTime

            _GLCassa_Collection = New XPCollection(Of GLCassa)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                _ocur.Session, CriteriaOperator.Parse("LiquidAssets_Cassa.Oid=? and GetDate(DateExecution) >=? and GetDate(DateExecution) <=?", _
                                                                      _LiquidAssetsReturnHeader.LiquidAssets.Oid, _
                                                                      _LiquidAssetsReturnHeader.DateFrom.Date, _
                                                                      DateAdd(DateInterval.Minute, 1439, _LiquidAssetsReturnHeader.DateTo.Date)))

            RaiseEvent InitWork(1, 1, _GLCassa_Collection.Count)
            For Each _GLCassa In _GLCassa_Collection
                RaiseEvent DoWork()
                _GLCassa.GLClosingTime = _GLClosingTime
            Next

            '-- Most PDF-be ki kell tolni a pénztári jelentést és azt el kell menteni mint csatolt filet. ----------------------------------------------------------------
            Dim _XafReport As XafReport = _LiquidAssetsReturnHeader.LiquidAssets.ReportData_Return.LoadReport(_ocur)
            _XafReport.FilterString = "[LiquidAssetsReturnHeader.Oid]='" & _LiquidAssetsReturnHeader.Oid.ToString & "'"
            Dim _FileName As String = Path.GetTempPath & "Report.pdf"
            Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
            _XafReport.ExportToPdf(_StreamFile)

            _GLClosingTime.DocumentFile = New FileData(_ocur.Session)
            _GLClosingTime.DocumentFile.LoadFromStream("Report.pdf", _StreamFile) '_FileData
            _GLClosingTime.DocumentFile.FileName = "Report.pdf"
            '-------------------------------------------------------------------------------------------------------------------------------------------------------------
            _StreamFile.Close()

            _ocur.CommitChanges()
            RaiseEvent FinalWork
            View.Close()
        End If
    End Sub
    Private Sub aGLCassaCloseBack_Execute(ByVal sender As System.Object, ByVal e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aGLCassaCloseBack.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _GLCassa_Collection As New XPCollection(Of GLCassa)
        Dim _LiquidAssetsReturnHeader As LiquidAssetsReturnHeader = TryCast(View.SelectedObjects(0), LiquidAssetsReturnHeader)
        Dim _GLCassa As GLCassa

        _GLCassa_Collection = New XPCollection(Of GLCassa)(PersistentCriteriaEvaluationBehavior.InTransaction, _
                                _ocur.Session, CriteriaOperator.Parse("LiquidAssets_Cassa.Oid=? and GLClosingTime.Oid=?", _
                                                                      _LiquidAssetsReturnHeader.LiquidAssets.Oid, _
                                                                      _LiquidAssetsReturnHeader.GLClosingTime.Oid))

        RaiseEvent InitWork(1, 1, _GLCassa_Collection.Count)
        For Each _GLCassa In _GLCassa_Collection
            RaiseEvent DoWork()
            _GLCassa.GLClosingTime = Nothing
        Next
        _LiquidAssetsReturnHeader.GLClosingTime = Nothing
        _ocur.CommitChanges()
        RaiseEvent FinalWork
        Frame.GetController(Of RefreshController).RefreshAction.DoExecute()
    End Sub
    Private Sub LiquidAssetsReturnHeader_ListView_Cassa_PrintLiquidAssetsReturn_Cassa_Executing(sender As Object, e As CancelEventArgs)
        Dim CheckResult As Boolean = DenominationCheck()
        If Not CheckResult Then
            MsgBox("A pénztári pénzkészlet és a címletjegyzékben feltüntetett pénzkészlet értéke nem egyezik!")
        End If
    End Sub
    Private Sub LiquidAssetsReturnHeader_ListView_Cassa_GLCassaClose_Executing(sender As Object, e As CancelEventArgs)
        Dim CheckResult As Boolean = DenominationCheck()
        If Not CheckResult Then
            Throw New Exception("*A pénztári pénzkészlet és a címletjegyzékben feltüntetett pénzkészlet értéke nem egyezik!")
        End If
    End Sub
    Private Function DenominationCheck() As Boolean
        If Frame.GetController(Of ModificationsController).SaveAction.Enabled = True Then
            Frame.GetController(Of ModificationsController).SaveAction.DoExecute()
        End If

        Dim Valid As Boolean = False
        Dim _LARHSumValue As Double = 0
        Dim _uow As New UnitOfWork(TryCast(View.ObjectSpace, Xpo.XPObjectSpace).Session.DataLayer)
        Dim _LiquidAssetsReturnHeader As LiquidAssetsReturnHeader = _uow.GetObjectByKey(Of LiquidAssetsReturnHeader)(TryCast(View.SelectedObjects(0), LiquidAssetsReturnHeader).Oid)
        For Each _LiquidAssetsReturnDenomination As LiquidAssetsReturnDenomination In _LiquidAssetsReturnHeader.LiquidAssetsReturnDenomination
            _LARHSumValue += _LiquidAssetsReturnDenomination.SumValue
        Next
        If _LiquidAssetsReturnHeader.LiquidAssets.CurrencyName.ShortName = "HUF" Then
            If _LARHSumValue = _LiquidAssetsReturnHeader.BallanceToHUF Then
                Valid = True
            End If
        Else
            If _LARHSumValue = _LiquidAssetsReturnHeader.BallanceToDEV Then
                Valid = True
            End If
        End If

        Return Valid
    End Function
End Class
