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.Win.Editors
Imports DevExpress.ExpressApp.Editors
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraBars
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp.Reports
Imports System.IO
Imports DevExpress.XtraPrinting
Imports DevExpress.XtraReports.UI
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices


Public Class OfferOutVC
    Inherits SISBusiness.Module.OffersOutVC
    Dim _RichEditControl As RichEditControl
    Dim _GridListEditor As GridListEditor
    Dim _Grid As GridView
    Dim _OfferOutHeader As OfferOutHeader
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()

        Frame.GetController(Of OfferOutVC).aReplaceValueDocumentTemplate_OfferOut.Active.SetItemValue("", False)

        If View.Id = "OfferOutHeader_OfferOutRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", False)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", False)
        End If
        If View.Id Like "OrderInHeaderFromOfferOutRowsHeader_OrderInHeaderFromOfferOutRowsRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", False)
        End If
        If View.Id = "OfferOutHeader_DetailView" Then
            _OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
        End If
        If View.Id = "OffersOutParameters_DetailView" Then

        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "OfferOutHeader_OfferOutRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.SystemModule.ListViewProcessCurrentObjectController)().Active.SetItemValue("", True)
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", True)
        End If
        If View.Id Like "OrderInHeaderFromOfferOutRowsHeader_OrderInHeaderFromOfferOutRowsRows_ListView" Then
            Frame.GetController(Of DevExpress.ExpressApp.Win.SystemModule.OpenObjectController)().Active.SetItemValue("", True)
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "OfferOutHeader_DetailView" Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _RichEditContolViewItem As ViewItem = _DetailView.FindItem("DocumentBody")
            If _RichEditContolViewItem IsNot Nothing Then
                AddHandler _RichEditContolViewItem.ControlCreated, AddressOf RichEditContolViewItem_ControlCreated
            End If
        End If
        If View.Id = "OfferOutParameters_DetailView" Then
            Dim _DetailView As DetailView = TryCast(View, DetailView)
            Dim _RichEditContolViewItem As ViewItem = _DetailView.FindItem("DocumentBody")
            If _RichEditContolViewItem IsNot Nothing Then
                AddHandler _RichEditContolViewItem.ControlCreated, AddressOf RichEditContolViewItem_ControlCreated
            End If
        End If
        If View.Id Like "OrderInHeaderFromOfferOutRowsHeader_OrderInHeaderFromOfferOutRowsRows_ListView" Then
            Dim _ListView As DevExpress.ExpressApp.ListView = CType(View, DevExpress.ExpressApp.ListView)
            _GridListEditor = TryCast(_ListView.Editor, GridListEditor)
            _Grid = TryCast(_GridListEditor.GridView, GridView)

            AddHandler _Grid.GridControl.HandleCreated, AddressOf Grid_HandleCreated
        End If
    End Sub
    Private Sub CreatePDFAndSendToDefaultMailClient()
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)
        Dim _ReportData As ReportData = Nothing

        Dim _OutlookApp As New Microsoft.Office.Interop.Outlook.Application()
        Dim _OutlookNameSpace As Microsoft.Office.Interop.Outlook.NameSpace
        Dim _mail As Outlook.MailItem = Nothing
        Dim _mailRecipients As Outlook.Recipients = Nothing
        Dim _mailRecipient As Outlook.Recipient = Nothing
        Dim _mailAttachments As Outlook.Attachments = Nothing
        Dim _mailAttachment As Outlook.Attachment = Nothing
        _OutlookNameSpace = _OutlookApp.GetNamespace("MAPI")
        _OutlookNameSpace.Logon(, , False, True)

        If _OfferOutHeader IsNot Nothing Then
            If Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.SelectedItem IsNot Nothing Then
                If _OfferOutHeader.IsEditable = True Then
                    _OfferOutHeader.RecalculateOfferOut(_OfferOutHeader, _ocur)
                    _ocur.CommitChanges()
                End If
                If Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.SelectedItem.Data = "0" Then
                    _ReportData = _OfferOutHeader.OfferOutParameters.ReportData
                End If
                If Frame.GetController(Of OffersOutPrintVC).aOfferOutPrintChoice.SelectedItem.Data = "1" Then
                    _ReportData = _OfferOutHeader.OfferOutParameters.ReportData1
                End If
            Else
                Throw New Exception("*Nincs nyomtatvány típus kiválasztva!")
            End If

            If _ReportData IsNot Nothing Then
                Dim _XafReport As XafReport = _ReportData.LoadReport(_ocur)
                Dim _FilterString As String = "[OfferOutHeader.Oid]='" & _OfferOutHeader.Oid.ToString & "'"
                _XafReport.FilterString = _FilterString
                Dim _FileName As String = Path.GetTempPath & CorrectFileName(_OfferOutHeader.DocumentNumber) & ".pdf"
                Dim _StreamFile As New FileStream(_FileName, FileMode.Create)

                _XafReport.ExportToPdf(_StreamFile)
                _StreamFile.Close()

                Try
                    _mail = _OutlookApp.CreateItem(Outlook.OlItemType.olMailItem)
                    _mail.Subject = "ÁRAJÁNLAT"

                    _mailAttachments = _mail.Attachments
                    _mailAttachment = _mailAttachments.Add(_FileName)

                    _mailRecipients = _mail.Recipients
                    _mailRecipient = _mailRecipients.Add(_OfferOutHeader.Contact.Email)
                    _mailRecipient.Resolve()

                    If (_mailRecipient.Resolved) Then
                        '_mail.Send()
                        _mail.Display(True)
                    Else
                        System.Windows.Forms.MessageBox.Show(
                            "There is no such record in your address book.")
                    End If
                Catch ex As Exception
                    System.Windows.Forms.MessageBox.Show(ex.Message,
                        "An exception is occured in the code of add-in.")
                Finally
                    If Not IsNothing(_mailRecipient) Then Marshal.ReleaseComObject(_mailRecipient)
                    If Not IsNothing(_mailRecipients) Then Marshal.ReleaseComObject(_mailRecipients)
                    If Not IsNothing(_mail) Then Marshal.ReleaseComObject(_mail)
                End Try
            End If
        End If
    End Sub
    Private Sub RichEditContolViewItem_ControlCreated(sender As Object, e As EventArgs)
        Dim _ItemControl As Object = TryCast(sender, ViewItem).Control
        Dim _RichEditUserControl As RichEditUserControl = TryCast(_ItemControl, RichEditUserControl)
        Dim _BarItem As BarItemLink

        _RichEditControl = TryCast(_ItemControl, RichEditUserControl).RichEditControl
        _RichEditControl.BeginUpdate()

        Dim _OfferOutHeader_IList As IList(Of OfferOutRTF) = New List(Of OfferOutRTF)
        Dim _OfferOutRTF As New OfferOutRTF

        If _OfferOutHeader IsNot Nothing Then
            _OfferOutRTF.Sajátcég_Adószám = _OfferOutHeader.CustomersFrom.VATNumber
            _OfferOutRTF.Sajátcég_Adószám_EU = _OfferOutHeader.CustomersFrom.VATNumberEU
            If _OfferOutHeader.CustomersFrom.Address1 IsNot Nothing Then
                _OfferOutRTF.Sajátcég_Irányítószám = _OfferOutHeader.CustomersFrom.Address1.ZipPostal
                _OfferOutRTF.Sajátcég_Ország = _OfferOutHeader.CustomersFrom.Address1.Country.Name
                _OfferOutRTF.Sajátcég_Utca = _OfferOutHeader.CustomersFrom.Address1.Street
                _OfferOutRTF.Sajátcég_Város = _OfferOutHeader.CustomersFrom.Address1.City
            Else
                _OfferOutRTF.Sajátcég_Irányítószám = "Nincs beállítva"
                _OfferOutRTF.Sajátcég_Ország = "Nincs beállítva"
                _OfferOutRTF.Sajátcég_Utca = "Nincs beállítva"
                _OfferOutRTF.Sajátcég_Város = "Nincs beállítva"
            End If
            _OfferOutRTF.Sajátcég_Teljesnév = _OfferOutHeader.CustomersFrom.FullName


            _OfferOutRTF.Partner_Adószám = _OfferOutHeader.Customers.VATNumber
            _OfferOutRTF.Partner_Adószám_EU = _OfferOutHeader.Customers.VATNumberEU
            If _OfferOutHeader.Customers.Address1 IsNot Nothing Then
                _OfferOutRTF.Partner_Irányítószám = _OfferOutHeader.Customers.Address1.ZipPostal
                _OfferOutRTF.Partner_Ország = _OfferOutHeader.Customers.Address1.Country.Name
                _OfferOutRTF.Partner_Utca = _OfferOutHeader.Customers.Address1.Street
                _OfferOutRTF.Partner_Város = _OfferOutHeader.Customers.Address1.City
            Else
                _OfferOutRTF.Partner_Irányítószám = "Nincs beállítva"
                _OfferOutRTF.Partner_Ország = "Nincs beállítva"
                _OfferOutRTF.Partner_Utca = "Nincs beállítva"
                _OfferOutRTF.Partner_Város = "Nincs beállítva"
            End If
            _OfferOutRTF.Partner_Teljesnév = _OfferOutHeader.Customers.FullName

            If _OfferOutHeader.Contact IsNot Nothing Then
                _OfferOutRTF.Kapcsolat_Teljesnév = _OfferOutHeader.Contact.FullName
                If _OfferOutHeader.Contact.PhoneNumbers.Count > 0 Then
                    _OfferOutRTF.Kapcsolat_Mobil = _OfferOutHeader.Contact.PhoneNumbers(0).Number
                End If
                _OfferOutRTF.Kapcsolat_Email = _OfferOutHeader.Contact.Email
            End If

            _OfferOutRTF.Ajánlat_Dokumentum_Sorszám = _OfferOutHeader.DocumentNumber
        End If
        _OfferOutHeader_IList.Add(_OfferOutRTF)
        _RichEditControl.Options.MailMerge.DataSource = _OfferOutHeader_IList

        _RichEditControl.Update()
        _RichEditControl.UpdateCommandUI()
        _RichEditControl.EndUpdate()

        _RichEditControl.BeginUpdate()
        For Each _BarItem In _RichEditUserControl.MailingInsertItemLink.ItemLinks
            Select Case _BarItem.Item.Caption
                Case ""
            End Select
            _BarItem.Item.Caption = "Lófasz !"
        Next
        _RichEditControl.EndUpdate()

        'AddHandler _RichEditUserControl.RichEditControl.Options.MailMerge.

    End Sub

    Private Sub Grid_HandleCreated(sender As Object, e As System.EventArgs)
        RemoveHandler _Grid.GridControl.HandleCreated, AddressOf Grid_HandleCreated
        _GridListEditor.Grid.BeginInvoke(New Windows.Forms.MethodInvoker(AddressOf SelectAll))

    End Sub
    Private Sub SelectAll()
        _Grid.SelectAll()
    End Sub
    Private Sub aInsertGridToRTFCurrentPos_OfferOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aInsertGridToRTFCurrentPos_OfferOut.Execute
        Dim _DetailView As DetailView = CType(View, DetailView)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(e.SelectedObjects(0), OfferOutHeader)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        If _OfferOutHeader.DocumentBody Is Nothing Then
            _OfferOutHeader.DocumentBody = "{\rtf1\deff0{\fonttbl{\f0 Times New Roman;}}" & _
                        "{\colortbl\red0\green0\blue0 ;\red0\green0\blue255 ;}{\*\listoverridetable}{\stylesheet {\ql\cf0 Normal;}" & _
                        "{\*\cs1\cf0 Default Paragraph Font;}{\*\cs2\sbasedon1\cf0 Line Number;}{\*\cs3\ul\cf1 Hyperlink;}}\sectd\pard\plain" & _
                        "\ql\f1\par\pard\plain\ql\f1\par\pard\plain\ql\f1\par\pard\plain\ql" & _
                        "\f2\fs18\cf0\par\pard\plain\ql\f1\par}"
        End If
        _RichEditControl.BeginUpdate()

        _RichEditControl.Document.InsertText(_RichEditControl.Document.CaretPosition, "orderrows")
        _RichEditControl.EndUpdate()
        'Dim _RTFMergeFieldCleaner As New RTFMergeFieldCleaner
        '_OfferOutHeader.DocumentBody = _RTFMergeFieldCleaner.CleanDocument(_RichEditControl.Document.RtfText)

        Dim _a As String = _RichEditControl.Document.RtfText

        _OfferOutHeader.DocumentBody = _a
        '=========================== ColorTable =======================================
        Dim _actColorNum As Int32 = 0
        Dim _OrigColorTable As String
        Dim _ColorTable As String = (_OfferOutHeader.DocumentBody.Remove(0, (InStr(_OfferOutHeader.DocumentBody, "{\colortbl")) + "{\colortbl".Length - 1))

        _ColorTable = _ColorTable.Remove(InStr(_ColorTable, "}") - 1) '--Act Color table 
        Dim _ColorArray As String() = _ColorTable.Split(New [Char]() {";"c})
        Dim _CTable As New StringBuilder()
        _CTable.Append("{\colortbl")
        For _ik As Int32 = 0 To _ColorArray.Length - 2
            _CTable.Append(_ColorArray(_ik) & ";")
        Next
        _CTable.Append("\red240\green240\blue240 ;}")
        _OrigColorTable = "{\colortbl" & _ColorTable & "}"
        _OfferOutHeader.DocumentBody = _OfferOutHeader.DocumentBody.Replace(_OrigColorTable, _CTable.ToString())

        '=================================================================================

        Dim _ListPropertyEditor As ListPropertyEditor = TryCast(View, DetailView).FindItem("OfferOutRows")
        Dim _ListView As DevExpress.ExpressApp.ListView = CType(_ListPropertyEditor.ListView, DevExpress.ExpressApp.ListView)
        If _ListView IsNot Nothing Then
            Dim _GridListEditor As GridListEditor = TryCast(_ListView.Editor, GridListEditor)
            Dim _Grid As GridView = TryCast(_GridListEditor.GridView, GridView)

            If _Grid IsNot Nothing Then
                _OfferOutHeader.DocumentBody = RTFTableCreator(_Grid, "orderrows", _OfferOutHeader.DocumentBody, _RGB:="241,241,241")
            End If
        End If
        '=================================================================================
    End Sub
    Function RTFTableCreator(_Grid As GridView, _Tag As String, _DocumentBody As String, Optional _FontSize As String = "6", _
                         Optional _FontType As String = "Tahoma", Optional _RGB As String = "240,240,240") As String
        Dim _DocumentBody_NEW As String
        Dim _HeaderFillColor As Int32 ' -- Current table header fill color index in colot table

        If _RGB <> "240,240,240" Then
            Dim _Parameters As String() = ColorTablereplacement(_DocumentBody, _RGB)
            _HeaderFillColor = Convert.ToInt32(_Parameters(0))
            _DocumentBody = _Parameters(1)
        Else
            _HeaderFillColor = 2
        End If

        If _Grid.DataRowCount > 0 Then
            Dim _FontSizeNUM As Int32 = (Convert.ToInt32(_FontSize) * 2) ' -- Font size in half-points (the default is 16). => Size 8!!!
            Dim _RTFtext As New StringBuilder
            Dim _RowNumFull As Integer = _Grid.RowCount
            Dim _RowNum As Integer = _Grid.DataRowCount
            Dim _ColumnsNum As Integer = _Grid.Columns.Count
            Dim irowIndex As Int32 = 0
            Dim _LastCellIndex As Int32 = 0

            '--Header to RTF format--
            '=================================================================================
            _RTFtext.Append("\pard\plain\ql\par\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10" & _
                            "\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")

            For _ik As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                _RTFtext.Append("\clvertalt\clcbpat2\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10" &
                                    "\cltxlrtb\clftsWidth3\clwWidth3302\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _ik)
                If _LastCellIndex < _ik Then
                    _LastCellIndex = _ik
                End If
            Next
            For _ik As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                _RTFtext.Append("\pard\plain\ql\intbl{\b\fs" & _FontSizeNUM & "\cf0 " & _Grid.Columns.View.VisibleColumns(_ik).Caption & "}\b\cell")
            Next

            _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10" & _
                            "\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")
            For _ik As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                _RTFtext.Append("\clvertalt\clcbpat" & _HeaderFillColor & "\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10" &
                                    "\cltxlrtb\clftsWidth3\clwWidth3302\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _ik)
            Next
            irowIndex += 1
            '' --Data to RTF format--
            '=================================================================================
            For _ik As Int16 = 0 To _RowNumFull - 2
                If _Grid.IsGroupRow(_Grid.Columns.View.GetVisibleRowHandle(_ik)) = True Then
                    _RTFtext.Append("\row")

                    _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.GetGroupRowDisplayText(_Grid.Columns.View.GetVisibleRowHandle(_ik)).ToString) & "}\cell")


                    _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                    "\trleft-108\trftsWidth1\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108\tblindtype3\tblind0")

                    _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth9619" & _
                                             "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _LastCellIndex)

                    irowIndex += 1
                Else
                    _RTFtext.Append("\row")

                    For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                        If _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Double" And _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Boolean" Then

                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.GetRowCellDisplayText(_Grid.Columns.View.GetVisibleRowHandle(_ik), _Grid.Columns.View.VisibleColumns(_jk))) & "}\cell")
                        ElseIf _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName = "System.Boolean" Then
                            If _Grid.GetRowCellValue(_Grid.Columns.View.GetVisibleRowHandle(_ik), _Grid.Columns.View.VisibleColumns(_jk)) = True Then
                                _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Van" & "}\cell")
                            Else
                                _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Nincs" & "}\cell")
                            End If
                        Else
                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0\qr " & LTrim(RTrim(Format(_Grid.Columns.View.GetRowCellValue(_Grid.Columns.View.GetVisibleRowHandle(_ik), _Grid.Columns.View.VisibleColumns(_jk)), "#,##0.00"))) & "}\qr\cell")

                        End If
                    Next

                    _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                    "\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")

                    For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1

                        _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth3302" & _
                                        "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _jk)
                    Next
                    irowIndex += 1
                End If
            Next
            '--Last row --
            '=================================================================================
            If _Grid.IsGroupRow(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1)) = True Then
                _RTFtext.Append("\row")
                _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.GetGroupRowDisplayText(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1)).ToString) & "}\cell")

                _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                "\trleft-108\trftsWidth1\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108\tblindtype3\tblind0")

                _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth9619" & _
                                         "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _LastCellIndex)
            Else
                _RTFtext.Append("\row")
                For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1
                    If _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Double" And _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName <> "System.Boolean" Then

                        _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & (_Grid.Columns.View.GetRowCellDisplayText(_RowNum - 1, _Grid.Columns.View.VisibleColumns(_jk))) & "}\cell")
                    ElseIf _Grid.Columns.View.VisibleColumns(_jk).ColumnType.FullName = "System.Boolean" Then
                        If _Grid.IsGroupRow(_Grid.Columns.View.GetVisibleRowHandle(_RowNum - 1)) = True Then

                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Van" & "}\cell")
                        Else
                            _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0 " & "Nincs" & "}\cell")
                        End If
                    Else
                        _RTFtext.Append("\pard\plain\ql\intbl{\fs" & _FontSizeNUM & "\cf0\qr " & LTrim(RTrim(Format(_Grid.Columns.View.GetRowCellValue(_Grid.Columns.View.GetVisibleRowHandle(_RowNumFull - 1), _Grid.Columns.View.VisibleColumns(_jk)), "#,##0.00"))) & "}\qr\cell")
                    End If
                Next

                _RTFtext.Append("\trowd\irow" & irowIndex & "\irowband" & irowIndex & "\lastrow\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10" & _
                                   "\trleft-108\trautofit1\trpaddfl3\trpaddl108\trpaddfr3\trpaddr108")
                ''---
                For _jk As Integer = 0 To _Grid.Columns.View.VisibleColumns.Count - 1

                    _RTFtext.Append("\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth3302" & _
                                    "\clpadfr3\clpadr108\clpadft3\clpadt108\cellx" & _jk)
                Next
            End If

            _RTFtext.Append("\row\pard\plain\ql")
            _DocumentBody_NEW = _DocumentBody.Replace(_Tag, _RTFtext.ToString())
            If _DocumentBody_NEW.Contains("\fonttbl{\f0 Times New Roman;") Then
                _DocumentBody_NEW = _DocumentBody_NEW.Replace("\fonttbl{\f0 Times New Roman;", "\fonttbl{\f0 " & _FontType & ";") 'Font table remake
            End If
        Else
            _DocumentBody_NEW = _DocumentBody.Replace(_Tag, " ")
        End If

        Return _DocumentBody_NEW
    End Function
    Function ColorTablereplacement(_DocumentBody As String, _RGB As String) As String()
        Dim _Parameters(1) As String
        Dim _RGBArray As String() = _RGB.Split(New [Char]() {","c})

        Dim _actColorNum As Int32 = -1
        Dim _ColorTable As String = (_DocumentBody.Remove(0, (InStr(_DocumentBody, "{\colortbl")) + "{\colortbl".Length - 1))
        _ColorTable = _ColorTable.Remove(InStr(_ColorTable, "}") - 1) '--Act Color table 
        Dim _ColorArray As String() = _ColorTable.Split(New [Char]() {";"c})

        For _ik As Int32 = 0 To _ColorArray.Length - 1
            If _ColorArray(_ik) = "\red" & _RGBArray(0) & "\green" & _RGBArray(1) & "\blue" & _RGBArray(2) & " " Then
                _actColorNum = _ik
            End If
        Next
        If _actColorNum = -1 Then
            Dim _CTable As New StringBuilder()
            _CTable.Append("{\colortbl")
            For _ik As Int32 = 0 To _ColorArray.Length - 2
                _CTable.Append(_ColorArray(_ik) & ";")
            Next
            _CTable.Append("\red" & _RGBArray(0) & "\green" & _RGBArray(1) & "\blue" & _RGBArray(2) & " ;}")
            Dim _OrigColorTable As String = "{\colortbl" & _ColorTable & "}"
            _DocumentBody = _DocumentBody.Replace(_OrigColorTable, _CTable.ToString())
            _Parameters(0) = _ColorArray.Length - 1
            _Parameters(1) = _DocumentBody
        Else
            _Parameters(0) = _actColorNum
            _Parameters(1) = _DocumentBody
        End If
        Return _Parameters
    End Function

    Private Sub aRestoreDocumentTemplate_OfferOut_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aRestoreDocumentTemplate_OfferOut.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _OfferOutHeader As OfferOutHeader = TryCast(View.SelectedObjects(0), OfferOutHeader)

        _OfferOutHeader.DocumentBody = _OfferOutHeader.OfferOutParameters.DocumentBody

    End Sub

    Private Sub aGeneratePDFandSendDefaultMailClient_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aGeneratePDFandSendDefaultMailClient.Execute
        Me.CreatePDFAndSendToDefaultMailClient()
    End Sub
End Class
