Imports System
Imports System.Configuration
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
Imports DevExpress.Persistent.BaseImpl
Imports SISBusiness.Module
Imports System.IO
Imports System.Windows.Forms
Imports DevExpress.Data.Filtering
Imports System.Threading
Imports System.Timers

Public Class SISService
    Private _Session As DevExpress.Xpo.Session
    Private _connectionstring As String
    Private _instancename As String
    Private _Timer As System.Timers.Timer
    Private _ReconfigurePCI As String
    Private _AssetsHandling As String
    Private _Period As Integer
    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        'EventLog1.WriteEntry("In OnStart")
        _instancename = ConfigurationManager.AppSettings("InstanceName")
        Diagnostics.EventLog.WriteEntry(_instancename, "SIS Business Framework service is ready for work !", EventLogEntryType.Information)
        _connectionstring = ConfigurationManager.ConnectionStrings("ConnectionString").ToString

        If _connectionstring <> "" Then
            If _Session Is Nothing Then _Session = New Session
            _Session.ConnectionString = _connectionstring
            _Session.Connect()
            If _Session.IsConnected Then
                Diagnostics.EventLog.WriteEntry(_instancename, "Database connection is succeed ! " & _Session.ConnectionString, EventLogEntryType.Information)
                _ReconfigurePCI = ConfigurationManager.AppSettings("ReconfigurePCI")
                _AssetsHandling = ConfigurationManager.AppSettings("AssetsHandling")
                _Period = CInt(ConfigurationManager.AppSettings("Period"))
                If _Period < 1000 Then _Period = 1000
                _Timer = New System.Timers.Timer(_Period)
                AddHandler _Timer.Elapsed, AddressOf SISServiceFire_Timer
                _Timer.Interval = _Period
                _Timer.Enabled = True
                Diagnostics.EventLog.WriteEntry(_instancename, "Timer period is initialized : " & _Period.ToString, EventLogEntryType.Information)
            Else
                Diagnostics.EventLog.WriteEntry(_instancename, "Database connection is failed ! " & _connectionstring, EventLogEntryType.Error)
            End If
        Else
            Diagnostics.EventLog.WriteEntry(_instancename, "Database connection is failed ! " & _connectionstring, EventLogEntryType.Error)
        End If

    End Sub
    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
        Diagnostics.EventLog.WriteEntry(_instancename, "SIS Business Framework service is stopped !", EventLogEntryType.Information)
    End Sub
    Public Sub New()
        MyBase.New()
        InitializeComponent()
        Me.ServiceName = ConfigurationManager.AppSettings.Item("InstanceName")
    End Sub

    Sub SISServiceFire_Timer(source As Object, e As ElapsedEventArgs)
        Dim _currenttime As Date = Now
        On Error GoTo CheckError
        _Timer.Enabled = False

        '// Ide kéne tenni, hogy mit csináljon ...
        If _Session IsNot Nothing Then
            If _currenttime.Hour = CLng(_ReconfigurePCI.Split(":")(0)) And _
               _currenttime.Minute = CLng(_ReconfigurePCI.Split(":")(1)) Then
                ReconfigurePCI()
            End If
        End If
        If _Session IsNot Nothing Then
            If _currenttime.Hour = CLng(_AssetsHandling.Split(":")(0)) And _
               _currenttime.Minute = CLng(_AssetsHandling.Split(":")(1)) Then
                AssetsHandling()
            End If
        End If

        _Timer.Enabled = True

        Exit Sub
CheckError:
        Diagnostics.EventLog.WriteEntry(_instancename, String.Format("Timer error : {0}{1}{2}", Chr(13), Chr(10), Err.Description), EventLogEntryType.Error)
        Resume Next
    End Sub
    Private Sub ReconfigurePCI()
        '// Partner pontozási info újragenerálása

        Dim _uow As New UnitOfWork(_Session.DataLayer)
        Dim _GLAccounts_Collection As New XPCollection(Of GLAccounts)(_uow)
        Dim _GLAccounts As GLAccounts
        Dim _GLRows As GLRows
        Dim _InvoiceHeader As InvoiceHeader
        Dim _RegistryHeader As RegistryHeader

        Dim _CurrentPCIInfo As String = ""
        On Error GoTo CheckError

        Diagnostics.EventLog.WriteEntry(_instancename, "ReconfigurePCI is started.", EventLogEntryType.Information)

        Dim _GLRows_Collection_ConnectInfo As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("isnull(ConnectInfo)=True"))
        Diagnostics.EventLog.WriteEntry(_instancename, "ConnectInfo check -> " & _GLRows_Collection_ConnectInfo.Count.ToString, EventLogEntryType.Information)
        For Each _GLRows In _GLRows_Collection_ConnectInfo
            _GLRows.ConnectInfo = ""
            _GLRows.Save()
        Next
        _uow.CommitChanges()

        Diagnostics.EventLog.WriteEntry(_instancename, "GLAccounts -> " & _GLAccounts_Collection.Count.ToString, EventLogEntryType.Information)
        For Each _GLAccounts In _GLAccounts_Collection
            _CurrentPCIInfo = _GLAccounts.ConnectInfo
            GLFunctions.ReconfigurePCI(_uow, _GLAccounts.CustomersFrom, _GLAccounts.Customers, _GLAccounts.PartnerType, _GLAccounts.ConnectInfo)
        Next

        Dim _GLRows_Collection As New XPCollection(Of GLRows)(_uow, CriteriaOperator.Parse("isnull(Customer)=False and PartnerType in (0,1)"))
        Diagnostics.EventLog.WriteEntry(_instancename, "GLRows -> " & _GLRows_Collection.Count.ToString, EventLogEntryType.Information)
        For Each _GLRows In _GLRows_Collection
            _CurrentPCIInfo = _GLRows.ConnectInfo
            GLFunctions.ReconfigurePCI(_uow, _GLRows.GLHeader.CustomersFrom, _GLRows.Customer, _GLRows.PartnerType, _GLRows.ConnectInfo)
        Next

        Dim _InvoiceHeader_Collection As New XPCollection(Of InvoiceHeader)(_uow, CriteriaOperator.Parse("isnull(Customers)=False and IsEditable=False and isnull(GLAccounts)=True"))
        Diagnostics.EventLog.WriteEntry(_instancename, "InvoiceHeader -> " & _InvoiceHeader_Collection.Count.ToString, EventLogEntryType.Information)
        For Each _InvoiceHeader In _InvoiceHeader_Collection
            _CurrentPCIInfo = _InvoiceHeader.ConnectInfo
            GLFunctions.ReconfigurePCI(_uow, _InvoiceHeader.CustomersFrom, _InvoiceHeader.Customers, 0, _InvoiceHeader.ConnectInfo)
        Next

        Dim _RegistryHeader_Collection As New XPCollection(Of RegistryHeader)(_uow, CriteriaOperator.Parse("RegistryAcceptState=0 and RegistryType.RegistryMainType=0 and IsEditable=False and isnull(F_GLAccounts)=True"))
        Diagnostics.EventLog.WriteEntry(_instancename, "RegistryHeader -> " & _RegistryHeader_Collection.Count.ToString, EventLogEntryType.Information)
        For Each _RegistryHeader In _RegistryHeader_Collection
            _CurrentPCIInfo = _RegistryHeader.DocumentNumber
            GLFunctions.ReconfigurePCI(_uow, _RegistryHeader.CustomersFrom, _RegistryHeader.Customers, 1, _RegistryHeader.DocumentNumber)
        Next

        Diagnostics.EventLog.WriteEntry(_instancename, "ReconfigurePCI is finished.", EventLogEntryType.Information)
        Exit Sub
CheckError:
        Diagnostics.EventLog.WriteEntry(_instancename, String.Format("ReconfigurePCI Error : {0}{1}{2}{3}{4}ConnectInfo -> {5}", Chr(13), Chr(10), Err.Description, Chr(13), Chr(10), _CurrentPCIInfo), EventLogEntryType.Error)
        Resume Next

    End Sub
    Private Sub AssetsHandling()
        On Error GoTo CheckError

        Exit Sub

CheckError:
        Diagnostics.EventLog.WriteEntry(_instancename, String.Format("PCI Error : {0}{1}{2}", Chr(13), Chr(10), Err.Description), EventLogEntryType.Error)
        Resume Next
    End Sub

    
End Class
