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 SISBusiness.Module
Imports FarPoint.Win.Spread
Imports FarPoint.PDF
Imports System.IO
Imports System.Web.HttpResponse
Imports System.Web.UI.Page
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Web.ASPxClasses.Internal
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.ExpressApp.Web.SystemModule
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.Reports

Public Class WorkSheet_VC
    Inherits SISBusiness.Module.WorkSheet_VC
    Private _FpSpread As FpSpread
    Private _FpPdf As FarPoint.Win.Spread.PrintInfo
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)

    End Sub

    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of WorkSheet_VC).aCloseWorkSheet_Subcontractor.Active.SetItemValue("", False)
        Frame.GetController(Of WorkSheet_VC).aOpenWorkSheet_Subcontractor.Active.SetItemValue("", False)

        If View.Id = "WorkSheet_Header_ListView_Subcontractor" Then
            Frame.GetController(Of WorkSheet_VC).aCloseWorkSheet_Subcontractor.Active.SetItemValue("", True)
            Frame.GetController(Of WorkSheet_VC).aOpenWorkSheet_Subcontractor.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", False)

        End If
        If View.Id = "WorkSheet_Header_ListView_Subcontractor_Closed" Then
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", False)
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", False)
        End If

    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "WorkSheet_Header_ListView_Subcontractor" Then
            Frame.GetController(Of WorkSheet_VC).aCloseWorkSheet_Subcontractor.Active.SetItemValue("", False)
            Frame.GetController(Of WorkSheet_VC).aOpenWorkSheet_Subcontractor.Active.SetItemValue("", False)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", True)
        End If
        If View.Id = "WorkSheet_Header_ListView_Subcontractor_Closed" Then

            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
            Frame.GetController(Of ListViewController).EditAction.Active.SetItemValue("", True)
            Frame.GetController(Of ListViewProcessCurrentObjectController).Active.SetItemValue("", True)
        End If
    End Sub

    Protected Overrides Sub aOpenWorkSheet_Subcontractor_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs)
        MyBase.aOpenWorkSheet_Subcontractor_CustomizePopupWindowParams(sender, e)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _User As User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
        Dim _SubcontractorCustomers As Customers = _Contacts.Customers
        Dim _OpenWorkSheet_SubcontractorPopUp As OpenWorkSheet_SubcontractorPopUp = _onew.CreateObject(Of OpenWorkSheet_SubcontractorPopUp)()
        Dim _SubContractor As Subcontractor = _onew.FindObject(Of Subcontractor)(CriteriaOperator.Parse("Customers.Oid=?", _SubcontractorCustomers.Oid))

        _OpenWorkSheet_SubcontractorPopUp.Subcontractor = _SubContractor
        e.View = Application.CreateDetailView(_onew, "OpenWorkSheet_SubcontractorPopUp_DetailView", False, _OpenWorkSheet_SubcontractorPopUp)
    End Sub
    Protected Overrides Sub aOpenWorkSheet_Subcontractor_Execute(sender As Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs)
        MyBase.aOpenWorkSheet_Subcontractor_Execute(sender, e)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OpenWorkSheet_SubcontractorPopUp As OpenWorkSheet_SubcontractorPopUp = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), OpenWorkSheet_SubcontractorPopUp))
        Dim _Subcontractor As Subcontractor = _ocur.GetObjectByKey(Of Subcontractor)(_OpenWorkSheet_SubcontractorPopUp.Subcontractor.Oid)
        Dim _User As User = _ocur.GetObjectByKey(Of User)(_ocur.GetKeyValue(SecuritySystem.CurrentUser))
        Dim _Contacts As Contacts = _ocur.FindObject(Of Contacts)(CriteriaOperator.Parse("User=?", _User))
        Dim _SubcontractorCustomers As Customers = _Contacts.Customers
        Dim _ReportData As ReportData


        Dim _WorkSheet_Header As WorkSheet_Header = _ocur.CreateObject(Of WorkSheet_Header)()
        _WorkSheet_Header.Customers = _ocur.GetObjectByKey(Of Customers)(_OpenWorkSheet_SubcontractorPopUp.WorkPlace.Customers.Oid)
        _WorkSheet_Header.CustomersFrom = _ocur.GetObjectByKey(Of CustomersFrom)(_OpenWorkSheet_SubcontractorPopUp.CustomersFrom.Oid)
        _WorkSheet_Header.WorkSheetType = _ocur.GetObjectByKey(Of WorkSheetType)(_OpenWorkSheet_SubcontractorPopUp.WorkSheetType.Oid)
        _WorkSheet_Header.DateExecution = _OpenWorkSheet_SubcontractorPopUp.DateCreated
        _WorkSheet_Header.DateCreated = GetSQLTime(_ocur.Session)
        _WorkSheet_Header.IsSubcontractor = True
        _WorkSheet_Header.SubcontractorCustomers = _SubcontractorCustomers
        _WorkSheet_Header.Description = _OpenWorkSheet_SubcontractorPopUp.Description
        _WorkSheet_Header.WorkPlace = _ocur.GetObjectByKey(Of WorkPlace)(_OpenWorkSheet_SubcontractorPopUp.WorkPlace.Oid)
        _WorkSheet_Header.WorkSheetState = eWorkSheetState.eOpened
        _WorkSheet_Header.CurrencyName = _ocur.FindObject(Of CurrencyName)(CriteriaOperator.Parse("ShortName='HUF'"))
        _WorkSheet_Header.WorkHour = _OpenWorkSheet_SubcontractorPopUp.WorkHour
        _WorkSheet_Header.WorkDescription = _OpenWorkSheet_SubcontractorPopUp.WorkDescription
        _WorkSheet_Header.WorkUsedMaterialDescription = _OpenWorkSheet_SubcontractorPopUp.WorkUsedMaterialDescription
        _WorkSheet_Header.WorkDescriptionOther = _OpenWorkSheet_SubcontractorPopUp.WorkDescriptionOther
        _WorkSheet_Header.WorkTravelKm = _OpenWorkSheet_SubcontractorPopUp.WorkTravelKm
        _WorkSheet_Header.InvoiceState = _OpenWorkSheet_SubcontractorPopUp.InvoiceState


        _ocur.CommitChanges()
        _ReportData = _ocur.FindObject(Of ReportData)(CriteriaOperator.Parse("Oid=?", _WorkSheet_Header.WorkSheetType.ReportData.Oid), True)
        If _ReportData IsNot Nothing Then
            Dim _XafReport As XafReport = _ReportData.LoadReport(_ocur)

            Dim _Param_Colletion As DevExpress.XtraReports.Parameters.ParameterCollection = _XafReport.Parameters

            For Each _Param As DevExpress.XtraReports.Parameters.Parameter In _Param_Colletion
                If _Param.Name = "Workplace" Then
                    Dim _SubcontractorCustomers_WorkPLace As SubcontractorCustomers = _ocur.FindObject(Of SubcontractorCustomers) _
                                                                            (CriteriaOperator.Parse("Subcontractor=? AND Customers=?", _Subcontractor, _WorkSheet_Header.Customers))
                    If _SubcontractorCustomers_WorkPLace.Workplace Is Nothing Then
                        _Param.Value = "Nincs megadva!"
                    Else
                        _Param.Value = _SubcontractorCustomers_WorkPLace.Workplace.ShortName
                    End If
                End If
            Next

            Dim _FilterString As String = "[Oid]='" & _WorkSheet_Header.Oid.ToString & "'"
            _XafReport.FilterString = _FilterString
            Dim _FileName As String = Path.GetTempPath & _WorkSheet_Header.DocumentNumber & "_Munkalap.pdf"
            Dim _StreamFile As New FileStream(_FileName, FileMode.Create)

            _XafReport.ExportToPdf(_StreamFile)
            _StreamFile.Close()

            Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)

            _WorkSheet_Header.WorkSheetFile_Original = New FileData(_ocur.Session)
            _WorkSheet_Header.WorkSheetFile_Original.LoadFromStream(_WorkSheet_Header.DocumentNumber + "_Munkalap.pdf", _StreamFile2)
            _StreamFile2.Close()
            Process.Start(_FileName)
        End If
        _ocur.CommitChanges()

        'Frame.GetController(Of Reports.ReportServiceController).ShowPreview(_ReportData, CriteriaOperator.Parse("Oid=?", _WorkSheet_Header.Oid), True)

        View.Close()
    End Sub

    Protected Overrides Sub aCloseWorkSheet_Subcontractor_CustomizePopupWindowParams(sender As Object, e As DevExpress.ExpressApp.Actions.CustomizePopupWindowParamsEventArgs)
        MyBase.aCloseWorkSheet_Subcontractor_CustomizePopupWindowParams(sender, e)
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _CloseWorkSheet_SubcontractorpopUp As CloseWorkSheet_SubcontractorpopUp = _onew.CreateObject(Of CloseWorkSheet_SubcontractorpopUp)()

        e.View = Application.CreateDetailView(_onew, "CloseWorkSheet_SubcontractorpopUp_DetailView", False, _CloseWorkSheet_SubcontractorpopUp)

    End Sub
    Protected Overrides Sub aCloseWorkSheet_Subcontractor_Execute(sender As Object, e As DevExpress.ExpressApp.Actions.PopupWindowShowActionExecuteEventArgs)
        MyBase.aCloseWorkSheet_Subcontractor_Execute(sender, e)
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _CloseWorkSheet_SubcontractorpopUp As CloseWorkSheet_SubcontractorpopUp = _ocur.GetObject(TryCast(e.PopupWindow.View.SelectedObjects(0), CloseWorkSheet_SubcontractorpopUp))
            If _CloseWorkSheet_SubcontractorpopUp.WorkSheetFile Is Nothing Then Throw New System.Exception("Lezáráshooz kötelező munkalapot csatolni!")
            Dim _WorkSheet_Header As WorkSheet_Header = TryCast(View.SelectedObjects(0), WorkSheet_Header)

            _WorkSheet_Header.WorkSheetState = eWorkSheetState.eReadyforClose
            _WorkSheet_Header.Description = _CloseWorkSheet_SubcontractorpopUp.Description
            _WorkSheet_Header.DateClosed = _CloseWorkSheet_SubcontractorpopUp.DateClosed

            Dim _FileName As String = Path.GetTempPath & _CloseWorkSheet_SubcontractorpopUp.WorkSheetFile.FileName
            Dim _StreamFile As New FileStream(_FileName, FileMode.Create)
            _CloseWorkSheet_SubcontractorpopUp.WorkSheetFile.SaveToStream(_StreamFile)
            _StreamFile.Close()
            Dim _StreamFile2 As New FileStream(_FileName, FileMode.Open)
            _WorkSheet_Header.WorkSheetFile = New FileData(_ocur.Session)
            _WorkSheet_Header.WorkSheetFile.LoadFromStream(_CloseWorkSheet_SubcontractorpopUp.WorkSheetFile.FileName, _StreamFile2)
            _WorkSheet_Header.Save()
            _StreamFile2.Close()

            If _CloseWorkSheet_SubcontractorpopUp.SubcontractorInvoice IsNot Nothing Then
                _FileName = Path.GetTempPath & _CloseWorkSheet_SubcontractorpopUp.SubcontractorInvoice.FileName
                _StreamFile = New FileStream(_FileName, FileMode.Create)
                _CloseWorkSheet_SubcontractorpopUp.SubcontractorInvoice.SaveToStream(_StreamFile)
                _StreamFile.Close()
                _StreamFile2 = New FileStream(_FileName, FileMode.Open)
                _WorkSheet_Header.SubcontractorInvoice = New FileData(_ocur.Session)
                _WorkSheet_Header.SubcontractorInvoice.LoadFromStream(_CloseWorkSheet_SubcontractorpopUp.SubcontractorInvoice.FileName, _StreamFile2)
                _WorkSheet_Header.Save()
                _StreamFile2.Close()
            End If

            _ocur.CommitChanges()
        Catch ext As System.Exception
            MsgBox(ext.Message)
        End Try
    End Sub

    Private Sub SetDatatoFpSpread(_WorkSheet_Header As WorkSheet_Header, xPCollection As DevExpress.Xpo.XPCollection, _Subcontractor As Subcontractor, _Customers As Customers, _ocur As Xpo.XPObjectSpace)

        Dim _Type As System.Type = GetType(WorkSheet_Header)
        Dim _TypeSubcontractorCustomers As System.Type = GetType(SubcontractorCustomers)
        Dim _TypeCustomersFrom As System.Type = GetType(CustomersFrom)
        Dim _PropertyInfo As System.Reflection.PropertyInfo() = _Type.GetProperties
        Dim _PropertyInfoSubcontractorCustomers As System.Reflection.PropertyInfo() = _TypeSubcontractorCustomers.GetProperties
        Dim _PropertyInfoCustomersFrom As System.Reflection.PropertyInfo() = _TypeCustomersFrom.GetProperties

        For Each _WorkSheetTypeExcelParam As WorkSheetTypeExcelParam In xPCollection
            For Each _PropertyInfo_ACT As System.Reflection.PropertyInfo In _PropertyInfo
                If _WorkSheetTypeExcelParam.PropertiesInfo = _PropertyInfo_ACT.Name Then
                    Select Case _PropertyInfo_ACT.Name
                        Case "DocumentNumber"
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Locked = True
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).TabStop = False
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).ResetText()
                            If _WorkSheet_Header.DocumentNumber IsNot Nothing Or _WorkSheet_Header.DocumentNumber <> "" Then
                                _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Text = _WorkSheet_Header.DocumentNumber.ToString
                            Else
                                _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Text = ""
                            End If
                            Exit For
                        Case "CustomersFrom"
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Locked = True
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).TabStop = False
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).ResetText()
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Text = _WorkSheet_Header.CustomersFrom.FullName.ToString
                            Exit For
                        Case "DateExecution"
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Locked = True
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).TabStop = False
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).ResetText()
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Text = _WorkSheet_Header.DateExecution.ToString
                            Exit For
                    End Select
                End If
            Next
        Next
        For Each _WorkSheetTypeExcelParam As WorkSheetTypeExcelParam In xPCollection
            For Each _PropertyInfo_ACT As System.Reflection.PropertyInfo In _PropertyInfoCustomersFrom
                If _WorkSheetTypeExcelParam.PropertiesInfo = _PropertyInfo_ACT.Name Then
                    Select Case _PropertyInfo_ACT.Name
                        Case "Address1"
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Locked = True
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).TabStop = False
                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).ResetText()

                            _FpSpread.ActiveSheet.Cells(_WorkSheetTypeExcelParam.XLSCellAdress).Text = _
                                _WorkSheet_Header.CustomersFrom.Address1.ToString()

                            Exit For
                    End Select
                End If
            Next
        Next
    End Sub
    Public Function StreamToMemory(ByVal input As Stream) As IO.MemoryStream
        Dim _buffer(1023) As Byte
        Dim _count As Integer = 1024
        Dim _output As MemoryStream

        If input.CanSeek Then
            _output = New MemoryStream(input.Length)
        Else
            _output = New MemoryStream
        End If
        Do
            _count = input.Read(_buffer, 0, _count)
            If _count = 0 Then Exit Do
            _output.Write(_buffer, 0, _count)
        Loop
        _output.Position = 0

        Return _output
    End Function

End Class


